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.assertSame;
49 import static org.junit.Assert.assertTrue;
50
51 import java.util.Iterator;
52
53 import org.junit.Test;
54
55 public class BlockListTest {
56 @Test
57 public void testEmptyList() {
58 BlockList<String> empty;
59
60 empty = new BlockList<>();
61 assertEquals(0, empty.size());
62 assertTrue(empty.isEmpty());
63 assertFalse(empty.iterator().hasNext());
64
65 empty = new BlockList<>(0);
66 assertEquals(0, empty.size());
67 assertTrue(empty.isEmpty());
68 assertFalse(empty.iterator().hasNext());
69
70 empty = new BlockList<>(1);
71 assertEquals(0, empty.size());
72 assertTrue(empty.isEmpty());
73 assertFalse(empty.iterator().hasNext());
74
75 empty = new BlockList<>(64);
76 assertEquals(0, empty.size());
77 assertTrue(empty.isEmpty());
78 assertFalse(empty.iterator().hasNext());
79 }
80
81 @Test
82 public void testGet() {
83 BlockList<String> list = new BlockList<>(4);
84
85 try {
86 list.get(-1);
87 } catch (IndexOutOfBoundsException badIndex) {
88 assertEquals(String.valueOf(-1), badIndex.getMessage());
89 }
90
91 try {
92 list.get(0);
93 } catch (IndexOutOfBoundsException badIndex) {
94 assertEquals(String.valueOf(0), badIndex.getMessage());
95 }
96
97 try {
98 list.get(4);
99 } catch (IndexOutOfBoundsException badIndex) {
100 assertEquals(String.valueOf(4), badIndex.getMessage());
101 }
102
103 String fooStr = "foo";
104 String barStr = "bar";
105 String foobarStr = "foobar";
106
107 list.add(fooStr);
108 list.add(barStr);
109 list.add(foobarStr);
110
111 assertSame(fooStr, list.get(0));
112 assertSame(barStr, list.get(1));
113 assertSame(foobarStr, list.get(2));
114
115 try {
116 list.get(3);
117 } catch (IndexOutOfBoundsException badIndex) {
118 assertEquals(String.valueOf(3), badIndex.getMessage());
119 }
120 }
121
122 @Test
123 public void testSet() {
124 BlockList<String> list = new BlockList<>(4);
125
126 try {
127 list.set(-1, "foo");
128 } catch (IndexOutOfBoundsException badIndex) {
129 assertEquals(String.valueOf(-1), badIndex.getMessage());
130 }
131
132 try {
133 list.set(0, "foo");
134 } catch (IndexOutOfBoundsException badIndex) {
135 assertEquals(String.valueOf(0), badIndex.getMessage());
136 }
137
138 try {
139 list.set(4, "foo");
140 } catch (IndexOutOfBoundsException badIndex) {
141 assertEquals(String.valueOf(4), badIndex.getMessage());
142 }
143
144 String fooStr = "foo";
145 String barStr = "bar";
146 String foobarStr = "foobar";
147
148 list.add(fooStr);
149 list.add(barStr);
150 list.add(foobarStr);
151
152 assertSame(fooStr, list.get(0));
153 assertSame(barStr, list.get(1));
154 assertSame(foobarStr, list.get(2));
155
156 assertSame(fooStr, list.set(0, barStr));
157 assertSame(barStr, list.set(1, fooStr));
158
159 assertSame(barStr, list.get(0));
160 assertSame(fooStr, list.get(1));
161
162 try {
163 list.set(3, "bar");
164 } catch (IndexOutOfBoundsException badIndex) {
165 assertEquals(String.valueOf(3), badIndex.getMessage());
166 }
167 }
168
169 @Test
170 public void testAddToEnd() {
171 BlockList<Integer> list = new BlockList<>(4);
172 int cnt = BlockList.BLOCK_SIZE * 3;
173
174 for (int i = 0; i < cnt; i++)
175 list.add(Integer.valueOf(42 + i));
176 assertEquals(cnt, list.size());
177
178 for (int i = 0; i < cnt; i++)
179 assertEquals(Integer.valueOf(42 + i), list.get(i));
180
181 list.clear();
182 assertEquals(0, list.size());
183 assertTrue(list.isEmpty());
184
185 for (int i = 0; i < cnt; i++)
186 list.add(i, Integer.valueOf(42 + i));
187 assertEquals(cnt, list.size());
188
189 for (int i = 0; i < cnt; i++)
190 assertEquals(Integer.valueOf(42 + i), list.get(i));
191 }
192
193 @Test
194 public void testAddSlowPath() {
195 BlockList<String> list = new BlockList<>(4);
196
197 String fooStr = "foo";
198 String barStr = "bar";
199 String foobarStr = "foobar";
200 String firstStr = "first";
201 String zeroStr = "zero";
202
203 list.add(fooStr);
204 list.add(barStr);
205 list.add(foobarStr);
206 assertEquals(3, list.size());
207
208 list.add(1, firstStr);
209 assertEquals(4, list.size());
210 assertSame(fooStr, list.get(0));
211 assertSame(firstStr, list.get(1));
212 assertSame(barStr, list.get(2));
213 assertSame(foobarStr, list.get(3));
214
215 list.add(0, zeroStr);
216 assertEquals(5, list.size());
217 assertSame(zeroStr, list.get(0));
218 assertSame(fooStr, list.get(1));
219 assertSame(firstStr, list.get(2));
220 assertSame(barStr, list.get(3));
221 assertSame(foobarStr, list.get(4));
222 }
223
224 @Test
225 public void testRemoveFromEnd() {
226 BlockList<String> list = new BlockList<>(4);
227
228 String fooStr = "foo";
229 String barStr = "bar";
230 String foobarStr = "foobar";
231
232 list.add(fooStr);
233 list.add(barStr);
234 list.add(foobarStr);
235
236 assertSame(foobarStr, list.remove(2));
237 assertEquals(2, list.size());
238
239 assertSame(barStr, list.remove(1));
240 assertEquals(1, list.size());
241
242 assertSame(fooStr, list.remove(0));
243 assertEquals(0, list.size());
244 }
245
246 @Test
247 public void testRemoveSlowPath() {
248 BlockList<String> list = new BlockList<>(4);
249
250 String fooStr = "foo";
251 String barStr = "bar";
252 String foobarStr = "foobar";
253
254 list.add(fooStr);
255 list.add(barStr);
256 list.add(foobarStr);
257
258 assertSame(barStr, list.remove(1));
259 assertEquals(2, list.size());
260 assertSame(fooStr, list.get(0));
261 assertSame(foobarStr, list.get(1));
262
263 assertSame(fooStr, list.remove(0));
264 assertEquals(1, list.size());
265 assertSame(foobarStr, list.get(0));
266
267 assertSame(foobarStr, list.remove(0));
268 assertEquals(0, list.size());
269 }
270
271 @Test
272 public void testAddRemoveAdd() {
273 BlockList<Integer> list = new BlockList<>();
274 for (int i = 0; i < BlockList.BLOCK_SIZE + 1; i++)
275 list.add(Integer.valueOf(i));
276 assertEquals(Integer.valueOf(BlockList.BLOCK_SIZE),
277 list.remove(list.size() - 1));
278 assertEquals(Integer.valueOf(BlockList.BLOCK_SIZE - 1),
279 list.remove(list.size() - 1));
280 assertTrue(list.add(Integer.valueOf(1)));
281 assertEquals(Integer.valueOf(1), list.get(list.size() - 1));
282 }
283
284 @Test
285 public void testAddAllFromOtherList() {
286 BlockList<Integer> src = new BlockList<>(4);
287 int cnt = BlockList.BLOCK_SIZE * 2;
288
289 for (int i = 0; i < cnt; i++)
290 src.add(Integer.valueOf(42 + i));
291 src.add(Integer.valueOf(1));
292
293 BlockList<Integer> dst = new BlockList<>(4);
294 dst.add(Integer.valueOf(255));
295 dst.addAll(src);
296 assertEquals(cnt + 2, dst.size());
297 for (int i = 0; i < cnt; i++)
298 assertEquals(Integer.valueOf(42 + i), dst.get(i + 1));
299 assertEquals(Integer.valueOf(1), dst.get(dst.size() - 1));
300 }
301
302 @Test
303 public void testFastIterator() {
304 BlockList<Integer> list = new BlockList<>(4);
305 int cnt = BlockList.BLOCK_SIZE * 3;
306
307 for (int i = 0; i < cnt; i++)
308 list.add(Integer.valueOf(42 + i));
309 assertEquals(cnt, list.size());
310
311 Iterator<Integer> itr = list.iterator();
312 for (int i = 0; i < cnt; i++) {
313 assertTrue(itr.hasNext());
314 assertEquals(Integer.valueOf(42 + i), itr.next());
315 }
316 assertFalse(itr.hasNext());
317 }
318
319 @Test
320 public void testAddRejectsBadIndexes() {
321 BlockList<Integer> list = new BlockList<>(4);
322 list.add(Integer.valueOf(41));
323
324 try {
325 list.add(-1, Integer.valueOf(42));
326 } catch (IndexOutOfBoundsException badIndex) {
327 assertEquals(String.valueOf(-1), badIndex.getMessage());
328 }
329
330 try {
331 list.add(4, Integer.valueOf(42));
332 } catch (IndexOutOfBoundsException badIndex) {
333 assertEquals(String.valueOf(4), badIndex.getMessage());
334 }
335 }
336
337 @Test
338 public void testRemoveRejectsBadIndexes() {
339 BlockList<Integer> list = new BlockList<>(4);
340 list.add(Integer.valueOf(41));
341
342 try {
343 list.remove(-1);
344 } catch (IndexOutOfBoundsException badIndex) {
345 assertEquals(String.valueOf(-1), badIndex.getMessage());
346 }
347
348 try {
349 list.remove(4);
350 } catch (IndexOutOfBoundsException badIndex) {
351 assertEquals(String.valueOf(4), badIndex.getMessage());
352 }
353 }
354 }