e7de8773390f1f0ea39e5c5127f7a2f0d74aaf27
[cacao.git] / tests / regression / native / checkjni.java
1 /* src/tests/native/checkjni.java - for testing JNI related stuff
2
3    Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
4    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6    TU Wien
7
8    This file is part of CACAO.
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25    Contact: cacao@cacaojvm.org
26
27    Authors: Christian Thalinger
28
29    Changes:
30
31 */
32
33
34 public class checkjni {
35     public static native boolean IsAssignableFrom(Class sub, Class sup);
36     public static native boolean IsInstanceOf(Object obj, Class clazz);
37     public static native boolean IsSameObject(Object obj1, Object obj2);
38     public static native int     PushLocalFrame(int capacity);
39         public static native void    Throw() throws Exception;
40         public static native Class   GetObjectClass(Object obj);
41         public static native String  NewString(int type);
42         public static native int     GetIntField(Object obj);
43         public static native Object  GetObjectField(Object obj);
44         public static native int     GetStaticIntField();
45         public static native Object  GetStaticObjectField();
46         public static native void    SetStaticIntField(int val);
47         public static native int     GetIntField();
48         public static native int[]   NewIntArray(int length);
49         public static native long[]  NewLongArray(int length);
50
51         public static int     jsfI = 0x123456;
52         public static Object  jsfL = new Object();
53
54         class checkjniobj {
55                 public int    jfI = 0x123456;
56                 public Object jfL = new Object();
57         }
58
59     public static void main(String[] argv) {
60         System.loadLibrary("checkjni");
61
62         new checkjni();
63     }
64
65     public checkjni() {
66         checkIsAssignableFrom();
67         checkIsInstanceOf();
68                 checkIsSameObject();
69         checkPushLocalFrame();
70                 checkThrow();
71                 checkGetObjectClass();
72                 checkFields();
73                 checkArrays();
74                 checkNewString();
75     }
76
77     void checkIsAssignableFrom() {
78         p("IsAssignableFrom:");
79
80         Class sub = Integer.class;
81         Class sup = Object.class;
82
83         equal(IsAssignableFrom(sup, sup), true);
84         equal(IsAssignableFrom(sub, sup), true);
85         equal(IsAssignableFrom(sup, sub), false);
86     }
87
88     void checkIsInstanceOf() {
89         p("IsInstanceOf:");
90
91         Object obj = new Object();
92         Object obj2 = new Integer(1);
93         Class clazz = Object.class;
94         Class clazz2 = Integer.class;
95
96         equal(IsInstanceOf(obj, clazz), true);
97         equal(IsInstanceOf(obj2, clazz), true);
98         equal(IsInstanceOf(obj, clazz2), false);
99     }
100
101     void checkIsSameObject() {
102         p("IsSameObject:");
103
104         Object obj1 = new Object();
105         Object obj2 = new Integer(1);
106         Class clazz = Object.class;
107
108         equal(IsSameObject(obj1, obj1), true);
109         equal(IsSameObject(clazz, clazz), true);
110         equal(IsSameObject(null, null), true);
111         equal(IsSameObject(obj1, obj2), false);
112         equal(IsSameObject(obj1, clazz), false);
113         equal(IsSameObject(obj1, null), false);
114     }
115
116     void checkPushLocalFrame() {
117         p("PushLocalFrame:");
118
119         equal(PushLocalFrame(100), 0);
120     }
121
122         void checkThrow() {
123                 p("Throw");
124                 
125                 try {
126                         Throw();
127                         p("FAILED, no exception thrown");
128                 } catch (Exception e) {
129                         p("PASS, " + e);
130                 }
131         }
132
133     void checkGetObjectClass() {
134         p("GetObjectClass:");
135
136         Object obj1 = new Object();
137         Object obj2 = new Integer(1);
138         Class clazz1 = Object.class;
139         Class clazz2 = Integer.class;
140
141         equal(GetObjectClass(obj1), clazz1);
142         equal(GetObjectClass(obj2), clazz2);
143         equal(GetObjectClass(null), null);
144     }
145
146         void checkNewString() {
147                 p("NewString:");
148                 
149                 equal(NewString(2), "Test String from JNI with UTF");
150         }
151
152         void checkFields() {
153                 p("Field Access:");
154                 
155                 equal(GetStaticIntField(), jsfI);
156                 equal(GetStaticObjectField(), jsfL);
157                 
158                 checkjniobj o = new checkjniobj();
159
160                 equal(GetIntField(o), o.jfI);
161                 equal(GetObjectField(o), o.jfL);
162                 
163                 SetStaticIntField(0xABCDEF); equal(jsfI, 0xABCDEF);
164         }
165
166         void checkArrays() {
167                 p("Array Access:");
168
169                 int i;
170                 boolean result;
171
172                 int[] aI = NewIntArray(10);
173                 for (i = 0, result = true; i < aI.length; i++) result &= (aI[i] == i);
174                 if (result)
175                         p("PASS, size=" + aI.length);
176                 else
177                         p("FAILED");
178
179                 long[] aL = NewLongArray(20);
180                 for (i = 0, result = true; i < aL.length; i++) result &= (aL[i] == i);
181                 if (result)
182                         p("PASS, size=" + aL.length);
183                 else
184                         p("FAILED");
185         }
186
187     void equal(boolean a, boolean b) {
188         if (a == b)
189             p("PASS");
190         else
191             p("FAILED");
192     }
193
194     void equal(int a, int b) {
195         if (a == b)
196             p("PASS");
197         else
198             p("FAILED ("+a+"!="+b+")");
199     }
200
201     void equal(Object a, Object b) {
202         if (a == b)
203             p("PASS");
204         else
205             p("FAILED");
206     }
207
208     void equal(String a, String b) {
209         if (a.equals(b))
210             p("PASS");
211         else
212             p("FAILED ("+a+"!="+b+")");
213     }
214
215     void p(String s) {
216         System.out.println(s);
217     }
218 }