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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
/*
* Copyright (c) 2004, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 8304028
* @summary Tests for {Math, StrictMath}.IEEEremainder
*/
public class IeeeRemainderTests {
private IeeeRemainderTests(){}
public static void main(String... args) {
int failures = 0;
failures += testIeeeRemainderSpecials();
failures += testIeeeRemainderZeroResult();
failures += testIeeeRemainderOneResult();
failures += testIeeeRemainderRounding();
if (failures > 0) {
System.err.println("Testing IEEEremainder incurred "
+ failures + " failures.");
throw new RuntimeException();
}
}
private static final double NaNd = Double.NaN;
private static final double MIN_VALUE = Double.MIN_VALUE;
private static final double MIN_NORM = Double.MIN_NORMAL;
private static final double MAX_VALUE = Double.MAX_VALUE;
private static final double InfinityD = Double.POSITIVE_INFINITY;
/**
* Special cases from the spec interspersed with test cases.
*/
private static int testIeeeRemainderSpecials() {
int failures = 0;
/*
* If either argument is NaN, or the first argument is
* infinite, or the second argument is positive zero or
* negative zero, then the result is NaN.
*
*/
for(double nan : Tests.NaNs) {
failures += testIEEEremainderCase(nan, 1.0, NaNd);
failures += testIEEEremainderCase(1.0, nan, NaNd);
}
double [][] nanResultCases = {
{ InfinityD, InfinityD},
{-InfinityD, InfinityD},
{ InfinityD, 1.0},
{-InfinityD, 1.0},
{ InfinityD, NaNd},
{-InfinityD, NaNd},
{ InfinityD, 0.0},
{-InfinityD, 0.0},
{ InfinityD, -0.0},
{-InfinityD, -0.0},
};
for(double[] testCase : nanResultCases) {
failures += testIEEEremainderCase(testCase[0], testCase[1], NaNd);
}
/*
* If the first argument is finite and the second argument is
* infinite, then the result is the same as the first
* argument.
*
*/
double [] specialCases = {
+0.0,
+MIN_VALUE,
+MIN_NORM,
+MAX_VALUE,
-0.0,
-MIN_VALUE,
-MIN_NORM,
-MAX_VALUE,
};
double [] infinities = {
+InfinityD,
-InfinityD
};
for (double specialCase : specialCases) {
for (double infinity: infinities) {
failures += testIEEEremainderCase(specialCase, infinity, specialCase);
}
}
return failures;
}
private static int testIeeeRemainderZeroResult() {
int failures = 0;
double [] testCases = {
+MIN_VALUE,
+MIN_NORM,
+MAX_VALUE*0.5,
-MIN_VALUE,
-MIN_NORM,
-MAX_VALUE*0.5,
};
for (double testCase : testCases) {
/*
* "If the remainder is zero, its sign is the same as the sign of the first argument."
*/
failures += testIEEEremainderCase(testCase*2.0, +testCase, Math.copySign(0.0, testCase));
failures += testIEEEremainderCase(testCase*2.0, -testCase, Math.copySign(0.0, testCase));
}
return failures;
}
/*
* Construct test cases where the remainder is one.
*/
private static int testIeeeRemainderOneResult() {
int failures = 0;
double [][] testCases = {
{4.0, 3.0},
{10_001.0, 5000.0},
{15_001.0, 5000.0},
{10_000.0, 9999.0},
{0x1.0p52 + 1.0, 0x1.0p52},
{0x1.fffffffffffffp52, 0x1.ffffffffffffep52},
};
for (var testCase : testCases) {
failures += testIEEEremainderCase(testCase[0], testCase[1], 1.0);
}
return failures;
}
/*
* Test cases that differ in rounding between % and IEEEremainder.
*/
private static int testIeeeRemainderRounding() {
int failures = 0;
double [][] testCases = {
{3.0, 2.0, -1.0},
{3.0, -2.0, -1.0},
};
for (var testCase : testCases) {
failures += testIEEEremainderCase(testCase[0], testCase[1], testCase[2]);
}
return failures;
}
/*
* For exact cases, built-in % remainder and IEEE remainder should
* be the same since the rounding mode in the implicit divide
* doesn't come into play.
*/
private static double remainder(double a, double b) {
return a % b;
}
private static int testIEEEremainderCase(double input1, double input2, double expected) {
int failures = 0;
failures += Tests.test("StrictMath.IEEEremainder", input1, input2, StrictMath::IEEEremainder, expected);
failures += Tests.test("Math.IEEEremainder", input1, input2, Math::IEEEremainder, expected);
return failures;
}
}
|