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 import java.lang.reflect.Method;
18 import java.lang.reflect.InvocationTargetException;
19 
20 public class Main {
21   // Workaround for b/18051191.
22   class Inner {}
23 
assertIsInterpreted()24   public static native void assertIsInterpreted();
25 
assertEqual(String expected, String actual)26   private static void assertEqual(String expected, String actual) {
27     if (!expected.equals(actual)) {
28       throw new Error("Assertion failed: " + expected + " != " + actual);
29     }
30   }
31 
main(String[] args)32   public static void main(String[] args) throws Throwable {
33     System.loadLibrary(args[0]);
34     Class<?> c = Class.forName("TestCase");
35     String testString = "Hello world";
36     byte[] testData = testString.getBytes("UTF8");
37 
38     {
39       Method m = c.getMethod("vregAliasing", byte[].class);
40       String result = (String) m.invoke(null, new Object[] { testData });
41       assertEqual(testString, result);
42     }
43 
44     {
45       c.getMethod("compareNewInstance").invoke(null, (Object[]) null);
46     }
47 
48     {
49       Method m = c.getMethod("deoptimizeNewInstance", int[].class, byte[].class);
50       try {
51         m.invoke(null, new Object[] { new int[] { 1, 2, 3 }, testData });
52       } catch (InvocationTargetException ex) {
53         if (ex.getCause() instanceof ArrayIndexOutOfBoundsException) {
54           // Expected.
55         } else {
56           throw ex.getCause();
57         }
58       }
59     }
60 
61     {
62       Method m = c.getMethod("removeNewInstance", byte[].class);
63       String result = (String) m.invoke(null, new Object[] { testData });
64       assertEqual(testString, result);
65     }
66 
67     {
68       Method m = c.getMethod("thisNotNewInstance1", byte[].class, boolean.class);
69       String result = (String) m.invoke(null, new Object[] { testData, true });
70       assertEqual(testString, result);
71       result = (String) m.invoke(null, new Object[] { testData, false });
72       assertEqual(testString, result);
73     }
74     {
75       Method m = c.getMethod("thisNotNewInstance2", byte[].class, boolean.class);
76       String result = (String) m.invoke(null, new Object[] { testData, true });
77       assertEqual(testString, result);
78       result = (String) m.invoke(null, new Object[] { testData, false });
79       assertEqual(testString, result);
80     }
81   }
82 }
83