BooleanArrayListTest.java
Go to the documentation of this file.
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 
36 import java.util.Collections;
37 import java.util.ConcurrentModificationException;
38 import java.util.Iterator;
39 import junit.framework.TestCase;
40 
46 public class BooleanArrayListTest extends TestCase {
47 
48  private static final BooleanArrayList UNARY_LIST = newImmutableBooleanArrayList(true);
49  private static final BooleanArrayList TERTIARY_LIST =
50  newImmutableBooleanArrayList(true, false, true);
51 
52  private BooleanArrayList list;
53 
54  @Override
55  protected void setUp() throws Exception {
56  list = new BooleanArrayList();
57  }
58 
60  assertSame(BooleanArrayList.emptyList(), BooleanArrayList.emptyList());
61  }
62 
63  public void testEmptyListIsImmutable() {
64  assertImmutable(BooleanArrayList.emptyList());
65  }
66 
67  public void testMakeImmutable() {
68  list.addBoolean(true);
69  list.addBoolean(false);
70  list.addBoolean(true);
71  list.addBoolean(true);
72  list.makeImmutable();
74  }
75 
77  list.addAll(asList(true, false, true, false));
78  Iterator<Boolean> iterator = list.iterator();
79  assertEquals(4, list.size());
80  assertEquals(true, (boolean) list.get(0));
81  assertEquals(true, (boolean) iterator.next());
82  list.set(0, true);
83  assertEquals(false, (boolean) iterator.next());
84 
85  list.remove(0);
86  try {
87  iterator.next();
88  fail();
89  } catch (ConcurrentModificationException e) {
90  // expected
91  }
92 
93  iterator = list.iterator();
94  list.add(0, false);
95  try {
96  iterator.next();
97  fail();
98  } catch (ConcurrentModificationException e) {
99  // expected
100  }
101  }
102 
103  public void testGet() {
104  assertEquals(true, (boolean) TERTIARY_LIST.get(0));
105  assertEquals(false, (boolean) TERTIARY_LIST.get(1));
106  assertEquals(true, (boolean) TERTIARY_LIST.get(2));
107 
108  try {
109  TERTIARY_LIST.get(-1);
110  fail();
111  } catch (IndexOutOfBoundsException e) {
112  // expected
113  }
114 
115  try {
116  TERTIARY_LIST.get(3);
117  fail();
118  } catch (IndexOutOfBoundsException e) {
119  // expected
120  }
121  }
122 
123  public void testGetBoolean() {
124  assertEquals(true, TERTIARY_LIST.getBoolean(0));
125  assertEquals(false, TERTIARY_LIST.getBoolean(1));
126  assertEquals(true, TERTIARY_LIST.getBoolean(2));
127 
128  try {
129  TERTIARY_LIST.get(-1);
130  fail();
131  } catch (IndexOutOfBoundsException e) {
132  // expected
133  }
134 
135  try {
136  TERTIARY_LIST.get(3);
137  fail();
138  } catch (IndexOutOfBoundsException e) {
139  // expected
140  }
141  }
142 
143  public void testSize() {
144  assertEquals(0, BooleanArrayList.emptyList().size());
145  assertEquals(1, UNARY_LIST.size());
146  assertEquals(3, TERTIARY_LIST.size());
147 
148  list.addBoolean(true);
149  list.addBoolean(false);
150  list.addBoolean(false);
151  list.addBoolean(false);
152  assertEquals(4, list.size());
153 
154  list.remove(0);
155  assertEquals(3, list.size());
156 
157  list.add(true);
158  assertEquals(4, list.size());
159  }
160 
161  public void testSet() {
162  list.addBoolean(false);
163  list.addBoolean(false);
164 
165  assertEquals(false, (boolean) list.set(0, true));
166  assertEquals(true, list.getBoolean(0));
167 
168  assertEquals(false, (boolean) list.set(1, false));
169  assertEquals(false, list.getBoolean(1));
170 
171  try {
172  list.set(-1, false);
173  fail();
174  } catch (IndexOutOfBoundsException e) {
175  // expected
176  }
177 
178  try {
179  list.set(2, false);
180  fail();
181  } catch (IndexOutOfBoundsException e) {
182  // expected
183  }
184  }
185 
186  public void testSetBoolean() {
187  list.addBoolean(true);
188  list.addBoolean(true);
189 
190  assertEquals(true, list.setBoolean(0, false));
191  assertEquals(false, list.getBoolean(0));
192 
193  assertEquals(true, list.setBoolean(1, false));
194  assertEquals(false, list.getBoolean(1));
195 
196  try {
197  list.setBoolean(-1, false);
198  fail();
199  } catch (IndexOutOfBoundsException e) {
200  // expected
201  }
202 
203  try {
204  list.setBoolean(2, false);
205  fail();
206  } catch (IndexOutOfBoundsException e) {
207  // expected
208  }
209  }
210 
211  public void testAdd() {
212  assertEquals(0, list.size());
213 
214  assertTrue(list.add(false));
215  assertEquals(asList(false), list);
216 
217  assertTrue(list.add(true));
218  list.add(0, false);
219  assertEquals(asList(false, false, true), list);
220 
221  list.add(0, true);
222  list.add(0, false);
223  // Force a resize by getting up to 11 elements.
224  for (int i = 0; i < 6; i++) {
225  list.add(i % 2 == 0);
226  }
227  assertEquals(
228  asList(false, true, false, false, true, true, false, true, false, true, false), list);
229 
230  try {
231  list.add(-1, true);
232  } catch (IndexOutOfBoundsException e) {
233  // expected
234  }
235 
236  try {
237  list.add(4, true);
238  } catch (IndexOutOfBoundsException e) {
239  // expected
240  }
241  }
242 
243  public void testAddBoolean() {
244  assertEquals(0, list.size());
245 
246  list.addBoolean(false);
247  assertEquals(asList(false), list);
248 
249  list.addBoolean(true);
250  assertEquals(asList(false, true), list);
251  }
252 
253  public void testAddAll() {
254  assertEquals(0, list.size());
255 
256  assertTrue(list.addAll(Collections.singleton(true)));
257  assertEquals(1, list.size());
258  assertEquals(true, (boolean) list.get(0));
259  assertEquals(true, list.getBoolean(0));
260 
261  assertTrue(list.addAll(asList(false, true, false, true, false)));
262  assertEquals(asList(true, false, true, false, true, false), list);
263 
264  assertTrue(list.addAll(TERTIARY_LIST));
265  assertEquals(asList(true, false, true, false, true, false, true, false, true), list);
266 
267  assertFalse(list.addAll(Collections.<Boolean>emptyList()));
268  assertFalse(list.addAll(BooleanArrayList.emptyList()));
269  }
270 
271  public void testEquals() {
272  BooleanArrayList list1 = new BooleanArrayList();
273  BooleanArrayList list2 = new BooleanArrayList();
274 
275  assertEquals(list1, list2);
276  }
277 
278  public void testRemove() {
279  list.addAll(TERTIARY_LIST);
280  assertEquals(true, (boolean) list.remove(0));
281  assertEquals(asList(false, true), list);
282 
283  assertTrue(list.remove(Boolean.TRUE));
284  assertEquals(asList(false), list);
285 
286  assertFalse(list.remove(Boolean.TRUE));
287  assertEquals(asList(false), list);
288 
289  assertEquals(false, (boolean) list.remove(0));
290  assertEquals(asList(), list);
291 
292  try {
293  list.remove(-1);
294  fail();
295  } catch (IndexOutOfBoundsException e) {
296  // expected
297  }
298 
299  try {
300  list.remove(0);
301  } catch (IndexOutOfBoundsException e) {
302  // expected
303  }
304  }
305 
307  BooleanList toRemove = BooleanArrayList.emptyList().mutableCopyWithCapacity(1);
308  toRemove.addBoolean(true);
309  toRemove.remove(0);
310  assertEquals(0, toRemove.size());
311  }
312 
314  BooleanList toRemove = BooleanArrayList.emptyList().mutableCopyWithCapacity(2);
315  toRemove.addBoolean(true);
316  toRemove.addBoolean(false);
317  toRemove.remove(0);
318  assertEquals(1, toRemove.size());
319  assertEquals(false, (boolean) toRemove.get(0));
320  }
321 
323  BooleanList toRemove = BooleanArrayList.emptyList().mutableCopyWithCapacity(1);
324  toRemove.addBoolean(true);
325  toRemove.subList(0, 1).clear();
326  assertEquals(0, toRemove.size());
327  }
328 
330 
331  try {
332  list.add(true);
333  fail();
334  } catch (UnsupportedOperationException e) {
335  // expected
336  }
337 
338  try {
339  list.add(0, true);
340  fail();
341  } catch (UnsupportedOperationException e) {
342  // expected
343  }
344 
345  try {
346  list.addAll(Collections.<Boolean>emptyList());
347  fail();
348  } catch (UnsupportedOperationException e) {
349  // expected
350  }
351 
352  try {
353  list.addAll(Collections.singletonList(true));
354  fail();
355  } catch (UnsupportedOperationException e) {
356  // expected
357  }
358 
359  try {
360  list.addAll(new BooleanArrayList());
361  fail();
362  } catch (UnsupportedOperationException e) {
363  // expected
364  }
365 
366  try {
367  list.addAll(UNARY_LIST);
368  fail();
369  } catch (UnsupportedOperationException e) {
370  // expected
371  }
372 
373  try {
374  list.addAll(0, Collections.singleton(true));
375  fail();
376  } catch (UnsupportedOperationException e) {
377  // expected
378  }
379 
380  try {
381  list.addAll(0, UNARY_LIST);
382  fail();
383  } catch (UnsupportedOperationException e) {
384  // expected
385  }
386 
387  try {
388  list.addAll(0, Collections.<Boolean>emptyList());
389  fail();
390  } catch (UnsupportedOperationException e) {
391  // expected
392  }
393 
394  try {
395  list.addBoolean(false);
396  fail();
397  } catch (UnsupportedOperationException e) {
398  // expected
399  }
400 
401  try {
402  list.clear();
403  fail();
404  } catch (UnsupportedOperationException e) {
405  // expected
406  }
407 
408  try {
409  list.remove(1);
410  fail();
411  } catch (UnsupportedOperationException e) {
412  // expected
413  }
414 
415  try {
416  list.remove(new Object());
417  fail();
418  } catch (UnsupportedOperationException e) {
419  // expected
420  }
421 
422  try {
423  list.removeAll(Collections.<Boolean>emptyList());
424  fail();
425  } catch (UnsupportedOperationException e) {
426  // expected
427  }
428 
429  try {
430  list.removeAll(Collections.singleton(Boolean.TRUE));
431  fail();
432  } catch (UnsupportedOperationException e) {
433  // expected
434  }
435 
436  try {
437  list.removeAll(UNARY_LIST);
438  fail();
439  } catch (UnsupportedOperationException e) {
440  // expected
441  }
442 
443  try {
444  list.retainAll(Collections.<Boolean>emptyList());
445  fail();
446  } catch (UnsupportedOperationException e) {
447  // expected
448  }
449 
450  try {
451  list.removeAll(Collections.singleton(Boolean.TRUE));
452  fail();
453  } catch (UnsupportedOperationException e) {
454  // expected
455  }
456 
457  try {
458  list.retainAll(UNARY_LIST);
459  fail();
460  } catch (UnsupportedOperationException e) {
461  // expected
462  }
463 
464  try {
465  list.set(0, false);
466  fail();
467  } catch (UnsupportedOperationException e) {
468  // expected
469  }
470 
471  try {
472  list.setBoolean(0, false);
473  fail();
474  } catch (UnsupportedOperationException e) {
475  // expected
476  }
477  }
478 
479  private static BooleanArrayList newImmutableBooleanArrayList(boolean... elements) {
480  BooleanArrayList list = new BooleanArrayList();
481  for (boolean element : elements) {
482  list.addBoolean(element);
483  }
484  list.makeImmutable();
485  return list;
486  }
487 }
com.google.protobuf.BooleanArrayListTest.newImmutableBooleanArrayList
static BooleanArrayList newImmutableBooleanArrayList(boolean... elements)
Definition: BooleanArrayListTest.java:479
com.google.protobuf.BooleanArrayListTest.testAddBoolean
void testAddBoolean()
Definition: BooleanArrayListTest.java:243
com.google.protobuf.BooleanArrayListTest.testMakeImmutable
void testMakeImmutable()
Definition: BooleanArrayListTest.java:67
com.google.protobuf.BooleanArrayListTest.testEmptyListIsImmutable
void testEmptyListIsImmutable()
Definition: BooleanArrayListTest.java:63
com.google.protobuf
Definition: ProtoCaliperBenchmark.java:2
com.google.protobuf.BooleanArrayListTest.UNARY_LIST
static final BooleanArrayList UNARY_LIST
Definition: BooleanArrayListTest.java:48
com.google.protobuf.BooleanArrayListTest.testSublistRemoveEndOfCapacity
void testSublistRemoveEndOfCapacity()
Definition: BooleanArrayListTest.java:322
com.google.protobuf.BooleanArrayListTest.testAddAll
void testAddAll()
Definition: BooleanArrayListTest.java:253
com.google.protobuf.BooleanArrayListTest.testAdd
void testAdd()
Definition: BooleanArrayListTest.java:211
com.google.protobuf.Internal.BooleanList
Definition: Internal.java:611
com.google.protobuf.BooleanArrayListTest.testSetBoolean
void testSetBoolean()
Definition: BooleanArrayListTest.java:186
com.google.protobuf.BooleanArrayListTest.assertImmutable
void assertImmutable(BooleanList list)
Definition: BooleanArrayListTest.java:329
com.google.protobuf.BooleanArrayListTest.testSet
void testSet()
Definition: BooleanArrayListTest.java:161
i
int i
Definition: gmock-matchers_test.cc:764
com.google.protobuf.BooleanArrayListTest.testEquals
void testEquals()
Definition: BooleanArrayListTest.java:271
java
com.google.protobuf.BooleanArrayListTest.testRemove_listAtCapacity
void testRemove_listAtCapacity()
Definition: BooleanArrayListTest.java:313
com.google.protobuf.Internal.BooleanList.mutableCopyWithCapacity
BooleanList mutableCopyWithCapacity(int capacity)
com.google.protobuf.BooleanArrayListTest.testGet
void testGet()
Definition: BooleanArrayListTest.java:103
com.google.protobuf.Internal.BooleanList.addBoolean
void addBoolean(boolean element)
com.google.protobuf.BooleanArrayListTest.setUp
void setUp()
Definition: BooleanArrayListTest.java:55
com.google.protobuf.BooleanArrayListTest
Definition: BooleanArrayListTest.java:46
com.google
com
com.google.protobuf.BooleanArrayListTest.testModificationWithIteration
void testModificationWithIteration()
Definition: BooleanArrayListTest.java:76
com.google.protobuf.BooleanArrayListTest.list
BooleanArrayList list
Definition: BooleanArrayListTest.java:52
com.google.protobuf.Internal
Definition: Internal.java:54
com.google.protobuf.BooleanArrayListTest.testEmptyListReturnsSameInstance
void testEmptyListReturnsSameInstance()
Definition: BooleanArrayListTest.java:59
com.google.protobuf.BooleanArrayListTest.testRemove
void testRemove()
Definition: BooleanArrayListTest.java:278
gmock_test_utils.TestCase
TestCase
Definition: gmock_test_utils.py:97
com.google.protobuf.BooleanArrayListTest.testGetBoolean
void testGetBoolean()
Definition: BooleanArrayListTest.java:123
com.google.protobuf.BooleanArrayListTest.testSize
void testSize()
Definition: BooleanArrayListTest.java:143
com.google.protobuf.BooleanArrayListTest.TERTIARY_LIST
static final BooleanArrayList TERTIARY_LIST
Definition: BooleanArrayListTest.java:49
com.google.protobuf.BooleanArrayListTest.testRemoveEnd_listAtCapacity
void testRemoveEnd_listAtCapacity()
Definition: BooleanArrayListTest.java:306


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:48