aboutsummaryrefslogtreecommitdiffstats
path: root/guava-tests/test/com/google/common/math/DoubleUtilsTest.java
blob: f2f99a780e66842029d02e41f9cbbf42d0995b99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.google.common.math;

import static com.google.common.math.MathTesting.ALL_BIGINTEGER_CANDIDATES;
import static com.google.common.math.MathTesting.ALL_DOUBLE_CANDIDATES;
import static com.google.common.math.MathTesting.EXPONENTS;
import static com.google.common.math.MathTesting.FINITE_DOUBLE_CANDIDATES;

import java.math.BigInteger;

import junit.framework.TestCase;
import sun.misc.FpUtils;

public class DoubleUtilsTest extends TestCase {
  public strictfp void testScalbPositiveExponent() {
    for (int k : EXPONENTS) {
      for (double d : ALL_DOUBLE_CANDIDATES) {
        assertEquals(d * StrictMath.pow(2.0, k), DoubleUtils.scalb(d, k));
      }
    }
  }

  public void testGetExponent() {
    for (double d : ALL_DOUBLE_CANDIDATES) {
      assertEquals(FpUtils.getExponent(d), DoubleUtils.getExponent(d));
    }
  }

  public void testNextUp() {
    for (double d : FINITE_DOUBLE_CANDIDATES) {
      assertEquals(FpUtils.nextUp(d), DoubleUtils.next(d, true));
    }
  }

  public void testNextDown() {
    for (double d : FINITE_DOUBLE_CANDIDATES) {
      assertEquals(FpUtils.nextDown(d), DoubleUtils.next(d, false));
    }
  }
  
  public void testBigToDouble() {
    for (BigInteger b : ALL_BIGINTEGER_CANDIDATES) {
      assertEquals(b.doubleValue(), DoubleUtils.bigToDouble(b));
    }
  }
}