Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 16 | |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 17 | #include "jni_internal.h" |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 18 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 19 | #include <cmath> |
| 20 | |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 21 | #include "common_test.h" |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 22 | #include "ScopedLocalRef.h" |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 23 | |
| 24 | namespace art { |
| 25 | |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 26 | class JniInternalTest : public CommonTest { |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 27 | protected: |
| 28 | virtual void SetUp() { |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 29 | CommonTest::SetUp(); |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 30 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 31 | vm_ = Runtime::Current()->GetJavaVM(); |
| 32 | |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 33 | // Turn on -verbose:jni for the JNI tests. |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 34 | gLogVerbosity.jni = true; |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 35 | |
Brian Carlstrom | 4d57143 | 2012-05-16 00:21:41 -0700 | [diff] [blame] | 36 | vm_->AttachCurrentThread(&env_, NULL); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 37 | |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 38 | ScopedLocalRef<jclass> aioobe(env_, env_->FindClass("java/lang/ArrayIndexOutOfBoundsException")); |
| 39 | CHECK(aioobe.get() != NULL); |
| 40 | aioobe_ = reinterpret_cast<jclass>(env_->NewGlobalRef(aioobe.get())); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 41 | |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 42 | ScopedLocalRef<jclass> ase(env_, env_->FindClass("java/lang/ArrayStoreException")); |
| 43 | CHECK(ase.get() != NULL); |
| 44 | ase_ = reinterpret_cast<jclass>(env_->NewGlobalRef(ase.get())); |
| 45 | |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 46 | ScopedLocalRef<jclass> sioobe(env_, env_->FindClass("java/lang/StringIndexOutOfBoundsException")); |
| 47 | CHECK(sioobe.get() != NULL); |
| 48 | sioobe_ = reinterpret_cast<jclass>(env_->NewGlobalRef(sioobe.get())); |
| 49 | } |
| 50 | |
Brian Carlstrom | 4d57143 | 2012-05-16 00:21:41 -0700 | [diff] [blame] | 51 | void CleanUpJniEnv() { |
| 52 | if (aioobe_ != NULL) { |
| 53 | env_->DeleteGlobalRef(aioobe_); |
| 54 | aioobe_ = NULL; |
| 55 | } |
| 56 | if (ase_ != NULL) { |
| 57 | env_->DeleteGlobalRef(ase_); |
| 58 | ase_ = NULL; |
| 59 | } |
| 60 | if (sioobe_ != NULL) { |
| 61 | env_->DeleteGlobalRef(sioobe_); |
| 62 | sioobe_ = NULL; |
| 63 | } |
| 64 | } |
| 65 | |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 66 | virtual void TearDown() { |
Brian Carlstrom | 4d57143 | 2012-05-16 00:21:41 -0700 | [diff] [blame] | 67 | CleanUpJniEnv(); |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 68 | CommonTest::TearDown(); |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 69 | } |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 70 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 71 | Method::InvokeStub* DoCompile(Method*& method, Object*& receiver, bool is_static, |
| 72 | const char* method_name, const char* method_signature) |
| 73 | SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) { |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 74 | const char* class_name = is_static ? "StaticLeafMethods" : "NonStaticLeafMethods"; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 75 | jobject jclass_loader(LoadDex(class_name)); |
| 76 | SirtRef<ClassLoader> class_loader(ScopedObjectAccessUnchecked(Thread::Current()).Decode<ClassLoader*>(jclass_loader)); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 77 | if (is_static) { |
| 78 | CompileDirectMethod(class_loader.get(), class_name, method_name, method_signature); |
| 79 | } else { |
| 80 | CompileVirtualMethod(NULL, "java.lang.Class", "isFinalizable", "()Z"); |
| 81 | CompileDirectMethod(NULL, "java.lang.Object", "<init>", "()V"); |
| 82 | CompileVirtualMethod(class_loader.get(), class_name, method_name, method_signature); |
| 83 | } |
| 84 | |
| 85 | Class* c = class_linker_->FindClass(DotToDescriptor(class_name).c_str(), class_loader.get()); |
| 86 | CHECK(c != NULL); |
| 87 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 88 | method = is_static ? c->FindDirectMethod(method_name, method_signature) |
| 89 | : c->FindVirtualMethod(method_name, method_signature); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 90 | CHECK(method != NULL); |
| 91 | |
| 92 | receiver = (is_static ? NULL : c->AllocObject()); |
| 93 | |
| 94 | Method::InvokeStub* stub = method->GetInvokeStub(); |
| 95 | CHECK(stub != NULL); |
| 96 | |
| 97 | return stub; |
| 98 | } |
| 99 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 100 | void InvokeNopMethod(bool is_static) SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) { |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 101 | Method* method; |
| 102 | Object* receiver; |
| 103 | Method::InvokeStub* stub = DoCompile(method, receiver, is_static, "nop", "()V"); |
| 104 | (*stub)(method, receiver, Thread::Current(), NULL, NULL); |
| 105 | } |
| 106 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 107 | void InvokeIdentityByteMethod(bool is_static) |
| 108 | SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) { |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 109 | Method* method; |
| 110 | Object* receiver; |
| 111 | Method::InvokeStub* stub = DoCompile(method, receiver, is_static, "identity", "(B)B"); |
| 112 | |
| 113 | JValue args[1]; |
| 114 | JValue result; |
| 115 | |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 116 | args[0].SetB(0); |
| 117 | result.SetB(-1); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 118 | (*stub)(method, receiver, Thread::Current(), args, &result); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 119 | EXPECT_EQ(0, result.GetB()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 120 | |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 121 | args[0].SetB(-1); |
| 122 | result.SetB(0); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 123 | (*stub)(method, receiver, Thread::Current(), args, &result); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 124 | EXPECT_EQ(-1, result.GetB()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 125 | |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 126 | args[0].SetB(SCHAR_MAX); |
| 127 | result.SetB(0); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 128 | (*stub)(method, receiver, Thread::Current(), args, &result); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 129 | EXPECT_EQ(SCHAR_MAX, result.GetB()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 130 | |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 131 | args[0].SetB(SCHAR_MIN); |
| 132 | result.SetB(0); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 133 | (*stub)(method, receiver, Thread::Current(), args, &result); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 134 | EXPECT_EQ(SCHAR_MIN, result.GetB()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 137 | void InvokeIdentityIntMethod(bool is_static) |
| 138 | SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) { |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 139 | Method* method; |
| 140 | Object* receiver; |
| 141 | Method::InvokeStub* stub = DoCompile(method, receiver, is_static, "identity", "(I)I"); |
| 142 | |
| 143 | JValue args[1]; |
| 144 | JValue result; |
| 145 | |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 146 | args[0].SetI(0); |
| 147 | result.SetI(-1); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 148 | (*stub)(method, receiver, Thread::Current(), args, &result); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 149 | EXPECT_EQ(0, result.GetI()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 150 | |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 151 | args[0].SetI(-1); |
| 152 | result.SetI(0); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 153 | (*stub)(method, receiver, Thread::Current(), args, &result); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 154 | EXPECT_EQ(-1, result.GetI()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 155 | |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 156 | args[0].SetI(INT_MAX); |
| 157 | result.SetI(0); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 158 | (*stub)(method, receiver, Thread::Current(), args, &result); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 159 | EXPECT_EQ(INT_MAX, result.GetI()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 160 | |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 161 | args[0].SetI(INT_MIN); |
| 162 | result.SetI(0); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 163 | (*stub)(method, receiver, Thread::Current(), args, &result); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 164 | EXPECT_EQ(INT_MIN, result.GetI()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 167 | void InvokeIdentityDoubleMethod(bool is_static) |
| 168 | SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) { |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 169 | Method* method; |
| 170 | Object* receiver; |
| 171 | Method::InvokeStub* stub = DoCompile(method, receiver, is_static, "identity", "(D)D"); |
| 172 | |
| 173 | JValue args[1]; |
| 174 | JValue result; |
| 175 | |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 176 | args[0].SetD(0.0); |
| 177 | result.SetD(-1.0); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 178 | (*stub)(method, receiver, Thread::Current(), args, &result); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 179 | EXPECT_EQ(0.0, result.GetD()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 180 | |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 181 | args[0].SetD(-1.0); |
| 182 | result.SetD(0.0); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 183 | (*stub)(method, receiver, Thread::Current(), args, &result); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 184 | EXPECT_EQ(-1.0, result.GetD()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 185 | |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 186 | args[0].SetD(DBL_MAX); |
| 187 | result.SetD(0.0); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 188 | (*stub)(method, receiver, Thread::Current(), args, &result); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 189 | EXPECT_EQ(DBL_MAX, result.GetD()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 190 | |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 191 | args[0].SetD(DBL_MIN); |
| 192 | result.SetD(0.0); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 193 | (*stub)(method, receiver, Thread::Current(), args, &result); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 194 | EXPECT_EQ(DBL_MIN, result.GetD()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 197 | void InvokeSumIntIntMethod(bool is_static) |
| 198 | SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) { |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 199 | Method* method; |
| 200 | Object* receiver; |
| 201 | Method::InvokeStub* stub = DoCompile(method, receiver, is_static, "sum", "(II)I"); |
| 202 | |
| 203 | JValue result; |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 204 | result.SetI(-1); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 205 | JValue args[2]; |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 206 | args[0].SetI(0); |
| 207 | args[1].SetI(0); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 208 | (*stub)(method, NULL, Thread::Current(), args, &result); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 209 | EXPECT_EQ(0, result.GetI()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 210 | |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 211 | result.SetI(0); |
| 212 | args[0].SetI(1); |
| 213 | args[1].SetI(2); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 214 | (*stub)(method, NULL, Thread::Current(), args, &result); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 215 | EXPECT_EQ(3, result.GetI()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 216 | |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 217 | result.SetI(0); |
| 218 | args[0].SetI(-2); |
| 219 | args[1].SetI(5); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 220 | (*stub)(method, NULL, Thread::Current(), args, &result); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 221 | EXPECT_EQ(3, result.GetI()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 222 | |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 223 | result.SetI(1234); |
| 224 | args[0].SetI(INT_MAX); |
| 225 | args[1].SetI(INT_MIN); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 226 | (*stub)(method, NULL, Thread::Current(), args, &result); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 227 | EXPECT_EQ(-1, result.GetI()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 228 | |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 229 | result.SetI(INT_MIN); |
| 230 | args[0].SetI(INT_MAX); |
| 231 | args[1].SetI(INT_MAX); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 232 | (*stub)(method, NULL, Thread::Current(), args, &result); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 233 | EXPECT_EQ(-2, result.GetI()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 236 | void InvokeSumIntIntIntMethod(bool is_static) |
| 237 | SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) { |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 238 | Method* method; |
| 239 | Object* receiver; |
| 240 | Method::InvokeStub* stub = DoCompile(method, receiver, is_static, "sum", "(III)I"); |
| 241 | |
| 242 | JValue result; |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 243 | result.SetI(-1); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 244 | JValue args[3]; |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 245 | args[0].SetI(0); |
| 246 | args[1].SetI(0); |
| 247 | args[2].SetI(0); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 248 | (*stub)(method, NULL, Thread::Current(), args, &result); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 249 | EXPECT_EQ(0, result.GetI()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 250 | |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 251 | result.SetI(0); |
| 252 | args[0].SetI(1); |
| 253 | args[1].SetI(2); |
| 254 | args[2].SetI(3); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 255 | (*stub)(method, NULL, Thread::Current(), args, &result); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 256 | EXPECT_EQ(6, result.GetI()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 257 | |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 258 | result.SetI(0); |
| 259 | args[0].SetI(-1); |
| 260 | args[1].SetI(2); |
| 261 | args[2].SetI(-3); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 262 | (*stub)(method, NULL, Thread::Current(), args, &result); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 263 | EXPECT_EQ(-2, result.GetI()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 264 | |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 265 | result.SetI(1234); |
| 266 | args[0].SetI(INT_MAX); |
| 267 | args[1].SetI(INT_MIN); |
| 268 | args[2].SetI(INT_MAX); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 269 | (*stub)(method, NULL, Thread::Current(), args, &result); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 270 | EXPECT_EQ(2147483646, result.GetI()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 271 | |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 272 | result.SetI(INT_MIN); |
| 273 | args[0].SetI(INT_MAX); |
| 274 | args[1].SetI(INT_MAX); |
| 275 | args[2].SetI(INT_MAX); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 276 | (*stub)(method, NULL, Thread::Current(), args, &result); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 277 | EXPECT_EQ(2147483645, result.GetI()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 280 | void InvokeSumIntIntIntIntMethod(bool is_static) |
| 281 | SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) { |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 282 | Method* method; |
| 283 | Object* receiver; |
| 284 | Method::InvokeStub* stub = DoCompile(method, receiver, is_static, "sum", "(IIII)I"); |
| 285 | |
| 286 | JValue result; |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 287 | result.SetI(-1); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 288 | JValue args[4]; |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 289 | args[0].SetI(0); |
| 290 | args[1].SetI(0); |
| 291 | args[2].SetI(0); |
| 292 | args[3].SetI(0); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 293 | (*stub)(method, NULL, Thread::Current(), args, &result); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 294 | EXPECT_EQ(0, result.GetI()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 295 | |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 296 | result.SetI(0); |
| 297 | args[0].SetI(1); |
| 298 | args[1].SetI(2); |
| 299 | args[2].SetI(3); |
| 300 | args[3].SetI(4); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 301 | (*stub)(method, NULL, Thread::Current(), args, &result); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 302 | EXPECT_EQ(10, result.GetI()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 303 | |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 304 | result.SetI(0); |
| 305 | args[0].SetI(-1); |
| 306 | args[1].SetI(2); |
| 307 | args[2].SetI(-3); |
| 308 | args[3].SetI(4); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 309 | (*stub)(method, NULL, Thread::Current(), args, &result); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 310 | EXPECT_EQ(2, result.GetI()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 311 | |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 312 | result.SetI(1234); |
| 313 | args[0].SetI(INT_MAX); |
| 314 | args[1].SetI(INT_MIN); |
| 315 | args[2].SetI(INT_MAX); |
| 316 | args[3].SetI(INT_MIN); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 317 | (*stub)(method, NULL, Thread::Current(), args, &result); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 318 | EXPECT_EQ(-2, result.GetI()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 319 | |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 320 | result.SetI(INT_MIN); |
| 321 | args[0].SetI(INT_MAX); |
| 322 | args[1].SetI(INT_MAX); |
| 323 | args[2].SetI(INT_MAX); |
| 324 | args[3].SetI(INT_MAX); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 325 | (*stub)(method, NULL, Thread::Current(), args, &result); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 326 | EXPECT_EQ(-4, result.GetI()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 327 | } |
| 328 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 329 | void InvokeSumIntIntIntIntIntMethod(bool is_static) |
| 330 | SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) { |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 331 | Method* method; |
| 332 | Object* receiver; |
| 333 | Method::InvokeStub* stub = DoCompile(method, receiver, is_static, "sum", "(IIIII)I"); |
| 334 | |
| 335 | JValue result; |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 336 | result.SetI(-1.0); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 337 | JValue args[5]; |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 338 | args[0].SetI(0); |
| 339 | args[1].SetI(0); |
| 340 | args[2].SetI(0); |
| 341 | args[3].SetI(0); |
| 342 | args[4].SetI(0); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 343 | (*stub)(method, NULL, Thread::Current(), args, &result); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 344 | EXPECT_EQ(0, result.GetI()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 345 | |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 346 | result.SetI(0); |
| 347 | args[0].SetI(1); |
| 348 | args[1].SetI(2); |
| 349 | args[2].SetI(3); |
| 350 | args[3].SetI(4); |
| 351 | args[4].SetI(5); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 352 | (*stub)(method, NULL, Thread::Current(), args, &result); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 353 | EXPECT_EQ(15, result.GetI()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 354 | |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 355 | result.SetI(0); |
| 356 | args[0].SetI(-1); |
| 357 | args[1].SetI(2); |
| 358 | args[2].SetI(-3); |
| 359 | args[3].SetI(4); |
| 360 | args[4].SetI(-5); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 361 | (*stub)(method, NULL, Thread::Current(), args, &result); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 362 | EXPECT_EQ(-3, result.GetI()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 363 | |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 364 | result.SetI(1234); |
| 365 | args[0].SetI(INT_MAX); |
| 366 | args[1].SetI(INT_MIN); |
| 367 | args[2].SetI(INT_MAX); |
| 368 | args[3].SetI(INT_MIN); |
| 369 | args[4].SetI(INT_MAX); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 370 | (*stub)(method, NULL, Thread::Current(), args, &result); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 371 | EXPECT_EQ(2147483645, result.GetI()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 372 | |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 373 | result.SetI(INT_MIN); |
| 374 | args[0].SetI(INT_MAX); |
| 375 | args[1].SetI(INT_MAX); |
| 376 | args[2].SetI(INT_MAX); |
| 377 | args[3].SetI(INT_MAX); |
| 378 | args[4].SetI(INT_MAX); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 379 | (*stub)(method, NULL, Thread::Current(), args, &result); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 380 | EXPECT_EQ(2147483643, result.GetI()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 383 | void InvokeSumDoubleDoubleMethod(bool is_static) |
| 384 | SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) { |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 385 | Method* method; |
| 386 | Object* receiver; |
| 387 | Method::InvokeStub* stub = DoCompile(method, receiver, is_static, "sum", "(DD)D"); |
| 388 | |
| 389 | JValue args[2]; |
| 390 | JValue result; |
| 391 | |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 392 | args[0].SetD(0.0); |
| 393 | args[1].SetD(0.0); |
| 394 | result.SetD(-1.0); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 395 | (*stub)(method, NULL, Thread::Current(), args, &result); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 396 | EXPECT_EQ(0.0, result.GetD()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 397 | |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 398 | args[0].SetD(1.0); |
| 399 | args[1].SetD(2.0); |
| 400 | result.SetD(0.0); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 401 | (*stub)(method, NULL, Thread::Current(), args, &result); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 402 | EXPECT_EQ(3.0, result.GetD()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 403 | |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 404 | args[0].SetD(1.0); |
| 405 | args[1].SetD(-2.0); |
| 406 | result.SetD(0.0); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 407 | (*stub)(method, NULL, Thread::Current(), args, &result); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 408 | EXPECT_EQ(-1.0, result.GetD()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 409 | |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 410 | args[0].SetD(DBL_MAX); |
| 411 | args[1].SetD(DBL_MIN); |
| 412 | result.SetD(0.0); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 413 | (*stub)(method, NULL, Thread::Current(), args, &result); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 414 | EXPECT_EQ(1.7976931348623157e308, result.GetD()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 415 | |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 416 | args[0].SetD(DBL_MAX); |
| 417 | args[1].SetD(DBL_MAX); |
| 418 | result.SetD(0.0); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 419 | (*stub)(method, NULL, Thread::Current(), args, &result); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 420 | EXPECT_EQ(INFINITY, result.GetD()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 421 | } |
| 422 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 423 | void InvokeSumDoubleDoubleDoubleMethod(bool is_static) |
| 424 | SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) { |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 425 | Method* method; |
| 426 | Object* receiver; |
| 427 | Method::InvokeStub* stub = DoCompile(method, receiver, is_static, "sum", "(DDD)D"); |
| 428 | |
| 429 | JValue args[3]; |
| 430 | JValue result; |
| 431 | |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 432 | args[0].SetD(0.0); |
| 433 | args[1].SetD(0.0); |
| 434 | args[2].SetD(0.0); |
| 435 | result.SetD(-1.0); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 436 | (*stub)(method, NULL, Thread::Current(), args, &result); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 437 | EXPECT_EQ(0.0, result.GetD()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 438 | |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 439 | args[0].SetD(1.0); |
| 440 | args[1].SetD(2.0); |
| 441 | args[2].SetD(3.0); |
| 442 | result.SetD(0.0); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 443 | (*stub)(method, NULL, Thread::Current(), args, &result); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 444 | EXPECT_EQ(6.0, result.GetD()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 445 | |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 446 | args[0].SetD(1.0); |
| 447 | args[1].SetD(-2.0); |
| 448 | args[2].SetD(3.0); |
| 449 | result.SetD(0.0); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 450 | (*stub)(method, NULL, Thread::Current(), args, &result); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 451 | EXPECT_EQ(2.0, result.GetD()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 452 | } |
| 453 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 454 | void InvokeSumDoubleDoubleDoubleDoubleMethod(bool is_static) |
| 455 | SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) { |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 456 | Method* method; |
| 457 | Object* receiver; |
| 458 | Method::InvokeStub* stub = DoCompile(method, receiver, is_static, "sum", "(DDDD)D"); |
| 459 | |
| 460 | JValue args[4]; |
| 461 | JValue result; |
| 462 | |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 463 | args[0].SetD(0.0); |
| 464 | args[1].SetD(0.0); |
| 465 | args[2].SetD(0.0); |
| 466 | args[3].SetD(0.0); |
| 467 | result.SetD(-1.0); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 468 | (*stub)(method, NULL, Thread::Current(), args, &result); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 469 | EXPECT_EQ(0.0, result.GetD()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 470 | |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 471 | args[0].SetD(1.0); |
| 472 | args[1].SetD(2.0); |
| 473 | args[2].SetD(3.0); |
| 474 | args[3].SetD(4.0); |
| 475 | result.SetD(0.0); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 476 | (*stub)(method, NULL, Thread::Current(), args, &result); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 477 | EXPECT_EQ(10.0, result.GetD()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 478 | |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 479 | args[0].SetD(1.0); |
| 480 | args[1].SetD(-2.0); |
| 481 | args[2].SetD(3.0); |
| 482 | args[3].SetD(-4.0); |
| 483 | result.SetD(0.0); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 484 | (*stub)(method, NULL, Thread::Current(), args, &result); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 485 | EXPECT_EQ(-2.0, result.GetD()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 486 | } |
| 487 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 488 | void InvokeSumDoubleDoubleDoubleDoubleDoubleMethod(bool is_static) |
| 489 | SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) { |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 490 | Method* method; |
| 491 | Object* receiver; |
| 492 | Method::InvokeStub* stub = DoCompile(method, receiver, is_static, "sum", "(DDDDD)D"); |
| 493 | |
| 494 | JValue args[5]; |
| 495 | JValue result; |
| 496 | |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 497 | args[0].SetD(0.0); |
| 498 | args[1].SetD(0.0); |
| 499 | args[2].SetD(0.0); |
| 500 | args[3].SetD(0.0); |
| 501 | args[4].SetD(0.0); |
| 502 | result.SetD(-1.0); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 503 | (*stub)(method, NULL, Thread::Current(), args, &result); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 504 | EXPECT_EQ(0.0, result.GetD()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 505 | |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 506 | args[0].SetD(1.0); |
| 507 | args[1].SetD(2.0); |
| 508 | args[2].SetD(3.0); |
| 509 | args[3].SetD(4.0); |
| 510 | args[4].SetD(5.0); |
| 511 | result.SetD(0.0); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 512 | (*stub)(method, NULL, Thread::Current(), args, &result); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 513 | EXPECT_EQ(15.0, result.GetD()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 514 | |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 515 | args[0].SetD(1.0); |
| 516 | args[1].SetD(-2.0); |
| 517 | args[2].SetD(3.0); |
| 518 | args[3].SetD(-4.0); |
| 519 | args[4].SetD(5.0); |
| 520 | result.SetD(0.0); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 521 | (*stub)(method, NULL, Thread::Current(), args, &result); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 522 | EXPECT_EQ(3.0, result.GetD()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 523 | } |
| 524 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 525 | JavaVMExt* vm_; |
Brian Carlstrom | 4d57143 | 2012-05-16 00:21:41 -0700 | [diff] [blame] | 526 | JNIEnv* env_; |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 527 | jclass aioobe_; |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 528 | jclass ase_; |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 529 | jclass sioobe_; |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 530 | }; |
| 531 | |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 532 | TEST_F(JniInternalTest, AllocObject) { |
| 533 | jclass c = env_->FindClass("java/lang/String"); |
| 534 | ASSERT_TRUE(c != NULL); |
| 535 | jobject o = env_->AllocObject(c); |
| 536 | ASSERT_TRUE(o != NULL); |
| 537 | |
| 538 | // We have an instance of the class we asked for... |
| 539 | ASSERT_TRUE(env_->IsInstanceOf(o, c)); |
| 540 | // ...whose fields haven't been initialized because |
| 541 | // we didn't call a constructor. |
| 542 | ASSERT_EQ(0, env_->GetIntField(o, env_->GetFieldID(c, "count", "I"))); |
| 543 | ASSERT_EQ(0, env_->GetIntField(o, env_->GetFieldID(c, "offset", "I"))); |
| 544 | ASSERT_TRUE(env_->GetObjectField(o, env_->GetFieldID(c, "value", "[C")) == NULL); |
| 545 | } |
| 546 | |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 547 | TEST_F(JniInternalTest, GetVersion) { |
| 548 | ASSERT_EQ(JNI_VERSION_1_6, env_->GetVersion()); |
| 549 | } |
| 550 | |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 551 | #define EXPECT_CLASS_FOUND(NAME) \ |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 552 | EXPECT_TRUE(env_->FindClass(NAME) != NULL); \ |
| 553 | EXPECT_FALSE(env_->ExceptionCheck()) |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 554 | |
| 555 | #define EXPECT_CLASS_NOT_FOUND(NAME) \ |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 556 | EXPECT_TRUE(env_->FindClass(NAME) == NULL); \ |
| 557 | EXPECT_TRUE(env_->ExceptionCheck()); \ |
| 558 | env_->ExceptionClear() |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 559 | |
| 560 | TEST_F(JniInternalTest, FindClass) { |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 561 | // Reference types... |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 562 | EXPECT_CLASS_FOUND("java/lang/String"); |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 563 | // ...for arrays too, where you must include "L;". |
| 564 | EXPECT_CLASS_FOUND("[Ljava/lang/String;"); |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame] | 565 | // Primitive arrays are okay too, if the primitive type is valid. |
| 566 | EXPECT_CLASS_FOUND("[C"); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 567 | |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 568 | { |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 569 | // We support . as well as / for compatibility, if -Xcheck:jni is off. |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame] | 570 | CheckJniAbortCatcher check_jni_abort_catcher; |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 571 | EXPECT_CLASS_FOUND("java.lang.String"); |
Elliott Hughes | 56ef042 | 2012-06-19 14:35:04 -0700 | [diff] [blame] | 572 | check_jni_abort_catcher.Check("illegal class name 'java.lang.String'"); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 573 | EXPECT_CLASS_NOT_FOUND("Ljava.lang.String;"); |
Elliott Hughes | 56ef042 | 2012-06-19 14:35:04 -0700 | [diff] [blame] | 574 | check_jni_abort_catcher.Check("illegal class name 'Ljava.lang.String;'"); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 575 | EXPECT_CLASS_FOUND("[Ljava.lang.String;"); |
Elliott Hughes | 56ef042 | 2012-06-19 14:35:04 -0700 | [diff] [blame] | 576 | check_jni_abort_catcher.Check("illegal class name '[Ljava.lang.String;'"); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 577 | EXPECT_CLASS_NOT_FOUND("[java.lang.String"); |
Elliott Hughes | 56ef042 | 2012-06-19 14:35:04 -0700 | [diff] [blame] | 578 | check_jni_abort_catcher.Check("illegal class name '[java.lang.String'"); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 579 | |
| 580 | // You can't include the "L;" in a JNI class descriptor. |
| 581 | EXPECT_CLASS_NOT_FOUND("Ljava/lang/String;"); |
Elliott Hughes | 56ef042 | 2012-06-19 14:35:04 -0700 | [diff] [blame] | 582 | check_jni_abort_catcher.Check("illegal class name 'Ljava/lang/String;'"); |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame] | 583 | |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 584 | // But you must include it for an array of any reference type. |
| 585 | EXPECT_CLASS_NOT_FOUND("[java/lang/String"); |
Elliott Hughes | 56ef042 | 2012-06-19 14:35:04 -0700 | [diff] [blame] | 586 | check_jni_abort_catcher.Check("illegal class name '[java/lang/String'"); |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame] | 587 | |
| 588 | EXPECT_CLASS_NOT_FOUND("[K"); |
Elliott Hughes | 56ef042 | 2012-06-19 14:35:04 -0700 | [diff] [blame] | 589 | check_jni_abort_catcher.Check("illegal class name '[K'"); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 590 | } |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 591 | |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 592 | // But primitive types aren't allowed... |
| 593 | EXPECT_CLASS_NOT_FOUND("C"); |
| 594 | EXPECT_CLASS_NOT_FOUND("K"); |
| 595 | } |
| 596 | |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 597 | #define EXPECT_EXCEPTION(exception_class) \ |
| 598 | do { \ |
| 599 | EXPECT_TRUE(env_->ExceptionCheck()); \ |
| 600 | jthrowable exception = env_->ExceptionOccurred(); \ |
| 601 | EXPECT_NE(static_cast<jthrowable>(NULL), exception); \ |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 602 | env_->ExceptionClear(); \ |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 603 | EXPECT_TRUE(env_->IsInstanceOf(exception, exception_class)); \ |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 604 | } while (false) |
| 605 | |
| 606 | TEST_F(JniInternalTest, GetFieldID) { |
| 607 | jclass jlnsfe = env_->FindClass("java/lang/NoSuchFieldError"); |
| 608 | ASSERT_TRUE(jlnsfe != NULL); |
| 609 | jclass c = env_->FindClass("java/lang/String"); |
| 610 | ASSERT_TRUE(c != NULL); |
| 611 | |
| 612 | // Wrong type. |
| 613 | jfieldID fid = env_->GetFieldID(c, "count", "J"); |
| 614 | EXPECT_EQ(static_cast<jfieldID>(NULL), fid); |
| 615 | EXPECT_EXCEPTION(jlnsfe); |
| 616 | |
Ian Rogers | b17d08b | 2011-09-02 16:16:49 -0700 | [diff] [blame] | 617 | // Wrong type where type doesn't exist. |
| 618 | fid = env_->GetFieldID(c, "count", "Lrod/jane/freddy;"); |
| 619 | EXPECT_EQ(static_cast<jfieldID>(NULL), fid); |
| 620 | EXPECT_EXCEPTION(jlnsfe); |
| 621 | |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 622 | // Wrong name. |
| 623 | fid = env_->GetFieldID(c, "Count", "I"); |
| 624 | EXPECT_EQ(static_cast<jfieldID>(NULL), fid); |
| 625 | EXPECT_EXCEPTION(jlnsfe); |
| 626 | |
| 627 | // Good declared field lookup. |
| 628 | fid = env_->GetFieldID(c, "count", "I"); |
| 629 | EXPECT_NE(static_cast<jfieldID>(NULL), fid); |
| 630 | EXPECT_TRUE(fid != NULL); |
| 631 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 632 | |
| 633 | // Good superclass field lookup. |
| 634 | c = env_->FindClass("java/lang/StringBuilder"); |
| 635 | fid = env_->GetFieldID(c, "count", "I"); |
| 636 | EXPECT_NE(static_cast<jfieldID>(NULL), fid); |
| 637 | EXPECT_TRUE(fid != NULL); |
| 638 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 639 | |
| 640 | // Not instance. |
| 641 | fid = env_->GetFieldID(c, "CASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;"); |
| 642 | EXPECT_EQ(static_cast<jfieldID>(NULL), fid); |
| 643 | EXPECT_EXCEPTION(jlnsfe); |
| 644 | } |
| 645 | |
| 646 | TEST_F(JniInternalTest, GetStaticFieldID) { |
| 647 | jclass jlnsfe = env_->FindClass("java/lang/NoSuchFieldError"); |
| 648 | ASSERT_TRUE(jlnsfe != NULL); |
| 649 | jclass c = env_->FindClass("java/lang/String"); |
| 650 | ASSERT_TRUE(c != NULL); |
| 651 | |
| 652 | // Wrong type. |
| 653 | jfieldID fid = env_->GetStaticFieldID(c, "CASE_INSENSITIVE_ORDER", "J"); |
| 654 | EXPECT_EQ(static_cast<jfieldID>(NULL), fid); |
| 655 | EXPECT_EXCEPTION(jlnsfe); |
| 656 | |
Ian Rogers | b17d08b | 2011-09-02 16:16:49 -0700 | [diff] [blame] | 657 | // Wrong type where type doesn't exist. |
| 658 | fid = env_->GetStaticFieldID(c, "CASE_INSENSITIVE_ORDER", "Lrod/jane/freddy;"); |
| 659 | EXPECT_EQ(static_cast<jfieldID>(NULL), fid); |
| 660 | EXPECT_EXCEPTION(jlnsfe); |
| 661 | |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 662 | // Wrong name. |
| 663 | fid = env_->GetStaticFieldID(c, "cASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;"); |
| 664 | EXPECT_EQ(static_cast<jfieldID>(NULL), fid); |
| 665 | EXPECT_EXCEPTION(jlnsfe); |
| 666 | |
| 667 | // Good declared field lookup. |
| 668 | fid = env_->GetStaticFieldID(c, "CASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;"); |
| 669 | EXPECT_NE(static_cast<jfieldID>(NULL), fid); |
| 670 | EXPECT_TRUE(fid != NULL); |
| 671 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 672 | |
| 673 | // Not static. |
| 674 | fid = env_->GetStaticFieldID(c, "count", "I"); |
| 675 | EXPECT_EQ(static_cast<jfieldID>(NULL), fid); |
| 676 | EXPECT_EXCEPTION(jlnsfe); |
| 677 | } |
| 678 | |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 679 | TEST_F(JniInternalTest, GetMethodID) { |
| 680 | jclass jlobject = env_->FindClass("java/lang/Object"); |
| 681 | jclass jlstring = env_->FindClass("java/lang/String"); |
| 682 | jclass jlnsme = env_->FindClass("java/lang/NoSuchMethodError"); |
| 683 | |
| 684 | // Sanity check that no exceptions are pending |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 685 | ASSERT_FALSE(env_->ExceptionCheck()); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 686 | |
| 687 | // Check that java.lang.Object.foo() doesn't exist and NoSuchMethodError is |
| 688 | // a pending exception |
| 689 | jmethodID method = env_->GetMethodID(jlobject, "foo", "()V"); |
| 690 | EXPECT_EQ(static_cast<jmethodID>(NULL), method); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 691 | EXPECT_EXCEPTION(jlnsme); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 692 | |
| 693 | // Check that java.lang.Object.equals() does exist |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 694 | method = env_->GetMethodID(jlobject, "equals", "(Ljava/lang/Object;)Z"); |
| 695 | EXPECT_NE(static_cast<jmethodID>(NULL), method); |
| 696 | EXPECT_FALSE(env_->ExceptionCheck()); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 697 | |
| 698 | // Check that GetMethodID for java.lang.String.valueOf(int) fails as the |
| 699 | // method is static |
| 700 | method = env_->GetMethodID(jlstring, "valueOf", "(I)Ljava/lang/String;"); |
| 701 | EXPECT_EQ(static_cast<jmethodID>(NULL), method); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 702 | EXPECT_EXCEPTION(jlnsme); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | TEST_F(JniInternalTest, GetStaticMethodID) { |
| 706 | jclass jlobject = env_->FindClass("java/lang/Object"); |
| 707 | jclass jlnsme = env_->FindClass("java/lang/NoSuchMethodError"); |
| 708 | |
| 709 | // Sanity check that no exceptions are pending |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 710 | ASSERT_FALSE(env_->ExceptionCheck()); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 711 | |
| 712 | // Check that java.lang.Object.foo() doesn't exist and NoSuchMethodError is |
| 713 | // a pending exception |
| 714 | jmethodID method = env_->GetStaticMethodID(jlobject, "foo", "()V"); |
| 715 | EXPECT_EQ(static_cast<jmethodID>(NULL), method); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 716 | EXPECT_EXCEPTION(jlnsme); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 717 | |
| 718 | // Check that GetStaticMethodID for java.lang.Object.equals(Object) fails as |
| 719 | // the method is not static |
| 720 | method = env_->GetStaticMethodID(jlobject, "equals", "(Ljava/lang/Object;)Z"); |
| 721 | EXPECT_EQ(static_cast<jmethodID>(NULL), method); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 722 | EXPECT_EXCEPTION(jlnsme); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 723 | |
| 724 | // Check that java.lang.String.valueOf(int) does exist |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 725 | jclass jlstring = env_->FindClass("java/lang/String"); |
| 726 | method = env_->GetStaticMethodID(jlstring, "valueOf", |
| 727 | "(I)Ljava/lang/String;"); |
| 728 | EXPECT_NE(static_cast<jmethodID>(NULL), method); |
| 729 | EXPECT_FALSE(env_->ExceptionCheck()); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 730 | } |
| 731 | |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 732 | TEST_F(JniInternalTest, FromReflectedField_ToReflectedField) { |
| 733 | jclass jlrField = env_->FindClass("java/lang/reflect/Field"); |
| 734 | jclass c = env_->FindClass("java/lang/String"); |
| 735 | ASSERT_TRUE(c != NULL); |
| 736 | jfieldID fid = env_->GetFieldID(c, "count", "I"); |
| 737 | ASSERT_TRUE(fid != NULL); |
| 738 | // Turn the fid into a java.lang.reflect.Field... |
| 739 | jobject field = env_->ToReflectedField(c, fid, JNI_FALSE); |
| 740 | ASSERT_TRUE(c != NULL); |
| 741 | ASSERT_TRUE(env_->IsInstanceOf(field, jlrField)); |
| 742 | // ...and back again. |
| 743 | jfieldID fid2 = env_->FromReflectedField(field); |
| 744 | ASSERT_TRUE(fid2 != NULL); |
| 745 | } |
| 746 | |
| 747 | TEST_F(JniInternalTest, FromReflectedMethod_ToReflectedMethod) { |
| 748 | jclass jlrMethod = env_->FindClass("java/lang/reflect/Method"); |
| 749 | jclass c = env_->FindClass("java/lang/String"); |
| 750 | ASSERT_TRUE(c != NULL); |
| 751 | jmethodID mid = env_->GetMethodID(c, "length", "()I"); |
| 752 | ASSERT_TRUE(mid != NULL); |
| 753 | // Turn the mid into a java.lang.reflect.Method... |
| 754 | jobject method = env_->ToReflectedMethod(c, mid, JNI_FALSE); |
| 755 | ASSERT_TRUE(c != NULL); |
| 756 | ASSERT_TRUE(env_->IsInstanceOf(method, jlrMethod)); |
| 757 | // ...and back again. |
| 758 | jmethodID mid2 = env_->FromReflectedMethod(method); |
| 759 | ASSERT_TRUE(mid2 != NULL); |
| 760 | } |
| 761 | |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 762 | void BogusMethod() { |
| 763 | // You can't pass NULL function pointers to RegisterNatives. |
| 764 | } |
| 765 | |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 766 | TEST_F(JniInternalTest, RegisterNatives) { |
| 767 | jclass jlobject = env_->FindClass("java/lang/Object"); |
| 768 | jclass jlnsme = env_->FindClass("java/lang/NoSuchMethodError"); |
| 769 | |
| 770 | // Sanity check that no exceptions are pending |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 771 | ASSERT_FALSE(env_->ExceptionCheck()); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 772 | |
| 773 | // Check that registering to a non-existent java.lang.Object.foo() causes a |
| 774 | // NoSuchMethodError |
| 775 | { |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 776 | JNINativeMethod methods[] = { { "foo", "()V", NULL } }; |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 777 | env_->RegisterNatives(jlobject, methods, 1); |
| 778 | } |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 779 | EXPECT_EXCEPTION(jlnsme); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 780 | |
| 781 | // Check that registering non-native methods causes a NoSuchMethodError |
| 782 | { |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 783 | JNINativeMethod methods[] = { { "equals", "(Ljava/lang/Object;)Z", NULL } }; |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 784 | env_->RegisterNatives(jlobject, methods, 1); |
| 785 | } |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 786 | EXPECT_EXCEPTION(jlnsme); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 787 | |
| 788 | // Check that registering native methods is successful |
| 789 | { |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 790 | JNINativeMethod methods[] = { { "notify", "()V", reinterpret_cast<void*>(BogusMethod) } }; |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 791 | env_->RegisterNatives(jlobject, methods, 1); |
| 792 | } |
| 793 | EXPECT_FALSE(env_->ExceptionCheck()); |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 794 | |
| 795 | env_->UnregisterNatives(jlobject); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 796 | } |
| 797 | |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 798 | #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] | 799 | jsize size = 4; \ |
| 800 | /* Allocate an array and check it has the right type and length. */ \ |
| 801 | scalar_type ## Array a = env_->new_fn(size); \ |
| 802 | EXPECT_TRUE(a != NULL); \ |
| 803 | EXPECT_TRUE(env_->IsInstanceOf(a, env_->FindClass(expected_class_descriptor))); \ |
| 804 | EXPECT_EQ(size, env_->GetArrayLength(a)); \ |
| 805 | /* AIOOBE for negative start offset. */ \ |
| 806 | env_->get_region_fn(a, -1, 1, NULL); \ |
| 807 | EXPECT_EXCEPTION(aioobe_); \ |
| 808 | env_->set_region_fn(a, -1, 1, NULL); \ |
| 809 | EXPECT_EXCEPTION(aioobe_); \ |
| 810 | /* AIOOBE for negative length. */ \ |
| 811 | env_->get_region_fn(a, 0, -1, NULL); \ |
| 812 | EXPECT_EXCEPTION(aioobe_); \ |
| 813 | env_->set_region_fn(a, 0, -1, NULL); \ |
| 814 | EXPECT_EXCEPTION(aioobe_); \ |
| 815 | /* AIOOBE for buffer overrun. */ \ |
| 816 | env_->get_region_fn(a, size - 1, size, NULL); \ |
| 817 | EXPECT_EXCEPTION(aioobe_); \ |
| 818 | env_->set_region_fn(a, size - 1, size, NULL); \ |
| 819 | EXPECT_EXCEPTION(aioobe_); \ |
| 820 | /* Prepare a couple of buffers. */ \ |
Elliott Hughes | ee0fa76 | 2012-03-26 17:12:41 -0700 | [diff] [blame] | 821 | UniquePtr<scalar_type[]> src_buf(new scalar_type[size]); \ |
| 822 | UniquePtr<scalar_type[]> dst_buf(new scalar_type[size]); \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 823 | for (jsize i = 0; i < size; ++i) { src_buf[i] = scalar_type(i); } \ |
| 824 | for (jsize i = 0; i < size; ++i) { dst_buf[i] = scalar_type(-1); } \ |
| 825 | /* Copy all of src_buf onto the heap. */ \ |
Elliott Hughes | ee0fa76 | 2012-03-26 17:12:41 -0700 | [diff] [blame] | 826 | env_->set_region_fn(a, 0, size, &src_buf[0]); \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 827 | /* Copy back only part. */ \ |
| 828 | env_->get_region_fn(a, 1, size - 2, &dst_buf[1]); \ |
Elliott Hughes | ee0fa76 | 2012-03-26 17:12:41 -0700 | [diff] [blame] | 829 | EXPECT_NE(memcmp(&src_buf[0], &dst_buf[0], size * sizeof(scalar_type)), 0) << "short copy equal"; \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 830 | /* Copy the missing pieces. */ \ |
Elliott Hughes | ee0fa76 | 2012-03-26 17:12:41 -0700 | [diff] [blame] | 831 | env_->get_region_fn(a, 0, 1, &dst_buf[0]); \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 832 | env_->get_region_fn(a, size - 1, 1, &dst_buf[size - 1]); \ |
Elliott Hughes | ee0fa76 | 2012-03-26 17:12:41 -0700 | [diff] [blame] | 833 | EXPECT_EQ(memcmp(&src_buf[0], &dst_buf[0], size * sizeof(scalar_type)), 0) << "fixed copy not equal"; \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 834 | /* Copy back the whole array. */ \ |
Elliott Hughes | ee0fa76 | 2012-03-26 17:12:41 -0700 | [diff] [blame] | 835 | env_->get_region_fn(a, 0, size, &dst_buf[0]); \ |
| 836 | EXPECT_EQ(memcmp(&src_buf[0], &dst_buf[0], size * sizeof(scalar_type)), 0) << "full copy not equal"; \ |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 837 | /* GetPrimitiveArrayCritical */ \ |
| 838 | void* v = env_->GetPrimitiveArrayCritical(a, NULL); \ |
Elliott Hughes | ee0fa76 | 2012-03-26 17:12:41 -0700 | [diff] [blame] | 839 | EXPECT_EQ(memcmp(&src_buf[0], v, size * sizeof(scalar_type)), 0) << "GetPrimitiveArrayCritical not equal"; \ |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 840 | env_->ReleasePrimitiveArrayCritical(a, v, 0); \ |
| 841 | /* GetXArrayElements */ \ |
| 842 | scalar_type* xs = env_->get_elements_fn(a, NULL); \ |
Elliott Hughes | ee0fa76 | 2012-03-26 17:12:41 -0700 | [diff] [blame] | 843 | EXPECT_EQ(memcmp(&src_buf[0], xs, size * sizeof(scalar_type)), 0) << # get_elements_fn " not equal"; \ |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 844 | env_->release_elements_fn(a, xs, 0); \ |
| 845 | EXPECT_EQ(reinterpret_cast<uintptr_t>(v), reinterpret_cast<uintptr_t>(xs)) |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 846 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 847 | TEST_F(JniInternalTest, BooleanArrays) { |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 848 | EXPECT_PRIMITIVE_ARRAY(NewBooleanArray, GetBooleanArrayRegion, SetBooleanArrayRegion, GetBooleanArrayElements, ReleaseBooleanArrayElements, jboolean, "[Z"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 849 | } |
| 850 | TEST_F(JniInternalTest, ByteArrays) { |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 851 | EXPECT_PRIMITIVE_ARRAY(NewByteArray, GetByteArrayRegion, SetByteArrayRegion, GetByteArrayElements, ReleaseByteArrayElements, jbyte, "[B"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 852 | } |
| 853 | TEST_F(JniInternalTest, CharArrays) { |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 854 | EXPECT_PRIMITIVE_ARRAY(NewCharArray, GetCharArrayRegion, SetCharArrayRegion, GetCharArrayElements, ReleaseCharArrayElements, jchar, "[C"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 855 | } |
| 856 | TEST_F(JniInternalTest, DoubleArrays) { |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 857 | EXPECT_PRIMITIVE_ARRAY(NewDoubleArray, GetDoubleArrayRegion, SetDoubleArrayRegion, GetDoubleArrayElements, ReleaseDoubleArrayElements, jdouble, "[D"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 858 | } |
| 859 | TEST_F(JniInternalTest, FloatArrays) { |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 860 | EXPECT_PRIMITIVE_ARRAY(NewFloatArray, GetFloatArrayRegion, SetFloatArrayRegion, GetFloatArrayElements, ReleaseFloatArrayElements, jfloat, "[F"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 861 | } |
| 862 | TEST_F(JniInternalTest, IntArrays) { |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 863 | EXPECT_PRIMITIVE_ARRAY(NewIntArray, GetIntArrayRegion, SetIntArrayRegion, GetIntArrayElements, ReleaseIntArrayElements, jint, "[I"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 864 | } |
| 865 | TEST_F(JniInternalTest, LongArrays) { |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 866 | EXPECT_PRIMITIVE_ARRAY(NewLongArray, GetLongArrayRegion, SetLongArrayRegion, GetLongArrayElements, ReleaseLongArrayElements, jlong, "[J"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 867 | } |
| 868 | TEST_F(JniInternalTest, ShortArrays) { |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 869 | EXPECT_PRIMITIVE_ARRAY(NewShortArray, GetShortArrayRegion, SetShortArrayRegion, GetShortArrayElements, ReleaseShortArrayElements, jshort, "[S"); |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 870 | } |
| 871 | |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 872 | TEST_F(JniInternalTest, NewObjectArray) { |
| 873 | // TODO: death tests for negative array sizes. |
| 874 | |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 875 | // TODO: check non-NULL initial elements. |
| 876 | |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 877 | jclass element_class = env_->FindClass("java/lang/String"); |
| 878 | ASSERT_TRUE(element_class != NULL); |
| 879 | jclass array_class = env_->FindClass("[Ljava/lang/String;"); |
| 880 | ASSERT_TRUE(array_class != NULL); |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 881 | |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 882 | jobjectArray a; |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 883 | |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 884 | a = env_->NewObjectArray(0, element_class, NULL); |
| 885 | EXPECT_TRUE(a != NULL); |
| 886 | EXPECT_TRUE(env_->IsInstanceOf(a, array_class)); |
| 887 | EXPECT_EQ(0, env_->GetArrayLength(a)); |
| 888 | |
| 889 | a = env_->NewObjectArray(1, element_class, NULL); |
| 890 | EXPECT_TRUE(a != NULL); |
| 891 | EXPECT_TRUE(env_->IsInstanceOf(a, array_class)); |
| 892 | EXPECT_EQ(1, env_->GetArrayLength(a)); |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 893 | EXPECT_TRUE(env_->IsSameObject(env_->GetObjectArrayElement(a, 0), NULL)); |
| 894 | |
| 895 | jstring s = env_->NewStringUTF("poop"); |
| 896 | a = env_->NewObjectArray(2, element_class, s); |
| 897 | EXPECT_TRUE(a != NULL); |
| 898 | EXPECT_TRUE(env_->IsInstanceOf(a, array_class)); |
| 899 | EXPECT_EQ(2, env_->GetArrayLength(a)); |
| 900 | EXPECT_TRUE(env_->IsSameObject(env_->GetObjectArrayElement(a, 0), s)); |
| 901 | EXPECT_TRUE(env_->IsSameObject(env_->GetObjectArrayElement(a, 1), s)); |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | TEST_F(JniInternalTest, GetArrayLength) { |
| 905 | // Already tested in NewObjectArray/NewPrimitiveArray. |
Elliott Hughes | 8a26c5c | 2011-08-15 18:35:43 -0700 | [diff] [blame] | 906 | } |
| 907 | |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 908 | TEST_F(JniInternalTest, GetObjectClass) { |
| 909 | jclass string_class = env_->FindClass("java/lang/String"); |
| 910 | ASSERT_TRUE(string_class != NULL); |
| 911 | jclass class_class = env_->FindClass("java/lang/Class"); |
| 912 | ASSERT_TRUE(class_class != NULL); |
| 913 | |
| 914 | jstring s = env_->NewStringUTF("poop"); |
| 915 | jclass c = env_->GetObjectClass(s); |
| 916 | ASSERT_TRUE(env_->IsSameObject(string_class, c)); |
| 917 | |
| 918 | jclass c2 = env_->GetObjectClass(c); |
| 919 | ASSERT_TRUE(env_->IsSameObject(class_class, env_->GetObjectClass(c2))); |
| 920 | } |
| 921 | |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 922 | TEST_F(JniInternalTest, GetSuperclass) { |
| 923 | jclass object_class = env_->FindClass("java/lang/Object"); |
| 924 | ASSERT_TRUE(object_class != NULL); |
| 925 | jclass string_class = env_->FindClass("java/lang/String"); |
| 926 | ASSERT_TRUE(string_class != NULL); |
Ian Rogers | dc18020 | 2012-01-29 14:47:29 -0800 | [diff] [blame] | 927 | jclass runnable_interface = env_->FindClass("java/lang/Runnable"); |
| 928 | ASSERT_TRUE(runnable_interface != NULL); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 929 | ASSERT_TRUE(env_->IsSameObject(object_class, env_->GetSuperclass(string_class))); |
| 930 | ASSERT_TRUE(env_->GetSuperclass(object_class) == NULL); |
Ian Rogers | dc18020 | 2012-01-29 14:47:29 -0800 | [diff] [blame] | 931 | ASSERT_TRUE(env_->IsSameObject(object_class, env_->GetSuperclass(runnable_interface))); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 932 | } |
| 933 | |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 934 | TEST_F(JniInternalTest, IsAssignableFrom) { |
| 935 | jclass object_class = env_->FindClass("java/lang/Object"); |
| 936 | ASSERT_TRUE(object_class != NULL); |
| 937 | jclass string_class = env_->FindClass("java/lang/String"); |
| 938 | ASSERT_TRUE(string_class != NULL); |
| 939 | |
| 940 | ASSERT_TRUE(env_->IsAssignableFrom(object_class, string_class)); |
| 941 | ASSERT_FALSE(env_->IsAssignableFrom(string_class, object_class)); |
| 942 | } |
| 943 | |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 944 | TEST_F(JniInternalTest, GetObjectRefType) { |
| 945 | jclass local = env_->FindClass("java/lang/Object"); |
| 946 | ASSERT_TRUE(local != NULL); |
| 947 | EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(local)); |
| 948 | |
| 949 | jobject global = env_->NewGlobalRef(local); |
| 950 | EXPECT_EQ(JNIGlobalRefType, env_->GetObjectRefType(global)); |
| 951 | |
| 952 | jweak weak_global = env_->NewWeakGlobalRef(local); |
| 953 | EXPECT_EQ(JNIWeakGlobalRefType, env_->GetObjectRefType(weak_global)); |
| 954 | |
| 955 | jobject invalid = reinterpret_cast<jobject>(this); |
| 956 | EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(invalid)); |
| 957 | |
| 958 | // TODO: invoke a native method and test that its arguments are considered local references. |
| 959 | } |
| 960 | |
Elliott Hughes | 8a26c5c | 2011-08-15 18:35:43 -0700 | [diff] [blame] | 961 | TEST_F(JniInternalTest, NewStringUTF) { |
| 962 | EXPECT_TRUE(env_->NewStringUTF(NULL) == NULL); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 963 | jstring s; |
| 964 | |
| 965 | s = env_->NewStringUTF(""); |
| 966 | EXPECT_TRUE(s != NULL); |
| 967 | EXPECT_EQ(0, env_->GetStringLength(s)); |
| 968 | EXPECT_EQ(0, env_->GetStringUTFLength(s)); |
| 969 | s = env_->NewStringUTF("hello"); |
| 970 | EXPECT_TRUE(s != NULL); |
| 971 | EXPECT_EQ(5, env_->GetStringLength(s)); |
| 972 | EXPECT_EQ(5, env_->GetStringUTFLength(s)); |
| 973 | |
Elliott Hughes | 8a26c5c | 2011-08-15 18:35:43 -0700 | [diff] [blame] | 974 | // TODO: check some non-ASCII strings. |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 975 | } |
| 976 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 977 | TEST_F(JniInternalTest, NewString) { |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 978 | jchar chars[] = { 'h', 'i' }; |
| 979 | jstring s; |
| 980 | s = env_->NewString(chars, 0); |
| 981 | EXPECT_TRUE(s != NULL); |
| 982 | EXPECT_EQ(0, env_->GetStringLength(s)); |
| 983 | EXPECT_EQ(0, env_->GetStringUTFLength(s)); |
| 984 | s = env_->NewString(chars, 2); |
| 985 | EXPECT_TRUE(s != NULL); |
| 986 | EXPECT_EQ(2, env_->GetStringLength(s)); |
| 987 | EXPECT_EQ(2, env_->GetStringUTFLength(s)); |
| 988 | |
| 989 | // TODO: check some non-ASCII strings. |
| 990 | } |
| 991 | |
Jesse Wilson | 25e79a5 | 2011-11-18 15:31:58 -0500 | [diff] [blame] | 992 | TEST_F(JniInternalTest, NewStringNullCharsZeroLength) { |
| 993 | jstring s = env_->NewString(NULL, 0); |
| 994 | EXPECT_TRUE(s != NULL); |
| 995 | EXPECT_EQ(0, env_->GetStringLength(s)); |
| 996 | } |
| 997 | |
Brian Carlstrom | 3625812 | 2011-12-09 12:55:51 -0800 | [diff] [blame] | 998 | // TODO: fix gtest death tests on host http://b/5690440 (and target) |
| 999 | TEST_F(JniInternalTest, DISABLED_NewStringNullCharsNonzeroLength) { |
Jesse Wilson | 25e79a5 | 2011-11-18 15:31:58 -0500 | [diff] [blame] | 1000 | ASSERT_DEATH(env_->NewString(NULL, 1), ""); |
| 1001 | } |
| 1002 | |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1003 | TEST_F(JniInternalTest, GetStringLength_GetStringUTFLength) { |
| 1004 | // Already tested in the NewString/NewStringUTF tests. |
| 1005 | } |
| 1006 | |
| 1007 | TEST_F(JniInternalTest, GetStringRegion_GetStringUTFRegion) { |
| 1008 | jstring s = env_->NewStringUTF("hello"); |
| 1009 | ASSERT_TRUE(s != NULL); |
| 1010 | |
| 1011 | env_->GetStringRegion(s, -1, 0, NULL); |
| 1012 | EXPECT_EXCEPTION(sioobe_); |
| 1013 | env_->GetStringRegion(s, 0, -1, NULL); |
| 1014 | EXPECT_EXCEPTION(sioobe_); |
| 1015 | env_->GetStringRegion(s, 0, 10, NULL); |
| 1016 | EXPECT_EXCEPTION(sioobe_); |
| 1017 | env_->GetStringRegion(s, 10, 1, NULL); |
| 1018 | EXPECT_EXCEPTION(sioobe_); |
| 1019 | |
| 1020 | jchar chars[4] = { 'x', 'x', 'x', 'x' }; |
| 1021 | env_->GetStringRegion(s, 1, 2, &chars[1]); |
| 1022 | EXPECT_EQ('x', chars[0]); |
| 1023 | EXPECT_EQ('e', chars[1]); |
| 1024 | EXPECT_EQ('l', chars[2]); |
| 1025 | EXPECT_EQ('x', chars[3]); |
| 1026 | |
| 1027 | env_->GetStringUTFRegion(s, -1, 0, NULL); |
| 1028 | EXPECT_EXCEPTION(sioobe_); |
| 1029 | env_->GetStringUTFRegion(s, 0, -1, NULL); |
| 1030 | EXPECT_EXCEPTION(sioobe_); |
| 1031 | env_->GetStringUTFRegion(s, 0, 10, NULL); |
| 1032 | EXPECT_EXCEPTION(sioobe_); |
| 1033 | env_->GetStringUTFRegion(s, 10, 1, NULL); |
| 1034 | EXPECT_EXCEPTION(sioobe_); |
| 1035 | |
| 1036 | char bytes[4] = { 'x', 'x', 'x', 'x' }; |
| 1037 | env_->GetStringUTFRegion(s, 1, 2, &bytes[1]); |
| 1038 | EXPECT_EQ('x', bytes[0]); |
| 1039 | EXPECT_EQ('e', bytes[1]); |
| 1040 | EXPECT_EQ('l', bytes[2]); |
| 1041 | EXPECT_EQ('x', bytes[3]); |
| 1042 | } |
| 1043 | |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1044 | TEST_F(JniInternalTest, GetStringUTFChars_ReleaseStringUTFChars) { |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame] | 1045 | // Passing in a NULL jstring is ignored normally, but caught by -Xcheck:jni. |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1046 | { |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1047 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 1048 | EXPECT_TRUE(env_->GetStringUTFChars(NULL, NULL) == NULL); |
Elliott Hughes | 56ef042 | 2012-06-19 14:35:04 -0700 | [diff] [blame] | 1049 | check_jni_abort_catcher.Check("GetStringUTFChars received null jstring"); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1050 | } |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1051 | |
| 1052 | jstring s = env_->NewStringUTF("hello"); |
| 1053 | ASSERT_TRUE(s != NULL); |
| 1054 | |
| 1055 | const char* utf = env_->GetStringUTFChars(s, NULL); |
| 1056 | EXPECT_STREQ("hello", utf); |
| 1057 | env_->ReleaseStringUTFChars(s, utf); |
| 1058 | |
| 1059 | jboolean is_copy = JNI_FALSE; |
| 1060 | utf = env_->GetStringUTFChars(s, &is_copy); |
| 1061 | EXPECT_EQ(JNI_TRUE, is_copy); |
| 1062 | EXPECT_STREQ("hello", utf); |
| 1063 | env_->ReleaseStringUTFChars(s, utf); |
| 1064 | } |
| 1065 | |
| 1066 | TEST_F(JniInternalTest, GetStringChars_ReleaseStringChars) { |
| 1067 | jstring s = env_->NewStringUTF("hello"); |
| 1068 | ASSERT_TRUE(s != NULL); |
| 1069 | |
| 1070 | jchar expected[] = { 'h', 'e', 'l', 'l', 'o' }; |
| 1071 | const jchar* chars = env_->GetStringChars(s, NULL); |
| 1072 | EXPECT_EQ(expected[0], chars[0]); |
| 1073 | EXPECT_EQ(expected[1], chars[1]); |
| 1074 | EXPECT_EQ(expected[2], chars[2]); |
| 1075 | EXPECT_EQ(expected[3], chars[3]); |
| 1076 | EXPECT_EQ(expected[4], chars[4]); |
| 1077 | env_->ReleaseStringChars(s, chars); |
| 1078 | |
| 1079 | jboolean is_copy = JNI_FALSE; |
| 1080 | chars = env_->GetStringChars(s, &is_copy); |
| 1081 | EXPECT_EQ(JNI_FALSE, is_copy); |
| 1082 | EXPECT_EQ(expected[0], chars[0]); |
| 1083 | EXPECT_EQ(expected[1], chars[1]); |
| 1084 | EXPECT_EQ(expected[2], chars[2]); |
| 1085 | EXPECT_EQ(expected[3], chars[3]); |
| 1086 | EXPECT_EQ(expected[4], chars[4]); |
| 1087 | env_->ReleaseStringChars(s, chars); |
| 1088 | } |
| 1089 | |
| 1090 | TEST_F(JniInternalTest, GetStringCritical_ReleaseStringCritical) { |
| 1091 | jstring s = env_->NewStringUTF("hello"); |
| 1092 | ASSERT_TRUE(s != NULL); |
| 1093 | |
| 1094 | jchar expected[] = { 'h', 'e', 'l', 'l', 'o' }; |
| 1095 | const jchar* chars = env_->GetStringCritical(s, NULL); |
| 1096 | EXPECT_EQ(expected[0], chars[0]); |
| 1097 | EXPECT_EQ(expected[1], chars[1]); |
| 1098 | EXPECT_EQ(expected[2], chars[2]); |
| 1099 | EXPECT_EQ(expected[3], chars[3]); |
| 1100 | EXPECT_EQ(expected[4], chars[4]); |
| 1101 | env_->ReleaseStringCritical(s, chars); |
| 1102 | |
| 1103 | jboolean is_copy = JNI_FALSE; |
| 1104 | chars = env_->GetStringCritical(s, &is_copy); |
| 1105 | EXPECT_EQ(JNI_FALSE, is_copy); |
| 1106 | EXPECT_EQ(expected[0], chars[0]); |
| 1107 | EXPECT_EQ(expected[1], chars[1]); |
| 1108 | EXPECT_EQ(expected[2], chars[2]); |
| 1109 | EXPECT_EQ(expected[3], chars[3]); |
| 1110 | EXPECT_EQ(expected[4], chars[4]); |
| 1111 | env_->ReleaseStringCritical(s, chars); |
| 1112 | } |
| 1113 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1114 | TEST_F(JniInternalTest, GetObjectArrayElement_SetObjectArrayElement) { |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1115 | jclass java_lang_Class = env_->FindClass("java/lang/Class"); |
| 1116 | ASSERT_TRUE(java_lang_Class != NULL); |
Elliott Hughes | 289da82 | 2011-08-16 10:11:20 -0700 | [diff] [blame] | 1117 | |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1118 | jobjectArray array = env_->NewObjectArray(1, java_lang_Class, NULL); |
Elliott Hughes | 289da82 | 2011-08-16 10:11:20 -0700 | [diff] [blame] | 1119 | EXPECT_TRUE(array != NULL); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1120 | EXPECT_TRUE(env_->GetObjectArrayElement(array, 0) == NULL); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1121 | env_->SetObjectArrayElement(array, 0, java_lang_Class); |
| 1122 | EXPECT_TRUE(env_->IsSameObject(env_->GetObjectArrayElement(array, 0), java_lang_Class)); |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 1123 | |
| 1124 | // ArrayIndexOutOfBounds for negative index. |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1125 | env_->SetObjectArrayElement(array, -1, java_lang_Class); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1126 | EXPECT_EXCEPTION(aioobe_); |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 1127 | |
| 1128 | // ArrayIndexOutOfBounds for too-large index. |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1129 | env_->SetObjectArrayElement(array, 1, java_lang_Class); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1130 | EXPECT_EXCEPTION(aioobe_); |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 1131 | |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1132 | // ArrayStoreException thrown for bad types. |
| 1133 | env_->SetObjectArrayElement(array, 0, env_->NewStringUTF("not a jclass!")); |
| 1134 | EXPECT_EXCEPTION(ase_); |
Elliott Hughes | 289da82 | 2011-08-16 10:11:20 -0700 | [diff] [blame] | 1135 | } |
| 1136 | |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1137 | #define EXPECT_STATIC_PRIMITIVE_FIELD(type, field_name, sig, value1, value2) \ |
| 1138 | do { \ |
| 1139 | jfieldID fid = env_->GetStaticFieldID(c, field_name, sig); \ |
| 1140 | EXPECT_TRUE(fid != NULL); \ |
| 1141 | env_->SetStatic ## type ## Field(c, fid, value1); \ |
Brian Carlstrom | 2e3d1b2 | 2012-01-09 18:01:56 -0800 | [diff] [blame] | 1142 | EXPECT_TRUE(value1 == env_->GetStatic ## type ## Field(c, fid)); \ |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1143 | env_->SetStatic ## type ## Field(c, fid, value2); \ |
Brian Carlstrom | 2e3d1b2 | 2012-01-09 18:01:56 -0800 | [diff] [blame] | 1144 | EXPECT_TRUE(value2 == env_->GetStatic ## type ## Field(c, fid)); \ |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1145 | } while (false) |
| 1146 | |
| 1147 | #define EXPECT_PRIMITIVE_FIELD(instance, type, field_name, sig, value1, value2) \ |
| 1148 | do { \ |
| 1149 | jfieldID fid = env_->GetFieldID(c, field_name, sig); \ |
| 1150 | EXPECT_TRUE(fid != NULL); \ |
| 1151 | env_->Set ## type ## Field(instance, fid, value1); \ |
Brian Carlstrom | 2e3d1b2 | 2012-01-09 18:01:56 -0800 | [diff] [blame] | 1152 | EXPECT_TRUE(value1 == env_->Get ## type ## Field(instance, fid)); \ |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1153 | env_->Set ## type ## Field(instance, fid, value2); \ |
Brian Carlstrom | 2e3d1b2 | 2012-01-09 18:01:56 -0800 | [diff] [blame] | 1154 | EXPECT_TRUE(value2 == env_->Get ## type ## Field(instance, fid)); \ |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1155 | } while (false) |
| 1156 | |
| 1157 | |
Shih-wei Liao | 5b8b1ed | 2012-02-23 23:48:21 -0800 | [diff] [blame] | 1158 | #if !defined(ART_USE_LLVM_COMPILER) |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1159 | TEST_F(JniInternalTest, GetPrimitiveField_SetPrimitiveField) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1160 | Thread::Current()->TransitionFromSuspendedToRunnable(); |
| 1161 | LoadDex("AllFields"); |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 1162 | runtime_->Start(); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1163 | |
| 1164 | jclass c = env_->FindClass("AllFields"); |
| 1165 | ASSERT_TRUE(c != NULL); |
| 1166 | jobject o = env_->AllocObject(c); |
| 1167 | ASSERT_TRUE(o != NULL); |
| 1168 | |
| 1169 | EXPECT_STATIC_PRIMITIVE_FIELD(Boolean, "sZ", "Z", true, false); |
| 1170 | EXPECT_STATIC_PRIMITIVE_FIELD(Byte, "sB", "B", 1, 2); |
| 1171 | EXPECT_STATIC_PRIMITIVE_FIELD(Char, "sC", "C", 'a', 'b'); |
| 1172 | EXPECT_STATIC_PRIMITIVE_FIELD(Double, "sD", "D", 1.0, 2.0); |
| 1173 | EXPECT_STATIC_PRIMITIVE_FIELD(Float, "sF", "F", 1.0, 2.0); |
| 1174 | EXPECT_STATIC_PRIMITIVE_FIELD(Int, "sI", "I", 1, 2); |
| 1175 | EXPECT_STATIC_PRIMITIVE_FIELD(Long, "sJ", "J", 1, 2); |
| 1176 | EXPECT_STATIC_PRIMITIVE_FIELD(Short, "sS", "S", 1, 2); |
| 1177 | |
| 1178 | EXPECT_PRIMITIVE_FIELD(o, Boolean, "iZ", "Z", true, false); |
| 1179 | EXPECT_PRIMITIVE_FIELD(o, Byte, "iB", "B", 1, 2); |
| 1180 | EXPECT_PRIMITIVE_FIELD(o, Char, "iC", "C", 'a', 'b'); |
| 1181 | EXPECT_PRIMITIVE_FIELD(o, Double, "iD", "D", 1.0, 2.0); |
| 1182 | EXPECT_PRIMITIVE_FIELD(o, Float, "iF", "F", 1.0, 2.0); |
| 1183 | EXPECT_PRIMITIVE_FIELD(o, Int, "iI", "I", 1, 2); |
| 1184 | EXPECT_PRIMITIVE_FIELD(o, Long, "iJ", "J", 1, 2); |
| 1185 | EXPECT_PRIMITIVE_FIELD(o, Short, "iS", "S", 1, 2); |
| 1186 | } |
| 1187 | |
| 1188 | TEST_F(JniInternalTest, GetObjectField_SetObjectField) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1189 | Thread::Current()->TransitionFromSuspendedToRunnable(); |
| 1190 | LoadDex("AllFields"); |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 1191 | runtime_->Start(); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1192 | |
| 1193 | jclass c = env_->FindClass("AllFields"); |
| 1194 | ASSERT_TRUE(c != NULL); |
| 1195 | jobject o = env_->AllocObject(c); |
| 1196 | ASSERT_TRUE(o != NULL); |
| 1197 | |
| 1198 | jstring s1 = env_->NewStringUTF("hello"); |
| 1199 | ASSERT_TRUE(s1 != NULL); |
| 1200 | jstring s2 = env_->NewStringUTF("world"); |
| 1201 | ASSERT_TRUE(s2 != NULL); |
| 1202 | |
| 1203 | jfieldID s_fid = env_->GetStaticFieldID(c, "sObject", "Ljava/lang/Object;"); |
| 1204 | ASSERT_TRUE(s_fid != NULL); |
| 1205 | jfieldID i_fid = env_->GetFieldID(c, "iObject", "Ljava/lang/Object;"); |
| 1206 | ASSERT_TRUE(i_fid != NULL); |
| 1207 | |
| 1208 | env_->SetStaticObjectField(c, s_fid, s1); |
| 1209 | ASSERT_TRUE(env_->IsSameObject(s1, env_->GetStaticObjectField(c, s_fid))); |
| 1210 | env_->SetStaticObjectField(c, s_fid, s2); |
| 1211 | ASSERT_TRUE(env_->IsSameObject(s2, env_->GetStaticObjectField(c, s_fid))); |
| 1212 | |
| 1213 | env_->SetObjectField(o, i_fid, s1); |
| 1214 | ASSERT_TRUE(env_->IsSameObject(s1, env_->GetObjectField(o, i_fid))); |
| 1215 | env_->SetObjectField(o, i_fid, s2); |
| 1216 | ASSERT_TRUE(env_->IsSameObject(s2, env_->GetObjectField(o, i_fid))); |
| 1217 | } |
Shih-wei Liao | 5b8b1ed | 2012-02-23 23:48:21 -0800 | [diff] [blame] | 1218 | #endif |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1219 | |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1220 | TEST_F(JniInternalTest, NewLocalRef_NULL) { |
| 1221 | EXPECT_TRUE(env_->NewLocalRef(NULL) == NULL); |
| 1222 | } |
| 1223 | |
| 1224 | TEST_F(JniInternalTest, NewLocalRef) { |
| 1225 | jstring s = env_->NewStringUTF(""); |
| 1226 | ASSERT_TRUE(s != NULL); |
| 1227 | jobject o = env_->NewLocalRef(s); |
| 1228 | EXPECT_TRUE(o != NULL); |
| 1229 | EXPECT_TRUE(o != s); |
| 1230 | |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1231 | EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(o)); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1232 | } |
| 1233 | |
| 1234 | TEST_F(JniInternalTest, DeleteLocalRef_NULL) { |
| 1235 | env_->DeleteLocalRef(NULL); |
| 1236 | } |
| 1237 | |
| 1238 | TEST_F(JniInternalTest, DeleteLocalRef) { |
| 1239 | jstring s = env_->NewStringUTF(""); |
| 1240 | ASSERT_TRUE(s != NULL); |
| 1241 | env_->DeleteLocalRef(s); |
| 1242 | |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame] | 1243 | // Currently, deleting an already-deleted reference is just a CheckJNI warning. |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1244 | { |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1245 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 1246 | env_->DeleteLocalRef(s); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1247 | check_jni_abort_catcher.Check("native code passing in reference to invalid local reference: 0x1400001"); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1248 | } |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1249 | |
| 1250 | s = env_->NewStringUTF(""); |
| 1251 | ASSERT_TRUE(s != NULL); |
| 1252 | jobject o = env_->NewLocalRef(s); |
| 1253 | ASSERT_TRUE(o != NULL); |
| 1254 | |
| 1255 | env_->DeleteLocalRef(s); |
| 1256 | env_->DeleteLocalRef(o); |
| 1257 | } |
| 1258 | |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1259 | TEST_F(JniInternalTest, PushLocalFrame_PopLocalFrame) { |
| 1260 | jobject original = env_->NewStringUTF(""); |
| 1261 | ASSERT_TRUE(original != NULL); |
| 1262 | |
| 1263 | jobject outer; |
| 1264 | jobject inner1, inner2; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1265 | ScopedObjectAccess soa(env_); |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1266 | Object* inner2_direct_pointer; |
| 1267 | { |
| 1268 | env_->PushLocalFrame(4); |
| 1269 | outer = env_->NewLocalRef(original); |
| 1270 | |
| 1271 | { |
| 1272 | env_->PushLocalFrame(4); |
| 1273 | inner1 = env_->NewLocalRef(outer); |
| 1274 | inner2 = env_->NewStringUTF("survivor"); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1275 | inner2_direct_pointer = soa.Decode<Object*>(inner2); |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1276 | env_->PopLocalFrame(inner2); |
| 1277 | } |
| 1278 | |
| 1279 | EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(original)); |
| 1280 | EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(outer)); |
| 1281 | EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(inner1)); |
| 1282 | |
| 1283 | // Our local reference for the survivor is invalid because the survivor |
| 1284 | // gets a new local reference... |
| 1285 | EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(inner2)); |
| 1286 | // ...but the survivor should be in the local reference table. |
Brian Carlstrom | 4d57143 | 2012-05-16 00:21:41 -0700 | [diff] [blame] | 1287 | JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(env_); |
| 1288 | EXPECT_TRUE(env->locals.ContainsDirectPointer(inner2_direct_pointer)); |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1289 | |
| 1290 | env_->PopLocalFrame(NULL); |
| 1291 | } |
| 1292 | EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(original)); |
| 1293 | EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(outer)); |
| 1294 | EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(inner1)); |
| 1295 | EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(inner2)); |
| 1296 | } |
| 1297 | |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1298 | TEST_F(JniInternalTest, NewGlobalRef_NULL) { |
| 1299 | EXPECT_TRUE(env_->NewGlobalRef(NULL) == NULL); |
| 1300 | } |
| 1301 | |
| 1302 | TEST_F(JniInternalTest, NewGlobalRef) { |
| 1303 | jstring s = env_->NewStringUTF(""); |
| 1304 | ASSERT_TRUE(s != NULL); |
| 1305 | jobject o = env_->NewGlobalRef(s); |
| 1306 | EXPECT_TRUE(o != NULL); |
| 1307 | EXPECT_TRUE(o != s); |
| 1308 | |
| 1309 | // TODO: check that o is a global reference. |
| 1310 | } |
| 1311 | |
| 1312 | TEST_F(JniInternalTest, DeleteGlobalRef_NULL) { |
| 1313 | env_->DeleteGlobalRef(NULL); |
| 1314 | } |
| 1315 | |
| 1316 | TEST_F(JniInternalTest, DeleteGlobalRef) { |
| 1317 | jstring s = env_->NewStringUTF(""); |
| 1318 | ASSERT_TRUE(s != NULL); |
| 1319 | |
| 1320 | jobject o = env_->NewGlobalRef(s); |
| 1321 | ASSERT_TRUE(o != NULL); |
| 1322 | env_->DeleteGlobalRef(o); |
| 1323 | |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame] | 1324 | // Currently, deleting an already-deleted reference is just a CheckJNI warning. |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1325 | { |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1326 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 1327 | env_->DeleteGlobalRef(o); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1328 | check_jni_abort_catcher.Check("native code passing in reference to invalid global reference: 0x100056"); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1329 | } |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1330 | |
| 1331 | jobject o1 = env_->NewGlobalRef(s); |
| 1332 | ASSERT_TRUE(o1 != NULL); |
| 1333 | jobject o2 = env_->NewGlobalRef(s); |
| 1334 | ASSERT_TRUE(o2 != NULL); |
| 1335 | |
| 1336 | env_->DeleteGlobalRef(o1); |
| 1337 | env_->DeleteGlobalRef(o2); |
| 1338 | } |
| 1339 | |
| 1340 | TEST_F(JniInternalTest, NewWeakGlobalRef_NULL) { |
| 1341 | EXPECT_TRUE(env_->NewWeakGlobalRef(NULL) == NULL); |
| 1342 | } |
| 1343 | |
| 1344 | TEST_F(JniInternalTest, NewWeakGlobalRef) { |
| 1345 | jstring s = env_->NewStringUTF(""); |
| 1346 | ASSERT_TRUE(s != NULL); |
| 1347 | jobject o = env_->NewWeakGlobalRef(s); |
| 1348 | EXPECT_TRUE(o != NULL); |
| 1349 | EXPECT_TRUE(o != s); |
| 1350 | |
| 1351 | // TODO: check that o is a weak global reference. |
| 1352 | } |
| 1353 | |
| 1354 | TEST_F(JniInternalTest, DeleteWeakGlobalRef_NULL) { |
| 1355 | env_->DeleteWeakGlobalRef(NULL); |
| 1356 | } |
| 1357 | |
| 1358 | TEST_F(JniInternalTest, DeleteWeakGlobalRef) { |
| 1359 | jstring s = env_->NewStringUTF(""); |
| 1360 | ASSERT_TRUE(s != NULL); |
| 1361 | |
| 1362 | jobject o = env_->NewWeakGlobalRef(s); |
| 1363 | ASSERT_TRUE(o != NULL); |
| 1364 | env_->DeleteWeakGlobalRef(o); |
| 1365 | |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame] | 1366 | // Currently, deleting an already-deleted reference is just a CheckJNI warning. |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1367 | { |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1368 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 1369 | env_->DeleteWeakGlobalRef(o); |
Elliott Hughes | 56ef042 | 2012-06-19 14:35:04 -0700 | [diff] [blame] | 1370 | check_jni_abort_catcher.Check("native code passing in reference to invalid weak global reference: 0x100003"); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1371 | } |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1372 | |
| 1373 | jobject o1 = env_->NewWeakGlobalRef(s); |
| 1374 | ASSERT_TRUE(o1 != NULL); |
| 1375 | jobject o2 = env_->NewWeakGlobalRef(s); |
| 1376 | ASSERT_TRUE(o2 != NULL); |
| 1377 | |
| 1378 | env_->DeleteWeakGlobalRef(o1); |
| 1379 | env_->DeleteWeakGlobalRef(o2); |
| 1380 | } |
| 1381 | |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1382 | TEST_F(JniInternalTest, StaticMainMethod) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1383 | ScopedObjectAccess soa(Thread::Current()); |
| 1384 | jobject jclass_loader = LoadDex("Main"); |
| 1385 | SirtRef<ClassLoader> class_loader(soa.Decode<ClassLoader*>(jclass_loader)); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1386 | CompileDirectMethod(class_loader.get(), "Main", "main", "([Ljava/lang/String;)V"); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1387 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1388 | Class* klass = class_linker_->FindClass("LMain;", class_loader.get()); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1389 | ASSERT_TRUE(klass != NULL); |
| 1390 | |
| 1391 | Method* method = klass->FindDirectMethod("main", "([Ljava/lang/String;)V"); |
| 1392 | ASSERT_TRUE(method != NULL); |
| 1393 | |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 1394 | Method::InvokeStub* stub = method->GetInvokeStub(); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1395 | |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1396 | JValue args[1]; |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 1397 | args[0].SetL(NULL); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1398 | |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1399 | (*stub)(method, NULL, Thread::Current(), args, NULL); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1400 | } |
| 1401 | |
| 1402 | TEST_F(JniInternalTest, StaticNopMethod) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1403 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1404 | InvokeNopMethod(true); |
| 1405 | } |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1406 | |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1407 | TEST_F(JniInternalTest, NonStaticNopMethod) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1408 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1409 | InvokeNopMethod(false); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1410 | } |
| 1411 | |
| 1412 | TEST_F(JniInternalTest, StaticIdentityByteMethod) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1413 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1414 | InvokeIdentityByteMethod(true); |
| 1415 | } |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1416 | |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1417 | TEST_F(JniInternalTest, NonStaticIdentityByteMethod) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1418 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1419 | InvokeIdentityByteMethod(false); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1420 | } |
| 1421 | |
| 1422 | TEST_F(JniInternalTest, StaticIdentityIntMethod) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1423 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1424 | InvokeIdentityIntMethod(true); |
| 1425 | } |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1426 | |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1427 | TEST_F(JniInternalTest, NonStaticIdentityIntMethod) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1428 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1429 | InvokeIdentityIntMethod(false); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1430 | } |
| 1431 | |
| 1432 | TEST_F(JniInternalTest, StaticIdentityDoubleMethod) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1433 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1434 | InvokeIdentityDoubleMethod(true); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1435 | } |
| 1436 | |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1437 | TEST_F(JniInternalTest, NonStaticIdentityDoubleMethod) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1438 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1439 | InvokeIdentityDoubleMethod(false); |
Shih-wei Liao | 5b8b1ed | 2012-02-23 23:48:21 -0800 | [diff] [blame] | 1440 | } |
| 1441 | |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1442 | TEST_F(JniInternalTest, StaticSumIntIntMethod) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1443 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1444 | InvokeSumIntIntMethod(true); |
| 1445 | } |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1446 | |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1447 | TEST_F(JniInternalTest, NonStaticSumIntIntMethod) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1448 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1449 | InvokeSumIntIntMethod(false); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1450 | } |
| 1451 | |
| 1452 | TEST_F(JniInternalTest, StaticSumIntIntIntMethod) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1453 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1454 | InvokeSumIntIntIntMethod(true); |
| 1455 | } |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1456 | |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1457 | TEST_F(JniInternalTest, NonStaticSumIntIntIntMethod) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1458 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1459 | InvokeSumIntIntIntMethod(false); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1460 | } |
| 1461 | |
| 1462 | TEST_F(JniInternalTest, StaticSumIntIntIntIntMethod) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1463 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1464 | InvokeSumIntIntIntIntMethod(true); |
| 1465 | } |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1466 | |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1467 | TEST_F(JniInternalTest, NonStaticSumIntIntIntIntMethod) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1468 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1469 | InvokeSumIntIntIntIntMethod(false); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1470 | } |
| 1471 | |
| 1472 | TEST_F(JniInternalTest, StaticSumIntIntIntIntIntMethod) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1473 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1474 | InvokeSumIntIntIntIntIntMethod(true); |
| 1475 | } |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1476 | |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1477 | TEST_F(JniInternalTest, NonStaticSumIntIntIntIntIntMethod) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1478 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1479 | InvokeSumIntIntIntIntIntMethod(false); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1480 | } |
| 1481 | |
| 1482 | TEST_F(JniInternalTest, StaticSumDoubleDoubleMethod) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1483 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1484 | InvokeSumDoubleDoubleMethod(true); |
| 1485 | } |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1486 | |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1487 | TEST_F(JniInternalTest, NonStaticSumDoubleDoubleMethod) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1488 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1489 | InvokeSumDoubleDoubleMethod(false); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1490 | } |
| 1491 | |
| 1492 | TEST_F(JniInternalTest, StaticSumDoubleDoubleDoubleMethod) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1493 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1494 | InvokeSumDoubleDoubleDoubleMethod(true); |
| 1495 | } |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1496 | |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1497 | TEST_F(JniInternalTest, NonStaticSumDoubleDoubleDoubleMethod) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1498 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1499 | InvokeSumDoubleDoubleDoubleMethod(false); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1500 | } |
| 1501 | |
| 1502 | TEST_F(JniInternalTest, StaticSumDoubleDoubleDoubleDoubleMethod) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1503 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1504 | InvokeSumDoubleDoubleDoubleDoubleMethod(true); |
| 1505 | } |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1506 | |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1507 | TEST_F(JniInternalTest, NonStaticSumDoubleDoubleDoubleDoubleMethod) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1508 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1509 | InvokeSumDoubleDoubleDoubleDoubleMethod(false); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1510 | } |
| 1511 | |
| 1512 | TEST_F(JniInternalTest, StaticSumDoubleDoubleDoubleDoubleDoubleMethod) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1513 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1514 | InvokeSumDoubleDoubleDoubleDoubleDoubleMethod(true); |
| 1515 | } |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1516 | |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1517 | TEST_F(JniInternalTest, NonStaticSumDoubleDoubleDoubleDoubleDoubleMethod) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1518 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1519 | InvokeSumDoubleDoubleDoubleDoubleDoubleMethod(false); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1520 | } |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1521 | |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1522 | TEST_F(JniInternalTest, Throw) { |
| 1523 | EXPECT_EQ(JNI_ERR, env_->Throw(NULL)); |
| 1524 | |
| 1525 | jclass exception_class = env_->FindClass("java/lang/RuntimeException"); |
| 1526 | ASSERT_TRUE(exception_class != NULL); |
| 1527 | jthrowable exception = reinterpret_cast<jthrowable>(env_->AllocObject(exception_class)); |
| 1528 | ASSERT_TRUE(exception != NULL); |
| 1529 | |
| 1530 | EXPECT_EQ(JNI_OK, env_->Throw(exception)); |
| 1531 | EXPECT_TRUE(env_->ExceptionCheck()); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1532 | jthrowable thrown_exception = env_->ExceptionOccurred(); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1533 | env_->ExceptionClear(); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1534 | EXPECT_TRUE(env_->IsSameObject(exception, thrown_exception)); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1535 | } |
| 1536 | |
| 1537 | TEST_F(JniInternalTest, ThrowNew) { |
| 1538 | EXPECT_EQ(JNI_ERR, env_->Throw(NULL)); |
| 1539 | |
| 1540 | jclass exception_class = env_->FindClass("java/lang/RuntimeException"); |
| 1541 | ASSERT_TRUE(exception_class != NULL); |
| 1542 | |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 1543 | jthrowable thrown_exception; |
| 1544 | |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1545 | EXPECT_EQ(JNI_OK, env_->ThrowNew(exception_class, "hello world")); |
| 1546 | EXPECT_TRUE(env_->ExceptionCheck()); |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 1547 | thrown_exception = env_->ExceptionOccurred(); |
| 1548 | env_->ExceptionClear(); |
| 1549 | EXPECT_TRUE(env_->IsInstanceOf(thrown_exception, exception_class)); |
| 1550 | |
| 1551 | EXPECT_EQ(JNI_OK, env_->ThrowNew(exception_class, NULL)); |
| 1552 | EXPECT_TRUE(env_->ExceptionCheck()); |
| 1553 | thrown_exception = env_->ExceptionOccurred(); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1554 | env_->ExceptionClear(); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1555 | EXPECT_TRUE(env_->IsInstanceOf(thrown_exception, exception_class)); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1556 | } |
| 1557 | |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1558 | // TODO: this test is DISABLED until we can actually run java.nio.Buffer's <init>. |
| 1559 | TEST_F(JniInternalTest, DISABLED_NewDirectBuffer_GetDirectBufferAddress_GetDirectBufferCapacity) { |
| 1560 | jclass buffer_class = env_->FindClass("java/nio/Buffer"); |
| 1561 | ASSERT_TRUE(buffer_class != NULL); |
| 1562 | |
| 1563 | char bytes[1024]; |
| 1564 | jobject buffer = env_->NewDirectByteBuffer(bytes, sizeof(bytes)); |
| 1565 | ASSERT_TRUE(buffer != NULL); |
| 1566 | ASSERT_TRUE(env_->IsInstanceOf(buffer, buffer_class)); |
| 1567 | ASSERT_TRUE(env_->GetDirectBufferAddress(buffer) == bytes); |
| 1568 | ASSERT_TRUE(env_->GetDirectBufferCapacity(buffer) == sizeof(bytes)); |
| 1569 | } |
| 1570 | |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 1571 | TEST_F(JniInternalTest, MonitorEnterExit) { |
| 1572 | // Create an object to torture |
| 1573 | jclass object_class = env_->FindClass("java/lang/Object"); |
| 1574 | ASSERT_TRUE(object_class != NULL); |
| 1575 | jobject object = env_->AllocObject(object_class); |
| 1576 | ASSERT_TRUE(object != NULL); |
| 1577 | |
| 1578 | // Expected class of exceptions |
| 1579 | jclass imse_class = env_->FindClass("java/lang/IllegalMonitorStateException"); |
| 1580 | ASSERT_TRUE(imse_class != NULL); |
| 1581 | |
| 1582 | jthrowable thrown_exception; |
| 1583 | |
| 1584 | // Unlock of unowned monitor |
| 1585 | env_->MonitorExit(object); |
| 1586 | EXPECT_TRUE(env_->ExceptionCheck()); |
| 1587 | thrown_exception = env_->ExceptionOccurred(); |
| 1588 | env_->ExceptionClear(); |
| 1589 | EXPECT_TRUE(env_->IsInstanceOf(thrown_exception, imse_class)); |
| 1590 | |
| 1591 | // Lock of unowned monitor |
| 1592 | env_->MonitorEnter(object); |
| 1593 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 1594 | // Regular unlock |
| 1595 | env_->MonitorExit(object); |
| 1596 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 1597 | |
| 1598 | // Recursively lock a lot |
| 1599 | size_t max_recursive_lock = 1024; |
| 1600 | for (size_t i = 0; i < max_recursive_lock; i++) { |
| 1601 | env_->MonitorEnter(object); |
| 1602 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 1603 | } |
| 1604 | // Recursively unlock a lot |
| 1605 | for (size_t i = 0; i < max_recursive_lock; i++) { |
| 1606 | env_->MonitorExit(object); |
| 1607 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 1608 | } |
| 1609 | |
| 1610 | // Unlock of unowned monitor |
| 1611 | env_->MonitorExit(object); |
| 1612 | EXPECT_TRUE(env_->ExceptionCheck()); |
| 1613 | thrown_exception = env_->ExceptionOccurred(); |
| 1614 | env_->ExceptionClear(); |
| 1615 | EXPECT_TRUE(env_->IsInstanceOf(thrown_exception, imse_class)); |
Elliott Hughes | a92853e | 2012-02-07 16:09:27 -0800 | [diff] [blame] | 1616 | |
| 1617 | // It's an error to call MonitorEnter or MonitorExit on NULL. |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1618 | { |
| 1619 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 1620 | env_->MonitorEnter(NULL); |
| 1621 | check_jni_abort_catcher.Check("in call to MonitorEnter"); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1622 | |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1623 | env_->MonitorExit(NULL); |
| 1624 | check_jni_abort_catcher.Check("in call to MonitorExit"); |
| 1625 | } |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 1626 | } |
| 1627 | |
Brian Carlstrom | 4d57143 | 2012-05-16 00:21:41 -0700 | [diff] [blame] | 1628 | TEST_F(JniInternalTest, DetachCurrentThread) { |
| 1629 | CleanUpJniEnv(); // cleanup now so TearDown won't have junk from wrong JNIEnv |
| 1630 | jint ok = vm_->DetachCurrentThread(); |
| 1631 | EXPECT_EQ(JNI_OK, ok); |
| 1632 | |
| 1633 | jint err = vm_->DetachCurrentThread(); |
| 1634 | EXPECT_EQ(JNI_ERR, err); |
| 1635 | vm_->AttachCurrentThread(&env_, NULL); // need attached thread for CommonTest::TearDown |
| 1636 | } |
| 1637 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1638 | } // namespace art |