|
1 /* |
|
2 * Copyright (c) 2010, 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 7006109 |
|
27 * @summary Add test library to simplify the task of writing automated type-system tests |
|
28 * @author mcimadamore |
|
29 * @library . |
|
30 * @run main BoxingConversionTest |
|
31 */ |
|
32 |
|
33 import com.sun.tools.javac.code.Type; |
|
34 import com.sun.tools.javac.code.Type.*; |
|
35 import com.sun.tools.javac.code.Symbol.*; |
|
36 import java.lang.reflect.Array; |
|
37 import java.util.EnumSet; |
|
38 |
|
39 /** |
|
40 * Check invariants in assignment/method conversion involving boxing conversions |
|
41 */ |
|
42 public class BoxingConversionTest extends TypeHarness { |
|
43 |
|
44 Type[] types1; |
|
45 Type[] types2; |
|
46 Type[] types3; |
|
47 |
|
48 enum Result { |
|
49 OK_BOTH(true), |
|
50 FAIL_BOTH(false), |
|
51 OK_ASSIGN_ONLY(true); |
|
52 |
|
53 boolean value; |
|
54 |
|
55 Result(boolean value) { |
|
56 this.value = value; |
|
57 } |
|
58 } |
|
59 |
|
60 enum ConversionKind { |
|
61 ASSIGNMENT_CONVERSION(EnumSet.of(Result.OK_BOTH, Result.OK_ASSIGN_ONLY)) { |
|
62 @Override |
|
63 void check(TypeHarness harness, Type from, Type to, Result expected) { |
|
64 harness.assertAssignable(from, to, resSet.contains(expected)); |
|
65 } |
|
66 }, |
|
67 METHOD_CONVERSION(EnumSet.of(Result.OK_BOTH)) { |
|
68 @Override |
|
69 void check(TypeHarness harness, Type from, Type to, Result expected) { |
|
70 harness.assertConvertible(from, to, resSet.contains(expected)); |
|
71 } |
|
72 }; |
|
73 |
|
74 EnumSet<Result> resSet; |
|
75 |
|
76 private ConversionKind(EnumSet<Result> resSet) { |
|
77 this.resSet = resSet; |
|
78 } |
|
79 |
|
80 abstract void check(TypeHarness harness, Type from, Type to, Result expected); |
|
81 } |
|
82 |
|
83 enum TestKind { |
|
84 SIMPLE { |
|
85 @Override |
|
86 Type[] getFromTypes(BoxingConversionTest harness) { |
|
87 return harness.types1; |
|
88 } |
|
89 @Override |
|
90 Type[] getToTypes(BoxingConversionTest harness) { |
|
91 return harness.types1; |
|
92 } |
|
93 @Override |
|
94 Result[][] getResults(BoxingConversionTest harness) { |
|
95 return harness.results1; |
|
96 } |
|
97 }, |
|
98 CONSTANT_TYPES { |
|
99 @Override |
|
100 Type[] getFromTypes(BoxingConversionTest harness) { |
|
101 return harness.types2; |
|
102 } |
|
103 @Override |
|
104 Type[] getToTypes(BoxingConversionTest harness) { |
|
105 return harness.types3; |
|
106 } |
|
107 @Override |
|
108 Result[][] getResults(BoxingConversionTest harness) { |
|
109 return harness.results2; |
|
110 } |
|
111 }; |
|
112 |
|
113 abstract Type[] getFromTypes(BoxingConversionTest harness); |
|
114 abstract Type[] getToTypes(BoxingConversionTest harness); |
|
115 abstract Result[][] getResults(BoxingConversionTest harness); |
|
116 } |
|
117 |
|
118 static final Result T = Result.OK_BOTH; |
|
119 static final Result F = Result.FAIL_BOTH; |
|
120 static final Result A = Result.OK_ASSIGN_ONLY; |
|
121 |
|
122 Result[][] results1 = { |
|
123 //byte, short, int, long, float, double, char, bool, Byte, Short, Integer, Long, Float, Double, Character, Boolean |
|
124 /*byte*/ { T , T , T , T , T , T , F , F , T , F , F , F , F , F , F , F }, |
|
125 /*short*/ { F , T , T , T , T , T , F , F , F , T , F , F , F , F , F , F }, |
|
126 /*int*/ { F , F , T , T , T , T , F , F , F , F , T , F , F , F , F , F }, |
|
127 /*long*/ { F , F , F , T , T , T , F , F , F , F , F , T , F , F , F , F }, |
|
128 /*float*/ { F , F , F , F , T , T , F , F , F , F , F , F , T , F , F , F }, |
|
129 /*double*/ { F , F , F , F , F , T , F , F , F , F , F , F , F , T , F , F }, |
|
130 /*char*/ { F , F , T , T , T , T , T , F , F , F , F , F , F , F , T , F }, |
|
131 /*bool*/ { F , F , F , F , F , F , F , T , F , F , F , F , F , F , F , T }, |
|
132 /*Byte*/ { T , T , T , T , T , T , F , F , T , F , F , F , F , F , F , F }, |
|
133 /*Short*/ { F , T , T , T , T , T , F , F , F , T , F , F , F , F , F , F }, |
|
134 /*Integer*/ { F , F , T , T , T , T , F , F , F , F , T , F , F , F , F , F }, |
|
135 /*Long*/ { F , F , F , T , T , T , F , F , F , F , F , T , F , F , F , F }, |
|
136 /*Float*/ { F , F , F , F , T , T , F , F , F , F , F , F , T , F , F , F }, |
|
137 /*Double*/ { F , F , F , F , F , T , F , F , F , F , F , F , F , T , F , F }, |
|
138 /*Character*/ { F , F , T , T , T , T , T , F , F , F , F , F , F , F , T , F }, |
|
139 /*Boolean*/ { F , F , F , F , F , F , F , T , F , F , F , F , F , F , F , T }}; |
|
140 |
|
141 Result[][] results2 = { |
|
142 //Byte, Short, Integer, Long, Float, Double, Chararacter, Boolean |
|
143 /*byte*/ { T , F , F , F , F , F , F , F }, |
|
144 /*short*/ { F , T , F , F , F , F , F , F }, |
|
145 /*short1*/ { A , T , F , F , F , F , A , F }, |
|
146 /*short2*/ { F , T , F , F , F , F , A , F }, |
|
147 /*int*/ { F , F , T , F , F , F , F , F }, |
|
148 /*int1*/ { A , A , T , F , F , F , A , F }, |
|
149 /*int2*/ { F , A , T , F , F , F , A , F }, |
|
150 /*int4*/ { F , F , T , F , F , F , F , F }, |
|
151 /*long*/ { F , F , F , T , F , F , F , F }, |
|
152 /*long1*/ { F , F , F , T , F , F , F , F }, |
|
153 /*long2*/ { F , F , F , T , F , F , F , F }, |
|
154 /*long4*/ { F , F , F , T , F , F , F , F }, |
|
155 /*long8*/ { F , F , F , T , F , F , F , F }, |
|
156 /*float*/ { F , F , F , F , T , F , F , F }, |
|
157 /*float1*/ { F , F , F , F , T , F , F , F }, |
|
158 /*float2*/ { F , F , F , F , T , F , F , F }, |
|
159 /*float4*/ { F , F , F , F , T , F , F , F }, |
|
160 /*double*/ { F , F , F , F , F , T , F , F }, |
|
161 /*double1*/ { F , F , F , F , F , T , F , F }, |
|
162 /*double2*/ { F , F , F , F , F , T , F , F }, |
|
163 /*double4*/ { F , F , F , F , F , T , F , F }, |
|
164 /*double8*/ { F , F , F , F , F , T , F , F }, |
|
165 /*char*/ { F , F , F , F , F , F , T , F }, |
|
166 /*char1*/ { A , A , F , F , F , F , T , F }, |
|
167 /*char2*/ { F , A , F , F , F , F , T , F }, |
|
168 /*bool*/ { F , F , F , F , F , F , F , T }}; |
|
169 |
|
170 BoxingConversionTest() { |
|
171 Type[] primitiveTypes = new Type[] { |
|
172 predef.byteType, |
|
173 predef.shortType, |
|
174 predef.intType, |
|
175 predef.longType, |
|
176 predef.floatType, |
|
177 predef.doubleType, |
|
178 predef.charType, |
|
179 predef.booleanType }; |
|
180 |
|
181 Type[] boxedTypes = new Type[primitiveTypes.length]; |
|
182 for (int i = 0 ; i < primitiveTypes.length ; i++) { |
|
183 boxedTypes[i] = box(primitiveTypes[i]); |
|
184 } |
|
185 |
|
186 types1 = join(Type.class, primitiveTypes, boxedTypes); |
|
187 |
|
188 types2 = new Type[] { |
|
189 predef.byteType, |
|
190 predef.shortType, |
|
191 fac.Constant((short)0x0001), |
|
192 fac.Constant((short)0x0100), |
|
193 predef.intType, |
|
194 fac.Constant((int)0x0000_0001), |
|
195 fac.Constant((int)0x0000_0100), |
|
196 fac.Constant((int)0x0001_0000), |
|
197 predef.longType, |
|
198 fac.Constant((long)0x0000_0000_0000_0001L), |
|
199 fac.Constant((long)0x0000_0000_0000_0100L), |
|
200 fac.Constant((long)0x0000_0000_0001_0000L), |
|
201 fac.Constant((long)0x0001_0000_0000_0000L), |
|
202 predef.floatType, |
|
203 fac.Constant((float)0x0000_0001), |
|
204 fac.Constant((float)0x0000_0100), |
|
205 fac.Constant((float)0x0001_0000), |
|
206 predef.doubleType, |
|
207 fac.Constant((double)0x0000_0000_0000_0001L), |
|
208 fac.Constant((double)0x0000_0000_0000_0100L), |
|
209 fac.Constant((double)0x0000_0000_0001_0000L), |
|
210 fac.Constant((double)0x0001_0000_0000_0000L), |
|
211 predef.charType, |
|
212 fac.Constant((char)0x0001), |
|
213 fac.Constant((char)0x0100), |
|
214 predef.booleanType |
|
215 }; |
|
216 |
|
217 types3 = boxedTypes; |
|
218 } |
|
219 |
|
220 void testConversion(ConversionKind convKind, TestKind testKind) { |
|
221 Type[] rows = testKind.getFromTypes(this); |
|
222 Type[] cols = testKind.getToTypes(this); |
|
223 for (int i = 0; i < rows.length ; i++) { |
|
224 for (int j = 0; j < cols.length ; j++) { |
|
225 convKind.check(this, rows[i], cols[j], testKind.getResults(this)[i][j]); |
|
226 } |
|
227 } |
|
228 } |
|
229 |
|
230 @SuppressWarnings("unchecked") |
|
231 <T> T[] join(Class<T> type, T[]... args) { |
|
232 int totalLength = 0; |
|
233 for (T[] arr : args) { |
|
234 totalLength += arr.length; |
|
235 } |
|
236 T[] new_arr = (T[])Array.newInstance(type, totalLength); |
|
237 int idx = 0; |
|
238 for (T[] arr : args) { |
|
239 System.arraycopy(arr, 0, new_arr, idx, arr.length); |
|
240 idx += arr.length; |
|
241 } |
|
242 return new_arr; |
|
243 } |
|
244 |
|
245 public static void main(String[] args) { |
|
246 BoxingConversionTest harness = new BoxingConversionTest(); |
|
247 for (ConversionKind convKind : ConversionKind.values()) { |
|
248 for (TestKind testKind : TestKind.values()) { |
|
249 harness.testConversion(convKind, testKind); |
|
250 } |
|
251 } |
|
252 } |
|
253 } |