test/java/lang/Math/Tests.java

Print this page


   1 /*
   2  * Copyright (c) 2003, 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  * Shared static test methods for numerical tests.  Sharing these
  26  * helper test methods avoids repeated functions in the various test
  27  * programs.  The test methods return 1 for a test failure and 0 for
  28  * success.  The order of arguments to the test methods is generally
  29  * the test name, followed by the test arguments, the computed result,
  30  * and finally the expected result.
  31  */
  32 
  33 import sun.misc.FpUtils;
  34 
  35 public class Tests {
  36     private Tests(){}; // do not instantiate
  37 
  38     private static String toHexString(float f) {
  39         if (!Float.isNaN(f))
  40             return Float.toHexString(f);
  41         else
  42             return "NaN(0x" + Integer.toHexString(Float.floatToRawIntBits(f)) + ")";
  43     }
  44 
  45     private static String toHexString(double d) {
  46         if (!Double.isNaN(d))
  47             return Double.toHexString(d);
  48         else
  49             return "NaN(0x" + Long.toHexString(Double.doubleToRawLongBits(d)) + ")";
  50     }
  51 










  52     public static int test(String testName, float input,
  53                            boolean result, boolean expected) {
  54         if (expected != result) {
  55             System.err.println("Failure for " + testName + ":\n" +
  56                                "\tFor input " + input    + "\t(" + toHexString(input) + ")\n" +
  57                                "\texpected  " + expected + "\n"  +
  58                                "\tgot       " + result   + ").");
  59             return 1;
  60         }
  61         else
  62             return 0;
  63     }
  64 
  65     public static int test(String testName, double input,
  66                            boolean result, boolean expected) {
  67         if (expected != result) {
  68             System.err.println("Failure for " + testName + ":\n" +
  69                                "\tFor input " + input    + "\t(" + toHexString(input) + ")\n" +
  70                                "\texpected  " + expected + "\n"  +
  71                                "\tgot       " + result   + ").");


 321     public static int testTolerance(String testName, double input,
 322                                     double result, double expected, double tolerance) {
 323         if (Double.compare(expected, result ) != 0) {
 324             double difference = expected - result;
 325             if (FpUtils.isUnordered(expected, result) ||
 326                 Double.isNaN(difference) ||
 327                 // fail if greater than or unordered
 328                 !(Math.abs((difference)/expected) <= StrictMath.pow(10, -tolerance)) ) {
 329                 System.err.println("Failure for " + testName + ":\n" +
 330                                    "\tFor input " + input    + "\t(" + toHexString(input) + ")\n" +
 331                                    "\texpected  " + expected + "\t(" + toHexString(expected) + ")\n" +
 332                                    "\tgot       " + result   + "\t(" + toHexString(result) + ");\n" +
 333                                    "\tdifference greater than tolerance 10^-" + tolerance);
 334                 return 1;
 335             }
 336             return 0;
 337         }
 338         else
 339             return 0;
 340     }




















 341 }
   1 /*
   2  * Copyright (c) 2003, 2011 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  * Shared static test methods for numerical tests.  Sharing these
  26  * helper test methods avoids repeated functions in the various test
  27  * programs.  The test methods return 1 for a test failure and 0 for
  28  * success.  The order of arguments to the test methods is generally
  29  * the test name, followed by the test arguments, the computed result,
  30  * and finally the expected result.
  31  */
  32 
  33 import sun.misc.FpUtils;
  34 
  35 public class Tests {
  36     private Tests(){}; // do not instantiate
  37 
  38     public static String toHexString(float f) {
  39         if (!Float.isNaN(f))
  40             return Float.toHexString(f);
  41         else
  42             return "NaN(0x" + Integer.toHexString(Float.floatToRawIntBits(f)) + ")";
  43     }
  44 
  45     public static String toHexString(double d) {
  46         if (!Double.isNaN(d))
  47             return Double.toHexString(d);
  48         else
  49             return "NaN(0x" + Long.toHexString(Double.doubleToRawLongBits(d)) + ")";
  50     }
  51 
  52     /**
  53      * Return the floating-point value next larger in magnitude.
  54      */
  55     public static double nextOut(double d) {
  56         if (d > 0.0)
  57             return Math.nextUp(d);
  58         else
  59             return -Math.nextUp(-d);
  60     }
  61 
  62     public static int test(String testName, float input,
  63                            boolean result, boolean expected) {
  64         if (expected != result) {
  65             System.err.println("Failure for " + testName + ":\n" +
  66                                "\tFor input " + input    + "\t(" + toHexString(input) + ")\n" +
  67                                "\texpected  " + expected + "\n"  +
  68                                "\tgot       " + result   + ").");
  69             return 1;
  70         }
  71         else
  72             return 0;
  73     }
  74 
  75     public static int test(String testName, double input,
  76                            boolean result, boolean expected) {
  77         if (expected != result) {
  78             System.err.println("Failure for " + testName + ":\n" +
  79                                "\tFor input " + input    + "\t(" + toHexString(input) + ")\n" +
  80                                "\texpected  " + expected + "\n"  +
  81                                "\tgot       " + result   + ").");


 331     public static int testTolerance(String testName, double input,
 332                                     double result, double expected, double tolerance) {
 333         if (Double.compare(expected, result ) != 0) {
 334             double difference = expected - result;
 335             if (FpUtils.isUnordered(expected, result) ||
 336                 Double.isNaN(difference) ||
 337                 // fail if greater than or unordered
 338                 !(Math.abs((difference)/expected) <= StrictMath.pow(10, -tolerance)) ) {
 339                 System.err.println("Failure for " + testName + ":\n" +
 340                                    "\tFor input " + input    + "\t(" + toHexString(input) + ")\n" +
 341                                    "\texpected  " + expected + "\t(" + toHexString(expected) + ")\n" +
 342                                    "\tgot       " + result   + "\t(" + toHexString(result) + ");\n" +
 343                                    "\tdifference greater than tolerance 10^-" + tolerance);
 344                 return 1;
 345             }
 346             return 0;
 347         }
 348         else
 349             return 0;
 350     }
 351 
 352     // For a successful test, the result must be within the upper and
 353     // lower bounds.
 354     public static int testBounds(String testName, double input, double result,
 355                                  double bound1, double bound2) {
 356         if ((result >= bound1 && result <= bound2) ||
 357             (result <= bound1 && result >= bound2))
 358             return 0;
 359         else {
 360             double lowerBound = Math.min(bound1, bound2);
 361             double upperBound = Math.max(bound1, bound2);
 362             System.err.println("Failure for " + testName + ":\n" +
 363                                "\tFor input " + input    + "\t(" + toHexString(input) + ")\n" +
 364                                "\tgot       " + result   + "\t(" + toHexString(result) + ");\n" +
 365                                "\toutside of range\n" +
 366                                "\t[" + lowerBound    + "\t(" + toHexString(lowerBound) + "), " +
 367                                upperBound    + "\t(" + toHexString(upperBound) + ")]");
 368             return 1;
 369         }
 370     }
 371 }