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 | */ |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 16 | |
Vladimir Marko | 4e7b3c7 | 2021-02-23 18:13:47 +0000 | [diff] [blame] | 17 | #include "intern_table-inl.h" |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 18 | |
Alexey Grebenkin | 21f2364 | 2016-12-02 17:44:54 +0300 | [diff] [blame] | 19 | #include "base/hash_set.h" |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 20 | #include "common_runtime_test.h" |
David Sehr | 0225f8e | 2018-01-31 08:52:24 +0000 | [diff] [blame] | 21 | #include "dex/utf.h" |
Andreas Gampe | c15a2f4 | 2017-04-21 12:09:39 -0700 | [diff] [blame] | 22 | #include "gc_root-inl.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 23 | #include "handle_scope-inl.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 24 | #include "mirror/object.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 25 | #include "mirror/string.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 26 | #include "scoped_thread_state_change-inl.h" |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 27 | |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 28 | namespace art { |
| 29 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 30 | class InternTableTest : public CommonRuntimeTest {}; |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 31 | |
| 32 | TEST_F(InternTableTest, Intern) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 33 | ScopedObjectAccess soa(Thread::Current()); |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 34 | InternTable intern_table; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 35 | StackHandleScope<4> hs(soa.Self()); |
| 36 | Handle<mirror::String> foo_1(hs.NewHandle(intern_table.InternStrong(3, "foo"))); |
| 37 | Handle<mirror::String> foo_2(hs.NewHandle(intern_table.InternStrong(3, "foo"))); |
| 38 | Handle<mirror::String> foo_3( |
| 39 | hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "foo"))); |
| 40 | Handle<mirror::String> bar(hs.NewHandle(intern_table.InternStrong(3, "bar"))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 41 | ASSERT_TRUE(foo_1 != nullptr); |
| 42 | ASSERT_TRUE(foo_2 != nullptr); |
| 43 | ASSERT_TRUE(foo_3 != nullptr); |
| 44 | ASSERT_TRUE(bar != nullptr); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 45 | EXPECT_EQ(foo_1.Get(), foo_2.Get()); |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 46 | EXPECT_TRUE(foo_1->Equals("foo")); |
| 47 | EXPECT_TRUE(foo_2->Equals("foo")); |
| 48 | EXPECT_TRUE(foo_3->Equals("foo")); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 49 | EXPECT_NE(foo_1.Get(), bar.Get()); |
| 50 | EXPECT_NE(foo_2.Get(), bar.Get()); |
| 51 | EXPECT_NE(foo_3.Get(), bar.Get()); |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 54 | TEST_F(InternTableTest, Size) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 55 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 56 | InternTable t; |
| 57 | EXPECT_EQ(0U, t.Size()); |
| 58 | t.InternStrong(3, "foo"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 59 | StackHandleScope<1> hs(soa.Self()); |
| 60 | Handle<mirror::String> foo( |
| 61 | hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "foo"))); |
| 62 | t.InternWeak(foo.Get()); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 63 | EXPECT_EQ(1U, t.Size()); |
| 64 | t.InternStrong(3, "bar"); |
| 65 | EXPECT_EQ(2U, t.Size()); |
| 66 | } |
| 67 | |
Alexey Grebenkin | 21f2364 | 2016-12-02 17:44:54 +0300 | [diff] [blame] | 68 | // Check if table indexes match on 64 and 32 bit machines. |
| 69 | // This is done by ensuring hash values are the same on every machine and limited to 32-bit wide. |
| 70 | // Otherwise cross compilation can cause a table to be filled on host using one indexing algorithm |
| 71 | // and later on a device with different sizeof(size_t) can use another indexing algorithm. |
| 72 | // Thus the table may provide wrong data. |
| 73 | TEST_F(InternTableTest, CrossHash) { |
| 74 | ScopedObjectAccess soa(Thread::Current()); |
| 75 | InternTable t; |
| 76 | |
| 77 | // A string that has a negative hash value. |
Vladimir Marko | 365c020 | 2022-03-22 09:53:31 +0000 | [diff] [blame] | 78 | ObjPtr<mirror::String> str = mirror::String::AllocFromModifiedUtf8(soa.Self(), "00000000"); |
| 79 | // `String::GetHashCode()` ensures that the stored hash is calculated. |
| 80 | int32_t hash = str->GetHashCode(); |
| 81 | ASSERT_LT(hash, 0); |
Alexey Grebenkin | 21f2364 | 2016-12-02 17:44:54 +0300 | [diff] [blame] | 82 | |
| 83 | MutexLock mu(Thread::Current(), *Locks::intern_table_lock_); |
Mathieu Chartier | 8cc418e | 2018-10-31 10:54:30 -0700 | [diff] [blame] | 84 | for (InternTable::Table::InternalTable& table : t.strong_interns_.tables_) { |
Alexey Grebenkin | 21f2364 | 2016-12-02 17:44:54 +0300 | [diff] [blame] | 85 | // The negative hash value shall be 32-bit wide on every host. |
Vladimir Marko | 365c020 | 2022-03-22 09:53:31 +0000 | [diff] [blame] | 86 | ASSERT_TRUE(IsUint<32>(table.set_.hashfn_(GcRoot<mirror::String>(str)))); |
Alexey Grebenkin | 21f2364 | 2016-12-02 17:44:54 +0300 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 90 | class TestPredicate : public IsMarkedVisitor { |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 91 | public: |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 92 | mirror::Object* IsMarked(mirror::Object* s) override REQUIRES_SHARED(Locks::mutator_lock_) { |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 93 | bool erased = false; |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 94 | for (auto it = expected_.begin(), end = expected_.end(); it != end; ++it) { |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 95 | if (*it == s) { |
| 96 | expected_.erase(it); |
| 97 | erased = true; |
| 98 | break; |
| 99 | } |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 100 | } |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 101 | EXPECT_TRUE(erased); |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 102 | return nullptr; |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 103 | } |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 104 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 105 | void Expect(const mirror::String* s) { |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 106 | expected_.push_back(s); |
| 107 | } |
| 108 | |
| 109 | ~TestPredicate() { |
| 110 | EXPECT_EQ(0U, expected_.size()); |
| 111 | } |
| 112 | |
| 113 | private: |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 114 | mutable std::vector<const mirror::String*> expected_; |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 115 | }; |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 116 | |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 117 | TEST_F(InternTableTest, SweepInternTableWeaks) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 118 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 119 | InternTable t; |
| 120 | t.InternStrong(3, "foo"); |
| 121 | t.InternStrong(3, "bar"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 122 | StackHandleScope<5> hs(soa.Self()); |
| 123 | Handle<mirror::String> hello( |
| 124 | hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "hello"))); |
| 125 | Handle<mirror::String> world( |
| 126 | hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "world"))); |
| 127 | Handle<mirror::String> s0(hs.NewHandle(t.InternWeak(hello.Get()))); |
| 128 | Handle<mirror::String> s1(hs.NewHandle(t.InternWeak(world.Get()))); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 129 | |
| 130 | EXPECT_EQ(4U, t.Size()); |
| 131 | |
| 132 | // We should traverse only the weaks... |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 133 | TestPredicate p; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 134 | p.Expect(s0.Get()); |
| 135 | p.Expect(s1.Get()); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 136 | { |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 137 | ReaderMutexLock mu(soa.Self(), *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 138 | t.SweepInternTableWeaks(&p); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 139 | } |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 140 | |
| 141 | EXPECT_EQ(2U, t.Size()); |
| 142 | |
Elliott Hughes | e5448b5 | 2012-01-18 16:44:06 -0800 | [diff] [blame] | 143 | // Just check that we didn't corrupt the map. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 144 | Handle<mirror::String> still_here( |
| 145 | hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "still here"))); |
| 146 | t.InternWeak(still_here.Get()); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 147 | EXPECT_EQ(3U, t.Size()); |
| 148 | } |
| 149 | |
| 150 | TEST_F(InternTableTest, ContainsWeak) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 151 | ScopedObjectAccess soa(Thread::Current()); |
Vladimir Marko | 365c020 | 2022-03-22 09:53:31 +0000 | [diff] [blame] | 152 | auto ContainsWeak = [&](InternTable& t, ObjPtr<mirror::String> s) |
| 153 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 154 | return t.LookupWeak(soa.Self(), s) == s; |
| 155 | }; |
| 156 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 157 | { |
| 158 | // Strongs are never weak. |
| 159 | InternTable t; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 160 | StackHandleScope<2> hs(soa.Self()); |
| 161 | Handle<mirror::String> interned_foo_1(hs.NewHandle(t.InternStrong(3, "foo"))); |
Vladimir Marko | 365c020 | 2022-03-22 09:53:31 +0000 | [diff] [blame] | 162 | EXPECT_FALSE(ContainsWeak(t, interned_foo_1.Get())); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 163 | Handle<mirror::String> interned_foo_2(hs.NewHandle(t.InternStrong(3, "foo"))); |
Vladimir Marko | 365c020 | 2022-03-22 09:53:31 +0000 | [diff] [blame] | 164 | EXPECT_FALSE(ContainsWeak(t, interned_foo_2.Get())); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 165 | EXPECT_EQ(interned_foo_1.Get(), interned_foo_2.Get()); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | { |
| 169 | // Weaks are always weak. |
| 170 | InternTable t; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 171 | StackHandleScope<4> hs(soa.Self()); |
| 172 | Handle<mirror::String> foo_1( |
| 173 | hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "foo"))); |
| 174 | Handle<mirror::String> foo_2( |
| 175 | hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "foo"))); |
| 176 | EXPECT_NE(foo_1.Get(), foo_2.Get()); |
| 177 | Handle<mirror::String> interned_foo_1(hs.NewHandle(t.InternWeak(foo_1.Get()))); |
| 178 | Handle<mirror::String> interned_foo_2(hs.NewHandle(t.InternWeak(foo_2.Get()))); |
Vladimir Marko | 365c020 | 2022-03-22 09:53:31 +0000 | [diff] [blame] | 179 | EXPECT_TRUE(ContainsWeak(t, interned_foo_2.Get())); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 180 | EXPECT_EQ(interned_foo_1.Get(), interned_foo_2.Get()); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | { |
| 184 | // A weak can be promoted to a strong. |
| 185 | InternTable t; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 186 | StackHandleScope<3> hs(soa.Self()); |
| 187 | Handle<mirror::String> foo( |
| 188 | hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "foo"))); |
| 189 | Handle<mirror::String> interned_foo_1(hs.NewHandle(t.InternWeak(foo.Get()))); |
Vladimir Marko | 365c020 | 2022-03-22 09:53:31 +0000 | [diff] [blame] | 190 | EXPECT_TRUE(ContainsWeak(t, interned_foo_1.Get())); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 191 | Handle<mirror::String> interned_foo_2(hs.NewHandle(t.InternStrong(3, "foo"))); |
Vladimir Marko | 365c020 | 2022-03-22 09:53:31 +0000 | [diff] [blame] | 192 | EXPECT_FALSE(ContainsWeak(t, interned_foo_2.Get())); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 193 | EXPECT_EQ(interned_foo_1.Get(), interned_foo_2.Get()); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | { |
| 197 | // Interning a weak after a strong gets you the strong. |
| 198 | InternTable t; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 199 | StackHandleScope<3> hs(soa.Self()); |
| 200 | Handle<mirror::String> interned_foo_1(hs.NewHandle(t.InternStrong(3, "foo"))); |
Vladimir Marko | 365c020 | 2022-03-22 09:53:31 +0000 | [diff] [blame] | 201 | EXPECT_FALSE(ContainsWeak(t, interned_foo_1.Get())); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 202 | Handle<mirror::String> foo( |
| 203 | hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "foo"))); |
| 204 | Handle<mirror::String> interned_foo_2(hs.NewHandle(t.InternWeak(foo.Get()))); |
Vladimir Marko | 365c020 | 2022-03-22 09:53:31 +0000 | [diff] [blame] | 205 | EXPECT_FALSE(ContainsWeak(t, interned_foo_2.Get())); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 206 | EXPECT_EQ(interned_foo_1.Get(), interned_foo_2.Get()); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 210 | TEST_F(InternTableTest, LookupStrong) { |
| 211 | ScopedObjectAccess soa(Thread::Current()); |
| 212 | InternTable intern_table; |
| 213 | StackHandleScope<3> hs(soa.Self()); |
| 214 | Handle<mirror::String> foo(hs.NewHandle(intern_table.InternStrong(3, "foo"))); |
| 215 | Handle<mirror::String> bar(hs.NewHandle(intern_table.InternStrong(3, "bar"))); |
| 216 | Handle<mirror::String> foobar(hs.NewHandle(intern_table.InternStrong(6, "foobar"))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 217 | ASSERT_TRUE(foo != nullptr); |
| 218 | ASSERT_TRUE(bar != nullptr); |
| 219 | ASSERT_TRUE(foobar != nullptr); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 220 | ASSERT_TRUE(foo->Equals("foo")); |
| 221 | ASSERT_TRUE(bar->Equals("bar")); |
| 222 | ASSERT_TRUE(foobar->Equals("foobar")); |
| 223 | ASSERT_NE(foo.Get(), bar.Get()); |
| 224 | ASSERT_NE(foo.Get(), foobar.Get()); |
| 225 | ASSERT_NE(bar.Get(), foobar.Get()); |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 226 | ObjPtr<mirror::String> lookup_foo = intern_table.LookupStrong(soa.Self(), 3, "foo"); |
| 227 | EXPECT_OBJ_PTR_EQ(lookup_foo, foo.Get()); |
| 228 | ObjPtr<mirror::String> lookup_bar = intern_table.LookupStrong(soa.Self(), 3, "bar"); |
| 229 | EXPECT_OBJ_PTR_EQ(lookup_bar, bar.Get()); |
| 230 | ObjPtr<mirror::String> lookup_foobar = intern_table.LookupStrong(soa.Self(), 6, "foobar"); |
| 231 | EXPECT_OBJ_PTR_EQ(lookup_foobar, foobar.Get()); |
| 232 | ObjPtr<mirror::String> lookup_foox = intern_table.LookupStrong(soa.Self(), 4, "foox"); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 233 | EXPECT_TRUE(lookup_foox == nullptr); |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 234 | ObjPtr<mirror::String> lookup_fooba = intern_table.LookupStrong(soa.Self(), 5, "fooba"); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 235 | EXPECT_TRUE(lookup_fooba == nullptr); |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 236 | ObjPtr<mirror::String> lookup_foobaR = intern_table.LookupStrong(soa.Self(), 6, "foobaR"); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 237 | EXPECT_TRUE(lookup_foobaR == nullptr); |
| 238 | // Try a hash conflict. |
| 239 | ASSERT_EQ(ComputeUtf16HashFromModifiedUtf8("foobar", 6), |
| 240 | ComputeUtf16HashFromModifiedUtf8("foobbS", 6)); |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 241 | ObjPtr<mirror::String> lookup_foobbS = intern_table.LookupStrong(soa.Self(), 6, "foobbS"); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 242 | EXPECT_TRUE(lookup_foobbS == nullptr); |
| 243 | } |
| 244 | |
Vladimir Marko | 365c020 | 2022-03-22 09:53:31 +0000 | [diff] [blame] | 245 | TEST_F(InternTableTest, InternStrongFrozenWeak) { |
| 246 | ScopedObjectAccess soa(Thread::Current()); |
| 247 | InternTable intern_table; |
| 248 | StackHandleScope<1> hs(soa.Self()); |
| 249 | Handle<mirror::String> foo( |
| 250 | hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "foo"))); |
| 251 | ASSERT_TRUE(foo != nullptr); |
| 252 | ObjPtr<mirror::String> weak_foo = intern_table.InternWeak(foo.Get()); |
| 253 | ASSERT_TRUE(weak_foo == foo.Get()); |
| 254 | |
| 255 | intern_table.AddNewTable(); |
| 256 | |
| 257 | ObjPtr<mirror::String> strong_foo = intern_table.InternStrong(foo.Get()); |
| 258 | ASSERT_TRUE(strong_foo == foo.Get()); |
| 259 | } |
| 260 | |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 261 | } // namespace art |