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