• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 junit.framework.TestCase;
24 
25 import sun.misc.FpUtils;
26 
27 import java.math.BigInteger;
28 
29 /**
30  * Tests for {@link DoubleUtils}.
31  *
32  * @author Louis Wasserman
33  */
34 public class DoubleUtilsTest extends TestCase {
testNextDown()35   public void testNextDown() {
36     for (double d : FINITE_DOUBLE_CANDIDATES) {
37       assertEquals(FpUtils.nextDown(d), DoubleUtils.nextDown(d));
38     }
39   }
40 
testBigToDouble()41   public void testBigToDouble() {
42     for (BigInteger b : ALL_BIGINTEGER_CANDIDATES) {
43       assertEquals(b.doubleValue(), DoubleUtils.bigToDouble(b));
44     }
45   }
46 
testEnsureNonNegative()47   public void testEnsureNonNegative() {
48     assertEquals(0.0, DoubleUtils.ensureNonNegative(0.0));
49     for (double positiveValue : POSITIVE_FINITE_DOUBLE_CANDIDATES) {
50       assertEquals(positiveValue, DoubleUtils.ensureNonNegative(positiveValue));
51       assertEquals(0.0, DoubleUtils.ensureNonNegative(-positiveValue));
52     }
53     assertEquals(Double.POSITIVE_INFINITY, DoubleUtils.ensureNonNegative(Double.POSITIVE_INFINITY));
54     assertEquals(0.0, DoubleUtils.ensureNonNegative(Double.NEGATIVE_INFINITY));
55     try {
56       DoubleUtils.ensureNonNegative(Double.NaN);
57       fail("Expected IllegalArgumentException from ensureNonNegative(Double.NaN)");
58     } catch (IllegalArgumentException expected) {
59     }
60   }
61 }
62