Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 3 | #include "jni_internal.h" |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 4 | |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 5 | #include <sys/mman.h> |
| 6 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7 | #include <cmath> |
| 8 | |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 9 | #include "common_test.h" |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 10 | #include "ScopedLocalRef.h" |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 11 | |
| 12 | namespace art { |
| 13 | |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 14 | class JniInternalTest : public CommonTest { |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 15 | protected: |
| 16 | virtual void SetUp() { |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 17 | CommonTest::SetUp(); |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 18 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 19 | vm_ = Runtime::Current()->GetJavaVM(); |
| 20 | |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 21 | // Turn on -verbose:jni for the JNI tests. |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 22 | vm_->verbose_jni = true; |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 23 | |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 24 | env_ = Thread::Current()->GetJniEnv(); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 25 | |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 26 | ScopedLocalRef<jclass> aioobe(env_, env_->FindClass("java/lang/ArrayIndexOutOfBoundsException")); |
| 27 | CHECK(aioobe.get() != NULL); |
| 28 | aioobe_ = reinterpret_cast<jclass>(env_->NewGlobalRef(aioobe.get())); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 29 | |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 30 | 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 Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 39 | } |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 40 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 41 | JavaVMExt* vm_; |
| 42 | JNIEnvExt* env_; |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 43 | jclass aioobe_; |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 44 | jclass sioobe_; |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 45 | }; |
| 46 | |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 47 | TEST_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 Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 62 | TEST_F(JniInternalTest, GetVersion) { |
| 63 | ASSERT_EQ(JNI_VERSION_1_6, env_->GetVersion()); |
| 64 | } |
| 65 | |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 66 | #define EXPECT_CLASS_FOUND(NAME) \ |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 67 | EXPECT_TRUE(env_->FindClass(NAME) != NULL); \ |
| 68 | EXPECT_FALSE(env_->ExceptionCheck()) |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 69 | |
| 70 | #define EXPECT_CLASS_NOT_FOUND(NAME) \ |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 71 | EXPECT_TRUE(env_->FindClass(NAME) == NULL); \ |
| 72 | EXPECT_TRUE(env_->ExceptionCheck()); \ |
| 73 | env_->ExceptionClear() |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 74 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 75 | std::string gCheckJniAbortMessage; |
| 76 | void TestCheckJniAbortHook(const std::string& reason) { |
| 77 | gCheckJniAbortMessage = reason; |
| 78 | } |
| 79 | |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 80 | TEST_F(JniInternalTest, FindClass) { |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 81 | // Reference types... |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 82 | EXPECT_CLASS_FOUND("java/lang/String"); |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 83 | // ...for arrays too, where you must include "L;". |
| 84 | EXPECT_CLASS_FOUND("[Ljava/lang/String;"); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 85 | |
| 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 Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 90 | EXPECT_CLASS_FOUND("[Ljava.lang.String;"); |
| 91 | EXPECT_CLASS_NOT_FOUND("[java.lang.String"); |
| 92 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 93 | // 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 Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 99 | // Primitive arrays are okay (if the primitive type is valid)... |
| 100 | EXPECT_CLASS_FOUND("[C"); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 101 | vm_->check_jni_abort_hook = TestCheckJniAbortHook; |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 102 | EXPECT_CLASS_NOT_FOUND("[K"); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 103 | vm_->check_jni_abort_hook = NULL; |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 104 | // But primitive types aren't allowed... |
| 105 | EXPECT_CLASS_NOT_FOUND("C"); |
| 106 | EXPECT_CLASS_NOT_FOUND("K"); |
| 107 | } |
| 108 | |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 109 | #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 Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 114 | env_->ExceptionClear(); \ |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 115 | EXPECT_TRUE(env_->IsInstanceOf(exception, exception_class)); \ |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 116 | } while (false) |
| 117 | |
| 118 | TEST_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 Rogers | b17d08b | 2011-09-02 16:16:49 -0700 | [diff] [blame] | 129 | // 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 Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 134 | // 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 | |
| 158 | TEST_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 Rogers | b17d08b | 2011-09-02 16:16:49 -0700 | [diff] [blame] | 169 | // 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 Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 174 | // 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 Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 191 | TEST_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 Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 197 | ASSERT_FALSE(env_->ExceptionCheck()); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 198 | |
| 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 Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 203 | EXPECT_EXCEPTION(jlnsme); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 204 | |
| 205 | // Check that java.lang.Object.equals() does exist |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 206 | method = env_->GetMethodID(jlobject, "equals", "(Ljava/lang/Object;)Z"); |
| 207 | EXPECT_NE(static_cast<jmethodID>(NULL), method); |
| 208 | EXPECT_FALSE(env_->ExceptionCheck()); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 209 | |
| 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 Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 214 | EXPECT_EXCEPTION(jlnsme); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | TEST_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 Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 222 | ASSERT_FALSE(env_->ExceptionCheck()); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 223 | |
| 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 Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 228 | EXPECT_EXCEPTION(jlnsme); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 229 | |
| 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 Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 234 | EXPECT_EXCEPTION(jlnsme); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 235 | |
| 236 | // Check that java.lang.String.valueOf(int) does exist |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 237 | 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 Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 244 | TEST_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 | |
| 259 | TEST_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 Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 274 | void BogusMethod() { |
| 275 | // You can't pass NULL function pointers to RegisterNatives. |
| 276 | } |
| 277 | |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 278 | TEST_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 Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 283 | ASSERT_FALSE(env_->ExceptionCheck()); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 284 | |
| 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 Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 291 | EXPECT_EXCEPTION(jlnsme); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 292 | |
| 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 Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 298 | EXPECT_EXCEPTION(jlnsme); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 299 | |
| 300 | // Check that registering native methods is successful |
| 301 | { |
Elliott Hughes | ad7c2a3 | 2011-08-31 11:58:10 -0700 | [diff] [blame] | 302 | JNINativeMethod methods[] = {{"getClass", "()Ljava/lang/Class;", reinterpret_cast<void*>(BogusMethod)}}; |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 303 | env_->RegisterNatives(jlobject, methods, 1); |
| 304 | } |
| 305 | EXPECT_FALSE(env_->ExceptionCheck()); |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 306 | |
| 307 | env_->UnregisterNatives(jlobject); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 310 | #define EXPECT_PRIMITIVE_ARRAY(new_fn, get_region_fn, set_region_fn, get_elements_fn, release_elements_fn, scalar_type, expected_class_descriptor) \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 311 | 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]); \ |
Elliott Hughes | cc60747 | 2011-10-17 15:34:11 -0700 | [diff] [blame^] | 341 | EXPECT_NE(memcmp(src_buf, dst_buf, sizeof(src_buf)), 0) << "short copy equal"; \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 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]); \ |
Elliott Hughes | cc60747 | 2011-10-17 15:34:11 -0700 | [diff] [blame^] | 345 | EXPECT_EQ(memcmp(src_buf, dst_buf, sizeof(src_buf)), 0) << "fixed copy not equal"; \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 346 | /* Copy back the whole array. */ \ |
| 347 | env_->get_region_fn(a, 0, size, dst_buf); \ |
Elliott Hughes | cc60747 | 2011-10-17 15:34:11 -0700 | [diff] [blame^] | 348 | EXPECT_EQ(memcmp(src_buf, dst_buf, sizeof(src_buf)), 0) << "full copy not equal"; \ |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 349 | /* GetPrimitiveArrayCritical */ \ |
| 350 | void* v = env_->GetPrimitiveArrayCritical(a, NULL); \ |
Elliott Hughes | cc60747 | 2011-10-17 15:34:11 -0700 | [diff] [blame^] | 351 | EXPECT_EQ(memcmp(src_buf, v, sizeof(src_buf)), 0) << "GetPrimitiveArrayCritical not equal"; \ |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 352 | env_->ReleasePrimitiveArrayCritical(a, v, 0); \ |
| 353 | /* GetXArrayElements */ \ |
| 354 | scalar_type* xs = env_->get_elements_fn(a, NULL); \ |
Elliott Hughes | cc60747 | 2011-10-17 15:34:11 -0700 | [diff] [blame^] | 355 | EXPECT_EQ(memcmp(src_buf, xs, sizeof(src_buf)), 0) << # get_elements_fn " not equal"; \ |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 356 | env_->release_elements_fn(a, xs, 0); \ |
| 357 | EXPECT_EQ(reinterpret_cast<uintptr_t>(v), reinterpret_cast<uintptr_t>(xs)) |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 358 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 359 | TEST_F(JniInternalTest, BooleanArrays) { |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 360 | EXPECT_PRIMITIVE_ARRAY(NewBooleanArray, GetBooleanArrayRegion, SetBooleanArrayRegion, GetBooleanArrayElements, ReleaseBooleanArrayElements, jboolean, "[Z"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 361 | } |
| 362 | TEST_F(JniInternalTest, ByteArrays) { |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 363 | EXPECT_PRIMITIVE_ARRAY(NewByteArray, GetByteArrayRegion, SetByteArrayRegion, GetByteArrayElements, ReleaseByteArrayElements, jbyte, "[B"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 364 | } |
| 365 | TEST_F(JniInternalTest, CharArrays) { |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 366 | EXPECT_PRIMITIVE_ARRAY(NewCharArray, GetCharArrayRegion, SetCharArrayRegion, GetCharArrayElements, ReleaseCharArrayElements, jchar, "[C"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 367 | } |
| 368 | TEST_F(JniInternalTest, DoubleArrays) { |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 369 | EXPECT_PRIMITIVE_ARRAY(NewDoubleArray, GetDoubleArrayRegion, SetDoubleArrayRegion, GetDoubleArrayElements, ReleaseDoubleArrayElements, jdouble, "[D"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 370 | } |
| 371 | TEST_F(JniInternalTest, FloatArrays) { |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 372 | EXPECT_PRIMITIVE_ARRAY(NewFloatArray, GetFloatArrayRegion, SetFloatArrayRegion, GetFloatArrayElements, ReleaseFloatArrayElements, jfloat, "[F"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 373 | } |
| 374 | TEST_F(JniInternalTest, IntArrays) { |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 375 | EXPECT_PRIMITIVE_ARRAY(NewIntArray, GetIntArrayRegion, SetIntArrayRegion, GetIntArrayElements, ReleaseIntArrayElements, jint, "[I"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 376 | } |
| 377 | TEST_F(JniInternalTest, LongArrays) { |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 378 | EXPECT_PRIMITIVE_ARRAY(NewLongArray, GetLongArrayRegion, SetLongArrayRegion, GetLongArrayElements, ReleaseLongArrayElements, jlong, "[J"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 379 | } |
| 380 | TEST_F(JniInternalTest, ShortArrays) { |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 381 | EXPECT_PRIMITIVE_ARRAY(NewShortArray, GetShortArrayRegion, SetShortArrayRegion, GetShortArrayElements, ReleaseShortArrayElements, jshort, "[S"); |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 382 | } |
| 383 | |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 384 | TEST_F(JniInternalTest, NewObjectArray) { |
| 385 | // TODO: death tests for negative array sizes. |
| 386 | |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 387 | // TODO: check non-NULL initial elements. |
| 388 | |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 389 | 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 Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 393 | |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 394 | jobjectArray a; |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 395 | |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 396 | 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 Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 405 | 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 Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | TEST_F(JniInternalTest, GetArrayLength) { |
| 417 | // Already tested in NewObjectArray/NewPrimitiveArray. |
Elliott Hughes | 8a26c5c | 2011-08-15 18:35:43 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 420 | TEST_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 Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 434 | TEST_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 Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 443 | TEST_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 Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 453 | TEST_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 Hughes | 8a26c5c | 2011-08-15 18:35:43 -0700 | [diff] [blame] | 470 | TEST_F(JniInternalTest, NewStringUTF) { |
| 471 | EXPECT_TRUE(env_->NewStringUTF(NULL) == NULL); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 472 | 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 Hughes | 8a26c5c | 2011-08-15 18:35:43 -0700 | [diff] [blame] | 483 | // TODO: check some non-ASCII strings. |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 484 | } |
| 485 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 486 | TEST_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 Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 503 | TEST_F(JniInternalTest, GetStringLength_GetStringUTFLength) { |
| 504 | // Already tested in the NewString/NewStringUTF tests. |
| 505 | } |
| 506 | |
| 507 | TEST_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 Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 544 | TEST_F(JniInternalTest, GetStringUTFChars_ReleaseStringUTFChars) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 545 | vm_->check_jni_abort_hook = TestCheckJniAbortHook; |
| 546 | // Passing in a NULL jstring is ignored normally, but caught by -Xcheck:jni. |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 547 | EXPECT_TRUE(env_->GetStringUTFChars(NULL, NULL) == NULL); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 548 | vm_->check_jni_abort_hook = NULL; |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 549 | |
| 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 | |
| 564 | TEST_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 | |
| 588 | TEST_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 Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 612 | TEST_F(JniInternalTest, GetObjectArrayElement_SetObjectArrayElement) { |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 613 | jclass c = env_->FindClass("java/lang/Object"); |
Elliott Hughes | 289da82 | 2011-08-16 10:11:20 -0700 | [diff] [blame] | 614 | ASSERT_TRUE(c != NULL); |
| 615 | |
| 616 | jobjectArray array = env_->NewObjectArray(1, c, NULL); |
| 617 | EXPECT_TRUE(array != NULL); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 618 | EXPECT_TRUE(env_->GetObjectArrayElement(array, 0) == NULL); |
Elliott Hughes | 289da82 | 2011-08-16 10:11:20 -0700 | [diff] [blame] | 619 | env_->SetObjectArrayElement(array, 0, c); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 620 | EXPECT_TRUE(env_->IsSameObject(env_->GetObjectArrayElement(array, 0), c)); |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 621 | |
| 622 | // ArrayIndexOutOfBounds for negative index. |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 623 | env_->SetObjectArrayElement(array, -1, c); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 624 | EXPECT_EXCEPTION(aioobe_); |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 625 | |
| 626 | // ArrayIndexOutOfBounds for too-large index. |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 627 | env_->SetObjectArrayElement(array, 1, c); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 628 | EXPECT_EXCEPTION(aioobe_); |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 629 | |
Elliott Hughes | 289da82 | 2011-08-16 10:11:20 -0700 | [diff] [blame] | 630 | // TODO: check ArrayStoreException thrown for bad types. |
| 631 | } |
| 632 | |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 633 | #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 | |
| 654 | TEST_F(JniInternalTest, GetPrimitiveField_SetPrimitiveField) { |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 655 | LoadDex("AllFields"); |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 656 | runtime_->Start(); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 657 | |
| 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 | |
| 682 | TEST_F(JniInternalTest, GetObjectField_SetObjectField) { |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 683 | LoadDex("AllFields"); |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 684 | runtime_->Start(); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 685 | |
| 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 Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 712 | TEST_F(JniInternalTest, NewLocalRef_NULL) { |
| 713 | EXPECT_TRUE(env_->NewLocalRef(NULL) == NULL); |
| 714 | } |
| 715 | |
| 716 | TEST_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 | |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 723 | EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(o)); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | TEST_F(JniInternalTest, DeleteLocalRef_NULL) { |
| 727 | env_->DeleteLocalRef(NULL); |
| 728 | } |
| 729 | |
| 730 | TEST_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 Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 736 | vm_->check_jni_abort_hook = TestCheckJniAbortHook; |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 737 | env_->DeleteLocalRef(s); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 738 | vm_->check_jni_abort_hook = NULL; |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 739 | |
| 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 | |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 749 | TEST_F(JniInternalTest, PushLocalFrame_PopLocalFrame) { |
| 750 | jobject original = env_->NewStringUTF(""); |
| 751 | ASSERT_TRUE(original != NULL); |
| 752 | |
| 753 | jobject outer; |
| 754 | jobject inner1, inner2; |
| 755 | Object* inner2_direct_pointer; |
| 756 | { |
| 757 | env_->PushLocalFrame(4); |
| 758 | outer = env_->NewLocalRef(original); |
| 759 | |
| 760 | { |
| 761 | env_->PushLocalFrame(4); |
| 762 | inner1 = env_->NewLocalRef(outer); |
| 763 | inner2 = env_->NewStringUTF("survivor"); |
| 764 | inner2_direct_pointer = Decode<Object*>(env_, inner2); |
| 765 | env_->PopLocalFrame(inner2); |
| 766 | } |
| 767 | |
| 768 | EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(original)); |
| 769 | EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(outer)); |
| 770 | EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(inner1)); |
| 771 | |
| 772 | // Our local reference for the survivor is invalid because the survivor |
| 773 | // gets a new local reference... |
| 774 | EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(inner2)); |
| 775 | // ...but the survivor should be in the local reference table. |
| 776 | EXPECT_TRUE(env_->locals.ContainsDirectPointer(inner2_direct_pointer)); |
| 777 | |
| 778 | env_->PopLocalFrame(NULL); |
| 779 | } |
| 780 | EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(original)); |
| 781 | EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(outer)); |
| 782 | EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(inner1)); |
| 783 | EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(inner2)); |
| 784 | } |
| 785 | |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 786 | TEST_F(JniInternalTest, NewGlobalRef_NULL) { |
| 787 | EXPECT_TRUE(env_->NewGlobalRef(NULL) == NULL); |
| 788 | } |
| 789 | |
| 790 | TEST_F(JniInternalTest, NewGlobalRef) { |
| 791 | jstring s = env_->NewStringUTF(""); |
| 792 | ASSERT_TRUE(s != NULL); |
| 793 | jobject o = env_->NewGlobalRef(s); |
| 794 | EXPECT_TRUE(o != NULL); |
| 795 | EXPECT_TRUE(o != s); |
| 796 | |
| 797 | // TODO: check that o is a global reference. |
| 798 | } |
| 799 | |
| 800 | TEST_F(JniInternalTest, DeleteGlobalRef_NULL) { |
| 801 | env_->DeleteGlobalRef(NULL); |
| 802 | } |
| 803 | |
| 804 | TEST_F(JniInternalTest, DeleteGlobalRef) { |
| 805 | jstring s = env_->NewStringUTF(""); |
| 806 | ASSERT_TRUE(s != NULL); |
| 807 | |
| 808 | jobject o = env_->NewGlobalRef(s); |
| 809 | ASSERT_TRUE(o != NULL); |
| 810 | env_->DeleteGlobalRef(o); |
| 811 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 812 | vm_->check_jni_abort_hook = TestCheckJniAbortHook; |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 813 | // Currently, deleting an already-deleted reference is just a warning. |
| 814 | env_->DeleteGlobalRef(o); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 815 | vm_->check_jni_abort_hook = NULL; |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 816 | |
| 817 | jobject o1 = env_->NewGlobalRef(s); |
| 818 | ASSERT_TRUE(o1 != NULL); |
| 819 | jobject o2 = env_->NewGlobalRef(s); |
| 820 | ASSERT_TRUE(o2 != NULL); |
| 821 | |
| 822 | env_->DeleteGlobalRef(o1); |
| 823 | env_->DeleteGlobalRef(o2); |
| 824 | } |
| 825 | |
| 826 | TEST_F(JniInternalTest, NewWeakGlobalRef_NULL) { |
| 827 | EXPECT_TRUE(env_->NewWeakGlobalRef(NULL) == NULL); |
| 828 | } |
| 829 | |
| 830 | TEST_F(JniInternalTest, NewWeakGlobalRef) { |
| 831 | jstring s = env_->NewStringUTF(""); |
| 832 | ASSERT_TRUE(s != NULL); |
| 833 | jobject o = env_->NewWeakGlobalRef(s); |
| 834 | EXPECT_TRUE(o != NULL); |
| 835 | EXPECT_TRUE(o != s); |
| 836 | |
| 837 | // TODO: check that o is a weak global reference. |
| 838 | } |
| 839 | |
| 840 | TEST_F(JniInternalTest, DeleteWeakGlobalRef_NULL) { |
| 841 | env_->DeleteWeakGlobalRef(NULL); |
| 842 | } |
| 843 | |
| 844 | TEST_F(JniInternalTest, DeleteWeakGlobalRef) { |
| 845 | jstring s = env_->NewStringUTF(""); |
| 846 | ASSERT_TRUE(s != NULL); |
| 847 | |
| 848 | jobject o = env_->NewWeakGlobalRef(s); |
| 849 | ASSERT_TRUE(o != NULL); |
| 850 | env_->DeleteWeakGlobalRef(o); |
| 851 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 852 | vm_->check_jni_abort_hook = TestCheckJniAbortHook; |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 853 | // Currently, deleting an already-deleted reference is just a warning. |
| 854 | env_->DeleteWeakGlobalRef(o); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 855 | vm_->check_jni_abort_hook = NULL; |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 856 | |
| 857 | jobject o1 = env_->NewWeakGlobalRef(s); |
| 858 | ASSERT_TRUE(o1 != NULL); |
| 859 | jobject o2 = env_->NewWeakGlobalRef(s); |
| 860 | ASSERT_TRUE(o2 != NULL); |
| 861 | |
| 862 | env_->DeleteWeakGlobalRef(o1); |
| 863 | env_->DeleteWeakGlobalRef(o2); |
| 864 | } |
| 865 | |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 866 | #if defined(__arm__) |
| 867 | TEST_F(JniInternalTest, StaticMainMethod) { |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 868 | const ClassLoader* class_loader = LoadDex("Main"); |
| 869 | CompileDirectMethod(class_loader, "Main", "main", "([Ljava/lang/String;)V"); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 870 | |
| 871 | Class* klass = class_linker_->FindClass("LMain;", class_loader); |
| 872 | ASSERT_TRUE(klass != NULL); |
| 873 | |
| 874 | Method* method = klass->FindDirectMethod("main", "([Ljava/lang/String;)V"); |
| 875 | ASSERT_TRUE(method != NULL); |
| 876 | |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 877 | Method::InvokeStub* stub = method->GetInvokeStub(); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 878 | |
| 879 | Object* arg = NULL; |
| 880 | |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 881 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(&arg), NULL); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | TEST_F(JniInternalTest, StaticNopMethod) { |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 885 | const ClassLoader* class_loader = LoadDex("StaticLeafMethods"); |
| 886 | CompileDirectMethod(class_loader, "StaticLeafMethods", "nop", "()V"); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 887 | |
| 888 | Class* klass = class_linker_->FindClass("LStaticLeafMethods;", class_loader); |
| 889 | ASSERT_TRUE(klass != NULL); |
| 890 | |
| 891 | Method* method = klass->FindDirectMethod("nop", "()V"); |
| 892 | ASSERT_TRUE(method != NULL); |
| 893 | |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 894 | Method::InvokeStub* stub = method->GetInvokeStub(); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 895 | |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 896 | (*stub)(method, NULL, Thread::Current(), NULL, NULL); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 897 | } |
| 898 | |
| 899 | TEST_F(JniInternalTest, StaticIdentityByteMethod) { |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 900 | const ClassLoader* class_loader = LoadDex("StaticLeafMethods"); |
| 901 | CompileDirectMethod(class_loader, "StaticLeafMethods", "identity", "(B)B"); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 902 | |
| 903 | Class* klass = class_linker_->FindClass("LStaticLeafMethods;", class_loader); |
| 904 | ASSERT_TRUE(klass != NULL); |
| 905 | |
| 906 | Method* method = klass->FindDirectMethod("identity", "(B)B"); |
| 907 | ASSERT_TRUE(method != NULL); |
| 908 | |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 909 | Method::InvokeStub* stub = method->GetInvokeStub(); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 910 | |
| 911 | int arg; |
| 912 | JValue result; |
| 913 | |
| 914 | arg = 0; |
| 915 | result.b = -1; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 916 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(&arg), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 917 | EXPECT_EQ(0, result.b); |
| 918 | |
| 919 | arg = -1; |
| 920 | result.b = 0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 921 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(&arg), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 922 | EXPECT_EQ(-1, result.b); |
| 923 | |
| 924 | arg = SCHAR_MAX; |
| 925 | result.b = 0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 926 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(&arg), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 927 | EXPECT_EQ(SCHAR_MAX, result.b); |
| 928 | |
| 929 | arg = SCHAR_MIN; |
| 930 | result.b = 0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 931 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(&arg), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 932 | EXPECT_EQ(SCHAR_MIN, result.b); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | TEST_F(JniInternalTest, StaticIdentityIntMethod) { |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 936 | const ClassLoader* class_loader = LoadDex("StaticLeafMethods"); |
| 937 | CompileDirectMethod(class_loader, "StaticLeafMethods", "identity", "(I)I"); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 938 | |
| 939 | Class* klass = class_linker_->FindClass("LStaticLeafMethods;", class_loader); |
| 940 | ASSERT_TRUE(klass != NULL); |
| 941 | |
| 942 | Method* method = klass->FindDirectMethod("identity", "(I)I"); |
| 943 | ASSERT_TRUE(method != NULL); |
| 944 | |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 945 | Method::InvokeStub* stub = method->GetInvokeStub(); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 946 | |
| 947 | int arg; |
| 948 | JValue result; |
| 949 | |
| 950 | arg = 0; |
| 951 | result.i = -1; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 952 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(&arg), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 953 | EXPECT_EQ(0, result.i); |
| 954 | |
| 955 | arg = -1; |
| 956 | result.i = 0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 957 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(&arg), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 958 | EXPECT_EQ(-1, result.i); |
| 959 | |
| 960 | arg = INT_MAX; |
| 961 | result.i = 0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 962 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(&arg), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 963 | EXPECT_EQ(INT_MAX, result.i); |
| 964 | |
| 965 | arg = INT_MIN; |
| 966 | result.i = 0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 967 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(&arg), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 968 | EXPECT_EQ(INT_MIN, result.i); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | TEST_F(JniInternalTest, StaticIdentityDoubleMethod) { |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 972 | const ClassLoader* class_loader = LoadDex("StaticLeafMethods"); |
| 973 | CompileDirectMethod(class_loader, "StaticLeafMethods", "identity", "(D)D"); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 974 | |
| 975 | Class* klass = class_linker_->FindClass("LStaticLeafMethods;", class_loader); |
| 976 | ASSERT_TRUE(klass != NULL); |
| 977 | |
| 978 | Method* method = klass->FindDirectMethod("identity", "(D)D"); |
| 979 | ASSERT_TRUE(method != NULL); |
| 980 | |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 981 | Method::InvokeStub* stub = method->GetInvokeStub(); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 982 | |
| 983 | double arg; |
| 984 | JValue result; |
| 985 | |
| 986 | arg = 0.0; |
| 987 | result.d = -1.0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 988 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(&arg), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 989 | EXPECT_EQ(0.0, result.d); |
| 990 | |
| 991 | arg = -1.0; |
| 992 | result.d = 0.0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 993 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(&arg), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 994 | EXPECT_EQ(-1.0, result.d); |
| 995 | |
| 996 | arg = DBL_MAX; |
| 997 | result.d = 0.0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 998 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(&arg), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 999 | EXPECT_EQ(DBL_MAX, result.d); |
| 1000 | |
| 1001 | arg = DBL_MIN; |
| 1002 | result.d = 0.0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1003 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(&arg), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1004 | EXPECT_EQ(DBL_MIN, result.d); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | TEST_F(JniInternalTest, StaticSumIntIntMethod) { |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 1008 | const ClassLoader* class_loader = LoadDex("StaticLeafMethods"); |
| 1009 | CompileDirectMethod(class_loader, "StaticLeafMethods", "sum", "(II)I"); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1010 | |
| 1011 | Class* klass = class_linker_->FindClass("LStaticLeafMethods;", class_loader); |
| 1012 | ASSERT_TRUE(klass != NULL); |
| 1013 | |
| 1014 | Method* method = klass->FindDirectMethod("sum", "(II)I"); |
| 1015 | ASSERT_TRUE(method != NULL); |
| 1016 | |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 1017 | Method::InvokeStub* stub = method->GetInvokeStub(); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1018 | |
| 1019 | int args[2]; |
| 1020 | JValue result; |
| 1021 | |
| 1022 | args[0] = 0; |
| 1023 | args[1] = 0; |
| 1024 | result.i = -1; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1025 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1026 | EXPECT_EQ(0, result.i); |
| 1027 | |
| 1028 | args[0] = 1; |
| 1029 | args[1] = 2; |
| 1030 | result.i = 0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1031 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1032 | EXPECT_EQ(3, result.i); |
| 1033 | |
| 1034 | args[0] = -2; |
| 1035 | args[1] = 5; |
| 1036 | result.i = 0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1037 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1038 | EXPECT_EQ(3, result.i); |
| 1039 | |
| 1040 | args[0] = INT_MAX; |
| 1041 | args[1] = INT_MIN; |
| 1042 | result.i = 1234; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1043 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1044 | EXPECT_EQ(-1, result.i); |
| 1045 | |
| 1046 | args[0] = INT_MAX; |
| 1047 | args[1] = INT_MAX; |
| 1048 | result.i = INT_MIN; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1049 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1050 | EXPECT_EQ(-2, result.i); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1051 | } |
| 1052 | |
| 1053 | TEST_F(JniInternalTest, StaticSumIntIntIntMethod) { |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 1054 | const ClassLoader* class_loader = LoadDex("StaticLeafMethods"); |
| 1055 | CompileDirectMethod(class_loader, "StaticLeafMethods", "sum", "(III)I"); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1056 | |
| 1057 | Class* klass = class_linker_->FindClass("LStaticLeafMethods;", class_loader); |
| 1058 | ASSERT_TRUE(klass != NULL); |
| 1059 | |
| 1060 | Method* method = klass->FindDirectMethod("sum", "(III)I"); |
| 1061 | ASSERT_TRUE(method != NULL); |
| 1062 | |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 1063 | Method::InvokeStub* stub = method->GetInvokeStub(); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1064 | |
| 1065 | int args[3]; |
| 1066 | JValue result; |
| 1067 | |
| 1068 | args[0] = 0; |
| 1069 | args[1] = 0; |
| 1070 | args[2] = 0; |
| 1071 | result.i = -1; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1072 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1073 | EXPECT_EQ(0, result.i); |
| 1074 | |
| 1075 | args[0] = 1; |
| 1076 | args[1] = 2; |
| 1077 | args[2] = 3; |
| 1078 | result.i = 0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1079 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1080 | EXPECT_EQ(6, result.i); |
| 1081 | |
| 1082 | args[0] = -1; |
| 1083 | args[1] = 2; |
| 1084 | args[2] = -3; |
| 1085 | result.i = 0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1086 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1087 | EXPECT_EQ(-2, result.i); |
| 1088 | |
| 1089 | args[0] = INT_MAX; |
| 1090 | args[1] = INT_MIN; |
| 1091 | args[2] = INT_MAX; |
| 1092 | result.i = 1234; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1093 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1094 | EXPECT_EQ(2147483646, result.i); |
| 1095 | |
| 1096 | args[0] = INT_MAX; |
| 1097 | args[1] = INT_MAX; |
| 1098 | args[2] = INT_MAX; |
| 1099 | result.i = INT_MIN; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1100 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1101 | EXPECT_EQ(2147483645, result.i); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1102 | } |
| 1103 | |
| 1104 | TEST_F(JniInternalTest, StaticSumIntIntIntIntMethod) { |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 1105 | const ClassLoader* class_loader = LoadDex("StaticLeafMethods"); |
| 1106 | CompileDirectMethod(class_loader, "StaticLeafMethods", "sum", "(IIII)I"); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1107 | |
| 1108 | Class* klass = class_linker_->FindClass("LStaticLeafMethods;", class_loader); |
| 1109 | ASSERT_TRUE(klass != NULL); |
| 1110 | |
| 1111 | Method* method = klass->FindDirectMethod("sum", "(IIII)I"); |
| 1112 | ASSERT_TRUE(method != NULL); |
| 1113 | |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 1114 | Method::InvokeStub* stub = method->GetInvokeStub(); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1115 | |
| 1116 | int args[4]; |
| 1117 | JValue result; |
| 1118 | |
| 1119 | args[0] = 0; |
| 1120 | args[1] = 0; |
| 1121 | args[2] = 0; |
| 1122 | args[3] = 0; |
| 1123 | result.i = -1; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1124 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1125 | EXPECT_EQ(0, result.i); |
| 1126 | |
| 1127 | args[0] = 1; |
| 1128 | args[1] = 2; |
| 1129 | args[2] = 3; |
| 1130 | args[3] = 4; |
| 1131 | result.i = 0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1132 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1133 | EXPECT_EQ(10, result.i); |
| 1134 | |
| 1135 | args[0] = -1; |
| 1136 | args[1] = 2; |
| 1137 | args[2] = -3; |
| 1138 | args[3] = 4; |
| 1139 | result.i = 0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1140 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1141 | EXPECT_EQ(2, result.i); |
| 1142 | |
| 1143 | args[0] = INT_MAX; |
| 1144 | args[1] = INT_MIN; |
| 1145 | args[2] = INT_MAX; |
| 1146 | args[3] = INT_MIN; |
| 1147 | result.i = 1234; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1148 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1149 | EXPECT_EQ(-2, result.i); |
| 1150 | |
| 1151 | args[0] = INT_MAX; |
| 1152 | args[1] = INT_MAX; |
| 1153 | args[2] = INT_MAX; |
| 1154 | args[3] = INT_MAX; |
| 1155 | result.i = INT_MIN; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1156 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1157 | EXPECT_EQ(-4, result.i); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1158 | } |
| 1159 | |
| 1160 | TEST_F(JniInternalTest, StaticSumIntIntIntIntIntMethod) { |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 1161 | const ClassLoader* class_loader = LoadDex("StaticLeafMethods"); |
| 1162 | CompileDirectMethod(class_loader, "StaticLeafMethods", "sum", "(IIIII)I"); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1163 | |
| 1164 | Class* klass = class_linker_->FindClass("LStaticLeafMethods;", class_loader); |
| 1165 | ASSERT_TRUE(klass != NULL); |
| 1166 | |
| 1167 | Method* method = klass->FindDirectMethod("sum", "(IIIII)I"); |
| 1168 | ASSERT_TRUE(method != NULL); |
| 1169 | |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 1170 | Method::InvokeStub* stub = method->GetInvokeStub(); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1171 | |
| 1172 | int args[5]; |
| 1173 | JValue result; |
| 1174 | |
| 1175 | args[0] = 0; |
| 1176 | args[1] = 0; |
| 1177 | args[2] = 0; |
| 1178 | args[3] = 0; |
| 1179 | args[4] = 0; |
| 1180 | result.i = -1.0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1181 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1182 | EXPECT_EQ(0, result.i); |
| 1183 | |
| 1184 | args[0] = 1; |
| 1185 | args[1] = 2; |
| 1186 | args[2] = 3; |
| 1187 | args[3] = 4; |
| 1188 | args[4] = 5; |
| 1189 | result.i = 0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1190 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1191 | EXPECT_EQ(15, result.i); |
| 1192 | |
| 1193 | args[0] = -1; |
| 1194 | args[1] = 2; |
| 1195 | args[2] = -3; |
| 1196 | args[3] = 4; |
| 1197 | args[4] = -5; |
| 1198 | result.i = 0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1199 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1200 | EXPECT_EQ(-3, result.i); |
| 1201 | |
| 1202 | args[0] = INT_MAX; |
| 1203 | args[1] = INT_MIN; |
| 1204 | args[2] = INT_MAX; |
| 1205 | args[3] = INT_MIN; |
| 1206 | args[4] = INT_MAX; |
| 1207 | result.i = 1234; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1208 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1209 | EXPECT_EQ(2147483645, result.i); |
| 1210 | |
| 1211 | args[0] = INT_MAX; |
| 1212 | args[1] = INT_MAX; |
| 1213 | args[2] = INT_MAX; |
| 1214 | args[3] = INT_MAX; |
| 1215 | args[4] = INT_MAX; |
| 1216 | result.i = INT_MIN; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1217 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1218 | EXPECT_EQ(2147483643, result.i); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1219 | } |
| 1220 | |
| 1221 | TEST_F(JniInternalTest, StaticSumDoubleDoubleMethod) { |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 1222 | const ClassLoader* class_loader = LoadDex("StaticLeafMethods"); |
| 1223 | CompileDirectMethod(class_loader, "StaticLeafMethods", "sum", "(DD)D"); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1224 | |
| 1225 | Class* klass = class_linker_->FindClass("LStaticLeafMethods;", class_loader); |
| 1226 | ASSERT_TRUE(klass != NULL); |
| 1227 | |
| 1228 | Method* method = klass->FindDirectMethod("sum", "(DD)D"); |
| 1229 | ASSERT_TRUE(method != NULL); |
| 1230 | |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 1231 | Method::InvokeStub* stub = method->GetInvokeStub(); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1232 | |
| 1233 | double args[2]; |
| 1234 | JValue result; |
| 1235 | |
| 1236 | args[0] = 0.0; |
| 1237 | args[1] = 0.0; |
| 1238 | result.d = -1.0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1239 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1240 | EXPECT_EQ(0.0, result.d); |
| 1241 | |
| 1242 | args[0] = 1.0; |
| 1243 | args[1] = 2.0; |
| 1244 | result.d = 0.0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1245 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1246 | EXPECT_EQ(3.0, result.d); |
| 1247 | |
| 1248 | args[0] = 1.0; |
| 1249 | args[1] = -2.0; |
| 1250 | result.d = 0.0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1251 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1252 | EXPECT_EQ(-1.0, result.d); |
| 1253 | |
| 1254 | args[0] = DBL_MAX; |
| 1255 | args[1] = DBL_MIN; |
| 1256 | result.d = 0.0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1257 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1258 | EXPECT_EQ(1.7976931348623157e308, result.d); |
| 1259 | |
| 1260 | args[0] = DBL_MAX; |
| 1261 | args[1] = DBL_MAX; |
| 1262 | result.d = 0.0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1263 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1264 | EXPECT_EQ(INFINITY, result.d); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1265 | } |
| 1266 | |
| 1267 | TEST_F(JniInternalTest, StaticSumDoubleDoubleDoubleMethod) { |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 1268 | const ClassLoader* class_loader = LoadDex("StaticLeafMethods"); |
| 1269 | CompileDirectMethod(class_loader, "StaticLeafMethods", "sum", "(DDD)D"); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1270 | |
| 1271 | Class* klass = class_linker_->FindClass("LStaticLeafMethods;", class_loader); |
| 1272 | ASSERT_TRUE(klass != NULL); |
| 1273 | |
| 1274 | Method* method = klass->FindDirectMethod("sum", "(DDD)D"); |
| 1275 | ASSERT_TRUE(method != NULL); |
| 1276 | |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 1277 | Method::InvokeStub* stub = method->GetInvokeStub(); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1278 | |
| 1279 | double args[3]; |
| 1280 | JValue result; |
| 1281 | |
| 1282 | args[0] = 0.0; |
| 1283 | args[1] = 0.0; |
| 1284 | args[2] = 0.0; |
| 1285 | result.d = -1.0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1286 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1287 | EXPECT_EQ(0.0, result.d); |
| 1288 | |
| 1289 | args[0] = 1.0; |
| 1290 | args[1] = 2.0; |
| 1291 | args[2] = 3.0; |
| 1292 | result.d = 0.0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1293 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1294 | EXPECT_EQ(6.0, result.d); |
| 1295 | |
| 1296 | args[0] = 1.0; |
| 1297 | args[1] = -2.0; |
| 1298 | args[2] = 3.0; |
| 1299 | result.d = 0.0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1300 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1301 | EXPECT_EQ(2.0, result.d); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1302 | } |
| 1303 | |
| 1304 | TEST_F(JniInternalTest, StaticSumDoubleDoubleDoubleDoubleMethod) { |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 1305 | const ClassLoader* class_loader = LoadDex("StaticLeafMethods"); |
| 1306 | CompileDirectMethod(class_loader, "StaticLeafMethods", "sum", "(DDDD)D"); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1307 | |
| 1308 | Class* klass = class_linker_->FindClass("LStaticLeafMethods;", class_loader); |
| 1309 | ASSERT_TRUE(klass != NULL); |
| 1310 | |
| 1311 | Method* method = klass->FindDirectMethod("sum", "(DDDD)D"); |
| 1312 | ASSERT_TRUE(method != NULL); |
| 1313 | |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 1314 | Method::InvokeStub* stub = method->GetInvokeStub(); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1315 | |
| 1316 | double args[4]; |
| 1317 | JValue result; |
| 1318 | |
| 1319 | args[0] = 0.0; |
| 1320 | args[1] = 0.0; |
| 1321 | args[2] = 0.0; |
| 1322 | args[3] = 0.0; |
| 1323 | result.d = -1.0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1324 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1325 | EXPECT_EQ(0.0, result.d); |
| 1326 | |
| 1327 | args[0] = 1.0; |
| 1328 | args[1] = 2.0; |
| 1329 | args[2] = 3.0; |
| 1330 | args[3] = 4.0; |
| 1331 | result.d = 0.0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1332 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1333 | EXPECT_EQ(10.0, result.d); |
| 1334 | |
| 1335 | args[0] = 1.0; |
| 1336 | args[1] = -2.0; |
| 1337 | args[2] = 3.0; |
| 1338 | args[3] = -4.0; |
| 1339 | result.d = 0.0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1340 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1341 | EXPECT_EQ(-2.0, result.d); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1342 | } |
| 1343 | |
| 1344 | TEST_F(JniInternalTest, StaticSumDoubleDoubleDoubleDoubleDoubleMethod) { |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 1345 | const ClassLoader* class_loader = LoadDex("StaticLeafMethods"); |
| 1346 | CompileDirectMethod(class_loader, "StaticLeafMethods", "sum", "(DDDDD)D"); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1347 | |
| 1348 | Class* klass = class_linker_->FindClass("LStaticLeafMethods;", class_loader); |
| 1349 | ASSERT_TRUE(klass != NULL); |
| 1350 | |
| 1351 | Method* method = klass->FindDirectMethod("sum", "(DDDDD)D"); |
| 1352 | ASSERT_TRUE(method != NULL); |
| 1353 | |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 1354 | Method::InvokeStub* stub = method->GetInvokeStub(); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1355 | |
| 1356 | double args[5]; |
| 1357 | JValue result; |
| 1358 | |
| 1359 | args[0] = 0.0; |
| 1360 | args[1] = 0.0; |
| 1361 | args[2] = 0.0; |
| 1362 | args[3] = 0.0; |
| 1363 | args[4] = 0.0; |
| 1364 | result.d = -1.0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1365 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1366 | EXPECT_EQ(0.0, result.d); |
| 1367 | |
| 1368 | args[0] = 1.0; |
| 1369 | args[1] = 2.0; |
| 1370 | args[2] = 3.0; |
| 1371 | args[3] = 4.0; |
| 1372 | args[4] = 5.0; |
| 1373 | result.d = 0.0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1374 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1375 | EXPECT_EQ(15.0, result.d); |
| 1376 | |
| 1377 | args[0] = 1.0; |
| 1378 | args[1] = -2.0; |
| 1379 | args[2] = 3.0; |
| 1380 | args[3] = -4.0; |
| 1381 | args[4] = 5.0; |
| 1382 | result.d = 0.0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1383 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1384 | EXPECT_EQ(3.0, result.d); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1385 | } |
| 1386 | #endif // __arm__ |
| 1387 | |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1388 | TEST_F(JniInternalTest, Throw) { |
| 1389 | EXPECT_EQ(JNI_ERR, env_->Throw(NULL)); |
| 1390 | |
| 1391 | jclass exception_class = env_->FindClass("java/lang/RuntimeException"); |
| 1392 | ASSERT_TRUE(exception_class != NULL); |
| 1393 | jthrowable exception = reinterpret_cast<jthrowable>(env_->AllocObject(exception_class)); |
| 1394 | ASSERT_TRUE(exception != NULL); |
| 1395 | |
| 1396 | EXPECT_EQ(JNI_OK, env_->Throw(exception)); |
| 1397 | EXPECT_TRUE(env_->ExceptionCheck()); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1398 | jthrowable thrown_exception = env_->ExceptionOccurred(); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1399 | env_->ExceptionClear(); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1400 | EXPECT_TRUE(env_->IsSameObject(exception, thrown_exception)); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1401 | } |
| 1402 | |
| 1403 | TEST_F(JniInternalTest, ThrowNew) { |
| 1404 | EXPECT_EQ(JNI_ERR, env_->Throw(NULL)); |
| 1405 | |
| 1406 | jclass exception_class = env_->FindClass("java/lang/RuntimeException"); |
| 1407 | ASSERT_TRUE(exception_class != NULL); |
| 1408 | |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 1409 | jthrowable thrown_exception; |
| 1410 | |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1411 | EXPECT_EQ(JNI_OK, env_->ThrowNew(exception_class, "hello world")); |
| 1412 | EXPECT_TRUE(env_->ExceptionCheck()); |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 1413 | thrown_exception = env_->ExceptionOccurred(); |
| 1414 | env_->ExceptionClear(); |
| 1415 | EXPECT_TRUE(env_->IsInstanceOf(thrown_exception, exception_class)); |
| 1416 | |
| 1417 | EXPECT_EQ(JNI_OK, env_->ThrowNew(exception_class, NULL)); |
| 1418 | EXPECT_TRUE(env_->ExceptionCheck()); |
| 1419 | thrown_exception = env_->ExceptionOccurred(); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1420 | env_->ExceptionClear(); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1421 | EXPECT_TRUE(env_->IsInstanceOf(thrown_exception, exception_class)); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1422 | } |
| 1423 | |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1424 | // TODO: this test is DISABLED until we can actually run java.nio.Buffer's <init>. |
| 1425 | TEST_F(JniInternalTest, DISABLED_NewDirectBuffer_GetDirectBufferAddress_GetDirectBufferCapacity) { |
| 1426 | jclass buffer_class = env_->FindClass("java/nio/Buffer"); |
| 1427 | ASSERT_TRUE(buffer_class != NULL); |
| 1428 | |
| 1429 | char bytes[1024]; |
| 1430 | jobject buffer = env_->NewDirectByteBuffer(bytes, sizeof(bytes)); |
| 1431 | ASSERT_TRUE(buffer != NULL); |
| 1432 | ASSERT_TRUE(env_->IsInstanceOf(buffer, buffer_class)); |
| 1433 | ASSERT_TRUE(env_->GetDirectBufferAddress(buffer) == bytes); |
| 1434 | ASSERT_TRUE(env_->GetDirectBufferCapacity(buffer) == sizeof(bytes)); |
| 1435 | } |
| 1436 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1437 | } // namespace art |