Main.java revision 1268b742c8cff7318dc0b5b283cbaeabfe0725ba
1/*
2 * Copyright (C) 2013 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.Method;
18
19public class Main {
20    public static void main(String[] args) {
21        System.loadLibrary("arttest");
22        testFindClassOnAttachedNativeThread();
23        testFindFieldOnAttachedNativeThread();
24        testReflectFieldGetFromAttachedNativeThreadNative();
25        testCallStaticVoidMethodOnSubClass();
26        testGetMirandaMethod();
27        testZeroLengthByteBuffers();
28        testByteMethod();
29        testShortMethod();
30        testBooleanMethod();
31        testCharMethod();
32        testIsAssignableFromOnPrimitiveTypes();
33    }
34
35    private static native void testFindClassOnAttachedNativeThread();
36
37    private static boolean testFindFieldOnAttachedNativeThreadField;
38
39    private static native void testReflectFieldGetFromAttachedNativeThreadNative();
40
41    public static boolean testReflectFieldGetFromAttachedNativeThreadField;
42
43    private static void testFindFieldOnAttachedNativeThread() {
44      testFindFieldOnAttachedNativeThreadNative();
45      if (!testFindFieldOnAttachedNativeThreadField) {
46            throw new AssertionError();
47        }
48    }
49
50    private static native void testFindFieldOnAttachedNativeThreadNative();
51
52    private static void testCallStaticVoidMethodOnSubClass() {
53        testCallStaticVoidMethodOnSubClassNative();
54        if (!testCallStaticVoidMethodOnSubClass_SuperClass.executed) {
55            throw new AssertionError();
56        }
57    }
58
59    private static native void testCallStaticVoidMethodOnSubClassNative();
60
61    private static class testCallStaticVoidMethodOnSubClass_SuperClass {
62        private static boolean executed = false;
63        private static void execute() {
64            executed = true;
65        }
66    }
67
68    private static class testCallStaticVoidMethodOnSubClass_SubClass
69        extends testCallStaticVoidMethodOnSubClass_SuperClass {
70    }
71
72    private static native Method testGetMirandaMethodNative();
73
74    private static void testGetMirandaMethod() {
75        Method m = testGetMirandaMethodNative();
76        if (m.getDeclaringClass() != testGetMirandaMethod_MirandaInterface.class) {
77            throw new AssertionError();
78        }
79    }
80
81    private static native void testZeroLengthByteBuffers();
82
83    private static abstract class testGetMirandaMethod_MirandaAbstract implements testGetMirandaMethod_MirandaInterface {
84        public boolean inAbstract() {
85            return true;
86        }
87    }
88
89    private static interface testGetMirandaMethod_MirandaInterface {
90        public boolean inInterface();
91    }
92
93    // Test sign-extension for values < 32b
94
95    native static byte byteMethod(byte b1, byte b2, byte b3, byte b4, byte b5, byte b6, byte b7,
96        byte b8, byte b9, byte b10);
97
98    private static void testByteMethod() {
99      byte returns[] = { 0, 1, 2, 127, -1, -2, -128 };
100      for (int i = 0; i < returns.length; i++) {
101        byte result = byteMethod((byte)i, (byte)2, (byte)(-3), (byte)4, (byte)(-5), (byte)6,
102            (byte)(-7), (byte)8, (byte)(-9), (byte)10);
103        if (returns[i] != result) {
104          System.out.println("Run " + i + " with " + returns[i] + " vs " + result);
105          throw new AssertionError();
106        }
107      }
108    }
109
110    native static short shortMethod(short s1, short s2, short s3, short s4, short s5, short s6, short s7,
111        short s8, short s9, short s10);
112
113    private static void testShortMethod() {
114      short returns[] = { 0, 1, 2, 127, 32767, -1, -2, -128, -32768 };
115      for (int i = 0; i < returns.length; i++) {
116        short result = shortMethod((short)i, (short)2, (short)(-3), (short)4, (short)(-5), (short)6,
117            (short)(-7), (short)8, (short)(-9), (short)10);
118        if (returns[i] != result) {
119          System.out.println("Run " + i + " with " + returns[i] + " vs " + result);
120          throw new AssertionError();
121        }
122      }
123    }
124
125    // Test zero-extension for values < 32b
126
127    native static boolean booleanMethod(boolean b1, boolean b2, boolean b3, boolean b4, boolean b5, boolean b6, boolean b7,
128        boolean b8, boolean b9, boolean b10);
129
130    private static void testBooleanMethod() {
131      if (booleanMethod(false, true, false, true, false, true, false, true, false, true)) {
132        throw new AssertionError();
133      }
134
135      if (!booleanMethod(true, true, false, true, false, true, false, true, false, true)) {
136        throw new AssertionError();
137      }
138    }
139
140    native static char charMethod(char c1, char c2, char c3, char c4, char c5, char c6, char c7,
141        char c8, char c9, char c10);
142
143    private static void testCharMethod() {
144      char returns[] = { (char)0, (char)1, (char)2, (char)127, (char)255, (char)256, (char)15000,
145          (char)34000 };
146      for (int i = 0; i < returns.length; i++) {
147        char result = charMethod((char)i, 'a', 'b', 'c', '0', '1', '2', (char)1234, (char)2345,
148            (char)3456);
149        if (returns[i] != result) {
150          System.out.println("Run " + i + " with " + (int)returns[i] + " vs " + (int)result);
151          throw new AssertionError();
152        }
153      }
154    }
155
156    // http://b/16531674
157    private static void testIsAssignableFromOnPrimitiveTypes() {
158      if (!nativeIsAssignableFrom(int.class, Integer.TYPE)) {
159        System.out.println("IsAssignableFrom(int.class, Integer.TYPE) returned false, expected true");
160        throw new AssertionError();
161      }
162
163      if (!nativeIsAssignableFrom(Integer.TYPE, int.class)) {
164        System.out.println("IsAssignableFrom(Integer.TYPE, int.class) returned false, expected true");
165        throw new AssertionError();
166      }
167    }
168
169    native static boolean nativeIsAssignableFrom(Class<?> from, Class<?> to);
170}
171