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 | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 22 | #include "array-inl.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 23 | #include "art_field-inl.h" |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 24 | #include "asm_support.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 25 | #include "class-inl.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 26 | #include "class_linker.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 27 | #include "class_linker-inl.h" |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 28 | #include "common_runtime_test.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 29 | #include "dex_file.h" |
Ian Rogers | 7655f29 | 2013-07-29 11:07:13 -0700 | [diff] [blame] | 30 | #include "entrypoints/entrypoint_utils.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 31 | #include "gc/accounting/card_table-inl.h" |
| 32 | #include "gc/heap.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 33 | #include "iftable-inl.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 34 | #include "art_method-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 35 | #include "object-inl.h" |
| 36 | #include "object_array-inl.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 37 | #include "handle_scope-inl.h" |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 38 | #include "string-inl.h" |
Ian Rogers | 507dfdd | 2014-05-15 16:42:40 -0700 | [diff] [blame] | 39 | #include "UniquePtrCompat.h" |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 40 | |
| 41 | namespace art { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 42 | namespace mirror { |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 43 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 44 | class ObjectTest : public CommonRuntimeTest { |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 45 | protected: |
Brian Carlstrom | 0a02d12 | 2013-09-06 15:42:40 -0700 | [diff] [blame] | 46 | void AssertString(int32_t expected_utf16_length, |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 47 | const char* utf8_in, |
| 48 | const char* utf16_expected_le, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 49 | int32_t expected_hash) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 50 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | 0a02d12 | 2013-09-06 15:42:40 -0700 | [diff] [blame] | 51 | UniquePtr<uint16_t[]> utf16_expected(new uint16_t[expected_utf16_length]); |
| 52 | for (int32_t i = 0; i < expected_utf16_length; i++) { |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 53 | uint16_t ch = (((utf16_expected_le[i*2 + 0] & 0xff) << 8) | |
| 54 | ((utf16_expected_le[i*2 + 1] & 0xff) << 0)); |
| 55 | utf16_expected[i] = ch; |
| 56 | } |
| 57 | |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 58 | Thread* self = Thread::Current(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 59 | StackHandleScope<1> hs(self); |
| 60 | Handle<String> string( |
| 61 | hs.NewHandle(String::AllocFromModifiedUtf8(self, expected_utf16_length, utf8_in))); |
Brian Carlstrom | 0a02d12 | 2013-09-06 15:42:40 -0700 | [diff] [blame] | 62 | ASSERT_EQ(expected_utf16_length, string->GetLength()); |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 63 | ASSERT_TRUE(string->GetCharArray() != NULL); |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 64 | ASSERT_TRUE(string->GetCharArray()->GetData() != NULL); |
Brian Carlstrom | 0a02d12 | 2013-09-06 15:42:40 -0700 | [diff] [blame] | 65 | // 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)); |
| 67 | ASSERT_TRUE(string->Equals(StringPiece(utf8_in)) || (expected_utf16_length == 1 && strlen(utf8_in) == 0)); |
| 68 | for (int32_t i = 0; i < expected_utf16_length; i++) { |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 69 | EXPECT_EQ(utf16_expected[i], string->CharAt(i)); |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 70 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 71 | EXPECT_EQ(expected_hash, string->GetHashCode()); |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 72 | } |
| 73 | }; |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 74 | |
buzbee | fc9e6fa | 2012-03-23 15:14:29 -0700 | [diff] [blame] | 75 | // Keep the assembly code in sync |
| 76 | TEST_F(ObjectTest, AsmConstants) { |
Ian Rogers | a9a8254 | 2013-10-04 11:17:26 -0700 | [diff] [blame] | 77 | EXPECT_EQ(CLASS_OFFSET, Object::ClassOffset().Int32Value()); |
| 78 | EXPECT_EQ(LOCK_WORD_OFFSET, Object::MonitorOffset().Int32Value()); |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 79 | |
Ian Rogers | a9a8254 | 2013-10-04 11:17:26 -0700 | [diff] [blame] | 80 | EXPECT_EQ(CLASS_COMPONENT_TYPE_OFFSET, Class::ComponentTypeOffset().Int32Value()); |
| 81 | |
| 82 | EXPECT_EQ(ARRAY_LENGTH_OFFSET, Array::LengthOffset().Int32Value()); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 83 | EXPECT_EQ(OBJECT_ARRAY_DATA_OFFSET, Array::DataOffset(sizeof(HeapReference<Object>)).Int32Value()); |
Ian Rogers | a9a8254 | 2013-10-04 11:17:26 -0700 | [diff] [blame] | 84 | |
| 85 | EXPECT_EQ(STRING_VALUE_OFFSET, String::ValueOffset().Int32Value()); |
| 86 | EXPECT_EQ(STRING_COUNT_OFFSET, String::CountOffset().Int32Value()); |
| 87 | EXPECT_EQ(STRING_OFFSET_OFFSET, String::OffsetOffset().Int32Value()); |
| 88 | EXPECT_EQ(STRING_DATA_OFFSET, Array::DataOffset(sizeof(uint16_t)).Int32Value()); |
| 89 | |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 90 | EXPECT_EQ(METHOD_DEX_CACHE_METHODS_OFFSET, ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 91 | EXPECT_EQ(METHOD_PORTABLE_CODE_OFFSET, ArtMethod::EntryPointFromPortableCompiledCodeOffset().Int32Value()); |
| 92 | EXPECT_EQ(METHOD_QUICK_CODE_OFFSET, ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()); |
buzbee | fc9e6fa | 2012-03-23 15:14:29 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 95 | TEST_F(ObjectTest, IsInSamePackage) { |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 96 | // Matches |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 97 | EXPECT_TRUE(Class::IsInSamePackage("Ljava/lang/Object;", "Ljava/lang/Class;")); |
| 98 | EXPECT_TRUE(Class::IsInSamePackage("LFoo;", "LBar;")); |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 99 | |
| 100 | // Mismatches |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 101 | EXPECT_FALSE(Class::IsInSamePackage("Ljava/lang/Object;", "Ljava/io/File;")); |
| 102 | EXPECT_FALSE(Class::IsInSamePackage("Ljava/lang/Object;", "Ljava/lang/reflect/Method;")); |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Elliott Hughes | 081be7f | 2011-09-18 16:50:26 -0700 | [diff] [blame] | 105 | TEST_F(ObjectTest, Clone) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 106 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 107 | StackHandleScope<1> hs(soa.Self()); |
| 108 | Handle<ObjectArray<Object>> a1( |
| 109 | hs.NewHandle(class_linker_->AllocObjectArray<Object>(soa.Self(), 256))); |
Elliott Hughes | 081be7f | 2011-09-18 16:50:26 -0700 | [diff] [blame] | 110 | size_t s1 = a1->SizeOf(); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 111 | Object* clone = a1->Clone(soa.Self()); |
Elliott Hughes | 081be7f | 2011-09-18 16:50:26 -0700 | [diff] [blame] | 112 | EXPECT_EQ(s1, clone->SizeOf()); |
| 113 | EXPECT_TRUE(clone->GetClass() == a1->GetClass()); |
| 114 | } |
| 115 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 116 | TEST_F(ObjectTest, AllocObjectArray) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 117 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 118 | StackHandleScope<1> hs(soa.Self()); |
| 119 | Handle<ObjectArray<Object> > oa( |
| 120 | hs.NewHandle(class_linker_->AllocObjectArray<Object>(soa.Self(), 2))); |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 121 | EXPECT_EQ(2, oa->GetLength()); |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 122 | EXPECT_TRUE(oa->Get(0) == NULL); |
| 123 | EXPECT_TRUE(oa->Get(1) == NULL); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 124 | oa->Set<false>(0, oa.Get()); |
| 125 | EXPECT_TRUE(oa->Get(0) == oa.Get()); |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 126 | EXPECT_TRUE(oa->Get(1) == NULL); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 127 | oa->Set<false>(1, oa.Get()); |
| 128 | EXPECT_TRUE(oa->Get(0) == oa.Get()); |
| 129 | EXPECT_TRUE(oa->Get(1) == oa.Get()); |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 130 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 131 | Class* aioobe = class_linker_->FindSystemClass(soa.Self(), |
| 132 | "Ljava/lang/ArrayIndexOutOfBoundsException;"); |
Elliott Hughes | 710a0cb | 2011-08-16 14:32:37 -0700 | [diff] [blame] | 133 | |
| 134 | EXPECT_TRUE(oa->Get(-1) == NULL); |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 135 | EXPECT_TRUE(soa.Self()->IsExceptionPending()); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 136 | EXPECT_EQ(aioobe, soa.Self()->GetException(NULL)->GetClass()); |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 137 | soa.Self()->ClearException(); |
Elliott Hughes | 710a0cb | 2011-08-16 14:32:37 -0700 | [diff] [blame] | 138 | |
| 139 | EXPECT_TRUE(oa->Get(2) == NULL); |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 140 | EXPECT_TRUE(soa.Self()->IsExceptionPending()); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 141 | EXPECT_EQ(aioobe, soa.Self()->GetException(NULL)->GetClass()); |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 142 | soa.Self()->ClearException(); |
Elliott Hughes | 710a0cb | 2011-08-16 14:32:37 -0700 | [diff] [blame] | 143 | |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 144 | ASSERT_TRUE(oa->GetClass() != NULL); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 145 | ClassHelper oa_ch(oa->GetClass()); |
Ian Rogers | d24e264 | 2012-06-06 21:21:43 -0700 | [diff] [blame] | 146 | ASSERT_EQ(2U, oa_ch.NumDirectInterfaces()); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 147 | EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Cloneable;"), |
| 148 | oa_ch.GetDirectInterface(0)); |
| 149 | EXPECT_EQ(class_linker_->FindSystemClass(soa.Self(), "Ljava/io/Serializable;"), |
| 150 | oa_ch.GetDirectInterface(1)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Elliott Hughes | 68f4fa0 | 2011-08-21 10:46:59 -0700 | [diff] [blame] | 153 | TEST_F(ObjectTest, AllocArray) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 154 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 155 | Class* c = class_linker_->FindSystemClass(soa.Self(), "[I"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 156 | StackHandleScope<1> hs(soa.Self()); |
| 157 | Handle<Array> a( |
| 158 | hs.NewHandle(Array::Alloc<true>(soa.Self(), c, 1, c->GetComponentSize(), |
| 159 | Runtime::Current()->GetHeap()->GetCurrentAllocator()))); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 160 | EXPECT_TRUE(c == a->GetClass()); |
| 161 | EXPECT_EQ(1, a->GetLength()); |
Elliott Hughes | 68f4fa0 | 2011-08-21 10:46:59 -0700 | [diff] [blame] | 162 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 163 | c = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/Object;"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 164 | a.Assign(Array::Alloc<true>(soa.Self(), c, 1, c->GetComponentSize(), |
| 165 | Runtime::Current()->GetHeap()->GetCurrentAllocator())); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 166 | EXPECT_TRUE(c == a->GetClass()); |
| 167 | EXPECT_EQ(1, a->GetLength()); |
Elliott Hughes | 68f4fa0 | 2011-08-21 10:46:59 -0700 | [diff] [blame] | 168 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 169 | c = class_linker_->FindSystemClass(soa.Self(), "[[Ljava/lang/Object;"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 170 | a.Assign(Array::Alloc<true>(soa.Self(), c, 1, c->GetComponentSize(), |
| 171 | Runtime::Current()->GetHeap()->GetCurrentAllocator())); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 172 | EXPECT_TRUE(c == a->GetClass()); |
| 173 | EXPECT_EQ(1, a->GetLength()); |
| 174 | } |
| 175 | |
| 176 | TEST_F(ObjectTest, AllocArray_FillUsable) { |
| 177 | ScopedObjectAccess soa(Thread::Current()); |
| 178 | Class* c = class_linker_->FindSystemClass(soa.Self(), "[B"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 179 | StackHandleScope<1> hs(soa.Self()); |
| 180 | Handle<Array> a( |
| 181 | hs.NewHandle(Array::Alloc<true>(soa.Self(), c, 1, c->GetComponentSize(), |
| 182 | Runtime::Current()->GetHeap()->GetCurrentAllocator(), true))); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 183 | EXPECT_TRUE(c == a->GetClass()); |
| 184 | EXPECT_LE(1, a->GetLength()); |
| 185 | |
| 186 | c = class_linker_->FindSystemClass(soa.Self(), "[I"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 187 | a.Assign(Array::Alloc<true>(soa.Self(), c, 2, c->GetComponentSize(), |
| 188 | Runtime::Current()->GetHeap()->GetCurrentAllocator(), true)); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 189 | EXPECT_TRUE(c == a->GetClass()); |
| 190 | EXPECT_LE(2, a->GetLength()); |
| 191 | |
| 192 | c = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/Object;"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 193 | a.Assign(Array::Alloc<true>(soa.Self(), c, 2, c->GetComponentSize(), |
| 194 | Runtime::Current()->GetHeap()->GetCurrentAllocator(), true)); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 195 | EXPECT_TRUE(c == a->GetClass()); |
| 196 | EXPECT_LE(2, a->GetLength()); |
| 197 | |
| 198 | c = class_linker_->FindSystemClass(soa.Self(), "[[Ljava/lang/Object;"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 199 | a.Assign(Array::Alloc<true>(soa.Self(), c, 2, c->GetComponentSize(), |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 200 | Runtime::Current()->GetHeap()->GetCurrentAllocator(), true)); |
| 201 | EXPECT_TRUE(c == a->GetClass()); |
| 202 | EXPECT_LE(2, a->GetLength()); |
Elliott Hughes | 68f4fa0 | 2011-08-21 10:46:59 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Elliott Hughes | 710a0cb | 2011-08-16 14:32:37 -0700 | [diff] [blame] | 205 | template<typename ArrayT> |
| 206 | void TestPrimitiveArray(ClassLinker* cl) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 207 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 710a0cb | 2011-08-16 14:32:37 -0700 | [diff] [blame] | 208 | typedef typename ArrayT::ElementType T; |
| 209 | |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 210 | ArrayT* a = ArrayT::Alloc(soa.Self(), 2); |
Elliott Hughes | 710a0cb | 2011-08-16 14:32:37 -0700 | [diff] [blame] | 211 | EXPECT_EQ(2, a->GetLength()); |
| 212 | EXPECT_EQ(0, a->Get(0)); |
| 213 | EXPECT_EQ(0, a->Get(1)); |
| 214 | a->Set(0, T(123)); |
| 215 | EXPECT_EQ(T(123), a->Get(0)); |
| 216 | EXPECT_EQ(0, a->Get(1)); |
| 217 | a->Set(1, T(321)); |
| 218 | EXPECT_EQ(T(123), a->Get(0)); |
| 219 | EXPECT_EQ(T(321), a->Get(1)); |
| 220 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 221 | Class* aioobe = cl->FindSystemClass(soa.Self(), "Ljava/lang/ArrayIndexOutOfBoundsException;"); |
Elliott Hughes | 710a0cb | 2011-08-16 14:32:37 -0700 | [diff] [blame] | 222 | |
| 223 | EXPECT_EQ(0, a->Get(-1)); |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 224 | EXPECT_TRUE(soa.Self()->IsExceptionPending()); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 225 | EXPECT_EQ(aioobe, soa.Self()->GetException(NULL)->GetClass()); |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 226 | soa.Self()->ClearException(); |
Elliott Hughes | 710a0cb | 2011-08-16 14:32:37 -0700 | [diff] [blame] | 227 | |
| 228 | EXPECT_EQ(0, a->Get(2)); |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 229 | EXPECT_TRUE(soa.Self()->IsExceptionPending()); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 230 | EXPECT_EQ(aioobe, soa.Self()->GetException(NULL)->GetClass()); |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 231 | soa.Self()->ClearException(); |
Elliott Hughes | 710a0cb | 2011-08-16 14:32:37 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | TEST_F(ObjectTest, PrimitiveArray_Boolean_Alloc) { |
| 235 | TestPrimitiveArray<BooleanArray>(class_linker_); |
| 236 | } |
| 237 | TEST_F(ObjectTest, PrimitiveArray_Byte_Alloc) { |
| 238 | TestPrimitiveArray<ByteArray>(class_linker_); |
| 239 | } |
| 240 | TEST_F(ObjectTest, PrimitiveArray_Char_Alloc) { |
| 241 | TestPrimitiveArray<CharArray>(class_linker_); |
| 242 | } |
| 243 | TEST_F(ObjectTest, PrimitiveArray_Double_Alloc) { |
| 244 | TestPrimitiveArray<DoubleArray>(class_linker_); |
| 245 | } |
| 246 | TEST_F(ObjectTest, PrimitiveArray_Float_Alloc) { |
| 247 | TestPrimitiveArray<FloatArray>(class_linker_); |
| 248 | } |
| 249 | TEST_F(ObjectTest, PrimitiveArray_Int_Alloc) { |
| 250 | TestPrimitiveArray<IntArray>(class_linker_); |
| 251 | } |
| 252 | TEST_F(ObjectTest, PrimitiveArray_Long_Alloc) { |
| 253 | TestPrimitiveArray<LongArray>(class_linker_); |
| 254 | } |
| 255 | TEST_F(ObjectTest, PrimitiveArray_Short_Alloc) { |
| 256 | TestPrimitiveArray<ShortArray>(class_linker_); |
| 257 | } |
| 258 | |
Elliott Hughes | b408de7 | 2011-10-04 14:35:05 -0700 | [diff] [blame] | 259 | TEST_F(ObjectTest, CheckAndAllocArrayFromCode) { |
Ian Rogers | b886da8 | 2011-09-23 16:27:54 -0700 | [diff] [blame] | 260 | // pretend we are trying to call 'new char[3]' from String.toCharArray |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 261 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 262 | Class* java_util_Arrays = class_linker_->FindSystemClass(soa.Self(), "Ljava/util/Arrays;"); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 263 | ArtMethod* sort = java_util_Arrays->FindDirectMethod("sort", "([I)V"); |
Ian Rogers | 9b1a4f4 | 2011-11-14 18:35:10 -0800 | [diff] [blame] | 264 | const DexFile::StringId* string_id = java_lang_dex_file_->FindStringId("[I"); |
| 265 | ASSERT_TRUE(string_id != NULL); |
| 266 | const DexFile::TypeId* type_id = java_lang_dex_file_->FindTypeId( |
| 267 | java_lang_dex_file_->GetIndexForStringId(*string_id)); |
| 268 | ASSERT_TRUE(type_id != NULL); |
| 269 | uint32_t type_idx = java_lang_dex_file_->GetIndexForTypeId(*type_id); |
Hiroshi Yamauchi | e4e23c0 | 2013-12-06 13:38:43 -0800 | [diff] [blame] | 270 | Object* array = CheckAndAllocArrayFromCodeInstrumented(type_idx, sort, 3, Thread::Current(), false, |
| 271 | Runtime::Current()->GetHeap()->GetCurrentAllocator()); |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 272 | EXPECT_TRUE(array->IsArrayInstance()); |
| 273 | EXPECT_EQ(3, array->AsArray()->GetLength()); |
| 274 | EXPECT_TRUE(array->GetClass()->IsArrayClass()); |
| 275 | EXPECT_TRUE(array->GetClass()->GetComponentType()->IsPrimitive()); |
| 276 | } |
| 277 | |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 278 | TEST_F(ObjectTest, CreateMultiArray) { |
| 279 | ScopedObjectAccess soa(Thread::Current()); |
| 280 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 281 | StackHandleScope<2> hs(soa.Self()); |
| 282 | Handle<Class> c(hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "I"))); |
| 283 | Handle<IntArray> dims(hs.NewHandle(IntArray::Alloc(soa.Self(), 1))); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 284 | dims->Set<false>(0, 1); |
Mathieu Chartier | 5bb9903 | 2014-02-08 16:20:58 -0800 | [diff] [blame] | 285 | Array* multi = Array::CreateMultiArray(soa.Self(), c, dims); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 286 | EXPECT_TRUE(multi->GetClass() == class_linker_->FindSystemClass(soa.Self(), "[I")); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 287 | EXPECT_EQ(1, multi->GetLength()); |
| 288 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 289 | dims->Set<false>(0, -1); |
Mathieu Chartier | 5bb9903 | 2014-02-08 16:20:58 -0800 | [diff] [blame] | 290 | multi = Array::CreateMultiArray(soa.Self(), c, dims); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 291 | EXPECT_TRUE(soa.Self()->IsExceptionPending()); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 292 | EXPECT_EQ(PrettyDescriptor(soa.Self()->GetException(NULL)->GetClass()), |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 293 | "java.lang.NegativeArraySizeException"); |
| 294 | soa.Self()->ClearException(); |
| 295 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 296 | dims.Assign(IntArray::Alloc(soa.Self(), 2)); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 297 | for (int i = 1; i < 20; ++i) { |
| 298 | for (int j = 0; j < 20; ++j) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 299 | dims->Set<false>(0, i); |
| 300 | dims->Set<false>(1, j); |
Mathieu Chartier | 5bb9903 | 2014-02-08 16:20:58 -0800 | [diff] [blame] | 301 | multi = Array::CreateMultiArray(soa.Self(), c, dims); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 302 | EXPECT_TRUE(multi->GetClass() == class_linker_->FindSystemClass(soa.Self(), "[[I")); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 303 | EXPECT_EQ(i, multi->GetLength()); |
| 304 | for (int k = 0; k < i; ++k) { |
| 305 | Array* outer = multi->AsObjectArray<Array>()->Get(k); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 306 | EXPECT_TRUE(outer->GetClass() == class_linker_->FindSystemClass(soa.Self(), "[I")); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 307 | EXPECT_EQ(j, outer->GetLength()); |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | |
Brian Carlstrom | b9edb84 | 2011-08-28 16:31:06 -0700 | [diff] [blame] | 313 | TEST_F(ObjectTest, StaticFieldFromCode) { |
jeffhao | abcfde3 | 2011-09-29 15:05:18 -0700 | [diff] [blame] | 314 | // pretend we are trying to access 'Static.s0' from StaticsFromCode.<clinit> |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 315 | ScopedObjectAccess soa(Thread::Current()); |
| 316 | jobject class_loader = LoadDex("StaticsFromCode"); |
| 317 | const DexFile* dex_file = Runtime::Current()->GetCompileTimeClassPath(class_loader)[0]; |
Brian Carlstrom | 848a4b3 | 2011-09-04 11:29:27 -0700 | [diff] [blame] | 318 | CHECK(dex_file != NULL); |
| 319 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 320 | StackHandleScope<2> hs(soa.Self()); |
| 321 | Handle<mirror::ClassLoader> loader(hs.NewHandle(soa.Decode<ClassLoader*>(class_loader))); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 322 | Class* klass = class_linker_->FindClass(soa.Self(), "LStaticsFromCode;", loader); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 323 | ArtMethod* clinit = klass->FindClassInitializer(); |
Ian Rogers | 9b1a4f4 | 2011-11-14 18:35:10 -0800 | [diff] [blame] | 324 | const DexFile::StringId* klass_string_id = dex_file->FindStringId("LStaticsFromCode;"); |
| 325 | ASSERT_TRUE(klass_string_id != NULL); |
| 326 | const DexFile::TypeId* klass_type_id = dex_file->FindTypeId( |
| 327 | dex_file->GetIndexForStringId(*klass_string_id)); |
| 328 | ASSERT_TRUE(klass_type_id != NULL); |
| 329 | |
| 330 | const DexFile::StringId* type_string_id = dex_file->FindStringId("Ljava/lang/Object;"); |
| 331 | ASSERT_TRUE(type_string_id != NULL); |
| 332 | const DexFile::TypeId* type_type_id = dex_file->FindTypeId( |
| 333 | dex_file->GetIndexForStringId(*type_string_id)); |
| 334 | ASSERT_TRUE(type_type_id != NULL); |
| 335 | |
| 336 | const DexFile::StringId* name_str_id = dex_file->FindStringId("s0"); |
| 337 | ASSERT_TRUE(name_str_id != NULL); |
| 338 | |
| 339 | const DexFile::FieldId* field_id = dex_file->FindFieldId( |
| 340 | *klass_type_id, *name_str_id, *type_type_id); |
| 341 | ASSERT_TRUE(field_id != NULL); |
| 342 | uint32_t field_idx = dex_file->GetIndexForFieldId(*field_id); |
| 343 | |
Sebastien Hertz | d4beb6b | 2013-10-02 17:07:20 +0200 | [diff] [blame] | 344 | ArtField* field = FindFieldFromCode<StaticObjectRead, true>(field_idx, clinit, Thread::Current(), |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 345 | sizeof(HeapReference<Object>)); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 346 | Object* s0 = field->GetObj(klass); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 347 | EXPECT_TRUE(s0 != NULL); |
Brian Carlstrom | b9edb84 | 2011-08-28 16:31:06 -0700 | [diff] [blame] | 348 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 349 | Handle<CharArray> char_array(hs.NewHandle(CharArray::Alloc(soa.Self(), 0))); |
| 350 | field->SetObj<false>(field->GetDeclaringClass(), char_array.Get()); |
| 351 | EXPECT_EQ(char_array.Get(), field->GetObj(klass)); |
Brian Carlstrom | b9edb84 | 2011-08-28 16:31:06 -0700 | [diff] [blame] | 352 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 353 | field->SetObj<false>(field->GetDeclaringClass(), NULL); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 354 | EXPECT_EQ(NULL, field->GetObj(klass)); |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 355 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 356 | // TODO: more exhaustive tests of all 6 cases of ArtField::*FromCode |
Brian Carlstrom | b9edb84 | 2011-08-28 16:31:06 -0700 | [diff] [blame] | 357 | } |
| 358 | |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 359 | TEST_F(ObjectTest, String) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 360 | ScopedObjectAccess soa(Thread::Current()); |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 361 | // Test the empty string. |
| 362 | AssertString(0, "", "", 0); |
| 363 | |
| 364 | // Test one-byte characters. |
| 365 | AssertString(1, " ", "\x00\x20", 0x20); |
| 366 | AssertString(1, "", "\x00\x00", 0); |
| 367 | AssertString(1, "\x7f", "\x00\x7f", 0x7f); |
| 368 | AssertString(2, "hi", "\x00\x68\x00\x69", (31 * 0x68) + 0x69); |
| 369 | |
| 370 | // Test two-byte characters. |
| 371 | AssertString(1, "\xc2\x80", "\x00\x80", 0x80); |
| 372 | AssertString(1, "\xd9\xa6", "\x06\x66", 0x0666); |
| 373 | AssertString(1, "\xdf\xbf", "\x07\xff", 0x07ff); |
| 374 | AssertString(3, "h\xd9\xa6i", "\x00\x68\x06\x66\x00\x69", (31 * ((31 * 0x68) + 0x0666)) + 0x69); |
| 375 | |
| 376 | // Test three-byte characters. |
| 377 | AssertString(1, "\xe0\xa0\x80", "\x08\x00", 0x0800); |
| 378 | AssertString(1, "\xe1\x88\xb4", "\x12\x34", 0x1234); |
| 379 | AssertString(1, "\xef\xbf\xbf", "\xff\xff", 0xffff); |
| 380 | AssertString(3, "h\xe1\x88\xb4i", "\x00\x68\x12\x34\x00\x69", (31 * ((31 * 0x68) + 0x1234)) + 0x69); |
| 381 | } |
Jesse Wilson | cbe9fc0 | 2011-07-29 18:59:50 -0400 | [diff] [blame] | 382 | |
Jesse Wilson | f7e85a5 | 2011-08-01 18:45:58 -0700 | [diff] [blame] | 383 | TEST_F(ObjectTest, StringEqualsUtf8) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 384 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 385 | StackHandleScope<2> hs(soa.Self()); |
| 386 | Handle<String> string(hs.NewHandle(String::AllocFromModifiedUtf8(soa.Self(), "android"))); |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 387 | EXPECT_TRUE(string->Equals("android")); |
| 388 | EXPECT_FALSE(string->Equals("Android")); |
| 389 | EXPECT_FALSE(string->Equals("ANDROID")); |
| 390 | EXPECT_FALSE(string->Equals("")); |
| 391 | EXPECT_FALSE(string->Equals("and")); |
| 392 | EXPECT_FALSE(string->Equals("androids")); |
Jesse Wilson | f7e85a5 | 2011-08-01 18:45:58 -0700 | [diff] [blame] | 393 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 394 | Handle<String> empty(hs.NewHandle(String::AllocFromModifiedUtf8(soa.Self(), ""))); |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 395 | EXPECT_TRUE(empty->Equals("")); |
| 396 | EXPECT_FALSE(empty->Equals("a")); |
Jesse Wilson | cbe9fc0 | 2011-07-29 18:59:50 -0400 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | TEST_F(ObjectTest, StringEquals) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 400 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 401 | StackHandleScope<3> hs(soa.Self()); |
| 402 | Handle<String> string(hs.NewHandle(String::AllocFromModifiedUtf8(soa.Self(), "android"))); |
| 403 | Handle<String> string_2(hs.NewHandle(String::AllocFromModifiedUtf8(soa.Self(), "android"))); |
| 404 | EXPECT_TRUE(string->Equals(string_2.Get())); |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 405 | EXPECT_FALSE(string->Equals("Android")); |
| 406 | EXPECT_FALSE(string->Equals("ANDROID")); |
| 407 | EXPECT_FALSE(string->Equals("")); |
| 408 | EXPECT_FALSE(string->Equals("and")); |
| 409 | EXPECT_FALSE(string->Equals("androids")); |
Jesse Wilson | cbe9fc0 | 2011-07-29 18:59:50 -0400 | [diff] [blame] | 410 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 411 | Handle<String> empty(hs.NewHandle(String::AllocFromModifiedUtf8(soa.Self(), ""))); |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 412 | EXPECT_TRUE(empty->Equals("")); |
| 413 | EXPECT_FALSE(empty->Equals("a")); |
| 414 | } |
| 415 | |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 416 | TEST_F(ObjectTest, StringCompareTo) { |
| 417 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 418 | StackHandleScope<5> hs(soa.Self()); |
| 419 | Handle<String> string(hs.NewHandle(String::AllocFromModifiedUtf8(soa.Self(), "android"))); |
| 420 | Handle<String> string_2(hs.NewHandle(String::AllocFromModifiedUtf8(soa.Self(), "android"))); |
| 421 | Handle<String> string_3(hs.NewHandle(String::AllocFromModifiedUtf8(soa.Self(), "Android"))); |
| 422 | Handle<String> string_4(hs.NewHandle(String::AllocFromModifiedUtf8(soa.Self(), "and"))); |
| 423 | Handle<String> string_5(hs.NewHandle(String::AllocFromModifiedUtf8(soa.Self(), ""))); |
| 424 | EXPECT_EQ(0, string->CompareTo(string_2.Get())); |
| 425 | EXPECT_LT(0, string->CompareTo(string_3.Get())); |
| 426 | EXPECT_GT(0, string_3->CompareTo(string.Get())); |
| 427 | EXPECT_LT(0, string->CompareTo(string_4.Get())); |
| 428 | EXPECT_GT(0, string_4->CompareTo(string.Get())); |
| 429 | EXPECT_LT(0, string->CompareTo(string_5.Get())); |
| 430 | EXPECT_GT(0, string_5->CompareTo(string.Get())); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 431 | } |
| 432 | |
jeffhao | 0ce1315 | 2012-03-27 19:45:50 -0700 | [diff] [blame] | 433 | TEST_F(ObjectTest, StringLength) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 434 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 435 | StackHandleScope<1> hs(soa.Self()); |
| 436 | Handle<String> string(hs.NewHandle(String::AllocFromModifiedUtf8(soa.Self(), "android"))); |
jeffhao | 0ce1315 | 2012-03-27 19:45:50 -0700 | [diff] [blame] | 437 | EXPECT_EQ(string->GetLength(), 7); |
| 438 | EXPECT_EQ(string->GetUtfLength(), 7); |
| 439 | |
Sebastien Hertz | ee1d79a | 2014-02-21 15:46:30 +0100 | [diff] [blame] | 440 | string->SetOffset(2); |
| 441 | string->SetCount(5); |
jeffhao | 0ce1315 | 2012-03-27 19:45:50 -0700 | [diff] [blame] | 442 | EXPECT_TRUE(string->Equals("droid")); |
| 443 | EXPECT_EQ(string->GetLength(), 5); |
| 444 | EXPECT_EQ(string->GetUtfLength(), 5); |
| 445 | } |
| 446 | |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 447 | TEST_F(ObjectTest, DescriptorCompare) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 448 | // Two classloaders conflicts in compile_time_class_paths_. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 449 | ScopedObjectAccess soa(Thread::Current()); |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 450 | ClassLinker* linker = class_linker_; |
| 451 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 452 | jobject jclass_loader_1 = LoadDex("ProtoCompare"); |
| 453 | jobject jclass_loader_2 = LoadDex("ProtoCompare2"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 454 | StackHandleScope<2> hs(soa.Self()); |
| 455 | Handle<ClassLoader> class_loader_1(hs.NewHandle(soa.Decode<ClassLoader*>(jclass_loader_1))); |
| 456 | Handle<ClassLoader> class_loader_2(hs.NewHandle(soa.Decode<ClassLoader*>(jclass_loader_2))); |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 457 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 458 | Class* klass1 = linker->FindClass(soa.Self(), "LProtoCompare;", class_loader_1); |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 459 | ASSERT_TRUE(klass1 != NULL); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 460 | Class* klass2 = linker->FindClass(soa.Self(), "LProtoCompare2;", class_loader_2); |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 461 | ASSERT_TRUE(klass2 != NULL); |
| 462 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 463 | ArtMethod* m1_1 = klass1->GetVirtualMethod(0); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 464 | MethodHelper mh(m1_1); |
| 465 | EXPECT_STREQ(mh.GetName(), "m1"); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 466 | ArtMethod* m2_1 = klass1->GetVirtualMethod(1); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 467 | mh.ChangeMethod(m2_1); |
| 468 | EXPECT_STREQ(mh.GetName(), "m2"); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 469 | ArtMethod* m3_1 = klass1->GetVirtualMethod(2); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 470 | mh.ChangeMethod(m3_1); |
| 471 | EXPECT_STREQ(mh.GetName(), "m3"); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 472 | ArtMethod* m4_1 = klass1->GetVirtualMethod(3); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 473 | mh.ChangeMethod(m4_1); |
| 474 | EXPECT_STREQ(mh.GetName(), "m4"); |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 475 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 476 | ArtMethod* m1_2 = klass2->GetVirtualMethod(0); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 477 | mh.ChangeMethod(m1_2); |
| 478 | EXPECT_STREQ(mh.GetName(), "m1"); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 479 | ArtMethod* m2_2 = klass2->GetVirtualMethod(1); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 480 | mh.ChangeMethod(m2_2); |
| 481 | EXPECT_STREQ(mh.GetName(), "m2"); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 482 | ArtMethod* m3_2 = klass2->GetVirtualMethod(2); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 483 | mh.ChangeMethod(m3_2); |
| 484 | EXPECT_STREQ(mh.GetName(), "m3"); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 485 | ArtMethod* m4_2 = klass2->GetVirtualMethod(3); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 486 | mh.ChangeMethod(m4_2); |
| 487 | EXPECT_STREQ(mh.GetName(), "m4"); |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 488 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 489 | mh.ChangeMethod(m1_1); |
| 490 | MethodHelper mh2(m1_2); |
| 491 | EXPECT_TRUE(mh.HasSameNameAndSignature(&mh2)); |
| 492 | EXPECT_TRUE(mh2.HasSameNameAndSignature(&mh)); |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 493 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 494 | mh.ChangeMethod(m2_1); |
| 495 | mh2.ChangeMethod(m2_2); |
| 496 | EXPECT_TRUE(mh.HasSameNameAndSignature(&mh2)); |
| 497 | EXPECT_TRUE(mh2.HasSameNameAndSignature(&mh)); |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 498 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 499 | mh.ChangeMethod(m3_1); |
| 500 | mh2.ChangeMethod(m3_2); |
| 501 | EXPECT_TRUE(mh.HasSameNameAndSignature(&mh2)); |
| 502 | EXPECT_TRUE(mh2.HasSameNameAndSignature(&mh)); |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 503 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 504 | mh.ChangeMethod(m4_1); |
| 505 | mh2.ChangeMethod(m4_2); |
| 506 | EXPECT_TRUE(mh.HasSameNameAndSignature(&mh2)); |
| 507 | EXPECT_TRUE(mh2.HasSameNameAndSignature(&mh)); |
Jesse Wilson | cbe9fc0 | 2011-07-29 18:59:50 -0400 | [diff] [blame] | 508 | } |
| 509 | |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 510 | TEST_F(ObjectTest, StringHashCode) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 511 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 512 | StackHandleScope<3> hs(soa.Self()); |
| 513 | Handle<String> empty(hs.NewHandle(String::AllocFromModifiedUtf8(soa.Self(), ""))); |
| 514 | Handle<String> A(hs.NewHandle(String::AllocFromModifiedUtf8(soa.Self(), "A"))); |
| 515 | Handle<String> ABC(hs.NewHandle(String::AllocFromModifiedUtf8(soa.Self(), "ABC"))); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 516 | |
| 517 | EXPECT_EQ(0, empty->GetHashCode()); |
| 518 | EXPECT_EQ(65, A->GetHashCode()); |
| 519 | EXPECT_EQ(64578, ABC->GetHashCode()); |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 520 | } |
| 521 | |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 522 | TEST_F(ObjectTest, InstanceOf) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 523 | ScopedObjectAccess soa(Thread::Current()); |
| 524 | jobject jclass_loader = LoadDex("XandY"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 525 | StackHandleScope<3> hs(soa.Self()); |
| 526 | Handle<ClassLoader> class_loader(hs.NewHandle(soa.Decode<ClassLoader*>(jclass_loader))); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 527 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 528 | Class* X = class_linker_->FindClass(soa.Self(), "LX;", class_loader); |
| 529 | Class* Y = class_linker_->FindClass(soa.Self(), "LY;", class_loader); |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 530 | ASSERT_TRUE(X != NULL); |
| 531 | ASSERT_TRUE(Y != NULL); |
| 532 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 533 | Handle<Object> x(hs.NewHandle(X->AllocObject(soa.Self()))); |
| 534 | Handle<Object> y(hs.NewHandle(Y->AllocObject(soa.Self()))); |
| 535 | ASSERT_TRUE(x.Get() != NULL); |
| 536 | ASSERT_TRUE(y.Get() != NULL); |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 537 | |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 538 | EXPECT_TRUE(x->InstanceOf(X)); |
| 539 | EXPECT_FALSE(x->InstanceOf(Y)); |
| 540 | EXPECT_TRUE(y->InstanceOf(X)); |
| 541 | EXPECT_TRUE(y->InstanceOf(Y)); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 542 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 543 | Class* java_lang_Class = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Class;"); |
| 544 | Class* Object_array_class = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/Object;"); |
Elliott Hughes | 92f14b2 | 2011-10-06 12:29:54 -0700 | [diff] [blame] | 545 | |
| 546 | EXPECT_FALSE(java_lang_Class->InstanceOf(Object_array_class)); |
| 547 | EXPECT_TRUE(Object_array_class->InstanceOf(java_lang_Class)); |
| 548 | |
| 549 | // All array classes implement Cloneable and Serializable. |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 550 | Object* array = ObjectArray<Object>::Alloc(soa.Self(), Object_array_class, 1); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 551 | Class* java_lang_Cloneable = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Cloneable;"); |
| 552 | Class* java_io_Serializable = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/Serializable;"); |
Elliott Hughes | 92f14b2 | 2011-10-06 12:29:54 -0700 | [diff] [blame] | 553 | EXPECT_TRUE(array->InstanceOf(java_lang_Cloneable)); |
| 554 | EXPECT_TRUE(array->InstanceOf(java_io_Serializable)); |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | TEST_F(ObjectTest, IsAssignableFrom) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 558 | ScopedObjectAccess soa(Thread::Current()); |
| 559 | jobject jclass_loader = LoadDex("XandY"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 560 | StackHandleScope<1> hs(soa.Self()); |
| 561 | Handle<ClassLoader> class_loader(hs.NewHandle(soa.Decode<ClassLoader*>(jclass_loader))); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 562 | Class* X = class_linker_->FindClass(soa.Self(), "LX;", class_loader); |
| 563 | Class* Y = class_linker_->FindClass(soa.Self(), "LY;", class_loader); |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 564 | |
| 565 | EXPECT_TRUE(X->IsAssignableFrom(X)); |
| 566 | EXPECT_TRUE(X->IsAssignableFrom(Y)); |
| 567 | EXPECT_FALSE(Y->IsAssignableFrom(X)); |
| 568 | EXPECT_TRUE(Y->IsAssignableFrom(Y)); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 569 | |
| 570 | // class final String implements CharSequence, .. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 571 | Class* string = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/String;"); |
| 572 | Class* charseq = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/CharSequence;"); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 573 | // Can String be assigned to CharSequence without a cast? |
| 574 | EXPECT_TRUE(charseq->IsAssignableFrom(string)); |
| 575 | // Can CharSequence be assigned to String without a cast? |
| 576 | EXPECT_FALSE(string->IsAssignableFrom(charseq)); |
| 577 | |
| 578 | // Primitive types are only assignable to themselves |
| 579 | const char* prims = "ZBCSIJFD"; |
| 580 | Class* prim_types[strlen(prims)]; |
| 581 | for (size_t i = 0; i < strlen(prims); i++) { |
| 582 | prim_types[i] = class_linker_->FindPrimitiveClass(prims[i]); |
| 583 | } |
| 584 | for (size_t i = 0; i < strlen(prims); i++) { |
| 585 | for (size_t j = 0; i < strlen(prims); i++) { |
| 586 | if (i == j) { |
| 587 | EXPECT_TRUE(prim_types[i]->IsAssignableFrom(prim_types[j])); |
| 588 | } else { |
| 589 | EXPECT_FALSE(prim_types[i]->IsAssignableFrom(prim_types[j])); |
| 590 | } |
| 591 | } |
| 592 | } |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | TEST_F(ObjectTest, IsAssignableFromArray) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 596 | ScopedObjectAccess soa(Thread::Current()); |
| 597 | jobject jclass_loader = LoadDex("XandY"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 598 | StackHandleScope<1> hs(soa.Self()); |
| 599 | Handle<ClassLoader> class_loader(hs.NewHandle(soa.Decode<ClassLoader*>(jclass_loader))); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 600 | Class* X = class_linker_->FindClass(soa.Self(), "LX;", class_loader); |
| 601 | Class* Y = class_linker_->FindClass(soa.Self(), "LY;", class_loader); |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 602 | ASSERT_TRUE(X != NULL); |
| 603 | ASSERT_TRUE(Y != NULL); |
| 604 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 605 | Class* YA = class_linker_->FindClass(soa.Self(), "[LY;", class_loader); |
| 606 | Class* YAA = class_linker_->FindClass(soa.Self(), "[[LY;", class_loader); |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 607 | ASSERT_TRUE(YA != NULL); |
| 608 | ASSERT_TRUE(YAA != NULL); |
| 609 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 610 | Class* XAA = class_linker_->FindClass(soa.Self(), "[[LX;", class_loader); |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 611 | ASSERT_TRUE(XAA != NULL); |
| 612 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 613 | Class* O = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;"); |
| 614 | Class* OA = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/Object;"); |
| 615 | Class* OAA = class_linker_->FindSystemClass(soa.Self(), "[[Ljava/lang/Object;"); |
| 616 | Class* OAAA = class_linker_->FindSystemClass(soa.Self(), "[[[Ljava/lang/Object;"); |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 617 | ASSERT_TRUE(O != NULL); |
| 618 | ASSERT_TRUE(OA != NULL); |
| 619 | ASSERT_TRUE(OAA != NULL); |
| 620 | ASSERT_TRUE(OAAA != NULL); |
| 621 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 622 | Class* S = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/Serializable;"); |
| 623 | Class* SA = class_linker_->FindSystemClass(soa.Self(), "[Ljava/io/Serializable;"); |
| 624 | Class* SAA = class_linker_->FindSystemClass(soa.Self(), "[[Ljava/io/Serializable;"); |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 625 | ASSERT_TRUE(S != NULL); |
| 626 | ASSERT_TRUE(SA != NULL); |
| 627 | ASSERT_TRUE(SAA != NULL); |
| 628 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 629 | Class* IA = class_linker_->FindSystemClass(soa.Self(), "[I"); |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 630 | ASSERT_TRUE(IA != NULL); |
| 631 | |
| 632 | EXPECT_TRUE(YAA->IsAssignableFrom(YAA)); // identity |
| 633 | EXPECT_TRUE(XAA->IsAssignableFrom(YAA)); // element superclass |
| 634 | EXPECT_FALSE(YAA->IsAssignableFrom(XAA)); |
| 635 | EXPECT_FALSE(Y->IsAssignableFrom(YAA)); |
| 636 | EXPECT_FALSE(YA->IsAssignableFrom(YAA)); |
| 637 | EXPECT_TRUE(O->IsAssignableFrom(YAA)); // everything is an Object |
| 638 | EXPECT_TRUE(OA->IsAssignableFrom(YAA)); |
| 639 | EXPECT_TRUE(OAA->IsAssignableFrom(YAA)); |
| 640 | EXPECT_TRUE(S->IsAssignableFrom(YAA)); // all arrays are Serializable |
| 641 | EXPECT_TRUE(SA->IsAssignableFrom(YAA)); |
| 642 | EXPECT_FALSE(SAA->IsAssignableFrom(YAA)); // unless Y was Serializable |
| 643 | |
| 644 | EXPECT_FALSE(IA->IsAssignableFrom(OA)); |
| 645 | EXPECT_FALSE(OA->IsAssignableFrom(IA)); |
| 646 | EXPECT_TRUE(O->IsAssignableFrom(IA)); |
| 647 | } |
| 648 | |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 649 | TEST_F(ObjectTest, FindInstanceField) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 650 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 651 | StackHandleScope<1> hs(soa.Self()); |
| 652 | Handle<String> s(hs.NewHandle(String::AllocFromModifiedUtf8(soa.Self(), "ABC"))); |
| 653 | ASSERT_TRUE(s.Get() != NULL); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 654 | Class* c = s->GetClass(); |
| 655 | ASSERT_TRUE(c != NULL); |
| 656 | |
| 657 | // Wrong type. |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 658 | EXPECT_TRUE(c->FindDeclaredInstanceField("count", "J") == NULL); |
| 659 | EXPECT_TRUE(c->FindInstanceField("count", "J") == NULL); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 660 | |
| 661 | // Wrong name. |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 662 | EXPECT_TRUE(c->FindDeclaredInstanceField("Count", "I") == NULL); |
| 663 | EXPECT_TRUE(c->FindInstanceField("Count", "I") == NULL); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 664 | |
| 665 | // Right name and type. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 666 | ArtField* f1 = c->FindDeclaredInstanceField("count", "I"); |
| 667 | ArtField* f2 = c->FindInstanceField("count", "I"); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 668 | EXPECT_TRUE(f1 != NULL); |
| 669 | EXPECT_TRUE(f2 != NULL); |
| 670 | EXPECT_EQ(f1, f2); |
| 671 | |
| 672 | // TODO: check that s.count == 3. |
| 673 | |
| 674 | // Ensure that we handle superclass fields correctly... |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 675 | c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/StringBuilder;"); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 676 | ASSERT_TRUE(c != NULL); |
| 677 | // No StringBuilder.count... |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 678 | EXPECT_TRUE(c->FindDeclaredInstanceField("count", "I") == NULL); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 679 | // ...but there is an AbstractStringBuilder.count. |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 680 | EXPECT_TRUE(c->FindInstanceField("count", "I") != NULL); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | TEST_F(ObjectTest, FindStaticField) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 684 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 685 | StackHandleScope<1> hs(soa.Self()); |
| 686 | Handle<String> s(hs.NewHandle(String::AllocFromModifiedUtf8(soa.Self(), "ABC"))); |
| 687 | ASSERT_TRUE(s.Get() != NULL); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 688 | Class* c = s->GetClass(); |
| 689 | ASSERT_TRUE(c != NULL); |
| 690 | |
| 691 | // Wrong type. |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 692 | EXPECT_TRUE(c->FindDeclaredStaticField("CASE_INSENSITIVE_ORDER", "I") == NULL); |
| 693 | EXPECT_TRUE(c->FindStaticField("CASE_INSENSITIVE_ORDER", "I") == NULL); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 694 | |
| 695 | // Wrong name. |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 696 | EXPECT_TRUE(c->FindDeclaredStaticField("cASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;") == NULL); |
| 697 | EXPECT_TRUE(c->FindStaticField("cASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;") == NULL); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 698 | |
| 699 | // Right name and type. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 700 | ArtField* f1 = c->FindDeclaredStaticField("CASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;"); |
| 701 | ArtField* f2 = c->FindStaticField("CASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;"); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 702 | EXPECT_TRUE(f1 != NULL); |
| 703 | EXPECT_TRUE(f2 != NULL); |
| 704 | EXPECT_EQ(f1, f2); |
| 705 | |
| 706 | // TODO: test static fields via superclasses. |
| 707 | // TODO: test static fields via interfaces. |
| 708 | // TODO: test that interfaces trump superclasses. |
| 709 | } |
| 710 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 711 | } // namespace mirror |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 712 | } // namespace art |