1 /*
2  * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /*
25  *
26  *
27  * Shared static test method for StrictMath tests.
28  */
29 package test.java.lang.StrictMath;
30 
31 import org.testng.Assert;
32 
33 public class Tests {
34 
Tests()35     private Tests() {
36     }
37 
test(String testName, double input, double result, double expected)38     static void test(String testName, double input, double result, double expected) {
39         Assert.assertEquals(Double.compare(expected, result), 0,
40                 "Failure for " + testName + ":\n" +
41                         "\tFor input " + input + "\t(" + Double.toHexString(input) + ")\n" +
42                         "\texpected  " + expected + "\t(" + Double.toHexString(expected) + ")\n" +
43                         "\tgot       " + result + "\t(" + Double.toHexString(result) + ").");
44     }
45 
test(String testName, double input1, double input2, double result, double expected)46     static void test(String testName, double input1, double input2, double result,
47             double expected) {
48         Assert.assertEquals(Double.compare(expected, result), 0,
49                 "Failure for " + testName + ":\n" +
50                         "\tFor input " + input1 + "\t(" + Double.toHexString(input1) + "), " +
51                         +input2 + "\t(" + Double.toHexString(input2) + ")\n" +
52                         "\texpected  " + expected + "\t(" + Double.toHexString(expected) + ")\n" +
53                         "\tgot       " + result + "\t(" + Double.toHexString(result) + ").");
54     }
55 
56     /**
57      * Returns a double over the normalized range of floating-point values.
58      *
59      * @return a double over the normalized range of floating-point values
60      */
createRandomDouble(java.util.Random random)61     static double createRandomDouble(java.util.Random random) {
62         final int EXPONENT_RANGE = Double.MAX_EXPONENT - Double.MIN_EXPONENT + 1;
63 
64         int targetExponent = Double.MIN_EXPONENT + random.nextInt(EXPONENT_RANGE + 1);
65         double tmp = random.nextDouble(); // Double in the range of [0.0, 1.0)
66         int tmpExponent = Math.getExponent(tmp);
67         return Math.scalb(tmp, targetExponent - tmpExponent);
68     }
69 }
70