blob: 3c4ed3505f84109fb1e28fda2154935e1438d510 [file] [log] [blame]
Brian Carlstromce888532013-10-10 00:32:58 -07001/*
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
Jeff Hao201803f2013-11-20 18:11:39 -080017import java.lang.reflect.Method;
18
Brian Carlstromce888532013-10-10 00:32:58 -070019class JniTest {
20 public static void main(String[] args) {
21 System.loadLibrary("arttest");
22 testFindClassOnAttachedNativeThread();
Jeff Hao62509b62013-12-10 17:44:56 -080023 testFindFieldOnAttachedNativeThread();
Brian Carlstrom67fe2b42013-10-15 18:51:42 -070024 testCallStaticVoidMethodOnSubClass();
Jeff Hao201803f2013-11-20 18:11:39 -080025 testGetMirandaMethod();
Narayan Kamathef809d02013-12-19 17:52:47 +000026 testZeroLengthByteBuffers();
Andreas Gamped1104322014-05-01 14:38:56 -070027 testByteMethod();
28 testShortMethod();
29 testBooleanMethod();
30 testCharMethod();
Brian Carlstromce888532013-10-10 00:32:58 -070031 }
32
33 private static native void testFindClassOnAttachedNativeThread();
Brian Carlstrom67fe2b42013-10-15 18:51:42 -070034
Jeff Hao62509b62013-12-10 17:44:56 -080035 private static boolean testFindFieldOnAttachedNativeThreadField;
36
37 private static void testFindFieldOnAttachedNativeThread() {
38 testFindFieldOnAttachedNativeThreadNative();
39 if (!testFindFieldOnAttachedNativeThreadField) {
40 throw new AssertionError();
41 }
42 }
43
44 private static native void testFindFieldOnAttachedNativeThreadNative();
45
Brian Carlstrom67fe2b42013-10-15 18:51:42 -070046 private static void testCallStaticVoidMethodOnSubClass() {
47 testCallStaticVoidMethodOnSubClassNative();
48 if (!testCallStaticVoidMethodOnSubClass_SuperClass.executed) {
49 throw new AssertionError();
50 }
51 }
52
53 private static native void testCallStaticVoidMethodOnSubClassNative();
54
55 private static class testCallStaticVoidMethodOnSubClass_SuperClass {
56 private static boolean executed = false;
57 private static void execute() {
58 executed = true;
59 }
60 }
61
62 private static class testCallStaticVoidMethodOnSubClass_SubClass
63 extends testCallStaticVoidMethodOnSubClass_SuperClass {
64 }
Jeff Hao201803f2013-11-20 18:11:39 -080065
66 private static native Method testGetMirandaMethodNative();
67
68 private static void testGetMirandaMethod() {
69 Method m = testGetMirandaMethodNative();
70 if (m.getDeclaringClass() != testGetMirandaMethod_MirandaInterface.class) {
71 throw new AssertionError();
72 }
73 }
74
Narayan Kamathef809d02013-12-19 17:52:47 +000075 private static native void testZeroLengthByteBuffers();
76
Jeff Hao201803f2013-11-20 18:11:39 -080077 private static abstract class testGetMirandaMethod_MirandaAbstract implements testGetMirandaMethod_MirandaInterface {
78 public boolean inAbstract() {
79 return true;
80 }
81 }
82
83 private static interface testGetMirandaMethod_MirandaInterface {
84 public boolean inInterface();
85 }
Andreas Gamped1104322014-05-01 14:38:56 -070086
87 // Test sign-extension for values < 32b
88
89 native static byte byteMethod(byte b1, byte b2, byte b3, byte b4, byte b5, byte b6, byte b7,
90 byte b8, byte b9, byte b10);
91
92 private static void testByteMethod() {
93 byte returns[] = { 0, 1, 2, 127, -1, -2, -128 };
94 for (int i = 0; i < returns.length; i++) {
95 byte result = byteMethod((byte)i, (byte)2, (byte)(-3), (byte)4, (byte)(-5), (byte)6,
96 (byte)(-7), (byte)8, (byte)(-9), (byte)10);
97 if (returns[i] != result) {
98 System.out.println("Run " + i + " with " + returns[i] + " vs " + result);
99 throw new AssertionError();
100 }
101 }
102 }
103
104 native static short shortMethod(short s1, short s2, short s3, short s4, short s5, short s6, short s7,
105 short s8, short s9, short s10);
106
107 private static void testShortMethod() {
108 short returns[] = { 0, 1, 2, 127, 32767, -1, -2, -128, -32768 };
109 for (int i = 0; i < returns.length; i++) {
110 short result = shortMethod((short)i, (short)2, (short)(-3), (short)4, (short)(-5), (short)6,
111 (short)(-7), (short)8, (short)(-9), (short)10);
112 if (returns[i] != result) {
113 System.out.println("Run " + i + " with " + returns[i] + " vs " + result);
114 throw new AssertionError();
115 }
116 }
117 }
118
119 // Test zero-extension for values < 32b
120
121 native static boolean booleanMethod(boolean b1, boolean b2, boolean b3, boolean b4, boolean b5, boolean b6, boolean b7,
122 boolean b8, boolean b9, boolean b10);
123
124 private static void testBooleanMethod() {
125 if (booleanMethod(false, true, false, true, false, true, false, true, false, true)) {
126 throw new AssertionError();
127 }
128
129 if (!booleanMethod(true, true, false, true, false, true, false, true, false, true)) {
130 throw new AssertionError();
131 }
132 }
133
134 native static char charMethod(char c1, char c2, char c3, char c4, char c5, char c6, char c7,
135 char c8, char c9, char c10);
136
137 private static void testCharMethod() {
138 char returns[] = { (char)0, (char)1, (char)2, (char)127, (char)255, (char)256, (char)15000,
139 (char)34000 };
140 for (int i = 0; i < returns.length; i++) {
141 char result = charMethod((char)i, 'a', 'b', 'c', '0', '1', '2', (char)1234, (char)2345,
142 (char)3456);
143 if (returns[i] != result) {
144 System.out.println("Run " + i + " with " + (int)returns[i] + " vs " + (int)result);
145 throw new AssertionError();
146 }
147 }
148 }
Brian Carlstromce888532013-10-10 00:32:58 -0700149}