blob: 3c74275831a1c21173988a872ab8b4672310bb7a [file] [log] [blame]
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001/*
2 * Copyright (C) 2014 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
17import java.lang.reflect.InvocationTargetException;
18import java.lang.reflect.Method;
19
20// Note that $opt$ is a marker for the optimizing compiler to ensure
21// it does compile the method.
22
23public class Main extends TestCase {
24 public static void main(String[] args) throws Exception {
25 $opt$TestAllocations();
26 $opt$TestWithInitializations();
27 testSmaliFilledNewArray();
28 testSmaliFillArrayData();
29 testSmaliVerifyError();
30 }
31
32 static void $opt$TestAllocations() {
33 float[] a = new float[1];
34 assertEquals(1, a.length);
35
36 double[] b = new double[2];
37 assertEquals(2, b.length);
38
39 long[] c = new long[3];
40 assertEquals(3, c.length);
41
42 int[] d = new int[4];
43 assertEquals(4, d.length);
44
45 short[] e = new short[5];
46 assertEquals(5, e.length);
47
48 char[] f = new char[6];
49 assertEquals(6, f.length);
50
51 byte[] g = new byte[7];
52 assertEquals(7, g.length);
53
54 boolean[] h = new boolean[8];
55 assertEquals(8, h.length);
56
57 Object[] i = new Object[9];
58 assertEquals(9, i.length);
59 }
60
61 static void $opt$TestWithInitializations() {
62 float[] a = { 1.2f };
63 assertEquals(1, a.length);
64 assertEquals(1.2f, a[0]);
65
66 double[] b = { 4.3, 1.2 };
67 assertEquals(2, b.length);
68 assertEquals(4.3, b[0]);
69 assertEquals(1.2, b[1]);
70
71 long[] c = { 4L, 5L };
72 assertEquals(2, c.length);
73 assertEquals(4L, c[0]);
74 assertEquals(5L, c[1]);
75
76 int[] d = {1, 2, 3};
77 assertEquals(3, d.length);
78 assertEquals(1, d[0]);
79 assertEquals(2, d[1]);
80 assertEquals(3, d[2]);
81
82 short[] e = {4, 5, 6};
83 assertEquals(3, e.length);
84 assertEquals(4, e[0]);
85 assertEquals(5, e[1]);
86 assertEquals(6, e[2]);
87
88 char[] f = {'a', 'b'};
89 assertEquals(2, f.length);
90 assertEquals('a', f[0]);
91 assertEquals('b', f[1]);
92
93 byte[] g = {7, 8, 9};
94 assertEquals(3, g.length);
95 assertEquals(7, g[0]);
96 assertEquals(8, g[1]);
97 assertEquals(9, g[2]);
98
99 boolean[] h = {true, false};
100 assertEquals(2, h.length);
101 assertEquals(true, h[0]);
102 assertEquals(false, h[1]);
103
104 Object obj1 = new Object();
105 Object obj2 = new Object();
106 Object[] i = {obj1, obj2};
107 assertEquals(2, i.length);
108 assertEquals(obj1, i[0]);
109 assertEquals(obj2, i[1]);
110 }
111
112 public static void testSmaliFilledNewArray() throws Exception {
113 Class<?> c = Class.forName("FilledNewArray");
114
115 {
116 Method m = c.getMethod("newInt", Integer.TYPE, Integer.TYPE, Integer.TYPE);
117 Object[] args = {new Integer(1), new Integer(2), new Integer(3)};
118 int[] result = (int[])m.invoke(null, args);
119 assertEquals(3, result.length);
120 assertEquals(1, result[0]);
121 assertEquals(2, result[1]);
122 assertEquals(3, result[2]);
123 }
124
125 {
126 Method m = c.getMethod("newRef", Object.class, Object.class);
127 Object[] args = {new Integer(1), new Integer(2)};
128 Object[] result = (Object[])m.invoke(null, args);
129 assertEquals(2, result.length);
130 assertEquals(args[0], result[0]);
131 assertEquals(args[1], result[1]);
132 }
133
134 {
135 Method m = c.getMethod("newArray", int[].class, int[].class);
136 Object[] args = {new int[0], new int[1]};
137 Object[] result = (Object[])m.invoke(null, args);
138 assertEquals(2, result.length);
139 assertEquals(args[0], result[0]);
140 assertEquals(args[1], result[1]);
141 }
142
143 {
144 Method m = c.getMethod("newIntRange", Integer.TYPE, Integer.TYPE, Integer.TYPE);
145 Object[] args = {new Integer(1), new Integer(2), new Integer(3)};
146 int[] result = (int[])m.invoke(null, args);
147 assertEquals(3, result.length);
148 assertEquals(1, result[0]);
149 assertEquals(2, result[1]);
150 assertEquals(3, result[2]);
151 }
152
153 {
154 Method m = c.getMethod("newRefRange", Object.class, Object.class);
155 Object[] args = {new Integer(1), new Integer(2)};
156 Object[] result = (Object[])m.invoke(null, args);
157 assertEquals(2, result.length);
158 assertEquals(args[0], result[0]);
159 assertEquals(args[1], result[1]);
160 }
161
162 {
163 Method m = c.getMethod("newArrayRange", int[].class, int[].class);
164 Object[] args = {new int[0], new int[1]};
165 Object[] result = (Object[])m.invoke(null, args);
166 assertEquals(2, result.length);
167 assertEquals(args[0], result[0]);
168 assertEquals(args[1], result[1]);
169 }
170 }
171
172 public static void testSmaliVerifyError() throws Exception {
173 Error error = null;
174 // Ensure the elements in filled-new-array must be assignable
175 // to the array component type.
176 try {
177 Class.forName("FilledNewArrayVerifyError");
178 } catch (VerifyError e) {
179 error = e;
180 }
181 assertNotNull(error);
182 }
183
184 public static void testSmaliFillArrayData() throws Exception {
185 Class<?> c = Class.forName("FillArrayData");
186 {
187 Method m = c.getMethod("intArray", int[].class);
188 int[] array = new int[7];
189 Object[] args = { array };
190 m.invoke(null, args);
191 assertEquals(7, array.length);
192 assertEquals(1, array[0]);
193 assertEquals(2, array[1]);
194 assertEquals(3, array[2]);
195 assertEquals(4, array[3]);
196 assertEquals(5, array[4]);
197 assertEquals(0, array[5]);
198 assertEquals(0, array[6]);
199
200 array = new int[2];
201 args[0] = array;
202 Throwable exception = null;
203 try {
204 m.invoke(null, args);
205 } catch (InvocationTargetException e) {
206 exception = e.getCause();
207 assertTrue(exception instanceof IndexOutOfBoundsException);
208 }
209 assertNotNull(exception);
210 exception = null;
211 // Test that nothing has been written to the array.
212 assertEquals(0, array[0]);
213 assertEquals(0, array[1]);
214
215 args[0] = null;
216 try {
217 m.invoke(null, args);
218 } catch (InvocationTargetException e) {
219 exception = e.getCause();
220 assertTrue(exception instanceof NullPointerException);
221 }
222 assertNotNull(exception);
223 }
224
225 {
226 Method m = c.getMethod("shortArray", short[].class);
227 short[] array = new short[7];
228 Object[] args = { array };
229 m.invoke(null, args);
230 assertEquals(7, array.length);
231 assertEquals(1, array[0]);
232 assertEquals(2, array[1]);
233 assertEquals(3, array[2]);
234 assertEquals(4, array[3]);
235 assertEquals(5, array[4]);
236 assertEquals(0, array[5]);
237 assertEquals(0, array[6]);
238
239 array = new short[2];
240 args[0] = array;
241 Throwable exception = null;
242 try {
243 m.invoke(null, args);
244 } catch (InvocationTargetException e) {
245 exception = e.getCause();
246 assertTrue(exception instanceof IndexOutOfBoundsException);
247 }
248 assertNotNull(exception);
249 exception = null;
250 // Test that nothing has been written to the array.
251 assertEquals(0, array[0]);
252 assertEquals(0, array[1]);
253
254 args[0] = null;
255 try {
256 m.invoke(null, args);
257 } catch (InvocationTargetException e) {
258 exception = e.getCause();
259 assertTrue(exception instanceof NullPointerException);
260 }
261 assertNotNull(exception);
262 }
263
264 {
265 Method m = c.getMethod("longArray", long[].class);
266 long[] array = new long[7];
267 Object[] args = { array };
268 m.invoke(null, args);
269 assertEquals(7, array.length);
270 assertEquals(1L, array[0]);
271 assertEquals(2L, array[1]);
272 assertEquals(3L, array[2]);
273 assertEquals(4L, array[3]);
274 assertEquals(5L, array[4]);
275 assertEquals(0L, array[5]);
276 assertEquals(0L, array[6]);
277
278 array = new long[2];
279 args[0] = array;
280 Throwable exception = null;
281 try {
282 m.invoke(null, args);
283 } catch (InvocationTargetException e) {
284 exception = e.getCause();
285 assertTrue(exception instanceof IndexOutOfBoundsException);
286 }
287 assertNotNull(exception);
288 exception = null;
289 // Test that nothing has been written to the array.
290 assertEquals(0, array[0]);
291 assertEquals(0, array[1]);
292
293 args[0] = null;
294 try {
295 m.invoke(null, args);
296 } catch (InvocationTargetException e) {
297 exception = e.getCause();
298 assertTrue(exception instanceof NullPointerException);
299 }
300 assertNotNull(exception);
301 }
302
303 {
304 Method m = c.getMethod("charArray", char[].class);
305 char[] array = new char[7];
306 Object[] args = { array };
307 m.invoke(null, args);
308 assertEquals(7, array.length);
309 assertEquals(1, array[0]);
310 assertEquals(2, array[1]);
311 assertEquals(3, array[2]);
312 assertEquals(4, array[3]);
313 assertEquals(5, array[4]);
314 assertEquals(0, array[5]);
315 assertEquals(0, array[6]);
316
317 array = new char[2];
318 args[0] = array;
319 Throwable exception = null;
320 try {
321 m.invoke(null, args);
322 } catch (InvocationTargetException e) {
323 exception = e.getCause();
324 assertTrue(exception instanceof IndexOutOfBoundsException);
325 }
326 assertNotNull(exception);
327 exception = null;
328 // Test that nothing has been written to the array.
329 assertEquals(0, array[0]);
330 assertEquals(0, array[1]);
331
332 args[0] = null;
333 try {
334 m.invoke(null, args);
335 } catch (InvocationTargetException e) {
336 exception = e.getCause();
337 assertTrue(exception instanceof NullPointerException);
338 }
339 assertNotNull(exception);
340 }
341
342 {
343 Method m = c.getMethod("byteArray", byte[].class);
344 byte[] array = new byte[7];
345 Object[] args = { array };
346 m.invoke(null, args);
347 assertEquals(7, array.length);
348 assertEquals(1, array[0]);
349 assertEquals(2, array[1]);
350 assertEquals(3, array[2]);
351 assertEquals(4, array[3]);
352 assertEquals(5, array[4]);
353 assertEquals(0, array[5]);
354 assertEquals(0, array[6]);
355
356 array = new byte[2];
357 args[0] = array;
358 Throwable exception = null;
359 try {
360 m.invoke(null, args);
361 } catch (InvocationTargetException e) {
362 exception = e.getCause();
363 assertTrue(exception instanceof IndexOutOfBoundsException);
364 }
365 assertNotNull(exception);
366 exception = null;
367 // Test that nothing has been written to the array.
368 assertEquals(0, array[0]);
369 assertEquals(0, array[1]);
370
371 args[0] = null;
372 try {
373 m.invoke(null, args);
374 } catch (InvocationTargetException e) {
375 exception = e.getCause();
376 assertTrue(exception instanceof NullPointerException);
377 }
378 assertNotNull(exception);
379 }
380
381 {
382 Method m = c.getMethod("booleanArray", boolean[].class);
383 boolean[] array = new boolean[5];
384 Object[] args = { array };
385 m.invoke(null, args);
386 assertEquals(5, array.length);
387 assertEquals(false, array[0]);
388 assertEquals(true, array[1]);
389 assertEquals(true, array[2]);
390 assertEquals(false, array[3]);
391 assertEquals(false, array[4]);
392
393 array = new boolean[2];
394 args[0] = array;
395 Throwable exception = null;
396 try {
397 m.invoke(null, args);
398 } catch (InvocationTargetException e) {
399 exception = e.getCause();
400 assertTrue(exception instanceof IndexOutOfBoundsException);
401 }
402 assertNotNull(exception);
403 exception = null;
404 // Test that nothing has been written to the array.
405 assertEquals(false, array[0]);
406 assertEquals(false, array[1]);
407
408 args[0] = null;
409 try {
410 m.invoke(null, args);
411 } catch (InvocationTargetException e) {
412 exception = e.getCause();
413 assertTrue(exception instanceof NullPointerException);
414 }
415 assertNotNull(exception);
416 }
417 }
418}