blob: ddd39b540ad868e5519b52deaa7e3f1a477b7082 [file] [log] [blame]
Elliott Hughes0c9cd562011-08-12 10:59:29 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
Carl Shapiro9b9ba282011-08-14 15:30:39 -07003#include "jni_internal.h"
Elliott Hughes0c9cd562011-08-12 10:59:29 -07004
Carl Shapiro9b9ba282011-08-14 15:30:39 -07005#include <sys/mman.h>
6
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07007#include <cmath>
8
Carl Shapiro9b9ba282011-08-14 15:30:39 -07009#include "common_test.h"
Elliott Hughes726079d2011-10-07 18:43:44 -070010#include "ScopedLocalRef.h"
Elliott Hughes0c9cd562011-08-12 10:59:29 -070011
12namespace art {
13
Brian Carlstromf734cf52011-08-17 16:28:14 -070014class JniInternalTest : public CommonTest {
Elliott Hughesc7ac37f2011-08-12 12:21:58 -070015 protected:
16 virtual void SetUp() {
Brian Carlstromf734cf52011-08-17 16:28:14 -070017 CommonTest::SetUp();
Elliott Hughes5174fe62011-08-23 15:12:35 -070018
Elliott Hughesa2501992011-08-26 19:39:54 -070019 vm_ = Runtime::Current()->GetJavaVM();
20
Elliott Hughes5174fe62011-08-23 15:12:35 -070021 // Turn on -verbose:jni for the JNI tests.
Elliott Hughesa2501992011-08-26 19:39:54 -070022 vm_->verbose_jni = true;
Elliott Hughes5174fe62011-08-23 15:12:35 -070023
Elliott Hughesc7ac37f2011-08-12 12:21:58 -070024 env_ = Thread::Current()->GetJniEnv();
Elliott Hughesb465ab02011-08-24 11:21:21 -070025
Elliott Hughes726079d2011-10-07 18:43:44 -070026 ScopedLocalRef<jclass> aioobe(env_, env_->FindClass("java/lang/ArrayIndexOutOfBoundsException"));
27 CHECK(aioobe.get() != NULL);
28 aioobe_ = reinterpret_cast<jclass>(env_->NewGlobalRef(aioobe.get()));
Elliott Hughesb465ab02011-08-24 11:21:21 -070029
Elliott Hughes726079d2011-10-07 18:43:44 -070030 ScopedLocalRef<jclass> sioobe(env_, env_->FindClass("java/lang/StringIndexOutOfBoundsException"));
31 CHECK(sioobe.get() != NULL);
32 sioobe_ = reinterpret_cast<jclass>(env_->NewGlobalRef(sioobe.get()));
33 }
34
35 virtual void TearDown() {
36 env_->DeleteGlobalRef(aioobe_);
37 env_->DeleteGlobalRef(sioobe_);
38 CommonTest::TearDown();
Elliott Hughesc7ac37f2011-08-12 12:21:58 -070039 }
Elliott Hughesb465ab02011-08-24 11:21:21 -070040
Elliott Hughesa2501992011-08-26 19:39:54 -070041 JavaVMExt* vm_;
42 JNIEnvExt* env_;
Elliott Hughes814e4032011-08-23 12:07:56 -070043 jclass aioobe_;
Elliott Hughesb465ab02011-08-24 11:21:21 -070044 jclass sioobe_;
Elliott Hughes0c9cd562011-08-12 10:59:29 -070045};
46
Elliott Hughes885c3bd2011-08-22 16:59:20 -070047TEST_F(JniInternalTest, AllocObject) {
48 jclass c = env_->FindClass("java/lang/String");
49 ASSERT_TRUE(c != NULL);
50 jobject o = env_->AllocObject(c);
51 ASSERT_TRUE(o != NULL);
52
53 // We have an instance of the class we asked for...
54 ASSERT_TRUE(env_->IsInstanceOf(o, c));
55 // ...whose fields haven't been initialized because
56 // we didn't call a constructor.
57 ASSERT_EQ(0, env_->GetIntField(o, env_->GetFieldID(c, "count", "I")));
58 ASSERT_EQ(0, env_->GetIntField(o, env_->GetFieldID(c, "offset", "I")));
59 ASSERT_TRUE(env_->GetObjectField(o, env_->GetFieldID(c, "value", "[C")) == NULL);
60}
61
Elliott Hughesc7ac37f2011-08-12 12:21:58 -070062TEST_F(JniInternalTest, GetVersion) {
63 ASSERT_EQ(JNI_VERSION_1_6, env_->GetVersion());
64}
65
Elliott Hughes0c9cd562011-08-12 10:59:29 -070066#define EXPECT_CLASS_FOUND(NAME) \
Elliott Hughesbd935992011-08-22 11:59:34 -070067 EXPECT_TRUE(env_->FindClass(NAME) != NULL); \
68 EXPECT_FALSE(env_->ExceptionCheck())
Elliott Hughes0c9cd562011-08-12 10:59:29 -070069
70#define EXPECT_CLASS_NOT_FOUND(NAME) \
Elliott Hughesbd935992011-08-22 11:59:34 -070071 EXPECT_TRUE(env_->FindClass(NAME) == NULL); \
72 EXPECT_TRUE(env_->ExceptionCheck()); \
73 env_->ExceptionClear()
Elliott Hughes0c9cd562011-08-12 10:59:29 -070074
Elliott Hughesa2501992011-08-26 19:39:54 -070075std::string gCheckJniAbortMessage;
76void TestCheckJniAbortHook(const std::string& reason) {
77 gCheckJniAbortMessage = reason;
78}
79
Elliott Hughes0c9cd562011-08-12 10:59:29 -070080TEST_F(JniInternalTest, FindClass) {
Elliott Hughes0c9cd562011-08-12 10:59:29 -070081 // Reference types...
Elliott Hughes0c9cd562011-08-12 10:59:29 -070082 EXPECT_CLASS_FOUND("java/lang/String");
Elliott Hughes0c9cd562011-08-12 10:59:29 -070083 // ...for arrays too, where you must include "L;".
84 EXPECT_CLASS_FOUND("[Ljava/lang/String;");
Elliott Hughesa2501992011-08-26 19:39:54 -070085
86 vm_->check_jni_abort_hook = TestCheckJniAbortHook;
87 // We support . as well as / for compatibility, if -Xcheck:jni is off.
88 EXPECT_CLASS_FOUND("java.lang.String");
89 EXPECT_CLASS_NOT_FOUND("Ljava.lang.String;");
Elliott Hughes0c9cd562011-08-12 10:59:29 -070090 EXPECT_CLASS_FOUND("[Ljava.lang.String;");
91 EXPECT_CLASS_NOT_FOUND("[java.lang.String");
92
Elliott Hughesa2501992011-08-26 19:39:54 -070093 // You can't include the "L;" in a JNI class descriptor.
94 EXPECT_CLASS_NOT_FOUND("Ljava/lang/String;");
95 // But you must include it for an array of any reference type.
96 EXPECT_CLASS_NOT_FOUND("[java/lang/String");
97 vm_->check_jni_abort_hook = NULL;
98
Elliott Hughes0c9cd562011-08-12 10:59:29 -070099 // Primitive arrays are okay (if the primitive type is valid)...
100 EXPECT_CLASS_FOUND("[C");
Elliott Hughesa2501992011-08-26 19:39:54 -0700101 vm_->check_jni_abort_hook = TestCheckJniAbortHook;
Elliott Hughes0c9cd562011-08-12 10:59:29 -0700102 EXPECT_CLASS_NOT_FOUND("[K");
Elliott Hughesa2501992011-08-26 19:39:54 -0700103 vm_->check_jni_abort_hook = NULL;
Elliott Hughes0c9cd562011-08-12 10:59:29 -0700104 // But primitive types aren't allowed...
105 EXPECT_CLASS_NOT_FOUND("C");
106 EXPECT_CLASS_NOT_FOUND("K");
107}
108
Elliott Hughescdf53122011-08-19 15:46:09 -0700109#define EXPECT_EXCEPTION(exception_class) \
110 do { \
111 EXPECT_TRUE(env_->ExceptionCheck()); \
112 jthrowable exception = env_->ExceptionOccurred(); \
113 EXPECT_NE(static_cast<jthrowable>(NULL), exception); \
Elliott Hughescdf53122011-08-19 15:46:09 -0700114 env_->ExceptionClear(); \
Elliott Hughesa2501992011-08-26 19:39:54 -0700115 EXPECT_TRUE(env_->IsInstanceOf(exception, exception_class)); \
Elliott Hughescdf53122011-08-19 15:46:09 -0700116 } while (false)
117
118TEST_F(JniInternalTest, GetFieldID) {
119 jclass jlnsfe = env_->FindClass("java/lang/NoSuchFieldError");
120 ASSERT_TRUE(jlnsfe != NULL);
121 jclass c = env_->FindClass("java/lang/String");
122 ASSERT_TRUE(c != NULL);
123
124 // Wrong type.
125 jfieldID fid = env_->GetFieldID(c, "count", "J");
126 EXPECT_EQ(static_cast<jfieldID>(NULL), fid);
127 EXPECT_EXCEPTION(jlnsfe);
128
Ian Rogersb17d08b2011-09-02 16:16:49 -0700129 // Wrong type where type doesn't exist.
130 fid = env_->GetFieldID(c, "count", "Lrod/jane/freddy;");
131 EXPECT_EQ(static_cast<jfieldID>(NULL), fid);
132 EXPECT_EXCEPTION(jlnsfe);
133
Elliott Hughescdf53122011-08-19 15:46:09 -0700134 // Wrong name.
135 fid = env_->GetFieldID(c, "Count", "I");
136 EXPECT_EQ(static_cast<jfieldID>(NULL), fid);
137 EXPECT_EXCEPTION(jlnsfe);
138
139 // Good declared field lookup.
140 fid = env_->GetFieldID(c, "count", "I");
141 EXPECT_NE(static_cast<jfieldID>(NULL), fid);
142 EXPECT_TRUE(fid != NULL);
143 EXPECT_FALSE(env_->ExceptionCheck());
144
145 // Good superclass field lookup.
146 c = env_->FindClass("java/lang/StringBuilder");
147 fid = env_->GetFieldID(c, "count", "I");
148 EXPECT_NE(static_cast<jfieldID>(NULL), fid);
149 EXPECT_TRUE(fid != NULL);
150 EXPECT_FALSE(env_->ExceptionCheck());
151
152 // Not instance.
153 fid = env_->GetFieldID(c, "CASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;");
154 EXPECT_EQ(static_cast<jfieldID>(NULL), fid);
155 EXPECT_EXCEPTION(jlnsfe);
156}
157
158TEST_F(JniInternalTest, GetStaticFieldID) {
159 jclass jlnsfe = env_->FindClass("java/lang/NoSuchFieldError");
160 ASSERT_TRUE(jlnsfe != NULL);
161 jclass c = env_->FindClass("java/lang/String");
162 ASSERT_TRUE(c != NULL);
163
164 // Wrong type.
165 jfieldID fid = env_->GetStaticFieldID(c, "CASE_INSENSITIVE_ORDER", "J");
166 EXPECT_EQ(static_cast<jfieldID>(NULL), fid);
167 EXPECT_EXCEPTION(jlnsfe);
168
Ian Rogersb17d08b2011-09-02 16:16:49 -0700169 // Wrong type where type doesn't exist.
170 fid = env_->GetStaticFieldID(c, "CASE_INSENSITIVE_ORDER", "Lrod/jane/freddy;");
171 EXPECT_EQ(static_cast<jfieldID>(NULL), fid);
172 EXPECT_EXCEPTION(jlnsfe);
173
Elliott Hughescdf53122011-08-19 15:46:09 -0700174 // Wrong name.
175 fid = env_->GetStaticFieldID(c, "cASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;");
176 EXPECT_EQ(static_cast<jfieldID>(NULL), fid);
177 EXPECT_EXCEPTION(jlnsfe);
178
179 // Good declared field lookup.
180 fid = env_->GetStaticFieldID(c, "CASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;");
181 EXPECT_NE(static_cast<jfieldID>(NULL), fid);
182 EXPECT_TRUE(fid != NULL);
183 EXPECT_FALSE(env_->ExceptionCheck());
184
185 // Not static.
186 fid = env_->GetStaticFieldID(c, "count", "I");
187 EXPECT_EQ(static_cast<jfieldID>(NULL), fid);
188 EXPECT_EXCEPTION(jlnsfe);
189}
190
Ian Rogers4dd71f12011-08-16 14:16:02 -0700191TEST_F(JniInternalTest, GetMethodID) {
192 jclass jlobject = env_->FindClass("java/lang/Object");
193 jclass jlstring = env_->FindClass("java/lang/String");
194 jclass jlnsme = env_->FindClass("java/lang/NoSuchMethodError");
195
196 // Sanity check that no exceptions are pending
Elliott Hughescdf53122011-08-19 15:46:09 -0700197 ASSERT_FALSE(env_->ExceptionCheck());
Ian Rogers4dd71f12011-08-16 14:16:02 -0700198
199 // Check that java.lang.Object.foo() doesn't exist and NoSuchMethodError is
200 // a pending exception
201 jmethodID method = env_->GetMethodID(jlobject, "foo", "()V");
202 EXPECT_EQ(static_cast<jmethodID>(NULL), method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700203 EXPECT_EXCEPTION(jlnsme);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700204
205 // Check that java.lang.Object.equals() does exist
Ian Rogers4dd71f12011-08-16 14:16:02 -0700206 method = env_->GetMethodID(jlobject, "equals", "(Ljava/lang/Object;)Z");
207 EXPECT_NE(static_cast<jmethodID>(NULL), method);
208 EXPECT_FALSE(env_->ExceptionCheck());
Ian Rogers4dd71f12011-08-16 14:16:02 -0700209
210 // Check that GetMethodID for java.lang.String.valueOf(int) fails as the
211 // method is static
212 method = env_->GetMethodID(jlstring, "valueOf", "(I)Ljava/lang/String;");
213 EXPECT_EQ(static_cast<jmethodID>(NULL), method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700214 EXPECT_EXCEPTION(jlnsme);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700215}
216
217TEST_F(JniInternalTest, GetStaticMethodID) {
218 jclass jlobject = env_->FindClass("java/lang/Object");
219 jclass jlnsme = env_->FindClass("java/lang/NoSuchMethodError");
220
221 // Sanity check that no exceptions are pending
Elliott Hughescdf53122011-08-19 15:46:09 -0700222 ASSERT_FALSE(env_->ExceptionCheck());
Ian Rogers4dd71f12011-08-16 14:16:02 -0700223
224 // Check that java.lang.Object.foo() doesn't exist and NoSuchMethodError is
225 // a pending exception
226 jmethodID method = env_->GetStaticMethodID(jlobject, "foo", "()V");
227 EXPECT_EQ(static_cast<jmethodID>(NULL), method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700228 EXPECT_EXCEPTION(jlnsme);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700229
230 // Check that GetStaticMethodID for java.lang.Object.equals(Object) fails as
231 // the method is not static
232 method = env_->GetStaticMethodID(jlobject, "equals", "(Ljava/lang/Object;)Z");
233 EXPECT_EQ(static_cast<jmethodID>(NULL), method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700234 EXPECT_EXCEPTION(jlnsme);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700235
236 // Check that java.lang.String.valueOf(int) does exist
Ian Rogers4dd71f12011-08-16 14:16:02 -0700237 jclass jlstring = env_->FindClass("java/lang/String");
238 method = env_->GetStaticMethodID(jlstring, "valueOf",
239 "(I)Ljava/lang/String;");
240 EXPECT_NE(static_cast<jmethodID>(NULL), method);
241 EXPECT_FALSE(env_->ExceptionCheck());
Ian Rogers4dd71f12011-08-16 14:16:02 -0700242}
243
Elliott Hughescdf53122011-08-19 15:46:09 -0700244TEST_F(JniInternalTest, FromReflectedField_ToReflectedField) {
245 jclass jlrField = env_->FindClass("java/lang/reflect/Field");
246 jclass c = env_->FindClass("java/lang/String");
247 ASSERT_TRUE(c != NULL);
248 jfieldID fid = env_->GetFieldID(c, "count", "I");
249 ASSERT_TRUE(fid != NULL);
250 // Turn the fid into a java.lang.reflect.Field...
251 jobject field = env_->ToReflectedField(c, fid, JNI_FALSE);
252 ASSERT_TRUE(c != NULL);
253 ASSERT_TRUE(env_->IsInstanceOf(field, jlrField));
254 // ...and back again.
255 jfieldID fid2 = env_->FromReflectedField(field);
256 ASSERT_TRUE(fid2 != NULL);
257}
258
259TEST_F(JniInternalTest, FromReflectedMethod_ToReflectedMethod) {
260 jclass jlrMethod = env_->FindClass("java/lang/reflect/Method");
261 jclass c = env_->FindClass("java/lang/String");
262 ASSERT_TRUE(c != NULL);
263 jmethodID mid = env_->GetMethodID(c, "length", "()I");
264 ASSERT_TRUE(mid != NULL);
265 // Turn the mid into a java.lang.reflect.Method...
266 jobject method = env_->ToReflectedMethod(c, mid, JNI_FALSE);
267 ASSERT_TRUE(c != NULL);
268 ASSERT_TRUE(env_->IsInstanceOf(method, jlrMethod));
269 // ...and back again.
270 jmethodID mid2 = env_->FromReflectedMethod(method);
271 ASSERT_TRUE(mid2 != NULL);
272}
273
Elliott Hughes5174fe62011-08-23 15:12:35 -0700274void BogusMethod() {
275 // You can't pass NULL function pointers to RegisterNatives.
276}
277
Ian Rogers4dd71f12011-08-16 14:16:02 -0700278TEST_F(JniInternalTest, RegisterNatives) {
279 jclass jlobject = env_->FindClass("java/lang/Object");
280 jclass jlnsme = env_->FindClass("java/lang/NoSuchMethodError");
281
282 // Sanity check that no exceptions are pending
Elliott Hughescdf53122011-08-19 15:46:09 -0700283 ASSERT_FALSE(env_->ExceptionCheck());
Ian Rogers4dd71f12011-08-16 14:16:02 -0700284
285 // Check that registering to a non-existent java.lang.Object.foo() causes a
286 // NoSuchMethodError
287 {
288 JNINativeMethod methods[] = {{"foo", "()V", NULL}};
289 env_->RegisterNatives(jlobject, methods, 1);
290 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700291 EXPECT_EXCEPTION(jlnsme);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700292
293 // Check that registering non-native methods causes a NoSuchMethodError
294 {
295 JNINativeMethod methods[] = {{"equals", "(Ljava/lang/Object;)Z", NULL}};
296 env_->RegisterNatives(jlobject, methods, 1);
297 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700298 EXPECT_EXCEPTION(jlnsme);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700299
300 // Check that registering native methods is successful
301 {
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700302 JNINativeMethod methods[] = {{"getClass", "()Ljava/lang/Class;", reinterpret_cast<void*>(BogusMethod)}};
Ian Rogers4dd71f12011-08-16 14:16:02 -0700303 env_->RegisterNatives(jlobject, methods, 1);
304 }
305 EXPECT_FALSE(env_->ExceptionCheck());
Elliott Hughes5174fe62011-08-23 15:12:35 -0700306
307 env_->UnregisterNatives(jlobject);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700308}
309
Elliott Hughes75770752011-08-24 17:52:38 -0700310#define EXPECT_PRIMITIVE_ARRAY(new_fn, get_region_fn, set_region_fn, get_elements_fn, release_elements_fn, scalar_type, expected_class_descriptor) \
Elliott Hughes814e4032011-08-23 12:07:56 -0700311 jsize size = 4; \
312 /* Allocate an array and check it has the right type and length. */ \
313 scalar_type ## Array a = env_->new_fn(size); \
314 EXPECT_TRUE(a != NULL); \
315 EXPECT_TRUE(env_->IsInstanceOf(a, env_->FindClass(expected_class_descriptor))); \
316 EXPECT_EQ(size, env_->GetArrayLength(a)); \
317 /* AIOOBE for negative start offset. */ \
318 env_->get_region_fn(a, -1, 1, NULL); \
319 EXPECT_EXCEPTION(aioobe_); \
320 env_->set_region_fn(a, -1, 1, NULL); \
321 EXPECT_EXCEPTION(aioobe_); \
322 /* AIOOBE for negative length. */ \
323 env_->get_region_fn(a, 0, -1, NULL); \
324 EXPECT_EXCEPTION(aioobe_); \
325 env_->set_region_fn(a, 0, -1, NULL); \
326 EXPECT_EXCEPTION(aioobe_); \
327 /* AIOOBE for buffer overrun. */ \
328 env_->get_region_fn(a, size - 1, size, NULL); \
329 EXPECT_EXCEPTION(aioobe_); \
330 env_->set_region_fn(a, size - 1, size, NULL); \
331 EXPECT_EXCEPTION(aioobe_); \
332 /* Prepare a couple of buffers. */ \
333 scalar_type src_buf[size]; \
334 scalar_type dst_buf[size]; \
335 for (jsize i = 0; i < size; ++i) { src_buf[i] = scalar_type(i); } \
336 for (jsize i = 0; i < size; ++i) { dst_buf[i] = scalar_type(-1); } \
337 /* Copy all of src_buf onto the heap. */ \
338 env_->set_region_fn(a, 0, size, src_buf); \
339 /* Copy back only part. */ \
340 env_->get_region_fn(a, 1, size - 2, &dst_buf[1]); \
341 EXPECT_FALSE(memcmp(src_buf, dst_buf, sizeof(src_buf)) == 0) << "short copy equal"; \
342 /* Copy the missing pieces. */ \
343 env_->get_region_fn(a, 0, 1, dst_buf); \
344 env_->get_region_fn(a, size - 1, 1, &dst_buf[size - 1]); \
345 EXPECT_TRUE(memcmp(src_buf, dst_buf, sizeof(src_buf)) == 0) << "fixed copy not equal"; \
346 /* Copy back the whole array. */ \
347 env_->get_region_fn(a, 0, size, dst_buf); \
Elliott Hughes75770752011-08-24 17:52:38 -0700348 EXPECT_TRUE(memcmp(src_buf, dst_buf, sizeof(src_buf)) == 0) << "full copy not equal"; \
349 /* GetPrimitiveArrayCritical */ \
350 void* v = env_->GetPrimitiveArrayCritical(a, NULL); \
351 EXPECT_TRUE(memcmp(src_buf, v, sizeof(src_buf)) == 0) << "GetPrimitiveArrayCritical not equal"; \
352 env_->ReleasePrimitiveArrayCritical(a, v, 0); \
353 /* GetXArrayElements */ \
354 scalar_type* xs = env_->get_elements_fn(a, NULL); \
355 EXPECT_TRUE(memcmp(src_buf, xs, sizeof(src_buf)) == 0) << # get_elements_fn " not equal"; \
356 env_->release_elements_fn(a, xs, 0); \
357 EXPECT_EQ(reinterpret_cast<uintptr_t>(v), reinterpret_cast<uintptr_t>(xs))
Elliott Hughesbd935992011-08-22 11:59:34 -0700358
Elliott Hughes814e4032011-08-23 12:07:56 -0700359TEST_F(JniInternalTest, BooleanArrays) {
Elliott Hughes75770752011-08-24 17:52:38 -0700360 EXPECT_PRIMITIVE_ARRAY(NewBooleanArray, GetBooleanArrayRegion, SetBooleanArrayRegion, GetBooleanArrayElements, ReleaseBooleanArrayElements, jboolean, "[Z");
Elliott Hughes814e4032011-08-23 12:07:56 -0700361}
362TEST_F(JniInternalTest, ByteArrays) {
Elliott Hughes75770752011-08-24 17:52:38 -0700363 EXPECT_PRIMITIVE_ARRAY(NewByteArray, GetByteArrayRegion, SetByteArrayRegion, GetByteArrayElements, ReleaseByteArrayElements, jbyte, "[B");
Elliott Hughes814e4032011-08-23 12:07:56 -0700364}
365TEST_F(JniInternalTest, CharArrays) {
Elliott Hughes75770752011-08-24 17:52:38 -0700366 EXPECT_PRIMITIVE_ARRAY(NewCharArray, GetCharArrayRegion, SetCharArrayRegion, GetCharArrayElements, ReleaseCharArrayElements, jchar, "[C");
Elliott Hughes814e4032011-08-23 12:07:56 -0700367}
368TEST_F(JniInternalTest, DoubleArrays) {
Elliott Hughes75770752011-08-24 17:52:38 -0700369 EXPECT_PRIMITIVE_ARRAY(NewDoubleArray, GetDoubleArrayRegion, SetDoubleArrayRegion, GetDoubleArrayElements, ReleaseDoubleArrayElements, jdouble, "[D");
Elliott Hughes814e4032011-08-23 12:07:56 -0700370}
371TEST_F(JniInternalTest, FloatArrays) {
Elliott Hughes75770752011-08-24 17:52:38 -0700372 EXPECT_PRIMITIVE_ARRAY(NewFloatArray, GetFloatArrayRegion, SetFloatArrayRegion, GetFloatArrayElements, ReleaseFloatArrayElements, jfloat, "[F");
Elliott Hughes814e4032011-08-23 12:07:56 -0700373}
374TEST_F(JniInternalTest, IntArrays) {
Elliott Hughes75770752011-08-24 17:52:38 -0700375 EXPECT_PRIMITIVE_ARRAY(NewIntArray, GetIntArrayRegion, SetIntArrayRegion, GetIntArrayElements, ReleaseIntArrayElements, jint, "[I");
Elliott Hughes814e4032011-08-23 12:07:56 -0700376}
377TEST_F(JniInternalTest, LongArrays) {
Elliott Hughes75770752011-08-24 17:52:38 -0700378 EXPECT_PRIMITIVE_ARRAY(NewLongArray, GetLongArrayRegion, SetLongArrayRegion, GetLongArrayElements, ReleaseLongArrayElements, jlong, "[J");
Elliott Hughes814e4032011-08-23 12:07:56 -0700379}
380TEST_F(JniInternalTest, ShortArrays) {
Elliott Hughes75770752011-08-24 17:52:38 -0700381 EXPECT_PRIMITIVE_ARRAY(NewShortArray, GetShortArrayRegion, SetShortArrayRegion, GetShortArrayElements, ReleaseShortArrayElements, jshort, "[S");
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700382}
383
Elliott Hughesf2682d52011-08-15 16:37:04 -0700384TEST_F(JniInternalTest, NewObjectArray) {
385 // TODO: death tests for negative array sizes.
386
Elliott Hughesf2682d52011-08-15 16:37:04 -0700387 // TODO: check non-NULL initial elements.
388
Elliott Hughesbd935992011-08-22 11:59:34 -0700389 jclass element_class = env_->FindClass("java/lang/String");
390 ASSERT_TRUE(element_class != NULL);
391 jclass array_class = env_->FindClass("[Ljava/lang/String;");
392 ASSERT_TRUE(array_class != NULL);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700393
Elliott Hughesbd935992011-08-22 11:59:34 -0700394 jobjectArray a;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700395
Elliott Hughesbd935992011-08-22 11:59:34 -0700396 a = env_->NewObjectArray(0, element_class, NULL);
397 EXPECT_TRUE(a != NULL);
398 EXPECT_TRUE(env_->IsInstanceOf(a, array_class));
399 EXPECT_EQ(0, env_->GetArrayLength(a));
400
401 a = env_->NewObjectArray(1, element_class, NULL);
402 EXPECT_TRUE(a != NULL);
403 EXPECT_TRUE(env_->IsInstanceOf(a, array_class));
404 EXPECT_EQ(1, env_->GetArrayLength(a));
Elliott Hughes75770752011-08-24 17:52:38 -0700405 EXPECT_TRUE(env_->IsSameObject(env_->GetObjectArrayElement(a, 0), NULL));
406
407 jstring s = env_->NewStringUTF("poop");
408 a = env_->NewObjectArray(2, element_class, s);
409 EXPECT_TRUE(a != NULL);
410 EXPECT_TRUE(env_->IsInstanceOf(a, array_class));
411 EXPECT_EQ(2, env_->GetArrayLength(a));
412 EXPECT_TRUE(env_->IsSameObject(env_->GetObjectArrayElement(a, 0), s));
413 EXPECT_TRUE(env_->IsSameObject(env_->GetObjectArrayElement(a, 1), s));
Elliott Hughesbd935992011-08-22 11:59:34 -0700414}
415
416TEST_F(JniInternalTest, GetArrayLength) {
417 // Already tested in NewObjectArray/NewPrimitiveArray.
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700418}
419
Elliott Hughes37f7a402011-08-22 18:56:01 -0700420TEST_F(JniInternalTest, GetObjectClass) {
421 jclass string_class = env_->FindClass("java/lang/String");
422 ASSERT_TRUE(string_class != NULL);
423 jclass class_class = env_->FindClass("java/lang/Class");
424 ASSERT_TRUE(class_class != NULL);
425
426 jstring s = env_->NewStringUTF("poop");
427 jclass c = env_->GetObjectClass(s);
428 ASSERT_TRUE(env_->IsSameObject(string_class, c));
429
430 jclass c2 = env_->GetObjectClass(c);
431 ASSERT_TRUE(env_->IsSameObject(class_class, env_->GetObjectClass(c2)));
432}
433
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700434TEST_F(JniInternalTest, GetSuperclass) {
435 jclass object_class = env_->FindClass("java/lang/Object");
436 ASSERT_TRUE(object_class != NULL);
437 jclass string_class = env_->FindClass("java/lang/String");
438 ASSERT_TRUE(string_class != NULL);
439 ASSERT_TRUE(env_->IsSameObject(object_class, env_->GetSuperclass(string_class)));
440 ASSERT_TRUE(env_->GetSuperclass(object_class) == NULL);
441}
442
Elliott Hughes37f7a402011-08-22 18:56:01 -0700443TEST_F(JniInternalTest, IsAssignableFrom) {
444 jclass object_class = env_->FindClass("java/lang/Object");
445 ASSERT_TRUE(object_class != NULL);
446 jclass string_class = env_->FindClass("java/lang/String");
447 ASSERT_TRUE(string_class != NULL);
448
449 ASSERT_TRUE(env_->IsAssignableFrom(object_class, string_class));
450 ASSERT_FALSE(env_->IsAssignableFrom(string_class, object_class));
451}
452
Elliott Hughesb465ab02011-08-24 11:21:21 -0700453TEST_F(JniInternalTest, GetObjectRefType) {
454 jclass local = env_->FindClass("java/lang/Object");
455 ASSERT_TRUE(local != NULL);
456 EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(local));
457
458 jobject global = env_->NewGlobalRef(local);
459 EXPECT_EQ(JNIGlobalRefType, env_->GetObjectRefType(global));
460
461 jweak weak_global = env_->NewWeakGlobalRef(local);
462 EXPECT_EQ(JNIWeakGlobalRefType, env_->GetObjectRefType(weak_global));
463
464 jobject invalid = reinterpret_cast<jobject>(this);
465 EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(invalid));
466
467 // TODO: invoke a native method and test that its arguments are considered local references.
468}
469
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700470TEST_F(JniInternalTest, NewStringUTF) {
471 EXPECT_TRUE(env_->NewStringUTF(NULL) == NULL);
Elliott Hughes814e4032011-08-23 12:07:56 -0700472 jstring s;
473
474 s = env_->NewStringUTF("");
475 EXPECT_TRUE(s != NULL);
476 EXPECT_EQ(0, env_->GetStringLength(s));
477 EXPECT_EQ(0, env_->GetStringUTFLength(s));
478 s = env_->NewStringUTF("hello");
479 EXPECT_TRUE(s != NULL);
480 EXPECT_EQ(5, env_->GetStringLength(s));
481 EXPECT_EQ(5, env_->GetStringUTFLength(s));
482
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700483 // TODO: check some non-ASCII strings.
Elliott Hughesf2682d52011-08-15 16:37:04 -0700484}
485
Elliott Hughes814e4032011-08-23 12:07:56 -0700486TEST_F(JniInternalTest, NewString) {
487 EXPECT_TRUE(env_->NewString(NULL, 0) == NULL);
488
489 jchar chars[] = { 'h', 'i' };
490 jstring s;
491 s = env_->NewString(chars, 0);
492 EXPECT_TRUE(s != NULL);
493 EXPECT_EQ(0, env_->GetStringLength(s));
494 EXPECT_EQ(0, env_->GetStringUTFLength(s));
495 s = env_->NewString(chars, 2);
496 EXPECT_TRUE(s != NULL);
497 EXPECT_EQ(2, env_->GetStringLength(s));
498 EXPECT_EQ(2, env_->GetStringUTFLength(s));
499
500 // TODO: check some non-ASCII strings.
501}
502
Elliott Hughesb465ab02011-08-24 11:21:21 -0700503TEST_F(JniInternalTest, GetStringLength_GetStringUTFLength) {
504 // Already tested in the NewString/NewStringUTF tests.
505}
506
507TEST_F(JniInternalTest, GetStringRegion_GetStringUTFRegion) {
508 jstring s = env_->NewStringUTF("hello");
509 ASSERT_TRUE(s != NULL);
510
511 env_->GetStringRegion(s, -1, 0, NULL);
512 EXPECT_EXCEPTION(sioobe_);
513 env_->GetStringRegion(s, 0, -1, NULL);
514 EXPECT_EXCEPTION(sioobe_);
515 env_->GetStringRegion(s, 0, 10, NULL);
516 EXPECT_EXCEPTION(sioobe_);
517 env_->GetStringRegion(s, 10, 1, NULL);
518 EXPECT_EXCEPTION(sioobe_);
519
520 jchar chars[4] = { 'x', 'x', 'x', 'x' };
521 env_->GetStringRegion(s, 1, 2, &chars[1]);
522 EXPECT_EQ('x', chars[0]);
523 EXPECT_EQ('e', chars[1]);
524 EXPECT_EQ('l', chars[2]);
525 EXPECT_EQ('x', chars[3]);
526
527 env_->GetStringUTFRegion(s, -1, 0, NULL);
528 EXPECT_EXCEPTION(sioobe_);
529 env_->GetStringUTFRegion(s, 0, -1, NULL);
530 EXPECT_EXCEPTION(sioobe_);
531 env_->GetStringUTFRegion(s, 0, 10, NULL);
532 EXPECT_EXCEPTION(sioobe_);
533 env_->GetStringUTFRegion(s, 10, 1, NULL);
534 EXPECT_EXCEPTION(sioobe_);
535
536 char bytes[4] = { 'x', 'x', 'x', 'x' };
537 env_->GetStringUTFRegion(s, 1, 2, &bytes[1]);
538 EXPECT_EQ('x', bytes[0]);
539 EXPECT_EQ('e', bytes[1]);
540 EXPECT_EQ('l', bytes[2]);
541 EXPECT_EQ('x', bytes[3]);
542}
543
Elliott Hughes75770752011-08-24 17:52:38 -0700544TEST_F(JniInternalTest, GetStringUTFChars_ReleaseStringUTFChars) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700545 vm_->check_jni_abort_hook = TestCheckJniAbortHook;
546 // Passing in a NULL jstring is ignored normally, but caught by -Xcheck:jni.
Elliott Hughes75770752011-08-24 17:52:38 -0700547 EXPECT_TRUE(env_->GetStringUTFChars(NULL, NULL) == NULL);
Elliott Hughesa2501992011-08-26 19:39:54 -0700548 vm_->check_jni_abort_hook = NULL;
Elliott Hughes75770752011-08-24 17:52:38 -0700549
550 jstring s = env_->NewStringUTF("hello");
551 ASSERT_TRUE(s != NULL);
552
553 const char* utf = env_->GetStringUTFChars(s, NULL);
554 EXPECT_STREQ("hello", utf);
555 env_->ReleaseStringUTFChars(s, utf);
556
557 jboolean is_copy = JNI_FALSE;
558 utf = env_->GetStringUTFChars(s, &is_copy);
559 EXPECT_EQ(JNI_TRUE, is_copy);
560 EXPECT_STREQ("hello", utf);
561 env_->ReleaseStringUTFChars(s, utf);
562}
563
564TEST_F(JniInternalTest, GetStringChars_ReleaseStringChars) {
565 jstring s = env_->NewStringUTF("hello");
566 ASSERT_TRUE(s != NULL);
567
568 jchar expected[] = { 'h', 'e', 'l', 'l', 'o' };
569 const jchar* chars = env_->GetStringChars(s, NULL);
570 EXPECT_EQ(expected[0], chars[0]);
571 EXPECT_EQ(expected[1], chars[1]);
572 EXPECT_EQ(expected[2], chars[2]);
573 EXPECT_EQ(expected[3], chars[3]);
574 EXPECT_EQ(expected[4], chars[4]);
575 env_->ReleaseStringChars(s, chars);
576
577 jboolean is_copy = JNI_FALSE;
578 chars = env_->GetStringChars(s, &is_copy);
579 EXPECT_EQ(JNI_FALSE, is_copy);
580 EXPECT_EQ(expected[0], chars[0]);
581 EXPECT_EQ(expected[1], chars[1]);
582 EXPECT_EQ(expected[2], chars[2]);
583 EXPECT_EQ(expected[3], chars[3]);
584 EXPECT_EQ(expected[4], chars[4]);
585 env_->ReleaseStringChars(s, chars);
586}
587
588TEST_F(JniInternalTest, GetStringCritical_ReleaseStringCritical) {
589 jstring s = env_->NewStringUTF("hello");
590 ASSERT_TRUE(s != NULL);
591
592 jchar expected[] = { 'h', 'e', 'l', 'l', 'o' };
593 const jchar* chars = env_->GetStringCritical(s, NULL);
594 EXPECT_EQ(expected[0], chars[0]);
595 EXPECT_EQ(expected[1], chars[1]);
596 EXPECT_EQ(expected[2], chars[2]);
597 EXPECT_EQ(expected[3], chars[3]);
598 EXPECT_EQ(expected[4], chars[4]);
599 env_->ReleaseStringCritical(s, chars);
600
601 jboolean is_copy = JNI_FALSE;
602 chars = env_->GetStringCritical(s, &is_copy);
603 EXPECT_EQ(JNI_FALSE, is_copy);
604 EXPECT_EQ(expected[0], chars[0]);
605 EXPECT_EQ(expected[1], chars[1]);
606 EXPECT_EQ(expected[2], chars[2]);
607 EXPECT_EQ(expected[3], chars[3]);
608 EXPECT_EQ(expected[4], chars[4]);
609 env_->ReleaseStringCritical(s, chars);
610}
611
Elliott Hughes814e4032011-08-23 12:07:56 -0700612TEST_F(JniInternalTest, GetObjectArrayElement_SetObjectArrayElement) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700613 jclass c = env_->FindClass("java/lang/Object");
Elliott Hughes289da822011-08-16 10:11:20 -0700614 ASSERT_TRUE(c != NULL);
615
616 jobjectArray array = env_->NewObjectArray(1, c, NULL);
617 EXPECT_TRUE(array != NULL);
Elliott Hughes814e4032011-08-23 12:07:56 -0700618 EXPECT_TRUE(env_->GetObjectArrayElement(array, 0) == NULL);
Elliott Hughes289da822011-08-16 10:11:20 -0700619 env_->SetObjectArrayElement(array, 0, c);
Elliott Hughes814e4032011-08-23 12:07:56 -0700620 EXPECT_TRUE(env_->IsSameObject(env_->GetObjectArrayElement(array, 0), c));
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700621
622 // ArrayIndexOutOfBounds for negative index.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700623 env_->SetObjectArrayElement(array, -1, c);
Elliott Hughes814e4032011-08-23 12:07:56 -0700624 EXPECT_EXCEPTION(aioobe_);
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700625
626 // ArrayIndexOutOfBounds for too-large index.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700627 env_->SetObjectArrayElement(array, 1, c);
Elliott Hughes814e4032011-08-23 12:07:56 -0700628 EXPECT_EXCEPTION(aioobe_);
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700629
Elliott Hughes289da822011-08-16 10:11:20 -0700630 // TODO: check ArrayStoreException thrown for bad types.
631}
632
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700633#define EXPECT_STATIC_PRIMITIVE_FIELD(type, field_name, sig, value1, value2) \
634 do { \
635 jfieldID fid = env_->GetStaticFieldID(c, field_name, sig); \
636 EXPECT_TRUE(fid != NULL); \
637 env_->SetStatic ## type ## Field(c, fid, value1); \
638 EXPECT_EQ(value1, env_->GetStatic ## type ## Field(c, fid)); \
639 env_->SetStatic ## type ## Field(c, fid, value2); \
640 EXPECT_EQ(value2, env_->GetStatic ## type ## Field(c, fid)); \
641 } while (false)
642
643#define EXPECT_PRIMITIVE_FIELD(instance, type, field_name, sig, value1, value2) \
644 do { \
645 jfieldID fid = env_->GetFieldID(c, field_name, sig); \
646 EXPECT_TRUE(fid != NULL); \
647 env_->Set ## type ## Field(instance, fid, value1); \
648 EXPECT_EQ(value1, env_->Get ## type ## Field(instance, fid)); \
649 env_->Set ## type ## Field(instance, fid, value2); \
650 EXPECT_EQ(value2, env_->Get ## type ## Field(instance, fid)); \
651 } while (false)
652
653
654TEST_F(JniInternalTest, GetPrimitiveField_SetPrimitiveField) {
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700655 LoadDex("AllFields");
Brian Carlstrom25c33252011-09-18 15:58:35 -0700656 runtime_->Start();
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700657
658 jclass c = env_->FindClass("AllFields");
659 ASSERT_TRUE(c != NULL);
660 jobject o = env_->AllocObject(c);
661 ASSERT_TRUE(o != NULL);
662
663 EXPECT_STATIC_PRIMITIVE_FIELD(Boolean, "sZ", "Z", true, false);
664 EXPECT_STATIC_PRIMITIVE_FIELD(Byte, "sB", "B", 1, 2);
665 EXPECT_STATIC_PRIMITIVE_FIELD(Char, "sC", "C", 'a', 'b');
666 EXPECT_STATIC_PRIMITIVE_FIELD(Double, "sD", "D", 1.0, 2.0);
667 EXPECT_STATIC_PRIMITIVE_FIELD(Float, "sF", "F", 1.0, 2.0);
668 EXPECT_STATIC_PRIMITIVE_FIELD(Int, "sI", "I", 1, 2);
669 EXPECT_STATIC_PRIMITIVE_FIELD(Long, "sJ", "J", 1, 2);
670 EXPECT_STATIC_PRIMITIVE_FIELD(Short, "sS", "S", 1, 2);
671
672 EXPECT_PRIMITIVE_FIELD(o, Boolean, "iZ", "Z", true, false);
673 EXPECT_PRIMITIVE_FIELD(o, Byte, "iB", "B", 1, 2);
674 EXPECT_PRIMITIVE_FIELD(o, Char, "iC", "C", 'a', 'b');
675 EXPECT_PRIMITIVE_FIELD(o, Double, "iD", "D", 1.0, 2.0);
676 EXPECT_PRIMITIVE_FIELD(o, Float, "iF", "F", 1.0, 2.0);
677 EXPECT_PRIMITIVE_FIELD(o, Int, "iI", "I", 1, 2);
678 EXPECT_PRIMITIVE_FIELD(o, Long, "iJ", "J", 1, 2);
679 EXPECT_PRIMITIVE_FIELD(o, Short, "iS", "S", 1, 2);
680}
681
682TEST_F(JniInternalTest, GetObjectField_SetObjectField) {
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700683 LoadDex("AllFields");
Brian Carlstrom25c33252011-09-18 15:58:35 -0700684 runtime_->Start();
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700685
686 jclass c = env_->FindClass("AllFields");
687 ASSERT_TRUE(c != NULL);
688 jobject o = env_->AllocObject(c);
689 ASSERT_TRUE(o != NULL);
690
691 jstring s1 = env_->NewStringUTF("hello");
692 ASSERT_TRUE(s1 != NULL);
693 jstring s2 = env_->NewStringUTF("world");
694 ASSERT_TRUE(s2 != NULL);
695
696 jfieldID s_fid = env_->GetStaticFieldID(c, "sObject", "Ljava/lang/Object;");
697 ASSERT_TRUE(s_fid != NULL);
698 jfieldID i_fid = env_->GetFieldID(c, "iObject", "Ljava/lang/Object;");
699 ASSERT_TRUE(i_fid != NULL);
700
701 env_->SetStaticObjectField(c, s_fid, s1);
702 ASSERT_TRUE(env_->IsSameObject(s1, env_->GetStaticObjectField(c, s_fid)));
703 env_->SetStaticObjectField(c, s_fid, s2);
704 ASSERT_TRUE(env_->IsSameObject(s2, env_->GetStaticObjectField(c, s_fid)));
705
706 env_->SetObjectField(o, i_fid, s1);
707 ASSERT_TRUE(env_->IsSameObject(s1, env_->GetObjectField(o, i_fid)));
708 env_->SetObjectField(o, i_fid, s2);
709 ASSERT_TRUE(env_->IsSameObject(s2, env_->GetObjectField(o, i_fid)));
710}
711
Elliott Hughes18c07532011-08-18 15:50:51 -0700712TEST_F(JniInternalTest, NewLocalRef_NULL) {
713 EXPECT_TRUE(env_->NewLocalRef(NULL) == NULL);
714}
715
716TEST_F(JniInternalTest, NewLocalRef) {
717 jstring s = env_->NewStringUTF("");
718 ASSERT_TRUE(s != NULL);
719 jobject o = env_->NewLocalRef(s);
720 EXPECT_TRUE(o != NULL);
721 EXPECT_TRUE(o != s);
722
723 // TODO: check that o is a local reference.
724}
725
726TEST_F(JniInternalTest, DeleteLocalRef_NULL) {
727 env_->DeleteLocalRef(NULL);
728}
729
730TEST_F(JniInternalTest, DeleteLocalRef) {
731 jstring s = env_->NewStringUTF("");
732 ASSERT_TRUE(s != NULL);
733 env_->DeleteLocalRef(s);
734
735 // Currently, deleting an already-deleted reference is just a warning.
Elliott Hughesa2501992011-08-26 19:39:54 -0700736 vm_->check_jni_abort_hook = TestCheckJniAbortHook;
Elliott Hughes18c07532011-08-18 15:50:51 -0700737 env_->DeleteLocalRef(s);
Elliott Hughesa2501992011-08-26 19:39:54 -0700738 vm_->check_jni_abort_hook = NULL;
Elliott Hughes18c07532011-08-18 15:50:51 -0700739
740 s = env_->NewStringUTF("");
741 ASSERT_TRUE(s != NULL);
742 jobject o = env_->NewLocalRef(s);
743 ASSERT_TRUE(o != NULL);
744
745 env_->DeleteLocalRef(s);
746 env_->DeleteLocalRef(o);
747}
748
749TEST_F(JniInternalTest, NewGlobalRef_NULL) {
750 EXPECT_TRUE(env_->NewGlobalRef(NULL) == NULL);
751}
752
753TEST_F(JniInternalTest, NewGlobalRef) {
754 jstring s = env_->NewStringUTF("");
755 ASSERT_TRUE(s != NULL);
756 jobject o = env_->NewGlobalRef(s);
757 EXPECT_TRUE(o != NULL);
758 EXPECT_TRUE(o != s);
759
760 // TODO: check that o is a global reference.
761}
762
763TEST_F(JniInternalTest, DeleteGlobalRef_NULL) {
764 env_->DeleteGlobalRef(NULL);
765}
766
767TEST_F(JniInternalTest, DeleteGlobalRef) {
768 jstring s = env_->NewStringUTF("");
769 ASSERT_TRUE(s != NULL);
770
771 jobject o = env_->NewGlobalRef(s);
772 ASSERT_TRUE(o != NULL);
773 env_->DeleteGlobalRef(o);
774
Elliott Hughesa2501992011-08-26 19:39:54 -0700775 vm_->check_jni_abort_hook = TestCheckJniAbortHook;
Elliott Hughes18c07532011-08-18 15:50:51 -0700776 // Currently, deleting an already-deleted reference is just a warning.
777 env_->DeleteGlobalRef(o);
Elliott Hughesa2501992011-08-26 19:39:54 -0700778 vm_->check_jni_abort_hook = NULL;
Elliott Hughes18c07532011-08-18 15:50:51 -0700779
780 jobject o1 = env_->NewGlobalRef(s);
781 ASSERT_TRUE(o1 != NULL);
782 jobject o2 = env_->NewGlobalRef(s);
783 ASSERT_TRUE(o2 != NULL);
784
785 env_->DeleteGlobalRef(o1);
786 env_->DeleteGlobalRef(o2);
787}
788
789TEST_F(JniInternalTest, NewWeakGlobalRef_NULL) {
790 EXPECT_TRUE(env_->NewWeakGlobalRef(NULL) == NULL);
791}
792
793TEST_F(JniInternalTest, NewWeakGlobalRef) {
794 jstring s = env_->NewStringUTF("");
795 ASSERT_TRUE(s != NULL);
796 jobject o = env_->NewWeakGlobalRef(s);
797 EXPECT_TRUE(o != NULL);
798 EXPECT_TRUE(o != s);
799
800 // TODO: check that o is a weak global reference.
801}
802
803TEST_F(JniInternalTest, DeleteWeakGlobalRef_NULL) {
804 env_->DeleteWeakGlobalRef(NULL);
805}
806
807TEST_F(JniInternalTest, DeleteWeakGlobalRef) {
808 jstring s = env_->NewStringUTF("");
809 ASSERT_TRUE(s != NULL);
810
811 jobject o = env_->NewWeakGlobalRef(s);
812 ASSERT_TRUE(o != NULL);
813 env_->DeleteWeakGlobalRef(o);
814
Elliott Hughesa2501992011-08-26 19:39:54 -0700815 vm_->check_jni_abort_hook = TestCheckJniAbortHook;
Elliott Hughes18c07532011-08-18 15:50:51 -0700816 // Currently, deleting an already-deleted reference is just a warning.
817 env_->DeleteWeakGlobalRef(o);
Elliott Hughesa2501992011-08-26 19:39:54 -0700818 vm_->check_jni_abort_hook = NULL;
Elliott Hughes18c07532011-08-18 15:50:51 -0700819
820 jobject o1 = env_->NewWeakGlobalRef(s);
821 ASSERT_TRUE(o1 != NULL);
822 jobject o2 = env_->NewWeakGlobalRef(s);
823 ASSERT_TRUE(o2 != NULL);
824
825 env_->DeleteWeakGlobalRef(o1);
826 env_->DeleteWeakGlobalRef(o2);
827}
828
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700829#if defined(__arm__)
830TEST_F(JniInternalTest, StaticMainMethod) {
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700831 const ClassLoader* class_loader = LoadDex("Main");
832 CompileDirectMethod(class_loader, "Main", "main", "([Ljava/lang/String;)V");
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700833
834 Class* klass = class_linker_->FindClass("LMain;", class_loader);
835 ASSERT_TRUE(klass != NULL);
836
837 Method* method = klass->FindDirectMethod("main", "([Ljava/lang/String;)V");
838 ASSERT_TRUE(method != NULL);
839
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700840 Method::InvokeStub* stub = method->GetInvokeStub();
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700841
842 Object* arg = NULL;
843
Ian Rogersff1ed472011-09-20 13:46:24 -0700844 (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(&arg), NULL);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700845}
846
847TEST_F(JniInternalTest, StaticNopMethod) {
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700848 const ClassLoader* class_loader = LoadDex("StaticLeafMethods");
849 CompileDirectMethod(class_loader, "StaticLeafMethods", "nop", "()V");
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700850
851 Class* klass = class_linker_->FindClass("LStaticLeafMethods;", class_loader);
852 ASSERT_TRUE(klass != NULL);
853
854 Method* method = klass->FindDirectMethod("nop", "()V");
855 ASSERT_TRUE(method != NULL);
856
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700857 Method::InvokeStub* stub = method->GetInvokeStub();
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700858
Ian Rogersff1ed472011-09-20 13:46:24 -0700859 (*stub)(method, NULL, Thread::Current(), NULL, NULL);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700860}
861
862TEST_F(JniInternalTest, StaticIdentityByteMethod) {
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700863 const ClassLoader* class_loader = LoadDex("StaticLeafMethods");
864 CompileDirectMethod(class_loader, "StaticLeafMethods", "identity", "(B)B");
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700865
866 Class* klass = class_linker_->FindClass("LStaticLeafMethods;", class_loader);
867 ASSERT_TRUE(klass != NULL);
868
869 Method* method = klass->FindDirectMethod("identity", "(B)B");
870 ASSERT_TRUE(method != NULL);
871
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700872 Method::InvokeStub* stub = method->GetInvokeStub();
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700873
874 int arg;
875 JValue result;
876
877 arg = 0;
878 result.b = -1;
Ian Rogersff1ed472011-09-20 13:46:24 -0700879 (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(&arg), &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700880 EXPECT_EQ(0, result.b);
881
882 arg = -1;
883 result.b = 0;
Ian Rogersff1ed472011-09-20 13:46:24 -0700884 (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(&arg), &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700885 EXPECT_EQ(-1, result.b);
886
887 arg = SCHAR_MAX;
888 result.b = 0;
Ian Rogersff1ed472011-09-20 13:46:24 -0700889 (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(&arg), &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700890 EXPECT_EQ(SCHAR_MAX, result.b);
891
892 arg = SCHAR_MIN;
893 result.b = 0;
Ian Rogersff1ed472011-09-20 13:46:24 -0700894 (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(&arg), &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700895 EXPECT_EQ(SCHAR_MIN, result.b);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700896}
897
898TEST_F(JniInternalTest, StaticIdentityIntMethod) {
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700899 const ClassLoader* class_loader = LoadDex("StaticLeafMethods");
900 CompileDirectMethod(class_loader, "StaticLeafMethods", "identity", "(I)I");
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700901
902 Class* klass = class_linker_->FindClass("LStaticLeafMethods;", class_loader);
903 ASSERT_TRUE(klass != NULL);
904
905 Method* method = klass->FindDirectMethod("identity", "(I)I");
906 ASSERT_TRUE(method != NULL);
907
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700908 Method::InvokeStub* stub = method->GetInvokeStub();
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700909
910 int arg;
911 JValue result;
912
913 arg = 0;
914 result.i = -1;
Ian Rogersff1ed472011-09-20 13:46:24 -0700915 (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(&arg), &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700916 EXPECT_EQ(0, result.i);
917
918 arg = -1;
919 result.i = 0;
Ian Rogersff1ed472011-09-20 13:46:24 -0700920 (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(&arg), &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700921 EXPECT_EQ(-1, result.i);
922
923 arg = INT_MAX;
924 result.i = 0;
Ian Rogersff1ed472011-09-20 13:46:24 -0700925 (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(&arg), &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700926 EXPECT_EQ(INT_MAX, result.i);
927
928 arg = INT_MIN;
929 result.i = 0;
Ian Rogersff1ed472011-09-20 13:46:24 -0700930 (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(&arg), &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700931 EXPECT_EQ(INT_MIN, result.i);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700932}
933
934TEST_F(JniInternalTest, StaticIdentityDoubleMethod) {
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700935 const ClassLoader* class_loader = LoadDex("StaticLeafMethods");
936 CompileDirectMethod(class_loader, "StaticLeafMethods", "identity", "(D)D");
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700937
938 Class* klass = class_linker_->FindClass("LStaticLeafMethods;", class_loader);
939 ASSERT_TRUE(klass != NULL);
940
941 Method* method = klass->FindDirectMethod("identity", "(D)D");
942 ASSERT_TRUE(method != NULL);
943
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700944 Method::InvokeStub* stub = method->GetInvokeStub();
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700945
946 double arg;
947 JValue result;
948
949 arg = 0.0;
950 result.d = -1.0;
Ian Rogersff1ed472011-09-20 13:46:24 -0700951 (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(&arg), &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700952 EXPECT_EQ(0.0, result.d);
953
954 arg = -1.0;
955 result.d = 0.0;
Ian Rogersff1ed472011-09-20 13:46:24 -0700956 (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(&arg), &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700957 EXPECT_EQ(-1.0, result.d);
958
959 arg = DBL_MAX;
960 result.d = 0.0;
Ian Rogersff1ed472011-09-20 13:46:24 -0700961 (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(&arg), &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700962 EXPECT_EQ(DBL_MAX, result.d);
963
964 arg = DBL_MIN;
965 result.d = 0.0;
Ian Rogersff1ed472011-09-20 13:46:24 -0700966 (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(&arg), &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700967 EXPECT_EQ(DBL_MIN, result.d);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700968}
969
970TEST_F(JniInternalTest, StaticSumIntIntMethod) {
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700971 const ClassLoader* class_loader = LoadDex("StaticLeafMethods");
972 CompileDirectMethod(class_loader, "StaticLeafMethods", "sum", "(II)I");
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700973
974 Class* klass = class_linker_->FindClass("LStaticLeafMethods;", class_loader);
975 ASSERT_TRUE(klass != NULL);
976
977 Method* method = klass->FindDirectMethod("sum", "(II)I");
978 ASSERT_TRUE(method != NULL);
979
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700980 Method::InvokeStub* stub = method->GetInvokeStub();
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700981
982 int args[2];
983 JValue result;
984
985 args[0] = 0;
986 args[1] = 0;
987 result.i = -1;
Ian Rogersff1ed472011-09-20 13:46:24 -0700988 (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700989 EXPECT_EQ(0, result.i);
990
991 args[0] = 1;
992 args[1] = 2;
993 result.i = 0;
Ian Rogersff1ed472011-09-20 13:46:24 -0700994 (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700995 EXPECT_EQ(3, result.i);
996
997 args[0] = -2;
998 args[1] = 5;
999 result.i = 0;
Ian Rogersff1ed472011-09-20 13:46:24 -07001000 (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001001 EXPECT_EQ(3, result.i);
1002
1003 args[0] = INT_MAX;
1004 args[1] = INT_MIN;
1005 result.i = 1234;
Ian Rogersff1ed472011-09-20 13:46:24 -07001006 (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001007 EXPECT_EQ(-1, result.i);
1008
1009 args[0] = INT_MAX;
1010 args[1] = INT_MAX;
1011 result.i = INT_MIN;
Ian Rogersff1ed472011-09-20 13:46:24 -07001012 (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001013 EXPECT_EQ(-2, result.i);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001014}
1015
1016TEST_F(JniInternalTest, StaticSumIntIntIntMethod) {
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -07001017 const ClassLoader* class_loader = LoadDex("StaticLeafMethods");
1018 CompileDirectMethod(class_loader, "StaticLeafMethods", "sum", "(III)I");
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001019
1020 Class* klass = class_linker_->FindClass("LStaticLeafMethods;", class_loader);
1021 ASSERT_TRUE(klass != NULL);
1022
1023 Method* method = klass->FindDirectMethod("sum", "(III)I");
1024 ASSERT_TRUE(method != NULL);
1025
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -07001026 Method::InvokeStub* stub = method->GetInvokeStub();
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001027
1028 int args[3];
1029 JValue result;
1030
1031 args[0] = 0;
1032 args[1] = 0;
1033 args[2] = 0;
1034 result.i = -1;
Ian Rogersff1ed472011-09-20 13:46:24 -07001035 (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001036 EXPECT_EQ(0, result.i);
1037
1038 args[0] = 1;
1039 args[1] = 2;
1040 args[2] = 3;
1041 result.i = 0;
Ian Rogersff1ed472011-09-20 13:46:24 -07001042 (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001043 EXPECT_EQ(6, result.i);
1044
1045 args[0] = -1;
1046 args[1] = 2;
1047 args[2] = -3;
1048 result.i = 0;
Ian Rogersff1ed472011-09-20 13:46:24 -07001049 (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001050 EXPECT_EQ(-2, result.i);
1051
1052 args[0] = INT_MAX;
1053 args[1] = INT_MIN;
1054 args[2] = INT_MAX;
1055 result.i = 1234;
Ian Rogersff1ed472011-09-20 13:46:24 -07001056 (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001057 EXPECT_EQ(2147483646, result.i);
1058
1059 args[0] = INT_MAX;
1060 args[1] = INT_MAX;
1061 args[2] = INT_MAX;
1062 result.i = INT_MIN;
Ian Rogersff1ed472011-09-20 13:46:24 -07001063 (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001064 EXPECT_EQ(2147483645, result.i);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001065}
1066
1067TEST_F(JniInternalTest, StaticSumIntIntIntIntMethod) {
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -07001068 const ClassLoader* class_loader = LoadDex("StaticLeafMethods");
1069 CompileDirectMethod(class_loader, "StaticLeafMethods", "sum", "(IIII)I");
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001070
1071 Class* klass = class_linker_->FindClass("LStaticLeafMethods;", class_loader);
1072 ASSERT_TRUE(klass != NULL);
1073
1074 Method* method = klass->FindDirectMethod("sum", "(IIII)I");
1075 ASSERT_TRUE(method != NULL);
1076
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -07001077 Method::InvokeStub* stub = method->GetInvokeStub();
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001078
1079 int args[4];
1080 JValue result;
1081
1082 args[0] = 0;
1083 args[1] = 0;
1084 args[2] = 0;
1085 args[3] = 0;
1086 result.i = -1;
Ian Rogersff1ed472011-09-20 13:46:24 -07001087 (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001088 EXPECT_EQ(0, result.i);
1089
1090 args[0] = 1;
1091 args[1] = 2;
1092 args[2] = 3;
1093 args[3] = 4;
1094 result.i = 0;
Ian Rogersff1ed472011-09-20 13:46:24 -07001095 (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001096 EXPECT_EQ(10, result.i);
1097
1098 args[0] = -1;
1099 args[1] = 2;
1100 args[2] = -3;
1101 args[3] = 4;
1102 result.i = 0;
Ian Rogersff1ed472011-09-20 13:46:24 -07001103 (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001104 EXPECT_EQ(2, result.i);
1105
1106 args[0] = INT_MAX;
1107 args[1] = INT_MIN;
1108 args[2] = INT_MAX;
1109 args[3] = INT_MIN;
1110 result.i = 1234;
Ian Rogersff1ed472011-09-20 13:46:24 -07001111 (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001112 EXPECT_EQ(-2, result.i);
1113
1114 args[0] = INT_MAX;
1115 args[1] = INT_MAX;
1116 args[2] = INT_MAX;
1117 args[3] = INT_MAX;
1118 result.i = INT_MIN;
Ian Rogersff1ed472011-09-20 13:46:24 -07001119 (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001120 EXPECT_EQ(-4, result.i);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001121}
1122
1123TEST_F(JniInternalTest, StaticSumIntIntIntIntIntMethod) {
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -07001124 const ClassLoader* class_loader = LoadDex("StaticLeafMethods");
1125 CompileDirectMethod(class_loader, "StaticLeafMethods", "sum", "(IIIII)I");
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001126
1127 Class* klass = class_linker_->FindClass("LStaticLeafMethods;", class_loader);
1128 ASSERT_TRUE(klass != NULL);
1129
1130 Method* method = klass->FindDirectMethod("sum", "(IIIII)I");
1131 ASSERT_TRUE(method != NULL);
1132
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -07001133 Method::InvokeStub* stub = method->GetInvokeStub();
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001134
1135 int args[5];
1136 JValue result;
1137
1138 args[0] = 0;
1139 args[1] = 0;
1140 args[2] = 0;
1141 args[3] = 0;
1142 args[4] = 0;
1143 result.i = -1.0;
Ian Rogersff1ed472011-09-20 13:46:24 -07001144 (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001145 EXPECT_EQ(0, result.i);
1146
1147 args[0] = 1;
1148 args[1] = 2;
1149 args[2] = 3;
1150 args[3] = 4;
1151 args[4] = 5;
1152 result.i = 0;
Ian Rogersff1ed472011-09-20 13:46:24 -07001153 (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001154 EXPECT_EQ(15, result.i);
1155
1156 args[0] = -1;
1157 args[1] = 2;
1158 args[2] = -3;
1159 args[3] = 4;
1160 args[4] = -5;
1161 result.i = 0;
Ian Rogersff1ed472011-09-20 13:46:24 -07001162 (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001163 EXPECT_EQ(-3, result.i);
1164
1165 args[0] = INT_MAX;
1166 args[1] = INT_MIN;
1167 args[2] = INT_MAX;
1168 args[3] = INT_MIN;
1169 args[4] = INT_MAX;
1170 result.i = 1234;
Ian Rogersff1ed472011-09-20 13:46:24 -07001171 (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001172 EXPECT_EQ(2147483645, result.i);
1173
1174 args[0] = INT_MAX;
1175 args[1] = INT_MAX;
1176 args[2] = INT_MAX;
1177 args[3] = INT_MAX;
1178 args[4] = INT_MAX;
1179 result.i = INT_MIN;
Ian Rogersff1ed472011-09-20 13:46:24 -07001180 (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001181 EXPECT_EQ(2147483643, result.i);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001182}
1183
1184TEST_F(JniInternalTest, StaticSumDoubleDoubleMethod) {
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -07001185 const ClassLoader* class_loader = LoadDex("StaticLeafMethods");
1186 CompileDirectMethod(class_loader, "StaticLeafMethods", "sum", "(DD)D");
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001187
1188 Class* klass = class_linker_->FindClass("LStaticLeafMethods;", class_loader);
1189 ASSERT_TRUE(klass != NULL);
1190
1191 Method* method = klass->FindDirectMethod("sum", "(DD)D");
1192 ASSERT_TRUE(method != NULL);
1193
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -07001194 Method::InvokeStub* stub = method->GetInvokeStub();
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001195
1196 double args[2];
1197 JValue result;
1198
1199 args[0] = 0.0;
1200 args[1] = 0.0;
1201 result.d = -1.0;
Ian Rogersff1ed472011-09-20 13:46:24 -07001202 (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001203 EXPECT_EQ(0.0, result.d);
1204
1205 args[0] = 1.0;
1206 args[1] = 2.0;
1207 result.d = 0.0;
Ian Rogersff1ed472011-09-20 13:46:24 -07001208 (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001209 EXPECT_EQ(3.0, result.d);
1210
1211 args[0] = 1.0;
1212 args[1] = -2.0;
1213 result.d = 0.0;
Ian Rogersff1ed472011-09-20 13:46:24 -07001214 (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001215 EXPECT_EQ(-1.0, result.d);
1216
1217 args[0] = DBL_MAX;
1218 args[1] = DBL_MIN;
1219 result.d = 0.0;
Ian Rogersff1ed472011-09-20 13:46:24 -07001220 (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001221 EXPECT_EQ(1.7976931348623157e308, result.d);
1222
1223 args[0] = DBL_MAX;
1224 args[1] = DBL_MAX;
1225 result.d = 0.0;
Ian Rogersff1ed472011-09-20 13:46:24 -07001226 (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001227 EXPECT_EQ(INFINITY, result.d);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001228}
1229
1230TEST_F(JniInternalTest, StaticSumDoubleDoubleDoubleMethod) {
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -07001231 const ClassLoader* class_loader = LoadDex("StaticLeafMethods");
1232 CompileDirectMethod(class_loader, "StaticLeafMethods", "sum", "(DDD)D");
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001233
1234 Class* klass = class_linker_->FindClass("LStaticLeafMethods;", class_loader);
1235 ASSERT_TRUE(klass != NULL);
1236
1237 Method* method = klass->FindDirectMethod("sum", "(DDD)D");
1238 ASSERT_TRUE(method != NULL);
1239
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -07001240 Method::InvokeStub* stub = method->GetInvokeStub();
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001241
1242 double args[3];
1243 JValue result;
1244
1245 args[0] = 0.0;
1246 args[1] = 0.0;
1247 args[2] = 0.0;
1248 result.d = -1.0;
Ian Rogersff1ed472011-09-20 13:46:24 -07001249 (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001250 EXPECT_EQ(0.0, result.d);
1251
1252 args[0] = 1.0;
1253 args[1] = 2.0;
1254 args[2] = 3.0;
1255 result.d = 0.0;
Ian Rogersff1ed472011-09-20 13:46:24 -07001256 (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001257 EXPECT_EQ(6.0, result.d);
1258
1259 args[0] = 1.0;
1260 args[1] = -2.0;
1261 args[2] = 3.0;
1262 result.d = 0.0;
Ian Rogersff1ed472011-09-20 13:46:24 -07001263 (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001264 EXPECT_EQ(2.0, result.d);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001265}
1266
1267TEST_F(JniInternalTest, StaticSumDoubleDoubleDoubleDoubleMethod) {
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -07001268 const ClassLoader* class_loader = LoadDex("StaticLeafMethods");
1269 CompileDirectMethod(class_loader, "StaticLeafMethods", "sum", "(DDDD)D");
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001270
1271 Class* klass = class_linker_->FindClass("LStaticLeafMethods;", class_loader);
1272 ASSERT_TRUE(klass != NULL);
1273
1274 Method* method = klass->FindDirectMethod("sum", "(DDDD)D");
1275 ASSERT_TRUE(method != NULL);
1276
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -07001277 Method::InvokeStub* stub = method->GetInvokeStub();
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001278
1279 double args[4];
1280 JValue result;
1281
1282 args[0] = 0.0;
1283 args[1] = 0.0;
1284 args[2] = 0.0;
1285 args[3] = 0.0;
1286 result.d = -1.0;
Ian Rogersff1ed472011-09-20 13:46:24 -07001287 (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001288 EXPECT_EQ(0.0, result.d);
1289
1290 args[0] = 1.0;
1291 args[1] = 2.0;
1292 args[2] = 3.0;
1293 args[3] = 4.0;
1294 result.d = 0.0;
Ian Rogersff1ed472011-09-20 13:46:24 -07001295 (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001296 EXPECT_EQ(10.0, result.d);
1297
1298 args[0] = 1.0;
1299 args[1] = -2.0;
1300 args[2] = 3.0;
1301 args[3] = -4.0;
1302 result.d = 0.0;
Ian Rogersff1ed472011-09-20 13:46:24 -07001303 (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001304 EXPECT_EQ(-2.0, result.d);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001305}
1306
1307TEST_F(JniInternalTest, StaticSumDoubleDoubleDoubleDoubleDoubleMethod) {
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -07001308 const ClassLoader* class_loader = LoadDex("StaticLeafMethods");
1309 CompileDirectMethod(class_loader, "StaticLeafMethods", "sum", "(DDDDD)D");
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001310
1311 Class* klass = class_linker_->FindClass("LStaticLeafMethods;", class_loader);
1312 ASSERT_TRUE(klass != NULL);
1313
1314 Method* method = klass->FindDirectMethod("sum", "(DDDDD)D");
1315 ASSERT_TRUE(method != NULL);
1316
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -07001317 Method::InvokeStub* stub = method->GetInvokeStub();
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001318
1319 double args[5];
1320 JValue result;
1321
1322 args[0] = 0.0;
1323 args[1] = 0.0;
1324 args[2] = 0.0;
1325 args[3] = 0.0;
1326 args[4] = 0.0;
1327 result.d = -1.0;
Ian Rogersff1ed472011-09-20 13:46:24 -07001328 (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001329 EXPECT_EQ(0.0, result.d);
1330
1331 args[0] = 1.0;
1332 args[1] = 2.0;
1333 args[2] = 3.0;
1334 args[3] = 4.0;
1335 args[4] = 5.0;
1336 result.d = 0.0;
Ian Rogersff1ed472011-09-20 13:46:24 -07001337 (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001338 EXPECT_EQ(15.0, result.d);
1339
1340 args[0] = 1.0;
1341 args[1] = -2.0;
1342 args[2] = 3.0;
1343 args[3] = -4.0;
1344 args[4] = 5.0;
1345 result.d = 0.0;
Ian Rogersff1ed472011-09-20 13:46:24 -07001346 (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001347 EXPECT_EQ(3.0, result.d);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001348}
1349#endif // __arm__
1350
Elliott Hughes37f7a402011-08-22 18:56:01 -07001351TEST_F(JniInternalTest, Throw) {
1352 EXPECT_EQ(JNI_ERR, env_->Throw(NULL));
1353
1354 jclass exception_class = env_->FindClass("java/lang/RuntimeException");
1355 ASSERT_TRUE(exception_class != NULL);
1356 jthrowable exception = reinterpret_cast<jthrowable>(env_->AllocObject(exception_class));
1357 ASSERT_TRUE(exception != NULL);
1358
1359 EXPECT_EQ(JNI_OK, env_->Throw(exception));
1360 EXPECT_TRUE(env_->ExceptionCheck());
Elliott Hughesa2501992011-08-26 19:39:54 -07001361 jthrowable thrown_exception = env_->ExceptionOccurred();
Elliott Hughes37f7a402011-08-22 18:56:01 -07001362 env_->ExceptionClear();
Elliott Hughesa2501992011-08-26 19:39:54 -07001363 EXPECT_TRUE(env_->IsSameObject(exception, thrown_exception));
Elliott Hughes37f7a402011-08-22 18:56:01 -07001364}
1365
1366TEST_F(JniInternalTest, ThrowNew) {
1367 EXPECT_EQ(JNI_ERR, env_->Throw(NULL));
1368
1369 jclass exception_class = env_->FindClass("java/lang/RuntimeException");
1370 ASSERT_TRUE(exception_class != NULL);
1371
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001372 jthrowable thrown_exception;
1373
Elliott Hughes37f7a402011-08-22 18:56:01 -07001374 EXPECT_EQ(JNI_OK, env_->ThrowNew(exception_class, "hello world"));
1375 EXPECT_TRUE(env_->ExceptionCheck());
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001376 thrown_exception = env_->ExceptionOccurred();
1377 env_->ExceptionClear();
1378 EXPECT_TRUE(env_->IsInstanceOf(thrown_exception, exception_class));
1379
1380 EXPECT_EQ(JNI_OK, env_->ThrowNew(exception_class, NULL));
1381 EXPECT_TRUE(env_->ExceptionCheck());
1382 thrown_exception = env_->ExceptionOccurred();
Elliott Hughes37f7a402011-08-22 18:56:01 -07001383 env_->ExceptionClear();
Elliott Hughesa2501992011-08-26 19:39:54 -07001384 EXPECT_TRUE(env_->IsInstanceOf(thrown_exception, exception_class));
Elliott Hughes37f7a402011-08-22 18:56:01 -07001385}
1386
Elliott Hughesb465ab02011-08-24 11:21:21 -07001387// TODO: this test is DISABLED until we can actually run java.nio.Buffer's <init>.
1388TEST_F(JniInternalTest, DISABLED_NewDirectBuffer_GetDirectBufferAddress_GetDirectBufferCapacity) {
1389 jclass buffer_class = env_->FindClass("java/nio/Buffer");
1390 ASSERT_TRUE(buffer_class != NULL);
1391
1392 char bytes[1024];
1393 jobject buffer = env_->NewDirectByteBuffer(bytes, sizeof(bytes));
1394 ASSERT_TRUE(buffer != NULL);
1395 ASSERT_TRUE(env_->IsInstanceOf(buffer, buffer_class));
1396 ASSERT_TRUE(env_->GetDirectBufferAddress(buffer) == bytes);
1397 ASSERT_TRUE(env_->GetDirectBufferCapacity(buffer) == sizeof(bytes));
1398}
1399
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001400} // namespace art