You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IntListTest.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright (C) 2008, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.util;
  44. import junit.framework.TestCase;
  45. public class IntListTest extends TestCase {
  46. public void testEmpty_DefaultCapacity() {
  47. final IntList i = new IntList();
  48. assertEquals(0, i.size());
  49. try {
  50. i.get(0);
  51. fail("Accepted 0 index on empty list");
  52. } catch (ArrayIndexOutOfBoundsException e) {
  53. assertTrue(true);
  54. }
  55. }
  56. public void testEmpty_SpecificCapacity() {
  57. final IntList i = new IntList(5);
  58. assertEquals(0, i.size());
  59. try {
  60. i.get(0);
  61. fail("Accepted 0 index on empty list");
  62. } catch (ArrayIndexOutOfBoundsException e) {
  63. assertTrue(true);
  64. }
  65. }
  66. public void testAdd_SmallGroup() {
  67. final IntList i = new IntList();
  68. final int n = 5;
  69. for (int v = 0; v < n; v++)
  70. i.add(10 + v);
  71. assertEquals(n, i.size());
  72. for (int v = 0; v < n; v++)
  73. assertEquals(10 + v, i.get(v));
  74. try {
  75. i.get(n);
  76. fail("Accepted out of bound index on list");
  77. } catch (ArrayIndexOutOfBoundsException e) {
  78. assertTrue(true);
  79. }
  80. }
  81. public void testAdd_ZeroCapacity() {
  82. final IntList i = new IntList(0);
  83. assertEquals(0, i.size());
  84. i.add(1);
  85. assertEquals(1, i.get(0));
  86. }
  87. public void testAdd_LargeGroup() {
  88. final IntList i = new IntList();
  89. final int n = 500;
  90. for (int v = 0; v < n; v++)
  91. i.add(10 + v);
  92. assertEquals(n, i.size());
  93. for (int v = 0; v < n; v++)
  94. assertEquals(10 + v, i.get(v));
  95. try {
  96. i.get(n);
  97. fail("Accepted out of bound index on list");
  98. } catch (ArrayIndexOutOfBoundsException e) {
  99. assertTrue(true);
  100. }
  101. }
  102. public void testFillTo0() {
  103. final IntList i = new IntList();
  104. i.fillTo(0, Integer.MIN_VALUE);
  105. assertEquals(0, i.size());
  106. }
  107. public void testFillTo1() {
  108. final IntList i = new IntList();
  109. i.fillTo(1, Integer.MIN_VALUE);
  110. assertEquals(1, i.size());
  111. i.add(0);
  112. assertEquals(Integer.MIN_VALUE, i.get(0));
  113. assertEquals(0, i.get(1));
  114. }
  115. public void testFillTo100() {
  116. final IntList i = new IntList();
  117. i.fillTo(100, Integer.MIN_VALUE);
  118. assertEquals(100, i.size());
  119. i.add(3);
  120. assertEquals(Integer.MIN_VALUE, i.get(99));
  121. assertEquals(3, i.get(100));
  122. }
  123. public void testClear() {
  124. final IntList i = new IntList();
  125. final int n = 5;
  126. for (int v = 0; v < n; v++)
  127. i.add(10 + v);
  128. assertEquals(n, i.size());
  129. i.clear();
  130. assertEquals(0, i.size());
  131. try {
  132. i.get(0);
  133. fail("Accepted 0 index on empty list");
  134. } catch (ArrayIndexOutOfBoundsException e) {
  135. assertTrue(true);
  136. }
  137. }
  138. public void testSet() {
  139. final IntList i = new IntList();
  140. i.add(1);
  141. assertEquals(1, i.size());
  142. assertEquals(1, i.get(0));
  143. i.set(0, 5);
  144. assertEquals(5, i.get(0));
  145. try {
  146. i.set(5, 5);
  147. fail("accepted set of 5 beyond end of list");
  148. } catch (ArrayIndexOutOfBoundsException e){
  149. assertTrue(true);
  150. }
  151. i.set(1, 2);
  152. assertEquals(2, i.size());
  153. assertEquals(2, i.get(1));
  154. }
  155. public void testToString() {
  156. final IntList i = new IntList();
  157. i.add(1);
  158. assertEquals("[1]", i.toString());
  159. i.add(13);
  160. i.add(5);
  161. assertEquals("[1, 13, 5]", i.toString());
  162. }
  163. }