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