blob: d1c275cfef265e9fda13483b38d784c0559aae14 [file] [log] [blame]
Andreas Gampe8fda9f22014-10-03 16:15:37 -07001/*
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
Vladimir Marko920506d2014-11-18 14:47:31 +000017import java.lang.reflect.InvocationTargetException;
Andreas Gampe8fda9f22014-10-03 16:15:37 -070018import java.lang.reflect.Method;
Ian Rogers8e1f4f82014-11-05 11:07:30 -080019import java.lang.reflect.Modifier;
Andreas Gampe8fda9f22014-10-03 16:15:37 -070020import java.util.LinkedList;
21import java.util.List;
22
23/**
24 * Smali excercise.
25 */
26public class Main {
27
28 private static class TestCase {
29 public TestCase(String testName, String testClass, String testMethodName, Object[] values,
30 Throwable expectedException, Object expectedReturn) {
31 this.testName = testName;
32 this.testClass = testClass;
33 this.testMethodName = testMethodName;
34 this.values = values;
35 this.expectedException = expectedException;
36 this.expectedReturn = expectedReturn;
37 }
38
39 String testName;
40 String testClass;
41 String testMethodName;
42 Object[] values;
43 Throwable expectedException;
44 Object expectedReturn;
45 }
46
47 private List<TestCase> testCases;
48
49 public Main() {
50 // Create the test cases.
51 testCases = new LinkedList<TestCase>();
buzbee6489d222014-11-25 10:52:19 -080052 testCases.add(new TestCase("PackedSwitch", "PackedSwitch", "packedSwitch",
Sebastien Hertz270a0e12015-01-16 19:49:09 +010053 new Object[]{123}, null, 123));
Andreas Gampe8fda9f22014-10-03 16:15:37 -070054
55 testCases.add(new TestCase("b/17790197", "B17790197", "getInt", null, null, 100));
Sebastien Hertz270a0e12015-01-16 19:49:09 +010056 testCases.add(new TestCase("b/17978759", "B17978759", "test", null, new VerifyError(),
57 null));
Stephen Kyle8fe0e352014-10-16 15:02:42 +010058 testCases.add(new TestCase("FloatBadArgReg", "FloatBadArgReg", "getInt",
Sebastien Hertz270a0e12015-01-16 19:49:09 +010059 new Object[]{100}, null, 100));
nikolay serdjukd24c9342014-11-10 16:53:27 +070060 testCases.add(new TestCase("negLong", "negLong", "negLong", null, null, 122142L));
Vladimir Marko7a7c1db2014-11-17 15:13:34 +000061 testCases.add(new TestCase("sameFieldNames", "sameFieldNames", "getInt", null, null, 7));
Vladimir Marko920506d2014-11-18 14:47:31 +000062 testCases.add(new TestCase("b/18380491", "B18380491ConcreteClass", "foo",
Sebastien Hertz270a0e12015-01-16 19:49:09 +010063 new Object[]{42}, null, 42));
Vladimir Marko920506d2014-11-18 14:47:31 +000064 testCases.add(new TestCase("invoke-super abstract", "B18380491ConcreteClass", "foo",
Sebastien Hertz270a0e12015-01-16 19:49:09 +010065 new Object[]{0}, new AbstractMethodError(), null));
66 testCases.add(new TestCase("BadCaseInOpRegRegReg", "BadCaseInOpRegRegReg", "getInt", null,
67 null, 2));
Nicolas Geoffray32b2a522014-11-27 14:54:18 +000068 testCases.add(new TestCase("CmpLong", "CmpLong", "run", null, null, 0));
Sebastien Hertz270a0e12015-01-16 19:49:09 +010069 testCases.add(new TestCase("FloatIntConstPassing", "FloatIntConstPassing", "run", null,
70 null, 2));
Vladimir Marko341e4252014-12-19 10:29:51 +000071 testCases.add(new TestCase("b/18718277", "B18718277", "getInt", null, null, 0));
Sebastien Hertz270a0e12015-01-16 19:49:09 +010072 testCases.add(new TestCase("b/18800943 (1)", "B18800943_1", "n_a", null, new VerifyError(),
73 0));
74 testCases.add(new TestCase("b/18800943 (2)", "B18800943_2", "n_a", null, new VerifyError(),
75 0));
76 testCases.add(new TestCase("MoveExc", "MoveExc", "run", null, new ArithmeticException(),
77 null));
78 testCases.add(new TestCase("MoveExceptionOnEntry", "MoveExceptionOnEntry",
79 "moveExceptionOnEntry", new Object[]{0}, new VerifyError(), null));
Jeff Hao9ccd1512015-03-20 18:11:45 -070080 testCases.add(new TestCase("EmptySparseSwitch", "EmptySparseSwitch", "run", null, null,
81 null));
Andreas Gampeb588f4c2015-05-26 13:35:39 -070082 testCases.add(new TestCase("b/20224106", "B20224106", "run", null, new VerifyError(),
83 0));
Andreas Gampeda9badb2015-06-05 20:22:12 -070084 testCases.add(new TestCase("b/17410612", "B17410612", "run", null, new VerifyError(),
85 0));
Nicolas Geoffray69505f82015-06-18 18:04:12 +010086 testCases.add(new TestCase("b/21865464", "B21865464", "run", null, null,
87 null));
Vladimir Marko2d1a0a42015-06-18 17:40:00 +010088 testCases.add(new TestCase("b/21873167", "B21873167", "test", null, null, null));
Andreas Gampe8fda9f22014-10-03 16:15:37 -070089 }
90
91 public void runTests() {
92 for (TestCase tc : testCases) {
93 System.out.println(tc.testName);
94 try {
95 runTest(tc);
96 } catch (Exception exc) {
97 exc.printStackTrace(System.out);
98 }
99 }
100 }
101
102 private void runTest(TestCase tc) throws Exception {
Andreas Gampe8fda9f22014-10-03 16:15:37 -0700103 Exception errorReturn = null;
104 try {
Ian Rogers8e1f4f82014-11-05 11:07:30 -0800105 Class<?> c = Class.forName(tc.testClass);
106
107 Method[] methods = c.getDeclaredMethods();
108
109 // For simplicity we assume that test methods are not overloaded. So searching by name
110 // will give us the method we need to run.
111 Method method = null;
112 for (Method m : methods) {
113 if (m.getName().equals(tc.testMethodName)) {
114 method = m;
115 break;
116 }
Andreas Gampe8fda9f22014-10-03 16:15:37 -0700117 }
Ian Rogers8e1f4f82014-11-05 11:07:30 -0800118
119 if (method == null) {
120 errorReturn = new IllegalArgumentException("Could not find test method " +
121 tc.testMethodName + " in class " +
122 tc.testClass + " for test " +
123 tc.testName);
124 } else {
125 Object retValue;
126 if (Modifier.isStatic(method.getModifiers())) {
127 retValue = method.invoke(null, tc.values);
128 } else {
129 retValue = method.invoke(method.getDeclaringClass().newInstance(), tc.values);
130 }
131 if (tc.expectedException != null) {
132 errorReturn = new IllegalStateException("Expected an exception in test " +
133 tc.testName);
134 }
135 if (tc.expectedReturn == null && retValue != null) {
136 errorReturn = new IllegalStateException("Expected a null result in test " +
137 tc.testName);
138 } else if (tc.expectedReturn != null &&
139 (retValue == null || !tc.expectedReturn.equals(retValue))) {
140 errorReturn = new IllegalStateException("Expected return " +
141 tc.expectedReturn +
142 ", but got " + retValue);
143 } else {
144 // Expected result, do nothing.
145 }
Andreas Gampe8fda9f22014-10-03 16:15:37 -0700146 }
Ian Rogers8e1f4f82014-11-05 11:07:30 -0800147 } catch (Throwable exc) {
Andreas Gampe8fda9f22014-10-03 16:15:37 -0700148 if (tc.expectedException == null) {
149 errorReturn = new IllegalStateException("Did not expect exception", exc);
Vladimir Marko920506d2014-11-18 14:47:31 +0000150 } else if (exc instanceof InvocationTargetException && exc.getCause() != null &&
151 exc.getCause().getClass().equals(tc.expectedException.getClass())) {
152 // Expected exception is wrapped in InvocationTargetException.
Andreas Gampe8fda9f22014-10-03 16:15:37 -0700153 } else if (!tc.expectedException.getClass().equals(exc.getClass())) {
154 errorReturn = new IllegalStateException("Expected " +
Ian Rogers8e1f4f82014-11-05 11:07:30 -0800155 tc.expectedException.getClass().getName() +
156 ", but got " + exc.getClass(), exc);
157 } else {
158 // Expected exception, do nothing.
Andreas Gampe8fda9f22014-10-03 16:15:37 -0700159 }
160 } finally {
161 if (errorReturn != null) {
162 throw errorReturn;
163 }
164 }
165 }
166
167 public static void main(String[] args) throws Exception {
168 Main main = new Main();
169
170 main.runTests();
171
172 System.out.println("Done!");
173 }
174}