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 | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 10 | |
| 11 | namespace art { |
| 12 | |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 13 | class JniInternalTest : public CommonTest { |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 14 | protected: |
| 15 | virtual void SetUp() { |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 16 | CommonTest::SetUp(); |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 17 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 18 | vm_ = Runtime::Current()->GetJavaVM(); |
| 19 | |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 20 | // Turn on -verbose:jni for the JNI tests. |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 21 | vm_->verbose_jni = true; |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 22 | |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 23 | env_ = Thread::Current()->GetJniEnv(); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 24 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 25 | aioobe_ = env_->FindClass("java/lang/ArrayIndexOutOfBoundsException"); |
| 26 | CHECK(aioobe_ != NULL); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 27 | |
| 28 | sioobe_ = env_->FindClass("java/lang/StringIndexOutOfBoundsException"); |
| 29 | CHECK(sioobe_ != NULL); |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 30 | } |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 31 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 32 | JavaVMExt* vm_; |
| 33 | JNIEnvExt* env_; |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 34 | jclass aioobe_; |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 35 | jclass sioobe_; |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 36 | }; |
| 37 | |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 38 | TEST_F(JniInternalTest, AllocObject) { |
| 39 | jclass c = env_->FindClass("java/lang/String"); |
| 40 | ASSERT_TRUE(c != NULL); |
| 41 | jobject o = env_->AllocObject(c); |
| 42 | ASSERT_TRUE(o != NULL); |
| 43 | |
| 44 | // We have an instance of the class we asked for... |
| 45 | ASSERT_TRUE(env_->IsInstanceOf(o, c)); |
| 46 | // ...whose fields haven't been initialized because |
| 47 | // we didn't call a constructor. |
| 48 | ASSERT_EQ(0, env_->GetIntField(o, env_->GetFieldID(c, "count", "I"))); |
| 49 | ASSERT_EQ(0, env_->GetIntField(o, env_->GetFieldID(c, "offset", "I"))); |
| 50 | ASSERT_TRUE(env_->GetObjectField(o, env_->GetFieldID(c, "value", "[C")) == NULL); |
| 51 | } |
| 52 | |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 53 | TEST_F(JniInternalTest, GetVersion) { |
| 54 | ASSERT_EQ(JNI_VERSION_1_6, env_->GetVersion()); |
| 55 | } |
| 56 | |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 57 | #define EXPECT_CLASS_FOUND(NAME) \ |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 58 | EXPECT_TRUE(env_->FindClass(NAME) != NULL); \ |
| 59 | EXPECT_FALSE(env_->ExceptionCheck()) |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 60 | |
| 61 | #define EXPECT_CLASS_NOT_FOUND(NAME) \ |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 62 | EXPECT_TRUE(env_->FindClass(NAME) == NULL); \ |
| 63 | EXPECT_TRUE(env_->ExceptionCheck()); \ |
| 64 | env_->ExceptionClear() |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 65 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 66 | std::string gCheckJniAbortMessage; |
| 67 | void TestCheckJniAbortHook(const std::string& reason) { |
| 68 | gCheckJniAbortMessage = reason; |
| 69 | } |
| 70 | |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 71 | TEST_F(JniInternalTest, FindClass) { |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 72 | // Reference types... |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 73 | EXPECT_CLASS_FOUND("java/lang/String"); |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 74 | // ...for arrays too, where you must include "L;". |
| 75 | EXPECT_CLASS_FOUND("[Ljava/lang/String;"); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 76 | |
| 77 | vm_->check_jni_abort_hook = TestCheckJniAbortHook; |
| 78 | // We support . as well as / for compatibility, if -Xcheck:jni is off. |
| 79 | EXPECT_CLASS_FOUND("java.lang.String"); |
| 80 | EXPECT_CLASS_NOT_FOUND("Ljava.lang.String;"); |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 81 | EXPECT_CLASS_FOUND("[Ljava.lang.String;"); |
| 82 | EXPECT_CLASS_NOT_FOUND("[java.lang.String"); |
| 83 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 84 | // You can't include the "L;" in a JNI class descriptor. |
| 85 | EXPECT_CLASS_NOT_FOUND("Ljava/lang/String;"); |
| 86 | // But you must include it for an array of any reference type. |
| 87 | EXPECT_CLASS_NOT_FOUND("[java/lang/String"); |
| 88 | vm_->check_jni_abort_hook = NULL; |
| 89 | |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 90 | // Primitive arrays are okay (if the primitive type is valid)... |
| 91 | EXPECT_CLASS_FOUND("[C"); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 92 | vm_->check_jni_abort_hook = TestCheckJniAbortHook; |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 93 | EXPECT_CLASS_NOT_FOUND("[K"); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 94 | vm_->check_jni_abort_hook = NULL; |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 95 | // But primitive types aren't allowed... |
| 96 | EXPECT_CLASS_NOT_FOUND("C"); |
| 97 | EXPECT_CLASS_NOT_FOUND("K"); |
| 98 | } |
| 99 | |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 100 | #define EXPECT_EXCEPTION(exception_class) \ |
| 101 | do { \ |
| 102 | EXPECT_TRUE(env_->ExceptionCheck()); \ |
| 103 | jthrowable exception = env_->ExceptionOccurred(); \ |
| 104 | EXPECT_NE(static_cast<jthrowable>(NULL), exception); \ |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 105 | env_->ExceptionClear(); \ |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 106 | EXPECT_TRUE(env_->IsInstanceOf(exception, exception_class)); \ |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 107 | } while (false) |
| 108 | |
| 109 | TEST_F(JniInternalTest, GetFieldID) { |
| 110 | jclass jlnsfe = env_->FindClass("java/lang/NoSuchFieldError"); |
| 111 | ASSERT_TRUE(jlnsfe != NULL); |
| 112 | jclass c = env_->FindClass("java/lang/String"); |
| 113 | ASSERT_TRUE(c != NULL); |
| 114 | |
| 115 | // Wrong type. |
| 116 | jfieldID fid = env_->GetFieldID(c, "count", "J"); |
| 117 | EXPECT_EQ(static_cast<jfieldID>(NULL), fid); |
| 118 | EXPECT_EXCEPTION(jlnsfe); |
| 119 | |
Ian Rogers | b17d08b | 2011-09-02 16:16:49 -0700 | [diff] [blame] | 120 | // Wrong type where type doesn't exist. |
| 121 | fid = env_->GetFieldID(c, "count", "Lrod/jane/freddy;"); |
| 122 | EXPECT_EQ(static_cast<jfieldID>(NULL), fid); |
| 123 | EXPECT_EXCEPTION(jlnsfe); |
| 124 | |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 125 | // Wrong name. |
| 126 | fid = env_->GetFieldID(c, "Count", "I"); |
| 127 | EXPECT_EQ(static_cast<jfieldID>(NULL), fid); |
| 128 | EXPECT_EXCEPTION(jlnsfe); |
| 129 | |
| 130 | // Good declared field lookup. |
| 131 | fid = env_->GetFieldID(c, "count", "I"); |
| 132 | EXPECT_NE(static_cast<jfieldID>(NULL), fid); |
| 133 | EXPECT_TRUE(fid != NULL); |
| 134 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 135 | |
| 136 | // Good superclass field lookup. |
| 137 | c = env_->FindClass("java/lang/StringBuilder"); |
| 138 | fid = env_->GetFieldID(c, "count", "I"); |
| 139 | EXPECT_NE(static_cast<jfieldID>(NULL), fid); |
| 140 | EXPECT_TRUE(fid != NULL); |
| 141 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 142 | |
| 143 | // Not instance. |
| 144 | fid = env_->GetFieldID(c, "CASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;"); |
| 145 | EXPECT_EQ(static_cast<jfieldID>(NULL), fid); |
| 146 | EXPECT_EXCEPTION(jlnsfe); |
| 147 | } |
| 148 | |
| 149 | TEST_F(JniInternalTest, GetStaticFieldID) { |
| 150 | jclass jlnsfe = env_->FindClass("java/lang/NoSuchFieldError"); |
| 151 | ASSERT_TRUE(jlnsfe != NULL); |
| 152 | jclass c = env_->FindClass("java/lang/String"); |
| 153 | ASSERT_TRUE(c != NULL); |
| 154 | |
| 155 | // Wrong type. |
| 156 | jfieldID fid = env_->GetStaticFieldID(c, "CASE_INSENSITIVE_ORDER", "J"); |
| 157 | EXPECT_EQ(static_cast<jfieldID>(NULL), fid); |
| 158 | EXPECT_EXCEPTION(jlnsfe); |
| 159 | |
Ian Rogers | b17d08b | 2011-09-02 16:16:49 -0700 | [diff] [blame] | 160 | // Wrong type where type doesn't exist. |
| 161 | fid = env_->GetStaticFieldID(c, "CASE_INSENSITIVE_ORDER", "Lrod/jane/freddy;"); |
| 162 | EXPECT_EQ(static_cast<jfieldID>(NULL), fid); |
| 163 | EXPECT_EXCEPTION(jlnsfe); |
| 164 | |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 165 | // Wrong name. |
| 166 | fid = env_->GetStaticFieldID(c, "cASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;"); |
| 167 | EXPECT_EQ(static_cast<jfieldID>(NULL), fid); |
| 168 | EXPECT_EXCEPTION(jlnsfe); |
| 169 | |
| 170 | // Good declared field lookup. |
| 171 | fid = env_->GetStaticFieldID(c, "CASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;"); |
| 172 | EXPECT_NE(static_cast<jfieldID>(NULL), fid); |
| 173 | EXPECT_TRUE(fid != NULL); |
| 174 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 175 | |
| 176 | // Not static. |
| 177 | fid = env_->GetStaticFieldID(c, "count", "I"); |
| 178 | EXPECT_EQ(static_cast<jfieldID>(NULL), fid); |
| 179 | EXPECT_EXCEPTION(jlnsfe); |
| 180 | } |
| 181 | |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 182 | TEST_F(JniInternalTest, GetMethodID) { |
| 183 | jclass jlobject = env_->FindClass("java/lang/Object"); |
| 184 | jclass jlstring = env_->FindClass("java/lang/String"); |
| 185 | jclass jlnsme = env_->FindClass("java/lang/NoSuchMethodError"); |
| 186 | |
| 187 | // Sanity check that no exceptions are pending |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 188 | ASSERT_FALSE(env_->ExceptionCheck()); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 189 | |
| 190 | // Check that java.lang.Object.foo() doesn't exist and NoSuchMethodError is |
| 191 | // a pending exception |
| 192 | jmethodID method = env_->GetMethodID(jlobject, "foo", "()V"); |
| 193 | EXPECT_EQ(static_cast<jmethodID>(NULL), method); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 194 | EXPECT_EXCEPTION(jlnsme); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 195 | |
| 196 | // Check that java.lang.Object.equals() does exist |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 197 | method = env_->GetMethodID(jlobject, "equals", "(Ljava/lang/Object;)Z"); |
| 198 | EXPECT_NE(static_cast<jmethodID>(NULL), method); |
| 199 | EXPECT_FALSE(env_->ExceptionCheck()); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 200 | |
| 201 | // Check that GetMethodID for java.lang.String.valueOf(int) fails as the |
| 202 | // method is static |
| 203 | method = env_->GetMethodID(jlstring, "valueOf", "(I)Ljava/lang/String;"); |
| 204 | EXPECT_EQ(static_cast<jmethodID>(NULL), method); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 205 | EXPECT_EXCEPTION(jlnsme); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | TEST_F(JniInternalTest, GetStaticMethodID) { |
| 209 | jclass jlobject = env_->FindClass("java/lang/Object"); |
| 210 | jclass jlnsme = env_->FindClass("java/lang/NoSuchMethodError"); |
| 211 | |
| 212 | // Sanity check that no exceptions are pending |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 213 | ASSERT_FALSE(env_->ExceptionCheck()); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 214 | |
| 215 | // Check that java.lang.Object.foo() doesn't exist and NoSuchMethodError is |
| 216 | // a pending exception |
| 217 | jmethodID method = env_->GetStaticMethodID(jlobject, "foo", "()V"); |
| 218 | EXPECT_EQ(static_cast<jmethodID>(NULL), method); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 219 | EXPECT_EXCEPTION(jlnsme); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 220 | |
| 221 | // Check that GetStaticMethodID for java.lang.Object.equals(Object) fails as |
| 222 | // the method is not static |
| 223 | method = env_->GetStaticMethodID(jlobject, "equals", "(Ljava/lang/Object;)Z"); |
| 224 | EXPECT_EQ(static_cast<jmethodID>(NULL), method); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 225 | EXPECT_EXCEPTION(jlnsme); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 226 | |
| 227 | // Check that java.lang.String.valueOf(int) does exist |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 228 | jclass jlstring = env_->FindClass("java/lang/String"); |
| 229 | method = env_->GetStaticMethodID(jlstring, "valueOf", |
| 230 | "(I)Ljava/lang/String;"); |
| 231 | EXPECT_NE(static_cast<jmethodID>(NULL), method); |
| 232 | EXPECT_FALSE(env_->ExceptionCheck()); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 235 | TEST_F(JniInternalTest, FromReflectedField_ToReflectedField) { |
| 236 | jclass jlrField = env_->FindClass("java/lang/reflect/Field"); |
| 237 | jclass c = env_->FindClass("java/lang/String"); |
| 238 | ASSERT_TRUE(c != NULL); |
| 239 | jfieldID fid = env_->GetFieldID(c, "count", "I"); |
| 240 | ASSERT_TRUE(fid != NULL); |
| 241 | // Turn the fid into a java.lang.reflect.Field... |
| 242 | jobject field = env_->ToReflectedField(c, fid, JNI_FALSE); |
| 243 | ASSERT_TRUE(c != NULL); |
| 244 | ASSERT_TRUE(env_->IsInstanceOf(field, jlrField)); |
| 245 | // ...and back again. |
| 246 | jfieldID fid2 = env_->FromReflectedField(field); |
| 247 | ASSERT_TRUE(fid2 != NULL); |
| 248 | } |
| 249 | |
| 250 | TEST_F(JniInternalTest, FromReflectedMethod_ToReflectedMethod) { |
| 251 | jclass jlrMethod = env_->FindClass("java/lang/reflect/Method"); |
| 252 | jclass c = env_->FindClass("java/lang/String"); |
| 253 | ASSERT_TRUE(c != NULL); |
| 254 | jmethodID mid = env_->GetMethodID(c, "length", "()I"); |
| 255 | ASSERT_TRUE(mid != NULL); |
| 256 | // Turn the mid into a java.lang.reflect.Method... |
| 257 | jobject method = env_->ToReflectedMethod(c, mid, JNI_FALSE); |
| 258 | ASSERT_TRUE(c != NULL); |
| 259 | ASSERT_TRUE(env_->IsInstanceOf(method, jlrMethod)); |
| 260 | // ...and back again. |
| 261 | jmethodID mid2 = env_->FromReflectedMethod(method); |
| 262 | ASSERT_TRUE(mid2 != NULL); |
| 263 | } |
| 264 | |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 265 | void BogusMethod() { |
| 266 | // You can't pass NULL function pointers to RegisterNatives. |
| 267 | } |
| 268 | |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 269 | TEST_F(JniInternalTest, RegisterNatives) { |
| 270 | jclass jlobject = env_->FindClass("java/lang/Object"); |
| 271 | jclass jlnsme = env_->FindClass("java/lang/NoSuchMethodError"); |
| 272 | |
| 273 | // Sanity check that no exceptions are pending |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 274 | ASSERT_FALSE(env_->ExceptionCheck()); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 275 | |
| 276 | // Check that registering to a non-existent java.lang.Object.foo() causes a |
| 277 | // NoSuchMethodError |
| 278 | { |
| 279 | JNINativeMethod methods[] = {{"foo", "()V", NULL}}; |
| 280 | env_->RegisterNatives(jlobject, methods, 1); |
| 281 | } |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 282 | EXPECT_EXCEPTION(jlnsme); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 283 | |
| 284 | // Check that registering non-native methods causes a NoSuchMethodError |
| 285 | { |
| 286 | JNINativeMethod methods[] = {{"equals", "(Ljava/lang/Object;)Z", NULL}}; |
| 287 | env_->RegisterNatives(jlobject, methods, 1); |
| 288 | } |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 289 | EXPECT_EXCEPTION(jlnsme); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 290 | |
| 291 | // Check that registering native methods is successful |
| 292 | { |
Elliott Hughes | ad7c2a3 | 2011-08-31 11:58:10 -0700 | [diff] [blame] | 293 | JNINativeMethod methods[] = {{"getClass", "()Ljava/lang/Class;", reinterpret_cast<void*>(BogusMethod)}}; |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 294 | env_->RegisterNatives(jlobject, methods, 1); |
| 295 | } |
| 296 | EXPECT_FALSE(env_->ExceptionCheck()); |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 297 | |
| 298 | env_->UnregisterNatives(jlobject); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 299 | } |
| 300 | |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 301 | #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] | 302 | jsize size = 4; \ |
| 303 | /* Allocate an array and check it has the right type and length. */ \ |
| 304 | scalar_type ## Array a = env_->new_fn(size); \ |
| 305 | EXPECT_TRUE(a != NULL); \ |
| 306 | EXPECT_TRUE(env_->IsInstanceOf(a, env_->FindClass(expected_class_descriptor))); \ |
| 307 | EXPECT_EQ(size, env_->GetArrayLength(a)); \ |
| 308 | /* AIOOBE for negative start offset. */ \ |
| 309 | env_->get_region_fn(a, -1, 1, NULL); \ |
| 310 | EXPECT_EXCEPTION(aioobe_); \ |
| 311 | env_->set_region_fn(a, -1, 1, NULL); \ |
| 312 | EXPECT_EXCEPTION(aioobe_); \ |
| 313 | /* AIOOBE for negative length. */ \ |
| 314 | env_->get_region_fn(a, 0, -1, NULL); \ |
| 315 | EXPECT_EXCEPTION(aioobe_); \ |
| 316 | env_->set_region_fn(a, 0, -1, NULL); \ |
| 317 | EXPECT_EXCEPTION(aioobe_); \ |
| 318 | /* AIOOBE for buffer overrun. */ \ |
| 319 | env_->get_region_fn(a, size - 1, size, NULL); \ |
| 320 | EXPECT_EXCEPTION(aioobe_); \ |
| 321 | env_->set_region_fn(a, size - 1, size, NULL); \ |
| 322 | EXPECT_EXCEPTION(aioobe_); \ |
| 323 | /* Prepare a couple of buffers. */ \ |
| 324 | scalar_type src_buf[size]; \ |
| 325 | scalar_type dst_buf[size]; \ |
| 326 | for (jsize i = 0; i < size; ++i) { src_buf[i] = scalar_type(i); } \ |
| 327 | for (jsize i = 0; i < size; ++i) { dst_buf[i] = scalar_type(-1); } \ |
| 328 | /* Copy all of src_buf onto the heap. */ \ |
| 329 | env_->set_region_fn(a, 0, size, src_buf); \ |
| 330 | /* Copy back only part. */ \ |
| 331 | env_->get_region_fn(a, 1, size - 2, &dst_buf[1]); \ |
| 332 | EXPECT_FALSE(memcmp(src_buf, dst_buf, sizeof(src_buf)) == 0) << "short copy equal"; \ |
| 333 | /* Copy the missing pieces. */ \ |
| 334 | env_->get_region_fn(a, 0, 1, dst_buf); \ |
| 335 | env_->get_region_fn(a, size - 1, 1, &dst_buf[size - 1]); \ |
| 336 | EXPECT_TRUE(memcmp(src_buf, dst_buf, sizeof(src_buf)) == 0) << "fixed copy not equal"; \ |
| 337 | /* Copy back the whole array. */ \ |
| 338 | env_->get_region_fn(a, 0, size, dst_buf); \ |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 339 | EXPECT_TRUE(memcmp(src_buf, dst_buf, sizeof(src_buf)) == 0) << "full copy not equal"; \ |
| 340 | /* GetPrimitiveArrayCritical */ \ |
| 341 | void* v = env_->GetPrimitiveArrayCritical(a, NULL); \ |
| 342 | EXPECT_TRUE(memcmp(src_buf, v, sizeof(src_buf)) == 0) << "GetPrimitiveArrayCritical not equal"; \ |
| 343 | env_->ReleasePrimitiveArrayCritical(a, v, 0); \ |
| 344 | /* GetXArrayElements */ \ |
| 345 | scalar_type* xs = env_->get_elements_fn(a, NULL); \ |
| 346 | EXPECT_TRUE(memcmp(src_buf, xs, sizeof(src_buf)) == 0) << # get_elements_fn " not equal"; \ |
| 347 | env_->release_elements_fn(a, xs, 0); \ |
| 348 | EXPECT_EQ(reinterpret_cast<uintptr_t>(v), reinterpret_cast<uintptr_t>(xs)) |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 349 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 350 | TEST_F(JniInternalTest, BooleanArrays) { |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 351 | EXPECT_PRIMITIVE_ARRAY(NewBooleanArray, GetBooleanArrayRegion, SetBooleanArrayRegion, GetBooleanArrayElements, ReleaseBooleanArrayElements, jboolean, "[Z"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 352 | } |
| 353 | TEST_F(JniInternalTest, ByteArrays) { |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 354 | EXPECT_PRIMITIVE_ARRAY(NewByteArray, GetByteArrayRegion, SetByteArrayRegion, GetByteArrayElements, ReleaseByteArrayElements, jbyte, "[B"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 355 | } |
| 356 | TEST_F(JniInternalTest, CharArrays) { |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 357 | EXPECT_PRIMITIVE_ARRAY(NewCharArray, GetCharArrayRegion, SetCharArrayRegion, GetCharArrayElements, ReleaseCharArrayElements, jchar, "[C"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 358 | } |
| 359 | TEST_F(JniInternalTest, DoubleArrays) { |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 360 | EXPECT_PRIMITIVE_ARRAY(NewDoubleArray, GetDoubleArrayRegion, SetDoubleArrayRegion, GetDoubleArrayElements, ReleaseDoubleArrayElements, jdouble, "[D"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 361 | } |
| 362 | TEST_F(JniInternalTest, FloatArrays) { |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 363 | EXPECT_PRIMITIVE_ARRAY(NewFloatArray, GetFloatArrayRegion, SetFloatArrayRegion, GetFloatArrayElements, ReleaseFloatArrayElements, jfloat, "[F"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 364 | } |
| 365 | TEST_F(JniInternalTest, IntArrays) { |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 366 | EXPECT_PRIMITIVE_ARRAY(NewIntArray, GetIntArrayRegion, SetIntArrayRegion, GetIntArrayElements, ReleaseIntArrayElements, jint, "[I"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 367 | } |
| 368 | TEST_F(JniInternalTest, LongArrays) { |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 369 | EXPECT_PRIMITIVE_ARRAY(NewLongArray, GetLongArrayRegion, SetLongArrayRegion, GetLongArrayElements, ReleaseLongArrayElements, jlong, "[J"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 370 | } |
| 371 | TEST_F(JniInternalTest, ShortArrays) { |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 372 | EXPECT_PRIMITIVE_ARRAY(NewShortArray, GetShortArrayRegion, SetShortArrayRegion, GetShortArrayElements, ReleaseShortArrayElements, jshort, "[S"); |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 373 | } |
| 374 | |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 375 | TEST_F(JniInternalTest, NewObjectArray) { |
| 376 | // TODO: death tests for negative array sizes. |
| 377 | |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 378 | // TODO: check non-NULL initial elements. |
| 379 | |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 380 | jclass element_class = env_->FindClass("java/lang/String"); |
| 381 | ASSERT_TRUE(element_class != NULL); |
| 382 | jclass array_class = env_->FindClass("[Ljava/lang/String;"); |
| 383 | ASSERT_TRUE(array_class != NULL); |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 384 | |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 385 | jobjectArray a; |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 386 | |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 387 | a = env_->NewObjectArray(0, element_class, NULL); |
| 388 | EXPECT_TRUE(a != NULL); |
| 389 | EXPECT_TRUE(env_->IsInstanceOf(a, array_class)); |
| 390 | EXPECT_EQ(0, env_->GetArrayLength(a)); |
| 391 | |
| 392 | a = env_->NewObjectArray(1, element_class, NULL); |
| 393 | EXPECT_TRUE(a != NULL); |
| 394 | EXPECT_TRUE(env_->IsInstanceOf(a, array_class)); |
| 395 | EXPECT_EQ(1, env_->GetArrayLength(a)); |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 396 | EXPECT_TRUE(env_->IsSameObject(env_->GetObjectArrayElement(a, 0), NULL)); |
| 397 | |
| 398 | jstring s = env_->NewStringUTF("poop"); |
| 399 | a = env_->NewObjectArray(2, element_class, s); |
| 400 | EXPECT_TRUE(a != NULL); |
| 401 | EXPECT_TRUE(env_->IsInstanceOf(a, array_class)); |
| 402 | EXPECT_EQ(2, env_->GetArrayLength(a)); |
| 403 | EXPECT_TRUE(env_->IsSameObject(env_->GetObjectArrayElement(a, 0), s)); |
| 404 | EXPECT_TRUE(env_->IsSameObject(env_->GetObjectArrayElement(a, 1), s)); |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | TEST_F(JniInternalTest, GetArrayLength) { |
| 408 | // Already tested in NewObjectArray/NewPrimitiveArray. |
Elliott Hughes | 8a26c5c | 2011-08-15 18:35:43 -0700 | [diff] [blame] | 409 | } |
| 410 | |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 411 | TEST_F(JniInternalTest, GetObjectClass) { |
| 412 | jclass string_class = env_->FindClass("java/lang/String"); |
| 413 | ASSERT_TRUE(string_class != NULL); |
| 414 | jclass class_class = env_->FindClass("java/lang/Class"); |
| 415 | ASSERT_TRUE(class_class != NULL); |
| 416 | |
| 417 | jstring s = env_->NewStringUTF("poop"); |
| 418 | jclass c = env_->GetObjectClass(s); |
| 419 | ASSERT_TRUE(env_->IsSameObject(string_class, c)); |
| 420 | |
| 421 | jclass c2 = env_->GetObjectClass(c); |
| 422 | ASSERT_TRUE(env_->IsSameObject(class_class, env_->GetObjectClass(c2))); |
| 423 | } |
| 424 | |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 425 | TEST_F(JniInternalTest, GetSuperclass) { |
| 426 | jclass object_class = env_->FindClass("java/lang/Object"); |
| 427 | ASSERT_TRUE(object_class != NULL); |
| 428 | jclass string_class = env_->FindClass("java/lang/String"); |
| 429 | ASSERT_TRUE(string_class != NULL); |
| 430 | ASSERT_TRUE(env_->IsSameObject(object_class, env_->GetSuperclass(string_class))); |
| 431 | ASSERT_TRUE(env_->GetSuperclass(object_class) == NULL); |
| 432 | } |
| 433 | |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 434 | TEST_F(JniInternalTest, IsAssignableFrom) { |
| 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 | |
| 440 | ASSERT_TRUE(env_->IsAssignableFrom(object_class, string_class)); |
| 441 | ASSERT_FALSE(env_->IsAssignableFrom(string_class, object_class)); |
| 442 | } |
| 443 | |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 444 | TEST_F(JniInternalTest, GetObjectRefType) { |
| 445 | jclass local = env_->FindClass("java/lang/Object"); |
| 446 | ASSERT_TRUE(local != NULL); |
| 447 | EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(local)); |
| 448 | |
| 449 | jobject global = env_->NewGlobalRef(local); |
| 450 | EXPECT_EQ(JNIGlobalRefType, env_->GetObjectRefType(global)); |
| 451 | |
| 452 | jweak weak_global = env_->NewWeakGlobalRef(local); |
| 453 | EXPECT_EQ(JNIWeakGlobalRefType, env_->GetObjectRefType(weak_global)); |
| 454 | |
| 455 | jobject invalid = reinterpret_cast<jobject>(this); |
| 456 | EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(invalid)); |
| 457 | |
| 458 | // TODO: invoke a native method and test that its arguments are considered local references. |
| 459 | } |
| 460 | |
Elliott Hughes | 8a26c5c | 2011-08-15 18:35:43 -0700 | [diff] [blame] | 461 | TEST_F(JniInternalTest, NewStringUTF) { |
| 462 | EXPECT_TRUE(env_->NewStringUTF(NULL) == NULL); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 463 | jstring s; |
| 464 | |
| 465 | s = env_->NewStringUTF(""); |
| 466 | EXPECT_TRUE(s != NULL); |
| 467 | EXPECT_EQ(0, env_->GetStringLength(s)); |
| 468 | EXPECT_EQ(0, env_->GetStringUTFLength(s)); |
| 469 | s = env_->NewStringUTF("hello"); |
| 470 | EXPECT_TRUE(s != NULL); |
| 471 | EXPECT_EQ(5, env_->GetStringLength(s)); |
| 472 | EXPECT_EQ(5, env_->GetStringUTFLength(s)); |
| 473 | |
Elliott Hughes | 8a26c5c | 2011-08-15 18:35:43 -0700 | [diff] [blame] | 474 | // TODO: check some non-ASCII strings. |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 475 | } |
| 476 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 477 | TEST_F(JniInternalTest, NewString) { |
| 478 | EXPECT_TRUE(env_->NewString(NULL, 0) == NULL); |
| 479 | |
| 480 | jchar chars[] = { 'h', 'i' }; |
| 481 | jstring s; |
| 482 | s = env_->NewString(chars, 0); |
| 483 | EXPECT_TRUE(s != NULL); |
| 484 | EXPECT_EQ(0, env_->GetStringLength(s)); |
| 485 | EXPECT_EQ(0, env_->GetStringUTFLength(s)); |
| 486 | s = env_->NewString(chars, 2); |
| 487 | EXPECT_TRUE(s != NULL); |
| 488 | EXPECT_EQ(2, env_->GetStringLength(s)); |
| 489 | EXPECT_EQ(2, env_->GetStringUTFLength(s)); |
| 490 | |
| 491 | // TODO: check some non-ASCII strings. |
| 492 | } |
| 493 | |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 494 | TEST_F(JniInternalTest, GetStringLength_GetStringUTFLength) { |
| 495 | // Already tested in the NewString/NewStringUTF tests. |
| 496 | } |
| 497 | |
| 498 | TEST_F(JniInternalTest, GetStringRegion_GetStringUTFRegion) { |
| 499 | jstring s = env_->NewStringUTF("hello"); |
| 500 | ASSERT_TRUE(s != NULL); |
| 501 | |
| 502 | env_->GetStringRegion(s, -1, 0, NULL); |
| 503 | EXPECT_EXCEPTION(sioobe_); |
| 504 | env_->GetStringRegion(s, 0, -1, NULL); |
| 505 | EXPECT_EXCEPTION(sioobe_); |
| 506 | env_->GetStringRegion(s, 0, 10, NULL); |
| 507 | EXPECT_EXCEPTION(sioobe_); |
| 508 | env_->GetStringRegion(s, 10, 1, NULL); |
| 509 | EXPECT_EXCEPTION(sioobe_); |
| 510 | |
| 511 | jchar chars[4] = { 'x', 'x', 'x', 'x' }; |
| 512 | env_->GetStringRegion(s, 1, 2, &chars[1]); |
| 513 | EXPECT_EQ('x', chars[0]); |
| 514 | EXPECT_EQ('e', chars[1]); |
| 515 | EXPECT_EQ('l', chars[2]); |
| 516 | EXPECT_EQ('x', chars[3]); |
| 517 | |
| 518 | env_->GetStringUTFRegion(s, -1, 0, NULL); |
| 519 | EXPECT_EXCEPTION(sioobe_); |
| 520 | env_->GetStringUTFRegion(s, 0, -1, NULL); |
| 521 | EXPECT_EXCEPTION(sioobe_); |
| 522 | env_->GetStringUTFRegion(s, 0, 10, NULL); |
| 523 | EXPECT_EXCEPTION(sioobe_); |
| 524 | env_->GetStringUTFRegion(s, 10, 1, NULL); |
| 525 | EXPECT_EXCEPTION(sioobe_); |
| 526 | |
| 527 | char bytes[4] = { 'x', 'x', 'x', 'x' }; |
| 528 | env_->GetStringUTFRegion(s, 1, 2, &bytes[1]); |
| 529 | EXPECT_EQ('x', bytes[0]); |
| 530 | EXPECT_EQ('e', bytes[1]); |
| 531 | EXPECT_EQ('l', bytes[2]); |
| 532 | EXPECT_EQ('x', bytes[3]); |
| 533 | } |
| 534 | |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 535 | TEST_F(JniInternalTest, GetStringUTFChars_ReleaseStringUTFChars) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 536 | vm_->check_jni_abort_hook = TestCheckJniAbortHook; |
| 537 | // 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] | 538 | EXPECT_TRUE(env_->GetStringUTFChars(NULL, NULL) == NULL); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 539 | vm_->check_jni_abort_hook = NULL; |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 540 | |
| 541 | jstring s = env_->NewStringUTF("hello"); |
| 542 | ASSERT_TRUE(s != NULL); |
| 543 | |
| 544 | const char* utf = env_->GetStringUTFChars(s, NULL); |
| 545 | EXPECT_STREQ("hello", utf); |
| 546 | env_->ReleaseStringUTFChars(s, utf); |
| 547 | |
| 548 | jboolean is_copy = JNI_FALSE; |
| 549 | utf = env_->GetStringUTFChars(s, &is_copy); |
| 550 | EXPECT_EQ(JNI_TRUE, is_copy); |
| 551 | EXPECT_STREQ("hello", utf); |
| 552 | env_->ReleaseStringUTFChars(s, utf); |
| 553 | } |
| 554 | |
| 555 | TEST_F(JniInternalTest, GetStringChars_ReleaseStringChars) { |
| 556 | jstring s = env_->NewStringUTF("hello"); |
| 557 | ASSERT_TRUE(s != NULL); |
| 558 | |
| 559 | jchar expected[] = { 'h', 'e', 'l', 'l', 'o' }; |
| 560 | const jchar* chars = env_->GetStringChars(s, NULL); |
| 561 | EXPECT_EQ(expected[0], chars[0]); |
| 562 | EXPECT_EQ(expected[1], chars[1]); |
| 563 | EXPECT_EQ(expected[2], chars[2]); |
| 564 | EXPECT_EQ(expected[3], chars[3]); |
| 565 | EXPECT_EQ(expected[4], chars[4]); |
| 566 | env_->ReleaseStringChars(s, chars); |
| 567 | |
| 568 | jboolean is_copy = JNI_FALSE; |
| 569 | chars = env_->GetStringChars(s, &is_copy); |
| 570 | EXPECT_EQ(JNI_FALSE, is_copy); |
| 571 | EXPECT_EQ(expected[0], chars[0]); |
| 572 | EXPECT_EQ(expected[1], chars[1]); |
| 573 | EXPECT_EQ(expected[2], chars[2]); |
| 574 | EXPECT_EQ(expected[3], chars[3]); |
| 575 | EXPECT_EQ(expected[4], chars[4]); |
| 576 | env_->ReleaseStringChars(s, chars); |
| 577 | } |
| 578 | |
| 579 | TEST_F(JniInternalTest, GetStringCritical_ReleaseStringCritical) { |
| 580 | jstring s = env_->NewStringUTF("hello"); |
| 581 | ASSERT_TRUE(s != NULL); |
| 582 | |
| 583 | jchar expected[] = { 'h', 'e', 'l', 'l', 'o' }; |
| 584 | const jchar* chars = env_->GetStringCritical(s, NULL); |
| 585 | EXPECT_EQ(expected[0], chars[0]); |
| 586 | EXPECT_EQ(expected[1], chars[1]); |
| 587 | EXPECT_EQ(expected[2], chars[2]); |
| 588 | EXPECT_EQ(expected[3], chars[3]); |
| 589 | EXPECT_EQ(expected[4], chars[4]); |
| 590 | env_->ReleaseStringCritical(s, chars); |
| 591 | |
| 592 | jboolean is_copy = JNI_FALSE; |
| 593 | chars = env_->GetStringCritical(s, &is_copy); |
| 594 | EXPECT_EQ(JNI_FALSE, is_copy); |
| 595 | EXPECT_EQ(expected[0], chars[0]); |
| 596 | EXPECT_EQ(expected[1], chars[1]); |
| 597 | EXPECT_EQ(expected[2], chars[2]); |
| 598 | EXPECT_EQ(expected[3], chars[3]); |
| 599 | EXPECT_EQ(expected[4], chars[4]); |
| 600 | env_->ReleaseStringCritical(s, chars); |
| 601 | } |
| 602 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 603 | TEST_F(JniInternalTest, GetObjectArrayElement_SetObjectArrayElement) { |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 604 | jclass c = env_->FindClass("java/lang/Object"); |
Elliott Hughes | 289da82 | 2011-08-16 10:11:20 -0700 | [diff] [blame] | 605 | ASSERT_TRUE(c != NULL); |
| 606 | |
| 607 | jobjectArray array = env_->NewObjectArray(1, c, NULL); |
| 608 | EXPECT_TRUE(array != NULL); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 609 | EXPECT_TRUE(env_->GetObjectArrayElement(array, 0) == NULL); |
Elliott Hughes | 289da82 | 2011-08-16 10:11:20 -0700 | [diff] [blame] | 610 | env_->SetObjectArrayElement(array, 0, c); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 611 | EXPECT_TRUE(env_->IsSameObject(env_->GetObjectArrayElement(array, 0), c)); |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 612 | |
| 613 | // ArrayIndexOutOfBounds for negative index. |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 614 | env_->SetObjectArrayElement(array, -1, c); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 615 | EXPECT_EXCEPTION(aioobe_); |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 616 | |
| 617 | // ArrayIndexOutOfBounds for too-large index. |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 618 | env_->SetObjectArrayElement(array, 1, c); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 619 | EXPECT_EXCEPTION(aioobe_); |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 620 | |
Elliott Hughes | 289da82 | 2011-08-16 10:11:20 -0700 | [diff] [blame] | 621 | // TODO: check ArrayStoreException thrown for bad types. |
| 622 | } |
| 623 | |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 624 | #define EXPECT_STATIC_PRIMITIVE_FIELD(type, field_name, sig, value1, value2) \ |
| 625 | do { \ |
| 626 | jfieldID fid = env_->GetStaticFieldID(c, field_name, sig); \ |
| 627 | EXPECT_TRUE(fid != NULL); \ |
| 628 | env_->SetStatic ## type ## Field(c, fid, value1); \ |
| 629 | EXPECT_EQ(value1, env_->GetStatic ## type ## Field(c, fid)); \ |
| 630 | env_->SetStatic ## type ## Field(c, fid, value2); \ |
| 631 | EXPECT_EQ(value2, env_->GetStatic ## type ## Field(c, fid)); \ |
| 632 | } while (false) |
| 633 | |
| 634 | #define EXPECT_PRIMITIVE_FIELD(instance, type, field_name, sig, value1, value2) \ |
| 635 | do { \ |
| 636 | jfieldID fid = env_->GetFieldID(c, field_name, sig); \ |
| 637 | EXPECT_TRUE(fid != NULL); \ |
| 638 | env_->Set ## type ## Field(instance, fid, value1); \ |
| 639 | EXPECT_EQ(value1, env_->Get ## type ## Field(instance, fid)); \ |
| 640 | env_->Set ## type ## Field(instance, fid, value2); \ |
| 641 | EXPECT_EQ(value2, env_->Get ## type ## Field(instance, fid)); \ |
| 642 | } while (false) |
| 643 | |
| 644 | |
| 645 | TEST_F(JniInternalTest, GetPrimitiveField_SetPrimitiveField) { |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 646 | LoadDex("AllFields"); |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 647 | runtime_->Start(); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 648 | |
| 649 | jclass c = env_->FindClass("AllFields"); |
| 650 | ASSERT_TRUE(c != NULL); |
| 651 | jobject o = env_->AllocObject(c); |
| 652 | ASSERT_TRUE(o != NULL); |
| 653 | |
| 654 | EXPECT_STATIC_PRIMITIVE_FIELD(Boolean, "sZ", "Z", true, false); |
| 655 | EXPECT_STATIC_PRIMITIVE_FIELD(Byte, "sB", "B", 1, 2); |
| 656 | EXPECT_STATIC_PRIMITIVE_FIELD(Char, "sC", "C", 'a', 'b'); |
| 657 | EXPECT_STATIC_PRIMITIVE_FIELD(Double, "sD", "D", 1.0, 2.0); |
| 658 | EXPECT_STATIC_PRIMITIVE_FIELD(Float, "sF", "F", 1.0, 2.0); |
| 659 | EXPECT_STATIC_PRIMITIVE_FIELD(Int, "sI", "I", 1, 2); |
| 660 | EXPECT_STATIC_PRIMITIVE_FIELD(Long, "sJ", "J", 1, 2); |
| 661 | EXPECT_STATIC_PRIMITIVE_FIELD(Short, "sS", "S", 1, 2); |
| 662 | |
| 663 | EXPECT_PRIMITIVE_FIELD(o, Boolean, "iZ", "Z", true, false); |
| 664 | EXPECT_PRIMITIVE_FIELD(o, Byte, "iB", "B", 1, 2); |
| 665 | EXPECT_PRIMITIVE_FIELD(o, Char, "iC", "C", 'a', 'b'); |
| 666 | EXPECT_PRIMITIVE_FIELD(o, Double, "iD", "D", 1.0, 2.0); |
| 667 | EXPECT_PRIMITIVE_FIELD(o, Float, "iF", "F", 1.0, 2.0); |
| 668 | EXPECT_PRIMITIVE_FIELD(o, Int, "iI", "I", 1, 2); |
| 669 | EXPECT_PRIMITIVE_FIELD(o, Long, "iJ", "J", 1, 2); |
| 670 | EXPECT_PRIMITIVE_FIELD(o, Short, "iS", "S", 1, 2); |
| 671 | } |
| 672 | |
| 673 | TEST_F(JniInternalTest, GetObjectField_SetObjectField) { |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 674 | LoadDex("AllFields"); |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 675 | runtime_->Start(); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 676 | |
| 677 | jclass c = env_->FindClass("AllFields"); |
| 678 | ASSERT_TRUE(c != NULL); |
| 679 | jobject o = env_->AllocObject(c); |
| 680 | ASSERT_TRUE(o != NULL); |
| 681 | |
| 682 | jstring s1 = env_->NewStringUTF("hello"); |
| 683 | ASSERT_TRUE(s1 != NULL); |
| 684 | jstring s2 = env_->NewStringUTF("world"); |
| 685 | ASSERT_TRUE(s2 != NULL); |
| 686 | |
| 687 | jfieldID s_fid = env_->GetStaticFieldID(c, "sObject", "Ljava/lang/Object;"); |
| 688 | ASSERT_TRUE(s_fid != NULL); |
| 689 | jfieldID i_fid = env_->GetFieldID(c, "iObject", "Ljava/lang/Object;"); |
| 690 | ASSERT_TRUE(i_fid != NULL); |
| 691 | |
| 692 | env_->SetStaticObjectField(c, s_fid, s1); |
| 693 | ASSERT_TRUE(env_->IsSameObject(s1, env_->GetStaticObjectField(c, s_fid))); |
| 694 | env_->SetStaticObjectField(c, s_fid, s2); |
| 695 | ASSERT_TRUE(env_->IsSameObject(s2, env_->GetStaticObjectField(c, s_fid))); |
| 696 | |
| 697 | env_->SetObjectField(o, i_fid, s1); |
| 698 | ASSERT_TRUE(env_->IsSameObject(s1, env_->GetObjectField(o, i_fid))); |
| 699 | env_->SetObjectField(o, i_fid, s2); |
| 700 | ASSERT_TRUE(env_->IsSameObject(s2, env_->GetObjectField(o, i_fid))); |
| 701 | } |
| 702 | |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 703 | TEST_F(JniInternalTest, NewLocalRef_NULL) { |
| 704 | EXPECT_TRUE(env_->NewLocalRef(NULL) == NULL); |
| 705 | } |
| 706 | |
| 707 | TEST_F(JniInternalTest, NewLocalRef) { |
| 708 | jstring s = env_->NewStringUTF(""); |
| 709 | ASSERT_TRUE(s != NULL); |
| 710 | jobject o = env_->NewLocalRef(s); |
| 711 | EXPECT_TRUE(o != NULL); |
| 712 | EXPECT_TRUE(o != s); |
| 713 | |
| 714 | // TODO: check that o is a local reference. |
| 715 | } |
| 716 | |
| 717 | TEST_F(JniInternalTest, DeleteLocalRef_NULL) { |
| 718 | env_->DeleteLocalRef(NULL); |
| 719 | } |
| 720 | |
| 721 | TEST_F(JniInternalTest, DeleteLocalRef) { |
| 722 | jstring s = env_->NewStringUTF(""); |
| 723 | ASSERT_TRUE(s != NULL); |
| 724 | env_->DeleteLocalRef(s); |
| 725 | |
| 726 | // Currently, deleting an already-deleted reference is just a warning. |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 727 | vm_->check_jni_abort_hook = TestCheckJniAbortHook; |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 728 | env_->DeleteLocalRef(s); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 729 | vm_->check_jni_abort_hook = NULL; |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 730 | |
| 731 | s = env_->NewStringUTF(""); |
| 732 | ASSERT_TRUE(s != NULL); |
| 733 | jobject o = env_->NewLocalRef(s); |
| 734 | ASSERT_TRUE(o != NULL); |
| 735 | |
| 736 | env_->DeleteLocalRef(s); |
| 737 | env_->DeleteLocalRef(o); |
| 738 | } |
| 739 | |
| 740 | TEST_F(JniInternalTest, NewGlobalRef_NULL) { |
| 741 | EXPECT_TRUE(env_->NewGlobalRef(NULL) == NULL); |
| 742 | } |
| 743 | |
| 744 | TEST_F(JniInternalTest, NewGlobalRef) { |
| 745 | jstring s = env_->NewStringUTF(""); |
| 746 | ASSERT_TRUE(s != NULL); |
| 747 | jobject o = env_->NewGlobalRef(s); |
| 748 | EXPECT_TRUE(o != NULL); |
| 749 | EXPECT_TRUE(o != s); |
| 750 | |
| 751 | // TODO: check that o is a global reference. |
| 752 | } |
| 753 | |
| 754 | TEST_F(JniInternalTest, DeleteGlobalRef_NULL) { |
| 755 | env_->DeleteGlobalRef(NULL); |
| 756 | } |
| 757 | |
| 758 | TEST_F(JniInternalTest, DeleteGlobalRef) { |
| 759 | jstring s = env_->NewStringUTF(""); |
| 760 | ASSERT_TRUE(s != NULL); |
| 761 | |
| 762 | jobject o = env_->NewGlobalRef(s); |
| 763 | ASSERT_TRUE(o != NULL); |
| 764 | env_->DeleteGlobalRef(o); |
| 765 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 766 | vm_->check_jni_abort_hook = TestCheckJniAbortHook; |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 767 | // Currently, deleting an already-deleted reference is just a warning. |
| 768 | env_->DeleteGlobalRef(o); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 769 | vm_->check_jni_abort_hook = NULL; |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 770 | |
| 771 | jobject o1 = env_->NewGlobalRef(s); |
| 772 | ASSERT_TRUE(o1 != NULL); |
| 773 | jobject o2 = env_->NewGlobalRef(s); |
| 774 | ASSERT_TRUE(o2 != NULL); |
| 775 | |
| 776 | env_->DeleteGlobalRef(o1); |
| 777 | env_->DeleteGlobalRef(o2); |
| 778 | } |
| 779 | |
| 780 | TEST_F(JniInternalTest, NewWeakGlobalRef_NULL) { |
| 781 | EXPECT_TRUE(env_->NewWeakGlobalRef(NULL) == NULL); |
| 782 | } |
| 783 | |
| 784 | TEST_F(JniInternalTest, NewWeakGlobalRef) { |
| 785 | jstring s = env_->NewStringUTF(""); |
| 786 | ASSERT_TRUE(s != NULL); |
| 787 | jobject o = env_->NewWeakGlobalRef(s); |
| 788 | EXPECT_TRUE(o != NULL); |
| 789 | EXPECT_TRUE(o != s); |
| 790 | |
| 791 | // TODO: check that o is a weak global reference. |
| 792 | } |
| 793 | |
| 794 | TEST_F(JniInternalTest, DeleteWeakGlobalRef_NULL) { |
| 795 | env_->DeleteWeakGlobalRef(NULL); |
| 796 | } |
| 797 | |
| 798 | TEST_F(JniInternalTest, DeleteWeakGlobalRef) { |
| 799 | jstring s = env_->NewStringUTF(""); |
| 800 | ASSERT_TRUE(s != NULL); |
| 801 | |
| 802 | jobject o = env_->NewWeakGlobalRef(s); |
| 803 | ASSERT_TRUE(o != NULL); |
| 804 | env_->DeleteWeakGlobalRef(o); |
| 805 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 806 | vm_->check_jni_abort_hook = TestCheckJniAbortHook; |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 807 | // Currently, deleting an already-deleted reference is just a warning. |
| 808 | env_->DeleteWeakGlobalRef(o); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 809 | vm_->check_jni_abort_hook = NULL; |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 810 | |
| 811 | jobject o1 = env_->NewWeakGlobalRef(s); |
| 812 | ASSERT_TRUE(o1 != NULL); |
| 813 | jobject o2 = env_->NewWeakGlobalRef(s); |
| 814 | ASSERT_TRUE(o2 != NULL); |
| 815 | |
| 816 | env_->DeleteWeakGlobalRef(o1); |
| 817 | env_->DeleteWeakGlobalRef(o2); |
| 818 | } |
| 819 | |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 820 | #if defined(__arm__) |
| 821 | TEST_F(JniInternalTest, StaticMainMethod) { |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 822 | const ClassLoader* class_loader = LoadDex("Main"); |
| 823 | CompileDirectMethod(class_loader, "Main", "main", "([Ljava/lang/String;)V"); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 824 | |
| 825 | Class* klass = class_linker_->FindClass("LMain;", class_loader); |
| 826 | ASSERT_TRUE(klass != NULL); |
| 827 | |
| 828 | Method* method = klass->FindDirectMethod("main", "([Ljava/lang/String;)V"); |
| 829 | ASSERT_TRUE(method != NULL); |
| 830 | |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 831 | Method::InvokeStub* stub = method->GetInvokeStub(); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 832 | |
| 833 | Object* arg = NULL; |
| 834 | |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 835 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(&arg), NULL); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | TEST_F(JniInternalTest, StaticNopMethod) { |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 839 | const ClassLoader* class_loader = LoadDex("StaticLeafMethods"); |
| 840 | CompileDirectMethod(class_loader, "StaticLeafMethods", "nop", "()V"); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 841 | |
| 842 | Class* klass = class_linker_->FindClass("LStaticLeafMethods;", class_loader); |
| 843 | ASSERT_TRUE(klass != NULL); |
| 844 | |
| 845 | Method* method = klass->FindDirectMethod("nop", "()V"); |
| 846 | ASSERT_TRUE(method != NULL); |
| 847 | |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 848 | Method::InvokeStub* stub = method->GetInvokeStub(); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 849 | |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 850 | (*stub)(method, NULL, Thread::Current(), NULL, NULL); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | TEST_F(JniInternalTest, StaticIdentityByteMethod) { |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 854 | const ClassLoader* class_loader = LoadDex("StaticLeafMethods"); |
| 855 | CompileDirectMethod(class_loader, "StaticLeafMethods", "identity", "(B)B"); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 856 | |
| 857 | Class* klass = class_linker_->FindClass("LStaticLeafMethods;", class_loader); |
| 858 | ASSERT_TRUE(klass != NULL); |
| 859 | |
| 860 | Method* method = klass->FindDirectMethod("identity", "(B)B"); |
| 861 | ASSERT_TRUE(method != NULL); |
| 862 | |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 863 | Method::InvokeStub* stub = method->GetInvokeStub(); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 864 | |
| 865 | int arg; |
| 866 | JValue result; |
| 867 | |
| 868 | arg = 0; |
| 869 | result.b = -1; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 870 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(&arg), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 871 | EXPECT_EQ(0, result.b); |
| 872 | |
| 873 | arg = -1; |
| 874 | result.b = 0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 875 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(&arg), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 876 | EXPECT_EQ(-1, result.b); |
| 877 | |
| 878 | arg = SCHAR_MAX; |
| 879 | result.b = 0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 880 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(&arg), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 881 | EXPECT_EQ(SCHAR_MAX, result.b); |
| 882 | |
| 883 | arg = SCHAR_MIN; |
| 884 | result.b = 0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 885 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(&arg), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 886 | EXPECT_EQ(SCHAR_MIN, result.b); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | TEST_F(JniInternalTest, StaticIdentityIntMethod) { |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 890 | const ClassLoader* class_loader = LoadDex("StaticLeafMethods"); |
| 891 | CompileDirectMethod(class_loader, "StaticLeafMethods", "identity", "(I)I"); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 892 | |
| 893 | Class* klass = class_linker_->FindClass("LStaticLeafMethods;", class_loader); |
| 894 | ASSERT_TRUE(klass != NULL); |
| 895 | |
| 896 | Method* method = klass->FindDirectMethod("identity", "(I)I"); |
| 897 | ASSERT_TRUE(method != NULL); |
| 898 | |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 899 | Method::InvokeStub* stub = method->GetInvokeStub(); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 900 | |
| 901 | int arg; |
| 902 | JValue result; |
| 903 | |
| 904 | arg = 0; |
| 905 | result.i = -1; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 906 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(&arg), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 907 | EXPECT_EQ(0, result.i); |
| 908 | |
| 909 | arg = -1; |
| 910 | result.i = 0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 911 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(&arg), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 912 | EXPECT_EQ(-1, result.i); |
| 913 | |
| 914 | arg = INT_MAX; |
| 915 | result.i = 0; |
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(INT_MAX, result.i); |
| 918 | |
| 919 | arg = INT_MIN; |
| 920 | result.i = 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(INT_MIN, result.i); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | TEST_F(JniInternalTest, StaticIdentityDoubleMethod) { |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 926 | const ClassLoader* class_loader = LoadDex("StaticLeafMethods"); |
| 927 | CompileDirectMethod(class_loader, "StaticLeafMethods", "identity", "(D)D"); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 928 | |
| 929 | Class* klass = class_linker_->FindClass("LStaticLeafMethods;", class_loader); |
| 930 | ASSERT_TRUE(klass != NULL); |
| 931 | |
| 932 | Method* method = klass->FindDirectMethod("identity", "(D)D"); |
| 933 | ASSERT_TRUE(method != NULL); |
| 934 | |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 935 | Method::InvokeStub* stub = method->GetInvokeStub(); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 936 | |
| 937 | double arg; |
| 938 | JValue result; |
| 939 | |
| 940 | arg = 0.0; |
| 941 | result.d = -1.0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 942 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(&arg), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 943 | EXPECT_EQ(0.0, result.d); |
| 944 | |
| 945 | arg = -1.0; |
| 946 | result.d = 0.0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 947 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(&arg), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 948 | EXPECT_EQ(-1.0, result.d); |
| 949 | |
| 950 | arg = DBL_MAX; |
| 951 | result.d = 0.0; |
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(DBL_MAX, result.d); |
| 954 | |
| 955 | arg = DBL_MIN; |
| 956 | result.d = 0.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(DBL_MIN, result.d); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 959 | } |
| 960 | |
| 961 | TEST_F(JniInternalTest, StaticSumIntIntMethod) { |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 962 | const ClassLoader* class_loader = LoadDex("StaticLeafMethods"); |
| 963 | CompileDirectMethod(class_loader, "StaticLeafMethods", "sum", "(II)I"); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 964 | |
| 965 | Class* klass = class_linker_->FindClass("LStaticLeafMethods;", class_loader); |
| 966 | ASSERT_TRUE(klass != NULL); |
| 967 | |
| 968 | Method* method = klass->FindDirectMethod("sum", "(II)I"); |
| 969 | ASSERT_TRUE(method != NULL); |
| 970 | |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 971 | Method::InvokeStub* stub = method->GetInvokeStub(); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 972 | |
| 973 | int args[2]; |
| 974 | JValue result; |
| 975 | |
| 976 | args[0] = 0; |
| 977 | args[1] = 0; |
| 978 | result.i = -1; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 979 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 980 | EXPECT_EQ(0, result.i); |
| 981 | |
| 982 | args[0] = 1; |
| 983 | args[1] = 2; |
| 984 | result.i = 0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 985 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 986 | EXPECT_EQ(3, result.i); |
| 987 | |
| 988 | args[0] = -2; |
| 989 | args[1] = 5; |
| 990 | result.i = 0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 991 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 992 | EXPECT_EQ(3, result.i); |
| 993 | |
| 994 | args[0] = INT_MAX; |
| 995 | args[1] = INT_MIN; |
| 996 | result.i = 1234; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 997 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 998 | EXPECT_EQ(-1, result.i); |
| 999 | |
| 1000 | args[0] = INT_MAX; |
| 1001 | args[1] = INT_MAX; |
| 1002 | result.i = INT_MIN; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1003 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1004 | EXPECT_EQ(-2, result.i); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | TEST_F(JniInternalTest, StaticSumIntIntIntMethod) { |
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", "(III)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", "(III)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[3]; |
| 1020 | JValue result; |
| 1021 | |
| 1022 | args[0] = 0; |
| 1023 | args[1] = 0; |
| 1024 | args[2] = 0; |
| 1025 | result.i = -1; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1026 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1027 | EXPECT_EQ(0, result.i); |
| 1028 | |
| 1029 | args[0] = 1; |
| 1030 | args[1] = 2; |
| 1031 | args[2] = 3; |
| 1032 | result.i = 0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1033 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1034 | EXPECT_EQ(6, result.i); |
| 1035 | |
| 1036 | args[0] = -1; |
| 1037 | args[1] = 2; |
| 1038 | args[2] = -3; |
| 1039 | result.i = 0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1040 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1041 | EXPECT_EQ(-2, result.i); |
| 1042 | |
| 1043 | args[0] = INT_MAX; |
| 1044 | args[1] = INT_MIN; |
| 1045 | args[2] = INT_MAX; |
| 1046 | result.i = 1234; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1047 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1048 | EXPECT_EQ(2147483646, result.i); |
| 1049 | |
| 1050 | args[0] = INT_MAX; |
| 1051 | args[1] = INT_MAX; |
| 1052 | args[2] = INT_MAX; |
| 1053 | result.i = INT_MIN; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1054 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1055 | EXPECT_EQ(2147483645, result.i); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1056 | } |
| 1057 | |
| 1058 | TEST_F(JniInternalTest, StaticSumIntIntIntIntMethod) { |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 1059 | const ClassLoader* class_loader = LoadDex("StaticLeafMethods"); |
| 1060 | CompileDirectMethod(class_loader, "StaticLeafMethods", "sum", "(IIII)I"); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1061 | |
| 1062 | Class* klass = class_linker_->FindClass("LStaticLeafMethods;", class_loader); |
| 1063 | ASSERT_TRUE(klass != NULL); |
| 1064 | |
| 1065 | Method* method = klass->FindDirectMethod("sum", "(IIII)I"); |
| 1066 | ASSERT_TRUE(method != NULL); |
| 1067 | |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 1068 | Method::InvokeStub* stub = method->GetInvokeStub(); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1069 | |
| 1070 | int args[4]; |
| 1071 | JValue result; |
| 1072 | |
| 1073 | args[0] = 0; |
| 1074 | args[1] = 0; |
| 1075 | args[2] = 0; |
| 1076 | args[3] = 0; |
| 1077 | result.i = -1; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1078 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1079 | EXPECT_EQ(0, result.i); |
| 1080 | |
| 1081 | args[0] = 1; |
| 1082 | args[1] = 2; |
| 1083 | args[2] = 3; |
| 1084 | args[3] = 4; |
| 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(10, result.i); |
| 1088 | |
| 1089 | args[0] = -1; |
| 1090 | args[1] = 2; |
| 1091 | args[2] = -3; |
| 1092 | args[3] = 4; |
| 1093 | result.i = 0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1094 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1095 | EXPECT_EQ(2, result.i); |
| 1096 | |
| 1097 | args[0] = INT_MAX; |
| 1098 | args[1] = INT_MIN; |
| 1099 | args[2] = INT_MAX; |
| 1100 | args[3] = INT_MIN; |
| 1101 | result.i = 1234; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1102 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1103 | EXPECT_EQ(-2, result.i); |
| 1104 | |
| 1105 | args[0] = INT_MAX; |
| 1106 | args[1] = INT_MAX; |
| 1107 | args[2] = INT_MAX; |
| 1108 | args[3] = INT_MAX; |
| 1109 | result.i = INT_MIN; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1110 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1111 | EXPECT_EQ(-4, result.i); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1112 | } |
| 1113 | |
| 1114 | TEST_F(JniInternalTest, StaticSumIntIntIntIntIntMethod) { |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 1115 | const ClassLoader* class_loader = LoadDex("StaticLeafMethods"); |
| 1116 | CompileDirectMethod(class_loader, "StaticLeafMethods", "sum", "(IIIII)I"); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1117 | |
| 1118 | Class* klass = class_linker_->FindClass("LStaticLeafMethods;", class_loader); |
| 1119 | ASSERT_TRUE(klass != NULL); |
| 1120 | |
| 1121 | Method* method = klass->FindDirectMethod("sum", "(IIIII)I"); |
| 1122 | ASSERT_TRUE(method != NULL); |
| 1123 | |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 1124 | Method::InvokeStub* stub = method->GetInvokeStub(); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1125 | |
| 1126 | int args[5]; |
| 1127 | JValue result; |
| 1128 | |
| 1129 | args[0] = 0; |
| 1130 | args[1] = 0; |
| 1131 | args[2] = 0; |
| 1132 | args[3] = 0; |
| 1133 | args[4] = 0; |
| 1134 | result.i = -1.0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1135 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1136 | EXPECT_EQ(0, result.i); |
| 1137 | |
| 1138 | args[0] = 1; |
| 1139 | args[1] = 2; |
| 1140 | args[2] = 3; |
| 1141 | args[3] = 4; |
| 1142 | args[4] = 5; |
| 1143 | result.i = 0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1144 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1145 | EXPECT_EQ(15, result.i); |
| 1146 | |
| 1147 | args[0] = -1; |
| 1148 | args[1] = 2; |
| 1149 | args[2] = -3; |
| 1150 | args[3] = 4; |
| 1151 | args[4] = -5; |
| 1152 | result.i = 0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1153 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1154 | EXPECT_EQ(-3, result.i); |
| 1155 | |
| 1156 | args[0] = INT_MAX; |
| 1157 | args[1] = INT_MIN; |
| 1158 | args[2] = INT_MAX; |
| 1159 | args[3] = INT_MIN; |
| 1160 | args[4] = INT_MAX; |
| 1161 | result.i = 1234; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1162 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1163 | EXPECT_EQ(2147483645, result.i); |
| 1164 | |
| 1165 | args[0] = INT_MAX; |
| 1166 | args[1] = INT_MAX; |
| 1167 | args[2] = INT_MAX; |
| 1168 | args[3] = INT_MAX; |
| 1169 | args[4] = INT_MAX; |
| 1170 | result.i = INT_MIN; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1171 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1172 | EXPECT_EQ(2147483643, result.i); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1173 | } |
| 1174 | |
| 1175 | TEST_F(JniInternalTest, StaticSumDoubleDoubleMethod) { |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 1176 | const ClassLoader* class_loader = LoadDex("StaticLeafMethods"); |
| 1177 | CompileDirectMethod(class_loader, "StaticLeafMethods", "sum", "(DD)D"); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1178 | |
| 1179 | Class* klass = class_linker_->FindClass("LStaticLeafMethods;", class_loader); |
| 1180 | ASSERT_TRUE(klass != NULL); |
| 1181 | |
| 1182 | Method* method = klass->FindDirectMethod("sum", "(DD)D"); |
| 1183 | ASSERT_TRUE(method != NULL); |
| 1184 | |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 1185 | Method::InvokeStub* stub = method->GetInvokeStub(); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1186 | |
| 1187 | double args[2]; |
| 1188 | JValue result; |
| 1189 | |
| 1190 | args[0] = 0.0; |
| 1191 | args[1] = 0.0; |
| 1192 | result.d = -1.0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1193 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1194 | EXPECT_EQ(0.0, result.d); |
| 1195 | |
| 1196 | args[0] = 1.0; |
| 1197 | args[1] = 2.0; |
| 1198 | result.d = 0.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.0, result.d); |
| 1201 | |
| 1202 | args[0] = 1.0; |
| 1203 | args[1] = -2.0; |
| 1204 | result.d = 0.0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1205 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1206 | EXPECT_EQ(-1.0, result.d); |
| 1207 | |
| 1208 | args[0] = DBL_MAX; |
| 1209 | args[1] = DBL_MIN; |
| 1210 | result.d = 0.0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1211 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1212 | EXPECT_EQ(1.7976931348623157e308, result.d); |
| 1213 | |
| 1214 | args[0] = DBL_MAX; |
| 1215 | args[1] = DBL_MAX; |
| 1216 | result.d = 0.0; |
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(INFINITY, result.d); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1219 | } |
| 1220 | |
| 1221 | TEST_F(JniInternalTest, StaticSumDoubleDoubleDoubleMethod) { |
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", "(DDD)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", "(DDD)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[3]; |
| 1234 | JValue result; |
| 1235 | |
| 1236 | args[0] = 0.0; |
| 1237 | args[1] = 0.0; |
| 1238 | args[2] = 0.0; |
| 1239 | result.d = -1.0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1240 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1241 | EXPECT_EQ(0.0, result.d); |
| 1242 | |
| 1243 | args[0] = 1.0; |
| 1244 | args[1] = 2.0; |
| 1245 | args[2] = 3.0; |
| 1246 | result.d = 0.0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1247 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1248 | EXPECT_EQ(6.0, result.d); |
| 1249 | |
| 1250 | args[0] = 1.0; |
| 1251 | args[1] = -2.0; |
| 1252 | args[2] = 3.0; |
| 1253 | result.d = 0.0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1254 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1255 | EXPECT_EQ(2.0, result.d); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1256 | } |
| 1257 | |
| 1258 | TEST_F(JniInternalTest, StaticSumDoubleDoubleDoubleDoubleMethod) { |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 1259 | const ClassLoader* class_loader = LoadDex("StaticLeafMethods"); |
| 1260 | CompileDirectMethod(class_loader, "StaticLeafMethods", "sum", "(DDDD)D"); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1261 | |
| 1262 | Class* klass = class_linker_->FindClass("LStaticLeafMethods;", class_loader); |
| 1263 | ASSERT_TRUE(klass != NULL); |
| 1264 | |
| 1265 | Method* method = klass->FindDirectMethod("sum", "(DDDD)D"); |
| 1266 | ASSERT_TRUE(method != NULL); |
| 1267 | |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 1268 | Method::InvokeStub* stub = method->GetInvokeStub(); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1269 | |
| 1270 | double args[4]; |
| 1271 | JValue result; |
| 1272 | |
| 1273 | args[0] = 0.0; |
| 1274 | args[1] = 0.0; |
| 1275 | args[2] = 0.0; |
| 1276 | args[3] = 0.0; |
| 1277 | result.d = -1.0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1278 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1279 | EXPECT_EQ(0.0, result.d); |
| 1280 | |
| 1281 | args[0] = 1.0; |
| 1282 | args[1] = 2.0; |
| 1283 | args[2] = 3.0; |
| 1284 | args[3] = 4.0; |
| 1285 | result.d = 0.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(10.0, result.d); |
| 1288 | |
| 1289 | args[0] = 1.0; |
| 1290 | args[1] = -2.0; |
| 1291 | args[2] = 3.0; |
| 1292 | args[3] = -4.0; |
| 1293 | result.d = 0.0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1294 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1295 | EXPECT_EQ(-2.0, result.d); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1296 | } |
| 1297 | |
| 1298 | TEST_F(JniInternalTest, StaticSumDoubleDoubleDoubleDoubleDoubleMethod) { |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 1299 | const ClassLoader* class_loader = LoadDex("StaticLeafMethods"); |
| 1300 | CompileDirectMethod(class_loader, "StaticLeafMethods", "sum", "(DDDDD)D"); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1301 | |
| 1302 | Class* klass = class_linker_->FindClass("LStaticLeafMethods;", class_loader); |
| 1303 | ASSERT_TRUE(klass != NULL); |
| 1304 | |
| 1305 | Method* method = klass->FindDirectMethod("sum", "(DDDDD)D"); |
| 1306 | ASSERT_TRUE(method != NULL); |
| 1307 | |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 1308 | Method::InvokeStub* stub = method->GetInvokeStub(); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1309 | |
| 1310 | double args[5]; |
| 1311 | JValue result; |
| 1312 | |
| 1313 | args[0] = 0.0; |
| 1314 | args[1] = 0.0; |
| 1315 | args[2] = 0.0; |
| 1316 | args[3] = 0.0; |
| 1317 | args[4] = 0.0; |
| 1318 | result.d = -1.0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1319 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1320 | EXPECT_EQ(0.0, result.d); |
| 1321 | |
| 1322 | args[0] = 1.0; |
| 1323 | args[1] = 2.0; |
| 1324 | args[2] = 3.0; |
| 1325 | args[3] = 4.0; |
| 1326 | args[4] = 5.0; |
| 1327 | result.d = 0.0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1328 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1329 | EXPECT_EQ(15.0, result.d); |
| 1330 | |
| 1331 | args[0] = 1.0; |
| 1332 | args[1] = -2.0; |
| 1333 | args[2] = 3.0; |
| 1334 | args[3] = -4.0; |
| 1335 | args[4] = 5.0; |
| 1336 | result.d = 0.0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1337 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1338 | EXPECT_EQ(3.0, result.d); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1339 | } |
| 1340 | #endif // __arm__ |
| 1341 | |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1342 | TEST_F(JniInternalTest, Throw) { |
| 1343 | EXPECT_EQ(JNI_ERR, env_->Throw(NULL)); |
| 1344 | |
| 1345 | jclass exception_class = env_->FindClass("java/lang/RuntimeException"); |
| 1346 | ASSERT_TRUE(exception_class != NULL); |
| 1347 | jthrowable exception = reinterpret_cast<jthrowable>(env_->AllocObject(exception_class)); |
| 1348 | ASSERT_TRUE(exception != NULL); |
| 1349 | |
| 1350 | EXPECT_EQ(JNI_OK, env_->Throw(exception)); |
| 1351 | EXPECT_TRUE(env_->ExceptionCheck()); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1352 | jthrowable thrown_exception = env_->ExceptionOccurred(); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1353 | env_->ExceptionClear(); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1354 | EXPECT_TRUE(env_->IsSameObject(exception, thrown_exception)); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1355 | } |
| 1356 | |
| 1357 | TEST_F(JniInternalTest, ThrowNew) { |
| 1358 | EXPECT_EQ(JNI_ERR, env_->Throw(NULL)); |
| 1359 | |
| 1360 | jclass exception_class = env_->FindClass("java/lang/RuntimeException"); |
| 1361 | ASSERT_TRUE(exception_class != NULL); |
| 1362 | |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 1363 | jthrowable thrown_exception; |
| 1364 | |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1365 | EXPECT_EQ(JNI_OK, env_->ThrowNew(exception_class, "hello world")); |
| 1366 | EXPECT_TRUE(env_->ExceptionCheck()); |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 1367 | thrown_exception = env_->ExceptionOccurred(); |
| 1368 | env_->ExceptionClear(); |
| 1369 | EXPECT_TRUE(env_->IsInstanceOf(thrown_exception, exception_class)); |
| 1370 | |
| 1371 | EXPECT_EQ(JNI_OK, env_->ThrowNew(exception_class, NULL)); |
| 1372 | EXPECT_TRUE(env_->ExceptionCheck()); |
| 1373 | thrown_exception = env_->ExceptionOccurred(); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1374 | env_->ExceptionClear(); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1375 | EXPECT_TRUE(env_->IsInstanceOf(thrown_exception, exception_class)); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1376 | } |
| 1377 | |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1378 | // TODO: this test is DISABLED until we can actually run java.nio.Buffer's <init>. |
| 1379 | TEST_F(JniInternalTest, DISABLED_NewDirectBuffer_GetDirectBufferAddress_GetDirectBufferCapacity) { |
| 1380 | jclass buffer_class = env_->FindClass("java/nio/Buffer"); |
| 1381 | ASSERT_TRUE(buffer_class != NULL); |
| 1382 | |
| 1383 | char bytes[1024]; |
| 1384 | jobject buffer = env_->NewDirectByteBuffer(bytes, sizeof(bytes)); |
| 1385 | ASSERT_TRUE(buffer != NULL); |
| 1386 | ASSERT_TRUE(env_->IsInstanceOf(buffer, buffer_class)); |
| 1387 | ASSERT_TRUE(env_->GetDirectBufferAddress(buffer) == bytes); |
| 1388 | ASSERT_TRUE(env_->GetDirectBufferCapacity(buffer) == sizeof(bytes)); |
| 1389 | } |
| 1390 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1391 | } // namespace art |