• 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 BooleanArrayList}.
43  *
44  * @author dweis@google.com (Daniel Weis)
45  */
46 public class BooleanArrayListTest extends TestCase {
47 
48   private static final BooleanArrayList UNARY_LIST =
49       newImmutableBooleanArrayList(true);
50   private static final BooleanArrayList TERTIARY_LIST =
51       newImmutableBooleanArrayList(true, false, true);
52 
53   private BooleanArrayList list;
54 
55   @Override
setUp()56   protected void setUp() throws Exception {
57     list = new BooleanArrayList();
58   }
59 
testEmptyListReturnsSameInstance()60   public void testEmptyListReturnsSameInstance() {
61     assertSame(BooleanArrayList.emptyList(), BooleanArrayList.emptyList());
62   }
63 
testEmptyListIsImmutable()64   public void testEmptyListIsImmutable() {
65     assertImmutable(BooleanArrayList.emptyList());
66   }
67 
testMakeImmutable()68   public void testMakeImmutable() {
69     list.addBoolean(true);
70     list.addBoolean(false);
71     list.addBoolean(true);
72     list.addBoolean(true);
73     list.makeImmutable();
74     assertImmutable(list);
75   }
76 
testModificationWithIteration()77   public void testModificationWithIteration() {
78     list.addAll(asList(true, false, true, false));
79     Iterator<Boolean> iterator = list.iterator();
80     assertEquals(4, list.size());
81     assertEquals(true, (boolean) list.get(0));
82     assertEquals(true, (boolean) iterator.next());
83     list.set(0, true);
84     assertEquals(false, (boolean) 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, false);
96     try {
97       iterator.next();
98       fail();
99     } catch (ConcurrentModificationException e) {
100       // expected
101     }
102   }
103 
testGet()104   public void testGet() {
105     assertEquals(true, (boolean) TERTIARY_LIST.get(0));
106     assertEquals(false, (boolean) TERTIARY_LIST.get(1));
107     assertEquals(true, (boolean) 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 
testGetBoolean()124   public void testGetBoolean() {
125     assertEquals(true, TERTIARY_LIST.getBoolean(0));
126     assertEquals(false, TERTIARY_LIST.getBoolean(1));
127     assertEquals(true, TERTIARY_LIST.getBoolean(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, BooleanArrayList.emptyList().size());
146     assertEquals(1, UNARY_LIST.size());
147     assertEquals(3, TERTIARY_LIST.size());
148 
149     list.addBoolean(true);
150     list.addBoolean(false);
151     list.addBoolean(false);
152     list.addBoolean(false);
153     assertEquals(4, list.size());
154 
155     list.remove(0);
156     assertEquals(3, list.size());
157 
158     list.add(true);
159     assertEquals(4, list.size());
160   }
161 
testSet()162   public void testSet() {
163     list.addBoolean(false);
164     list.addBoolean(false);
165 
166     assertEquals(false, (boolean) list.set(0, true));
167     assertEquals(true, list.getBoolean(0));
168 
169     assertEquals(false, (boolean) list.set(1, false));
170     assertEquals(false, list.getBoolean(1));
171 
172     try {
173       list.set(-1, false);
174       fail();
175     } catch (IndexOutOfBoundsException e) {
176       // expected
177     }
178 
179     try {
180       list.set(2, false);
181       fail();
182     } catch (IndexOutOfBoundsException e) {
183       // expected
184     }
185   }
186 
testSetBoolean()187   public void testSetBoolean() {
188     list.addBoolean(true);
189     list.addBoolean(true);
190 
191     assertEquals(true, list.setBoolean(0, false));
192     assertEquals(false, list.getBoolean(0));
193 
194     assertEquals(true, list.setBoolean(1, false));
195     assertEquals(false, list.getBoolean(1));
196 
197     try {
198       list.setBoolean(-1, false);
199       fail();
200     } catch (IndexOutOfBoundsException e) {
201       // expected
202     }
203 
204     try {
205       list.setBoolean(2, false);
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(false));
216     assertEquals(asList(false), list);
217 
218     assertTrue(list.add(true));
219     list.add(0, false);
220     assertEquals(asList(false, false, true), list);
221 
222     list.add(0, true);
223     list.add(0, false);
224     // Force a resize by getting up to 11 elements.
225     for (int i = 0; i < 6; i++) {
226       list.add(i % 2 == 0);
227     }
228     assertEquals(
229         asList(false, true, false, false, true, true, false, true, false, true, false),
230         list);
231 
232     try {
233       list.add(-1, true);
234     } catch (IndexOutOfBoundsException e) {
235       // expected
236     }
237 
238     try {
239       list.add(4, true);
240     } catch (IndexOutOfBoundsException e) {
241       // expected
242     }
243   }
244 
testAddBoolean()245   public void testAddBoolean() {
246     assertEquals(0, list.size());
247 
248     list.addBoolean(false);
249     assertEquals(asList(false), list);
250 
251     list.addBoolean(true);
252     assertEquals(asList(false, true), list);
253   }
254 
testAddAll()255   public void testAddAll() {
256     assertEquals(0, list.size());
257 
258     assertTrue(list.addAll(Collections.singleton(true)));
259     assertEquals(1, list.size());
260     assertEquals(true, (boolean) list.get(0));
261     assertEquals(true, list.getBoolean(0));
262 
263     assertTrue(list.addAll(asList(false, true, false, true, false)));
264     assertEquals(asList(true, false, true, false, true, false), list);
265 
266     assertTrue(list.addAll(TERTIARY_LIST));
267     assertEquals(asList(true, false, true, false, true, false, true, false, true), list);
268 
269     assertFalse(list.addAll(Collections.<Boolean>emptyList()));
270     assertFalse(list.addAll(BooleanArrayList.emptyList()));
271   }
272 
testRemove()273   public void testRemove() {
274     list.addAll(TERTIARY_LIST);
275     assertEquals(true, (boolean) list.remove(0));
276     assertEquals(asList(false, true), list);
277 
278     assertTrue(list.remove(Boolean.TRUE));
279     assertEquals(asList(false), list);
280 
281     assertFalse(list.remove(Boolean.TRUE));
282     assertEquals(asList(false), list);
283 
284     assertEquals(false, (boolean) 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(BooleanArrayList list)301   private void assertImmutable(BooleanArrayList list) {
302 
303     try {
304       list.add(true);
305       fail();
306     } catch (UnsupportedOperationException e) {
307       // expected
308     }
309 
310     try {
311       list.add(0, true);
312       fail();
313     } catch (UnsupportedOperationException e) {
314       // expected
315     }
316 
317     try {
318       list.addAll(Collections.<Boolean>emptyList());
319       fail();
320     } catch (UnsupportedOperationException e) {
321       // expected
322     }
323 
324     try {
325       list.addAll(Collections.singletonList(true));
326       fail();
327     } catch (UnsupportedOperationException e) {
328       // expected
329     }
330 
331     try {
332       list.addAll(new BooleanArrayList());
333       fail();
334     } catch (UnsupportedOperationException e) {
335       // expected
336     }
337 
338     try {
339       list.addAll(UNARY_LIST);
340       fail();
341     } catch (UnsupportedOperationException e) {
342       // expected
343     }
344 
345     try {
346       list.addAll(0, Collections.singleton(true));
347       fail();
348     } catch (UnsupportedOperationException e) {
349       // expected
350     }
351 
352     try {
353       list.addAll(0, UNARY_LIST);
354       fail();
355     } catch (UnsupportedOperationException e) {
356       // expected
357     }
358 
359     try {
360       list.addAll(0, Collections.<Boolean>emptyList());
361       fail();
362     } catch (UnsupportedOperationException e) {
363       // expected
364     }
365 
366     try {
367       list.addBoolean(false);
368       fail();
369     } catch (UnsupportedOperationException e) {
370       // expected
371     }
372 
373     try {
374       list.clear();
375       fail();
376     } catch (UnsupportedOperationException e) {
377       // expected
378     }
379 
380     try {
381       list.remove(1);
382       fail();
383     } catch (UnsupportedOperationException e) {
384       // expected
385     }
386 
387     try {
388       list.remove(new Object());
389       fail();
390     } catch (UnsupportedOperationException e) {
391       // expected
392     }
393 
394     try {
395       list.removeAll(Collections.<Boolean>emptyList());
396       fail();
397     } catch (UnsupportedOperationException e) {
398       // expected
399     }
400 
401     try {
402       list.removeAll(Collections.singleton(Boolean.TRUE));
403       fail();
404     } catch (UnsupportedOperationException e) {
405       // expected
406     }
407 
408     try {
409       list.removeAll(UNARY_LIST);
410       fail();
411     } catch (UnsupportedOperationException e) {
412       // expected
413     }
414 
415     try {
416       list.retainAll(Collections.<Boolean>emptyList());
417       fail();
418     } catch (UnsupportedOperationException e) {
419       // expected
420     }
421 
422     try {
423       list.removeAll(Collections.singleton(Boolean.TRUE));
424       fail();
425     } catch (UnsupportedOperationException e) {
426       // expected
427     }
428 
429     try {
430       list.retainAll(UNARY_LIST);
431       fail();
432     } catch (UnsupportedOperationException e) {
433       // expected
434     }
435 
436     try {
437       list.set(0, false);
438       fail();
439     } catch (UnsupportedOperationException e) {
440       // expected
441     }
442 
443     try {
444       list.setBoolean(0, false);
445       fail();
446     } catch (UnsupportedOperationException e) {
447       // expected
448     }
449   }
450 
newImmutableBooleanArrayList(boolean... elements)451   private static BooleanArrayList newImmutableBooleanArrayList(boolean... elements) {
452     BooleanArrayList list = new BooleanArrayList();
453     for (boolean element : elements) {
454       list.addBoolean(element);
455     }
456     list.makeImmutable();
457     return list;
458   }
459 }
460