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 189 @GwtIncompatible testMax_noArgs()190 public void testMax_noArgs() { 191 try { 192 Floats.max(); 193 fail(); 194 } catch (IllegalArgumentException expected) { 195 } 196 } 197 testMax()198 public void testMax() { 199 assertEquals(GREATEST, Floats.max(GREATEST)); 200 assertEquals(LEAST, Floats.max(LEAST)); 201 assertEquals( 202 (float) 9, 203 Floats.max((float) 8, (float) 6, (float) 7, (float) 5, (float) 3, (float) 0, (float) 9)); 204 205 assertEquals(0f, Floats.max(-0f, 0f)); 206 assertEquals(0f, Floats.max(0f, -0f)); 207 assertEquals(GREATEST, Floats.max(NUMBERS)); 208 assertTrue(Float.isNaN(Floats.max(VALUES))); 209 } 210 211 @GwtIncompatible testMin_noArgs()212 public void testMin_noArgs() { 213 try { 214 Floats.min(); 215 fail(); 216 } catch (IllegalArgumentException expected) { 217 } 218 } 219 testMin()220 public void testMin() { 221 assertEquals(LEAST, Floats.min(LEAST)); 222 assertEquals(GREATEST, Floats.min(GREATEST)); 223 assertEquals( 224 (float) 0, 225 Floats.min((float) 8, (float) 6, (float) 7, (float) 5, (float) 3, (float) 0, (float) 9)); 226 227 assertEquals(-0f, Floats.min(-0f, 0f)); 228 assertEquals(-0f, Floats.min(0f, -0f)); 229 assertEquals(LEAST, Floats.min(NUMBERS)); 230 assertTrue(Float.isNaN(Floats.min(VALUES))); 231 } 232 testConstrainToRange()233 public void testConstrainToRange() { 234 float tolerance = 1e-10f; 235 assertEquals((float) 1, Floats.constrainToRange((float) 1, (float) 0, (float) 5), tolerance); 236 assertEquals((float) 1, Floats.constrainToRange((float) 1, (float) 1, (float) 5), tolerance); 237 assertEquals((float) 3, Floats.constrainToRange((float) 1, (float) 3, (float) 5), tolerance); 238 assertEquals((float) -1, Floats.constrainToRange((float) 0, (float) -5, (float) -1), tolerance); 239 assertEquals((float) 2, Floats.constrainToRange((float) 5, (float) 2, (float) 2), tolerance); 240 try { 241 Floats.constrainToRange((float) 1, (float) 3, (float) 2); 242 fail(); 243 } catch (IllegalArgumentException expected) { 244 } 245 } 246 testConcat()247 public void testConcat() { 248 assertTrue(Arrays.equals(EMPTY, Floats.concat())); 249 assertTrue(Arrays.equals(EMPTY, Floats.concat(EMPTY))); 250 assertTrue(Arrays.equals(EMPTY, Floats.concat(EMPTY, EMPTY, EMPTY))); 251 assertTrue(Arrays.equals(ARRAY1, Floats.concat(ARRAY1))); 252 assertNotSame(ARRAY1, Floats.concat(ARRAY1)); 253 assertTrue(Arrays.equals(ARRAY1, Floats.concat(EMPTY, ARRAY1, EMPTY))); 254 assertTrue( 255 Arrays.equals( 256 new float[] {(float) 1, (float) 1, (float) 1}, Floats.concat(ARRAY1, ARRAY1, ARRAY1))); 257 assertTrue( 258 Arrays.equals( 259 new float[] {(float) 1, (float) 2, (float) 3, (float) 4}, 260 Floats.concat(ARRAY1, ARRAY234))); 261 } 262 testEnsureCapacity()263 public void testEnsureCapacity() { 264 assertSame(EMPTY, Floats.ensureCapacity(EMPTY, 0, 1)); 265 assertSame(ARRAY1, Floats.ensureCapacity(ARRAY1, 0, 1)); 266 assertSame(ARRAY1, Floats.ensureCapacity(ARRAY1, 1, 1)); 267 assertTrue( 268 Arrays.equals( 269 new float[] {(float) 1, (float) 0, (float) 0}, Floats.ensureCapacity(ARRAY1, 2, 1))); 270 } 271 testEnsureCapacity_fail()272 public void testEnsureCapacity_fail() { 273 try { 274 Floats.ensureCapacity(ARRAY1, -1, 1); 275 fail(); 276 } catch (IllegalArgumentException expected) { 277 } 278 try { 279 // notice that this should even fail when no growth was needed 280 Floats.ensureCapacity(ARRAY1, 1, -1); 281 fail(); 282 } catch (IllegalArgumentException expected) { 283 } 284 } 285 286 @GwtIncompatible // Float.toString returns different value in GWT. testJoin()287 public void testJoin() { 288 assertEquals("", Floats.join(",", EMPTY)); 289 assertEquals("1.0", Floats.join(",", ARRAY1)); 290 assertEquals("1.0,2.0", Floats.join(",", (float) 1, (float) 2)); 291 assertEquals("1.02.03.0", Floats.join("", (float) 1, (float) 2, (float) 3)); 292 } 293 testLexicographicalComparator()294 public void testLexicographicalComparator() { 295 List<float[]> ordered = 296 Arrays.asList( 297 new float[] {}, 298 new float[] {LEAST}, 299 new float[] {LEAST, LEAST}, 300 new float[] {LEAST, (float) 1}, 301 new float[] {(float) 1}, 302 new float[] {(float) 1, LEAST}, 303 new float[] {GREATEST, Float.MAX_VALUE}, 304 new float[] {GREATEST, GREATEST}, 305 new float[] {GREATEST, GREATEST, GREATEST}); 306 307 Comparator<float[]> comparator = Floats.lexicographicalComparator(); 308 Helpers.testComparator(comparator, ordered); 309 } 310 311 @GwtIncompatible // SerializableTester testLexicographicalComparatorSerializable()312 public void testLexicographicalComparatorSerializable() { 313 Comparator<float[]> comparator = Floats.lexicographicalComparator(); 314 assertSame(comparator, SerializableTester.reserialize(comparator)); 315 } 316 testReverse()317 public void testReverse() { 318 testReverse(new float[] {}, new float[] {}); 319 testReverse(new float[] {1}, new float[] {1}); 320 testReverse(new float[] {1, 2}, new float[] {2, 1}); 321 testReverse(new float[] {3, 1, 1}, new float[] {1, 1, 3}); 322 testReverse(new float[] {-1, 1, -2, 2}, new float[] {2, -2, 1, -1}); 323 } 324 testReverse(float[] input, float[] expectedOutput)325 private static void testReverse(float[] input, float[] expectedOutput) { 326 input = Arrays.copyOf(input, input.length); 327 Floats.reverse(input); 328 assertTrue(Arrays.equals(expectedOutput, input)); 329 } 330 testReverse( float[] input, int fromIndex, int toIndex, float[] expectedOutput)331 private static void testReverse( 332 float[] input, int fromIndex, int toIndex, float[] expectedOutput) { 333 input = Arrays.copyOf(input, input.length); 334 Floats.reverse(input, fromIndex, toIndex); 335 assertTrue(Arrays.equals(expectedOutput, input)); 336 } 337 testReverseIndexed()338 public void testReverseIndexed() { 339 testReverse(new float[] {}, 0, 0, new float[] {}); 340 testReverse(new float[] {1}, 0, 1, new float[] {1}); 341 testReverse(new float[] {1, 2}, 0, 2, new float[] {2, 1}); 342 testReverse(new float[] {3, 1, 1}, 0, 2, new float[] {1, 3, 1}); 343 testReverse(new float[] {3, 1, 1}, 0, 1, new float[] {3, 1, 1}); 344 testReverse(new float[] {-1, 1, -2, 2}, 1, 3, new float[] {-1, -2, 1, 2}); 345 } 346 testSortDescending()347 public void testSortDescending() { 348 testSortDescending(new float[] {}, new float[] {}); 349 testSortDescending(new float[] {1}, new float[] {1}); 350 testSortDescending(new float[] {1, 2}, new float[] {2, 1}); 351 testSortDescending(new float[] {1, 3, 1}, new float[] {3, 1, 1}); 352 testSortDescending(new float[] {-1, 1, -2, 2}, new float[] {2, 1, -1, -2}); 353 testSortDescending( 354 new float[] {-1, 1, Float.NaN, -2, -0, 0, 2}, new float[] {Float.NaN, 2, 1, 0, -0, -1, -2}); 355 } 356 testSortDescending(float[] input, float[] expectedOutput)357 private static void testSortDescending(float[] input, float[] expectedOutput) { 358 input = Arrays.copyOf(input, input.length); 359 Floats.sortDescending(input); 360 // GWT's Arrays.equals doesn't appear to handle NaN correctly, so test each element individually 361 for (int i = 0; i < input.length; i++) { 362 assertEquals(0, Float.compare(expectedOutput[i], input[i])); 363 } 364 } 365 testSortDescending( float[] input, int fromIndex, int toIndex, float[] expectedOutput)366 private static void testSortDescending( 367 float[] input, int fromIndex, int toIndex, float[] expectedOutput) { 368 input = Arrays.copyOf(input, input.length); 369 Floats.sortDescending(input, fromIndex, toIndex); 370 // GWT's Arrays.equals doesn't appear to handle NaN correctly, so test each element individually 371 for (int i = 0; i < input.length; i++) { 372 assertEquals(0, Float.compare(expectedOutput[i], input[i])); 373 } 374 } 375 testSortDescendingIndexed()376 public void testSortDescendingIndexed() { 377 testSortDescending(new float[] {}, 0, 0, new float[] {}); 378 testSortDescending(new float[] {1}, 0, 1, new float[] {1}); 379 testSortDescending(new float[] {1, 2}, 0, 2, new float[] {2, 1}); 380 testSortDescending(new float[] {1, 3, 1}, 0, 2, new float[] {3, 1, 1}); 381 testSortDescending(new float[] {1, 3, 1}, 0, 1, new float[] {1, 3, 1}); 382 testSortDescending(new float[] {-1, -2, 1, 2}, 1, 3, new float[] {-1, 1, -2, 2}); 383 testSortDescending( 384 new float[] {-1, 1, Float.NaN, -2, 2}, 1, 4, new float[] {-1, Float.NaN, 1, -2, 2}); 385 } 386 387 @GwtIncompatible // SerializableTester testStringConverterSerialization()388 public void testStringConverterSerialization() { 389 SerializableTester.reserializeAndAssert(Floats.stringConverter()); 390 } 391 testToArray()392 public void testToArray() { 393 // need explicit type parameter to avoid javac warning!? 394 List<Float> none = Arrays.<Float>asList(); 395 assertTrue(Arrays.equals(EMPTY, Floats.toArray(none))); 396 397 List<Float> one = Arrays.asList((float) 1); 398 assertTrue(Arrays.equals(ARRAY1, Floats.toArray(one))); 399 400 float[] array = {(float) 0, (float) 1, (float) 3}; 401 402 List<Float> three = Arrays.asList((float) 0, (float) 1, (float) 3); 403 assertTrue(Arrays.equals(array, Floats.toArray(three))); 404 405 assertTrue(Arrays.equals(array, Floats.toArray(Floats.asList(array)))); 406 } 407 testToArray_threadSafe()408 public void testToArray_threadSafe() { 409 for (int delta : new int[] {+1, 0, -1}) { 410 for (int i = 0; i < VALUES.length; i++) { 411 List<Float> list = Floats.asList(VALUES).subList(0, i); 412 Collection<Float> misleadingSize = Helpers.misleadingSizeCollection(delta); 413 misleadingSize.addAll(list); 414 float[] arr = Floats.toArray(misleadingSize); 415 assertEquals(i, arr.length); 416 for (int j = 0; j < i; j++) { 417 assertEquals(VALUES[j], arr[j]); 418 } 419 } 420 } 421 } 422 testToArray_withNull()423 public void testToArray_withNull() { 424 List<Float> list = Arrays.asList((float) 0, (float) 1, null); 425 try { 426 Floats.toArray(list); 427 fail(); 428 } catch (NullPointerException expected) { 429 } 430 } 431 testToArray_withConversion()432 public void testToArray_withConversion() { 433 float[] array = {(float) 0, (float) 1, (float) 2}; 434 435 List<Byte> bytes = Arrays.asList((byte) 0, (byte) 1, (byte) 2); 436 List<Short> shorts = Arrays.asList((short) 0, (short) 1, (short) 2); 437 List<Integer> ints = Arrays.asList(0, 1, 2); 438 List<Float> floats = Arrays.asList((float) 0, (float) 1, (float) 2); 439 List<Long> longs = Arrays.asList((long) 0, (long) 1, (long) 2); 440 List<Double> doubles = Arrays.asList((double) 0, (double) 1, (double) 2); 441 442 assertTrue(Arrays.equals(array, Floats.toArray(bytes))); 443 assertTrue(Arrays.equals(array, Floats.toArray(shorts))); 444 assertTrue(Arrays.equals(array, Floats.toArray(ints))); 445 assertTrue(Arrays.equals(array, Floats.toArray(floats))); 446 assertTrue(Arrays.equals(array, Floats.toArray(longs))); 447 assertTrue(Arrays.equals(array, Floats.toArray(doubles))); 448 } 449 testAsList_isAView()450 public void testAsList_isAView() { 451 float[] array = {(float) 0, (float) 1}; 452 List<Float> list = Floats.asList(array); 453 list.set(0, (float) 2); 454 assertTrue(Arrays.equals(new float[] {(float) 2, (float) 1}, array)); 455 array[1] = (float) 3; 456 assertThat(list).containsExactly((float) 2, (float) 3).inOrder(); 457 } 458 testAsList_toArray_roundTrip()459 public void testAsList_toArray_roundTrip() { 460 float[] array = {(float) 0, (float) 1, (float) 2}; 461 List<Float> list = Floats.asList(array); 462 float[] newArray = Floats.toArray(list); 463 464 // Make sure it returned a copy 465 list.set(0, (float) 4); 466 assertTrue(Arrays.equals(new float[] {(float) 0, (float) 1, (float) 2}, newArray)); 467 newArray[1] = (float) 5; 468 assertEquals((float) 1, (float) list.get(1)); 469 } 470 471 // This test stems from a real bug found by andrewk testAsList_subList_toArray_roundTrip()472 public void testAsList_subList_toArray_roundTrip() { 473 float[] array = {(float) 0, (float) 1, (float) 2, (float) 3}; 474 List<Float> list = Floats.asList(array); 475 assertTrue( 476 Arrays.equals(new float[] {(float) 1, (float) 2}, Floats.toArray(list.subList(1, 3)))); 477 assertTrue(Arrays.equals(new float[] {}, Floats.toArray(list.subList(2, 2)))); 478 } 479 testAsListEmpty()480 public void testAsListEmpty() { 481 assertSame(Collections.emptyList(), Floats.asList(EMPTY)); 482 } 483 484 /** 485 * A reference implementation for {@code tryParse} that just catches the exception from {@link 486 * Float#valueOf}. 487 */ referenceTryParse(String input)488 private static Float referenceTryParse(String input) { 489 if (input.trim().length() < input.length()) { 490 return null; 491 } 492 try { 493 return Float.valueOf(input); 494 } catch (NumberFormatException e) { 495 return null; 496 } 497 } 498 499 @GwtIncompatible // Floats.tryParse checkTryParse(String input)500 private static void checkTryParse(String input) { 501 assertEquals(referenceTryParse(input), Floats.tryParse(input)); 502 } 503 504 @GwtIncompatible // Floats.tryParse checkTryParse(float expected, String input)505 private static void checkTryParse(float expected, String input) { 506 assertEquals(Float.valueOf(expected), Floats.tryParse(input)); 507 } 508 509 @GwtIncompatible // Floats.tryParse testTryParseHex()510 public void testTryParseHex() { 511 for (String signChar : ImmutableList.of("", "+", "-")) { 512 for (String hexPrefix : ImmutableList.of("0x", "0X")) { 513 for (String iPart : ImmutableList.of("", "0", "1", "F", "f", "c4", "CE")) { 514 for (String fPart : ImmutableList.of("", ".", ".F", ".52", ".a")) { 515 for (String expMarker : ImmutableList.of("p", "P")) { 516 for (String exponent : ImmutableList.of("0", "-5", "+20", "52")) { 517 for (String typePart : ImmutableList.of("", "D", "F", "d", "f")) { 518 checkTryParse( 519 signChar + hexPrefix + iPart + fPart + expMarker + exponent + typePart); 520 } 521 } 522 } 523 } 524 } 525 } 526 } 527 } 528 529 @AndroidIncompatible // slow 530 @GwtIncompatible // Floats.tryParse testTryParseAllCodePoints()531 public void testTryParseAllCodePoints() { 532 // Exercise non-ASCII digit test cases and the like. 533 char[] tmp = new char[2]; 534 for (int i = Character.MIN_CODE_POINT; i < Character.MAX_CODE_POINT; i++) { 535 Character.toChars(i, tmp, 0); 536 checkTryParse(String.copyValueOf(tmp, 0, Character.charCount(i))); 537 } 538 } 539 540 @GwtIncompatible // Floats.tryParse testTryParseOfToStringIsOriginal()541 public void testTryParseOfToStringIsOriginal() { 542 for (float f : NUMBERS) { 543 checkTryParse(f, Float.toString(f)); 544 } 545 } 546 547 @GwtIncompatible // Floats.tryParse testTryParseOfToHexStringIsOriginal()548 public void testTryParseOfToHexStringIsOriginal() { 549 for (float f : NUMBERS) { 550 checkTryParse(f, Float.toHexString(f)); 551 } 552 } 553 554 @GwtIncompatible // Floats.tryParse testTryParseNaN()555 public void testTryParseNaN() { 556 checkTryParse("NaN"); 557 checkTryParse("+NaN"); 558 checkTryParse("-NaN"); 559 } 560 561 @GwtIncompatible // Floats.tryParse testTryParseInfinity()562 public void testTryParseInfinity() { 563 checkTryParse(Float.POSITIVE_INFINITY, "Infinity"); 564 checkTryParse(Float.POSITIVE_INFINITY, "+Infinity"); 565 checkTryParse(Float.NEGATIVE_INFINITY, "-Infinity"); 566 } 567 568 private static final String[] BAD_TRY_PARSE_INPUTS = { 569 "", 570 "+-", 571 "+-0", 572 " 5", 573 "32 ", 574 " 55 ", 575 "infinity", 576 "POSITIVE_INFINITY", 577 "0x9A", 578 "0x9A.bE-5", 579 ".", 580 ".e5", 581 "NaNd", 582 "InfinityF" 583 }; 584 585 @GwtIncompatible // Floats.tryParse testTryParseFailures()586 public void testTryParseFailures() { 587 for (String badInput : BAD_TRY_PARSE_INPUTS) { 588 assertEquals(referenceTryParse(badInput), Floats.tryParse(badInput)); 589 assertNull(Floats.tryParse(badInput)); 590 } 591 } 592 593 @GwtIncompatible // NullPointerTester testNulls()594 public void testNulls() { 595 new NullPointerTester().testAllPublicStaticMethods(Floats.class); 596 } 597 598 @GwtIncompatible // Float.toString returns different value in GWT. testStringConverter_convert()599 public void testStringConverter_convert() { 600 Converter<String, Float> converter = Floats.stringConverter(); 601 assertEquals((Float) 1.0f, converter.convert("1.0")); 602 assertEquals((Float) 0.0f, converter.convert("0.0")); 603 assertEquals((Float) (-1.0f), converter.convert("-1.0")); 604 assertEquals((Float) 1.0f, converter.convert("1")); 605 assertEquals((Float) 0.0f, converter.convert("0")); 606 assertEquals((Float) (-1.0f), converter.convert("-1")); 607 assertEquals((Float) 1e6f, converter.convert("1e6")); 608 assertEquals((Float) 1e-6f, converter.convert("1e-6")); 609 } 610 testStringConverter_convertError()611 public void testStringConverter_convertError() { 612 try { 613 Floats.stringConverter().convert("notanumber"); 614 fail(); 615 } catch (NumberFormatException expected) { 616 } 617 } 618 testStringConverter_nullConversions()619 public void testStringConverter_nullConversions() { 620 assertNull(Floats.stringConverter().convert(null)); 621 assertNull(Floats.stringConverter().reverse().convert(null)); 622 } 623 624 @GwtIncompatible // Float.toString returns different value in GWT. testStringConverter_reverse()625 public void testStringConverter_reverse() { 626 Converter<String, Float> converter = Floats.stringConverter(); 627 assertEquals("1.0", converter.reverse().convert(1.0f)); 628 assertEquals("0.0", converter.reverse().convert(0.0f)); 629 assertEquals("-1.0", converter.reverse().convert(-1.0f)); 630 assertEquals("1000000.0", converter.reverse().convert(1e6f)); 631 assertEquals("1.0E-6", converter.reverse().convert(1e-6f)); 632 } 633 634 @GwtIncompatible // NullPointerTester testStringConverter_nullPointerTester()635 public void testStringConverter_nullPointerTester() throws Exception { 636 NullPointerTester tester = new NullPointerTester(); 637 tester.testAllPublicInstanceMethods(Floats.stringConverter()); 638 } 639 640 @GwtIncompatible testTryParse_withNullNoGwt()641 public void testTryParse_withNullNoGwt() { 642 assertNull(Floats.tryParse("null")); 643 try { 644 Floats.tryParse(null); 645 fail("Expected NPE"); 646 } catch (NullPointerException expected) { 647 } 648 } 649 } 650