Imported Upstream version 3.8.0
[platform/upstream/protobuf.git] / java / core / src / test / java / com / google / protobuf / FloatArrayListTest.java
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 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;
40
41 /**
42  * Tests for {@link FloatArrayList}.
43  *
44  * @author dweis@google.com (Daniel Weis)
45  */
46 public class FloatArrayListTest extends TestCase {
47
48   private static final FloatArrayList UNARY_LIST = newImmutableFloatArrayList(1);
49   private static final FloatArrayList TERTIARY_LIST = newImmutableFloatArrayList(1, 2, 3);
50
51   private FloatArrayList list;
52
53   @Override
54   protected void setUp() throws Exception {
55     list = new FloatArrayList();
56   }
57
58   public void testEmptyListReturnsSameInstance() {
59     assertSame(FloatArrayList.emptyList(), FloatArrayList.emptyList());
60   }
61
62   public void testEmptyListIsImmutable() {
63     assertImmutable(FloatArrayList.emptyList());
64   }
65
66   public void testMakeImmutable() {
67     list.addFloat(3);
68     list.addFloat(4);
69     list.addFloat(5);
70     list.addFloat(7);
71     list.makeImmutable();
72     assertImmutable(list);
73   }
74
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);
81     list.set(0, 1F);
82     assertEquals(2F, (float) iterator.next(), 0.0f);
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, 0F);
94     try {
95       iterator.next();
96       fail();
97     } catch (ConcurrentModificationException e) {
98       // expected
99     }
100   }
101
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);
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 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);
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, FloatArrayList.emptyList().size());
144     assertEquals(1, UNARY_LIST.size());
145     assertEquals(3, TERTIARY_LIST.size());
146
147     list.addFloat(3);
148     list.addFloat(4);
149     list.addFloat(6);
150     list.addFloat(8);
151     assertEquals(4, list.size());
152
153     list.remove(0);
154     assertEquals(3, list.size());
155
156     list.add(17F);
157     assertEquals(4, list.size());
158   }
159
160   public void testSet() {
161     list.addFloat(2);
162     list.addFloat(4);
163
164     assertEquals(2F, (float) list.set(0, 3F), 0.0f);
165     assertEquals(3F, list.getFloat(0), 0.0f);
166
167     assertEquals(4F, (float) list.set(1, 0F), 0.0f);
168     assertEquals(0F, list.getFloat(1), 0.0f);
169
170     try {
171       list.set(-1, 0F);
172       fail();
173     } catch (IndexOutOfBoundsException e) {
174       // expected
175     }
176
177     try {
178       list.set(2, 0F);
179       fail();
180     } catch (IndexOutOfBoundsException e) {
181       // expected
182     }
183   }
184
185   public void testSetFloat() {
186     list.addFloat(1);
187     list.addFloat(3);
188
189     assertEquals(1F, list.setFloat(0, 0), 0.0f);
190     assertEquals(0F, list.getFloat(0), 0.0f);
191
192     assertEquals(3F, list.setFloat(1, 0), 0.0f);
193     assertEquals(0F, list.getFloat(1), 0.0f);
194
195     try {
196       list.setFloat(-1, 0);
197       fail();
198     } catch (IndexOutOfBoundsException e) {
199       // expected
200     }
201
202     try {
203       list.setFloat(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(2F));
214     assertEquals(asList(2F), list);
215
216     assertTrue(list.add(3F));
217     list.add(0, 4F);
218     assertEquals(asList(4F, 2F, 3F), list);
219
220     list.add(0, 1F);
221     list.add(0, 0F);
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));
225     }
226     assertEquals(asList(0F, 1F, 4F, 2F, 3F, 5F, 6F, 7F, 8F, 9F, 10F), list);
227
228     try {
229       list.add(-1, 5F);
230     } catch (IndexOutOfBoundsException e) {
231       // expected
232     }
233
234     try {
235       list.add(4, 5F);
236     } catch (IndexOutOfBoundsException e) {
237       // expected
238     }
239   }
240
241   public void testAddFloat() {
242     assertEquals(0, list.size());
243
244     list.addFloat(2);
245     assertEquals(asList(2F), list);
246
247     list.addFloat(3);
248     assertEquals(asList(2F, 3F), list);
249   }
250
251   public void testAddAll() {
252     assertEquals(0, list.size());
253
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);
258
259     assertTrue(list.addAll(asList(2F, 3F, 4F, 5F, 6F)));
260     assertEquals(asList(1F, 2F, 3F, 4F, 5F, 6F), list);
261
262     assertTrue(list.addAll(TERTIARY_LIST));
263     assertEquals(asList(1F, 2F, 3F, 4F, 5F, 6F, 1F, 2F, 3F), list);
264
265     assertFalse(list.addAll(Collections.<Float>emptyList()));
266     assertFalse(list.addAll(FloatArrayList.emptyList()));
267   }
268
269   public void testEquals() {
270     FloatArrayList list1 = new FloatArrayList();
271     FloatArrayList list2 = new FloatArrayList();
272
273     list1.addFloat(Float.intBitsToFloat(0xff800001));
274     list2.addFloat(Float.intBitsToFloat(0xff800002));
275     assertEquals(list1, list2);
276   }
277
278   public void testRemove() {
279     list.addAll(TERTIARY_LIST);
280     assertEquals(1F, (float) list.remove(0), 0.0f);
281     assertEquals(asList(2F, 3F), list);
282
283     assertTrue(list.remove(Float.valueOf(3)));
284     assertEquals(asList(2F), list);
285
286     assertFalse(list.remove(Float.valueOf(3)));
287     assertEquals(asList(2F), list);
288
289     assertEquals(2F, (float) list.remove(0), 0.0f);
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
306   public void testRemoveEnd_listAtCapacity() {
307     FloatList toRemove = FloatArrayList.emptyList().mutableCopyWithCapacity(1);
308     toRemove.addFloat(3);
309     toRemove.remove(0);
310     assertEquals(0, toRemove.size());
311   }
312
313   public void testRemove_listAtCapacity() {
314     FloatList toRemove = FloatArrayList.emptyList().mutableCopyWithCapacity(2);
315     toRemove.addFloat(3);
316     toRemove.addFloat(4);
317     toRemove.remove(0);
318     assertEquals(1, toRemove.size());
319     assertEquals(4F, (float) toRemove.get(0));
320   }
321
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());
327   }
328
329   private void assertImmutable(FloatList list) {
330     if (list.contains(1F)) {
331       throw new RuntimeException("Cannot test the immutability of lists that contain 1.");
332     }
333
334     try {
335       list.add(1F);
336       fail();
337     } catch (UnsupportedOperationException e) {
338       // expected
339     }
340
341     try {
342       list.add(0, 1F);
343       fail();
344     } catch (UnsupportedOperationException e) {
345       // expected
346     }
347
348     try {
349       list.addAll(Collections.<Float>emptyList());
350       fail();
351     } catch (UnsupportedOperationException e) {
352       // expected
353     }
354
355     try {
356       list.addAll(Collections.singletonList(1F));
357       fail();
358     } catch (UnsupportedOperationException e) {
359       // expected
360     }
361
362     try {
363       list.addAll(new FloatArrayList());
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(1F));
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.<Float>emptyList());
392       fail();
393     } catch (UnsupportedOperationException e) {
394       // expected
395     }
396
397     try {
398       list.addFloat(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.<Float>emptyList());
427       fail();
428     } catch (UnsupportedOperationException e) {
429       // expected
430     }
431
432     try {
433       list.removeAll(Collections.singleton(1F));
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.<Float>emptyList());
448       fail();
449     } catch (UnsupportedOperationException e) {
450       // expected
451     }
452
453     try {
454       list.retainAll(Collections.singleton(1F));
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, 0F);
469       fail();
470     } catch (UnsupportedOperationException e) {
471       // expected
472     }
473
474     try {
475       list.setFloat(0, 0);
476       fail();
477     } catch (UnsupportedOperationException e) {
478       // expected
479     }
480   }
481
482   private static FloatArrayList newImmutableFloatArrayList(float... elements) {
483     FloatArrayList list = new FloatArrayList();
484     for (float element : elements) {
485       list.addFloat(element);
486     }
487     list.makeImmutable();
488     return list;
489   }
490 }