1 /* src/tests/native/checkjni.java - for testing JNI related stuff
3 Copyright (C) 1996-2012
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2, or (at
11 your option) any later version.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 public class checkjni {
27 public static native boolean IsAssignableFrom(Class sub, Class sup);
28 public static native boolean IsInstanceOf(Object obj, Class clazz);
29 public static native boolean IsSameObject(Object obj1, Object obj2);
30 public static native int PushLocalFrame(int capacity);
31 public static native void Throw() throws Exception;
32 public static native Class GetObjectClass(Object obj);
33 public static native String NewString(int type);
34 public static native int GetIntField(Object obj);
35 public static native Object GetObjectField(Object obj);
36 public static native int GetStaticIntField();
37 public static native Object GetStaticObjectField();
38 public static native void SetStaticIntField(int val);
39 public static native int GetIntField();
40 public static native int[] NewIntArray(int length);
41 public static native long[] NewLongArray(int length);
43 public static int jsfI = 0x123456;
44 public static Object jsfL = new Object();
47 public int jfI = 0x123456;
48 public Object jfL = new Object();
51 public static void main(String[] argv) {
52 System.loadLibrary("checkjni");
58 checkIsAssignableFrom();
61 checkPushLocalFrame();
63 checkGetObjectClass();
69 void checkIsAssignableFrom() {
70 p("IsAssignableFrom:");
72 Class sub = Integer.class;
73 Class sup = Object.class;
75 equal(IsAssignableFrom(sup, sup), true);
76 equal(IsAssignableFrom(sub, sup), true);
77 equal(IsAssignableFrom(sup, sub), false);
80 void checkIsInstanceOf() {
83 Object obj = new Object();
84 Object obj2 = new Integer(1);
85 Class clazz = Object.class;
86 Class clazz2 = Integer.class;
88 equal(IsInstanceOf(obj, clazz), true);
89 equal(IsInstanceOf(obj2, clazz), true);
90 equal(IsInstanceOf(obj, clazz2), false);
93 void checkIsSameObject() {
96 Object obj1 = new Object();
97 Object obj2 = new Integer(1);
98 Class clazz = Object.class;
100 equal(IsSameObject(obj1, obj1), true);
101 equal(IsSameObject(clazz, clazz), true);
102 equal(IsSameObject(null, null), true);
103 equal(IsSameObject(obj1, obj2), false);
104 equal(IsSameObject(obj1, clazz), false);
105 equal(IsSameObject(obj1, null), false);
108 void checkPushLocalFrame() {
109 p("PushLocalFrame:");
111 equal(PushLocalFrame(100), 0);
119 p("FAILED, no exception thrown");
120 } catch (Exception e) {
125 void checkGetObjectClass() {
126 p("GetObjectClass:");
128 Object obj1 = new Object();
129 Object obj2 = new Integer(1);
130 Class clazz1 = Object.class;
131 Class clazz2 = Integer.class;
133 equal(GetObjectClass(obj1), clazz1);
134 equal(GetObjectClass(obj2), clazz2);
137 void checkNewString() {
140 equal(NewString(2), "Test String from JNI with UTF");
146 equal(GetStaticIntField(), jsfI);
147 equal(GetStaticObjectField(), jsfL);
149 checkjniobj o = new checkjniobj();
151 equal(GetIntField(o), o.jfI);
152 equal(GetObjectField(o), o.jfL);
154 SetStaticIntField(0xABCDEF); equal(jsfI, 0xABCDEF);
163 int[] aI = NewIntArray(10);
164 for (i = 0, result = true; i < aI.length; i++) result &= (aI[i] == i);
166 p("PASS, size=" + aI.length);
170 long[] aL = NewLongArray(20);
171 for (i = 0, result = true; i < aL.length; i++) result &= (aL[i] == i);
173 p("PASS, size=" + aL.length);
178 void equal(boolean a, boolean b) {
185 void equal(int a, int b) {
189 p("FAILED ("+a+"!="+b+")");
192 void equal(Object a, Object b) {
199 void equal(String a, String b) {
203 p("FAILED ("+a+"!="+b+")");
207 System.out.println(s);