1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 package org.eclipse.jgit.util;
45
46 import static org.junit.Assert.assertEquals;
47 import static org.junit.Assert.assertFalse;
48 import static org.junit.Assert.assertTrue;
49 import static org.junit.Assert.fail;
50
51 import org.junit.Test;
52
53 public class IntListTest {
54 @Test
55 public void testEmpty_DefaultCapacity() {
56 final IntList i = new IntList();
57 assertEquals(0, i.size());
58 try {
59 i.get(0);
60 fail("Accepted 0 index on empty list");
61 } catch (ArrayIndexOutOfBoundsException e) {
62 assertTrue(true);
63 }
64 }
65
66 @Test
67 public void testEmpty_SpecificCapacity() {
68 final IntList i = new IntList(5);
69 assertEquals(0, i.size());
70 try {
71 i.get(0);
72 fail("Accepted 0 index on empty list");
73 } catch (ArrayIndexOutOfBoundsException e) {
74 assertTrue(true);
75 }
76 }
77
78 @Test
79 public void testAdd_SmallGroup() {
80 final IntList i = new IntList();
81 final int n = 5;
82 for (int v = 0; v < n; v++)
83 i.add(10 + v);
84 assertEquals(n, i.size());
85
86 for (int v = 0; v < n; v++)
87 assertEquals(10 + v, i.get(v));
88 try {
89 i.get(n);
90 fail("Accepted out of bound index on list");
91 } catch (ArrayIndexOutOfBoundsException e) {
92 assertTrue(true);
93 }
94 }
95
96 @Test
97 public void testAdd_ZeroCapacity() {
98 final IntList i = new IntList(0);
99 assertEquals(0, i.size());
100 i.add(1);
101 assertEquals(1, i.get(0));
102 }
103
104 @Test
105 public void testAdd_LargeGroup() {
106 final IntList i = new IntList();
107 final int n = 500;
108 for (int v = 0; v < n; v++)
109 i.add(10 + v);
110 assertEquals(n, i.size());
111
112 for (int v = 0; v < n; v++)
113 assertEquals(10 + v, i.get(v));
114 try {
115 i.get(n);
116 fail("Accepted out of bound index on list");
117 } catch (ArrayIndexOutOfBoundsException e) {
118 assertTrue(true);
119 }
120 }
121
122 @Test
123 public void testFillTo0() {
124 final IntList i = new IntList();
125 i.fillTo(0, Integer.MIN_VALUE);
126 assertEquals(0, i.size());
127 }
128
129 @Test
130 public void testFillTo1() {
131 final IntList i = new IntList();
132 i.fillTo(1, Integer.MIN_VALUE);
133 assertEquals(1, i.size());
134 i.add(0);
135 assertEquals(Integer.MIN_VALUE, i.get(0));
136 assertEquals(0, i.get(1));
137 }
138
139 @Test
140 public void testFillTo100() {
141 final IntList i = new IntList();
142 i.fillTo(100, Integer.MIN_VALUE);
143 assertEquals(100, i.size());
144 i.add(3);
145 assertEquals(Integer.MIN_VALUE, i.get(99));
146 assertEquals(3, i.get(100));
147 }
148
149 @Test
150 public void testClear() {
151 final IntList i = new IntList();
152 final int n = 5;
153 for (int v = 0; v < n; v++)
154 i.add(10 + v);
155 assertEquals(n, i.size());
156
157 i.clear();
158 assertEquals(0, i.size());
159 try {
160 i.get(0);
161 fail("Accepted 0 index on empty list");
162 } catch (ArrayIndexOutOfBoundsException e) {
163 assertTrue(true);
164 }
165 }
166
167 @Test
168 public void testSet() {
169 final IntList i = new IntList();
170 i.add(1);
171 assertEquals(1, i.size());
172 assertEquals(1, i.get(0));
173
174 i.set(0, 5);
175 assertEquals(5, i.get(0));
176
177 try {
178 i.set(5, 5);
179 fail("accepted set of 5 beyond end of list");
180 } catch (ArrayIndexOutOfBoundsException e){
181 assertTrue(true);
182 }
183
184 i.set(1, 2);
185 assertEquals(2, i.size());
186 assertEquals(2, i.get(1));
187 }
188
189 @Test
190 public void testContains() {
191 IntList i = new IntList();
192 i.add(1);
193 i.add(4);
194 assertTrue(i.contains(1));
195 assertTrue(i.contains(4));
196 assertFalse(i.contains(2));
197 }
198
199 @Test
200 public void testToString() {
201 final IntList i = new IntList();
202 i.add(1);
203 assertEquals("[1]", i.toString());
204 i.add(13);
205 i.add(5);
206 assertEquals("[1, 13, 5]", i.toString());
207 }
208
209 }