1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
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
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.
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.
31 package com.google.protobuf;
33 import static java.util.Arrays.asList;
35 import com.google.protobuf.Internal.FloatList;
36 import java.util.Collections;
37 import java.util.ConcurrentModificationException;
38 import java.util.Iterator;
39 import junit.framework.TestCase;
42 * Tests for {@link FloatArrayList}.
44 * @author dweis@google.com (Daniel Weis)
46 public class FloatArrayListTest extends TestCase {
48 private static final FloatArrayList UNARY_LIST = newImmutableFloatArrayList(1);
49 private static final FloatArrayList TERTIARY_LIST = newImmutableFloatArrayList(1, 2, 3);
51 private FloatArrayList list;
54 protected void setUp() throws Exception {
55 list = new FloatArrayList();
58 public void testEmptyListReturnsSameInstance() {
59 assertSame(FloatArrayList.emptyList(), FloatArrayList.emptyList());
62 public void testEmptyListIsImmutable() {
63 assertImmutable(FloatArrayList.emptyList());
66 public void testMakeImmutable() {
72 assertImmutable(list);
75 public void testModificationWithIteration() {
76 list.addAll(asList(1F, 2F, 3F, 4F));
77 Iterator<Float> iterator = list.iterator();
78 assertEquals(4, list.size());
79 assertEquals(1F, (float) list.get(0), 0.0f);
80 assertEquals(1F, (float) iterator.next(), 0.0f);
82 assertEquals(2F, (float) iterator.next(), 0.0f);
88 } catch (ConcurrentModificationException e) {
92 iterator = list.iterator();
97 } catch (ConcurrentModificationException e) {
102 public void testGet() {
103 assertEquals(1F, (float) TERTIARY_LIST.get(0), 0.0f);
104 assertEquals(2F, (float) TERTIARY_LIST.get(1), 0.0f);
105 assertEquals(3F, (float) TERTIARY_LIST.get(2), 0.0f);
108 TERTIARY_LIST.get(-1);
110 } catch (IndexOutOfBoundsException e) {
115 TERTIARY_LIST.get(3);
117 } catch (IndexOutOfBoundsException e) {
122 public void testGetFloat() {
123 assertEquals(1F, TERTIARY_LIST.getFloat(0), 0.0f);
124 assertEquals(2F, TERTIARY_LIST.getFloat(1), 0.0f);
125 assertEquals(3F, TERTIARY_LIST.getFloat(2), 0.0f);
128 TERTIARY_LIST.get(-1);
130 } catch (IndexOutOfBoundsException e) {
135 TERTIARY_LIST.get(3);
137 } catch (IndexOutOfBoundsException e) {
142 public void testSize() {
143 assertEquals(0, FloatArrayList.emptyList().size());
144 assertEquals(1, UNARY_LIST.size());
145 assertEquals(3, TERTIARY_LIST.size());
151 assertEquals(4, list.size());
154 assertEquals(3, list.size());
157 assertEquals(4, list.size());
160 public void testSet() {
164 assertEquals(2F, (float) list.set(0, 3F), 0.0f);
165 assertEquals(3F, list.getFloat(0), 0.0f);
167 assertEquals(4F, (float) list.set(1, 0F), 0.0f);
168 assertEquals(0F, list.getFloat(1), 0.0f);
173 } catch (IndexOutOfBoundsException e) {
180 } catch (IndexOutOfBoundsException e) {
185 public void testSetFloat() {
189 assertEquals(1F, list.setFloat(0, 0), 0.0f);
190 assertEquals(0F, list.getFloat(0), 0.0f);
192 assertEquals(3F, list.setFloat(1, 0), 0.0f);
193 assertEquals(0F, list.getFloat(1), 0.0f);
196 list.setFloat(-1, 0);
198 } catch (IndexOutOfBoundsException e) {
205 } catch (IndexOutOfBoundsException e) {
210 public void testAdd() {
211 assertEquals(0, list.size());
213 assertTrue(list.add(2F));
214 assertEquals(asList(2F), list);
216 assertTrue(list.add(3F));
218 assertEquals(asList(4F, 2F, 3F), list);
222 // Force a resize by getting up to 11 elements.
223 for (int i = 0; i < 6; i++) {
224 list.add(Float.valueOf(5 + i));
226 assertEquals(asList(0F, 1F, 4F, 2F, 3F, 5F, 6F, 7F, 8F, 9F, 10F), list);
230 } catch (IndexOutOfBoundsException e) {
236 } catch (IndexOutOfBoundsException e) {
241 public void testAddFloat() {
242 assertEquals(0, list.size());
245 assertEquals(asList(2F), list);
248 assertEquals(asList(2F, 3F), list);
251 public void testAddAll() {
252 assertEquals(0, list.size());
254 assertTrue(list.addAll(Collections.singleton(1F)));
255 assertEquals(1, list.size());
256 assertEquals(1F, (float) list.get(0), 0.0f);
257 assertEquals(1F, list.getFloat(0), 0.0f);
259 assertTrue(list.addAll(asList(2F, 3F, 4F, 5F, 6F)));
260 assertEquals(asList(1F, 2F, 3F, 4F, 5F, 6F), list);
262 assertTrue(list.addAll(TERTIARY_LIST));
263 assertEquals(asList(1F, 2F, 3F, 4F, 5F, 6F, 1F, 2F, 3F), list);
265 assertFalse(list.addAll(Collections.<Float>emptyList()));
266 assertFalse(list.addAll(FloatArrayList.emptyList()));
269 public void testEquals() {
270 FloatArrayList list1 = new FloatArrayList();
271 FloatArrayList list2 = new FloatArrayList();
273 list1.addFloat(Float.intBitsToFloat(0xff800001));
274 list2.addFloat(Float.intBitsToFloat(0xff800002));
275 assertEquals(list1, list2);
278 public void testRemove() {
279 list.addAll(TERTIARY_LIST);
280 assertEquals(1F, (float) list.remove(0), 0.0f);
281 assertEquals(asList(2F, 3F), list);
283 assertTrue(list.remove(Float.valueOf(3)));
284 assertEquals(asList(2F), list);
286 assertFalse(list.remove(Float.valueOf(3)));
287 assertEquals(asList(2F), list);
289 assertEquals(2F, (float) list.remove(0), 0.0f);
290 assertEquals(asList(), list);
295 } catch (IndexOutOfBoundsException e) {
301 } catch (IndexOutOfBoundsException e) {
306 public void testRemoveEnd_listAtCapacity() {
307 FloatList toRemove = FloatArrayList.emptyList().mutableCopyWithCapacity(1);
308 toRemove.addFloat(3);
310 assertEquals(0, toRemove.size());
313 public void testRemove_listAtCapacity() {
314 FloatList toRemove = FloatArrayList.emptyList().mutableCopyWithCapacity(2);
315 toRemove.addFloat(3);
316 toRemove.addFloat(4);
318 assertEquals(1, toRemove.size());
319 assertEquals(4F, (float) toRemove.get(0));
322 public void testSublistRemoveEndOfCapacity() {
323 FloatList toRemove = FloatArrayList.emptyList().mutableCopyWithCapacity(1);
324 toRemove.addFloat(3);
325 toRemove.subList(0, 1).clear();
326 assertEquals(0, toRemove.size());
329 private void assertImmutable(FloatList list) {
330 if (list.contains(1F)) {
331 throw new RuntimeException("Cannot test the immutability of lists that contain 1.");
337 } catch (UnsupportedOperationException e) {
344 } catch (UnsupportedOperationException e) {
349 list.addAll(Collections.<Float>emptyList());
351 } catch (UnsupportedOperationException e) {
356 list.addAll(Collections.singletonList(1F));
358 } catch (UnsupportedOperationException e) {
363 list.addAll(new FloatArrayList());
365 } catch (UnsupportedOperationException e) {
370 list.addAll(UNARY_LIST);
372 } catch (UnsupportedOperationException e) {
377 list.addAll(0, Collections.singleton(1F));
379 } catch (UnsupportedOperationException e) {
384 list.addAll(0, UNARY_LIST);
386 } catch (UnsupportedOperationException e) {
391 list.addAll(0, Collections.<Float>emptyList());
393 } catch (UnsupportedOperationException e) {
400 } catch (UnsupportedOperationException e) {
407 } catch (UnsupportedOperationException e) {
414 } catch (UnsupportedOperationException e) {
419 list.remove(new Object());
421 } catch (UnsupportedOperationException e) {
426 list.removeAll(Collections.<Float>emptyList());
428 } catch (UnsupportedOperationException e) {
433 list.removeAll(Collections.singleton(1F));
435 } catch (UnsupportedOperationException e) {
440 list.removeAll(UNARY_LIST);
442 } catch (UnsupportedOperationException e) {
447 list.retainAll(Collections.<Float>emptyList());
449 } catch (UnsupportedOperationException e) {
454 list.retainAll(Collections.singleton(1F));
456 } catch (UnsupportedOperationException e) {
461 list.retainAll(UNARY_LIST);
463 } catch (UnsupportedOperationException e) {
470 } catch (UnsupportedOperationException e) {
477 } catch (UnsupportedOperationException e) {
482 private static FloatArrayList newImmutableFloatArrayList(float... elements) {
483 FloatArrayList list = new FloatArrayList();
484 for (float element : elements) {
485 list.addFloat(element);
487 list.makeImmutable();