1 /*
2  * Copyright (C) 2008 The Guava Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.common.primitives;
18 
19 import static java.lang.Float.NaN;
20 import static org.truth0.Truth.ASSERT;
21 
22 import com.google.common.annotations.GwtCompatible;
23 import com.google.common.annotations.GwtIncompatible;
24 import com.google.common.base.Converter;
25 import com.google.common.collect.ImmutableList;
26 import com.google.common.collect.testing.Helpers;
27 import com.google.common.testing.NullPointerTester;
28 import com.google.common.testing.SerializableTester;
29 
30 import junit.framework.TestCase;
31 
32 import java.util.Arrays;
33 import java.util.Collection;
34 import java.util.Collections;
35 import java.util.Comparator;
36 import java.util.List;
37 
38 /**
39  * Unit test for {@link Floats}.
40  *
41  * @author Kevin Bourrillion
42  */
43 @GwtCompatible(emulated = true)
44 @SuppressWarnings("cast") // redundant casts are intentional and harmless
45 public class FloatsTest extends TestCase {
46   private static final float[] EMPTY = {};
47   private static final float[] ARRAY1 = {(float) 1};
48   private static final float[] ARRAY234
49       = {(float) 2, (float) 3, (float) 4};
50 
51   private static final float LEAST = Float.NEGATIVE_INFINITY;
52   private static final float GREATEST = Float.POSITIVE_INFINITY;
53   private static final float MIN_NORMAL = 1.17549435E-38f; // Doubles.MIN_NORMAL from 1.6
54 
55   private static final float[] NUMBERS = new float[] {
56       LEAST, -Float.MAX_VALUE, -1f, -0f, 0f, 1f, Float.MAX_VALUE, GREATEST,
57       MIN_NORMAL, -MIN_NORMAL,  Float.MIN_VALUE, -Float.MIN_VALUE,
58       Integer.MIN_VALUE, Integer.MAX_VALUE, Long.MIN_VALUE, Long.MAX_VALUE
59   };
60 
61   private static final float[] VALUES
62       = Floats.concat(NUMBERS, new float[] {NaN});
63 
testHashCode()64   public void testHashCode() {
65     for (float value : VALUES) {
66       assertEquals(((Float) value).hashCode(), Floats.hashCode(value));
67     }
68   }
69 
testIsFinite()70   public void testIsFinite() {
71     for (float value : NUMBERS) {
72       assertEquals(!(Float.isInfinite(value) || Float.isNaN(value)), Floats.isFinite(value));
73     }
74   }
75 
testCompare()76   public void testCompare() {
77     for (float x : VALUES) {
78       for (float y : VALUES) {
79         // note: spec requires only that the sign is the same
80         assertEquals(x + ", " + y,
81                      Float.valueOf(x).compareTo(y),
82                      Floats.compare(x, y));
83       }
84     }
85   }
86 
testContains()87   public void testContains() {
88     assertFalse(Floats.contains(EMPTY, (float) 1));
89     assertFalse(Floats.contains(ARRAY1, (float) 2));
90     assertFalse(Floats.contains(ARRAY234, (float) 1));
91     assertTrue(Floats.contains(new float[] {(float) -1}, (float) -1));
92     assertTrue(Floats.contains(ARRAY234, (float) 2));
93     assertTrue(Floats.contains(ARRAY234, (float) 3));
94     assertTrue(Floats.contains(ARRAY234, (float) 4));
95 
96     for (float value : NUMBERS) {
97       assertTrue("" + value, Floats.contains(new float[] {5f, value}, value));
98     }
99     assertFalse(Floats.contains(new float[] {5f, NaN}, NaN));
100   }
101 
testIndexOf()102   public void testIndexOf() {
103     assertEquals(-1, Floats.indexOf(EMPTY, (float) 1));
104     assertEquals(-1, Floats.indexOf(ARRAY1, (float) 2));
105     assertEquals(-1, Floats.indexOf(ARRAY234, (float) 1));
106     assertEquals(0, Floats.indexOf(
107         new float[] {(float) -1}, (float) -1));
108     assertEquals(0, Floats.indexOf(ARRAY234, (float) 2));
109     assertEquals(1, Floats.indexOf(ARRAY234, (float) 3));
110     assertEquals(2, Floats.indexOf(ARRAY234, (float) 4));
111     assertEquals(1, Floats.indexOf(
112         new float[] { (float) 2, (float) 3, (float) 2, (float) 3 },
113         (float) 3));
114 
115     for (float value : NUMBERS) {
116       assertEquals("" + value, 1,
117           Floats.indexOf(new float[] {5f, value}, value));
118     }
119     assertEquals(-1, Floats.indexOf(new float[] {5f, NaN}, NaN));
120   }
121 
testIndexOf_arrayTarget()122   public void testIndexOf_arrayTarget() {
123     assertEquals(0, Floats.indexOf(EMPTY, EMPTY));
124     assertEquals(0, Floats.indexOf(ARRAY234, EMPTY));
125     assertEquals(-1, Floats.indexOf(EMPTY, ARRAY234));
126     assertEquals(-1, Floats.indexOf(ARRAY234, ARRAY1));
127     assertEquals(-1, Floats.indexOf(ARRAY1, ARRAY234));
128     assertEquals(0, Floats.indexOf(ARRAY1, ARRAY1));
129     assertEquals(0, Floats.indexOf(ARRAY234, ARRAY234));
130     assertEquals(0, Floats.indexOf(
131         ARRAY234, new float[] { (float) 2, (float) 3 }));
132     assertEquals(1, Floats.indexOf(
133         ARRAY234, new float[] { (float) 3, (float) 4 }));
134     assertEquals(1, Floats.indexOf(ARRAY234, new float[] { (float) 3 }));
135     assertEquals(2, Floats.indexOf(ARRAY234, new float[] { (float) 4 }));
136     assertEquals(1, Floats.indexOf(new float[] { (float) 2, (float) 3,
137         (float) 3, (float) 3, (float) 3 },
138         new float[] { (float) 3 }
139     ));
140     assertEquals(2, Floats.indexOf(
141         new float[] { (float) 2, (float) 3, (float) 2,
142             (float) 3, (float) 4, (float) 2, (float) 3},
143         new float[] { (float) 2, (float) 3, (float) 4}
144     ));
145     assertEquals(1, Floats.indexOf(
146         new float[] { (float) 2, (float) 2, (float) 3,
147             (float) 4, (float) 2, (float) 3, (float) 4},
148         new float[] { (float) 2, (float) 3, (float) 4}
149     ));
150     assertEquals(-1, Floats.indexOf(
151         new float[] { (float) 4, (float) 3, (float) 2},
152         new float[] { (float) 2, (float) 3, (float) 4}
153     ));
154 
155     for (float value : NUMBERS) {
156       assertEquals("" + value, 1, Floats.indexOf(
157           new float[] {5f, value, value, 5f}, new float[] {value, value}));
158     }
159     assertEquals(-1, Floats.indexOf(
160         new float[] {5f, NaN, NaN, 5f}, new float[] {NaN, NaN}));
161   }
162 
testLastIndexOf()163   public void testLastIndexOf() {
164     assertEquals(-1, Floats.lastIndexOf(EMPTY, (float) 1));
165     assertEquals(-1, Floats.lastIndexOf(ARRAY1, (float) 2));
166     assertEquals(-1, Floats.lastIndexOf(ARRAY234, (float) 1));
167     assertEquals(0, Floats.lastIndexOf(
168         new float[] {(float) -1}, (float) -1));
169     assertEquals(0, Floats.lastIndexOf(ARRAY234, (float) 2));
170     assertEquals(1, Floats.lastIndexOf(ARRAY234, (float) 3));
171     assertEquals(2, Floats.lastIndexOf(ARRAY234, (float) 4));
172     assertEquals(3, Floats.lastIndexOf(
173         new float[] { (float) 2, (float) 3, (float) 2, (float) 3 },
174         (float) 3));
175 
176     for (float value : NUMBERS) {
177       assertEquals("" + value,
178           0, Floats.lastIndexOf(new float[] {value, 5f}, value));
179     }
180     assertEquals(-1, Floats.lastIndexOf(new float[] {NaN, 5f}, NaN));
181   }
182 
testMax_noArgs()183   public void testMax_noArgs() {
184     try {
185       Floats.max();
186       fail();
187     } catch (IllegalArgumentException expected) {
188     }
189   }
190 
testMax()191   public void testMax() {
192     assertEquals(GREATEST, Floats.max(GREATEST));
193     assertEquals(LEAST, Floats.max(LEAST));
194     assertEquals((float) 9, Floats.max(
195         (float) 8, (float) 6, (float) 7,
196         (float) 5, (float) 3, (float) 0, (float) 9));
197 
198     assertEquals(0f, Floats.max(-0f, 0f));
199     assertEquals(0f, Floats.max(0f, -0f));
200     assertEquals(GREATEST, Floats.max(NUMBERS));
201     assertTrue(Float.isNaN(Floats.max(VALUES)));
202   }
203 
testMin_noArgs()204   public void testMin_noArgs() {
205     try {
206       Floats.min();
207       fail();
208     } catch (IllegalArgumentException expected) {
209     }
210   }
211 
testMin()212   public void testMin() {
213     assertEquals(LEAST, Floats.min(LEAST));
214     assertEquals(GREATEST, Floats.min(GREATEST));
215     assertEquals((float) 0, Floats.min(
216         (float) 8, (float) 6, (float) 7,
217         (float) 5, (float) 3, (float) 0, (float) 9));
218 
219     assertEquals(-0f, Floats.min(-0f, 0f));
220     assertEquals(-0f, Floats.min(0f, -0f));
221     assertEquals(LEAST, Floats.min(NUMBERS));
222     assertTrue(Float.isNaN(Floats.min(VALUES)));
223   }
224 
testConcat()225   public void testConcat() {
226     assertTrue(Arrays.equals(EMPTY, Floats.concat()));
227     assertTrue(Arrays.equals(EMPTY, Floats.concat(EMPTY)));
228     assertTrue(Arrays.equals(EMPTY, Floats.concat(EMPTY, EMPTY, EMPTY)));
229     assertTrue(Arrays.equals(ARRAY1, Floats.concat(ARRAY1)));
230     assertNotSame(ARRAY1, Floats.concat(ARRAY1));
231     assertTrue(Arrays.equals(ARRAY1, Floats.concat(EMPTY, ARRAY1, EMPTY)));
232     assertTrue(Arrays.equals(
233         new float[] {(float) 1, (float) 1, (float) 1},
234         Floats.concat(ARRAY1, ARRAY1, ARRAY1)));
235     assertTrue(Arrays.equals(
236         new float[] {(float) 1, (float) 2, (float) 3, (float) 4},
237         Floats.concat(ARRAY1, ARRAY234)));
238   }
239 
testEnsureCapacity()240   public void testEnsureCapacity() {
241     assertSame(EMPTY, Floats.ensureCapacity(EMPTY, 0, 1));
242     assertSame(ARRAY1, Floats.ensureCapacity(ARRAY1, 0, 1));
243     assertSame(ARRAY1, Floats.ensureCapacity(ARRAY1, 1, 1));
244     assertTrue(Arrays.equals(
245         new float[] {(float) 1, (float) 0, (float) 0},
246         Floats.ensureCapacity(ARRAY1, 2, 1)));
247   }
248 
testEnsureCapacity_fail()249   public void testEnsureCapacity_fail() {
250     try {
251       Floats.ensureCapacity(ARRAY1, -1, 1);
252       fail();
253     } catch (IllegalArgumentException expected) {
254     }
255     try {
256       // notice that this should even fail when no growth was needed
257       Floats.ensureCapacity(ARRAY1, 1, -1);
258       fail();
259     } catch (IllegalArgumentException expected) {
260     }
261   }
262 
263   @GwtIncompatible("Float.toString returns different value in GWT.")
testJoin()264   public void testJoin() {
265     assertEquals("", Floats.join(",", EMPTY));
266     assertEquals("1.0", Floats.join(",", ARRAY1));
267     assertEquals("1.0,2.0", Floats.join(",", (float) 1, (float) 2));
268     assertEquals("1.02.03.0",
269         Floats.join("", (float) 1, (float) 2, (float) 3));
270   }
271 
testLexicographicalComparator()272   public void testLexicographicalComparator() {
273     List<float[]> ordered = Arrays.asList(
274         new float[] {},
275         new float[] {LEAST},
276         new float[] {LEAST, LEAST},
277         new float[] {LEAST, (float) 1},
278         new float[] {(float) 1},
279         new float[] {(float) 1, LEAST},
280         new float[] {GREATEST, Float.MAX_VALUE},
281         new float[] {GREATEST, GREATEST},
282         new float[] {GREATEST, GREATEST, GREATEST});
283 
284     Comparator<float[]> comparator = Floats.lexicographicalComparator();
285     Helpers.testComparator(comparator, ordered);
286   }
287 
288   @GwtIncompatible("SerializableTester")
testLexicographicalComparatorSerializable()289   public void testLexicographicalComparatorSerializable() {
290     Comparator<float[]> comparator = Floats.lexicographicalComparator();
291     assertSame(comparator, SerializableTester.reserialize(comparator));
292   }
293 
294   @GwtIncompatible("SerializableTester")
testStringConverterSerialization()295   public void testStringConverterSerialization() {
296     SerializableTester.reserializeAndAssert(Floats.stringConverter());
297   }
298 
testToArray()299   public void testToArray() {
300     // need explicit type parameter to avoid javac warning!?
301     List<Float> none = Arrays.<Float>asList();
302     assertTrue(Arrays.equals(EMPTY, Floats.toArray(none)));
303 
304     List<Float> one = Arrays.asList((float) 1);
305     assertTrue(Arrays.equals(ARRAY1, Floats.toArray(one)));
306 
307     float[] array = {(float) 0, (float) 1, (float) 3};
308 
309     List<Float> three = Arrays.asList((float) 0, (float) 1, (float) 3);
310     assertTrue(Arrays.equals(array, Floats.toArray(three)));
311 
312     assertTrue(Arrays.equals(array, Floats.toArray(Floats.asList(array))));
313   }
314 
testToArray_threadSafe()315   public void testToArray_threadSafe() {
316     for (int delta : new int[] { +1, 0, -1 }) {
317       for (int i = 0; i < VALUES.length; i++) {
318         List<Float> list = Floats.asList(VALUES).subList(0, i);
319         Collection<Float> misleadingSize =
320             Helpers.misleadingSizeCollection(delta);
321         misleadingSize.addAll(list);
322         float[] arr = Floats.toArray(misleadingSize);
323         assertEquals(i, arr.length);
324         for (int j = 0; j < i; j++) {
325           assertEquals(VALUES[j], arr[j]);
326         }
327       }
328     }
329   }
330 
testToArray_withNull()331   public void testToArray_withNull() {
332     List<Float> list = Arrays.asList((float) 0, (float) 1, null);
333     try {
334       Floats.toArray(list);
335       fail();
336     } catch (NullPointerException expected) {
337     }
338   }
339 
testToArray_withConversion()340   public void testToArray_withConversion() {
341     float[] array = {(float) 0, (float) 1, (float) 2};
342 
343     List<Byte> bytes = Arrays.asList((byte) 0, (byte) 1, (byte) 2);
344     List<Short> shorts = Arrays.asList((short) 0, (short) 1, (short) 2);
345     List<Integer> ints = Arrays.asList(0, 1, 2);
346     List<Float> floats = Arrays.asList((float) 0, (float) 1, (float) 2);
347     List<Long> longs = Arrays.asList((long) 0, (long) 1, (long) 2);
348     List<Double> doubles = Arrays.asList((double) 0, (double) 1, (double) 2);
349 
350     assertTrue(Arrays.equals(array, Floats.toArray(bytes)));
351     assertTrue(Arrays.equals(array, Floats.toArray(shorts)));
352     assertTrue(Arrays.equals(array, Floats.toArray(ints)));
353     assertTrue(Arrays.equals(array, Floats.toArray(floats)));
354     assertTrue(Arrays.equals(array, Floats.toArray(longs)));
355     assertTrue(Arrays.equals(array, Floats.toArray(doubles)));
356   }
357 
testAsList_isAView()358   public void testAsList_isAView() {
359     float[] array = {(float) 0, (float) 1};
360     List<Float> list = Floats.asList(array);
361     list.set(0, (float) 2);
362     assertTrue(Arrays.equals(new float[] {(float) 2, (float) 1}, array));
363     array[1] = (float) 3;
364     ASSERT.that(list).has().exactly((float) 2, (float) 3).inOrder();
365   }
366 
testAsList_toArray_roundTrip()367   public void testAsList_toArray_roundTrip() {
368     float[] array = { (float) 0, (float) 1, (float) 2 };
369     List<Float> list = Floats.asList(array);
370     float[] newArray = Floats.toArray(list);
371 
372     // Make sure it returned a copy
373     list.set(0, (float) 4);
374     assertTrue(Arrays.equals(
375         new float[] { (float) 0, (float) 1, (float) 2 }, newArray));
376     newArray[1] = (float) 5;
377     assertEquals((float) 1, (float) list.get(1));
378   }
379 
380   // This test stems from a real bug found by andrewk
testAsList_subList_toArray_roundTrip()381   public void testAsList_subList_toArray_roundTrip() {
382     float[] array = { (float) 0, (float) 1, (float) 2, (float) 3 };
383     List<Float> list = Floats.asList(array);
384     assertTrue(Arrays.equals(new float[] { (float) 1, (float) 2 },
385         Floats.toArray(list.subList(1, 3))));
386     assertTrue(Arrays.equals(new float[] {},
387         Floats.toArray(list.subList(2, 2))));
388   }
389 
testAsListEmpty()390   public void testAsListEmpty() {
391     assertSame(Collections.emptyList(), Floats.asList(EMPTY));
392   }
393 
394   /**
395    * A reference implementation for {@code tryParse} that just catches the exception from
396    * {@link Float#valueOf}.
397    */
referenceTryParse(String input)398   private static Float referenceTryParse(String input) {
399     if (input.trim().length() < input.length()) {
400       return null;
401     }
402     try {
403       return Float.valueOf(input);
404     } catch (NumberFormatException e) {
405       return null;
406     }
407  }
408 
409   @GwtIncompatible("Floats.tryParse")
checkTryParse(String input)410   private static void checkTryParse(String input) {
411     assertEquals(referenceTryParse(input), Floats.tryParse(input));
412   }
413 
414   @GwtIncompatible("Floats.tryParse")
checkTryParse(float expected, String input)415   private static void checkTryParse(float expected, String input) {
416     assertEquals(Float.valueOf(expected), Floats.tryParse(input));
417   }
418 
419   @GwtIncompatible("Floats.tryParse")
testTryParseHex()420   public void testTryParseHex() {
421     for (String signChar : ImmutableList.of("", "+", "-")) {
422       for (String hexPrefix : ImmutableList.of("0x", "0X")) {
423         for (String iPart : ImmutableList.of("", "0", "1", "F", "f", "c4", "CE")) {
424           for (String fPart : ImmutableList.of("", ".", ".F", ".52", ".a")) {
425             for (String expMarker : ImmutableList.of("p", "P")) {
426               for (String exponent : ImmutableList.of("0", "-5", "+20", "52")) {
427                 for (String typePart : ImmutableList.of("", "D", "F", "d", "f")) {
428                   checkTryParse(
429                       signChar + hexPrefix + iPart + fPart + expMarker + exponent + typePart);
430                 }
431               }
432             }
433           }
434         }
435       }
436     }
437   }
438 
439   @GwtIncompatible("Floats.tryParse")
testTryParseAllCodePoints()440   public void testTryParseAllCodePoints() {
441     // Exercise non-ASCII digit test cases and the like.
442     char[] tmp = new char[2];
443     for (int i = Character.MIN_CODE_POINT; i < Character.MAX_CODE_POINT; i++) {
444       Character.toChars(i, tmp, 0);
445       checkTryParse(String.copyValueOf(tmp, 0, Character.charCount(i)));
446     }
447   }
448 
449   @GwtIncompatible("Floats.tryParse")
testTryParseOfToStringIsOriginal()450   public void testTryParseOfToStringIsOriginal() {
451     for (float f : NUMBERS) {
452       checkTryParse(f, Float.toString(f));
453     }
454   }
455 
456   @GwtIncompatible("Floats.tryParse")
testTryParseOfToHexStringIsOriginal()457   public void testTryParseOfToHexStringIsOriginal() {
458     for (float f : NUMBERS) {
459       checkTryParse(f, Float.toHexString(f));
460     }
461   }
462 
463   @GwtIncompatible("Floats.tryParse")
testTryParseNaN()464   public void testTryParseNaN() {
465     checkTryParse("NaN");
466     checkTryParse("+NaN");
467     checkTryParse("-NaN");
468   }
469 
470   @GwtIncompatible("Floats.tryParse")
testTryParseInfinity()471   public void testTryParseInfinity() {
472     checkTryParse(Float.POSITIVE_INFINITY, "Infinity");
473     checkTryParse(Float.POSITIVE_INFINITY, "+Infinity");
474     checkTryParse(Float.NEGATIVE_INFINITY, "-Infinity");
475   }
476 
477   private static final String[] BAD_TRY_PARSE_INPUTS =
478     { "", "+-", "+-0", " 5", "32 ", " 55 ", "infinity", "POSITIVE_INFINITY", "0x9A", "0x9A.bE-5",
479       ".", ".e5", "NaNd", "InfinityF" };
480 
481   @GwtIncompatible("Floats.tryParse")
testTryParseFailures()482   public void testTryParseFailures() {
483     for (String badInput : BAD_TRY_PARSE_INPUTS) {
484       assertEquals(referenceTryParse(badInput), Floats.tryParse(badInput));
485       assertNull(Floats.tryParse(badInput));
486     }
487   }
488 
489   @GwtIncompatible("NullPointerTester")
testNulls()490   public void testNulls() {
491     new NullPointerTester().testAllPublicStaticMethods(Floats.class);
492   }
493 
494   @GwtIncompatible("Float.toString returns different value in GWT.")
testStringConverter_convert()495   public void testStringConverter_convert() {
496     Converter<String, Float> converter = Floats.stringConverter();
497     assertEquals((Float) 1.0f, converter.convert("1.0"));
498     assertEquals((Float) 0.0f, converter.convert("0.0"));
499     assertEquals((Float) (-1.0f), converter.convert("-1.0"));
500     assertEquals((Float) 1.0f, converter.convert("1"));
501     assertEquals((Float) 0.0f, converter.convert("0"));
502     assertEquals((Float) (-1.0f), converter.convert("-1"));
503     assertEquals((Float) 1e6f, converter.convert("1e6"));
504     assertEquals((Float) 1e-6f, converter.convert("1e-6"));
505   }
506 
testStringConverter_convertError()507   public void testStringConverter_convertError() {
508     try {
509       Floats.stringConverter().convert("notanumber");
510       fail();
511     } catch (NumberFormatException expected) {
512     }
513   }
514 
testStringConverter_nullConversions()515   public void testStringConverter_nullConversions() {
516     assertNull(Floats.stringConverter().convert(null));
517     assertNull(Floats.stringConverter().reverse().convert(null));
518   }
519 
520   @GwtIncompatible("Float.toString returns different value in GWT.")
testStringConverter_reverse()521   public void testStringConverter_reverse() {
522     Converter<String, Float> converter = Floats.stringConverter();
523     assertEquals("1.0", converter.reverse().convert(1.0f));
524     assertEquals("0.0", converter.reverse().convert(0.0f));
525     assertEquals("-1.0", converter.reverse().convert(-1.0f));
526     assertEquals("1000000.0", converter.reverse().convert(1e6f));
527     assertEquals("1.0E-6", converter.reverse().convert(1e-6f));
528   }
529 
530   @GwtIncompatible("NullPointerTester")
testStringConverter_nullPointerTester()531   public void testStringConverter_nullPointerTester() throws Exception {
532     NullPointerTester tester = new NullPointerTester();
533     tester.testAllPublicInstanceMethods(Floats.stringConverter());
534   }
535 }
536