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