test/java/lang/Math/Log10Tests.java

Print this page




   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  * @test
  26  * @bug 4074599 4939441
  27  * @summary Tests for {Math, StrictMath}.log10



  28  * @author Joseph D. Darcy
  29  */
  30 
  31 import sun.misc.DoubleConsts;
  32 
  33 public class Log10Tests {
  34     private Log10Tests(){}
  35 
  36     static final double infinityD = Double.POSITIVE_INFINITY;
  37     static final double NaNd = Double.NaN;
  38     static final double LN_10 = StrictMath.log(10.0);
  39 
  40     // Initialize shared random number generator
  41     static java.util.Random rand = new java.util.Random(0L);
  42 
  43     static int testLog10Case(double input, double expected) {
  44         int failures=0;
  45 
  46         failures+=Tests.test("Math.log10(double)", input,
  47                              Math.log10(input), expected);
  48 
  49         failures+=Tests.test("StrictMath.log10(double)", input,
  50                              StrictMath.log10(input), expected);
  51 
  52         return failures;
  53     }
  54 
  55     static int testLog10() {
  56         int failures = 0;
  57 
  58         double [][] testCases = {
  59             {Double.NaN,                NaNd},
  60             {Double.longBitsToDouble(0x7FF0000000000001L),      NaNd},
  61             {Double.longBitsToDouble(0xFFF0000000000001L),      NaNd},
  62             {Double.longBitsToDouble(0x7FF8555555555555L),      NaNd},
  63             {Double.longBitsToDouble(0xFFF8555555555555L),      NaNd},
  64             {Double.longBitsToDouble(0x7FFFFFFFFFFFFFFFL),      NaNd},
  65             {Double.longBitsToDouble(0xFFFFFFFFFFFFFFFFL),      NaNd},
  66             {Double.longBitsToDouble(0x7FFDeadBeef00000L),      NaNd},
  67             {Double.longBitsToDouble(0xFFFDeadBeef00000L),      NaNd},
  68             {Double.longBitsToDouble(0x7FFCafeBabe00000L),      NaNd},
  69             {Double.longBitsToDouble(0xFFFCafeBabe00000L),      NaNd},
  70             {Double.NEGATIVE_INFINITY,  NaNd},
  71             {-8.0,                      NaNd},
  72             {-1.0,                      NaNd},
  73             {-DoubleConsts.MIN_NORMAL,  NaNd},
  74             {-Double.MIN_VALUE,         NaNd},
  75             {-0.0,                      -infinityD},
  76             {+0.0,                      -infinityD},
  77             {+1.0,                      0.0},
  78             {Double.POSITIVE_INFINITY,  infinityD},
  79         };
  80 
  81         // Test special cases
  82         for(int i = 0; i < testCases.length; i++) {
  83             failures += testLog10Case(testCases[i][0],
  84                                           testCases[i][1]);
  85         }
  86 
  87         // Test log10(10^n) == n for integer n; 10^n, n < 0 is not
  88         // exactly representable as a floating-point value -- up to
  89         // 10^22 can be represented exactly
  90         double testCase = 1.0;
  91         for(int i = 0; i < 23; i++) {
  92             failures += testLog10Case(testCase, i);
  93             testCase *= 10.0;




   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  * @test
  26  * @bug 4074599 4939441
  27  * @summary Tests for {Math, StrictMath}.log10
  28  * @library /lib/testlibrary
  29  * @build jdk.testlibrary.DoubleUtils jdk.testlibrary.FloatUtils
  30  * @run main Log10Tests
  31  * @author Joseph D. Darcy
  32  */
  33 


  34 public class Log10Tests {
  35     private Log10Tests(){}
  36 
  37     static final double infinityD = Double.POSITIVE_INFINITY;
  38     static final double NaNd = Double.NaN;
  39     static final double LN_10 = StrictMath.log(10.0);
  40 
  41     // Initialize shared random number generator
  42     static java.util.Random rand = new java.util.Random(0L);
  43 
  44     static int testLog10Case(double input, double expected) {
  45         int failures=0;
  46 
  47         failures+=Tests.test("Math.log10(double)", input,
  48                              Math.log10(input), expected);
  49 
  50         failures+=Tests.test("StrictMath.log10(double)", input,
  51                              StrictMath.log10(input), expected);
  52 
  53         return failures;
  54     }
  55 
  56     static int testLog10() {
  57         int failures = 0;
  58 
  59         double [][] testCases = {
  60             {Double.NaN,                NaNd},
  61             {Double.longBitsToDouble(0x7FF0000000000001L),      NaNd},
  62             {Double.longBitsToDouble(0xFFF0000000000001L),      NaNd},
  63             {Double.longBitsToDouble(0x7FF8555555555555L),      NaNd},
  64             {Double.longBitsToDouble(0xFFF8555555555555L),      NaNd},
  65             {Double.longBitsToDouble(0x7FFFFFFFFFFFFFFFL),      NaNd},
  66             {Double.longBitsToDouble(0xFFFFFFFFFFFFFFFFL),      NaNd},
  67             {Double.longBitsToDouble(0x7FFDeadBeef00000L),      NaNd},
  68             {Double.longBitsToDouble(0xFFFDeadBeef00000L),      NaNd},
  69             {Double.longBitsToDouble(0x7FFCafeBabe00000L),      NaNd},
  70             {Double.longBitsToDouble(0xFFFCafeBabe00000L),      NaNd},
  71             {Double.NEGATIVE_INFINITY,  NaNd},
  72             {-8.0,                      NaNd},
  73             {-1.0,                      NaNd},
  74             {-Double.MIN_NORMAL,        NaNd},
  75             {-Double.MIN_VALUE,         NaNd},
  76             {-0.0,                      -infinityD},
  77             {+0.0,                      -infinityD},
  78             {+1.0,                      0.0},
  79             {Double.POSITIVE_INFINITY,  infinityD},
  80         };
  81 
  82         // Test special cases
  83         for(int i = 0; i < testCases.length; i++) {
  84             failures += testLog10Case(testCases[i][0],
  85                                           testCases[i][1]);
  86         }
  87 
  88         // Test log10(10^n) == n for integer n; 10^n, n < 0 is not
  89         // exactly representable as a floating-point value -- up to
  90         // 10^22 can be represented exactly
  91         double testCase = 1.0;
  92         for(int i = 0; i < 23; i++) {
  93             failures += testLog10Case(testCase, i);
  94             testCase *= 10.0;