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  */


 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  */


 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 
 342     // For a successful test, the result must be within the upper and
 343     // lower bounds.
 344     public static int testBounds(String testName, double input, double result,
 345                                  double bound1, double bound2) {
 346         if ((result >= bound1 && result <= bound2) ||
 347             (result <= bound1 && result >= bound2))
 348             return 0;
 349         else {
 350             double lowerBound = Math.min(bound1, bound2);
 351             double upperBound = Math.max(bound1, bound2);
 352             System.err.println("Failure for " + testName + ":\n" +
 353                                "\tFor input " + input    + "\t(" + toHexString(input) + ")\n" +
 354                                "\tgot       " + result   + "\t(" + toHexString(result) + ");\n" +
 355                                "\toutside of range\n" +
 356                                "\t[" + lowerBound    + "\t(" + toHexString(lowerBound) + "), " +
 357                                upperBound    + "\t(" + toHexString(upperBound) + ")]");
 358             return 1;
 359         }
 360     }
 361 }