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