Brian Carlstrom | ce88853 | 2013-10-10 00:32:58 -0700 | [diff] [blame] | 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 | |
Jeff Hao | 201803f | 2013-11-20 18:11:39 -0800 | [diff] [blame] | 17 | import java.lang.reflect.Method; |
| 18 | |
Brian Carlstrom | ce88853 | 2013-10-10 00:32:58 -0700 | [diff] [blame] | 19 | class JniTest { |
| 20 | public static void main(String[] args) { |
| 21 | System.loadLibrary("arttest"); |
| 22 | testFindClassOnAttachedNativeThread(); |
Jeff Hao | 62509b6 | 2013-12-10 17:44:56 -0800 | [diff] [blame] | 23 | testFindFieldOnAttachedNativeThread(); |
Brian Carlstrom | 67fe2b4 | 2013-10-15 18:51:42 -0700 | [diff] [blame] | 24 | testCallStaticVoidMethodOnSubClass(); |
Jeff Hao | 201803f | 2013-11-20 18:11:39 -0800 | [diff] [blame] | 25 | testGetMirandaMethod(); |
Narayan Kamath | ef809d0 | 2013-12-19 17:52:47 +0000 | [diff] [blame] | 26 | testZeroLengthByteBuffers(); |
Andreas Gampe | d110432 | 2014-05-01 14:38:56 -0700 | [diff] [blame^] | 27 | testByteMethod(); |
| 28 | testShortMethod(); |
| 29 | testBooleanMethod(); |
| 30 | testCharMethod(); |
Brian Carlstrom | ce88853 | 2013-10-10 00:32:58 -0700 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | private static native void testFindClassOnAttachedNativeThread(); |
Brian Carlstrom | 67fe2b4 | 2013-10-15 18:51:42 -0700 | [diff] [blame] | 34 | |
Jeff Hao | 62509b6 | 2013-12-10 17:44:56 -0800 | [diff] [blame] | 35 | 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 Carlstrom | 67fe2b4 | 2013-10-15 18:51:42 -0700 | [diff] [blame] | 46 | 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 Hao | 201803f | 2013-11-20 18:11:39 -0800 | [diff] [blame] | 65 | |
| 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 Kamath | ef809d0 | 2013-12-19 17:52:47 +0000 | [diff] [blame] | 75 | private static native void testZeroLengthByteBuffers(); |
| 76 | |
Jeff Hao | 201803f | 2013-11-20 18:11:39 -0800 | [diff] [blame] | 77 | 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 Gampe | d110432 | 2014-05-01 14:38:56 -0700 | [diff] [blame^] | 86 | |
| 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 Carlstrom | ce88853 | 2013-10-10 00:32:58 -0700 | [diff] [blame] | 149 | } |