• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 //     * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //     * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 //     * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 package com.google.protobuf;
32 
33 import static java.util.Arrays.asList;
34 
35 import junit.framework.TestCase;
36 
37 import java.util.Collections;
38 import java.util.ConcurrentModificationException;
39 import java.util.Iterator;
40 
41 /**
42  * Tests for {@link LongArrayList}.
43  *
44  * @author dweis@google.com (Daniel Weis)
45  */
46 public class LongArrayListTest extends TestCase {
47 
48   private static final LongArrayList UNARY_LIST =
49       newImmutableLongArrayList(1);
50   private static final LongArrayList TERTIARY_LIST =
51       newImmutableLongArrayList(1, 2, 3);
52 
53   private LongArrayList list;
54 
55   @Override
setUp()56   protected void setUp() throws Exception {
57     list = new LongArrayList();
58   }
59 
testEmptyListReturnsSameInstance()60   public void testEmptyListReturnsSameInstance() {
61     assertSame(LongArrayList.emptyList(), LongArrayList.emptyList());
62   }
63 
testEmptyListIsImmutable()64   public void testEmptyListIsImmutable() {
65     assertImmutable(LongArrayList.emptyList());
66   }
67 
testMakeImmutable()68   public void testMakeImmutable() {
69     list.addLong(3);
70     list.addLong(4);
71     list.addLong(5);
72     list.addLong(7);
73     list.makeImmutable();
74     assertImmutable(list);
75   }
76 
testModificationWithIteration()77   public void testModificationWithIteration() {
78     list.addAll(asList(1L, 2L, 3L, 4L));
79     Iterator<Long> iterator = list.iterator();
80     assertEquals(4, list.size());
81     assertEquals(1L, (long) list.get(0));
82     assertEquals(1L, (long) iterator.next());
83     list.set(0, 1L);
84     assertEquals(2L, (long) iterator.next());
85 
86     list.remove(0);
87     try {
88       iterator.next();
89       fail();
90     } catch (ConcurrentModificationException e) {
91       // expected
92     }
93 
94     iterator = list.iterator();
95     list.add(0, 0L);
96     try {
97       iterator.next();
98       fail();
99     } catch (ConcurrentModificationException e) {
100       // expected
101     }
102   }
103 
testGet()104   public void testGet() {
105     assertEquals(1L, (long) TERTIARY_LIST.get(0));
106     assertEquals(2L, (long) TERTIARY_LIST.get(1));
107     assertEquals(3L, (long) TERTIARY_LIST.get(2));
108 
109     try {
110       TERTIARY_LIST.get(-1);
111       fail();
112     } catch (IndexOutOfBoundsException e) {
113       // expected
114     }
115 
116     try {
117       TERTIARY_LIST.get(3);
118       fail();
119     } catch (IndexOutOfBoundsException e) {
120       // expected
121     }
122   }
123 
testGetLong()124   public void testGetLong() {
125     assertEquals(1L, TERTIARY_LIST.getLong(0));
126     assertEquals(2L, TERTIARY_LIST.getLong(1));
127     assertEquals(3L, TERTIARY_LIST.getLong(2));
128 
129     try {
130       TERTIARY_LIST.get(-1);
131       fail();
132     } catch (IndexOutOfBoundsException e) {
133       // expected
134     }
135 
136     try {
137       TERTIARY_LIST.get(3);
138       fail();
139     } catch (IndexOutOfBoundsException e) {
140       // expected
141     }
142   }
143 
testSize()144   public void testSize() {
145     assertEquals(0, LongArrayList.emptyList().size());
146     assertEquals(1, UNARY_LIST.size());
147     assertEquals(3, TERTIARY_LIST.size());
148 
149     list.addLong(3);
150     list.addLong(4);
151     list.addLong(6);
152     list.addLong(8);
153     assertEquals(4, list.size());
154 
155     list.remove(0);
156     assertEquals(3, list.size());
157 
158     list.add(17L);
159     assertEquals(4, list.size());
160   }
161 
testSet()162   public void testSet() {
163     list.addLong(2);
164     list.addLong(4);
165 
166     assertEquals(2L, (long) list.set(0, 3L));
167     assertEquals(3L, list.getLong(0));
168 
169     assertEquals(4L, (long) list.set(1, 0L));
170     assertEquals(0L, list.getLong(1));
171 
172     try {
173       list.set(-1, 0L);
174       fail();
175     } catch (IndexOutOfBoundsException e) {
176       // expected
177     }
178 
179     try {
180       list.set(2, 0L);
181       fail();
182     } catch (IndexOutOfBoundsException e) {
183       // expected
184     }
185   }
186 
testSetLong()187   public void testSetLong() {
188     list.addLong(1);
189     list.addLong(3);
190 
191     assertEquals(1L, list.setLong(0, 0));
192     assertEquals(0L, list.getLong(0));
193 
194     assertEquals(3L, list.setLong(1, 0));
195     assertEquals(0L, list.getLong(1));
196 
197     try {
198       list.setLong(-1, 0);
199       fail();
200     } catch (IndexOutOfBoundsException e) {
201       // expected
202     }
203 
204     try {
205       list.setLong(2, 0);
206       fail();
207     } catch (IndexOutOfBoundsException e) {
208       // expected
209     }
210   }
211 
testAdd()212   public void testAdd() {
213     assertEquals(0, list.size());
214 
215     assertTrue(list.add(2L));
216     assertEquals(asList(2L), list);
217 
218     assertTrue(list.add(3L));
219     list.add(0, 4L);
220     assertEquals(asList(4L, 2L, 3L), list);
221 
222     list.add(0, 1L);
223     list.add(0, 0L);
224     // Force a resize by getting up to 11 elements.
225     for (int i = 0; i < 6; i++) {
226       list.add(Long.valueOf(5 + i));
227     }
228     assertEquals(
229         asList(0L, 1L, 4L, 2L, 3L, 5L, 6L, 7L, 8L, 9L, 10L),
230         list);
231 
232     try {
233       list.add(-1, 5L);
234     } catch (IndexOutOfBoundsException e) {
235       // expected
236     }
237 
238     try {
239       list.add(4, 5L);
240     } catch (IndexOutOfBoundsException e) {
241       // expected
242     }
243   }
244 
testAddLong()245   public void testAddLong() {
246     assertEquals(0, list.size());
247 
248     list.addLong(2);
249     assertEquals(asList(2L), list);
250 
251     list.addLong(3);
252     assertEquals(asList(2L, 3L), list);
253   }
254 
testAddAll()255   public void testAddAll() {
256     assertEquals(0, list.size());
257 
258     assertTrue(list.addAll(Collections.singleton(1L)));
259     assertEquals(1, list.size());
260     assertEquals(1L, (long) list.get(0));
261     assertEquals(1L, list.getLong(0));
262 
263     assertTrue(list.addAll(asList(2L, 3L, 4L, 5L, 6L)));
264     assertEquals(asList(1L, 2L, 3L, 4L, 5L, 6L), list);
265 
266     assertTrue(list.addAll(TERTIARY_LIST));
267     assertEquals(asList(1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L), list);
268 
269     assertFalse(list.addAll(Collections.<Long>emptyList()));
270     assertFalse(list.addAll(LongArrayList.emptyList()));
271   }
272 
testRemove()273   public void testRemove() {
274     list.addAll(TERTIARY_LIST);
275     assertEquals(1L, (long) list.remove(0));
276     assertEquals(asList(2L, 3L), list);
277 
278     assertTrue(list.remove(Long.valueOf(3)));
279     assertEquals(asList(2L), list);
280 
281     assertFalse(list.remove(Long.valueOf(3)));
282     assertEquals(asList(2L), list);
283 
284     assertEquals(2L, (long) list.remove(0));
285     assertEquals(asList(), list);
286 
287     try {
288       list.remove(-1);
289       fail();
290     } catch (IndexOutOfBoundsException e) {
291       // expected
292     }
293 
294     try {
295       list.remove(0);
296     } catch (IndexOutOfBoundsException e) {
297       // expected
298     }
299   }
300 
assertImmutable(LongArrayList list)301   private void assertImmutable(LongArrayList list) {
302     if (list.contains(1L)) {
303       throw new RuntimeException("Cannot test the immutability of lists that contain 1.");
304     }
305 
306     try {
307       list.add(1L);
308       fail();
309     } catch (UnsupportedOperationException e) {
310       // expected
311     }
312 
313     try {
314       list.add(0, 1L);
315       fail();
316     } catch (UnsupportedOperationException e) {
317       // expected
318     }
319 
320     try {
321       list.addAll(Collections.<Long>emptyList());
322       fail();
323     } catch (UnsupportedOperationException e) {
324       // expected
325     }
326 
327     try {
328       list.addAll(Collections.singletonList(1L));
329       fail();
330     } catch (UnsupportedOperationException e) {
331       // expected
332     }
333 
334     try {
335       list.addAll(new LongArrayList());
336       fail();
337     } catch (UnsupportedOperationException e) {
338       // expected
339     }
340 
341     try {
342       list.addAll(UNARY_LIST);
343       fail();
344     } catch (UnsupportedOperationException e) {
345       // expected
346     }
347 
348     try {
349       list.addAll(0, Collections.singleton(1L));
350       fail();
351     } catch (UnsupportedOperationException e) {
352       // expected
353     }
354 
355     try {
356       list.addAll(0, UNARY_LIST);
357       fail();
358     } catch (UnsupportedOperationException e) {
359       // expected
360     }
361 
362     try {
363       list.addAll(0, Collections.<Long>emptyList());
364       fail();
365     } catch (UnsupportedOperationException e) {
366       // expected
367     }
368 
369     try {
370       list.addLong(0);
371       fail();
372     } catch (UnsupportedOperationException e) {
373       // expected
374     }
375 
376     try {
377       list.clear();
378       fail();
379     } catch (UnsupportedOperationException e) {
380       // expected
381     }
382 
383     try {
384       list.remove(1);
385       fail();
386     } catch (UnsupportedOperationException e) {
387       // expected
388     }
389 
390     try {
391       list.remove(new Object());
392       fail();
393     } catch (UnsupportedOperationException e) {
394       // expected
395     }
396 
397     try {
398       list.removeAll(Collections.<Long>emptyList());
399       fail();
400     } catch (UnsupportedOperationException e) {
401       // expected
402     }
403 
404     try {
405       list.removeAll(Collections.singleton(1L));
406       fail();
407     } catch (UnsupportedOperationException e) {
408       // expected
409     }
410 
411     try {
412       list.removeAll(UNARY_LIST);
413       fail();
414     } catch (UnsupportedOperationException e) {
415       // expected
416     }
417 
418     try {
419       list.retainAll(Collections.<Long>emptyList());
420       fail();
421     } catch (UnsupportedOperationException e) {
422       // expected
423     }
424 
425     try {
426       list.retainAll(Collections.singleton(1L));
427       fail();
428     } catch (UnsupportedOperationException e) {
429       // expected
430     }
431 
432     try {
433       list.retainAll(UNARY_LIST);
434       fail();
435     } catch (UnsupportedOperationException e) {
436       // expected
437     }
438 
439     try {
440       list.set(0, 0L);
441       fail();
442     } catch (UnsupportedOperationException e) {
443       // expected
444     }
445 
446     try {
447       list.setLong(0, 0);
448       fail();
449     } catch (UnsupportedOperationException e) {
450       // expected
451     }
452   }
453 
newImmutableLongArrayList(long... elements)454   private static LongArrayList newImmutableLongArrayList(long... elements) {
455     LongArrayList list = new LongArrayList();
456     for (long element : elements) {
457       list.addLong(element);
458     }
459     list.makeImmutable();
460     return list;
461   }
462 }
463