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 | */ |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 16 | |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 17 | #include "object.h" |
| 18 | |
| 19 | #include <stdint.h> |
| 20 | #include <stdio.h> |
| 21 | |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame^] | 22 | #include "asm_support.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 23 | #include "class_linker.h" |
| 24 | #include "common_test.h" |
| 25 | #include "dex_file.h" |
| 26 | #include "heap.h" |
Ian Rogers | ce9eca6 | 2011-10-07 17:11:03 -0700 | [diff] [blame] | 27 | #include "runtime_support.h" |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame^] | 28 | #include "sirt_ref.h" |
| 29 | #include "UniquePtr.h" |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 30 | |
| 31 | namespace art { |
| 32 | |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 33 | class ObjectTest : public CommonTest { |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 34 | protected: |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 35 | void AssertString(int32_t length, |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 36 | const char* utf8_in, |
| 37 | const char* utf16_expected_le, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 38 | int32_t expected_hash) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 39 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | ee0fa76 | 2012-03-26 17:12:41 -0700 | [diff] [blame] | 40 | UniquePtr<uint16_t[]> utf16_expected(new uint16_t[length]); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 41 | for (int32_t i = 0; i < length; i++) { |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 42 | 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 Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame^] | 47 | SirtRef<String> string(Thread::Current(), String::AllocFromModifiedUtf8(length, utf8_in)); |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 48 | ASSERT_EQ(length, string->GetLength()); |
| 49 | ASSERT_TRUE(string->GetCharArray() != NULL); |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 50 | ASSERT_TRUE(string->GetCharArray()->GetData() != NULL); |
Jesse Wilson | cbe9fc0 | 2011-07-29 18:59:50 -0400 | [diff] [blame] | 51 | // strlen is necessary because the 1-character string "\0" is interpreted as "" |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 52 | ASSERT_TRUE(string->Equals(utf8_in) || length != static_cast<int32_t>(strlen(utf8_in))); |
| 53 | for (int32_t i = 0; i < length; i++) { |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 54 | EXPECT_EQ(utf16_expected[i], string->CharAt(i)); |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 55 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 56 | EXPECT_EQ(expected_hash, string->GetHashCode()); |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 57 | } |
| 58 | }; |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 59 | |
buzbee | fc9e6fa | 2012-03-23 15:14:29 -0700 | [diff] [blame] | 60 | // Keep the assembly code in sync |
| 61 | TEST_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 Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 68 | TEST_F(ObjectTest, IsInSamePackage) { |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 69 | // Matches |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 70 | EXPECT_TRUE(Class::IsInSamePackage("Ljava/lang/Object;", "Ljava/lang/Class;")); |
| 71 | EXPECT_TRUE(Class::IsInSamePackage("LFoo;", "LBar;")); |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 72 | |
| 73 | // Mismatches |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 74 | EXPECT_FALSE(Class::IsInSamePackage("Ljava/lang/Object;", "Ljava/io/File;")); |
| 75 | EXPECT_FALSE(Class::IsInSamePackage("Ljava/lang/Object;", "Ljava/lang/reflect/Method;")); |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Elliott Hughes | 081be7f | 2011-09-18 16:50:26 -0700 | [diff] [blame] | 78 | TEST_F(ObjectTest, Clone) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 79 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame^] | 80 | SirtRef<ObjectArray<Object> > a1(soa.Self(), class_linker_->AllocObjectArray<Object>(256)); |
Elliott Hughes | 081be7f | 2011-09-18 16:50:26 -0700 | [diff] [blame] | 81 | 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 Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 87 | TEST_F(ObjectTest, AllocObjectArray) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 88 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame^] | 89 | SirtRef<ObjectArray<Object> > oa(soa.Self(), class_linker_->AllocObjectArray<Object>(2)); |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 90 | EXPECT_EQ(2, oa->GetLength()); |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 91 | EXPECT_TRUE(oa->Get(0) == NULL); |
| 92 | EXPECT_TRUE(oa->Get(1) == NULL); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 93 | oa->Set(0, oa.get()); |
| 94 | EXPECT_TRUE(oa->Get(0) == oa.get()); |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 95 | EXPECT_TRUE(oa->Get(1) == NULL); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 96 | oa->Set(1, oa.get()); |
| 97 | EXPECT_TRUE(oa->Get(0) == oa.get()); |
| 98 | EXPECT_TRUE(oa->Get(1) == oa.get()); |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 99 | |
Elliott Hughes | 710a0cb | 2011-08-16 14:32:37 -0700 | [diff] [blame] | 100 | Class* aioobe = class_linker_->FindSystemClass("Ljava/lang/ArrayIndexOutOfBoundsException;"); |
| 101 | |
| 102 | EXPECT_TRUE(oa->Get(-1) == NULL); |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame^] | 103 | EXPECT_TRUE(soa.Self()->IsExceptionPending()); |
| 104 | EXPECT_EQ(aioobe, soa.Self()->GetException()->GetClass()); |
| 105 | soa.Self()->ClearException(); |
Elliott Hughes | 710a0cb | 2011-08-16 14:32:37 -0700 | [diff] [blame] | 106 | |
| 107 | EXPECT_TRUE(oa->Get(2) == NULL); |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame^] | 108 | EXPECT_TRUE(soa.Self()->IsExceptionPending()); |
| 109 | EXPECT_EQ(aioobe, soa.Self()->GetException()->GetClass()); |
| 110 | soa.Self()->ClearException(); |
Elliott Hughes | 710a0cb | 2011-08-16 14:32:37 -0700 | [diff] [blame] | 111 | |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 112 | ASSERT_TRUE(oa->GetClass() != NULL); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 113 | ClassHelper oa_ch(oa->GetClass()); |
Ian Rogers | d24e264 | 2012-06-06 21:21:43 -0700 | [diff] [blame] | 114 | 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 Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Elliott Hughes | 68f4fa0 | 2011-08-21 10:46:59 -0700 | [diff] [blame] | 119 | TEST_F(ObjectTest, AllocArray) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 120 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 68f4fa0 | 2011-08-21 10:46:59 -0700 | [diff] [blame] | 121 | Class* c = class_linker_->FindSystemClass("[I"); |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame^] | 122 | SirtRef<Array> a(soa.Self(), Array::Alloc(c, 1)); |
Elliott Hughes | 68f4fa0 | 2011-08-21 10:46:59 -0700 | [diff] [blame] | 123 | ASSERT_TRUE(c == a->GetClass()); |
| 124 | |
| 125 | c = class_linker_->FindSystemClass("[Ljava/lang/Object;"); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 126 | a.reset(Array::Alloc(c, 1)); |
Elliott Hughes | 68f4fa0 | 2011-08-21 10:46:59 -0700 | [diff] [blame] | 127 | ASSERT_TRUE(c == a->GetClass()); |
| 128 | |
| 129 | c = class_linker_->FindSystemClass("[[Ljava/lang/Object;"); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 130 | a.reset(Array::Alloc(c, 1)); |
Elliott Hughes | 68f4fa0 | 2011-08-21 10:46:59 -0700 | [diff] [blame] | 131 | ASSERT_TRUE(c == a->GetClass()); |
| 132 | } |
| 133 | |
Elliott Hughes | 710a0cb | 2011-08-16 14:32:37 -0700 | [diff] [blame] | 134 | template<typename ArrayT> |
| 135 | void TestPrimitiveArray(ClassLinker* cl) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 136 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 710a0cb | 2011-08-16 14:32:37 -0700 | [diff] [blame] | 137 | 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 Hughes | 710a0cb | 2011-08-16 14:32:37 -0700 | [diff] [blame] | 150 | Class* aioobe = cl->FindSystemClass("Ljava/lang/ArrayIndexOutOfBoundsException;"); |
| 151 | |
| 152 | EXPECT_EQ(0, a->Get(-1)); |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame^] | 153 | EXPECT_TRUE(soa.Self()->IsExceptionPending()); |
| 154 | EXPECT_EQ(aioobe, soa.Self()->GetException()->GetClass()); |
| 155 | soa.Self()->ClearException(); |
Elliott Hughes | 710a0cb | 2011-08-16 14:32:37 -0700 | [diff] [blame] | 156 | |
| 157 | EXPECT_EQ(0, a->Get(2)); |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame^] | 158 | EXPECT_TRUE(soa.Self()->IsExceptionPending()); |
| 159 | EXPECT_EQ(aioobe, soa.Self()->GetException()->GetClass()); |
| 160 | soa.Self()->ClearException(); |
Elliott Hughes | 710a0cb | 2011-08-16 14:32:37 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | TEST_F(ObjectTest, PrimitiveArray_Boolean_Alloc) { |
| 164 | TestPrimitiveArray<BooleanArray>(class_linker_); |
| 165 | } |
| 166 | TEST_F(ObjectTest, PrimitiveArray_Byte_Alloc) { |
| 167 | TestPrimitiveArray<ByteArray>(class_linker_); |
| 168 | } |
| 169 | TEST_F(ObjectTest, PrimitiveArray_Char_Alloc) { |
| 170 | TestPrimitiveArray<CharArray>(class_linker_); |
| 171 | } |
| 172 | TEST_F(ObjectTest, PrimitiveArray_Double_Alloc) { |
| 173 | TestPrimitiveArray<DoubleArray>(class_linker_); |
| 174 | } |
| 175 | TEST_F(ObjectTest, PrimitiveArray_Float_Alloc) { |
| 176 | TestPrimitiveArray<FloatArray>(class_linker_); |
| 177 | } |
| 178 | TEST_F(ObjectTest, PrimitiveArray_Int_Alloc) { |
| 179 | TestPrimitiveArray<IntArray>(class_linker_); |
| 180 | } |
| 181 | TEST_F(ObjectTest, PrimitiveArray_Long_Alloc) { |
| 182 | TestPrimitiveArray<LongArray>(class_linker_); |
| 183 | } |
| 184 | TEST_F(ObjectTest, PrimitiveArray_Short_Alloc) { |
| 185 | TestPrimitiveArray<ShortArray>(class_linker_); |
| 186 | } |
| 187 | |
Elliott Hughes | b408de7 | 2011-10-04 14:35:05 -0700 | [diff] [blame] | 188 | TEST_F(ObjectTest, CheckAndAllocArrayFromCode) { |
Ian Rogers | b886da8 | 2011-09-23 16:27:54 -0700 | [diff] [blame] | 189 | // pretend we are trying to call 'new char[3]' from String.toCharArray |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 190 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | b886da8 | 2011-09-23 16:27:54 -0700 | [diff] [blame] | 191 | Class* java_util_Arrays = class_linker_->FindSystemClass("Ljava/util/Arrays;"); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 192 | AbstractMethod* sort = java_util_Arrays->FindDirectMethod("sort", "([I)V"); |
Ian Rogers | 9b1a4f4 | 2011-11-14 18:35:10 -0800 | [diff] [blame] | 193 | 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 Rogers | 0eb7d7e | 2012-01-31 21:12:32 -0800 | [diff] [blame] | 199 | Object* array = CheckAndAllocArrayFromCode(type_idx, sort, 3, Thread::Current(), false); |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 200 | 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 Carlstrom | b9edb84 | 2011-08-28 16:31:06 -0700 | [diff] [blame] | 206 | TEST_F(ObjectTest, StaticFieldFromCode) { |
jeffhao | abcfde3 | 2011-09-29 15:05:18 -0700 | [diff] [blame] | 207 | // pretend we are trying to access 'Static.s0' from StaticsFromCode.<clinit> |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 208 | ScopedObjectAccess soa(Thread::Current()); |
| 209 | jobject class_loader = LoadDex("StaticsFromCode"); |
| 210 | const DexFile* dex_file = Runtime::Current()->GetCompileTimeClassPath(class_loader)[0]; |
Brian Carlstrom | 848a4b3 | 2011-09-04 11:29:27 -0700 | [diff] [blame] | 211 | CHECK(dex_file != NULL); |
| 212 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 213 | Class* klass = |
| 214 | class_linker_->FindClass("LStaticsFromCode;", soa.Decode<ClassLoader*>(class_loader)); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 215 | AbstractMethod* clinit = klass->FindDirectMethod("<clinit>", "()V"); |
Ian Rogers | 9b1a4f4 | 2011-11-14 18:35:10 -0800 | [diff] [blame] | 216 | 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 Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 236 | Field* field = FindFieldFromCode(field_idx, clinit, Thread::Current(), StaticObjectRead, |
| 237 | sizeof(Object*)); |
Ian Rogers | ce9eca6 | 2011-10-07 17:11:03 -0700 | [diff] [blame] | 238 | Object* s0 = field->GetObj(NULL); |
jeffhao | abcfde3 | 2011-09-29 15:05:18 -0700 | [diff] [blame] | 239 | EXPECT_EQ(NULL, s0); |
Brian Carlstrom | b9edb84 | 2011-08-28 16:31:06 -0700 | [diff] [blame] | 240 | |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame^] | 241 | SirtRef<CharArray> char_array(soa.Self(), CharArray::Alloc(0)); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 242 | field->SetObj(NULL, char_array.get()); |
| 243 | EXPECT_EQ(char_array.get(), field->GetObj(NULL)); |
Brian Carlstrom | b9edb84 | 2011-08-28 16:31:06 -0700 | [diff] [blame] | 244 | |
Ian Rogers | ce9eca6 | 2011-10-07 17:11:03 -0700 | [diff] [blame] | 245 | field->SetObj(NULL, NULL); |
| 246 | EXPECT_EQ(NULL, field->GetObj(NULL)); |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 247 | |
Brian Carlstrom | b9edb84 | 2011-08-28 16:31:06 -0700 | [diff] [blame] | 248 | // TODO: more exhaustive tests of all 6 cases of Field::*FromCode |
| 249 | } |
| 250 | |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 251 | TEST_F(ObjectTest, String) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 252 | ScopedObjectAccess soa(Thread::Current()); |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 253 | // 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 Wilson | cbe9fc0 | 2011-07-29 18:59:50 -0400 | [diff] [blame] | 274 | |
Jesse Wilson | f7e85a5 | 2011-08-01 18:45:58 -0700 | [diff] [blame] | 275 | TEST_F(ObjectTest, StringEqualsUtf8) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 276 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame^] | 277 | SirtRef<String> string(soa.Self(), String::AllocFromModifiedUtf8("android")); |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 278 | 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 Wilson | f7e85a5 | 2011-08-01 18:45:58 -0700 | [diff] [blame] | 284 | |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame^] | 285 | SirtRef<String> empty(soa.Self(), String::AllocFromModifiedUtf8("")); |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 286 | EXPECT_TRUE(empty->Equals("")); |
| 287 | EXPECT_FALSE(empty->Equals("a")); |
Jesse Wilson | cbe9fc0 | 2011-07-29 18:59:50 -0400 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | TEST_F(ObjectTest, StringEquals) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 291 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame^] | 292 | SirtRef<String> string(soa.Self(), String::AllocFromModifiedUtf8("android")); |
| 293 | SirtRef<String> string_2(soa.Self(), String::AllocFromModifiedUtf8("android")); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 294 | EXPECT_TRUE(string->Equals(string_2.get())); |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 295 | 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 Wilson | cbe9fc0 | 2011-07-29 18:59:50 -0400 | [diff] [blame] | 300 | |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame^] | 301 | SirtRef<String> empty(soa.Self(), String::AllocFromModifiedUtf8("")); |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 302 | EXPECT_TRUE(empty->Equals("")); |
| 303 | EXPECT_FALSE(empty->Equals("a")); |
| 304 | } |
| 305 | |
jeffhao | 0ce1315 | 2012-03-27 19:45:50 -0700 | [diff] [blame] | 306 | TEST_F(ObjectTest, StringLength) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 307 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame^] | 308 | SirtRef<String> string(soa.Self(), String::AllocFromModifiedUtf8("android")); |
jeffhao | 0ce1315 | 2012-03-27 19:45:50 -0700 | [diff] [blame] | 309 | 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 Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 319 | TEST_F(ObjectTest, DescriptorCompare) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 320 | ScopedObjectAccess soa(Thread::Current()); |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 321 | ClassLinker* linker = class_linker_; |
| 322 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 323 | jobject jclass_loader_1 = LoadDex("ProtoCompare"); |
| 324 | jobject jclass_loader_2 = LoadDex("ProtoCompare2"); |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame^] | 325 | 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 Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 327 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 328 | Class* klass1 = linker->FindClass("LProtoCompare;", class_loader_1.get()); |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 329 | ASSERT_TRUE(klass1 != NULL); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 330 | Class* klass2 = linker->FindClass("LProtoCompare2;", class_loader_2.get()); |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 331 | ASSERT_TRUE(klass2 != NULL); |
| 332 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 333 | AbstractMethod* m1_1 = klass1->GetVirtualMethod(0); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 334 | MethodHelper mh(m1_1); |
| 335 | EXPECT_STREQ(mh.GetName(), "m1"); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 336 | AbstractMethod* m2_1 = klass1->GetVirtualMethod(1); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 337 | mh.ChangeMethod(m2_1); |
| 338 | EXPECT_STREQ(mh.GetName(), "m2"); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 339 | AbstractMethod* m3_1 = klass1->GetVirtualMethod(2); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 340 | mh.ChangeMethod(m3_1); |
| 341 | EXPECT_STREQ(mh.GetName(), "m3"); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 342 | AbstractMethod* m4_1 = klass1->GetVirtualMethod(3); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 343 | mh.ChangeMethod(m4_1); |
| 344 | EXPECT_STREQ(mh.GetName(), "m4"); |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 345 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 346 | AbstractMethod* m1_2 = klass2->GetVirtualMethod(0); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 347 | mh.ChangeMethod(m1_2); |
| 348 | EXPECT_STREQ(mh.GetName(), "m1"); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 349 | AbstractMethod* m2_2 = klass2->GetVirtualMethod(1); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 350 | mh.ChangeMethod(m2_2); |
| 351 | EXPECT_STREQ(mh.GetName(), "m2"); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 352 | AbstractMethod* m3_2 = klass2->GetVirtualMethod(2); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 353 | mh.ChangeMethod(m3_2); |
| 354 | EXPECT_STREQ(mh.GetName(), "m3"); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 355 | AbstractMethod* m4_2 = klass2->GetVirtualMethod(3); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 356 | mh.ChangeMethod(m4_2); |
| 357 | EXPECT_STREQ(mh.GetName(), "m4"); |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 358 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 359 | mh.ChangeMethod(m1_1); |
| 360 | MethodHelper mh2(m1_2); |
| 361 | EXPECT_TRUE(mh.HasSameNameAndSignature(&mh2)); |
| 362 | EXPECT_TRUE(mh2.HasSameNameAndSignature(&mh)); |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 363 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 364 | mh.ChangeMethod(m2_1); |
| 365 | mh2.ChangeMethod(m2_2); |
| 366 | EXPECT_TRUE(mh.HasSameNameAndSignature(&mh2)); |
| 367 | EXPECT_TRUE(mh2.HasSameNameAndSignature(&mh)); |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 368 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 369 | mh.ChangeMethod(m3_1); |
| 370 | mh2.ChangeMethod(m3_2); |
| 371 | EXPECT_TRUE(mh.HasSameNameAndSignature(&mh2)); |
| 372 | EXPECT_TRUE(mh2.HasSameNameAndSignature(&mh)); |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 373 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 374 | mh.ChangeMethod(m4_1); |
| 375 | mh2.ChangeMethod(m4_2); |
| 376 | EXPECT_TRUE(mh.HasSameNameAndSignature(&mh2)); |
| 377 | EXPECT_TRUE(mh2.HasSameNameAndSignature(&mh)); |
Jesse Wilson | cbe9fc0 | 2011-07-29 18:59:50 -0400 | [diff] [blame] | 378 | } |
| 379 | |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 380 | |
| 381 | TEST_F(ObjectTest, StringHashCode) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 382 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame^] | 383 | 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 Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 386 | |
| 387 | EXPECT_EQ(0, empty->GetHashCode()); |
| 388 | EXPECT_EQ(65, A->GetHashCode()); |
| 389 | EXPECT_EQ(64578, ABC->GetHashCode()); |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 390 | } |
| 391 | |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 392 | TEST_F(ObjectTest, InstanceOf) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 393 | ScopedObjectAccess soa(Thread::Current()); |
| 394 | jobject jclass_loader = LoadDex("XandY"); |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame^] | 395 | SirtRef<ClassLoader> class_loader(soa.Self(), soa.Decode<ClassLoader*>(jclass_loader)); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 396 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 397 | Class* X = class_linker_->FindClass("LX;", class_loader.get()); |
| 398 | Class* Y = class_linker_->FindClass("LY;", class_loader.get()); |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 399 | ASSERT_TRUE(X != NULL); |
| 400 | ASSERT_TRUE(Y != NULL); |
| 401 | |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame^] | 402 | SirtRef<Object> x(soa.Self(), X->AllocObject()); |
| 403 | SirtRef<Object> y(soa.Self(), Y->AllocObject()); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 404 | ASSERT_TRUE(x.get() != NULL); |
| 405 | ASSERT_TRUE(y.get() != NULL); |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 406 | |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 407 | EXPECT_TRUE(x->InstanceOf(X)); |
| 408 | EXPECT_FALSE(x->InstanceOf(Y)); |
| 409 | EXPECT_TRUE(y->InstanceOf(X)); |
| 410 | EXPECT_TRUE(y->InstanceOf(Y)); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 411 | |
Elliott Hughes | 92f14b2 | 2011-10-06 12:29:54 -0700 | [diff] [blame] | 412 | Class* java_lang_Class = class_linker_->FindSystemClass("Ljava/lang/Class;"); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 413 | Class* Object_array_class = class_linker_->FindSystemClass("[Ljava/lang/Object;"); |
Elliott Hughes | 92f14b2 | 2011-10-06 12:29:54 -0700 | [diff] [blame] | 414 | |
| 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 Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | TEST_F(ObjectTest, IsAssignableFrom) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 427 | ScopedObjectAccess soa(Thread::Current()); |
| 428 | jobject jclass_loader = LoadDex("XandY"); |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame^] | 429 | SirtRef<ClassLoader> class_loader(soa.Self(), soa.Decode<ClassLoader*>(jclass_loader)); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 430 | Class* X = class_linker_->FindClass("LX;", class_loader.get()); |
| 431 | Class* Y = class_linker_->FindClass("LY;", class_loader.get()); |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 432 | |
| 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 Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 437 | |
| 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 Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | TEST_F(ObjectTest, IsAssignableFromArray) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 464 | ScopedObjectAccess soa(Thread::Current()); |
| 465 | jobject jclass_loader = LoadDex("XandY"); |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame^] | 466 | SirtRef<ClassLoader> class_loader(soa.Self(), soa.Decode<ClassLoader*>(jclass_loader)); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 467 | Class* X = class_linker_->FindClass("LX;", class_loader.get()); |
| 468 | Class* Y = class_linker_->FindClass("LY;", class_loader.get()); |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 469 | ASSERT_TRUE(X != NULL); |
| 470 | ASSERT_TRUE(Y != NULL); |
| 471 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 472 | Class* YA = class_linker_->FindClass("[LY;", class_loader.get()); |
| 473 | Class* YAA = class_linker_->FindClass("[[LY;", class_loader.get()); |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 474 | ASSERT_TRUE(YA != NULL); |
| 475 | ASSERT_TRUE(YAA != NULL); |
| 476 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 477 | Class* XAA = class_linker_->FindClass("[[LX;", class_loader.get()); |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 478 | 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 Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 516 | TEST_F(ObjectTest, FindInstanceField) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 517 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame^] | 518 | SirtRef<String> s(soa.Self(), String::AllocFromModifiedUtf8("ABC")); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 519 | ASSERT_TRUE(s.get() != NULL); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 520 | Class* c = s->GetClass(); |
| 521 | ASSERT_TRUE(c != NULL); |
| 522 | |
| 523 | // Wrong type. |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 524 | EXPECT_TRUE(c->FindDeclaredInstanceField("count", "J") == NULL); |
| 525 | EXPECT_TRUE(c->FindInstanceField("count", "J") == NULL); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 526 | |
| 527 | // Wrong name. |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 528 | EXPECT_TRUE(c->FindDeclaredInstanceField("Count", "I") == NULL); |
| 529 | EXPECT_TRUE(c->FindInstanceField("Count", "I") == NULL); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 530 | |
| 531 | // Right name and type. |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 532 | Field* f1 = c->FindDeclaredInstanceField("count", "I"); |
| 533 | Field* f2 = c->FindInstanceField("count", "I"); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 534 | 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 Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 544 | EXPECT_TRUE(c->FindDeclaredInstanceField("count", "I") == NULL); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 545 | // ...but there is an AbstractStringBuilder.count. |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 546 | EXPECT_TRUE(c->FindInstanceField("count", "I") != NULL); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | TEST_F(ObjectTest, FindStaticField) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 550 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame^] | 551 | SirtRef<String> s(soa.Self(), String::AllocFromModifiedUtf8("ABC")); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 552 | ASSERT_TRUE(s.get() != NULL); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 553 | Class* c = s->GetClass(); |
| 554 | ASSERT_TRUE(c != NULL); |
| 555 | |
| 556 | // Wrong type. |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 557 | EXPECT_TRUE(c->FindDeclaredStaticField("CASE_INSENSITIVE_ORDER", "I") == NULL); |
| 558 | EXPECT_TRUE(c->FindStaticField("CASE_INSENSITIVE_ORDER", "I") == NULL); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 559 | |
| 560 | // Wrong name. |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 561 | EXPECT_TRUE(c->FindDeclaredStaticField("cASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;") == NULL); |
| 562 | EXPECT_TRUE(c->FindStaticField("cASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;") == NULL); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 563 | |
| 564 | // Right name and type. |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 565 | Field* f1 = c->FindDeclaredStaticField("CASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;"); |
| 566 | Field* f2 = c->FindStaticField("CASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;"); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 567 | 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 Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 576 | } // namespace art |