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