• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 com.google.common.truth.Truth.assertThat;
20 import static java.lang.Float.NaN;
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 import java.util.Arrays;
30 import java.util.Collection;
31 import java.util.Collections;
32 import java.util.Comparator;
33 import java.util.List;
34 import junit.framework.TestCase;
35 
36 /**
37  * Unit test for {@link Floats}.
38  *
39  * @author Kevin Bourrillion
40  */
41 @GwtCompatible(emulated = true)
42 @SuppressWarnings("cast") // redundant casts are intentional and harmless
43 public class FloatsTest extends TestCase {
44   private static final float[] EMPTY = {};
45   private static final float[] ARRAY1 = {(float) 1};
46   private static final float[] ARRAY234 = {(float) 2, (float) 3, (float) 4};
47 
48   private static final float LEAST = Float.NEGATIVE_INFINITY;
49   private static final float GREATEST = Float.POSITIVE_INFINITY;
50 
51   private static final float[] NUMBERS =
52       new float[] {
53         LEAST,
54         -Float.MAX_VALUE,
55         -1f,
56         -0f,
57         0f,
58         1f,
59         Float.MAX_VALUE,
60         GREATEST,
61         Float.MIN_NORMAL,
62         -Float.MIN_NORMAL,
63         Float.MIN_VALUE,
64         -Float.MIN_VALUE,
65         Integer.MIN_VALUE,
66         Integer.MAX_VALUE,
67         Long.MIN_VALUE,
68         Long.MAX_VALUE
69       };
70 
71   private static final float[] VALUES = Floats.concat(NUMBERS, new float[] {NaN});
72 
testHashCode()73   public void testHashCode() {
74     for (float value : VALUES) {
75       assertEquals(((Float) value).hashCode(), Floats.hashCode(value));
76     }
77   }
78 
testIsFinite()79   public void testIsFinite() {
80     for (float value : NUMBERS) {
81       assertEquals(!(Float.isInfinite(value) || Float.isNaN(value)), Floats.isFinite(value));
82     }
83   }
84 
testCompare()85   public void testCompare() {
86     for (float x : VALUES) {
87       for (float y : VALUES) {
88         // note: spec requires only that the sign is the same
89         assertEquals(x + ", " + y, Float.valueOf(x).compareTo(y), Floats.compare(x, y));
90       }
91     }
92   }
93 
testContains()94   public void testContains() {
95     assertFalse(Floats.contains(EMPTY, (float) 1));
96     assertFalse(Floats.contains(ARRAY1, (float) 2));
97     assertFalse(Floats.contains(ARRAY234, (float) 1));
98     assertTrue(Floats.contains(new float[] {(float) -1}, (float) -1));
99     assertTrue(Floats.contains(ARRAY234, (float) 2));
100     assertTrue(Floats.contains(ARRAY234, (float) 3));
101     assertTrue(Floats.contains(ARRAY234, (float) 4));
102 
103     for (float value : NUMBERS) {
104       assertTrue("" + value, Floats.contains(new float[] {5f, value}, value));
105     }
106     assertFalse(Floats.contains(new float[] {5f, NaN}, NaN));
107   }
108 
testIndexOf()109   public void testIndexOf() {
110     assertEquals(-1, Floats.indexOf(EMPTY, (float) 1));
111     assertEquals(-1, Floats.indexOf(ARRAY1, (float) 2));
112     assertEquals(-1, Floats.indexOf(ARRAY234, (float) 1));
113     assertEquals(0, Floats.indexOf(new float[] {(float) -1}, (float) -1));
114     assertEquals(0, Floats.indexOf(ARRAY234, (float) 2));
115     assertEquals(1, Floats.indexOf(ARRAY234, (float) 3));
116     assertEquals(2, Floats.indexOf(ARRAY234, (float) 4));
117     assertEquals(
118         1, Floats.indexOf(new float[] {(float) 2, (float) 3, (float) 2, (float) 3}, (float) 3));
119 
120     for (float value : NUMBERS) {
121       assertEquals("" + value, 1, Floats.indexOf(new float[] {5f, value}, value));
122     }
123     assertEquals(-1, Floats.indexOf(new float[] {5f, NaN}, NaN));
124   }
125 
testIndexOf_arrayTarget()126   public void testIndexOf_arrayTarget() {
127     assertEquals(0, Floats.indexOf(EMPTY, EMPTY));
128     assertEquals(0, Floats.indexOf(ARRAY234, EMPTY));
129     assertEquals(-1, Floats.indexOf(EMPTY, ARRAY234));
130     assertEquals(-1, Floats.indexOf(ARRAY234, ARRAY1));
131     assertEquals(-1, Floats.indexOf(ARRAY1, ARRAY234));
132     assertEquals(0, Floats.indexOf(ARRAY1, ARRAY1));
133     assertEquals(0, Floats.indexOf(ARRAY234, ARRAY234));
134     assertEquals(0, Floats.indexOf(ARRAY234, new float[] {(float) 2, (float) 3}));
135     assertEquals(1, Floats.indexOf(ARRAY234, new float[] {(float) 3, (float) 4}));
136     assertEquals(1, Floats.indexOf(ARRAY234, new float[] {(float) 3}));
137     assertEquals(2, Floats.indexOf(ARRAY234, new float[] {(float) 4}));
138     assertEquals(
139         1,
140         Floats.indexOf(
141             new float[] {(float) 2, (float) 3, (float) 3, (float) 3, (float) 3},
142             new float[] {(float) 3}));
143     assertEquals(
144         2,
145         Floats.indexOf(
146             new float[] {
147               (float) 2, (float) 3, (float) 2, (float) 3, (float) 4, (float) 2, (float) 3
148             },
149             new float[] {(float) 2, (float) 3, (float) 4}));
150     assertEquals(
151         1,
152         Floats.indexOf(
153             new float[] {
154               (float) 2, (float) 2, (float) 3, (float) 4, (float) 2, (float) 3, (float) 4
155             },
156             new float[] {(float) 2, (float) 3, (float) 4}));
157     assertEquals(
158         -1,
159         Floats.indexOf(
160             new float[] {(float) 4, (float) 3, (float) 2},
161             new float[] {(float) 2, (float) 3, (float) 4}));
162 
163     for (float value : NUMBERS) {
164       assertEquals(
165           "" + value,
166           1,
167           Floats.indexOf(new float[] {5f, value, value, 5f}, new float[] {value, value}));
168     }
169     assertEquals(-1, Floats.indexOf(new float[] {5f, NaN, NaN, 5f}, new float[] {NaN, NaN}));
170   }
171 
testLastIndexOf()172   public void testLastIndexOf() {
173     assertEquals(-1, Floats.lastIndexOf(EMPTY, (float) 1));
174     assertEquals(-1, Floats.lastIndexOf(ARRAY1, (float) 2));
175     assertEquals(-1, Floats.lastIndexOf(ARRAY234, (float) 1));
176     assertEquals(0, Floats.lastIndexOf(new float[] {(float) -1}, (float) -1));
177     assertEquals(0, Floats.lastIndexOf(ARRAY234, (float) 2));
178     assertEquals(1, Floats.lastIndexOf(ARRAY234, (float) 3));
179     assertEquals(2, Floats.lastIndexOf(ARRAY234, (float) 4));
180     assertEquals(
181         3, Floats.lastIndexOf(new float[] {(float) 2, (float) 3, (float) 2, (float) 3}, (float) 3));
182 
183     for (float value : NUMBERS) {
184       assertEquals("" + value, 0, Floats.lastIndexOf(new float[] {value, 5f}, value));
185     }
186     assertEquals(-1, Floats.lastIndexOf(new float[] {NaN, 5f}, NaN));
187   }
188 
testMax_noArgs()189   public void testMax_noArgs() {
190     try {
191       Floats.max();
192       fail();
193     } catch (IllegalArgumentException expected) {
194     }
195   }
196 
testMax()197   public void testMax() {
198     assertEquals(GREATEST, Floats.max(GREATEST));
199     assertEquals(LEAST, Floats.max(LEAST));
200     assertEquals(
201         (float) 9,
202         Floats.max((float) 8, (float) 6, (float) 7, (float) 5, (float) 3, (float) 0, (float) 9));
203 
204     assertEquals(0f, Floats.max(-0f, 0f));
205     assertEquals(0f, Floats.max(0f, -0f));
206     assertEquals(GREATEST, Floats.max(NUMBERS));
207     assertTrue(Float.isNaN(Floats.max(VALUES)));
208   }
209 
testMin_noArgs()210   public void testMin_noArgs() {
211     try {
212       Floats.min();
213       fail();
214     } catch (IllegalArgumentException expected) {
215     }
216   }
217 
testMin()218   public void testMin() {
219     assertEquals(LEAST, Floats.min(LEAST));
220     assertEquals(GREATEST, Floats.min(GREATEST));
221     assertEquals(
222         (float) 0,
223         Floats.min((float) 8, (float) 6, (float) 7, (float) 5, (float) 3, (float) 0, (float) 9));
224 
225     assertEquals(-0f, Floats.min(-0f, 0f));
226     assertEquals(-0f, Floats.min(0f, -0f));
227     assertEquals(LEAST, Floats.min(NUMBERS));
228     assertTrue(Float.isNaN(Floats.min(VALUES)));
229   }
230 
testConstrainToRange()231   public void testConstrainToRange() {
232     float tolerance = 1e-10f;
233     assertEquals((float) 1, Floats.constrainToRange((float) 1, (float) 0, (float) 5), tolerance);
234     assertEquals((float) 1, Floats.constrainToRange((float) 1, (float) 1, (float) 5), tolerance);
235     assertEquals((float) 3, Floats.constrainToRange((float) 1, (float) 3, (float) 5), tolerance);
236     assertEquals((float) -1, Floats.constrainToRange((float) 0, (float) -5, (float) -1), tolerance);
237     assertEquals((float) 2, Floats.constrainToRange((float) 5, (float) 2, (float) 2), tolerance);
238     try {
239       Floats.constrainToRange((float) 1, (float) 3, (float) 2);
240       fail();
241     } catch (IllegalArgumentException expected) {
242     }
243   }
244 
testConcat()245   public void testConcat() {
246     assertTrue(Arrays.equals(EMPTY, Floats.concat()));
247     assertTrue(Arrays.equals(EMPTY, Floats.concat(EMPTY)));
248     assertTrue(Arrays.equals(EMPTY, Floats.concat(EMPTY, EMPTY, EMPTY)));
249     assertTrue(Arrays.equals(ARRAY1, Floats.concat(ARRAY1)));
250     assertNotSame(ARRAY1, Floats.concat(ARRAY1));
251     assertTrue(Arrays.equals(ARRAY1, Floats.concat(EMPTY, ARRAY1, EMPTY)));
252     assertTrue(
253         Arrays.equals(
254             new float[] {(float) 1, (float) 1, (float) 1}, Floats.concat(ARRAY1, ARRAY1, ARRAY1)));
255     assertTrue(
256         Arrays.equals(
257             new float[] {(float) 1, (float) 2, (float) 3, (float) 4},
258             Floats.concat(ARRAY1, ARRAY234)));
259   }
260 
testEnsureCapacity()261   public void testEnsureCapacity() {
262     assertSame(EMPTY, Floats.ensureCapacity(EMPTY, 0, 1));
263     assertSame(ARRAY1, Floats.ensureCapacity(ARRAY1, 0, 1));
264     assertSame(ARRAY1, Floats.ensureCapacity(ARRAY1, 1, 1));
265     assertTrue(
266         Arrays.equals(
267             new float[] {(float) 1, (float) 0, (float) 0}, Floats.ensureCapacity(ARRAY1, 2, 1)));
268   }
269 
testEnsureCapacity_fail()270   public void testEnsureCapacity_fail() {
271     try {
272       Floats.ensureCapacity(ARRAY1, -1, 1);
273       fail();
274     } catch (IllegalArgumentException expected) {
275     }
276     try {
277       // notice that this should even fail when no growth was needed
278       Floats.ensureCapacity(ARRAY1, 1, -1);
279       fail();
280     } catch (IllegalArgumentException expected) {
281     }
282   }
283 
284   @GwtIncompatible // Float.toString returns different value in GWT.
testJoin()285   public void testJoin() {
286     assertEquals("", Floats.join(",", EMPTY));
287     assertEquals("1.0", Floats.join(",", ARRAY1));
288     assertEquals("1.0,2.0", Floats.join(",", (float) 1, (float) 2));
289     assertEquals("1.02.03.0", Floats.join("", (float) 1, (float) 2, (float) 3));
290   }
291 
testLexicographicalComparator()292   public void testLexicographicalComparator() {
293     List<float[]> ordered =
294         Arrays.asList(
295             new float[] {},
296             new float[] {LEAST},
297             new float[] {LEAST, LEAST},
298             new float[] {LEAST, (float) 1},
299             new float[] {(float) 1},
300             new float[] {(float) 1, LEAST},
301             new float[] {GREATEST, Float.MAX_VALUE},
302             new float[] {GREATEST, GREATEST},
303             new float[] {GREATEST, GREATEST, GREATEST});
304 
305     Comparator<float[]> comparator = Floats.lexicographicalComparator();
306     Helpers.testComparator(comparator, ordered);
307   }
308 
309   @GwtIncompatible // SerializableTester
testLexicographicalComparatorSerializable()310   public void testLexicographicalComparatorSerializable() {
311     Comparator<float[]> comparator = Floats.lexicographicalComparator();
312     assertSame(comparator, SerializableTester.reserialize(comparator));
313   }
314 
testReverse()315   public void testReverse() {
316     testReverse(new float[] {}, new float[] {});
317     testReverse(new float[] {1}, new float[] {1});
318     testReverse(new float[] {1, 2}, new float[] {2, 1});
319     testReverse(new float[] {3, 1, 1}, new float[] {1, 1, 3});
320     testReverse(new float[] {-1, 1, -2, 2}, new float[] {2, -2, 1, -1});
321   }
322 
testReverse(float[] input, float[] expectedOutput)323   private static void testReverse(float[] input, float[] expectedOutput) {
324     input = Arrays.copyOf(input, input.length);
325     Floats.reverse(input);
326     assertTrue(Arrays.equals(expectedOutput, input));
327   }
328 
testReverse( float[] input, int fromIndex, int toIndex, float[] expectedOutput)329   private static void testReverse(
330       float[] input, int fromIndex, int toIndex, float[] expectedOutput) {
331     input = Arrays.copyOf(input, input.length);
332     Floats.reverse(input, fromIndex, toIndex);
333     assertTrue(Arrays.equals(expectedOutput, input));
334   }
335 
testReverseIndexed()336   public void testReverseIndexed() {
337     testReverse(new float[] {}, 0, 0, new float[] {});
338     testReverse(new float[] {1}, 0, 1, new float[] {1});
339     testReverse(new float[] {1, 2}, 0, 2, new float[] {2, 1});
340     testReverse(new float[] {3, 1, 1}, 0, 2, new float[] {1, 3, 1});
341     testReverse(new float[] {3, 1, 1}, 0, 1, new float[] {3, 1, 1});
342     testReverse(new float[] {-1, 1, -2, 2}, 1, 3, new float[] {-1, -2, 1, 2});
343   }
344 
testSortDescending()345   public void testSortDescending() {
346     testSortDescending(new float[] {}, new float[] {});
347     testSortDescending(new float[] {1}, new float[] {1});
348     testSortDescending(new float[] {1, 2}, new float[] {2, 1});
349     testSortDescending(new float[] {1, 3, 1}, new float[] {3, 1, 1});
350     testSortDescending(new float[] {-1, 1, -2, 2}, new float[] {2, 1, -1, -2});
351     testSortDescending(
352         new float[] {-1, 1, Float.NaN, -2, -0, 0, 2}, new float[] {Float.NaN, 2, 1, 0, -0, -1, -2});
353   }
354 
testSortDescending(float[] input, float[] expectedOutput)355   private static void testSortDescending(float[] input, float[] expectedOutput) {
356     input = Arrays.copyOf(input, input.length);
357     Floats.sortDescending(input);
358     // GWT's Arrays.equals doesn't appear to handle NaN correctly, so test each element individually
359     for (int i = 0; i < input.length; i++) {
360       assertEquals(0, Float.compare(expectedOutput[i], input[i]));
361     }
362   }
363 
testSortDescending( float[] input, int fromIndex, int toIndex, float[] expectedOutput)364   private static void testSortDescending(
365       float[] input, int fromIndex, int toIndex, float[] expectedOutput) {
366     input = Arrays.copyOf(input, input.length);
367     Floats.sortDescending(input, fromIndex, toIndex);
368     // GWT's Arrays.equals doesn't appear to handle NaN correctly, so test each element individually
369     for (int i = 0; i < input.length; i++) {
370       assertEquals(0, Float.compare(expectedOutput[i], input[i]));
371     }
372   }
373 
testSortDescendingIndexed()374   public void testSortDescendingIndexed() {
375     testSortDescending(new float[] {}, 0, 0, new float[] {});
376     testSortDescending(new float[] {1}, 0, 1, new float[] {1});
377     testSortDescending(new float[] {1, 2}, 0, 2, new float[] {2, 1});
378     testSortDescending(new float[] {1, 3, 1}, 0, 2, new float[] {3, 1, 1});
379     testSortDescending(new float[] {1, 3, 1}, 0, 1, new float[] {1, 3, 1});
380     testSortDescending(new float[] {-1, -2, 1, 2}, 1, 3, new float[] {-1, 1, -2, 2});
381     testSortDescending(
382         new float[] {-1, 1, Float.NaN, -2, 2}, 1, 4, new float[] {-1, Float.NaN, 1, -2, 2});
383   }
384 
385   @GwtIncompatible // SerializableTester
testStringConverterSerialization()386   public void testStringConverterSerialization() {
387     SerializableTester.reserializeAndAssert(Floats.stringConverter());
388   }
389 
testToArray()390   public void testToArray() {
391     // need explicit type parameter to avoid javac warning!?
392     List<Float> none = Arrays.<Float>asList();
393     assertTrue(Arrays.equals(EMPTY, Floats.toArray(none)));
394 
395     List<Float> one = Arrays.asList((float) 1);
396     assertTrue(Arrays.equals(ARRAY1, Floats.toArray(one)));
397 
398     float[] array = {(float) 0, (float) 1, (float) 3};
399 
400     List<Float> three = Arrays.asList((float) 0, (float) 1, (float) 3);
401     assertTrue(Arrays.equals(array, Floats.toArray(three)));
402 
403     assertTrue(Arrays.equals(array, Floats.toArray(Floats.asList(array))));
404   }
405 
testToArray_threadSafe()406   public void testToArray_threadSafe() {
407     for (int delta : new int[] {+1, 0, -1}) {
408       for (int i = 0; i < VALUES.length; i++) {
409         List<Float> list = Floats.asList(VALUES).subList(0, i);
410         Collection<Float> misleadingSize = Helpers.misleadingSizeCollection(delta);
411         misleadingSize.addAll(list);
412         float[] arr = Floats.toArray(misleadingSize);
413         assertEquals(i, arr.length);
414         for (int j = 0; j < i; j++) {
415           assertEquals(VALUES[j], arr[j]);
416         }
417       }
418     }
419   }
420 
testToArray_withNull()421   public void testToArray_withNull() {
422     List<Float> list = Arrays.asList((float) 0, (float) 1, null);
423     try {
424       Floats.toArray(list);
425       fail();
426     } catch (NullPointerException expected) {
427     }
428   }
429 
testToArray_withConversion()430   public void testToArray_withConversion() {
431     float[] array = {(float) 0, (float) 1, (float) 2};
432 
433     List<Byte> bytes = Arrays.asList((byte) 0, (byte) 1, (byte) 2);
434     List<Short> shorts = Arrays.asList((short) 0, (short) 1, (short) 2);
435     List<Integer> ints = Arrays.asList(0, 1, 2);
436     List<Float> floats = Arrays.asList((float) 0, (float) 1, (float) 2);
437     List<Long> longs = Arrays.asList((long) 0, (long) 1, (long) 2);
438     List<Double> doubles = Arrays.asList((double) 0, (double) 1, (double) 2);
439 
440     assertTrue(Arrays.equals(array, Floats.toArray(bytes)));
441     assertTrue(Arrays.equals(array, Floats.toArray(shorts)));
442     assertTrue(Arrays.equals(array, Floats.toArray(ints)));
443     assertTrue(Arrays.equals(array, Floats.toArray(floats)));
444     assertTrue(Arrays.equals(array, Floats.toArray(longs)));
445     assertTrue(Arrays.equals(array, Floats.toArray(doubles)));
446   }
447 
testAsList_isAView()448   public void testAsList_isAView() {
449     float[] array = {(float) 0, (float) 1};
450     List<Float> list = Floats.asList(array);
451     list.set(0, (float) 2);
452     assertTrue(Arrays.equals(new float[] {(float) 2, (float) 1}, array));
453     array[1] = (float) 3;
454     assertThat(list).containsExactly((float) 2, (float) 3).inOrder();
455   }
456 
testAsList_toArray_roundTrip()457   public void testAsList_toArray_roundTrip() {
458     float[] array = {(float) 0, (float) 1, (float) 2};
459     List<Float> list = Floats.asList(array);
460     float[] newArray = Floats.toArray(list);
461 
462     // Make sure it returned a copy
463     list.set(0, (float) 4);
464     assertTrue(Arrays.equals(new float[] {(float) 0, (float) 1, (float) 2}, newArray));
465     newArray[1] = (float) 5;
466     assertEquals((float) 1, (float) list.get(1));
467   }
468 
469   // This test stems from a real bug found by andrewk
testAsList_subList_toArray_roundTrip()470   public void testAsList_subList_toArray_roundTrip() {
471     float[] array = {(float) 0, (float) 1, (float) 2, (float) 3};
472     List<Float> list = Floats.asList(array);
473     assertTrue(
474         Arrays.equals(new float[] {(float) 1, (float) 2}, Floats.toArray(list.subList(1, 3))));
475     assertTrue(Arrays.equals(new float[] {}, Floats.toArray(list.subList(2, 2))));
476   }
477 
testAsListEmpty()478   public void testAsListEmpty() {
479     assertSame(Collections.emptyList(), Floats.asList(EMPTY));
480   }
481 
482   /**
483    * A reference implementation for {@code tryParse} that just catches the exception from {@link
484    * Float#valueOf}.
485    */
referenceTryParse(String input)486   private static Float referenceTryParse(String input) {
487     if (input.trim().length() < input.length()) {
488       return null;
489     }
490     try {
491       return Float.valueOf(input);
492     } catch (NumberFormatException e) {
493       return null;
494     }
495   }
496 
497   @GwtIncompatible // Floats.tryParse
checkTryParse(String input)498   private static void checkTryParse(String input) {
499     assertEquals(referenceTryParse(input), Floats.tryParse(input));
500   }
501 
502   @GwtIncompatible // Floats.tryParse
checkTryParse(float expected, String input)503   private static void checkTryParse(float expected, String input) {
504     assertEquals(Float.valueOf(expected), Floats.tryParse(input));
505   }
506 
507   @GwtIncompatible // Floats.tryParse
testTryParseHex()508   public void testTryParseHex() {
509     for (String signChar : ImmutableList.of("", "+", "-")) {
510       for (String hexPrefix : ImmutableList.of("0x", "0X")) {
511         for (String iPart : ImmutableList.of("", "0", "1", "F", "f", "c4", "CE")) {
512           for (String fPart : ImmutableList.of("", ".", ".F", ".52", ".a")) {
513             for (String expMarker : ImmutableList.of("p", "P")) {
514               for (String exponent : ImmutableList.of("0", "-5", "+20", "52")) {
515                 for (String typePart : ImmutableList.of("", "D", "F", "d", "f")) {
516                   checkTryParse(
517                       signChar + hexPrefix + iPart + fPart + expMarker + exponent + typePart);
518                 }
519               }
520             }
521           }
522         }
523       }
524     }
525   }
526 
527   @AndroidIncompatible // slow
528   @GwtIncompatible // Floats.tryParse
testTryParseAllCodePoints()529   public void testTryParseAllCodePoints() {
530     // Exercise non-ASCII digit test cases and the like.
531     char[] tmp = new char[2];
532     for (int i = Character.MIN_CODE_POINT; i < Character.MAX_CODE_POINT; i++) {
533       Character.toChars(i, tmp, 0);
534       checkTryParse(String.copyValueOf(tmp, 0, Character.charCount(i)));
535     }
536   }
537 
538   @GwtIncompatible // Floats.tryParse
testTryParseOfToStringIsOriginal()539   public void testTryParseOfToStringIsOriginal() {
540     for (float f : NUMBERS) {
541       checkTryParse(f, Float.toString(f));
542     }
543   }
544 
545   @GwtIncompatible // Floats.tryParse
testTryParseOfToHexStringIsOriginal()546   public void testTryParseOfToHexStringIsOriginal() {
547     for (float f : NUMBERS) {
548       checkTryParse(f, Float.toHexString(f));
549     }
550   }
551 
552   @GwtIncompatible // Floats.tryParse
testTryParseNaN()553   public void testTryParseNaN() {
554     checkTryParse("NaN");
555     checkTryParse("+NaN");
556     checkTryParse("-NaN");
557   }
558 
559   @GwtIncompatible // Floats.tryParse
testTryParseInfinity()560   public void testTryParseInfinity() {
561     checkTryParse(Float.POSITIVE_INFINITY, "Infinity");
562     checkTryParse(Float.POSITIVE_INFINITY, "+Infinity");
563     checkTryParse(Float.NEGATIVE_INFINITY, "-Infinity");
564   }
565 
566   private static final String[] BAD_TRY_PARSE_INPUTS = {
567     "",
568     "+-",
569     "+-0",
570     " 5",
571     "32 ",
572     " 55 ",
573     "infinity",
574     "POSITIVE_INFINITY",
575     "0x9A",
576     "0x9A.bE-5",
577     ".",
578     ".e5",
579     "NaNd",
580     "InfinityF"
581   };
582 
583   @GwtIncompatible // Floats.tryParse
testTryParseFailures()584   public void testTryParseFailures() {
585     for (String badInput : BAD_TRY_PARSE_INPUTS) {
586       assertEquals(referenceTryParse(badInput), Floats.tryParse(badInput));
587       assertNull(Floats.tryParse(badInput));
588     }
589   }
590 
591   @GwtIncompatible // NullPointerTester
testNulls()592   public void testNulls() {
593     new NullPointerTester().testAllPublicStaticMethods(Floats.class);
594   }
595 
596   @GwtIncompatible // Float.toString returns different value in GWT.
testStringConverter_convert()597   public void testStringConverter_convert() {
598     Converter<String, Float> converter = Floats.stringConverter();
599     assertEquals((Float) 1.0f, converter.convert("1.0"));
600     assertEquals((Float) 0.0f, converter.convert("0.0"));
601     assertEquals((Float) (-1.0f), converter.convert("-1.0"));
602     assertEquals((Float) 1.0f, converter.convert("1"));
603     assertEquals((Float) 0.0f, converter.convert("0"));
604     assertEquals((Float) (-1.0f), converter.convert("-1"));
605     assertEquals((Float) 1e6f, converter.convert("1e6"));
606     assertEquals((Float) 1e-6f, converter.convert("1e-6"));
607   }
608 
testStringConverter_convertError()609   public void testStringConverter_convertError() {
610     try {
611       Floats.stringConverter().convert("notanumber");
612       fail();
613     } catch (NumberFormatException expected) {
614     }
615   }
616 
testStringConverter_nullConversions()617   public void testStringConverter_nullConversions() {
618     assertNull(Floats.stringConverter().convert(null));
619     assertNull(Floats.stringConverter().reverse().convert(null));
620   }
621 
622   @GwtIncompatible // Float.toString returns different value in GWT.
testStringConverter_reverse()623   public void testStringConverter_reverse() {
624     Converter<String, Float> converter = Floats.stringConverter();
625     assertEquals("1.0", converter.reverse().convert(1.0f));
626     assertEquals("0.0", converter.reverse().convert(0.0f));
627     assertEquals("-1.0", converter.reverse().convert(-1.0f));
628     assertEquals("1000000.0", converter.reverse().convert(1e6f));
629     assertEquals("1.0E-6", converter.reverse().convert(1e-6f));
630   }
631 
632   @GwtIncompatible // NullPointerTester
testStringConverter_nullPointerTester()633   public void testStringConverter_nullPointerTester() throws Exception {
634     NullPointerTester tester = new NullPointerTester();
635     tester.testAllPublicInstanceMethods(Floats.stringConverter());
636   }
637 
638   @GwtIncompatible
testTryParse_withNullNoGwt()639   public void testTryParse_withNullNoGwt() {
640     assertNull(Floats.tryParse("null"));
641     try {
642       Floats.tryParse(null);
643       fail("Expected NPE");
644     } catch (NullPointerException expected) {
645     }
646   }
647 }
648