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 | |
| 17 | #include "intern_table.h" |
| 18 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 19 | #include "common_runtime_test.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 20 | #include "mirror/object.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 21 | #include "handle_scope-inl.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 22 | #include "mirror/string.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 23 | #include "scoped_thread_state_change-inl.h" |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 24 | |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 25 | namespace art { |
| 26 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 27 | class InternTableTest : public CommonRuntimeTest {}; |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 28 | |
| 29 | TEST_F(InternTableTest, Intern) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 30 | ScopedObjectAccess soa(Thread::Current()); |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 31 | InternTable intern_table; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 32 | StackHandleScope<4> hs(soa.Self()); |
| 33 | Handle<mirror::String> foo_1(hs.NewHandle(intern_table.InternStrong(3, "foo"))); |
| 34 | Handle<mirror::String> foo_2(hs.NewHandle(intern_table.InternStrong(3, "foo"))); |
| 35 | Handle<mirror::String> foo_3( |
| 36 | hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "foo"))); |
| 37 | Handle<mirror::String> bar(hs.NewHandle(intern_table.InternStrong(3, "bar"))); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 38 | ASSERT_TRUE(foo_1.Get() != nullptr); |
| 39 | ASSERT_TRUE(foo_2.Get() != nullptr); |
| 40 | ASSERT_TRUE(foo_3.Get() != nullptr); |
| 41 | ASSERT_TRUE(bar.Get() != nullptr); |
| 42 | EXPECT_EQ(foo_1.Get(), foo_2.Get()); |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 43 | EXPECT_TRUE(foo_1->Equals("foo")); |
| 44 | EXPECT_TRUE(foo_2->Equals("foo")); |
| 45 | EXPECT_TRUE(foo_3->Equals("foo")); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 46 | EXPECT_NE(foo_1.Get(), bar.Get()); |
| 47 | EXPECT_NE(foo_2.Get(), bar.Get()); |
| 48 | EXPECT_NE(foo_3.Get(), bar.Get()); |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 51 | TEST_F(InternTableTest, Size) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 52 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 53 | InternTable t; |
| 54 | EXPECT_EQ(0U, t.Size()); |
| 55 | t.InternStrong(3, "foo"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 56 | StackHandleScope<1> hs(soa.Self()); |
| 57 | Handle<mirror::String> foo( |
| 58 | hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "foo"))); |
| 59 | t.InternWeak(foo.Get()); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 60 | EXPECT_EQ(1U, t.Size()); |
| 61 | t.InternStrong(3, "bar"); |
| 62 | EXPECT_EQ(2U, t.Size()); |
| 63 | } |
| 64 | |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 65 | class TestPredicate : public IsMarkedVisitor { |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 66 | public: |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 67 | mirror::Object* IsMarked(mirror::Object* s) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 68 | bool erased = false; |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 69 | for (auto it = expected_.begin(), end = expected_.end(); it != end; ++it) { |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 70 | if (*it == s) { |
| 71 | expected_.erase(it); |
| 72 | erased = true; |
| 73 | break; |
| 74 | } |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 75 | } |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 76 | EXPECT_TRUE(erased); |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 77 | return nullptr; |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 78 | } |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 79 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 80 | void Expect(const mirror::String* s) { |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 81 | expected_.push_back(s); |
| 82 | } |
| 83 | |
| 84 | ~TestPredicate() { |
| 85 | EXPECT_EQ(0U, expected_.size()); |
| 86 | } |
| 87 | |
| 88 | private: |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 89 | mutable std::vector<const mirror::String*> expected_; |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 90 | }; |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 91 | |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 92 | TEST_F(InternTableTest, SweepInternTableWeaks) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 93 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 94 | InternTable t; |
| 95 | t.InternStrong(3, "foo"); |
| 96 | t.InternStrong(3, "bar"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 97 | StackHandleScope<5> hs(soa.Self()); |
| 98 | Handle<mirror::String> hello( |
| 99 | hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "hello"))); |
| 100 | Handle<mirror::String> world( |
| 101 | hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "world"))); |
| 102 | Handle<mirror::String> s0(hs.NewHandle(t.InternWeak(hello.Get()))); |
| 103 | Handle<mirror::String> s1(hs.NewHandle(t.InternWeak(world.Get()))); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 104 | |
| 105 | EXPECT_EQ(4U, t.Size()); |
| 106 | |
| 107 | // We should traverse only the weaks... |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 108 | TestPredicate p; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 109 | p.Expect(s0.Get()); |
| 110 | p.Expect(s1.Get()); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 111 | { |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 112 | ReaderMutexLock mu(soa.Self(), *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 113 | t.SweepInternTableWeaks(&p); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 114 | } |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 115 | |
| 116 | EXPECT_EQ(2U, t.Size()); |
| 117 | |
Elliott Hughes | e5448b5 | 2012-01-18 16:44:06 -0800 | [diff] [blame] | 118 | // Just check that we didn't corrupt the map. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 119 | Handle<mirror::String> still_here( |
| 120 | hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "still here"))); |
| 121 | t.InternWeak(still_here.Get()); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 122 | EXPECT_EQ(3U, t.Size()); |
| 123 | } |
| 124 | |
| 125 | TEST_F(InternTableTest, ContainsWeak) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 126 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 127 | { |
| 128 | // Strongs are never weak. |
| 129 | InternTable t; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 130 | StackHandleScope<2> hs(soa.Self()); |
| 131 | Handle<mirror::String> interned_foo_1(hs.NewHandle(t.InternStrong(3, "foo"))); |
| 132 | EXPECT_FALSE(t.ContainsWeak(interned_foo_1.Get())); |
| 133 | Handle<mirror::String> interned_foo_2(hs.NewHandle(t.InternStrong(3, "foo"))); |
| 134 | EXPECT_FALSE(t.ContainsWeak(interned_foo_2.Get())); |
| 135 | EXPECT_EQ(interned_foo_1.Get(), interned_foo_2.Get()); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | { |
| 139 | // Weaks are always weak. |
| 140 | InternTable t; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 141 | StackHandleScope<4> hs(soa.Self()); |
| 142 | Handle<mirror::String> foo_1( |
| 143 | hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "foo"))); |
| 144 | Handle<mirror::String> foo_2( |
| 145 | hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "foo"))); |
| 146 | EXPECT_NE(foo_1.Get(), foo_2.Get()); |
| 147 | Handle<mirror::String> interned_foo_1(hs.NewHandle(t.InternWeak(foo_1.Get()))); |
| 148 | Handle<mirror::String> interned_foo_2(hs.NewHandle(t.InternWeak(foo_2.Get()))); |
| 149 | EXPECT_TRUE(t.ContainsWeak(interned_foo_2.Get())); |
| 150 | EXPECT_EQ(interned_foo_1.Get(), interned_foo_2.Get()); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | { |
| 154 | // A weak can be promoted to a strong. |
| 155 | InternTable t; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 156 | StackHandleScope<3> hs(soa.Self()); |
| 157 | Handle<mirror::String> foo( |
| 158 | hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "foo"))); |
| 159 | Handle<mirror::String> interned_foo_1(hs.NewHandle(t.InternWeak(foo.Get()))); |
| 160 | EXPECT_TRUE(t.ContainsWeak(interned_foo_1.Get())); |
| 161 | Handle<mirror::String> interned_foo_2(hs.NewHandle(t.InternStrong(3, "foo"))); |
| 162 | EXPECT_FALSE(t.ContainsWeak(interned_foo_2.Get())); |
| 163 | EXPECT_EQ(interned_foo_1.Get(), interned_foo_2.Get()); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | { |
| 167 | // Interning a weak after a strong gets you the strong. |
| 168 | InternTable t; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 169 | StackHandleScope<3> hs(soa.Self()); |
| 170 | Handle<mirror::String> interned_foo_1(hs.NewHandle(t.InternStrong(3, "foo"))); |
| 171 | EXPECT_FALSE(t.ContainsWeak(interned_foo_1.Get())); |
| 172 | Handle<mirror::String> foo( |
| 173 | hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "foo"))); |
| 174 | Handle<mirror::String> interned_foo_2(hs.NewHandle(t.InternWeak(foo.Get()))); |
| 175 | EXPECT_FALSE(t.ContainsWeak(interned_foo_2.Get())); |
| 176 | EXPECT_EQ(interned_foo_1.Get(), interned_foo_2.Get()); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 180 | TEST_F(InternTableTest, LookupStrong) { |
| 181 | ScopedObjectAccess soa(Thread::Current()); |
| 182 | InternTable intern_table; |
| 183 | StackHandleScope<3> hs(soa.Self()); |
| 184 | Handle<mirror::String> foo(hs.NewHandle(intern_table.InternStrong(3, "foo"))); |
| 185 | Handle<mirror::String> bar(hs.NewHandle(intern_table.InternStrong(3, "bar"))); |
| 186 | Handle<mirror::String> foobar(hs.NewHandle(intern_table.InternStrong(6, "foobar"))); |
| 187 | ASSERT_TRUE(foo.Get() != nullptr); |
| 188 | ASSERT_TRUE(bar.Get() != nullptr); |
| 189 | ASSERT_TRUE(foobar.Get() != nullptr); |
| 190 | ASSERT_TRUE(foo->Equals("foo")); |
| 191 | ASSERT_TRUE(bar->Equals("bar")); |
| 192 | ASSERT_TRUE(foobar->Equals("foobar")); |
| 193 | ASSERT_NE(foo.Get(), bar.Get()); |
| 194 | ASSERT_NE(foo.Get(), foobar.Get()); |
| 195 | ASSERT_NE(bar.Get(), foobar.Get()); |
| 196 | mirror::String* lookup_foo = intern_table.LookupStrong(soa.Self(), 3, "foo"); |
| 197 | EXPECT_EQ(lookup_foo, foo.Get()); |
| 198 | mirror::String* lookup_bar = intern_table.LookupStrong(soa.Self(), 3, "bar"); |
| 199 | EXPECT_EQ(lookup_bar, bar.Get()); |
| 200 | mirror::String* lookup_foobar = intern_table.LookupStrong(soa.Self(), 6, "foobar"); |
| 201 | EXPECT_EQ(lookup_foobar, foobar.Get()); |
| 202 | mirror::String* lookup_foox = intern_table.LookupStrong(soa.Self(), 4, "foox"); |
| 203 | EXPECT_TRUE(lookup_foox == nullptr); |
| 204 | mirror::String* lookup_fooba = intern_table.LookupStrong(soa.Self(), 5, "fooba"); |
| 205 | EXPECT_TRUE(lookup_fooba == nullptr); |
| 206 | mirror::String* lookup_foobaR = intern_table.LookupStrong(soa.Self(), 6, "foobaR"); |
| 207 | EXPECT_TRUE(lookup_foobaR == nullptr); |
| 208 | // Try a hash conflict. |
| 209 | ASSERT_EQ(ComputeUtf16HashFromModifiedUtf8("foobar", 6), |
| 210 | ComputeUtf16HashFromModifiedUtf8("foobbS", 6)); |
| 211 | mirror::String* lookup_foobbS = intern_table.LookupStrong(soa.Self(), 6, "foobbS"); |
| 212 | EXPECT_TRUE(lookup_foobbS == nullptr); |
| 213 | } |
| 214 | |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 215 | } // namespace art |