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