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