31 package com.google.protobuf;
33 import static java.util.Arrays.asList;
36 import java.util.Collections;
37 import java.util.ConcurrentModificationException;
38 import java.util.Iterator;
39 import junit.framework.TestCase;
54 protected void setUp() throws Exception {
55 list =
new IntArrayList();
59 assertSame(IntArrayList.emptyList(), IntArrayList.emptyList());
76 list.addAll(asList(1, 2, 3, 4));
77 Iterator<Integer> iterator =
list.iterator();
78 assertEquals(4,
list.size());
79 assertEquals(1, (
int)
list.get(0));
80 assertEquals(1, (
int) iterator.next());
82 assertEquals(2, (
int) iterator.next());
88 }
catch (ConcurrentModificationException e) {
92 iterator =
list.iterator();
97 }
catch (ConcurrentModificationException e) {
110 }
catch (IndexOutOfBoundsException e) {
117 }
catch (IndexOutOfBoundsException e) {
130 }
catch (IndexOutOfBoundsException e) {
137 }
catch (IndexOutOfBoundsException e) {
143 assertEquals(0, IntArrayList.emptyList().size());
151 assertEquals(4,
list.size());
154 assertEquals(3,
list.size());
157 assertEquals(4,
list.size());
164 assertEquals(2, (
int)
list.set(0, 3));
165 assertEquals(3,
list.getInt(0));
167 assertEquals(4, (
int)
list.set(1, 0));
168 assertEquals(0,
list.getInt(1));
173 }
catch (IndexOutOfBoundsException e) {
180 }
catch (IndexOutOfBoundsException e) {
189 assertEquals(1,
list.setInt(0, 0));
190 assertEquals(0,
list.getInt(0));
192 assertEquals(3,
list.setInt(1, 0));
193 assertEquals(0,
list.getInt(1));
198 }
catch (IndexOutOfBoundsException e) {
205 }
catch (IndexOutOfBoundsException e) {
211 assertEquals(0,
list.size());
213 assertTrue(
list.add(2));
214 assertEquals(asList(2),
list);
216 assertTrue(
list.add(3));
218 assertEquals(asList(4, 2, 3),
list);
223 for (
int i = 0;
i < 6;
i++) {
224 list.add(Integer.valueOf(5 +
i));
226 assertEquals(asList(0, 1, 4, 2, 3, 5, 6, 7, 8, 9, 10),
list);
230 }
catch (IndexOutOfBoundsException e) {
236 }
catch (IndexOutOfBoundsException e) {
242 assertEquals(0,
list.size());
245 assertEquals(asList(2),
list);
248 assertEquals(asList(2, 3),
list);
252 assertEquals(0,
list.size());
254 assertTrue(
list.addAll(Collections.singleton(1)));
255 assertEquals(1,
list.size());
256 assertEquals(1, (
int)
list.get(0));
257 assertEquals(1,
list.getInt(0));
259 assertTrue(
list.addAll(asList(2, 3, 4, 5, 6)));
260 assertEquals(asList(1, 2, 3, 4, 5, 6),
list);
263 assertEquals(asList(1, 2, 3, 4, 5, 6, 1, 2, 3),
list);
265 assertFalse(
list.addAll(Collections.<Integer>emptyList()));
266 assertFalse(
list.addAll(IntArrayList.emptyList()));
270 IntArrayList list1 =
new IntArrayList();
271 IntArrayList list2 =
new IntArrayList();
273 assertEquals(list1, list2);
278 assertEquals(1, (
int)
list.remove(0));
279 assertEquals(asList(2, 3),
list);
281 assertTrue(
list.remove(Integer.valueOf(3)));
282 assertEquals(asList(2),
list);
284 assertFalse(
list.remove(Integer.valueOf(3)));
285 assertEquals(asList(2),
list);
287 assertEquals(2, (
int)
list.remove(0));
288 assertEquals(asList(),
list);
293 }
catch (IndexOutOfBoundsException e) {
299 }
catch (IndexOutOfBoundsException e) {
308 assertEquals(0, toRemove.size());
316 assertEquals(1, toRemove.size());
317 assertEquals(4, (
int) toRemove.get(0));
323 toRemove.subList(0, 1).clear();
324 assertEquals(0, toRemove.size());
328 if (
list.contains(1)) {
329 throw new RuntimeException(
"Cannot test the immutability of lists that contain 1.");
335 }
catch (UnsupportedOperationException e) {
342 }
catch (UnsupportedOperationException e) {
347 list.addAll(Collections.<Integer>emptyList());
349 }
catch (UnsupportedOperationException e) {
354 list.addAll(Collections.singletonList(1));
356 }
catch (UnsupportedOperationException e) {
361 list.addAll(
new IntArrayList());
363 }
catch (UnsupportedOperationException e) {
370 }
catch (UnsupportedOperationException e) {
375 list.addAll(0, Collections.singleton(1));
377 }
catch (UnsupportedOperationException e) {
384 }
catch (UnsupportedOperationException e) {
389 list.addAll(0, Collections.<Integer>emptyList());
391 }
catch (UnsupportedOperationException e) {
398 }
catch (UnsupportedOperationException e) {
405 }
catch (UnsupportedOperationException e) {
412 }
catch (UnsupportedOperationException e) {
417 list.remove(
new Object());
419 }
catch (UnsupportedOperationException e) {
424 list.removeAll(Collections.<Integer>emptyList());
426 }
catch (UnsupportedOperationException e) {
431 list.removeAll(Collections.singleton(1));
433 }
catch (UnsupportedOperationException e) {
440 }
catch (UnsupportedOperationException e) {
445 list.retainAll(Collections.<Integer>emptyList());
447 }
catch (UnsupportedOperationException e) {
452 list.retainAll(Collections.singleton(1));
454 }
catch (UnsupportedOperationException e) {
461 }
catch (UnsupportedOperationException e) {
468 }
catch (UnsupportedOperationException e) {
475 }
catch (UnsupportedOperationException e) {
481 IntArrayList
list =
new IntArrayList();
482 for (
int element : elements) {
483 list.addInt(element);
485 list.makeImmutable();