1 /* 2 * Copyright (C) 2011 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.math; 18 19 import static com.google.common.math.MathTesting.ALL_BIGINTEGER_CANDIDATES; 20 import static com.google.common.math.MathTesting.FINITE_DOUBLE_CANDIDATES; 21 import static com.google.common.math.MathTesting.POSITIVE_FINITE_DOUBLE_CANDIDATES; 22 import static org.junit.Assert.assertThrows; 23 24 import java.lang.reflect.Method; 25 import java.math.BigInteger; 26 import junit.framework.TestCase; 27 28 /** 29 * Tests for {@link DoubleUtils}. 30 * 31 * @author Louis Wasserman 32 */ 33 public class DoubleUtilsTest extends TestCase { 34 @AndroidIncompatible // no FpUtils and no Math.nextDown in old versions testNextDown()35 public void testNextDown() throws Exception { 36 Method jdkNextDown = getJdkNextDown(); 37 for (double d : FINITE_DOUBLE_CANDIDATES) { 38 assertEquals(jdkNextDown.invoke(null, d), DoubleUtils.nextDown(d)); 39 } 40 } 41 getJdkNextDown()42 private static Method getJdkNextDown() throws Exception { 43 try { 44 return Math.class.getMethod("nextDown", double.class); 45 } catch (NoSuchMethodException expectedBeforeJava8) { 46 return Class.forName("sun.misc.FpUtils").getMethod("nextDown", double.class); 47 } 48 } 49 50 @AndroidIncompatible // TODO(cpovirk): File bug for BigDecimal.doubleValue(). testBigToDouble()51 public void testBigToDouble() { 52 for (BigInteger b : ALL_BIGINTEGER_CANDIDATES) { 53 if (b.doubleValue() != DoubleUtils.bigToDouble(b)) { 54 failFormat( 55 "Converting %s to double: expected doubleValue %s but got bigToDouble %s", 56 b, b.doubleValue(), DoubleUtils.bigToDouble(b)); 57 } 58 } 59 } 60 testEnsureNonNegative()61 public void testEnsureNonNegative() { 62 assertEquals(0.0, DoubleUtils.ensureNonNegative(0.0)); 63 for (double positiveValue : POSITIVE_FINITE_DOUBLE_CANDIDATES) { 64 assertEquals(positiveValue, DoubleUtils.ensureNonNegative(positiveValue)); 65 assertEquals(0.0, DoubleUtils.ensureNonNegative(-positiveValue)); 66 } 67 assertEquals(Double.POSITIVE_INFINITY, DoubleUtils.ensureNonNegative(Double.POSITIVE_INFINITY)); 68 assertEquals(0.0, DoubleUtils.ensureNonNegative(Double.NEGATIVE_INFINITY)); 69 assertThrows(IllegalArgumentException.class, () -> DoubleUtils.ensureNonNegative(Double.NaN)); 70 } 71 testOneBits()72 public void testOneBits() { 73 assertEquals(DoubleUtils.ONE_BITS, Double.doubleToRawLongBits(1.0)); 74 } 75 failFormat(String template, Object... args)76 private static void failFormat(String template, Object... args) { 77 fail(String.format(template, args)); 78 } 79 } 80