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 /*
  25  * @test
  26  * @bug 8026844
  27  * @summary Test repeating subtractExact
  28  * @library /testlibrary
  29  * @build com.oracle.java.testlibrary.*
  30  * @compile SubExactIRepeatTest.java Verify.java
  31  * @run main SubExactIRepeatTest
  32  *
  33  */
  34 
  35 import com.oracle.java.testlibrary.Utils;
  36 import java.util.Random;
  37 
  38 public class SubExactIRepeatTest {
  39     public static void main(String[] args) {
  40         runTest(new Verify.SubExactI());
  41     }
  42 
  43     public static int nonExact(int x, int y, Verify.BinaryMethod method) {
  44         int result = method.unchecked(x, y);
  45         result += method.unchecked(x, y);
  46         result += method.unchecked(x, y);
  47         result += method.unchecked(x, y);
  48         return result;
  49     }
  50 
  51     public static void runTest(Verify.BinaryMethod method) {
  52         Random rnd = Utils.getRandomInstance();
  53         for (int i = 0; i < 50000; ++i) {
  54             int x = Integer.MIN_VALUE + 10;
  55             int y = Integer.MAX_VALUE - 10 + rnd.nextInt(5);
  56 
  57             int c = rnd.nextInt() / 2;
  58             int d = rnd.nextInt() / 2;
  59 
  60             int a = catchingExact(x, y, method);
  61 
  62             if (a != 36) {
  63                 throw new RuntimeException("a != 36 : " + a);
  64             }
  65 
  66             int b = nonExact(c, d, method);
  67             int n = exact(c, d, method);
  68 
  69 
  70             if (n != b) {
  71                 throw new RuntimeException("n != b : " + n + " != " + b);
  72             }
  73         }
  74     }
  75 
  76     public static int exact(int x, int y, Verify.BinaryMethod method) {
  77         int result = 0;
  78         result += method.checkMethod(x, y);
  79         result += method.checkMethod(x, y);
  80         result += method.checkMethod(x, y);
  81         result += method.checkMethod(x, y);
  82         return result;
  83     }
  84 
  85     public static int catchingExact(int x, int y, Verify.BinaryMethod method) {
  86         int result = 0;
  87         try {
  88             result += 5;
  89             result = method.checkMethod(x, y);
  90         } catch (ArithmeticException e) {
  91             result += 1;
  92         }
  93         try {
  94             result += 6;
  95 
  96             result += method.checkMethod(x, y);
  97         } catch (ArithmeticException e) {
  98             result += 2;
  99         }
 100         try {
 101             result += 7;
 102             result += method.checkMethod(x, y);
 103         } catch (ArithmeticException e) {
 104             result += 3;
 105         }
 106         try {
 107             result += 8;
 108             result += method.checkMethod(x, y);
 109         } catch (ArithmeticException e) {
 110             result += 4;
 111         }
 112         return result;
 113     }
 114 }