Main.java revision 42249c3602c3d0243396ee3627ffb5906aa77c1e
1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//
18// Test on correctness in situations where slow paths may be shared
19// (actual sharing may vary between different code generators).
20//
21//
22public class Main {
23
24  // A method with two loops that can be optimized with dynamic BCE,
25  // resulting in a two times a deopt on null, a deopt on lower OOB,
26  // and a deopt on upper OOB.
27  private static void init(int[] x, int [] y, int l1, int h1, int l2, int h2) {
28    for (int i = l1; i < h1; i++) {
29      x[i] = i;
30    }
31    for (int i = l2; i < h2; i++) {
32      y[i] = i;
33    }
34  }
35
36  // Test that each of the six possible exceptions situations for init()
37  // are correctly handled by the deopt instructions.
38  public static void main(String[] args) {
39    int[] x = new int[100];
40    int[] y = new int[100];
41    int z;
42
43    // All is well.
44    z = 0;
45    reset(x, y);
46    try {
47      init(x, y, 0, 100, 0, 100);
48    } catch (Exception e) {
49      z = 1;
50    }
51    expectEquals(z, 0);
52    for (int i = 0; i < 100; i++) {
53      expectEquals(x[i], i);
54      expectEquals(y[i], i);
55    }
56
57    // Null deopt on x.
58    z = 0;
59    reset(x, y);
60    try {
61      init(null, y, 0, 100, 0, 100);
62    } catch (NullPointerException e) {
63      z = 1;
64    }
65    expectEquals(z, 1);
66    for (int i = 0; i < 100; i++) {
67      expectEquals(x[i], 0);
68      expectEquals(y[i], 0);
69    }
70
71    // Lower out-of-bounds on x.
72    z = 0;
73    reset(x, y);
74    try {
75      init(x, y, -1, 100, 0, 100);
76    } catch (ArrayIndexOutOfBoundsException e) {
77      z = 1;
78    }
79    expectEquals(z, 1);
80    for (int i = 0; i < 100; i++) {
81      expectEquals(x[i], 0);
82      expectEquals(y[i], 0);
83    }
84
85    // Upper out-of-bounds on x.
86    z = 0;
87    reset(x, y);
88    try {
89      init(x, y, 0, 101, 0, 100);
90    } catch (ArrayIndexOutOfBoundsException e) {
91      z = 1;
92    }
93    expectEquals(z, 1);
94    for (int i = 0; i < 100; i++) {
95      expectEquals(x[i], i);
96      expectEquals(y[i], 0);
97    }
98
99    // Null deopt on y.
100    z = 0;
101    reset(x, y);
102    try {
103      init(x, null, 0, 100, 0, 100);
104    } catch (NullPointerException e) {
105      z = 1;
106    }
107    expectEquals(z, 1);
108    for (int i = 0; i < 100; i++) {
109      expectEquals(x[i], i);
110      expectEquals(y[i], 0);
111    }
112
113    // Lower out-of-bounds on y.
114    z = 0;
115    reset(x, y);
116    try {
117      init(x, y, 0, 100, -1, 100);
118    } catch (ArrayIndexOutOfBoundsException e) {
119      z = 1;
120    }
121    expectEquals(z, 1);
122    for (int i = 0; i < 100; i++) {
123      expectEquals(x[i], i);
124      expectEquals(y[i], 0);
125    }
126
127    // Upper out-of-bounds on y.
128    z = 0;
129    reset(x, y);
130    try {
131      init(x, y, 0, 100, 0, 101);
132    } catch (ArrayIndexOutOfBoundsException e) {
133      z = 1;
134    }
135    expectEquals(z, 1);
136    for (int i = 0; i < 100; i++) {
137      expectEquals(x[i], i);
138      expectEquals(y[i], i);
139    }
140
141    System.out.println("passed");
142  }
143
144  private static void reset(int[] x, int[] y) {
145    for (int i = 0; i < x.length; i++) x[i] = 0;
146    for (int i = 0; i < y.length; i++) y[i] = 0;
147  }
148
149  private static void expectEquals(int expected, int result) {
150    if (expected != result) {
151      throw new Error("Expected: " + expected + ", found: " + result);
152    }
153  }
154}
155