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


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