test/java/lang/Math/CeilAndFloorTests.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 6908131
  27  * @summary Check for correct implementation of Math.ceil and Math.floor



  28  */
  29 
  30 import sun.misc.DoubleConsts;
  31 
  32 public class CeilAndFloorTests {
  33     private static int testCeilCase(double input, double expected) {
  34         int failures = 0;
  35         failures += Tests.test("Math.ceil",  input, Math.ceil(input),   expected);
  36         failures += Tests.test("StrictMath.ceil",  input, StrictMath.ceil(input), expected);
  37         return failures;
  38     }
  39 
  40     private static int testFloorCase(double input, double expected) {
  41         int failures = 0;
  42         failures += Tests.test("Math.floor",  input, Math.floor(input),   expected);
  43         failures += Tests.test("StrictMath.floor",  input, StrictMath.floor(input), expected);
  44         return failures;
  45     }
  46 
  47     private static int nearIntegerTests() {
  48         int failures = 0;
  49 
  50         double [] fixedPoints = {
  51             -0.0,


 122             failures += testFloorCase( d, d);
 123             failures += testFloorCase(-d, -d);
 124 
 125             if (Math.abs(d) > 1.0) {
 126                 failures += testCeilCase( neighborDown, d);
 127                 failures += testCeilCase(-neighborDown, -d+1);
 128 
 129                 failures += testFloorCase( neighborUp, d);
 130                 failures += testFloorCase(-neighborUp, -d-1);
 131             }
 132         }
 133 
 134         return failures;
 135     }
 136 
 137     public static int roundingTests() {
 138         int failures = 0;
 139         double [][] testCases = {
 140             { Double.MIN_VALUE,                           1.0},
 141             {-Double.MIN_VALUE,                          -0.0},
 142             { Math.nextDown(DoubleConsts.MIN_NORMAL),     1.0},
 143             {-Math.nextDown(DoubleConsts.MIN_NORMAL),    -0.0},
 144             { DoubleConsts.MIN_NORMAL,                    1.0},
 145             {-DoubleConsts.MIN_NORMAL,                   -0.0},
 146 
 147             { 0.1,                                        1.0},
 148             {-0.1,                                       -0.0},
 149 
 150             { 0.5,                                        1.0},
 151             {-0.5,                                       -0.0},
 152 
 153             { 1.5,                                        2.0},
 154             {-1.5,                                       -1.0},
 155 
 156             { 2.5,                                        3.0},
 157             {-2.5,                                       -2.0},
 158 
 159             { Math.nextDown(1.0),                         1.0},
 160             { Math.nextDown(-1.0),                       -1.0},
 161 
 162             { Math.nextUp(1.0),                           2.0},
 163             { Math.nextUp(-1.0),                         -0.0},
 164 
 165             { 0x1.0p51,                                 0x1.0p51},




   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 6908131
  27  * @summary Check for correct implementation of Math.ceil and Math.floor
  28  * @library /lib/testlibrary
  29  * @build jdk.testlibrary.DoubleUtils jdk.testlibrary.FloatUtils
  30  * @run main CeilAndFloorTests
  31  */
  32 


  33 public class CeilAndFloorTests {
  34     private static int testCeilCase(double input, double expected) {
  35         int failures = 0;
  36         failures += Tests.test("Math.ceil",  input, Math.ceil(input),   expected);
  37         failures += Tests.test("StrictMath.ceil",  input, StrictMath.ceil(input), expected);
  38         return failures;
  39     }
  40 
  41     private static int testFloorCase(double input, double expected) {
  42         int failures = 0;
  43         failures += Tests.test("Math.floor",  input, Math.floor(input),   expected);
  44         failures += Tests.test("StrictMath.floor",  input, StrictMath.floor(input), expected);
  45         return failures;
  46     }
  47 
  48     private static int nearIntegerTests() {
  49         int failures = 0;
  50 
  51         double [] fixedPoints = {
  52             -0.0,


 123             failures += testFloorCase( d, d);
 124             failures += testFloorCase(-d, -d);
 125 
 126             if (Math.abs(d) > 1.0) {
 127                 failures += testCeilCase( neighborDown, d);
 128                 failures += testCeilCase(-neighborDown, -d+1);
 129 
 130                 failures += testFloorCase( neighborUp, d);
 131                 failures += testFloorCase(-neighborUp, -d-1);
 132             }
 133         }
 134 
 135         return failures;
 136     }
 137 
 138     public static int roundingTests() {
 139         int failures = 0;
 140         double [][] testCases = {
 141             { Double.MIN_VALUE,                           1.0},
 142             {-Double.MIN_VALUE,                          -0.0},
 143             { Math.nextDown(Double.MIN_NORMAL),           1.0},
 144             {-Math.nextDown(Double.MIN_NORMAL),          -0.0},
 145             { Double.MIN_NORMAL,                          1.0},
 146             {-Double.MIN_NORMAL,                         -0.0},
 147 
 148             { 0.1,                                        1.0},
 149             {-0.1,                                       -0.0},
 150 
 151             { 0.5,                                        1.0},
 152             {-0.5,                                       -0.0},
 153 
 154             { 1.5,                                        2.0},
 155             {-1.5,                                       -1.0},
 156 
 157             { 2.5,                                        3.0},
 158             {-2.5,                                       -2.0},
 159 
 160             { Math.nextDown(1.0),                         1.0},
 161             { Math.nextDown(-1.0),                       -1.0},
 162 
 163             { Math.nextUp(1.0),                           2.0},
 164             { Math.nextUp(-1.0),                         -0.0},
 165 
 166             { 0x1.0p51,                                 0x1.0p51},