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