blob: 973e9e9f12b2df2e2b324c73f89074c43b2fb4fc [file] [log] [blame]
buzbee43a36422011-09-14 14:00:13 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
3class ExceptionTest {
4
5 public int ifoo;
6
7 /* Test requires visual inspection of object code to verify */
8 int noThrow(ExceptionTest nonNullA,
9 ExceptionTest nonNullB,
10 ExceptionTest nonNullC) {
11
12 // "this" check should be eliminated on both IGET/IPUT
13 ifoo++;
14
15 // "this" check should be eliminated on both IGET/IPUT
16 if (ifoo != 321) {
17 // Check not eliminated
18 nonNullA.ifoo = 12;
19 // Check not eliminated
20 nonNullB.ifoo = 21;
21 } else {
22 // Check not eliminated
23 nonNullA.ifoo = 12;
24 }
25
26 // Check eliminated
27 nonNullA.ifoo = 13;
28
29 // Check not eliminated
30 nonNullB.ifoo = 21;
31
32 nonNullC = nonNullB;
33
34 // Check eliminated
35 nonNullC.ifoo = 32;
36
37 // All null checks eliminated
38 return ifoo + nonNullA.ifoo + nonNullB.ifoo + nonNullC.ifoo;
39 }
40
41 /* Test to ensure we don't remove necessary null checks */
42 int checkThrow(ExceptionTest nonNullA,
43 ExceptionTest nonNullB,
44 ExceptionTest nonNullC,
45 ExceptionTest nullA,
46 ExceptionTest nullB,
47 ExceptionTest nullC) {
48
49 // "this" check should be eliminated on both IGET/IPUT
50 ifoo++;
51
52 try {
53 nullA.ifoo = 12;
54 // Should not be reached
55 return -1;
56 } catch (NullPointerException npe) {
57 ifoo++;
58 }
59 try {
60 nullB.ifoo = 13;
61 // Should not be reached
62 return -2;
63 } catch (NullPointerException npe) {
64 ifoo++;
65 }
66 try {
67 nullC.ifoo = 14;
68 // Should not be reached
69 return -3;
70 } catch (NullPointerException npe) {
71 ifoo++;
72 }
73
74 // "this" check should be eliminated
75 if (ifoo != 321) {
76 // Check not eliminated
77 nonNullA.ifoo = 12;
78 // Check not eliminated
79 nonNullB.ifoo = 21;
80 // Should throw here
81 try {
82 nullA.ifoo = 11;
83 return -4;
84 } catch (NullPointerException npe) {
85 }
86 } else {
87 // Check not eliminated
88 nonNullA.ifoo = 12;
89 // Should throw here
90 try {
91 nullA.ifoo = 11;
92 return -5;
93 } catch (NullPointerException npe) {
94 }
95 }
96
97 // Check not eliminated
98 nonNullA.ifoo = 13;
99
100 // Check not eliminated
101 nonNullB.ifoo = 21;
102
103 nonNullC = nonNullB;
104
105 // Check eliminated
106 nonNullC.ifoo = 32;
107
108 // Should throw here
109 try {
110 nullA.ifoo = 13;
111 return -6;
112 } catch (NullPointerException npe) {
113 }
114
115 return ifoo + nonNullA.ifoo + nonNullB.ifoo + nonNullC.ifoo;
116 }
117
118
119 static int nullCheckTestNoThrow(int x) {
120 ExceptionTest base = new ExceptionTest();
121 ExceptionTest a = new ExceptionTest();
122 ExceptionTest b = new ExceptionTest();
123 ExceptionTest c = new ExceptionTest();
124 base.ifoo = x;
125 return base.noThrow(a,b,c);
126 }
127
128 static int nullCheckTestThrow(int x) {
129 ExceptionTest base = new ExceptionTest();
130 ExceptionTest a = new ExceptionTest();
131 ExceptionTest b = new ExceptionTest();
132 ExceptionTest c = new ExceptionTest();
133 ExceptionTest d = null;
134 ExceptionTest e = null;
135 ExceptionTest f = null;
136 base.ifoo = x;
137 return base.checkThrow(a,b,c,d,e,f);
138 }
139
140
141 public static void main(String[] args) {
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700142 boolean failure = false;
buzbee43a36422011-09-14 14:00:13 -0700143 int res;
144
145 res = nullCheckTestNoThrow(1976);
146 if (res == 2054) {
147 System.out.println("nullCheckTestNoThrow PASSED");
148 } else {
149 System.out.println("nullCheckTestNoThrow FAILED: " + res);
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700150 failure = true;
buzbee43a36422011-09-14 14:00:13 -0700151 }
152
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700153if (false) { // TODO: enable this test when passing
buzbee43a36422011-09-14 14:00:13 -0700154 res = nullCheckTestThrow(1976);
155 if (res == 2057) {
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700156 System.out.println("nullCheckTestThrow PASSED");
buzbee43a36422011-09-14 14:00:13 -0700157 } else {
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700158 System.out.println("nullCheckTestThrow FAILED: " + res);
159 failure = true;
buzbee43a36422011-09-14 14:00:13 -0700160 }
Brian Carlstrombbf1e412011-09-18 14:14:51 -0700161}
162 System.exit(failure ? 1 : 0);
buzbee43a36422011-09-14 14:00:13 -0700163 }
164}