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