< prev index next >

test/jdk/java/lang/Math/ExactArithTests.java

Print this page


   1 /*
   2  * Copyright (c) 2012, 2016, 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  */


  39 
  40     /**
  41      * @param args the command line arguments
  42      */
  43     public static void main(String[] args) {
  44         testIntegerExact();
  45         testLongExact();
  46         testLongIntExact();
  47 
  48         if (errors > 0) {
  49             throw new RuntimeException(errors + " errors found in ExactArithTests.");
  50         }
  51     }
  52 
  53     static void fail(String message) {
  54         errors++;
  55         System.err.println(message);
  56     }
  57 
  58     /**
  59      * Test Math.addExact, multiplyExact, subtractExact, toIntValue methods
  60      * with {@code int} arguments.
  61      */
  62     static void testIntegerExact() {
  63         testIntegerExact(0, 0);
  64         testIntegerExact(1, 1);
  65         testIntegerExact(1, -1);
  66         testIntegerExact(-1, 1);
  67         testIntegerExact(1000, 2000);
  68 
  69         testIntegerExact(Integer.MIN_VALUE, Integer.MIN_VALUE);
  70         testIntegerExact(Integer.MAX_VALUE, Integer.MAX_VALUE);
  71         testIntegerExact(Integer.MIN_VALUE, 1);
  72         testIntegerExact(Integer.MAX_VALUE, 1);
  73         testIntegerExact(Integer.MIN_VALUE, 2);
  74         testIntegerExact(Integer.MAX_VALUE, 2);
  75         testIntegerExact(Integer.MIN_VALUE, -1);
  76         testIntegerExact(Integer.MAX_VALUE, -1);
  77         testIntegerExact(Integer.MIN_VALUE, -2);
  78         testIntegerExact(Integer.MAX_VALUE, -2);
  79 
  80     }
  81 
  82     /**
  83      * Test exact arithmetic by comparing with the same operations using long
  84      * and checking that the result is the same as the integer truncation.
  85      * Errors are reported with {@link fail}.
  86      *
  87      * @param x first parameter
  88      * @param y second parameter
  89      */
  90     static void testIntegerExact(int x, int y) {
  91         try {
  92             // Test addExact
  93             int sum = Math.addExact(x, y);
  94             long sum2 = (long) x + (long) y;
  95             if ((int) sum2 != sum2) {
  96                 fail("FAIL: int Math.addExact(" + x + " + " + y + ") = " + sum + "; expected Arithmetic exception");
  97             } else if (sum != sum2) {
  98                 fail("FAIL: long Math.addExact(" + x + " + " + y + ") = " + sum + "; expected: " + sum2);
  99             }
 100         } catch (ArithmeticException ex) {
 101             long sum2 = (long) x + (long) y;
 102             if ((int) sum2 == sum2) {
 103                 fail("FAIL: int Math.addExact(" + x + " + " + y + ")" + "; Unexpected exception: " + ex);
 104 
 105             }
 106         }
 107 
 108         try {
 109             // Test subtractExact
 110             int diff = Math.subtractExact(x, y);
 111             long diff2 = (long) x - (long) y;
 112             if ((int) diff2 != diff2) {
 113                 fail("FAIL: int Math.subtractExact(" + x + " - " + y + ") = " + diff + "; expected: " + diff2);
 114             }
 115 
 116         } catch (ArithmeticException ex) {
 117             long diff2 = (long) x - (long) y;
 118             if ((int) diff2 == diff2) {
 119                 fail("FAIL: int Math.subtractExact(" + x + " - " + y + ")" + "; Unexpected exception: " + ex);
 120             }
 121         }
 122 
 123         try {
 124             // Test multiplyExact


 130         } catch (ArithmeticException ex) {
 131             long m2 = (long) x * (long) y;
 132             if ((int) m2 == m2) {
 133                 fail("FAIL: int Math.multiplyExact(" + x + " * " + y + ")" + "; Unexpected exception: " + ex);
 134             }
 135         }
 136 
 137         try {
 138             // Test incrementExact
 139             int inc = Math.incrementExact(x);
 140             long inc2 = (long) x + 1L;
 141             if ((int) inc2 != inc2) {
 142                 fail("FAIL: int Math.incrementExact(" + x + ") = " + inc + "; expected Arithmetic exception");
 143             } else if (inc != inc2) {
 144                 fail("FAIL: long Math.incrementExact(" + x + ") = " + inc + "; expected: " + inc2);
 145             }
 146         } catch (ArithmeticException ex) {
 147             long inc2 = (long) x + 1L;
 148             if ((int) inc2 == inc2) {
 149                 fail("FAIL: int Math.incrementExact(" + x + ")" + "; Unexpected exception: " + ex);
 150 
 151             }
 152         }
 153 
 154         try {
 155             // Test decrementExact
 156             int dec = Math.decrementExact(x);
 157             long dec2 = (long) x - 1L;
 158             if ((int) dec2 != dec2) {
 159                 fail("FAIL: int Math.decrementExact(" + x + ") = " + dec + "; expected Arithmetic exception");
 160             } else if (dec != dec2) {
 161                 fail("FAIL: long Math.decrementExact(" + x + ") = " + dec + "; expected: " + dec2);
 162             }
 163         } catch (ArithmeticException ex) {
 164             long dec2 = (long) x - 1L;
 165             if ((int) dec2 == dec2) {
 166                 fail("FAIL: int Math.decrementExact(" + x + ")" + "; Unexpected exception: " + ex);
 167 
 168             }
 169         }
 170 
 171         try {
 172             // Test negateExact
 173             int neg = Math.negateExact(x);
 174             long neg2 = -((long)x) ;
 175             if ((int) neg2 != neg2) {
 176                 fail("FAIL: int Math.negateExact(" + x + ") = " + neg + "; expected Arithmetic exception");
 177             } else if (neg != neg2) {
 178                 fail("FAIL: long Math.negateExact(" + x + ") = " + neg + "; expected: " + neg2);
 179             }
 180         } catch (ArithmeticException ex) {
 181             long neg2 = (long) x - 1L;
 182             if ((int) neg2 == neg2) {
 183                 fail("FAIL: int Math.negateExact(" + x + ")" + "; Unexpected exception: " + ex);
 184 
 185             }
 186         }
 187     }
 188 
 189     /**
 190      * Test Math.addExact, multiplyExact, subtractExact, toIntExact methods
 191      * with {@code long} arguments.
 192      */
 193     static void testLongExact() {
 194         testLongExactTwice(0, 0);
 195         testLongExactTwice(1, 1);
 196         testLongExactTwice(1, -1);
 197         testLongExactTwice(1000, 2000);
 198 
 199         testLongExactTwice(Long.MIN_VALUE, Long.MIN_VALUE);
 200         testLongExactTwice(Long.MAX_VALUE, Long.MAX_VALUE);
 201         testLongExactTwice(Long.MIN_VALUE, 1);
 202         testLongExactTwice(Long.MAX_VALUE, 1);
 203         testLongExactTwice(Long.MIN_VALUE, 2);
 204         testLongExactTwice(Long.MAX_VALUE, 2);
 205         testLongExactTwice(Long.MIN_VALUE, -1);
 206         testLongExactTwice(Long.MAX_VALUE, -1);
 207         testLongExactTwice(Long.MIN_VALUE, -2);
 208         testLongExactTwice(Long.MAX_VALUE, -2);
 209         testLongExactTwice(Long.MIN_VALUE/2, 2);
 210         testLongExactTwice(Long.MAX_VALUE, 2);
 211         testLongExactTwice(Integer.MAX_VALUE, Integer.MAX_VALUE);
 212         testLongExactTwice(Integer.MAX_VALUE, -Integer.MAX_VALUE);
 213         testLongExactTwice(Integer.MAX_VALUE+1, Integer.MAX_VALUE+1);
 214         testLongExactTwice(Integer.MAX_VALUE+1, -Integer.MAX_VALUE+1);
 215         testLongExactTwice(Integer.MIN_VALUE-1, Integer.MIN_VALUE-1);
 216         testLongExactTwice(Integer.MIN_VALUE-1, -Integer.MIN_VALUE-1);
 217         testLongExactTwice(Integer.MIN_VALUE/2, 2);
 218 
 219     }
 220 
 221     /**
 222      * Test each of the exact operations with the arguments and
 223      * with the arguments reversed.
 224      * @param x
 225      * @param y
 226      */
 227     static void testLongExactTwice(long x, long y) {
 228         testLongExact(x, y);
 229         testLongExact(y, x);
 230     }
 231 
 232 
 233     /**
 234      * Test long exact arithmetic by comparing with the same operations using BigInteger
 235      * and checking that the result is the same as the long truncation.
 236      * Errors are reported with {@link fail}.
 237      *
 238      * @param x first parameter


 302             resultBig = xBig.negate();
 303             long dec = Math.negateExact(x);
 304             checkResult("long Math.negateExact", x, 0L, dec, resultBig);
 305         } catch (ArithmeticException ex) {
 306             if (inLongRange(resultBig)) {
 307                 fail("FAIL: long Math.negateExact(" + x + "); Unexpected exception: " + ex);
 308             }
 309         }
 310 
 311         try {
 312             // Test toIntExact
 313             int value = Math.toIntExact(x);
 314             if ((long)value != x) {
 315                 fail("FAIL: " + "long Math.toIntExact" + "(" + x + ") = " + value + "; expected an arithmetic exception: ");
 316             }
 317         } catch (ArithmeticException ex) {
 318             if (resultBig.bitLength() <= 32) {
 319                 fail("FAIL: long Math.toIntExact(" + x + ")" + "; Unexpected exception: " + ex);
 320             }
 321         }
 322 
 323     }
 324 
 325     /**
 326      * Compare the expected and actual results.
 327      * @param message message for the error
 328      * @param x first argument
 329      * @param y second argument
 330      * @param result actual result value
 331      * @param expected expected result value
 332      */
 333     static void checkResult(String message, long x, long y, long result, BigInteger expected) {
 334         BigInteger resultBig = BigInteger.valueOf(result);
 335         if (!inLongRange(expected)) {
 336             fail("FAIL: " + message + "(" + x + ", " + y + ") = " + result + "; expected an arithmetic exception: ");
 337         } else if (!resultBig.equals(expected)) {
 338             fail("FAIL: " + message + "(" + x + ", " + y + ") = " + result + "; expected " + expected);
 339         }
 340     }
 341 
 342     /**


   1 /*
   2  * Copyright (c) 2012, 2019, 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  */


  39 
  40     /**
  41      * @param args the command line arguments
  42      */
  43     public static void main(String[] args) {
  44         testIntegerExact();
  45         testLongExact();
  46         testLongIntExact();
  47 
  48         if (errors > 0) {
  49             throw new RuntimeException(errors + " errors found in ExactArithTests.");
  50         }
  51     }
  52 
  53     static void fail(String message) {
  54         errors++;
  55         System.err.println(message);
  56     }
  57 
  58     /**
  59      * Test Math.addExact, multiplyExact, subtractExact, incrementExact,
  60      * decrementExact, negateExact methods with {@code int} arguments.
  61      */
  62     static void testIntegerExact() {
  63         testIntegerExact(0, 0);
  64         testIntegerExact(1, 1);
  65         testIntegerExact(1, -1);
  66         testIntegerExact(-1, 1);
  67         testIntegerExact(1000, 2000);
  68 
  69         testIntegerExact(Integer.MIN_VALUE, Integer.MIN_VALUE);
  70         testIntegerExact(Integer.MAX_VALUE, Integer.MAX_VALUE);
  71         testIntegerExact(Integer.MIN_VALUE, 1);
  72         testIntegerExact(Integer.MAX_VALUE, 1);
  73         testIntegerExact(Integer.MIN_VALUE, 2);
  74         testIntegerExact(Integer.MAX_VALUE, 2);
  75         testIntegerExact(Integer.MIN_VALUE, -1);
  76         testIntegerExact(Integer.MAX_VALUE, -1);
  77         testIntegerExact(Integer.MIN_VALUE, -2);
  78         testIntegerExact(Integer.MAX_VALUE, -2);

  79     }
  80 
  81     /**
  82      * Test exact arithmetic by comparing with the same operations using long
  83      * and checking that the result is the same as the integer truncation.
  84      * Errors are reported with {@link fail}.
  85      *
  86      * @param x first parameter
  87      * @param y second parameter
  88      */
  89     static void testIntegerExact(int x, int y) {
  90         try {
  91             // Test addExact
  92             int sum = Math.addExact(x, y);
  93             long sum2 = (long) x + (long) y;
  94             if ((int) sum2 != sum2) {
  95                 fail("FAIL: int Math.addExact(" + x + " + " + y + ") = " + sum + "; expected Arithmetic exception");
  96             } else if (sum != sum2) {
  97                 fail("FAIL: long Math.addExact(" + x + " + " + y + ") = " + sum + "; expected: " + sum2);
  98             }
  99         } catch (ArithmeticException ex) {
 100             long sum2 = (long) x + (long) y;
 101             if ((int) sum2 == sum2) {
 102                 fail("FAIL: int Math.addExact(" + x + " + " + y + ")" + "; Unexpected exception: " + ex);

 103             }
 104         }
 105 
 106         try {
 107             // Test subtractExact
 108             int diff = Math.subtractExact(x, y);
 109             long diff2 = (long) x - (long) y;
 110             if ((int) diff2 != diff2) {
 111                 fail("FAIL: int Math.subtractExact(" + x + " - " + y + ") = " + diff + "; expected: " + diff2);
 112             }
 113 
 114         } catch (ArithmeticException ex) {
 115             long diff2 = (long) x - (long) y;
 116             if ((int) diff2 == diff2) {
 117                 fail("FAIL: int Math.subtractExact(" + x + " - " + y + ")" + "; Unexpected exception: " + ex);
 118             }
 119         }
 120 
 121         try {
 122             // Test multiplyExact


 128         } catch (ArithmeticException ex) {
 129             long m2 = (long) x * (long) y;
 130             if ((int) m2 == m2) {
 131                 fail("FAIL: int Math.multiplyExact(" + x + " * " + y + ")" + "; Unexpected exception: " + ex);
 132             }
 133         }
 134 
 135         try {
 136             // Test incrementExact
 137             int inc = Math.incrementExact(x);
 138             long inc2 = (long) x + 1L;
 139             if ((int) inc2 != inc2) {
 140                 fail("FAIL: int Math.incrementExact(" + x + ") = " + inc + "; expected Arithmetic exception");
 141             } else if (inc != inc2) {
 142                 fail("FAIL: long Math.incrementExact(" + x + ") = " + inc + "; expected: " + inc2);
 143             }
 144         } catch (ArithmeticException ex) {
 145             long inc2 = (long) x + 1L;
 146             if ((int) inc2 == inc2) {
 147                 fail("FAIL: int Math.incrementExact(" + x + ")" + "; Unexpected exception: " + ex);

 148             }
 149         }
 150 
 151         try {
 152             // Test decrementExact
 153             int dec = Math.decrementExact(x);
 154             long dec2 = (long) x - 1L;
 155             if ((int) dec2 != dec2) {
 156                 fail("FAIL: int Math.decrementExact(" + x + ") = " + dec + "; expected Arithmetic exception");
 157             } else if (dec != dec2) {
 158                 fail("FAIL: long Math.decrementExact(" + x + ") = " + dec + "; expected: " + dec2);
 159             }
 160         } catch (ArithmeticException ex) {
 161             long dec2 = (long) x - 1L;
 162             if ((int) dec2 == dec2) {
 163                 fail("FAIL: int Math.decrementExact(" + x + ")" + "; Unexpected exception: " + ex);

 164             }
 165         }
 166 
 167         try {
 168             // Test negateExact
 169             int neg = Math.negateExact(x);
 170             long neg2 = -((long)x) ;
 171             if ((int) neg2 != neg2) {
 172                 fail("FAIL: int Math.negateExact(" + x + ") = " + neg + "; expected Arithmetic exception");
 173             } else if (neg != neg2) {
 174                 fail("FAIL: long Math.negateExact(" + x + ") = " + neg + "; expected: " + neg2);
 175             }
 176         } catch (ArithmeticException ex) {
 177             long neg2 = (long) x - 1L;
 178             if ((int) neg2 == neg2) {
 179                 fail("FAIL: int Math.negateExact(" + x + ")" + "; Unexpected exception: " + ex);

 180             }
 181         }
 182     }
 183 
 184     /**
 185      * Test Math.addExact, multiplyExact, subtractExact, incrementExact,
 186      * decrementExact, negateExact, toIntExact methods with {@code long} arguments.
 187      */
 188     static void testLongExact() {
 189         testLongExactTwice(0, 0);
 190         testLongExactTwice(1, 1);
 191         testLongExactTwice(1, -1);
 192         testLongExactTwice(1000, 2000);
 193 
 194         testLongExactTwice(Long.MIN_VALUE, Long.MIN_VALUE);
 195         testLongExactTwice(Long.MAX_VALUE, Long.MAX_VALUE);
 196         testLongExactTwice(Long.MIN_VALUE, 1);
 197         testLongExactTwice(Long.MAX_VALUE, 1);
 198         testLongExactTwice(Long.MIN_VALUE, 2);
 199         testLongExactTwice(Long.MAX_VALUE, 2);
 200         testLongExactTwice(Long.MIN_VALUE, -1);
 201         testLongExactTwice(Long.MAX_VALUE, -1);
 202         testLongExactTwice(Long.MIN_VALUE, -2);
 203         testLongExactTwice(Long.MAX_VALUE, -2);
 204         testLongExactTwice(Long.MIN_VALUE/2, 2);
 205         testLongExactTwice(Long.MAX_VALUE, 2);
 206         testLongExactTwice(Integer.MAX_VALUE, Integer.MAX_VALUE);
 207         testLongExactTwice(Integer.MAX_VALUE, -Integer.MAX_VALUE);
 208         testLongExactTwice(Integer.MAX_VALUE+1, Integer.MAX_VALUE+1);
 209         testLongExactTwice(Integer.MAX_VALUE+1, -Integer.MAX_VALUE+1);
 210         testLongExactTwice(Integer.MIN_VALUE-1, Integer.MIN_VALUE-1);
 211         testLongExactTwice(Integer.MIN_VALUE-1, -Integer.MIN_VALUE-1);
 212         testLongExactTwice(Integer.MIN_VALUE/2, 2);

 213     }
 214 
 215     /**
 216      * Test each of the exact operations with the arguments and
 217      * with the arguments reversed.
 218      * @param x
 219      * @param y
 220      */
 221     static void testLongExactTwice(long x, long y) {
 222         testLongExact(x, y);
 223         testLongExact(y, x);
 224     }
 225 
 226 
 227     /**
 228      * Test long exact arithmetic by comparing with the same operations using BigInteger
 229      * and checking that the result is the same as the long truncation.
 230      * Errors are reported with {@link fail}.
 231      *
 232      * @param x first parameter


 296             resultBig = xBig.negate();
 297             long dec = Math.negateExact(x);
 298             checkResult("long Math.negateExact", x, 0L, dec, resultBig);
 299         } catch (ArithmeticException ex) {
 300             if (inLongRange(resultBig)) {
 301                 fail("FAIL: long Math.negateExact(" + x + "); Unexpected exception: " + ex);
 302             }
 303         }
 304 
 305         try {
 306             // Test toIntExact
 307             int value = Math.toIntExact(x);
 308             if ((long)value != x) {
 309                 fail("FAIL: " + "long Math.toIntExact" + "(" + x + ") = " + value + "; expected an arithmetic exception: ");
 310             }
 311         } catch (ArithmeticException ex) {
 312             if (resultBig.bitLength() <= 32) {
 313                 fail("FAIL: long Math.toIntExact(" + x + ")" + "; Unexpected exception: " + ex);
 314             }
 315         }

 316     }
 317 
 318     /**
 319      * Compare the expected and actual results.
 320      * @param message message for the error
 321      * @param x first argument
 322      * @param y second argument
 323      * @param result actual result value
 324      * @param expected expected result value
 325      */
 326     static void checkResult(String message, long x, long y, long result, BigInteger expected) {
 327         BigInteger resultBig = BigInteger.valueOf(result);
 328         if (!inLongRange(expected)) {
 329             fail("FAIL: " + message + "(" + x + ", " + y + ") = " + result + "; expected an arithmetic exception: ");
 330         } else if (!resultBig.equals(expected)) {
 331             fail("FAIL: " + message + "(" + x + ", " + y + ") = " + result + "; expected " + expected);
 332         }
 333     }
 334 
 335     /**


< prev index next >