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


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