blob: f28c20b59838ccf5f7cca388a17cfad440ff89f1 [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>
21
Ian Rogers1f539342012-10-03 21:09:42 -070022#include "asm_support.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070023#include "class_linker.h"
24#include "common_test.h"
25#include "dex_file.h"
26#include "heap.h"
Ian Rogersce9eca62011-10-07 17:11:03 -070027#include "runtime_support.h"
Ian Rogers1f539342012-10-03 21:09:42 -070028#include "sirt_ref.h"
29#include "UniquePtr.h"
Carl Shapiro894d0fa2011-06-30 14:48:49 -070030
31namespace art {
32
Brian Carlstromf734cf52011-08-17 16:28:14 -070033class ObjectTest : public CommonTest {
Brian Carlstrom0b138b22011-07-27 15:19:17 -070034 protected:
Elliott Hughes814e4032011-08-23 12:07:56 -070035 void AssertString(int32_t length,
Brian Carlstrom0b138b22011-07-27 15:19:17 -070036 const char* utf8_in,
37 const char* utf16_expected_le,
Ian Rogers00f7d0e2012-07-19 15:28:27 -070038 int32_t expected_hash)
Ian Rogersb726dcb2012-09-05 08:57:23 -070039 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesee0fa762012-03-26 17:12:41 -070040 UniquePtr<uint16_t[]> utf16_expected(new uint16_t[length]);
Elliott Hughes814e4032011-08-23 12:07:56 -070041 for (int32_t i = 0; i < length; i++) {
Brian Carlstrom0b138b22011-07-27 15:19:17 -070042 uint16_t ch = (((utf16_expected_le[i*2 + 0] & 0xff) << 8) |
43 ((utf16_expected_le[i*2 + 1] & 0xff) << 0));
44 utf16_expected[i] = ch;
45 }
46
Ian Rogers1f539342012-10-03 21:09:42 -070047 SirtRef<String> string(Thread::Current(), String::AllocFromModifiedUtf8(length, utf8_in));
Carl Shapiro8860c0e2011-08-04 17:36:16 -070048 ASSERT_EQ(length, string->GetLength());
49 ASSERT_TRUE(string->GetCharArray() != NULL);
Jesse Wilsonfd687c52011-08-04 19:27:35 -070050 ASSERT_TRUE(string->GetCharArray()->GetData() != NULL);
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -040051 // strlen is necessary because the 1-character string "\0" is interpreted as ""
Elliott Hughes814e4032011-08-23 12:07:56 -070052 ASSERT_TRUE(string->Equals(utf8_in) || length != static_cast<int32_t>(strlen(utf8_in)));
53 for (int32_t i = 0; i < length; i++) {
Jesse Wilsonfd687c52011-08-04 19:27:35 -070054 EXPECT_EQ(utf16_expected[i], string->CharAt(i));
Brian Carlstrom0b138b22011-07-27 15:19:17 -070055 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070056 EXPECT_EQ(expected_hash, string->GetHashCode());
Brian Carlstrom0b138b22011-07-27 15:19:17 -070057 }
58};
Brian Carlstroma331b3c2011-07-18 17:47:56 -070059
buzbeefc9e6fa2012-03-23 15:14:29 -070060// Keep the assembly code in sync
61TEST_F(ObjectTest, AsmConstants) {
62 ASSERT_EQ(STRING_VALUE_OFFSET, String::ValueOffset().Int32Value());
63 ASSERT_EQ(STRING_COUNT_OFFSET, String::CountOffset().Int32Value());
64 ASSERT_EQ(STRING_OFFSET_OFFSET, String::OffsetOffset().Int32Value());
65 ASSERT_EQ(STRING_DATA_OFFSET, Array::DataOffset(sizeof(uint16_t)).Int32Value());
66}
67
Brian Carlstroma331b3c2011-07-18 17:47:56 -070068TEST_F(ObjectTest, IsInSamePackage) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -070069 // Matches
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080070 EXPECT_TRUE(Class::IsInSamePackage("Ljava/lang/Object;", "Ljava/lang/Class;"));
71 EXPECT_TRUE(Class::IsInSamePackage("LFoo;", "LBar;"));
Carl Shapiro894d0fa2011-06-30 14:48:49 -070072
73 // Mismatches
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080074 EXPECT_FALSE(Class::IsInSamePackage("Ljava/lang/Object;", "Ljava/io/File;"));
75 EXPECT_FALSE(Class::IsInSamePackage("Ljava/lang/Object;", "Ljava/lang/reflect/Method;"));
Carl Shapiro894d0fa2011-06-30 14:48:49 -070076}
77
Elliott Hughes081be7f2011-09-18 16:50:26 -070078TEST_F(ObjectTest, Clone) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070079 ScopedObjectAccess soa(Thread::Current());
Ian Rogers1f539342012-10-03 21:09:42 -070080 SirtRef<ObjectArray<Object> > a1(soa.Self(), class_linker_->AllocObjectArray<Object>(256));
Elliott Hughes081be7f2011-09-18 16:50:26 -070081 size_t s1 = a1->SizeOf();
82 Object* clone = a1->Clone();
83 EXPECT_EQ(s1, clone->SizeOf());
84 EXPECT_TRUE(clone->GetClass() == a1->GetClass());
85}
86
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070087TEST_F(ObjectTest, AllocObjectArray) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070088 ScopedObjectAccess soa(Thread::Current());
Ian Rogers1f539342012-10-03 21:09:42 -070089 SirtRef<ObjectArray<Object> > oa(soa.Self(), class_linker_->AllocObjectArray<Object>(2));
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070090 EXPECT_EQ(2, oa->GetLength());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -070091 EXPECT_TRUE(oa->Get(0) == NULL);
92 EXPECT_TRUE(oa->Get(1) == NULL);
Brian Carlstrom40381fb2011-10-19 14:13:40 -070093 oa->Set(0, oa.get());
94 EXPECT_TRUE(oa->Get(0) == oa.get());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -070095 EXPECT_TRUE(oa->Get(1) == NULL);
Brian Carlstrom40381fb2011-10-19 14:13:40 -070096 oa->Set(1, oa.get());
97 EXPECT_TRUE(oa->Get(0) == oa.get());
98 EXPECT_TRUE(oa->Get(1) == oa.get());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -070099
Elliott Hughes710a0cb2011-08-16 14:32:37 -0700100 Class* aioobe = class_linker_->FindSystemClass("Ljava/lang/ArrayIndexOutOfBoundsException;");
101
102 EXPECT_TRUE(oa->Get(-1) == NULL);
Ian Rogers1f539342012-10-03 21:09:42 -0700103 EXPECT_TRUE(soa.Self()->IsExceptionPending());
104 EXPECT_EQ(aioobe, soa.Self()->GetException()->GetClass());
105 soa.Self()->ClearException();
Elliott Hughes710a0cb2011-08-16 14:32:37 -0700106
107 EXPECT_TRUE(oa->Get(2) == NULL);
Ian Rogers1f539342012-10-03 21:09:42 -0700108 EXPECT_TRUE(soa.Self()->IsExceptionPending());
109 EXPECT_EQ(aioobe, soa.Self()->GetException()->GetClass());
110 soa.Self()->ClearException();
Elliott Hughes710a0cb2011-08-16 14:32:37 -0700111
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700112 ASSERT_TRUE(oa->GetClass() != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800113 ClassHelper oa_ch(oa->GetClass());
Ian Rogersd24e2642012-06-06 21:21:43 -0700114 ASSERT_EQ(2U, oa_ch.NumDirectInterfaces());
115 EXPECT_EQ(class_linker_->FindSystemClass("Ljava/lang/Cloneable;"), oa_ch.GetDirectInterface(0));
116 EXPECT_EQ(class_linker_->FindSystemClass("Ljava/io/Serializable;"), oa_ch.GetDirectInterface(1));
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700117}
118
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700119TEST_F(ObjectTest, AllocArray) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700120 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700121 Class* c = class_linker_->FindSystemClass("[I");
Ian Rogers1f539342012-10-03 21:09:42 -0700122 SirtRef<Array> a(soa.Self(), Array::Alloc(c, 1));
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700123 ASSERT_TRUE(c == a->GetClass());
124
125 c = class_linker_->FindSystemClass("[Ljava/lang/Object;");
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700126 a.reset(Array::Alloc(c, 1));
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700127 ASSERT_TRUE(c == a->GetClass());
128
129 c = class_linker_->FindSystemClass("[[Ljava/lang/Object;");
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700130 a.reset(Array::Alloc(c, 1));
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700131 ASSERT_TRUE(c == a->GetClass());
132}
133
Elliott Hughes710a0cb2011-08-16 14:32:37 -0700134template<typename ArrayT>
135void TestPrimitiveArray(ClassLinker* cl) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700136 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes710a0cb2011-08-16 14:32:37 -0700137 typedef typename ArrayT::ElementType T;
138
139 ArrayT* a = ArrayT::Alloc(2);
140 EXPECT_EQ(2, a->GetLength());
141 EXPECT_EQ(0, a->Get(0));
142 EXPECT_EQ(0, a->Get(1));
143 a->Set(0, T(123));
144 EXPECT_EQ(T(123), a->Get(0));
145 EXPECT_EQ(0, a->Get(1));
146 a->Set(1, T(321));
147 EXPECT_EQ(T(123), a->Get(0));
148 EXPECT_EQ(T(321), a->Get(1));
149
Elliott Hughes710a0cb2011-08-16 14:32:37 -0700150 Class* aioobe = cl->FindSystemClass("Ljava/lang/ArrayIndexOutOfBoundsException;");
151
152 EXPECT_EQ(0, a->Get(-1));
Ian Rogers1f539342012-10-03 21:09:42 -0700153 EXPECT_TRUE(soa.Self()->IsExceptionPending());
154 EXPECT_EQ(aioobe, soa.Self()->GetException()->GetClass());
155 soa.Self()->ClearException();
Elliott Hughes710a0cb2011-08-16 14:32:37 -0700156
157 EXPECT_EQ(0, a->Get(2));
Ian Rogers1f539342012-10-03 21:09:42 -0700158 EXPECT_TRUE(soa.Self()->IsExceptionPending());
159 EXPECT_EQ(aioobe, soa.Self()->GetException()->GetClass());
160 soa.Self()->ClearException();
Elliott Hughes710a0cb2011-08-16 14:32:37 -0700161}
162
163TEST_F(ObjectTest, PrimitiveArray_Boolean_Alloc) {
164 TestPrimitiveArray<BooleanArray>(class_linker_);
165}
166TEST_F(ObjectTest, PrimitiveArray_Byte_Alloc) {
167 TestPrimitiveArray<ByteArray>(class_linker_);
168}
169TEST_F(ObjectTest, PrimitiveArray_Char_Alloc) {
170 TestPrimitiveArray<CharArray>(class_linker_);
171}
172TEST_F(ObjectTest, PrimitiveArray_Double_Alloc) {
173 TestPrimitiveArray<DoubleArray>(class_linker_);
174}
175TEST_F(ObjectTest, PrimitiveArray_Float_Alloc) {
176 TestPrimitiveArray<FloatArray>(class_linker_);
177}
178TEST_F(ObjectTest, PrimitiveArray_Int_Alloc) {
179 TestPrimitiveArray<IntArray>(class_linker_);
180}
181TEST_F(ObjectTest, PrimitiveArray_Long_Alloc) {
182 TestPrimitiveArray<LongArray>(class_linker_);
183}
184TEST_F(ObjectTest, PrimitiveArray_Short_Alloc) {
185 TestPrimitiveArray<ShortArray>(class_linker_);
186}
187
Elliott Hughesb408de72011-10-04 14:35:05 -0700188TEST_F(ObjectTest, CheckAndAllocArrayFromCode) {
Ian Rogersb886da82011-09-23 16:27:54 -0700189 // pretend we are trying to call 'new char[3]' from String.toCharArray
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700190 ScopedObjectAccess soa(Thread::Current());
Ian Rogersb886da82011-09-23 16:27:54 -0700191 Class* java_util_Arrays = class_linker_->FindSystemClass("Ljava/util/Arrays;");
Mathieu Chartier66f19252012-09-18 08:57:04 -0700192 AbstractMethod* sort = java_util_Arrays->FindDirectMethod("sort", "([I)V");
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800193 const DexFile::StringId* string_id = java_lang_dex_file_->FindStringId("[I");
194 ASSERT_TRUE(string_id != NULL);
195 const DexFile::TypeId* type_id = java_lang_dex_file_->FindTypeId(
196 java_lang_dex_file_->GetIndexForStringId(*string_id));
197 ASSERT_TRUE(type_id != NULL);
198 uint32_t type_idx = java_lang_dex_file_->GetIndexForTypeId(*type_id);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800199 Object* array = CheckAndAllocArrayFromCode(type_idx, sort, 3, Thread::Current(), false);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700200 EXPECT_TRUE(array->IsArrayInstance());
201 EXPECT_EQ(3, array->AsArray()->GetLength());
202 EXPECT_TRUE(array->GetClass()->IsArrayClass());
203 EXPECT_TRUE(array->GetClass()->GetComponentType()->IsPrimitive());
204}
205
Brian Carlstromb9edb842011-08-28 16:31:06 -0700206TEST_F(ObjectTest, StaticFieldFromCode) {
jeffhaoabcfde32011-09-29 15:05:18 -0700207 // pretend we are trying to access 'Static.s0' from StaticsFromCode.<clinit>
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700208 ScopedObjectAccess soa(Thread::Current());
209 jobject class_loader = LoadDex("StaticsFromCode");
210 const DexFile* dex_file = Runtime::Current()->GetCompileTimeClassPath(class_loader)[0];
Brian Carlstrom848a4b32011-09-04 11:29:27 -0700211 CHECK(dex_file != NULL);
212
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700213 Class* klass =
214 class_linker_->FindClass("LStaticsFromCode;", soa.Decode<ClassLoader*>(class_loader));
Mathieu Chartier66f19252012-09-18 08:57:04 -0700215 AbstractMethod* clinit = klass->FindDirectMethod("<clinit>", "()V");
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800216 const DexFile::StringId* klass_string_id = dex_file->FindStringId("LStaticsFromCode;");
217 ASSERT_TRUE(klass_string_id != NULL);
218 const DexFile::TypeId* klass_type_id = dex_file->FindTypeId(
219 dex_file->GetIndexForStringId(*klass_string_id));
220 ASSERT_TRUE(klass_type_id != NULL);
221
222 const DexFile::StringId* type_string_id = dex_file->FindStringId("Ljava/lang/Object;");
223 ASSERT_TRUE(type_string_id != NULL);
224 const DexFile::TypeId* type_type_id = dex_file->FindTypeId(
225 dex_file->GetIndexForStringId(*type_string_id));
226 ASSERT_TRUE(type_type_id != NULL);
227
228 const DexFile::StringId* name_str_id = dex_file->FindStringId("s0");
229 ASSERT_TRUE(name_str_id != NULL);
230
231 const DexFile::FieldId* field_id = dex_file->FindFieldId(
232 *klass_type_id, *name_str_id, *type_type_id);
233 ASSERT_TRUE(field_id != NULL);
234 uint32_t field_idx = dex_file->GetIndexForFieldId(*field_id);
235
Ian Rogers08f753d2012-08-24 14:35:25 -0700236 Field* field = FindFieldFromCode(field_idx, clinit, Thread::Current(), StaticObjectRead,
237 sizeof(Object*));
Ian Rogersce9eca62011-10-07 17:11:03 -0700238 Object* s0 = field->GetObj(NULL);
jeffhaoabcfde32011-09-29 15:05:18 -0700239 EXPECT_EQ(NULL, s0);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700240
Ian Rogers1f539342012-10-03 21:09:42 -0700241 SirtRef<CharArray> char_array(soa.Self(), CharArray::Alloc(0));
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700242 field->SetObj(NULL, char_array.get());
243 EXPECT_EQ(char_array.get(), field->GetObj(NULL));
Brian Carlstromb9edb842011-08-28 16:31:06 -0700244
Ian Rogersce9eca62011-10-07 17:11:03 -0700245 field->SetObj(NULL, NULL);
246 EXPECT_EQ(NULL, field->GetObj(NULL));
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700247
Brian Carlstromb9edb842011-08-28 16:31:06 -0700248 // TODO: more exhaustive tests of all 6 cases of Field::*FromCode
249}
250
Brian Carlstrom0b138b22011-07-27 15:19:17 -0700251TEST_F(ObjectTest, String) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700252 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom0b138b22011-07-27 15:19:17 -0700253 // Test the empty string.
254 AssertString(0, "", "", 0);
255
256 // Test one-byte characters.
257 AssertString(1, " ", "\x00\x20", 0x20);
258 AssertString(1, "", "\x00\x00", 0);
259 AssertString(1, "\x7f", "\x00\x7f", 0x7f);
260 AssertString(2, "hi", "\x00\x68\x00\x69", (31 * 0x68) + 0x69);
261
262 // Test two-byte characters.
263 AssertString(1, "\xc2\x80", "\x00\x80", 0x80);
264 AssertString(1, "\xd9\xa6", "\x06\x66", 0x0666);
265 AssertString(1, "\xdf\xbf", "\x07\xff", 0x07ff);
266 AssertString(3, "h\xd9\xa6i", "\x00\x68\x06\x66\x00\x69", (31 * ((31 * 0x68) + 0x0666)) + 0x69);
267
268 // Test three-byte characters.
269 AssertString(1, "\xe0\xa0\x80", "\x08\x00", 0x0800);
270 AssertString(1, "\xe1\x88\xb4", "\x12\x34", 0x1234);
271 AssertString(1, "\xef\xbf\xbf", "\xff\xff", 0xffff);
272 AssertString(3, "h\xe1\x88\xb4i", "\x00\x68\x12\x34\x00\x69", (31 * ((31 * 0x68) + 0x1234)) + 0x69);
273}
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -0400274
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700275TEST_F(ObjectTest, StringEqualsUtf8) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700276 ScopedObjectAccess soa(Thread::Current());
Ian Rogers1f539342012-10-03 21:09:42 -0700277 SirtRef<String> string(soa.Self(), String::AllocFromModifiedUtf8("android"));
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700278 EXPECT_TRUE(string->Equals("android"));
279 EXPECT_FALSE(string->Equals("Android"));
280 EXPECT_FALSE(string->Equals("ANDROID"));
281 EXPECT_FALSE(string->Equals(""));
282 EXPECT_FALSE(string->Equals("and"));
283 EXPECT_FALSE(string->Equals("androids"));
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700284
Ian Rogers1f539342012-10-03 21:09:42 -0700285 SirtRef<String> empty(soa.Self(), String::AllocFromModifiedUtf8(""));
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700286 EXPECT_TRUE(empty->Equals(""));
287 EXPECT_FALSE(empty->Equals("a"));
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -0400288}
289
290TEST_F(ObjectTest, StringEquals) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700291 ScopedObjectAccess soa(Thread::Current());
Ian Rogers1f539342012-10-03 21:09:42 -0700292 SirtRef<String> string(soa.Self(), String::AllocFromModifiedUtf8("android"));
293 SirtRef<String> string_2(soa.Self(), String::AllocFromModifiedUtf8("android"));
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700294 EXPECT_TRUE(string->Equals(string_2.get()));
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700295 EXPECT_FALSE(string->Equals("Android"));
296 EXPECT_FALSE(string->Equals("ANDROID"));
297 EXPECT_FALSE(string->Equals(""));
298 EXPECT_FALSE(string->Equals("and"));
299 EXPECT_FALSE(string->Equals("androids"));
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -0400300
Ian Rogers1f539342012-10-03 21:09:42 -0700301 SirtRef<String> empty(soa.Self(), String::AllocFromModifiedUtf8(""));
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700302 EXPECT_TRUE(empty->Equals(""));
303 EXPECT_FALSE(empty->Equals("a"));
304}
305
jeffhao0ce13152012-03-27 19:45:50 -0700306TEST_F(ObjectTest, StringLength) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700307 ScopedObjectAccess soa(Thread::Current());
Ian Rogers1f539342012-10-03 21:09:42 -0700308 SirtRef<String> string(soa.Self(), String::AllocFromModifiedUtf8("android"));
jeffhao0ce13152012-03-27 19:45:50 -0700309 EXPECT_EQ(string->GetLength(), 7);
310 EXPECT_EQ(string->GetUtfLength(), 7);
311
312 string->SetOffset(2);
313 string->SetCount(5);
314 EXPECT_TRUE(string->Equals("droid"));
315 EXPECT_EQ(string->GetLength(), 5);
316 EXPECT_EQ(string->GetUtfLength(), 5);
317}
318
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700319TEST_F(ObjectTest, DescriptorCompare) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700320 ScopedObjectAccess soa(Thread::Current());
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700321 ClassLinker* linker = class_linker_;
322
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700323 jobject jclass_loader_1 = LoadDex("ProtoCompare");
324 jobject jclass_loader_2 = LoadDex("ProtoCompare2");
Ian Rogers1f539342012-10-03 21:09:42 -0700325 SirtRef<ClassLoader> class_loader_1(soa.Self(), soa.Decode<ClassLoader*>(jclass_loader_1));
326 SirtRef<ClassLoader> class_loader_2(soa.Self(), soa.Decode<ClassLoader*>(jclass_loader_2));
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700327
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700328 Class* klass1 = linker->FindClass("LProtoCompare;", class_loader_1.get());
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700329 ASSERT_TRUE(klass1 != NULL);
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700330 Class* klass2 = linker->FindClass("LProtoCompare2;", class_loader_2.get());
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700331 ASSERT_TRUE(klass2 != NULL);
332
Mathieu Chartier66f19252012-09-18 08:57:04 -0700333 AbstractMethod* m1_1 = klass1->GetVirtualMethod(0);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800334 MethodHelper mh(m1_1);
335 EXPECT_STREQ(mh.GetName(), "m1");
Mathieu Chartier66f19252012-09-18 08:57:04 -0700336 AbstractMethod* m2_1 = klass1->GetVirtualMethod(1);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800337 mh.ChangeMethod(m2_1);
338 EXPECT_STREQ(mh.GetName(), "m2");
Mathieu Chartier66f19252012-09-18 08:57:04 -0700339 AbstractMethod* m3_1 = klass1->GetVirtualMethod(2);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800340 mh.ChangeMethod(m3_1);
341 EXPECT_STREQ(mh.GetName(), "m3");
Mathieu Chartier66f19252012-09-18 08:57:04 -0700342 AbstractMethod* m4_1 = klass1->GetVirtualMethod(3);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800343 mh.ChangeMethod(m4_1);
344 EXPECT_STREQ(mh.GetName(), "m4");
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700345
Mathieu Chartier66f19252012-09-18 08:57:04 -0700346 AbstractMethod* m1_2 = klass2->GetVirtualMethod(0);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800347 mh.ChangeMethod(m1_2);
348 EXPECT_STREQ(mh.GetName(), "m1");
Mathieu Chartier66f19252012-09-18 08:57:04 -0700349 AbstractMethod* m2_2 = klass2->GetVirtualMethod(1);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800350 mh.ChangeMethod(m2_2);
351 EXPECT_STREQ(mh.GetName(), "m2");
Mathieu Chartier66f19252012-09-18 08:57:04 -0700352 AbstractMethod* m3_2 = klass2->GetVirtualMethod(2);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800353 mh.ChangeMethod(m3_2);
354 EXPECT_STREQ(mh.GetName(), "m3");
Mathieu Chartier66f19252012-09-18 08:57:04 -0700355 AbstractMethod* m4_2 = klass2->GetVirtualMethod(3);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800356 mh.ChangeMethod(m4_2);
357 EXPECT_STREQ(mh.GetName(), "m4");
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700358
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800359 mh.ChangeMethod(m1_1);
360 MethodHelper mh2(m1_2);
361 EXPECT_TRUE(mh.HasSameNameAndSignature(&mh2));
362 EXPECT_TRUE(mh2.HasSameNameAndSignature(&mh));
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700363
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800364 mh.ChangeMethod(m2_1);
365 mh2.ChangeMethod(m2_2);
366 EXPECT_TRUE(mh.HasSameNameAndSignature(&mh2));
367 EXPECT_TRUE(mh2.HasSameNameAndSignature(&mh));
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700368
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800369 mh.ChangeMethod(m3_1);
370 mh2.ChangeMethod(m3_2);
371 EXPECT_TRUE(mh.HasSameNameAndSignature(&mh2));
372 EXPECT_TRUE(mh2.HasSameNameAndSignature(&mh));
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700373
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800374 mh.ChangeMethod(m4_1);
375 mh2.ChangeMethod(m4_2);
376 EXPECT_TRUE(mh.HasSameNameAndSignature(&mh2));
377 EXPECT_TRUE(mh2.HasSameNameAndSignature(&mh));
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -0400378}
379
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700380
381TEST_F(ObjectTest, StringHashCode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700382 ScopedObjectAccess soa(Thread::Current());
Ian Rogers1f539342012-10-03 21:09:42 -0700383 SirtRef<String> empty(soa.Self(), String::AllocFromModifiedUtf8(""));
384 SirtRef<String> A(soa.Self(), String::AllocFromModifiedUtf8("A"));
385 SirtRef<String> ABC(soa.Self(), String::AllocFromModifiedUtf8("ABC"));
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700386
387 EXPECT_EQ(0, empty->GetHashCode());
388 EXPECT_EQ(65, A->GetHashCode());
389 EXPECT_EQ(64578, ABC->GetHashCode());
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700390}
391
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700392TEST_F(ObjectTest, InstanceOf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700393 ScopedObjectAccess soa(Thread::Current());
394 jobject jclass_loader = LoadDex("XandY");
Ian Rogers1f539342012-10-03 21:09:42 -0700395 SirtRef<ClassLoader> class_loader(soa.Self(), soa.Decode<ClassLoader*>(jclass_loader));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700396
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700397 Class* X = class_linker_->FindClass("LX;", class_loader.get());
398 Class* Y = class_linker_->FindClass("LY;", class_loader.get());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700399 ASSERT_TRUE(X != NULL);
400 ASSERT_TRUE(Y != NULL);
401
Ian Rogers1f539342012-10-03 21:09:42 -0700402 SirtRef<Object> x(soa.Self(), X->AllocObject());
403 SirtRef<Object> y(soa.Self(), Y->AllocObject());
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700404 ASSERT_TRUE(x.get() != NULL);
405 ASSERT_TRUE(y.get() != NULL);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700406
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700407 EXPECT_TRUE(x->InstanceOf(X));
408 EXPECT_FALSE(x->InstanceOf(Y));
409 EXPECT_TRUE(y->InstanceOf(X));
410 EXPECT_TRUE(y->InstanceOf(Y));
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700411
Elliott Hughes92f14b22011-10-06 12:29:54 -0700412 Class* java_lang_Class = class_linker_->FindSystemClass("Ljava/lang/Class;");
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700413 Class* Object_array_class = class_linker_->FindSystemClass("[Ljava/lang/Object;");
Elliott Hughes92f14b22011-10-06 12:29:54 -0700414
415 EXPECT_FALSE(java_lang_Class->InstanceOf(Object_array_class));
416 EXPECT_TRUE(Object_array_class->InstanceOf(java_lang_Class));
417
418 // All array classes implement Cloneable and Serializable.
419 Object* array = ObjectArray<Object>::Alloc(Object_array_class, 1);
420 Class* java_lang_Cloneable = class_linker_->FindSystemClass("Ljava/lang/Cloneable;");
421 Class* java_io_Serializable = class_linker_->FindSystemClass("Ljava/io/Serializable;");
422 EXPECT_TRUE(array->InstanceOf(java_lang_Cloneable));
423 EXPECT_TRUE(array->InstanceOf(java_io_Serializable));
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700424}
425
426TEST_F(ObjectTest, IsAssignableFrom) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700427 ScopedObjectAccess soa(Thread::Current());
428 jobject jclass_loader = LoadDex("XandY");
Ian Rogers1f539342012-10-03 21:09:42 -0700429 SirtRef<ClassLoader> class_loader(soa.Self(), soa.Decode<ClassLoader*>(jclass_loader));
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700430 Class* X = class_linker_->FindClass("LX;", class_loader.get());
431 Class* Y = class_linker_->FindClass("LY;", class_loader.get());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700432
433 EXPECT_TRUE(X->IsAssignableFrom(X));
434 EXPECT_TRUE(X->IsAssignableFrom(Y));
435 EXPECT_FALSE(Y->IsAssignableFrom(X));
436 EXPECT_TRUE(Y->IsAssignableFrom(Y));
Ian Rogersd81871c2011-10-03 13:57:23 -0700437
438 // class final String implements CharSequence, ..
439 Class* string = class_linker_->FindSystemClass("Ljava/lang/String;");
440 Class* charseq = class_linker_->FindSystemClass("Ljava/lang/CharSequence;");
441 // Can String be assigned to CharSequence without a cast?
442 EXPECT_TRUE(charseq->IsAssignableFrom(string));
443 // Can CharSequence be assigned to String without a cast?
444 EXPECT_FALSE(string->IsAssignableFrom(charseq));
445
446 // Primitive types are only assignable to themselves
447 const char* prims = "ZBCSIJFD";
448 Class* prim_types[strlen(prims)];
449 for (size_t i = 0; i < strlen(prims); i++) {
450 prim_types[i] = class_linker_->FindPrimitiveClass(prims[i]);
451 }
452 for (size_t i = 0; i < strlen(prims); i++) {
453 for (size_t j = 0; i < strlen(prims); i++) {
454 if (i == j) {
455 EXPECT_TRUE(prim_types[i]->IsAssignableFrom(prim_types[j]));
456 } else {
457 EXPECT_FALSE(prim_types[i]->IsAssignableFrom(prim_types[j]));
458 }
459 }
460 }
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700461}
462
463TEST_F(ObjectTest, IsAssignableFromArray) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700464 ScopedObjectAccess soa(Thread::Current());
465 jobject jclass_loader = LoadDex("XandY");
Ian Rogers1f539342012-10-03 21:09:42 -0700466 SirtRef<ClassLoader> class_loader(soa.Self(), soa.Decode<ClassLoader*>(jclass_loader));
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700467 Class* X = class_linker_->FindClass("LX;", class_loader.get());
468 Class* Y = class_linker_->FindClass("LY;", class_loader.get());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700469 ASSERT_TRUE(X != NULL);
470 ASSERT_TRUE(Y != NULL);
471
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700472 Class* YA = class_linker_->FindClass("[LY;", class_loader.get());
473 Class* YAA = class_linker_->FindClass("[[LY;", class_loader.get());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700474 ASSERT_TRUE(YA != NULL);
475 ASSERT_TRUE(YAA != NULL);
476
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700477 Class* XAA = class_linker_->FindClass("[[LX;", class_loader.get());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700478 ASSERT_TRUE(XAA != NULL);
479
480 Class* O = class_linker_->FindSystemClass("Ljava/lang/Object;");
481 Class* OA = class_linker_->FindSystemClass("[Ljava/lang/Object;");
482 Class* OAA = class_linker_->FindSystemClass("[[Ljava/lang/Object;");
483 Class* OAAA = class_linker_->FindSystemClass("[[[Ljava/lang/Object;");
484 ASSERT_TRUE(O != NULL);
485 ASSERT_TRUE(OA != NULL);
486 ASSERT_TRUE(OAA != NULL);
487 ASSERT_TRUE(OAAA != NULL);
488
489 Class* S = class_linker_->FindSystemClass("Ljava/io/Serializable;");
490 Class* SA = class_linker_->FindSystemClass("[Ljava/io/Serializable;");
491 Class* SAA = class_linker_->FindSystemClass("[[Ljava/io/Serializable;");
492 ASSERT_TRUE(S != NULL);
493 ASSERT_TRUE(SA != NULL);
494 ASSERT_TRUE(SAA != NULL);
495
496 Class* IA = class_linker_->FindSystemClass("[I");
497 ASSERT_TRUE(IA != NULL);
498
499 EXPECT_TRUE(YAA->IsAssignableFrom(YAA)); // identity
500 EXPECT_TRUE(XAA->IsAssignableFrom(YAA)); // element superclass
501 EXPECT_FALSE(YAA->IsAssignableFrom(XAA));
502 EXPECT_FALSE(Y->IsAssignableFrom(YAA));
503 EXPECT_FALSE(YA->IsAssignableFrom(YAA));
504 EXPECT_TRUE(O->IsAssignableFrom(YAA)); // everything is an Object
505 EXPECT_TRUE(OA->IsAssignableFrom(YAA));
506 EXPECT_TRUE(OAA->IsAssignableFrom(YAA));
507 EXPECT_TRUE(S->IsAssignableFrom(YAA)); // all arrays are Serializable
508 EXPECT_TRUE(SA->IsAssignableFrom(YAA));
509 EXPECT_FALSE(SAA->IsAssignableFrom(YAA)); // unless Y was Serializable
510
511 EXPECT_FALSE(IA->IsAssignableFrom(OA));
512 EXPECT_FALSE(OA->IsAssignableFrom(IA));
513 EXPECT_TRUE(O->IsAssignableFrom(IA));
514}
515
Elliott Hughescdf53122011-08-19 15:46:09 -0700516TEST_F(ObjectTest, FindInstanceField) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700517 ScopedObjectAccess soa(Thread::Current());
Ian Rogers1f539342012-10-03 21:09:42 -0700518 SirtRef<String> s(soa.Self(), String::AllocFromModifiedUtf8("ABC"));
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700519 ASSERT_TRUE(s.get() != NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -0700520 Class* c = s->GetClass();
521 ASSERT_TRUE(c != NULL);
522
523 // Wrong type.
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700524 EXPECT_TRUE(c->FindDeclaredInstanceField("count", "J") == NULL);
525 EXPECT_TRUE(c->FindInstanceField("count", "J") == NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -0700526
527 // Wrong name.
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700528 EXPECT_TRUE(c->FindDeclaredInstanceField("Count", "I") == NULL);
529 EXPECT_TRUE(c->FindInstanceField("Count", "I") == NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -0700530
531 // Right name and type.
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700532 Field* f1 = c->FindDeclaredInstanceField("count", "I");
533 Field* f2 = c->FindInstanceField("count", "I");
Elliott Hughescdf53122011-08-19 15:46:09 -0700534 EXPECT_TRUE(f1 != NULL);
535 EXPECT_TRUE(f2 != NULL);
536 EXPECT_EQ(f1, f2);
537
538 // TODO: check that s.count == 3.
539
540 // Ensure that we handle superclass fields correctly...
541 c = class_linker_->FindSystemClass("Ljava/lang/StringBuilder;");
542 ASSERT_TRUE(c != NULL);
543 // No StringBuilder.count...
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700544 EXPECT_TRUE(c->FindDeclaredInstanceField("count", "I") == NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -0700545 // ...but there is an AbstractStringBuilder.count.
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700546 EXPECT_TRUE(c->FindInstanceField("count", "I") != NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -0700547}
548
549TEST_F(ObjectTest, FindStaticField) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700550 ScopedObjectAccess soa(Thread::Current());
Ian Rogers1f539342012-10-03 21:09:42 -0700551 SirtRef<String> s(soa.Self(), String::AllocFromModifiedUtf8("ABC"));
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700552 ASSERT_TRUE(s.get() != NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -0700553 Class* c = s->GetClass();
554 ASSERT_TRUE(c != NULL);
555
556 // Wrong type.
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700557 EXPECT_TRUE(c->FindDeclaredStaticField("CASE_INSENSITIVE_ORDER", "I") == NULL);
558 EXPECT_TRUE(c->FindStaticField("CASE_INSENSITIVE_ORDER", "I") == NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -0700559
560 // Wrong name.
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700561 EXPECT_TRUE(c->FindDeclaredStaticField("cASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;") == NULL);
562 EXPECT_TRUE(c->FindStaticField("cASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;") == NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -0700563
564 // Right name and type.
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700565 Field* f1 = c->FindDeclaredStaticField("CASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;");
566 Field* f2 = c->FindStaticField("CASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;");
Elliott Hughescdf53122011-08-19 15:46:09 -0700567 EXPECT_TRUE(f1 != NULL);
568 EXPECT_TRUE(f2 != NULL);
569 EXPECT_EQ(f1, f2);
570
571 // TODO: test static fields via superclasses.
572 // TODO: test static fields via interfaces.
573 // TODO: test that interfaces trump superclasses.
574}
575
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700576} // namespace art