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