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