test/compiler/intrinsics/mathexact/Verify.java
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File hotspot Sdiff test/compiler/intrinsics/mathexact

test/compiler/intrinsics/mathexact/Verify.java

Print this page
rev 7259 : 8044186: Introduce a reproducible random generator
Reviewed-by: iignatyev
Contributed-by: sergei.kovalev@oracle.com
   1 /*
   2  * Copyright (c) 2013, 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  */
  23 







  24 public class Verify {
  25     public static String throwWord(boolean threw) {
  26         return (threw ? "threw" : "didn't throw");
  27     }
  28 
  29     public static void verifyResult(UnaryMethod method, int result1, int result2, boolean exception1, boolean exception2, int value) {
  30         if (exception1 != exception2) {
  31             throw new RuntimeException("Intrinsic version [" + method.name() + "]" + throwWord(exception1) + " exception, NonIntrinsic version" + throwWord(exception2) + " for: " + value);
  32         }
  33         if (result1 != result2) {
  34             throw new RuntimeException("Intrinsic version [" + method.name() + "] returned: " + result1 + " while NonIntrinsic version returned: " + result2);
  35         }
  36     }
  37 
  38     public static void verifyResult(UnaryLongMethod method, long result1, long result2, boolean exception1, boolean exception2, long value) {
  39         if (exception1 != exception2) {
  40             throw new RuntimeException("Intrinsic version [" + method.name() + "]" + throwWord(exception1) + " exception, NonIntrinsic version" + throwWord(exception2) + " for: " + value);
  41         }
  42         if (result1 != result2) {
  43             throw new RuntimeException("Intrinsic version [" + method.name() + "] returned: " + result1 + " while NonIntrinsic version returned: " + result2);


 117 
 118     public static void verifyBinary(long a, long b, BinaryLongMethod method) {
 119         boolean exception1 = false, exception2 = false;
 120         long result1 = 0, result2 = 0;
 121         try {
 122             result1 = method.checkMethod(a, b);
 123         } catch (ArithmeticException e) {
 124             exception1 = true;
 125         }
 126         try {
 127             result2 = method.safeMethod(a, b);
 128         } catch (ArithmeticException e) {
 129             exception2 = true;
 130         }
 131 
 132         verifyResult(method, result1, result2, exception1, exception2, a, b);
 133     }
 134 
 135 
 136     public static class LoadTest {
 137         public static java.util.Random rnd = new java.util.Random();
 138         public static int[] values = new int[256];
 139 
 140         public static void init() {
 141             for (int i = 0; i < values.length; ++i) {
 142                 values[i] = rnd.nextInt();
 143             }
 144         }
 145 
 146         public static void verify(BinaryMethod method) {
 147             for (int i = 0; i < 50000; ++i) {
 148                 Verify.verifyBinary(values[i & 255], values[i & 255] - i, method);
 149                 Verify.verifyBinary(values[i & 255] + i, values[i & 255] - i, method);
 150                 Verify.verifyBinary(values[i & 255], values[i & 255], method);
 151                 if ((i & 1) == 1 && i > 5) {
 152                     Verify.verifyBinary(values[i & 255] + i, values[i & 255] - i, method);
 153                 } else {
 154                     Verify.verifyBinary(values[i & 255] - i, values[i & 255] + i, method);
 155                 }
 156                 Verify.verifyBinary(values[i & 255], values[(i + 1) & 255], method);
 157             }
 158         }
 159     }
 160 
 161     public static class NonConstantTest {
 162         public static java.util.Random rnd = new java.util.Random();
 163         public static int[] values = new int[] { Integer.MAX_VALUE, Integer.MIN_VALUE };
 164 
 165         public static void verify(BinaryMethod method) {
 166             for (int i = 0; i < 50000; ++i) {
 167                 int rnd1 = rnd.nextInt(), rnd2 = rnd.nextInt();
 168                 Verify.verifyBinary(rnd1, rnd2, method);
 169                 Verify.verifyBinary(rnd1, rnd2 + 1, method);
 170                 Verify.verifyBinary(rnd1 + 1, rnd2, method);
 171                 Verify.verifyBinary(rnd1 - 1, rnd2, method);
 172                 Verify.verifyBinary(rnd1, rnd2 - 1, method);
 173                 Verify.verifyBinary(0, values[0], method);
 174                 Verify.verifyBinary(values[0], 0, method);
 175                 Verify.verifyBinary(0, values[1], method);
 176                 Verify.verifyBinary(values[1], 0, method);
 177             }
 178         }
 179     }
 180 
 181     public static class NonConstantLongTest {
 182         public static long[] values = { Long.MIN_VALUE, Long.MAX_VALUE, 0, Long.MAX_VALUE - 1831 };
 183         public static java.util.Random rnd = new java.util.Random();
 184 
 185         public static void verify(BinaryLongMethod method) {
 186             for (int i = 0; i < 50000; ++i) {
 187                 long rnd1 = rnd.nextLong(), rnd2 = rnd.nextLong();
 188                 Verify.verifyBinary(rnd1, rnd2, method);
 189                 Verify.verifyBinary(rnd1, rnd2 + 1, method);
 190                 Verify.verifyBinary(rnd1 + 1, rnd2, method);
 191                 Verify.verifyBinary(rnd1 - 1, rnd2, method);
 192                 Verify.verifyBinary(rnd1, rnd2 - 1, method);
 193                 Verify.verifyBinary(rnd1 + Long.MAX_VALUE - rnd2, rnd2 + 1, method);
 194                 Verify.verifyBinary(values[0], values[2], method);
 195                 Verify.verifyBinary(values[1], values[2], method);
 196                 Verify.verifyBinary(values[3], 74L, method);
 197             }
 198         }
 199     }
 200 
 201     public static class LoopDependentTest {
 202         public static java.util.Random rnd = new java.util.Random();
 203 
 204         public static void verify(BinaryMethod method) {
 205             int rnd1 = rnd.nextInt(), rnd2 = rnd.nextInt();
 206             runTest(rnd1, rnd2, method);
 207         }
 208 
 209         private static void runTest(int rnd1, int rnd2, BinaryMethod method) {
 210             for (int i = 0; i < 50000; ++i) {
 211                 Verify.verifyBinary(rnd1 + i, rnd2 + i, method);
 212                 Verify.verifyBinary(rnd1 + i, rnd2 + (i & 0xff), method);
 213                 Verify.verifyBinary(rnd1 - i, rnd2 - (i & 0xff), method);
 214                 Verify.verifyBinary(rnd1 + i + 1, rnd2 + i + 2, method);
 215                 Verify.verifyBinary(rnd1 + i * 2, rnd2 + i, method);
 216             }
 217         }
 218     }
 219 
 220     public static class ConstantTest {
 221         public static void verify(BinaryMethod method) {
 222             for (int i = 0; i < 50000; ++i) {


   1 /*
   2  * Copyright (c) 2013, 2014, 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  */
  23 
  24 import com.oracle.java.testlibrary.Utils;
  25 import java.util.Random;
  26 
  27 /**
  28  * The class depends on Utils class from testlibrary package.
  29  * It uses factory method that obtains random generator.
  30  */
  31 public class Verify {
  32     public static String throwWord(boolean threw) {
  33         return (threw ? "threw" : "didn't throw");
  34     }
  35 
  36     public static void verifyResult(UnaryMethod method, int result1, int result2, boolean exception1, boolean exception2, int value) {
  37         if (exception1 != exception2) {
  38             throw new RuntimeException("Intrinsic version [" + method.name() + "]" + throwWord(exception1) + " exception, NonIntrinsic version" + throwWord(exception2) + " for: " + value);
  39         }
  40         if (result1 != result2) {
  41             throw new RuntimeException("Intrinsic version [" + method.name() + "] returned: " + result1 + " while NonIntrinsic version returned: " + result2);
  42         }
  43     }
  44 
  45     public static void verifyResult(UnaryLongMethod method, long result1, long result2, boolean exception1, boolean exception2, long value) {
  46         if (exception1 != exception2) {
  47             throw new RuntimeException("Intrinsic version [" + method.name() + "]" + throwWord(exception1) + " exception, NonIntrinsic version" + throwWord(exception2) + " for: " + value);
  48         }
  49         if (result1 != result2) {
  50             throw new RuntimeException("Intrinsic version [" + method.name() + "] returned: " + result1 + " while NonIntrinsic version returned: " + result2);


 124 
 125     public static void verifyBinary(long a, long b, BinaryLongMethod method) {
 126         boolean exception1 = false, exception2 = false;
 127         long result1 = 0, result2 = 0;
 128         try {
 129             result1 = method.checkMethod(a, b);
 130         } catch (ArithmeticException e) {
 131             exception1 = true;
 132         }
 133         try {
 134             result2 = method.safeMethod(a, b);
 135         } catch (ArithmeticException e) {
 136             exception2 = true;
 137         }
 138 
 139         verifyResult(method, result1, result2, exception1, exception2, a, b);
 140     }
 141 
 142 
 143     public static class LoadTest {
 144         public static Random rnd = Utils.getRandomInstance();
 145         public static int[] values = new int[256];
 146 
 147         public static void init() {
 148             for (int i = 0; i < values.length; ++i) {
 149                 values[i] = rnd.nextInt();
 150             }
 151         }
 152 
 153         public static void verify(BinaryMethod method) {
 154             for (int i = 0; i < 50000; ++i) {
 155                 Verify.verifyBinary(values[i & 255], values[i & 255] - i, method);
 156                 Verify.verifyBinary(values[i & 255] + i, values[i & 255] - i, method);
 157                 Verify.verifyBinary(values[i & 255], values[i & 255], method);
 158                 if ((i & 1) == 1 && i > 5) {
 159                     Verify.verifyBinary(values[i & 255] + i, values[i & 255] - i, method);
 160                 } else {
 161                     Verify.verifyBinary(values[i & 255] - i, values[i & 255] + i, method);
 162                 }
 163                 Verify.verifyBinary(values[i & 255], values[(i + 1) & 255], method);
 164             }
 165         }
 166     }
 167 
 168     public static class NonConstantTest {
 169         public static Random rnd = Utils.getRandomInstance();
 170         public static int[] values = new int[] { Integer.MAX_VALUE, Integer.MIN_VALUE };
 171 
 172         public static void verify(BinaryMethod method) {
 173             for (int i = 0; i < 50000; ++i) {
 174                 int rnd1 = rnd.nextInt(), rnd2 = rnd.nextInt();
 175                 Verify.verifyBinary(rnd1, rnd2, method);
 176                 Verify.verifyBinary(rnd1, rnd2 + 1, method);
 177                 Verify.verifyBinary(rnd1 + 1, rnd2, method);
 178                 Verify.verifyBinary(rnd1 - 1, rnd2, method);
 179                 Verify.verifyBinary(rnd1, rnd2 - 1, method);
 180                 Verify.verifyBinary(0, values[0], method);
 181                 Verify.verifyBinary(values[0], 0, method);
 182                 Verify.verifyBinary(0, values[1], method);
 183                 Verify.verifyBinary(values[1], 0, method);
 184             }
 185         }
 186     }
 187 
 188     public static class NonConstantLongTest {
 189         public static long[] values = { Long.MIN_VALUE, Long.MAX_VALUE, 0, Long.MAX_VALUE - 1831 };
 190         public static Random rnd = Utils.getRandomInstance();
 191 
 192         public static void verify(BinaryLongMethod method) {
 193             for (int i = 0; i < 50000; ++i) {
 194                 long rnd1 = rnd.nextLong(), rnd2 = rnd.nextLong();
 195                 Verify.verifyBinary(rnd1, rnd2, method);
 196                 Verify.verifyBinary(rnd1, rnd2 + 1, method);
 197                 Verify.verifyBinary(rnd1 + 1, rnd2, method);
 198                 Verify.verifyBinary(rnd1 - 1, rnd2, method);
 199                 Verify.verifyBinary(rnd1, rnd2 - 1, method);
 200                 Verify.verifyBinary(rnd1 + Long.MAX_VALUE - rnd2, rnd2 + 1, method);
 201                 Verify.verifyBinary(values[0], values[2], method);
 202                 Verify.verifyBinary(values[1], values[2], method);
 203                 Verify.verifyBinary(values[3], 74L, method);
 204             }
 205         }
 206     }
 207 
 208     public static class LoopDependentTest {
 209         public static Random rnd = Utils.getRandomInstance();
 210 
 211         public static void verify(BinaryMethod method) {
 212             int rnd1 = rnd.nextInt(), rnd2 = rnd.nextInt();
 213             runTest(rnd1, rnd2, method);
 214         }
 215 
 216         private static void runTest(int rnd1, int rnd2, BinaryMethod method) {
 217             for (int i = 0; i < 50000; ++i) {
 218                 Verify.verifyBinary(rnd1 + i, rnd2 + i, method);
 219                 Verify.verifyBinary(rnd1 + i, rnd2 + (i & 0xff), method);
 220                 Verify.verifyBinary(rnd1 - i, rnd2 - (i & 0xff), method);
 221                 Verify.verifyBinary(rnd1 + i + 1, rnd2 + i + 2, method);
 222                 Verify.verifyBinary(rnd1 + i * 2, rnd2 + i, method);
 223             }
 224         }
 225     }
 226 
 227     public static class ConstantTest {
 228         public static void verify(BinaryMethod method) {
 229             for (int i = 0; i < 50000; ++i) {


test/compiler/intrinsics/mathexact/Verify.java
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File