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 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertTrue;
  46. import static org.junit.Assert.fail;
  47. import org.junit.Test;
  48. public class IntListTest {
  49. @Test
  50. public void testEmpty_DefaultCapacity() {
  51. final IntList i = new IntList();
  52. assertEquals(0, i.size());
  53. try {
  54. i.get(0);
  55. fail("Accepted 0 index on empty list");
  56. } catch (ArrayIndexOutOfBoundsException e) {
  57. assertTrue(true);
  58. }
  59. }
  60. @Test
  61. public void testEmpty_SpecificCapacity() {
  62. final IntList i = new IntList(5);
  63. assertEquals(0, i.size());
  64. try {
  65. i.get(0);
  66. fail("Accepted 0 index on empty list");
  67. } catch (ArrayIndexOutOfBoundsException e) {
  68. assertTrue(true);
  69. }
  70. }
  71. @Test
  72. public void testAdd_SmallGroup() {
  73. final IntList i = new IntList();
  74. final int n = 5;
  75. for (int v = 0; v < n; v++)
  76. i.add(10 + v);
  77. assertEquals(n, i.size());
  78. for (int v = 0; v < n; v++)
  79. assertEquals(10 + v, i.get(v));
  80. try {
  81. i.get(n);
  82. fail("Accepted out of bound index on list");
  83. } catch (ArrayIndexOutOfBoundsException e) {
  84. assertTrue(true);
  85. }
  86. }
  87. @Test
  88. public void testAdd_ZeroCapacity() {
  89. final IntList i = new IntList(0);
  90. assertEquals(0, i.size());
  91. i.add(1);
  92. assertEquals(1, i.get(0));
  93. }
  94. @Test
  95. public void testAdd_LargeGroup() {
  96. final IntList i = new IntList();
  97. final int n = 500;
  98. for (int v = 0; v < n; v++)
  99. i.add(10 + v);
  100. assertEquals(n, i.size());
  101. for (int v = 0; v < n; v++)
  102. assertEquals(10 + v, i.get(v));
  103. try {
  104. i.get(n);
  105. fail("Accepted out of bound index on list");
  106. } catch (ArrayIndexOutOfBoundsException e) {
  107. assertTrue(true);
  108. }
  109. }
  110. @Test
  111. public void testFillTo0() {
  112. final IntList i = new IntList();
  113. i.fillTo(0, Integer.MIN_VALUE);
  114. assertEquals(0, i.size());
  115. }
  116. @Test
  117. public void testFillTo1() {
  118. final IntList i = new IntList();
  119. i.fillTo(1, Integer.MIN_VALUE);
  120. assertEquals(1, i.size());
  121. i.add(0);
  122. assertEquals(Integer.MIN_VALUE, i.get(0));
  123. assertEquals(0, i.get(1));
  124. }
  125. @Test
  126. public void testFillTo100() {
  127. final IntList i = new IntList();
  128. i.fillTo(100, Integer.MIN_VALUE);
  129. assertEquals(100, i.size());
  130. i.add(3);
  131. assertEquals(Integer.MIN_VALUE, i.get(99));
  132. assertEquals(3, i.get(100));
  133. }
  134. @Test
  135. public void testClear() {
  136. final IntList i = new IntList();
  137. final int n = 5;
  138. for (int v = 0; v < n; v++)
  139. i.add(10 + v);
  140. assertEquals(n, i.size());
  141. i.clear();
  142. assertEquals(0, i.size());
  143. try {
  144. i.get(0);
  145. fail("Accepted 0 index on empty list");
  146. } catch (ArrayIndexOutOfBoundsException e) {
  147. assertTrue(true);
  148. }
  149. }
  150. @Test
  151. public void testSet() {
  152. final IntList i = new IntList();
  153. i.add(1);
  154. assertEquals(1, i.size());
  155. assertEquals(1, i.get(0));
  156. i.set(0, 5);
  157. assertEquals(5, i.get(0));
  158. try {
  159. i.set(5, 5);
  160. fail("accepted set of 5 beyond end of list");
  161. } catch (ArrayIndexOutOfBoundsException e){
  162. assertTrue(true);
  163. }
  164. i.set(1, 2);
  165. assertEquals(2, i.size());
  166. assertEquals(2, i.get(1));
  167. }
  168. @Test
  169. public void testToString() {
  170. final IntList i = new IntList();
  171. i.add(1);
  172. assertEquals("[1]", i.toString());
  173. i.add(13);
  174. i.add(5);
  175. assertEquals("[1, 13, 5]", i.toString());
  176. }
  177. }