blob: c1284a639f9c83d8e5749118eaac64cb68e90d0d [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 */
Carl Shapiro894d0fa2011-06-30 14:48:49 -070016
Elliott Hughes90a33692011-08-30 13:27:07 -070017#include "object.h"
18
19#include <stdint.h>
20#include <stdio.h>
Ian Rogers700a4022014-05-19 16:49:03 -070021#include <memory>
Elliott Hughes90a33692011-08-30 13:27:07 -070022
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080023#include "array-inl.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070024#include "art_field-inl.h"
Ian Rogers1ff3c982014-08-12 02:30:58 -070025#include "art_method-inl.h"
Ian Rogers1f539342012-10-03 21:09:42 -070026#include "asm_support.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027#include "class-inl.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070028#include "class_linker.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029#include "class_linker-inl.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080030#include "common_runtime_test.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070031#include "dex_file.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070032#include "entrypoints/entrypoint_utils-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070033#include "gc/accounting/card_table-inl.h"
34#include "gc/heap.h"
Ian Rogers1ff3c982014-08-12 02:30:58 -070035#include "handle_scope-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036#include "iftable-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037#include "object-inl.h"
38#include "object_array-inl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070039#include "scoped_thread_state_change.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070040#include "string-inl.h"
Carl Shapiro894d0fa2011-06-30 14:48:49 -070041
42namespace art {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080043namespace mirror {
Carl Shapiro894d0fa2011-06-30 14:48:49 -070044
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080045class ObjectTest : public CommonRuntimeTest {
Brian Carlstrom0b138b22011-07-27 15:19:17 -070046 protected:
Brian Carlstrom0a02d122013-09-06 15:42:40 -070047 void AssertString(int32_t expected_utf16_length,
Brian Carlstrom0b138b22011-07-27 15:19:17 -070048 const char* utf8_in,
49 const char* utf16_expected_le,
Ian Rogers00f7d0e2012-07-19 15:28:27 -070050 int32_t expected_hash)
Mathieu Chartier90443472015-07-16 20:32:27 -070051 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers700a4022014-05-19 16:49:03 -070052 std::unique_ptr<uint16_t[]> utf16_expected(new uint16_t[expected_utf16_length]);
Brian Carlstrom0a02d122013-09-06 15:42:40 -070053 for (int32_t i = 0; i < expected_utf16_length; i++) {
Brian Carlstrom0b138b22011-07-27 15:19:17 -070054 uint16_t ch = (((utf16_expected_le[i*2 + 0] & 0xff) << 8) |
55 ((utf16_expected_le[i*2 + 1] & 0xff) << 0));
56 utf16_expected[i] = ch;
57 }
58
Ian Rogers50b35e22012-10-04 10:09:15 -070059 Thread* self = Thread::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070060 StackHandleScope<1> hs(self);
61 Handle<String> string(
62 hs.NewHandle(String::AllocFromModifiedUtf8(self, expected_utf16_length, utf8_in)));
Brian Carlstrom0a02d122013-09-06 15:42:40 -070063 ASSERT_EQ(expected_utf16_length, string->GetLength());
Jeff Hao848f70a2014-01-15 13:49:50 -080064 ASSERT_TRUE(string->GetValue() != nullptr);
Brian Carlstrom0a02d122013-09-06 15:42:40 -070065 // strlen is necessary because the 1-character string "\x00\x00" is interpreted as ""
66 ASSERT_TRUE(string->Equals(utf8_in) || (expected_utf16_length == 1 && strlen(utf8_in) == 0));
Mathieu Chartier2cebb242015-04-21 16:50:40 -070067 ASSERT_TRUE(string->Equals(StringPiece(utf8_in)) ||
68 (expected_utf16_length == 1 && strlen(utf8_in) == 0));
Brian Carlstrom0a02d122013-09-06 15:42:40 -070069 for (int32_t i = 0; i < expected_utf16_length; i++) {
Jeff Hao848f70a2014-01-15 13:49:50 -080070 EXPECT_EQ(utf16_expected[i], string->CharAt(i));
Brian Carlstrom0b138b22011-07-27 15:19:17 -070071 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070072 EXPECT_EQ(expected_hash, string->GetHashCode());
Brian Carlstrom0b138b22011-07-27 15:19:17 -070073 }
74};
Brian Carlstroma331b3c2011-07-18 17:47:56 -070075
Ian Rogers68d8b422014-07-17 11:09:10 -070076// Keep constants in sync.
77TEST_F(ObjectTest, Constants) {
Ian Rogerscdc1aaf2014-10-09 13:21:38 -070078 EXPECT_EQ(kObjectReferenceSize, sizeof(HeapReference<Object>));
79 EXPECT_EQ(kObjectHeaderSize, sizeof(Object));
Mathieu Chartiere401d142015-04-22 13:56:20 -070080 EXPECT_EQ(ART_METHOD_QUICK_CODE_OFFSET_32,
Mathieu Chartier2d721012014-11-10 11:08:06 -080081 ArtMethod::EntryPointFromQuickCompiledCodeOffset(4).Int32Value());
Mathieu Chartiere401d142015-04-22 13:56:20 -070082 EXPECT_EQ(ART_METHOD_QUICK_CODE_OFFSET_64,
Mathieu Chartier2d721012014-11-10 11:08:06 -080083 ArtMethod::EntryPointFromQuickCompiledCodeOffset(8).Int32Value());
Ian Rogers68d8b422014-07-17 11:09:10 -070084}
85
Brian Carlstroma331b3c2011-07-18 17:47:56 -070086TEST_F(ObjectTest, IsInSamePackage) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -070087 // Matches
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080088 EXPECT_TRUE(Class::IsInSamePackage("Ljava/lang/Object;", "Ljava/lang/Class;"));
89 EXPECT_TRUE(Class::IsInSamePackage("LFoo;", "LBar;"));
Carl Shapiro894d0fa2011-06-30 14:48:49 -070090
91 // Mismatches
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080092 EXPECT_FALSE(Class::IsInSamePackage("Ljava/lang/Object;", "Ljava/io/File;"));
93 EXPECT_FALSE(Class::IsInSamePackage("Ljava/lang/Object;", "Ljava/lang/reflect/Method;"));
Carl Shapiro894d0fa2011-06-30 14:48:49 -070094}
95
Elliott Hughes081be7f2011-09-18 16:50:26 -070096TEST_F(ObjectTest, Clone) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070097 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierf8322842014-05-16 10:59:25 -070098 StackHandleScope<2> hs(soa.Self());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070099 Handle<ObjectArray<Object>> a1(
100 hs.NewHandle(class_linker_->AllocObjectArray<Object>(soa.Self(), 256)));
Elliott Hughes081be7f2011-09-18 16:50:26 -0700101 size_t s1 = a1->SizeOf();
Ian Rogers50b35e22012-10-04 10:09:15 -0700102 Object* clone = a1->Clone(soa.Self());
Elliott Hughes081be7f2011-09-18 16:50:26 -0700103 EXPECT_EQ(s1, clone->SizeOf());
104 EXPECT_TRUE(clone->GetClass() == a1->GetClass());
105}
106
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700107TEST_F(ObjectTest, AllocObjectArray) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700108 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierf8322842014-05-16 10:59:25 -0700109 StackHandleScope<2> hs(soa.Self());
Ian Rogers700a4022014-05-19 16:49:03 -0700110 Handle<ObjectArray<Object>> oa(
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700111 hs.NewHandle(class_linker_->AllocObjectArray<Object>(soa.Self(), 2)));
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700112 EXPECT_EQ(2, oa->GetLength());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700113 EXPECT_TRUE(oa->Get(0) == nullptr);
114 EXPECT_TRUE(oa->Get(1) == nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700115 oa->Set<false>(0, oa.Get());
116 EXPECT_TRUE(oa->Get(0) == oa.Get());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700117 EXPECT_TRUE(oa->Get(1) == nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700118 oa->Set<false>(1, oa.Get());
119 EXPECT_TRUE(oa->Get(0) == oa.Get());
120 EXPECT_TRUE(oa->Get(1) == oa.Get());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700121
Ian Rogers98379392014-02-24 16:53:16 -0800122 Class* aioobe = class_linker_->FindSystemClass(soa.Self(),
123 "Ljava/lang/ArrayIndexOutOfBoundsException;");
Elliott Hughes710a0cb2011-08-16 14:32:37 -0700124
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700125 EXPECT_TRUE(oa->Get(-1) == nullptr);
Ian Rogers1f539342012-10-03 21:09:42 -0700126 EXPECT_TRUE(soa.Self()->IsExceptionPending());
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000127 EXPECT_EQ(aioobe, soa.Self()->GetException()->GetClass());
Ian Rogers1f539342012-10-03 21:09:42 -0700128 soa.Self()->ClearException();
Elliott Hughes710a0cb2011-08-16 14:32:37 -0700129
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700130 EXPECT_TRUE(oa->Get(2) == nullptr);
Ian Rogers1f539342012-10-03 21:09:42 -0700131 EXPECT_TRUE(soa.Self()->IsExceptionPending());
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000132 EXPECT_EQ(aioobe, soa.Self()->GetException()->GetClass());
Ian Rogers1f539342012-10-03 21:09:42 -0700133 soa.Self()->ClearException();
Elliott Hughes710a0cb2011-08-16 14:32:37 -0700134
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700135 ASSERT_TRUE(oa->GetClass() != nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700136 Handle<mirror::Class> klass(hs.NewHandle(oa->GetClass()));
137 ASSERT_EQ(2U, klass->NumDirectInterfaces());
Ian Rogers98379392014-02-24 16:53:16 -0800138 EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Cloneable;"),
Mathieu Chartierf8322842014-05-16 10:59:25 -0700139 mirror::Class::GetDirectInterface(soa.Self(), klass, 0));
Ian Rogers98379392014-02-24 16:53:16 -0800140 EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "Ljava/io/Serializable;"),
Mathieu Chartierf8322842014-05-16 10:59:25 -0700141 mirror::Class::GetDirectInterface(soa.Self(), klass, 1));
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700142}
143
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700144TEST_F(ObjectTest, AllocArray) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700145 ScopedObjectAccess soa(Thread::Current());
Ian Rogers98379392014-02-24 16:53:16 -0800146 Class* c = class_linker_->FindSystemClass(soa.Self(), "[I");
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700147 StackHandleScope<1> hs(soa.Self());
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700148 MutableHandle<Array> a(
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700149 hs.NewHandle(Array::Alloc<true>(soa.Self(), c, 1, c->GetComponentSizeShift(),
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700150 Runtime::Current()->GetHeap()->GetCurrentAllocator())));
Ian Rogers6fac4472014-02-25 17:01:10 -0800151 EXPECT_TRUE(c == a->GetClass());
152 EXPECT_EQ(1, a->GetLength());
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700153
Ian Rogers98379392014-02-24 16:53:16 -0800154 c = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/Object;");
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700155 a.Assign(Array::Alloc<true>(soa.Self(), c, 1, c->GetComponentSizeShift(),
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700156 Runtime::Current()->GetHeap()->GetCurrentAllocator()));
Ian Rogers6fac4472014-02-25 17:01:10 -0800157 EXPECT_TRUE(c == a->GetClass());
158 EXPECT_EQ(1, a->GetLength());
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700159
Ian Rogers98379392014-02-24 16:53:16 -0800160 c = class_linker_->FindSystemClass(soa.Self(), "[[Ljava/lang/Object;");
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700161 a.Assign(Array::Alloc<true>(soa.Self(), c, 1, c->GetComponentSizeShift(),
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700162 Runtime::Current()->GetHeap()->GetCurrentAllocator()));
Ian Rogers6fac4472014-02-25 17:01:10 -0800163 EXPECT_TRUE(c == a->GetClass());
164 EXPECT_EQ(1, a->GetLength());
165}
166
167TEST_F(ObjectTest, AllocArray_FillUsable) {
168 ScopedObjectAccess soa(Thread::Current());
169 Class* c = class_linker_->FindSystemClass(soa.Self(), "[B");
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700170 StackHandleScope<1> hs(soa.Self());
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700171 MutableHandle<Array> a(
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700172 hs.NewHandle(Array::Alloc<true, true>(soa.Self(), c, 1, c->GetComponentSizeShift(),
173 Runtime::Current()->GetHeap()->GetCurrentAllocator())));
Ian Rogers6fac4472014-02-25 17:01:10 -0800174 EXPECT_TRUE(c == a->GetClass());
175 EXPECT_LE(1, a->GetLength());
176
177 c = class_linker_->FindSystemClass(soa.Self(), "[I");
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700178 a.Assign(Array::Alloc<true, true>(soa.Self(), c, 2, c->GetComponentSizeShift(),
179 Runtime::Current()->GetHeap()->GetCurrentAllocator()));
Ian Rogers6fac4472014-02-25 17:01:10 -0800180 EXPECT_TRUE(c == a->GetClass());
181 EXPECT_LE(2, a->GetLength());
182
183 c = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/Object;");
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700184 a.Assign(Array::Alloc<true, true>(soa.Self(), c, 2, c->GetComponentSizeShift(),
185 Runtime::Current()->GetHeap()->GetCurrentAllocator()));
Ian Rogers6fac4472014-02-25 17:01:10 -0800186 EXPECT_TRUE(c == a->GetClass());
187 EXPECT_LE(2, a->GetLength());
188
189 c = class_linker_->FindSystemClass(soa.Self(), "[[Ljava/lang/Object;");
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700190 a.Assign(Array::Alloc<true, true>(soa.Self(), c, 2, c->GetComponentSizeShift(),
191 Runtime::Current()->GetHeap()->GetCurrentAllocator()));
Ian Rogers6fac4472014-02-25 17:01:10 -0800192 EXPECT_TRUE(c == a->GetClass());
193 EXPECT_LE(2, a->GetLength());
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700194}
195
Elliott Hughes710a0cb2011-08-16 14:32:37 -0700196template<typename ArrayT>
197void TestPrimitiveArray(ClassLinker* cl) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700198 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes710a0cb2011-08-16 14:32:37 -0700199 typedef typename ArrayT::ElementType T;
200
Ian Rogers50b35e22012-10-04 10:09:15 -0700201 ArrayT* a = ArrayT::Alloc(soa.Self(), 2);
Elliott Hughes710a0cb2011-08-16 14:32:37 -0700202 EXPECT_EQ(2, a->GetLength());
203 EXPECT_EQ(0, a->Get(0));
204 EXPECT_EQ(0, a->Get(1));
205 a->Set(0, T(123));
206 EXPECT_EQ(T(123), a->Get(0));
207 EXPECT_EQ(0, a->Get(1));
208 a->Set(1, T(321));
209 EXPECT_EQ(T(123), a->Get(0));
210 EXPECT_EQ(T(321), a->Get(1));
211
Ian Rogers98379392014-02-24 16:53:16 -0800212 Class* aioobe = cl->FindSystemClass(soa.Self(), "Ljava/lang/ArrayIndexOutOfBoundsException;");
Elliott Hughes710a0cb2011-08-16 14:32:37 -0700213
214 EXPECT_EQ(0, a->Get(-1));
Ian Rogers1f539342012-10-03 21:09:42 -0700215 EXPECT_TRUE(soa.Self()->IsExceptionPending());
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000216 EXPECT_EQ(aioobe, soa.Self()->GetException()->GetClass());
Ian Rogers1f539342012-10-03 21:09:42 -0700217 soa.Self()->ClearException();
Elliott Hughes710a0cb2011-08-16 14:32:37 -0700218
219 EXPECT_EQ(0, a->Get(2));
Ian Rogers1f539342012-10-03 21:09:42 -0700220 EXPECT_TRUE(soa.Self()->IsExceptionPending());
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000221 EXPECT_EQ(aioobe, soa.Self()->GetException()->GetClass());
Ian Rogers1f539342012-10-03 21:09:42 -0700222 soa.Self()->ClearException();
Elliott Hughes710a0cb2011-08-16 14:32:37 -0700223}
224
225TEST_F(ObjectTest, PrimitiveArray_Boolean_Alloc) {
226 TestPrimitiveArray<BooleanArray>(class_linker_);
227}
228TEST_F(ObjectTest, PrimitiveArray_Byte_Alloc) {
229 TestPrimitiveArray<ByteArray>(class_linker_);
230}
231TEST_F(ObjectTest, PrimitiveArray_Char_Alloc) {
232 TestPrimitiveArray<CharArray>(class_linker_);
233}
Elliott Hughes710a0cb2011-08-16 14:32:37 -0700234TEST_F(ObjectTest, PrimitiveArray_Int_Alloc) {
235 TestPrimitiveArray<IntArray>(class_linker_);
236}
237TEST_F(ObjectTest, PrimitiveArray_Long_Alloc) {
238 TestPrimitiveArray<LongArray>(class_linker_);
239}
240TEST_F(ObjectTest, PrimitiveArray_Short_Alloc) {
241 TestPrimitiveArray<ShortArray>(class_linker_);
242}
243
Ian Rogers647b1a82014-10-10 11:02:11 -0700244TEST_F(ObjectTest, PrimitiveArray_Double_Alloc) {
245 typedef DoubleArray ArrayT;
246 ScopedObjectAccess soa(Thread::Current());
247 typedef typename ArrayT::ElementType T;
248
249 ArrayT* a = ArrayT::Alloc(soa.Self(), 2);
250 EXPECT_EQ(2, a->GetLength());
251 EXPECT_DOUBLE_EQ(0, a->Get(0));
252 EXPECT_DOUBLE_EQ(0, a->Get(1));
253 a->Set(0, T(123));
254 EXPECT_DOUBLE_EQ(T(123), a->Get(0));
255 EXPECT_DOUBLE_EQ(0, a->Get(1));
256 a->Set(1, T(321));
257 EXPECT_DOUBLE_EQ(T(123), a->Get(0));
258 EXPECT_DOUBLE_EQ(T(321), a->Get(1));
259
260 Class* aioobe = class_linker_->FindSystemClass(soa.Self(),
261 "Ljava/lang/ArrayIndexOutOfBoundsException;");
262
263 EXPECT_DOUBLE_EQ(0, a->Get(-1));
264 EXPECT_TRUE(soa.Self()->IsExceptionPending());
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000265 EXPECT_EQ(aioobe, soa.Self()->GetException()->GetClass());
Ian Rogers647b1a82014-10-10 11:02:11 -0700266 soa.Self()->ClearException();
267
268 EXPECT_DOUBLE_EQ(0, a->Get(2));
269 EXPECT_TRUE(soa.Self()->IsExceptionPending());
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000270 EXPECT_EQ(aioobe, soa.Self()->GetException()->GetClass());
Ian Rogers647b1a82014-10-10 11:02:11 -0700271 soa.Self()->ClearException();
272}
273
274TEST_F(ObjectTest, PrimitiveArray_Float_Alloc) {
275 typedef FloatArray ArrayT;
276 ScopedObjectAccess soa(Thread::Current());
277 typedef typename ArrayT::ElementType T;
278
279 ArrayT* a = ArrayT::Alloc(soa.Self(), 2);
280 EXPECT_FLOAT_EQ(2, a->GetLength());
281 EXPECT_FLOAT_EQ(0, a->Get(0));
282 EXPECT_FLOAT_EQ(0, a->Get(1));
283 a->Set(0, T(123));
284 EXPECT_FLOAT_EQ(T(123), a->Get(0));
285 EXPECT_FLOAT_EQ(0, a->Get(1));
286 a->Set(1, T(321));
287 EXPECT_FLOAT_EQ(T(123), a->Get(0));
288 EXPECT_FLOAT_EQ(T(321), a->Get(1));
289
290 Class* aioobe = class_linker_->FindSystemClass(soa.Self(),
291 "Ljava/lang/ArrayIndexOutOfBoundsException;");
292
293 EXPECT_FLOAT_EQ(0, a->Get(-1));
294 EXPECT_TRUE(soa.Self()->IsExceptionPending());
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000295 EXPECT_EQ(aioobe, soa.Self()->GetException()->GetClass());
Ian Rogers647b1a82014-10-10 11:02:11 -0700296 soa.Self()->ClearException();
297
298 EXPECT_FLOAT_EQ(0, a->Get(2));
299 EXPECT_TRUE(soa.Self()->IsExceptionPending());
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000300 EXPECT_EQ(aioobe, soa.Self()->GetException()->GetClass());
Ian Rogers647b1a82014-10-10 11:02:11 -0700301 soa.Self()->ClearException();
302}
303
304
Elliott Hughesb408de72011-10-04 14:35:05 -0700305TEST_F(ObjectTest, CheckAndAllocArrayFromCode) {
Ian Rogersb886da82011-09-23 16:27:54 -0700306 // pretend we are trying to call 'new char[3]' from String.toCharArray
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700307 ScopedObjectAccess soa(Thread::Current());
Ian Rogers98379392014-02-24 16:53:16 -0800308 Class* java_util_Arrays = class_linker_->FindSystemClass(soa.Self(), "Ljava/util/Arrays;");
Mathieu Chartiere401d142015-04-22 13:56:20 -0700309 ArtMethod* sort = java_util_Arrays->FindDirectMethod("sort", "([I)V", sizeof(void*));
Mathieu Chartier9507fa22015-10-29 15:08:57 -0700310 const DexFile::TypeId* type_id = java_lang_dex_file_->FindTypeId("[I");
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700311 ASSERT_TRUE(type_id != nullptr);
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800312 uint32_t type_idx = java_lang_dex_file_->GetIndexForTypeId(*type_id);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700313 Object* array = CheckAndAllocArrayFromCodeInstrumented(
314 type_idx, 3, sort, Thread::Current(), false,
315 Runtime::Current()->GetHeap()->GetCurrentAllocator());
Brian Carlstromb63ec392011-08-27 17:38:27 -0700316 EXPECT_TRUE(array->IsArrayInstance());
317 EXPECT_EQ(3, array->AsArray()->GetLength());
318 EXPECT_TRUE(array->GetClass()->IsArrayClass());
319 EXPECT_TRUE(array->GetClass()->GetComponentType()->IsPrimitive());
320}
321
Ian Rogers64b6d142012-10-29 16:34:15 -0700322TEST_F(ObjectTest, CreateMultiArray) {
323 ScopedObjectAccess soa(Thread::Current());
324
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700325 StackHandleScope<2> hs(soa.Self());
326 Handle<Class> c(hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "I")));
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700327 MutableHandle<IntArray> dims(hs.NewHandle(IntArray::Alloc(soa.Self(), 1)));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100328 dims->Set<false>(0, 1);
Mathieu Chartier5bb99032014-02-08 16:20:58 -0800329 Array* multi = Array::CreateMultiArray(soa.Self(), c, dims);
Ian Rogers98379392014-02-24 16:53:16 -0800330 EXPECT_TRUE(multi->GetClass() == class_linker_->FindSystemClass(soa.Self(), "[I"));
Ian Rogers64b6d142012-10-29 16:34:15 -0700331 EXPECT_EQ(1, multi->GetLength());
332
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100333 dims->Set<false>(0, -1);
Mathieu Chartier5bb99032014-02-08 16:20:58 -0800334 multi = Array::CreateMultiArray(soa.Self(), c, dims);
Ian Rogers64b6d142012-10-29 16:34:15 -0700335 EXPECT_TRUE(soa.Self()->IsExceptionPending());
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000336 EXPECT_EQ(PrettyDescriptor(soa.Self()->GetException()->GetClass()),
Ian Rogers64b6d142012-10-29 16:34:15 -0700337 "java.lang.NegativeArraySizeException");
338 soa.Self()->ClearException();
339
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700340 dims.Assign(IntArray::Alloc(soa.Self(), 2));
Ian Rogers64b6d142012-10-29 16:34:15 -0700341 for (int i = 1; i < 20; ++i) {
342 for (int j = 0; j < 20; ++j) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100343 dims->Set<false>(0, i);
344 dims->Set<false>(1, j);
Mathieu Chartier5bb99032014-02-08 16:20:58 -0800345 multi = Array::CreateMultiArray(soa.Self(), c, dims);
Ian Rogers98379392014-02-24 16:53:16 -0800346 EXPECT_TRUE(multi->GetClass() == class_linker_->FindSystemClass(soa.Self(), "[[I"));
Ian Rogers64b6d142012-10-29 16:34:15 -0700347 EXPECT_EQ(i, multi->GetLength());
348 for (int k = 0; k < i; ++k) {
349 Array* outer = multi->AsObjectArray<Array>()->Get(k);
Ian Rogers98379392014-02-24 16:53:16 -0800350 EXPECT_TRUE(outer->GetClass() == class_linker_->FindSystemClass(soa.Self(), "[I"));
Ian Rogers64b6d142012-10-29 16:34:15 -0700351 EXPECT_EQ(j, outer->GetLength());
352 }
353 }
354 }
355}
356
Brian Carlstromb9edb842011-08-28 16:31:06 -0700357TEST_F(ObjectTest, StaticFieldFromCode) {
jeffhaoabcfde32011-09-29 15:05:18 -0700358 // pretend we are trying to access 'Static.s0' from StaticsFromCode.<clinit>
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700359 ScopedObjectAccess soa(Thread::Current());
360 jobject class_loader = LoadDex("StaticsFromCode");
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700361 const DexFile* dex_file = GetFirstDexFile(class_loader);
Brian Carlstrom848a4b32011-09-04 11:29:27 -0700362
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700363 StackHandleScope<2> hs(soa.Self());
364 Handle<mirror::ClassLoader> loader(hs.NewHandle(soa.Decode<ClassLoader*>(class_loader)));
Ian Rogers98379392014-02-24 16:53:16 -0800365 Class* klass = class_linker_->FindClass(soa.Self(), "LStaticsFromCode;", loader);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700366 ArtMethod* clinit = klass->FindClassInitializer(sizeof(void*));
Mathieu Chartier9507fa22015-10-29 15:08:57 -0700367 const DexFile::TypeId* klass_type_id = dex_file->FindTypeId("LStaticsFromCode;");
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700368 ASSERT_TRUE(klass_type_id != nullptr);
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800369
Mathieu Chartier9507fa22015-10-29 15:08:57 -0700370 const DexFile::TypeId* type_type_id = dex_file->FindTypeId("Ljava/lang/Object;");
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700371 ASSERT_TRUE(type_type_id != nullptr);
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800372
373 const DexFile::StringId* name_str_id = dex_file->FindStringId("s0");
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700374 ASSERT_TRUE(name_str_id != nullptr);
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800375
376 const DexFile::FieldId* field_id = dex_file->FindFieldId(
377 *klass_type_id, *name_str_id, *type_type_id);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700378 ASSERT_TRUE(field_id != nullptr);
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800379 uint32_t field_idx = dex_file->GetIndexForFieldId(*field_id);
380
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200381 ArtField* field = FindFieldFromCode<StaticObjectRead, true>(field_idx, clinit, Thread::Current(),
Ian Rogersef7d42f2014-01-06 12:55:46 -0800382 sizeof(HeapReference<Object>));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700383 Object* s0 = field->GetObj(klass);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700384 EXPECT_TRUE(s0 != nullptr);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700385
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700386 Handle<CharArray> char_array(hs.NewHandle(CharArray::Alloc(soa.Self(), 0)));
387 field->SetObj<false>(field->GetDeclaringClass(), char_array.Get());
388 EXPECT_EQ(char_array.Get(), field->GetObj(klass));
Brian Carlstromb9edb842011-08-28 16:31:06 -0700389
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700390 field->SetObj<false>(field->GetDeclaringClass(), nullptr);
391 EXPECT_EQ(nullptr, field->GetObj(klass));
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700392
Brian Carlstromea46f952013-07-30 01:26:50 -0700393 // TODO: more exhaustive tests of all 6 cases of ArtField::*FromCode
Brian Carlstromb9edb842011-08-28 16:31:06 -0700394}
395
Brian Carlstrom0b138b22011-07-27 15:19:17 -0700396TEST_F(ObjectTest, String) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700397 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom0b138b22011-07-27 15:19:17 -0700398 // Test the empty string.
399 AssertString(0, "", "", 0);
400
401 // Test one-byte characters.
402 AssertString(1, " ", "\x00\x20", 0x20);
403 AssertString(1, "", "\x00\x00", 0);
404 AssertString(1, "\x7f", "\x00\x7f", 0x7f);
405 AssertString(2, "hi", "\x00\x68\x00\x69", (31 * 0x68) + 0x69);
406
407 // Test two-byte characters.
408 AssertString(1, "\xc2\x80", "\x00\x80", 0x80);
409 AssertString(1, "\xd9\xa6", "\x06\x66", 0x0666);
410 AssertString(1, "\xdf\xbf", "\x07\xff", 0x07ff);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700411 AssertString(3, "h\xd9\xa6i", "\x00\x68\x06\x66\x00\x69",
412 (31 * ((31 * 0x68) + 0x0666)) + 0x69);
Brian Carlstrom0b138b22011-07-27 15:19:17 -0700413
414 // Test three-byte characters.
415 AssertString(1, "\xe0\xa0\x80", "\x08\x00", 0x0800);
416 AssertString(1, "\xe1\x88\xb4", "\x12\x34", 0x1234);
417 AssertString(1, "\xef\xbf\xbf", "\xff\xff", 0xffff);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700418 AssertString(3, "h\xe1\x88\xb4i", "\x00\x68\x12\x34\x00\x69",
419 (31 * ((31 * 0x68) + 0x1234)) + 0x69);
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000420
421 // Test four-byte characters.
422 AssertString(2, "\xf0\x9f\x8f\xa0", "\xd8\x3c\xdf\xe0", (31 * 0xd83c) + 0xdfe0);
423 AssertString(2, "\xf0\x9f\x9a\x80", "\xd8\x3d\xde\x80", (31 * 0xd83d) + 0xde80);
424 AssertString(4, "h\xf0\x9f\x9a\x80i", "\x00\x68\xd8\x3d\xde\x80\x00\x69",
425 (31 * (31 * (31 * 0x68 + 0xd83d) + 0xde80) + 0x69));
Brian Carlstrom0b138b22011-07-27 15:19:17 -0700426}
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -0400427
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700428TEST_F(ObjectTest, StringEqualsUtf8) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700429 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700430 StackHandleScope<2> hs(soa.Self());
431 Handle<String> string(hs.NewHandle(String::AllocFromModifiedUtf8(soa.Self(), "android")));
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700432 EXPECT_TRUE(string->Equals("android"));
433 EXPECT_FALSE(string->Equals("Android"));
434 EXPECT_FALSE(string->Equals("ANDROID"));
435 EXPECT_FALSE(string->Equals(""));
436 EXPECT_FALSE(string->Equals("and"));
437 EXPECT_FALSE(string->Equals("androids"));
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700438
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700439 Handle<String> empty(hs.NewHandle(String::AllocFromModifiedUtf8(soa.Self(), "")));
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700440 EXPECT_TRUE(empty->Equals(""));
441 EXPECT_FALSE(empty->Equals("a"));
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -0400442}
443
444TEST_F(ObjectTest, StringEquals) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700445 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700446 StackHandleScope<3> hs(soa.Self());
447 Handle<String> string(hs.NewHandle(String::AllocFromModifiedUtf8(soa.Self(), "android")));
448 Handle<String> string_2(hs.NewHandle(String::AllocFromModifiedUtf8(soa.Self(), "android")));
449 EXPECT_TRUE(string->Equals(string_2.Get()));
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700450 EXPECT_FALSE(string->Equals("Android"));
451 EXPECT_FALSE(string->Equals("ANDROID"));
452 EXPECT_FALSE(string->Equals(""));
453 EXPECT_FALSE(string->Equals("and"));
454 EXPECT_FALSE(string->Equals("androids"));
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -0400455
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700456 Handle<String> empty(hs.NewHandle(String::AllocFromModifiedUtf8(soa.Self(), "")));
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700457 EXPECT_TRUE(empty->Equals(""));
458 EXPECT_FALSE(empty->Equals("a"));
459}
460
Ian Rogers64b6d142012-10-29 16:34:15 -0700461TEST_F(ObjectTest, StringCompareTo) {
462 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700463 StackHandleScope<5> hs(soa.Self());
464 Handle<String> string(hs.NewHandle(String::AllocFromModifiedUtf8(soa.Self(), "android")));
465 Handle<String> string_2(hs.NewHandle(String::AllocFromModifiedUtf8(soa.Self(), "android")));
466 Handle<String> string_3(hs.NewHandle(String::AllocFromModifiedUtf8(soa.Self(), "Android")));
467 Handle<String> string_4(hs.NewHandle(String::AllocFromModifiedUtf8(soa.Self(), "and")));
468 Handle<String> string_5(hs.NewHandle(String::AllocFromModifiedUtf8(soa.Self(), "")));
469 EXPECT_EQ(0, string->CompareTo(string_2.Get()));
470 EXPECT_LT(0, string->CompareTo(string_3.Get()));
471 EXPECT_GT(0, string_3->CompareTo(string.Get()));
472 EXPECT_LT(0, string->CompareTo(string_4.Get()));
473 EXPECT_GT(0, string_4->CompareTo(string.Get()));
474 EXPECT_LT(0, string->CompareTo(string_5.Get()));
475 EXPECT_GT(0, string_5->CompareTo(string.Get()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700476}
477
jeffhao0ce13152012-03-27 19:45:50 -0700478TEST_F(ObjectTest, StringLength) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700479 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700480 StackHandleScope<1> hs(soa.Self());
481 Handle<String> string(hs.NewHandle(String::AllocFromModifiedUtf8(soa.Self(), "android")));
jeffhao0ce13152012-03-27 19:45:50 -0700482 EXPECT_EQ(string->GetLength(), 7);
483 EXPECT_EQ(string->GetUtfLength(), 7);
jeffhao0ce13152012-03-27 19:45:50 -0700484}
485
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700486TEST_F(ObjectTest, DescriptorCompare) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700487 // Two classloaders conflicts in compile_time_class_paths_.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700488 ScopedObjectAccess soa(Thread::Current());
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700489 ClassLinker* linker = class_linker_;
490
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700491 jobject jclass_loader_1 = LoadDex("ProtoCompare");
492 jobject jclass_loader_2 = LoadDex("ProtoCompare2");
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700493 StackHandleScope<4> hs(soa.Self());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700494 Handle<ClassLoader> class_loader_1(hs.NewHandle(soa.Decode<ClassLoader*>(jclass_loader_1)));
495 Handle<ClassLoader> class_loader_2(hs.NewHandle(soa.Decode<ClassLoader*>(jclass_loader_2)));
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700496
Ian Rogers98379392014-02-24 16:53:16 -0800497 Class* klass1 = linker->FindClass(soa.Self(), "LProtoCompare;", class_loader_1);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700498 ASSERT_TRUE(klass1 != nullptr);
Ian Rogers98379392014-02-24 16:53:16 -0800499 Class* klass2 = linker->FindClass(soa.Self(), "LProtoCompare2;", class_loader_2);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700500 ASSERT_TRUE(klass2 != nullptr);
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700501
Mathieu Chartiere401d142015-04-22 13:56:20 -0700502 ArtMethod* m1_1 = klass1->GetVirtualMethod(0, sizeof(void*));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700503 EXPECT_STREQ(m1_1->GetName(), "m1");
Mathieu Chartiere401d142015-04-22 13:56:20 -0700504 ArtMethod* m2_1 = klass1->GetVirtualMethod(1, sizeof(void*));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700505 EXPECT_STREQ(m2_1->GetName(), "m2");
Mathieu Chartiere401d142015-04-22 13:56:20 -0700506 ArtMethod* m3_1 = klass1->GetVirtualMethod(2, sizeof(void*));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700507 EXPECT_STREQ(m3_1->GetName(), "m3");
Mathieu Chartiere401d142015-04-22 13:56:20 -0700508 ArtMethod* m4_1 = klass1->GetVirtualMethod(3, sizeof(void*));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700509 EXPECT_STREQ(m4_1->GetName(), "m4");
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700510
Mathieu Chartiere401d142015-04-22 13:56:20 -0700511 ArtMethod* m1_2 = klass2->GetVirtualMethod(0, sizeof(void*));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700512 EXPECT_STREQ(m1_2->GetName(), "m1");
Mathieu Chartiere401d142015-04-22 13:56:20 -0700513 ArtMethod* m2_2 = klass2->GetVirtualMethod(1, sizeof(void*));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700514 EXPECT_STREQ(m2_2->GetName(), "m2");
Mathieu Chartiere401d142015-04-22 13:56:20 -0700515 ArtMethod* m3_2 = klass2->GetVirtualMethod(2, sizeof(void*));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700516 EXPECT_STREQ(m3_2->GetName(), "m3");
Mathieu Chartiere401d142015-04-22 13:56:20 -0700517 ArtMethod* m4_2 = klass2->GetVirtualMethod(3, sizeof(void*));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700518 EXPECT_STREQ(m4_2->GetName(), "m4");
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -0400519}
520
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700521TEST_F(ObjectTest, StringHashCode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700522 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700523 StackHandleScope<3> hs(soa.Self());
524 Handle<String> empty(hs.NewHandle(String::AllocFromModifiedUtf8(soa.Self(), "")));
525 Handle<String> A(hs.NewHandle(String::AllocFromModifiedUtf8(soa.Self(), "A")));
526 Handle<String> ABC(hs.NewHandle(String::AllocFromModifiedUtf8(soa.Self(), "ABC")));
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700527
528 EXPECT_EQ(0, empty->GetHashCode());
529 EXPECT_EQ(65, A->GetHashCode());
530 EXPECT_EQ(64578, ABC->GetHashCode());
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700531}
532
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700533TEST_F(ObjectTest, InstanceOf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700534 ScopedObjectAccess soa(Thread::Current());
535 jobject jclass_loader = LoadDex("XandY");
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700536 StackHandleScope<3> hs(soa.Self());
537 Handle<ClassLoader> class_loader(hs.NewHandle(soa.Decode<ClassLoader*>(jclass_loader)));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700538
Ian Rogers98379392014-02-24 16:53:16 -0800539 Class* X = class_linker_->FindClass(soa.Self(), "LX;", class_loader);
540 Class* Y = class_linker_->FindClass(soa.Self(), "LY;", class_loader);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700541 ASSERT_TRUE(X != nullptr);
542 ASSERT_TRUE(Y != nullptr);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700543
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700544 Handle<Object> x(hs.NewHandle(X->AllocObject(soa.Self())));
545 Handle<Object> y(hs.NewHandle(Y->AllocObject(soa.Self())));
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700546 ASSERT_TRUE(x.Get() != nullptr);
547 ASSERT_TRUE(y.Get() != nullptr);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700548
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700549 EXPECT_TRUE(x->InstanceOf(X));
550 EXPECT_FALSE(x->InstanceOf(Y));
551 EXPECT_TRUE(y->InstanceOf(X));
552 EXPECT_TRUE(y->InstanceOf(Y));
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700553
Ian Rogers98379392014-02-24 16:53:16 -0800554 Class* java_lang_Class = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Class;");
555 Class* Object_array_class = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/Object;");
Elliott Hughes92f14b22011-10-06 12:29:54 -0700556
557 EXPECT_FALSE(java_lang_Class->InstanceOf(Object_array_class));
558 EXPECT_TRUE(Object_array_class->InstanceOf(java_lang_Class));
559
560 // All array classes implement Cloneable and Serializable.
Ian Rogers50b35e22012-10-04 10:09:15 -0700561 Object* array = ObjectArray<Object>::Alloc(soa.Self(), Object_array_class, 1);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700562 Class* java_lang_Cloneable =
563 class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Cloneable;");
564 Class* java_io_Serializable =
565 class_linker_->FindSystemClass(soa.Self(), "Ljava/io/Serializable;");
Elliott Hughes92f14b22011-10-06 12:29:54 -0700566 EXPECT_TRUE(array->InstanceOf(java_lang_Cloneable));
567 EXPECT_TRUE(array->InstanceOf(java_io_Serializable));
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700568}
569
570TEST_F(ObjectTest, IsAssignableFrom) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700571 ScopedObjectAccess soa(Thread::Current());
572 jobject jclass_loader = LoadDex("XandY");
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700573 StackHandleScope<1> hs(soa.Self());
574 Handle<ClassLoader> class_loader(hs.NewHandle(soa.Decode<ClassLoader*>(jclass_loader)));
Ian Rogers98379392014-02-24 16:53:16 -0800575 Class* X = class_linker_->FindClass(soa.Self(), "LX;", class_loader);
576 Class* Y = class_linker_->FindClass(soa.Self(), "LY;", class_loader);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700577
578 EXPECT_TRUE(X->IsAssignableFrom(X));
579 EXPECT_TRUE(X->IsAssignableFrom(Y));
580 EXPECT_FALSE(Y->IsAssignableFrom(X));
581 EXPECT_TRUE(Y->IsAssignableFrom(Y));
Ian Rogersd81871c2011-10-03 13:57:23 -0700582
583 // class final String implements CharSequence, ..
Ian Rogers98379392014-02-24 16:53:16 -0800584 Class* string = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/String;");
585 Class* charseq = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/CharSequence;");
Ian Rogersd81871c2011-10-03 13:57:23 -0700586 // Can String be assigned to CharSequence without a cast?
587 EXPECT_TRUE(charseq->IsAssignableFrom(string));
588 // Can CharSequence be assigned to String without a cast?
589 EXPECT_FALSE(string->IsAssignableFrom(charseq));
590
591 // Primitive types are only assignable to themselves
592 const char* prims = "ZBCSIJFD";
593 Class* prim_types[strlen(prims)];
594 for (size_t i = 0; i < strlen(prims); i++) {
595 prim_types[i] = class_linker_->FindPrimitiveClass(prims[i]);
596 }
597 for (size_t i = 0; i < strlen(prims); i++) {
598 for (size_t j = 0; i < strlen(prims); i++) {
599 if (i == j) {
600 EXPECT_TRUE(prim_types[i]->IsAssignableFrom(prim_types[j]));
601 } else {
602 EXPECT_FALSE(prim_types[i]->IsAssignableFrom(prim_types[j]));
603 }
604 }
605 }
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700606}
607
608TEST_F(ObjectTest, IsAssignableFromArray) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700609 ScopedObjectAccess soa(Thread::Current());
610 jobject jclass_loader = LoadDex("XandY");
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700611 StackHandleScope<1> hs(soa.Self());
612 Handle<ClassLoader> class_loader(hs.NewHandle(soa.Decode<ClassLoader*>(jclass_loader)));
Ian Rogers98379392014-02-24 16:53:16 -0800613 Class* X = class_linker_->FindClass(soa.Self(), "LX;", class_loader);
614 Class* Y = class_linker_->FindClass(soa.Self(), "LY;", class_loader);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700615 ASSERT_TRUE(X != nullptr);
616 ASSERT_TRUE(Y != nullptr);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700617
Ian Rogers98379392014-02-24 16:53:16 -0800618 Class* YA = class_linker_->FindClass(soa.Self(), "[LY;", class_loader);
619 Class* YAA = class_linker_->FindClass(soa.Self(), "[[LY;", class_loader);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700620 ASSERT_TRUE(YA != nullptr);
621 ASSERT_TRUE(YAA != nullptr);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700622
Ian Rogers98379392014-02-24 16:53:16 -0800623 Class* XAA = class_linker_->FindClass(soa.Self(), "[[LX;", class_loader);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700624 ASSERT_TRUE(XAA != nullptr);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700625
Ian Rogers98379392014-02-24 16:53:16 -0800626 Class* O = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;");
627 Class* OA = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/Object;");
628 Class* OAA = class_linker_->FindSystemClass(soa.Self(), "[[Ljava/lang/Object;");
629 Class* OAAA = class_linker_->FindSystemClass(soa.Self(), "[[[Ljava/lang/Object;");
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700630 ASSERT_TRUE(O != nullptr);
631 ASSERT_TRUE(OA != nullptr);
632 ASSERT_TRUE(OAA != nullptr);
633 ASSERT_TRUE(OAAA != nullptr);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700634
Ian Rogers98379392014-02-24 16:53:16 -0800635 Class* S = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/Serializable;");
636 Class* SA = class_linker_->FindSystemClass(soa.Self(), "[Ljava/io/Serializable;");
637 Class* SAA = class_linker_->FindSystemClass(soa.Self(), "[[Ljava/io/Serializable;");
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700638 ASSERT_TRUE(S != nullptr);
639 ASSERT_TRUE(SA != nullptr);
640 ASSERT_TRUE(SAA != nullptr);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700641
Ian Rogers98379392014-02-24 16:53:16 -0800642 Class* IA = class_linker_->FindSystemClass(soa.Self(), "[I");
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700643 ASSERT_TRUE(IA != nullptr);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700644
645 EXPECT_TRUE(YAA->IsAssignableFrom(YAA)); // identity
646 EXPECT_TRUE(XAA->IsAssignableFrom(YAA)); // element superclass
647 EXPECT_FALSE(YAA->IsAssignableFrom(XAA));
648 EXPECT_FALSE(Y->IsAssignableFrom(YAA));
649 EXPECT_FALSE(YA->IsAssignableFrom(YAA));
650 EXPECT_TRUE(O->IsAssignableFrom(YAA)); // everything is an Object
651 EXPECT_TRUE(OA->IsAssignableFrom(YAA));
652 EXPECT_TRUE(OAA->IsAssignableFrom(YAA));
653 EXPECT_TRUE(S->IsAssignableFrom(YAA)); // all arrays are Serializable
654 EXPECT_TRUE(SA->IsAssignableFrom(YAA));
655 EXPECT_FALSE(SAA->IsAssignableFrom(YAA)); // unless Y was Serializable
656
657 EXPECT_FALSE(IA->IsAssignableFrom(OA));
658 EXPECT_FALSE(OA->IsAssignableFrom(IA));
659 EXPECT_TRUE(O->IsAssignableFrom(IA));
660}
661
Elliott Hughescdf53122011-08-19 15:46:09 -0700662TEST_F(ObjectTest, FindInstanceField) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700663 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700664 StackHandleScope<1> hs(soa.Self());
665 Handle<String> s(hs.NewHandle(String::AllocFromModifiedUtf8(soa.Self(), "ABC")));
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700666 ASSERT_TRUE(s.Get() != nullptr);
Elliott Hughescdf53122011-08-19 15:46:09 -0700667 Class* c = s->GetClass();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700668 ASSERT_TRUE(c != nullptr);
Elliott Hughescdf53122011-08-19 15:46:09 -0700669
670 // Wrong type.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700671 EXPECT_TRUE(c->FindDeclaredInstanceField("count", "J") == nullptr);
672 EXPECT_TRUE(c->FindInstanceField("count", "J") == nullptr);
Elliott Hughescdf53122011-08-19 15:46:09 -0700673
674 // Wrong name.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700675 EXPECT_TRUE(c->FindDeclaredInstanceField("Count", "I") == nullptr);
676 EXPECT_TRUE(c->FindInstanceField("Count", "I") == nullptr);
Elliott Hughescdf53122011-08-19 15:46:09 -0700677
678 // Right name and type.
Brian Carlstromea46f952013-07-30 01:26:50 -0700679 ArtField* f1 = c->FindDeclaredInstanceField("count", "I");
680 ArtField* f2 = c->FindInstanceField("count", "I");
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700681 EXPECT_TRUE(f1 != nullptr);
682 EXPECT_TRUE(f2 != nullptr);
Elliott Hughescdf53122011-08-19 15:46:09 -0700683 EXPECT_EQ(f1, f2);
684
685 // TODO: check that s.count == 3.
686
687 // Ensure that we handle superclass fields correctly...
Ian Rogers98379392014-02-24 16:53:16 -0800688 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/StringBuilder;");
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700689 ASSERT_TRUE(c != nullptr);
Elliott Hughescdf53122011-08-19 15:46:09 -0700690 // No StringBuilder.count...
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700691 EXPECT_TRUE(c->FindDeclaredInstanceField("count", "I") == nullptr);
Elliott Hughescdf53122011-08-19 15:46:09 -0700692 // ...but there is an AbstractStringBuilder.count.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700693 EXPECT_TRUE(c->FindInstanceField("count", "I") != nullptr);
Elliott Hughescdf53122011-08-19 15:46:09 -0700694}
695
696TEST_F(ObjectTest, FindStaticField) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700697 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierf8322842014-05-16 10:59:25 -0700698 StackHandleScope<4> hs(soa.Self());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700699 Handle<String> s(hs.NewHandle(String::AllocFromModifiedUtf8(soa.Self(), "ABC")));
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700700 ASSERT_TRUE(s.Get() != nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700701 Handle<Class> c(hs.NewHandle(s->GetClass()));
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700702 ASSERT_TRUE(c.Get() != nullptr);
Elliott Hughescdf53122011-08-19 15:46:09 -0700703
704 // Wrong type.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700705 EXPECT_TRUE(c->FindDeclaredStaticField("CASE_INSENSITIVE_ORDER", "I") == nullptr);
706 EXPECT_TRUE(mirror::Class::FindStaticField(
707 soa.Self(), c, "CASE_INSENSITIVE_ORDER", "I") == nullptr);
Elliott Hughescdf53122011-08-19 15:46:09 -0700708
709 // Wrong name.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700710 EXPECT_TRUE(c->FindDeclaredStaticField(
711 "cASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;") == nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700712 EXPECT_TRUE(
713 mirror::Class::FindStaticField(soa.Self(), c, "cASE_INSENSITIVE_ORDER",
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700714 "Ljava/util/Comparator;") == nullptr);
Elliott Hughescdf53122011-08-19 15:46:09 -0700715
716 // Right name and type.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700717 ArtField* f1 = c->FindDeclaredStaticField("CASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;");
718 ArtField* f2 = mirror::Class::FindStaticField(soa.Self(), c, "CASE_INSENSITIVE_ORDER",
719 "Ljava/util/Comparator;");
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700720 EXPECT_TRUE(f1 != nullptr);
721 EXPECT_TRUE(f2 != nullptr);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700722 EXPECT_EQ(f1, f2);
Elliott Hughescdf53122011-08-19 15:46:09 -0700723
724 // TODO: test static fields via superclasses.
725 // TODO: test static fields via interfaces.
726 // TODO: test that interfaces trump superclasses.
727}
728
tony.ys_liu7380c312015-01-16 19:16:45 +0800729TEST_F(ObjectTest, IdentityHashCode) {
730 // Regression test for b/19046417 which had an infinite loop if the
731 // (seed & LockWord::kHashMask) == 0. seed 0 triggered the infinite loop since we did the check
732 // before the CAS which resulted in the same seed the next loop iteration.
733 mirror::Object::SetHashCodeSeed(0);
734 int32_t hash_code = mirror::Object::GenerateIdentityHashCode();
735 EXPECT_NE(hash_code, 0);
736}
737
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800738} // namespace mirror
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700739} // namespace art