31 package com.google.protobuf;
33 import static java.util.Arrays.asList;
35 import java.util.Collections;
36 import java.util.ConcurrentModificationException;
37 import java.util.Iterator;
38 import java.util.List;
39 import junit.framework.TestCase;
48 private ProtobufArrayList<Integer>
list;
51 protected void setUp() throws Exception {
52 list =
new ProtobufArrayList<Integer>();
56 assertSame(ProtobufArrayList.emptyList(), ProtobufArrayList.emptyList());
64 list.addAll(asList(1, 2, 3, 4));
65 Iterator<Integer> iterator =
list.iterator();
66 assertEquals(4,
list.size());
67 assertEquals(1, (
int)
list.get(0));
68 assertEquals(1, (
int) iterator.next());
74 }
catch (ConcurrentModificationException e) {
78 iterator =
list.iterator();
83 }
catch (ConcurrentModificationException e) {
87 iterator =
list.iterator();
92 }
catch (ConcurrentModificationException e) {
102 list.makeImmutable();
108 assertEquals(1, (
int)
list.remove(0));
109 assertEquals(asList(2, 3),
list);
111 assertTrue(
list.remove(Integer.valueOf(3)));
112 assertEquals(asList(2),
list);
114 assertFalse(
list.remove(Integer.valueOf(3)));
115 assertEquals(asList(2),
list);
117 assertEquals(2, (
int)
list.remove(0));
118 assertEquals(asList(),
list);
123 }
catch (IndexOutOfBoundsException e) {
129 }
catch (IndexOutOfBoundsException e) {
142 }
catch (IndexOutOfBoundsException e) {
149 }
catch (IndexOutOfBoundsException e) {
158 assertEquals(2, (
int)
list.set(0, 3));
159 assertEquals(3, (
int)
list.get(0));
161 assertEquals(4, (
int)
list.set(1, 0));
162 assertEquals(0, (
int)
list.get(1));
167 }
catch (IndexOutOfBoundsException e) {
174 }
catch (IndexOutOfBoundsException e) {
180 assertEquals(0,
list.size());
182 assertTrue(
list.add(2));
183 assertEquals(asList(2),
list);
185 assertTrue(
list.add(3));
187 assertEquals(asList(4, 2, 3),
list);
192 for (
int i = 0;
i < 6;
i++) {
193 list.add(Integer.valueOf(5 +
i));
195 assertEquals(asList(0, 1, 4, 2, 3, 5, 6, 7, 8, 9, 10),
list);
199 }
catch (IndexOutOfBoundsException e) {
205 }
catch (IndexOutOfBoundsException e) {
211 assertEquals(0,
list.size());
213 assertTrue(
list.addAll(Collections.singleton(1)));
214 assertEquals(1,
list.size());
215 assertEquals(1, (
int)
list.get(0));
217 assertTrue(
list.addAll(asList(2, 3, 4, 5, 6)));
218 assertEquals(asList(1, 2, 3, 4, 5, 6),
list);
221 assertEquals(asList(1, 2, 3, 4, 5, 6, 1, 2, 3),
list);
223 assertFalse(
list.addAll(Collections.<Integer>emptyList()));
224 assertFalse(
list.addAll(IntArrayList.emptyList()));
228 assertEquals(0, ProtobufArrayList.emptyList().size());
236 assertEquals(4,
list.size());
239 assertEquals(3,
list.size());
242 assertEquals(4,
list.size());
246 if (
list.contains(1)) {
247 throw new RuntimeException(
"Cannot test the immutability of lists that contain 1.");
253 }
catch (UnsupportedOperationException e) {
260 }
catch (UnsupportedOperationException e) {
265 list.addAll(Collections.<Integer>emptyList());
267 }
catch (UnsupportedOperationException e) {
272 list.addAll(Collections.singletonList(1));
274 }
catch (UnsupportedOperationException e) {
279 list.addAll(
new ProtobufArrayList<Integer>());
281 }
catch (UnsupportedOperationException e) {
288 }
catch (UnsupportedOperationException e) {
293 list.addAll(0, Collections.singleton(1));
295 }
catch (UnsupportedOperationException e) {
302 }
catch (UnsupportedOperationException e) {
307 list.addAll(0, Collections.<Integer>emptyList());
309 }
catch (UnsupportedOperationException e) {
316 }
catch (UnsupportedOperationException e) {
323 }
catch (UnsupportedOperationException e) {
328 list.remove(
new Object());
330 }
catch (UnsupportedOperationException e) {
335 list.removeAll(Collections.emptyList());
337 }
catch (UnsupportedOperationException e) {
342 list.removeAll(Collections.singleton(1));
344 }
catch (UnsupportedOperationException e) {
351 }
catch (UnsupportedOperationException e) {
356 list.retainAll(Collections.emptyList());
358 }
catch (UnsupportedOperationException e) {
363 list.retainAll(Collections.singleton(1));
365 }
catch (UnsupportedOperationException e) {
372 }
catch (UnsupportedOperationException e) {
379 }
catch (UnsupportedOperationException e) {
385 ProtobufArrayList<Integer>
list =
new ProtobufArrayList<Integer>();
386 for (
int element : elements) {
389 list.makeImmutable();