blob: cb71fa29bcce2e2e77e4d2105901304a2f1d8936 [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 <cmath>
6#include <sys/mman.h>
7
8#include "common_test.h"
Elliott Hughes0c9cd562011-08-12 10:59:29 -07009#include "gtest/gtest.h"
10
11namespace art {
12
Brian Carlstromf734cf52011-08-17 16:28:14 -070013class JniInternalTest : public CommonTest {
Elliott Hughesc7ac37f2011-08-12 12:21:58 -070014 protected:
15 virtual void SetUp() {
Brian Carlstromf734cf52011-08-17 16:28:14 -070016 CommonTest::SetUp();
Elliott Hughesc7ac37f2011-08-12 12:21:58 -070017 env_ = Thread::Current()->GetJniEnv();
18 }
19 JNIEnv* env_;
Elliott Hughes0c9cd562011-08-12 10:59:29 -070020};
21
Elliott Hughesc7ac37f2011-08-12 12:21:58 -070022TEST_F(JniInternalTest, GetVersion) {
23 ASSERT_EQ(JNI_VERSION_1_6, env_->GetVersion());
24}
25
Elliott Hughes0c9cd562011-08-12 10:59:29 -070026#define EXPECT_CLASS_FOUND(NAME) \
Elliott Hughesc7ac37f2011-08-12 12:21:58 -070027 EXPECT_TRUE(env_->FindClass(NAME) != NULL)
Elliott Hughes0c9cd562011-08-12 10:59:29 -070028
29#define EXPECT_CLASS_NOT_FOUND(NAME) \
Elliott Hughesc7ac37f2011-08-12 12:21:58 -070030 EXPECT_TRUE(env_->FindClass(NAME) == NULL)
Elliott Hughes0c9cd562011-08-12 10:59:29 -070031
32TEST_F(JniInternalTest, FindClass) {
Elliott Hughes0c9cd562011-08-12 10:59:29 -070033 // TODO: when these tests start failing because you're calling FindClass
34 // with a pending exception, fix EXPECT_CLASS_NOT_FOUND to assert that an
35 // exception was thrown and clear the exception.
36
37 // TODO: . is only allowed as an alternative to / if CheckJNI is off.
38
39 // Reference types...
40 // You can't include the "L;" in a JNI class descriptor.
41 EXPECT_CLASS_FOUND("java/lang/String");
42 EXPECT_CLASS_NOT_FOUND("Ljava/lang/String;");
43 // We support . as well as / for compatibility.
44 EXPECT_CLASS_FOUND("java.lang.String");
45 EXPECT_CLASS_NOT_FOUND("Ljava.lang.String;");
46 // ...for arrays too, where you must include "L;".
47 EXPECT_CLASS_FOUND("[Ljava/lang/String;");
48 EXPECT_CLASS_NOT_FOUND("[java/lang/String");
49 EXPECT_CLASS_FOUND("[Ljava.lang.String;");
50 EXPECT_CLASS_NOT_FOUND("[java.lang.String");
51
52 // Primitive arrays are okay (if the primitive type is valid)...
53 EXPECT_CLASS_FOUND("[C");
54 EXPECT_CLASS_NOT_FOUND("[K");
55 // But primitive types aren't allowed...
56 EXPECT_CLASS_NOT_FOUND("C");
57 EXPECT_CLASS_NOT_FOUND("K");
58}
59
Elliott Hughescdf53122011-08-19 15:46:09 -070060#define EXPECT_EXCEPTION(exception_class) \
61 do { \
62 EXPECT_TRUE(env_->ExceptionCheck()); \
63 jthrowable exception = env_->ExceptionOccurred(); \
64 EXPECT_NE(static_cast<jthrowable>(NULL), exception); \
65 EXPECT_TRUE(env_->IsInstanceOf(exception, exception_class)); \
66 env_->ExceptionClear(); \
67 } while (false)
68
69TEST_F(JniInternalTest, GetFieldID) {
70 jclass jlnsfe = env_->FindClass("java/lang/NoSuchFieldError");
71 ASSERT_TRUE(jlnsfe != NULL);
72 jclass c = env_->FindClass("java/lang/String");
73 ASSERT_TRUE(c != NULL);
74
75 // Wrong type.
76 jfieldID fid = env_->GetFieldID(c, "count", "J");
77 EXPECT_EQ(static_cast<jfieldID>(NULL), fid);
78 EXPECT_EXCEPTION(jlnsfe);
79
80 // Wrong name.
81 fid = env_->GetFieldID(c, "Count", "I");
82 EXPECT_EQ(static_cast<jfieldID>(NULL), fid);
83 EXPECT_EXCEPTION(jlnsfe);
84
85 // Good declared field lookup.
86 fid = env_->GetFieldID(c, "count", "I");
87 EXPECT_NE(static_cast<jfieldID>(NULL), fid);
88 EXPECT_TRUE(fid != NULL);
89 EXPECT_FALSE(env_->ExceptionCheck());
90
91 // Good superclass field lookup.
92 c = env_->FindClass("java/lang/StringBuilder");
93 fid = env_->GetFieldID(c, "count", "I");
94 EXPECT_NE(static_cast<jfieldID>(NULL), fid);
95 EXPECT_TRUE(fid != NULL);
96 EXPECT_FALSE(env_->ExceptionCheck());
97
98 // Not instance.
99 fid = env_->GetFieldID(c, "CASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;");
100 EXPECT_EQ(static_cast<jfieldID>(NULL), fid);
101 EXPECT_EXCEPTION(jlnsfe);
102}
103
104TEST_F(JniInternalTest, GetStaticFieldID) {
105 jclass jlnsfe = env_->FindClass("java/lang/NoSuchFieldError");
106 ASSERT_TRUE(jlnsfe != NULL);
107 jclass c = env_->FindClass("java/lang/String");
108 ASSERT_TRUE(c != NULL);
109
110 // Wrong type.
111 jfieldID fid = env_->GetStaticFieldID(c, "CASE_INSENSITIVE_ORDER", "J");
112 EXPECT_EQ(static_cast<jfieldID>(NULL), fid);
113 EXPECT_EXCEPTION(jlnsfe);
114
115 // Wrong name.
116 fid = env_->GetStaticFieldID(c, "cASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;");
117 EXPECT_EQ(static_cast<jfieldID>(NULL), fid);
118 EXPECT_EXCEPTION(jlnsfe);
119
120 // Good declared field lookup.
121 fid = env_->GetStaticFieldID(c, "CASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;");
122 EXPECT_NE(static_cast<jfieldID>(NULL), fid);
123 EXPECT_TRUE(fid != NULL);
124 EXPECT_FALSE(env_->ExceptionCheck());
125
126 // Not static.
127 fid = env_->GetStaticFieldID(c, "count", "I");
128 EXPECT_EQ(static_cast<jfieldID>(NULL), fid);
129 EXPECT_EXCEPTION(jlnsfe);
130}
131
Ian Rogers4dd71f12011-08-16 14:16:02 -0700132TEST_F(JniInternalTest, GetMethodID) {
133 jclass jlobject = env_->FindClass("java/lang/Object");
134 jclass jlstring = env_->FindClass("java/lang/String");
135 jclass jlnsme = env_->FindClass("java/lang/NoSuchMethodError");
136
137 // Sanity check that no exceptions are pending
Elliott Hughescdf53122011-08-19 15:46:09 -0700138 ASSERT_FALSE(env_->ExceptionCheck());
Ian Rogers4dd71f12011-08-16 14:16:02 -0700139
140 // Check that java.lang.Object.foo() doesn't exist and NoSuchMethodError is
141 // a pending exception
142 jmethodID method = env_->GetMethodID(jlobject, "foo", "()V");
143 EXPECT_EQ(static_cast<jmethodID>(NULL), method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700144 EXPECT_EXCEPTION(jlnsme);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700145
146 // Check that java.lang.Object.equals() does exist
Ian Rogers4dd71f12011-08-16 14:16:02 -0700147 method = env_->GetMethodID(jlobject, "equals", "(Ljava/lang/Object;)Z");
148 EXPECT_NE(static_cast<jmethodID>(NULL), method);
149 EXPECT_FALSE(env_->ExceptionCheck());
Ian Rogers4dd71f12011-08-16 14:16:02 -0700150
151 // Check that GetMethodID for java.lang.String.valueOf(int) fails as the
152 // method is static
153 method = env_->GetMethodID(jlstring, "valueOf", "(I)Ljava/lang/String;");
154 EXPECT_EQ(static_cast<jmethodID>(NULL), method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700155 EXPECT_EXCEPTION(jlnsme);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700156}
157
158TEST_F(JniInternalTest, GetStaticMethodID) {
159 jclass jlobject = env_->FindClass("java/lang/Object");
160 jclass jlnsme = env_->FindClass("java/lang/NoSuchMethodError");
161
162 // Sanity check that no exceptions are pending
Elliott Hughescdf53122011-08-19 15:46:09 -0700163 ASSERT_FALSE(env_->ExceptionCheck());
Ian Rogers4dd71f12011-08-16 14:16:02 -0700164
165 // Check that java.lang.Object.foo() doesn't exist and NoSuchMethodError is
166 // a pending exception
167 jmethodID method = env_->GetStaticMethodID(jlobject, "foo", "()V");
168 EXPECT_EQ(static_cast<jmethodID>(NULL), method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700169 EXPECT_EXCEPTION(jlnsme);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700170
171 // Check that GetStaticMethodID for java.lang.Object.equals(Object) fails as
172 // the method is not static
173 method = env_->GetStaticMethodID(jlobject, "equals", "(Ljava/lang/Object;)Z");
174 EXPECT_EQ(static_cast<jmethodID>(NULL), method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700175 EXPECT_EXCEPTION(jlnsme);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700176
177 // Check that java.lang.String.valueOf(int) does exist
Ian Rogers4dd71f12011-08-16 14:16:02 -0700178 jclass jlstring = env_->FindClass("java/lang/String");
179 method = env_->GetStaticMethodID(jlstring, "valueOf",
180 "(I)Ljava/lang/String;");
181 EXPECT_NE(static_cast<jmethodID>(NULL), method);
182 EXPECT_FALSE(env_->ExceptionCheck());
Ian Rogers4dd71f12011-08-16 14:16:02 -0700183}
184
Elliott Hughescdf53122011-08-19 15:46:09 -0700185TEST_F(JniInternalTest, FromReflectedField_ToReflectedField) {
186 jclass jlrField = env_->FindClass("java/lang/reflect/Field");
187 jclass c = env_->FindClass("java/lang/String");
188 ASSERT_TRUE(c != NULL);
189 jfieldID fid = env_->GetFieldID(c, "count", "I");
190 ASSERT_TRUE(fid != NULL);
191 // Turn the fid into a java.lang.reflect.Field...
192 jobject field = env_->ToReflectedField(c, fid, JNI_FALSE);
193 ASSERT_TRUE(c != NULL);
194 ASSERT_TRUE(env_->IsInstanceOf(field, jlrField));
195 // ...and back again.
196 jfieldID fid2 = env_->FromReflectedField(field);
197 ASSERT_TRUE(fid2 != NULL);
198}
199
200TEST_F(JniInternalTest, FromReflectedMethod_ToReflectedMethod) {
201 jclass jlrMethod = env_->FindClass("java/lang/reflect/Method");
202 jclass c = env_->FindClass("java/lang/String");
203 ASSERT_TRUE(c != NULL);
204 jmethodID mid = env_->GetMethodID(c, "length", "()I");
205 ASSERT_TRUE(mid != NULL);
206 // Turn the mid into a java.lang.reflect.Method...
207 jobject method = env_->ToReflectedMethod(c, mid, JNI_FALSE);
208 ASSERT_TRUE(c != NULL);
209 ASSERT_TRUE(env_->IsInstanceOf(method, jlrMethod));
210 // ...and back again.
211 jmethodID mid2 = env_->FromReflectedMethod(method);
212 ASSERT_TRUE(mid2 != NULL);
213}
214
Ian Rogers4dd71f12011-08-16 14:16:02 -0700215TEST_F(JniInternalTest, RegisterNatives) {
216 jclass jlobject = env_->FindClass("java/lang/Object");
217 jclass jlnsme = env_->FindClass("java/lang/NoSuchMethodError");
218
219 // Sanity check that no exceptions are pending
Elliott Hughescdf53122011-08-19 15:46:09 -0700220 ASSERT_FALSE(env_->ExceptionCheck());
Ian Rogers4dd71f12011-08-16 14:16:02 -0700221
222 // Check that registering to a non-existent java.lang.Object.foo() causes a
223 // NoSuchMethodError
224 {
225 JNINativeMethod methods[] = {{"foo", "()V", NULL}};
226 env_->RegisterNatives(jlobject, methods, 1);
227 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700228 EXPECT_EXCEPTION(jlnsme);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700229
230 // Check that registering non-native methods causes a NoSuchMethodError
231 {
232 JNINativeMethod methods[] = {{"equals", "(Ljava/lang/Object;)Z", NULL}};
233 env_->RegisterNatives(jlobject, methods, 1);
234 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700235 EXPECT_EXCEPTION(jlnsme);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700236
237 // Check that registering native methods is successful
238 {
239 JNINativeMethod methods[] = {{"hashCode", "()I", NULL}};
240 env_->RegisterNatives(jlobject, methods, 1);
241 }
242 EXPECT_FALSE(env_->ExceptionCheck());
243}
244
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700245TEST_F(JniInternalTest, NewPrimitiveArray) {
246 // TODO: death tests for negative array sizes.
247
Elliott Hughesf2682d52011-08-15 16:37:04 -0700248 // TODO: check returned array size.
249
250 // TODO: check returned array class.
251
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700252 EXPECT_TRUE(env_->NewBooleanArray(0) != NULL);
253 EXPECT_TRUE(env_->NewByteArray(0) != NULL);
254 EXPECT_TRUE(env_->NewCharArray(0) != NULL);
255 EXPECT_TRUE(env_->NewDoubleArray(0) != NULL);
256 EXPECT_TRUE(env_->NewFloatArray(0) != NULL);
257 EXPECT_TRUE(env_->NewIntArray(0) != NULL);
258 EXPECT_TRUE(env_->NewLongArray(0) != NULL);
259 EXPECT_TRUE(env_->NewShortArray(0) != NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700260
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700261 EXPECT_TRUE(env_->NewBooleanArray(1) != NULL);
262 EXPECT_TRUE(env_->NewByteArray(1) != NULL);
263 EXPECT_TRUE(env_->NewCharArray(1) != NULL);
264 EXPECT_TRUE(env_->NewDoubleArray(1) != NULL);
265 EXPECT_TRUE(env_->NewFloatArray(1) != NULL);
266 EXPECT_TRUE(env_->NewIntArray(1) != NULL);
267 EXPECT_TRUE(env_->NewLongArray(1) != NULL);
268 EXPECT_TRUE(env_->NewShortArray(1) != NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700269}
270
Elliott Hughesf2682d52011-08-15 16:37:04 -0700271TEST_F(JniInternalTest, NewObjectArray) {
272 // TODO: death tests for negative array sizes.
273
274 // TODO: check returned array size.
275
276 // TODO: check returned array class.
277
278 // TODO: check non-NULL initial elements.
279
Elliott Hughes289da822011-08-16 10:11:20 -0700280 jclass c = env_->FindClass("[Ljava/lang/String;");
281 ASSERT_TRUE(c != NULL);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700282
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700283 EXPECT_TRUE(env_->NewObjectArray(0, c, NULL) != NULL);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700284
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700285 EXPECT_TRUE(env_->NewObjectArray(1, c, NULL) != NULL);
286}
287
288TEST_F(JniInternalTest, NewStringUTF) {
289 EXPECT_TRUE(env_->NewStringUTF(NULL) == NULL);
290 EXPECT_TRUE(env_->NewStringUTF("") != NULL);
291 EXPECT_TRUE(env_->NewStringUTF("hello") != NULL);
292 // TODO: check some non-ASCII strings.
Elliott Hughesf2682d52011-08-15 16:37:04 -0700293}
294
Elliott Hughes289da822011-08-16 10:11:20 -0700295TEST_F(JniInternalTest, SetObjectArrayElement) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700296 jclass aioobe = env_->FindClass("java/lang/ArrayIndexOutOfBoundsException");
Elliott Hughes289da822011-08-16 10:11:20 -0700297 jclass c = env_->FindClass("[Ljava/lang/Object;");
298 ASSERT_TRUE(c != NULL);
299
300 jobjectArray array = env_->NewObjectArray(1, c, NULL);
301 EXPECT_TRUE(array != NULL);
302 env_->SetObjectArrayElement(array, 0, c);
303 // TODO: check reading value back
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700304
305 // ArrayIndexOutOfBounds for negative index.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700306 env_->SetObjectArrayElement(array, -1, c);
Elliott Hughescdf53122011-08-19 15:46:09 -0700307 EXPECT_EXCEPTION(aioobe);
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700308
309 // ArrayIndexOutOfBounds for too-large index.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700310 env_->SetObjectArrayElement(array, 1, c);
Elliott Hughescdf53122011-08-19 15:46:09 -0700311 EXPECT_EXCEPTION(aioobe);
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700312
Elliott Hughes289da822011-08-16 10:11:20 -0700313 // TODO: check ArrayStoreException thrown for bad types.
314}
315
Elliott Hughes18c07532011-08-18 15:50:51 -0700316TEST_F(JniInternalTest, NewLocalRef_NULL) {
317 EXPECT_TRUE(env_->NewLocalRef(NULL) == NULL);
318}
319
320TEST_F(JniInternalTest, NewLocalRef) {
321 jstring s = env_->NewStringUTF("");
322 ASSERT_TRUE(s != NULL);
323 jobject o = env_->NewLocalRef(s);
324 EXPECT_TRUE(o != NULL);
325 EXPECT_TRUE(o != s);
326
327 // TODO: check that o is a local reference.
328}
329
330TEST_F(JniInternalTest, DeleteLocalRef_NULL) {
331 env_->DeleteLocalRef(NULL);
332}
333
334TEST_F(JniInternalTest, DeleteLocalRef) {
335 jstring s = env_->NewStringUTF("");
336 ASSERT_TRUE(s != NULL);
337 env_->DeleteLocalRef(s);
338
339 // Currently, deleting an already-deleted reference is just a warning.
340 env_->DeleteLocalRef(s);
341
342 s = env_->NewStringUTF("");
343 ASSERT_TRUE(s != NULL);
344 jobject o = env_->NewLocalRef(s);
345 ASSERT_TRUE(o != NULL);
346
347 env_->DeleteLocalRef(s);
348 env_->DeleteLocalRef(o);
349}
350
351TEST_F(JniInternalTest, NewGlobalRef_NULL) {
352 EXPECT_TRUE(env_->NewGlobalRef(NULL) == NULL);
353}
354
355TEST_F(JniInternalTest, NewGlobalRef) {
356 jstring s = env_->NewStringUTF("");
357 ASSERT_TRUE(s != NULL);
358 jobject o = env_->NewGlobalRef(s);
359 EXPECT_TRUE(o != NULL);
360 EXPECT_TRUE(o != s);
361
362 // TODO: check that o is a global reference.
363}
364
365TEST_F(JniInternalTest, DeleteGlobalRef_NULL) {
366 env_->DeleteGlobalRef(NULL);
367}
368
369TEST_F(JniInternalTest, DeleteGlobalRef) {
370 jstring s = env_->NewStringUTF("");
371 ASSERT_TRUE(s != NULL);
372
373 jobject o = env_->NewGlobalRef(s);
374 ASSERT_TRUE(o != NULL);
375 env_->DeleteGlobalRef(o);
376
377 // Currently, deleting an already-deleted reference is just a warning.
378 env_->DeleteGlobalRef(o);
379
380 jobject o1 = env_->NewGlobalRef(s);
381 ASSERT_TRUE(o1 != NULL);
382 jobject o2 = env_->NewGlobalRef(s);
383 ASSERT_TRUE(o2 != NULL);
384
385 env_->DeleteGlobalRef(o1);
386 env_->DeleteGlobalRef(o2);
387}
388
389TEST_F(JniInternalTest, NewWeakGlobalRef_NULL) {
390 EXPECT_TRUE(env_->NewWeakGlobalRef(NULL) == NULL);
391}
392
393TEST_F(JniInternalTest, NewWeakGlobalRef) {
394 jstring s = env_->NewStringUTF("");
395 ASSERT_TRUE(s != NULL);
396 jobject o = env_->NewWeakGlobalRef(s);
397 EXPECT_TRUE(o != NULL);
398 EXPECT_TRUE(o != s);
399
400 // TODO: check that o is a weak global reference.
401}
402
403TEST_F(JniInternalTest, DeleteWeakGlobalRef_NULL) {
404 env_->DeleteWeakGlobalRef(NULL);
405}
406
407TEST_F(JniInternalTest, DeleteWeakGlobalRef) {
408 jstring s = env_->NewStringUTF("");
409 ASSERT_TRUE(s != NULL);
410
411 jobject o = env_->NewWeakGlobalRef(s);
412 ASSERT_TRUE(o != NULL);
413 env_->DeleteWeakGlobalRef(o);
414
415 // Currently, deleting an already-deleted reference is just a warning.
416 env_->DeleteWeakGlobalRef(o);
417
418 jobject o1 = env_->NewWeakGlobalRef(s);
419 ASSERT_TRUE(o1 != NULL);
420 jobject o2 = env_->NewWeakGlobalRef(s);
421 ASSERT_TRUE(o2 != NULL);
422
423 env_->DeleteWeakGlobalRef(o1);
424 env_->DeleteWeakGlobalRef(o2);
425}
426
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700427bool EnsureInvokeStub(Method* method);
428
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700429Method::InvokeStub* AllocateStub(Method* method,
430 byte* code,
431 size_t length) {
432 CHECK(method->GetInvokeStub() == NULL);
433 EnsureInvokeStub(method);
434 Method::InvokeStub* stub = method->GetInvokeStub();
435 CHECK(stub != NULL);
buzbeec143c552011-08-20 17:38:58 -0700436 method->SetCode(code, length, kThumb2);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700437 return stub;
438}
439
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700440#if defined(__arm__)
441TEST_F(JniInternalTest, StaticMainMethod) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700442 scoped_ptr<DexFile> dex(OpenDexFileBase64(kMainDex, "kMainDex"));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700443
444 PathClassLoader* class_loader = AllocPathClassLoader(dex.get());
445 ASSERT_TRUE(class_loader != NULL);
446
447 Class* klass = class_linker_->FindClass("LMain;", class_loader);
448 ASSERT_TRUE(klass != NULL);
449
450 Method* method = klass->FindDirectMethod("main", "([Ljava/lang/String;)V");
451 ASSERT_TRUE(method != NULL);
452
453 byte main_LV_code[] = {
454 0x2d, 0xe9, 0x00, 0x40, 0x83, 0xb0, 0xcd, 0xf8, 0x00, 0x00,
455 0xcd, 0xf8, 0x14, 0x10, 0x03, 0xb0, 0xbd, 0xe8, 0x00, 0x80,
456 };
457
458 Method::InvokeStub* stub = AllocateStub(method,
459 main_LV_code,
460 sizeof(main_LV_code));
461
462 Object* arg = NULL;
463
464 (*stub)(method, NULL, NULL, reinterpret_cast<byte*>(&arg), NULL);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700465}
466
467TEST_F(JniInternalTest, StaticNopMethod) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700468 scoped_ptr<DexFile> dex(OpenDexFileBase64(kStaticLeafMethodsDex, "kStaticLeafMethodsDex"));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700469
470 PathClassLoader* class_loader = AllocPathClassLoader(dex.get());
471 ASSERT_TRUE(class_loader != NULL);
472
473 Class* klass = class_linker_->FindClass("LStaticLeafMethods;", class_loader);
474 ASSERT_TRUE(klass != NULL);
475
476 Method* method = klass->FindDirectMethod("nop", "()V");
477 ASSERT_TRUE(method != NULL);
478
479 byte nop_V_code[] = {
480 0x2d, 0xe9, 0x00, 0x40, 0x83, 0xb0, 0xcd, 0xf8,
481 0x00, 0x00, 0x03, 0xb0, 0xbd, 0xe8, 0x00, 0x80,
482 };
483
484 Method::InvokeStub* stub = AllocateStub(method,
485 nop_V_code,
486 sizeof(nop_V_code));
487 ASSERT_TRUE(stub);
488
489 (*stub)(method, NULL, NULL, NULL, NULL);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700490}
491
492TEST_F(JniInternalTest, StaticIdentityByteMethod) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700493 scoped_ptr<DexFile> dex(OpenDexFileBase64(kStaticLeafMethodsDex, "kStaticLeafMethodsDex"));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700494
495 PathClassLoader* class_loader = AllocPathClassLoader(dex.get());
496 ASSERT_TRUE(class_loader != NULL);
497
498 Class* klass = class_linker_->FindClass("LStaticLeafMethods;", class_loader);
499 ASSERT_TRUE(klass != NULL);
500
501 Method* method = klass->FindDirectMethod("identity", "(B)B");
502 ASSERT_TRUE(method != NULL);
503
504 byte identity_BB_code[] = {
505 0x2d, 0xe9, 0x00, 0x40, 0x83, 0xb0, 0xcd, 0xf8,
506 0x00, 0x00, 0xcd, 0xf8, 0x14, 0x10, 0x05, 0x98,
507 0x03, 0xb0, 0xbd, 0xe8, 0x00, 0x80, 0x00, 0x00,
508 };
509
510 Method::InvokeStub* stub = AllocateStub(method,
511 identity_BB_code,
512 sizeof(identity_BB_code));
513
514 int arg;
515 JValue result;
516
517 arg = 0;
518 result.b = -1;
519 (*stub)(method, NULL, NULL, reinterpret_cast<byte*>(&arg), &result);
520 EXPECT_EQ(0, result.b);
521
522 arg = -1;
523 result.b = 0;
524 (*stub)(method, NULL, NULL, reinterpret_cast<byte*>(&arg), &result);
525 EXPECT_EQ(-1, result.b);
526
527 arg = SCHAR_MAX;
528 result.b = 0;
529 (*stub)(method, NULL, NULL, reinterpret_cast<byte*>(&arg), &result);
530 EXPECT_EQ(SCHAR_MAX, result.b);
531
532 arg = SCHAR_MIN;
533 result.b = 0;
534 (*stub)(method, NULL, NULL, reinterpret_cast<byte*>(&arg), &result);
535 EXPECT_EQ(SCHAR_MIN, result.b);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700536}
537
538TEST_F(JniInternalTest, StaticIdentityIntMethod) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700539 scoped_ptr<DexFile> dex(OpenDexFileBase64(kStaticLeafMethodsDex, "kStaticLeafMethodsDex"));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700540
541 PathClassLoader* class_loader = AllocPathClassLoader(dex.get());
542 ASSERT_TRUE(class_loader != NULL);
543
544 Class* klass = class_linker_->FindClass("LStaticLeafMethods;", class_loader);
545 ASSERT_TRUE(klass != NULL);
546
547 Method* method = klass->FindDirectMethod("identity", "(I)I");
548 ASSERT_TRUE(method != NULL);
549
550 byte identity_II_code[] = {
551 0x2d, 0xe9, 0x00, 0x40, 0x83, 0xb0, 0xcd, 0xf8,
552 0x00, 0x00, 0xcd, 0xf8, 0x14, 0x10, 0x05, 0x98,
553 0x03, 0xb0, 0xbd, 0xe8, 0x00, 0x80, 0x00, 0x00,
554 };
555
556 Method::InvokeStub* stub = AllocateStub(method,
557 identity_II_code,
558 sizeof(identity_II_code));
559
560 int arg;
561 JValue result;
562
563 arg = 0;
564 result.i = -1;
565 (*stub)(method, NULL, NULL, reinterpret_cast<byte*>(&arg), &result);
566 EXPECT_EQ(0, result.i);
567
568 arg = -1;
569 result.i = 0;
570 (*stub)(method, NULL, NULL, reinterpret_cast<byte*>(&arg), &result);
571 EXPECT_EQ(-1, result.i);
572
573 arg = INT_MAX;
574 result.i = 0;
575 (*stub)(method, NULL, NULL, reinterpret_cast<byte*>(&arg), &result);
576 EXPECT_EQ(INT_MAX, result.i);
577
578 arg = INT_MIN;
579 result.i = 0;
580 (*stub)(method, NULL, NULL, reinterpret_cast<byte*>(&arg), &result);
581 EXPECT_EQ(INT_MIN, result.i);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700582}
583
584TEST_F(JniInternalTest, StaticIdentityDoubleMethod) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700585 scoped_ptr<DexFile> dex(OpenDexFileBase64(kStaticLeafMethodsDex, "kStaticLeafMethodsDex"));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700586
587 PathClassLoader* class_loader = AllocPathClassLoader(dex.get());
588 ASSERT_TRUE(class_loader != NULL);
589
590 Class* klass = class_linker_->FindClass("LStaticLeafMethods;", class_loader);
591 ASSERT_TRUE(klass != NULL);
592
593 Method* method = klass->FindDirectMethod("identity", "(D)D");
594 ASSERT_TRUE(method != NULL);
595
596 byte identity_DD_code[] = {
597 0x2d, 0xe9, 0x00, 0x40, 0x83, 0xb0, 0xcd, 0xf8,
598 0x00, 0x00, 0xcd, 0xf8, 0x14, 0x10, 0xcd, 0xf8,
599 0x18, 0x20, 0x05, 0x98, 0x06, 0x99, 0x03, 0xb0,
600 0xbd, 0xe8, 0x00, 0x80,
601 };
602
603 Method::InvokeStub* stub = AllocateStub(method,
604 identity_DD_code,
605 sizeof(identity_DD_code));
606
607 double arg;
608 JValue result;
609
610 arg = 0.0;
611 result.d = -1.0;
612 (*stub)(method, NULL, NULL, reinterpret_cast<byte*>(&arg), &result);
613 EXPECT_EQ(0.0, result.d);
614
615 arg = -1.0;
616 result.d = 0.0;
617 (*stub)(method, NULL, NULL, reinterpret_cast<byte*>(&arg), &result);
618 EXPECT_EQ(-1.0, result.d);
619
620 arg = DBL_MAX;
621 result.d = 0.0;
622 (*stub)(method, NULL, NULL, reinterpret_cast<byte*>(&arg), &result);
623 EXPECT_EQ(DBL_MAX, result.d);
624
625 arg = DBL_MIN;
626 result.d = 0.0;
627 (*stub)(method, NULL, NULL, reinterpret_cast<byte*>(&arg), &result);
628 EXPECT_EQ(DBL_MIN, result.d);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700629}
630
631TEST_F(JniInternalTest, StaticSumIntIntMethod) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700632 scoped_ptr<DexFile> dex(OpenDexFileBase64(kStaticLeafMethodsDex, "kStaticLeafMethodsDex"));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700633
634 PathClassLoader* class_loader = AllocPathClassLoader(dex.get());
635 ASSERT_TRUE(class_loader != NULL);
636
637 Class* klass = class_linker_->FindClass("LStaticLeafMethods;", class_loader);
638 ASSERT_TRUE(klass != NULL);
639
640 Method* method = klass->FindDirectMethod("sum", "(II)I");
641 ASSERT_TRUE(method != NULL);
642
643 byte sum_III_code[] = {
644 0x2d, 0xe9, 0x00, 0x40, 0x83, 0xb0, 0xcd, 0xf8,
645 0x00, 0x00, 0xcd, 0xf8, 0x14, 0x10, 0xcd, 0xf8,
646 0x18, 0x20, 0x05, 0x98, 0x06, 0x99, 0x42, 0x18,
647 0xcd, 0xf8, 0x04, 0x20, 0x01, 0x98, 0x03, 0xb0,
648 0xbd, 0xe8, 0x00, 0x80,
649 };
650
651 Method::InvokeStub* stub = AllocateStub(method,
652 sum_III_code,
653 sizeof(sum_III_code));
654
655 int args[2];
656 JValue result;
657
658 args[0] = 0;
659 args[1] = 0;
660 result.i = -1;
661 (*stub)(method, NULL, NULL, reinterpret_cast<byte*>(args), &result);
662 EXPECT_EQ(0, result.i);
663
664 args[0] = 1;
665 args[1] = 2;
666 result.i = 0;
667 (*stub)(method, NULL, NULL, reinterpret_cast<byte*>(args), &result);
668 EXPECT_EQ(3, result.i);
669
670 args[0] = -2;
671 args[1] = 5;
672 result.i = 0;
673 (*stub)(method, NULL, NULL, reinterpret_cast<byte*>(args), &result);
674 EXPECT_EQ(3, result.i);
675
676 args[0] = INT_MAX;
677 args[1] = INT_MIN;
678 result.i = 1234;
679 (*stub)(method, NULL, NULL, reinterpret_cast<byte*>(args), &result);
680 EXPECT_EQ(-1, result.i);
681
682 args[0] = INT_MAX;
683 args[1] = INT_MAX;
684 result.i = INT_MIN;
685 (*stub)(method, NULL, NULL, reinterpret_cast<byte*>(args), &result);
686 EXPECT_EQ(-2, result.i);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700687}
688
689TEST_F(JniInternalTest, StaticSumIntIntIntMethod) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700690 scoped_ptr<DexFile> dex(OpenDexFileBase64(kStaticLeafMethodsDex, "kStaticLeafMethodsDex"));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700691
692 PathClassLoader* class_loader = AllocPathClassLoader(dex.get());
693 ASSERT_TRUE(class_loader != NULL);
694
695 Class* klass = class_linker_->FindClass("LStaticLeafMethods;", class_loader);
696 ASSERT_TRUE(klass != NULL);
697
698 Method* method = klass->FindDirectMethod("sum", "(III)I");
699 ASSERT_TRUE(method != NULL);
700
701 byte sum_IIII_code[] = {
702 0x2d, 0xe9, 0x00, 0x40, 0x83, 0xb0, 0xcd, 0xf8,
703 0x00, 0x00, 0xcd, 0xf8, 0x14, 0x10, 0xcd, 0xf8,
704 0x18, 0x20, 0xcd, 0xf8, 0x1c, 0x30, 0x05, 0x98,
705 0x06, 0x99, 0x42, 0x18, 0xcd, 0xf8, 0x04, 0x20,
706 0x01, 0x9b, 0xdd, 0xf8, 0x1c, 0xc0, 0x13, 0xeb,
707 0x0c, 0x03, 0xcd, 0xf8, 0x04, 0x30, 0x01, 0x98,
708 0x03, 0xb0, 0xbd, 0xe8, 0x00, 0x80, 0x00, 0x00,
709 };
710
711 Method::InvokeStub* stub = AllocateStub(method,
712 sum_IIII_code,
713 sizeof(sum_IIII_code));
714
715 int args[3];
716 JValue result;
717
718 args[0] = 0;
719 args[1] = 0;
720 args[2] = 0;
721 result.i = -1;
722 (*stub)(method, NULL, NULL, reinterpret_cast<byte*>(args), &result);
723 EXPECT_EQ(0, result.i);
724
725 args[0] = 1;
726 args[1] = 2;
727 args[2] = 3;
728 result.i = 0;
729 (*stub)(method, NULL, NULL, reinterpret_cast<byte*>(args), &result);
730 EXPECT_EQ(6, result.i);
731
732 args[0] = -1;
733 args[1] = 2;
734 args[2] = -3;
735 result.i = 0;
736 (*stub)(method, NULL, NULL, reinterpret_cast<byte*>(args), &result);
737 EXPECT_EQ(-2, result.i);
738
739 args[0] = INT_MAX;
740 args[1] = INT_MIN;
741 args[2] = INT_MAX;
742 result.i = 1234;
743 (*stub)(method, NULL, NULL, reinterpret_cast<byte*>(args), &result);
744 EXPECT_EQ(2147483646, result.i);
745
746 args[0] = INT_MAX;
747 args[1] = INT_MAX;
748 args[2] = INT_MAX;
749 result.i = INT_MIN;
750 (*stub)(method, NULL, NULL, reinterpret_cast<byte*>(args), &result);
751 EXPECT_EQ(2147483645, result.i);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700752}
753
754TEST_F(JniInternalTest, StaticSumIntIntIntIntMethod) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700755 scoped_ptr<DexFile> dex(OpenDexFileBase64(kStaticLeafMethodsDex, "kStaticLeafMethodsDex"));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700756
757 PathClassLoader* class_loader = AllocPathClassLoader(dex.get());
758 ASSERT_TRUE(class_loader != NULL);
759
760 Class* klass = class_linker_->FindClass("LStaticLeafMethods;", class_loader);
761 ASSERT_TRUE(klass != NULL);
762
763 Method* method = klass->FindDirectMethod("sum", "(IIII)I");
764 ASSERT_TRUE(method != NULL);
765
766 byte sum_IIIII_code[] = {
767 0x2d, 0xe9, 0x00, 0x40, 0x83, 0xb0, 0xcd, 0xf8,
768 0x00, 0x00, 0xcd, 0xf8, 0x14, 0x10, 0xcd, 0xf8,
769 0x18, 0x20, 0xcd, 0xf8, 0x1c, 0x30, 0x05, 0x98,
770 0x06, 0x99, 0x42, 0x18, 0xcd, 0xf8, 0x04, 0x20,
771 0x01, 0x9b, 0xdd, 0xf8, 0x1c, 0xc0, 0x13, 0xeb,
772 0x0c, 0x03, 0xcd, 0xf8, 0x04, 0x30, 0x01, 0x98,
773 0x08, 0x99, 0x40, 0x18, 0xcd, 0xf8, 0x04, 0x00,
774 0x01, 0x98, 0x03, 0xb0, 0xbd, 0xe8, 0x00, 0x80,
775 };
776
777 Method::InvokeStub* stub = AllocateStub(method,
778 sum_IIIII_code,
779 sizeof(sum_IIIII_code));
780
781 int args[4];
782 JValue result;
783
784 args[0] = 0;
785 args[1] = 0;
786 args[2] = 0;
787 args[3] = 0;
788 result.i = -1;
789 (*stub)(method, NULL, NULL, reinterpret_cast<byte*>(args), &result);
790 EXPECT_EQ(0, result.i);
791
792 args[0] = 1;
793 args[1] = 2;
794 args[2] = 3;
795 args[3] = 4;
796 result.i = 0;
797 (*stub)(method, NULL, NULL, reinterpret_cast<byte*>(args), &result);
798 EXPECT_EQ(10, result.i);
799
800 args[0] = -1;
801 args[1] = 2;
802 args[2] = -3;
803 args[3] = 4;
804 result.i = 0;
805 (*stub)(method, NULL, NULL, reinterpret_cast<byte*>(args), &result);
806 EXPECT_EQ(2, result.i);
807
808 args[0] = INT_MAX;
809 args[1] = INT_MIN;
810 args[2] = INT_MAX;
811 args[3] = INT_MIN;
812 result.i = 1234;
813 (*stub)(method, NULL, NULL, reinterpret_cast<byte*>(args), &result);
814 EXPECT_EQ(-2, result.i);
815
816 args[0] = INT_MAX;
817 args[1] = INT_MAX;
818 args[2] = INT_MAX;
819 args[3] = INT_MAX;
820 result.i = INT_MIN;
821 (*stub)(method, NULL, NULL, reinterpret_cast<byte*>(args), &result);
822 EXPECT_EQ(-4, result.i);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700823}
824
825TEST_F(JniInternalTest, StaticSumIntIntIntIntIntMethod) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700826 scoped_ptr<DexFile> dex(OpenDexFileBase64(kStaticLeafMethodsDex, "kStaticLeafMethodsDex"));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700827
828 PathClassLoader* class_loader = AllocPathClassLoader(dex.get());
829 ASSERT_TRUE(class_loader != NULL);
830
831 Class* klass = class_linker_->FindClass("LStaticLeafMethods;", class_loader);
832 ASSERT_TRUE(klass != NULL);
833
834 Method* method = klass->FindDirectMethod("sum", "(IIIII)I");
835 ASSERT_TRUE(method != NULL);
836
837 byte sum_IIIIII_code[] = {
838 0x2d, 0xe9, 0x00, 0x40, 0x83, 0xb0, 0xcd, 0xf8,
839 0x00, 0x00, 0xcd, 0xf8, 0x14, 0x10, 0xcd, 0xf8,
840 0x18, 0x20, 0xcd, 0xf8, 0x1c, 0x30, 0x05, 0x98,
841 0x06, 0x99, 0x42, 0x18, 0xcd, 0xf8, 0x04, 0x20,
842 0x01, 0x9b, 0xdd, 0xf8, 0x1c, 0xc0, 0x13, 0xeb,
843 0x0c, 0x03, 0xcd, 0xf8, 0x04, 0x30, 0x01, 0x98,
844 0x08, 0x99, 0x40, 0x18, 0xcd, 0xf8, 0x04, 0x00,
845 0x01, 0x9a, 0x09, 0x9b, 0xd2, 0x18, 0xcd, 0xf8,
846 0x04, 0x20, 0x01, 0x98, 0x03, 0xb0, 0xbd, 0xe8,
847 0x00, 0x80, 0x00, 0x00,
848 };
849
850 Method::InvokeStub* stub = AllocateStub(method,
851 sum_IIIIII_code,
852 sizeof(sum_IIIIII_code));
853
854 int args[5];
855 JValue result;
856
857 args[0] = 0;
858 args[1] = 0;
859 args[2] = 0;
860 args[3] = 0;
861 args[4] = 0;
862 result.i = -1.0;
863 (*stub)(method, NULL, NULL, reinterpret_cast<byte*>(args), &result);
864 EXPECT_EQ(0, result.i);
865
866 args[0] = 1;
867 args[1] = 2;
868 args[2] = 3;
869 args[3] = 4;
870 args[4] = 5;
871 result.i = 0;
872 (*stub)(method, NULL, NULL, reinterpret_cast<byte*>(args), &result);
873 EXPECT_EQ(15, result.i);
874
875 args[0] = -1;
876 args[1] = 2;
877 args[2] = -3;
878 args[3] = 4;
879 args[4] = -5;
880 result.i = 0;
881 (*stub)(method, NULL, NULL, reinterpret_cast<byte*>(args), &result);
882 EXPECT_EQ(-3, result.i);
883
884 args[0] = INT_MAX;
885 args[1] = INT_MIN;
886 args[2] = INT_MAX;
887 args[3] = INT_MIN;
888 args[4] = INT_MAX;
889 result.i = 1234;
890 (*stub)(method, NULL, NULL, reinterpret_cast<byte*>(args), &result);
891 EXPECT_EQ(2147483645, result.i);
892
893 args[0] = INT_MAX;
894 args[1] = INT_MAX;
895 args[2] = INT_MAX;
896 args[3] = INT_MAX;
897 args[4] = INT_MAX;
898 result.i = INT_MIN;
899 (*stub)(method, NULL, NULL, reinterpret_cast<byte*>(args), &result);
900 EXPECT_EQ(2147483643, result.i);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700901}
902
903TEST_F(JniInternalTest, StaticSumDoubleDoubleMethod) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700904 scoped_ptr<DexFile> dex(OpenDexFileBase64(kStaticLeafMethodsDex, "kStaticLeafMethodsDex"));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700905
906 PathClassLoader* class_loader = AllocPathClassLoader(dex.get());
907 ASSERT_TRUE(class_loader != NULL);
908
909 Class* klass = class_linker_->FindClass("LStaticLeafMethods;", class_loader);
910 ASSERT_TRUE(klass != NULL);
911
912 Method* method = klass->FindDirectMethod("sum", "(DD)D");
913 ASSERT_TRUE(method != NULL);
914
915 byte sum_DDD_code[] = {
916 0x2d, 0xe9, 0x00, 0x40, 0x87, 0xb0, 0xcd, 0xf8,
917 0x00, 0x00, 0xcd, 0xf8, 0x24, 0x10, 0xcd, 0xf8,
918 0x28, 0x20, 0xcd, 0xf8, 0x2c, 0x30, 0x9d, 0xed,
919 0x09, 0x0b, 0x9d, 0xed, 0x0b, 0x1b, 0x30, 0xee,
920 0x01, 0x2b, 0x8d, 0xed, 0x04, 0x2b, 0x04, 0x98,
921 0x05, 0x99, 0x07, 0xb0, 0xbd, 0xe8, 0x00, 0x80,
922 };
923
924 Method::InvokeStub* stub = AllocateStub(method,
925 sum_DDD_code,
926 sizeof(sum_DDD_code));
927
928 double args[2];
929 JValue result;
930
931 args[0] = 0.0;
932 args[1] = 0.0;
933 result.d = -1.0;
934 (*stub)(method, NULL, NULL, reinterpret_cast<byte*>(args), &result);
935 EXPECT_EQ(0.0, result.d);
936
937 args[0] = 1.0;
938 args[1] = 2.0;
939 result.d = 0.0;
940 (*stub)(method, NULL, NULL, reinterpret_cast<byte*>(args), &result);
941 EXPECT_EQ(3.0, result.d);
942
943 args[0] = 1.0;
944 args[1] = -2.0;
945 result.d = 0.0;
946 (*stub)(method, NULL, NULL, reinterpret_cast<byte*>(args), &result);
947 EXPECT_EQ(-1.0, result.d);
948
949 args[0] = DBL_MAX;
950 args[1] = DBL_MIN;
951 result.d = 0.0;
952 (*stub)(method, NULL, NULL, reinterpret_cast<byte*>(args), &result);
953 EXPECT_EQ(1.7976931348623157e308, result.d);
954
955 args[0] = DBL_MAX;
956 args[1] = DBL_MAX;
957 result.d = 0.0;
958 (*stub)(method, NULL, NULL, reinterpret_cast<byte*>(args), &result);
959 EXPECT_EQ(INFINITY, result.d);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700960}
961
962TEST_F(JniInternalTest, StaticSumDoubleDoubleDoubleMethod) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700963 scoped_ptr<DexFile> dex(OpenDexFileBase64(kStaticLeafMethodsDex, "kStaticLeafMethodsDex"));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700964
965 PathClassLoader* class_loader = AllocPathClassLoader(dex.get());
966 ASSERT_TRUE(class_loader != NULL);
967
968 Class* klass = class_linker_->FindClass("LStaticLeafMethods;", class_loader);
969 ASSERT_TRUE(klass != NULL);
970
971 Method* method = klass->FindDirectMethod("sum", "(DDD)D");
972 ASSERT_TRUE(method != NULL);
973
974 byte sum_DDDD_code[] = {
975 0x2d, 0xe9, 0x00, 0x40, 0x87, 0xb0, 0xcd, 0xf8,
976 0x00, 0x00, 0xcd, 0xf8, 0x24, 0x10, 0xcd, 0xf8,
977 0x28, 0x20, 0xcd, 0xf8, 0x2c, 0x30, 0x9d, 0xed,
978 0x09, 0x0b, 0x9d, 0xed, 0x0b, 0x1b, 0x30, 0xee,
979 0x01, 0x2b, 0x8d, 0xed, 0x04, 0x2b, 0x9d, 0xed,
980 0x04, 0x3b, 0x9d, 0xed, 0x0d, 0x4b, 0x33, 0xee,
981 0x04, 0x3b, 0x8d, 0xed, 0x04, 0x3b, 0x04, 0x98,
982 0x05, 0x99, 0x07, 0xb0, 0xbd, 0xe8, 0x00, 0x80,
983 };
984
985 Method::InvokeStub* stub = AllocateStub(method,
986 sum_DDDD_code,
987 sizeof(sum_DDDD_code));
988
989 double args[3];
990 JValue result;
991
992 args[0] = 0.0;
993 args[1] = 0.0;
994 args[2] = 0.0;
995 result.d = -1.0;
996 (*stub)(method, NULL, NULL, reinterpret_cast<byte*>(args), &result);
997 EXPECT_EQ(0.0, result.d);
998
999 args[0] = 1.0;
1000 args[1] = 2.0;
1001 args[2] = 3.0;
1002 result.d = 0.0;
1003 (*stub)(method, NULL, NULL, reinterpret_cast<byte*>(args), &result);
1004 EXPECT_EQ(6.0, result.d);
1005
1006 args[0] = 1.0;
1007 args[1] = -2.0;
1008 args[2] = 3.0;
1009 result.d = 0.0;
1010 (*stub)(method, NULL, NULL, reinterpret_cast<byte*>(args), &result);
1011 EXPECT_EQ(2.0, result.d);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001012}
1013
1014TEST_F(JniInternalTest, StaticSumDoubleDoubleDoubleDoubleMethod) {
Brian Carlstroma663ea52011-08-19 23:33:41 -07001015 scoped_ptr<DexFile> dex(OpenDexFileBase64(kStaticLeafMethodsDex, "kStaticLeafMethodsDex"));
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001016
1017 PathClassLoader* class_loader = AllocPathClassLoader(dex.get());
1018 ASSERT_TRUE(class_loader != NULL);
1019
1020 Class* klass = class_linker_->FindClass("LStaticLeafMethods;", class_loader);
1021 ASSERT_TRUE(klass != NULL);
1022
1023 Method* method = klass->FindDirectMethod("sum", "(DDDD)D");
1024 ASSERT_TRUE(method != NULL);
1025
1026 byte sum_DDDDD_code[] = {
1027 0x2d, 0xe9, 0x00, 0x40, 0x87, 0xb0, 0xcd, 0xf8,
1028 0x00, 0x00, 0xcd, 0xf8, 0x24, 0x10, 0xcd, 0xf8,
1029 0x28, 0x20, 0xcd, 0xf8, 0x2c, 0x30, 0x9d, 0xed,
1030 0x09, 0x0b, 0x9d, 0xed, 0x0b, 0x1b, 0x30, 0xee,
1031 0x01, 0x2b, 0x8d, 0xed, 0x04, 0x2b, 0x9d, 0xed,
1032 0x04, 0x3b, 0x9d, 0xed, 0x0d, 0x4b, 0x33, 0xee,
1033 0x04, 0x3b, 0x8d, 0xed, 0x04, 0x3b, 0x9d, 0xed,
1034 0x04, 0x5b, 0x9d, 0xed, 0x0f, 0x6b, 0x35, 0xee,
1035 0x06, 0x5b, 0x8d, 0xed, 0x04, 0x5b, 0x04, 0x98,
1036 0x05, 0x99, 0x07, 0xb0, 0xbd, 0xe8, 0x00, 0x80,
1037 };
1038
1039 Method::InvokeStub* stub = AllocateStub(method,
1040 sum_DDDDD_code,
1041 sizeof(sum_DDDDD_code));
1042
1043 double args[4];
1044 JValue result;
1045
1046 args[0] = 0.0;
1047 args[1] = 0.0;
1048 args[2] = 0.0;
1049 args[3] = 0.0;
1050 result.d = -1.0;
1051 (*stub)(method, NULL, NULL, reinterpret_cast<byte*>(args), &result);
1052 EXPECT_EQ(0.0, result.d);
1053
1054 args[0] = 1.0;
1055 args[1] = 2.0;
1056 args[2] = 3.0;
1057 args[3] = 4.0;
1058 result.d = 0.0;
1059 (*stub)(method, NULL, NULL, reinterpret_cast<byte*>(args), &result);
1060 EXPECT_EQ(10.0, result.d);
1061
1062 args[0] = 1.0;
1063 args[1] = -2.0;
1064 args[2] = 3.0;
1065 args[3] = -4.0;
1066 result.d = 0.0;
1067 (*stub)(method, NULL, NULL, reinterpret_cast<byte*>(args), &result);
1068 EXPECT_EQ(-2.0, result.d);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001069}
1070
1071TEST_F(JniInternalTest, StaticSumDoubleDoubleDoubleDoubleDoubleMethod) {
Brian Carlstroma663ea52011-08-19 23:33:41 -07001072 scoped_ptr<DexFile> dex(OpenDexFileBase64(kStaticLeafMethodsDex, "kStaticLeafMethodsDex"));
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001073
1074 PathClassLoader* class_loader = AllocPathClassLoader(dex.get());
1075 ASSERT_TRUE(class_loader != NULL);
1076
1077 Class* klass = class_linker_->FindClass("LStaticLeafMethods;", class_loader);
1078 ASSERT_TRUE(klass != NULL);
1079
1080 Method* method = klass->FindDirectMethod("sum", "(DDDDD)D");
1081 ASSERT_TRUE(method != NULL);
1082
1083 byte sum_DDDDDD_code[] = {
1084 0x2d, 0xe9, 0x00, 0x40, 0x87, 0xb0, 0xcd, 0xf8,
1085 0x00, 0x00, 0xcd, 0xf8, 0x24, 0x10, 0xcd, 0xf8,
1086 0x28, 0x20, 0xcd, 0xf8, 0x2c, 0x30, 0x9d, 0xed,
1087 0x09, 0x0b, 0x9d, 0xed, 0x0b, 0x1b, 0x30, 0xee,
1088 0x01, 0x2b, 0x8d, 0xed, 0x04, 0x2b, 0x9d, 0xed,
1089 0x04, 0x3b, 0x9d, 0xed, 0x0d, 0x4b, 0x33, 0xee,
1090 0x04, 0x3b, 0x8d, 0xed, 0x04, 0x3b, 0x9d, 0xed,
1091 0x04, 0x5b, 0x9d, 0xed, 0x0f, 0x6b, 0x35, 0xee,
1092 0x06, 0x5b, 0x8d, 0xed, 0x04, 0x5b, 0x9d, 0xed,
1093 0x04, 0x7b, 0x9d, 0xed, 0x11, 0x0b, 0x37, 0xee,
1094 0x00, 0x7b, 0x8d, 0xed, 0x04, 0x7b, 0x04, 0x98,
1095 0x05, 0x99, 0x07, 0xb0, 0xbd, 0xe8, 0x00, 0x80,
1096 };
1097
1098 Method::InvokeStub* stub = AllocateStub(method,
1099 sum_DDDDDD_code,
1100 sizeof(sum_DDDDDD_code));
1101
1102 double args[5];
1103 JValue result;
1104
1105 args[0] = 0.0;
1106 args[1] = 0.0;
1107 args[2] = 0.0;
1108 args[3] = 0.0;
1109 args[4] = 0.0;
1110 result.d = -1.0;
1111 (*stub)(method, NULL, NULL, reinterpret_cast<byte*>(args), &result);
1112 EXPECT_EQ(0.0, result.d);
1113
1114 args[0] = 1.0;
1115 args[1] = 2.0;
1116 args[2] = 3.0;
1117 args[3] = 4.0;
1118 args[4] = 5.0;
1119 result.d = 0.0;
1120 (*stub)(method, NULL, NULL, reinterpret_cast<byte*>(args), &result);
1121 EXPECT_EQ(15.0, result.d);
1122
1123 args[0] = 1.0;
1124 args[1] = -2.0;
1125 args[2] = 3.0;
1126 args[3] = -4.0;
1127 args[4] = 5.0;
1128 result.d = 0.0;
1129 (*stub)(method, NULL, NULL, reinterpret_cast<byte*>(args), &result);
1130 EXPECT_EQ(3.0, result.d);
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001131}
1132#endif // __arm__
1133
1134}