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