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