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