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" |
| 23 | #include "scoped_thread_state_change.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"))); |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 38 | EXPECT_TRUE(foo_1->Equals("foo")); |
| 39 | EXPECT_TRUE(foo_2->Equals("foo")); |
| 40 | EXPECT_TRUE(foo_3->Equals("foo")); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 41 | EXPECT_TRUE(foo_1.Get() != NULL); |
| 42 | EXPECT_TRUE(foo_2.Get() != NULL); |
| 43 | EXPECT_EQ(foo_1.Get(), foo_2.Get()); |
| 44 | EXPECT_NE(foo_1.Get(), bar.Get()); |
| 45 | EXPECT_NE(foo_2.Get(), bar.Get()); |
| 46 | EXPECT_NE(foo_3.Get(), bar.Get()); |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 49 | TEST_F(InternTableTest, Size) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 50 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 51 | InternTable t; |
| 52 | EXPECT_EQ(0U, t.Size()); |
| 53 | t.InternStrong(3, "foo"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 54 | StackHandleScope<1> hs(soa.Self()); |
| 55 | Handle<mirror::String> foo( |
| 56 | hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "foo"))); |
| 57 | t.InternWeak(foo.Get()); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 58 | EXPECT_EQ(1U, t.Size()); |
| 59 | t.InternStrong(3, "bar"); |
| 60 | EXPECT_EQ(2U, t.Size()); |
| 61 | } |
| 62 | |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 63 | class TestPredicate { |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 64 | public: |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 65 | bool IsMarked(const mirror::Object* s) const { |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 66 | bool erased = false; |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 67 | for (auto it = expected_.begin(), end = expected_.end(); it != end; ++it) { |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 68 | if (*it == s) { |
| 69 | expected_.erase(it); |
| 70 | erased = true; |
| 71 | break; |
| 72 | } |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 73 | } |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 74 | EXPECT_TRUE(erased); |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 75 | return false; |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 76 | } |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 77 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 78 | void Expect(const mirror::String* s) { |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 79 | expected_.push_back(s); |
| 80 | } |
| 81 | |
| 82 | ~TestPredicate() { |
| 83 | EXPECT_EQ(0U, expected_.size()); |
| 84 | } |
| 85 | |
| 86 | private: |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 87 | mutable std::vector<const mirror::String*> expected_; |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 88 | }; |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 89 | |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 90 | mirror::Object* IsMarkedSweepingCallback(mirror::Object* object, void* arg) { |
Mathieu Chartier | 6aa3df9 | 2013-09-17 15:17:28 -0700 | [diff] [blame] | 91 | if (reinterpret_cast<TestPredicate*>(arg)->IsMarked(object)) { |
| 92 | return object; |
| 93 | } |
| 94 | return nullptr; |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | TEST_F(InternTableTest, SweepInternTableWeaks) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 98 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 99 | InternTable t; |
| 100 | t.InternStrong(3, "foo"); |
| 101 | t.InternStrong(3, "bar"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 102 | StackHandleScope<5> hs(soa.Self()); |
| 103 | Handle<mirror::String> hello( |
| 104 | hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "hello"))); |
| 105 | Handle<mirror::String> world( |
| 106 | hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "world"))); |
| 107 | Handle<mirror::String> s0(hs.NewHandle(t.InternWeak(hello.Get()))); |
| 108 | Handle<mirror::String> s1(hs.NewHandle(t.InternWeak(world.Get()))); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 109 | |
| 110 | EXPECT_EQ(4U, t.Size()); |
| 111 | |
| 112 | // We should traverse only the weaks... |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 113 | TestPredicate p; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 114 | p.Expect(s0.Get()); |
| 115 | p.Expect(s1.Get()); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 116 | { |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 117 | ReaderMutexLock mu(soa.Self(), *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 118 | t.SweepInternTableWeaks(IsMarkedSweepingCallback, &p); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 119 | } |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 120 | |
| 121 | EXPECT_EQ(2U, t.Size()); |
| 122 | |
Elliott Hughes | e5448b5 | 2012-01-18 16:44:06 -0800 | [diff] [blame] | 123 | // Just check that we didn't corrupt the map. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 124 | Handle<mirror::String> still_here( |
| 125 | hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "still here"))); |
| 126 | t.InternWeak(still_here.Get()); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 127 | EXPECT_EQ(3U, t.Size()); |
| 128 | } |
| 129 | |
| 130 | TEST_F(InternTableTest, ContainsWeak) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 131 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 132 | { |
| 133 | // Strongs are never weak. |
| 134 | InternTable t; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 135 | StackHandleScope<2> hs(soa.Self()); |
| 136 | Handle<mirror::String> interned_foo_1(hs.NewHandle(t.InternStrong(3, "foo"))); |
| 137 | EXPECT_FALSE(t.ContainsWeak(interned_foo_1.Get())); |
| 138 | Handle<mirror::String> interned_foo_2(hs.NewHandle(t.InternStrong(3, "foo"))); |
| 139 | EXPECT_FALSE(t.ContainsWeak(interned_foo_2.Get())); |
| 140 | EXPECT_EQ(interned_foo_1.Get(), interned_foo_2.Get()); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | { |
| 144 | // Weaks are always weak. |
| 145 | InternTable t; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 146 | StackHandleScope<4> hs(soa.Self()); |
| 147 | Handle<mirror::String> foo_1( |
| 148 | hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "foo"))); |
| 149 | Handle<mirror::String> foo_2( |
| 150 | hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "foo"))); |
| 151 | EXPECT_NE(foo_1.Get(), foo_2.Get()); |
| 152 | Handle<mirror::String> interned_foo_1(hs.NewHandle(t.InternWeak(foo_1.Get()))); |
| 153 | Handle<mirror::String> interned_foo_2(hs.NewHandle(t.InternWeak(foo_2.Get()))); |
| 154 | EXPECT_TRUE(t.ContainsWeak(interned_foo_2.Get())); |
| 155 | EXPECT_EQ(interned_foo_1.Get(), interned_foo_2.Get()); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | { |
| 159 | // A weak can be promoted to a strong. |
| 160 | InternTable t; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 161 | StackHandleScope<3> hs(soa.Self()); |
| 162 | Handle<mirror::String> foo( |
| 163 | hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "foo"))); |
| 164 | Handle<mirror::String> interned_foo_1(hs.NewHandle(t.InternWeak(foo.Get()))); |
| 165 | EXPECT_TRUE(t.ContainsWeak(interned_foo_1.Get())); |
| 166 | Handle<mirror::String> interned_foo_2(hs.NewHandle(t.InternStrong(3, "foo"))); |
| 167 | EXPECT_FALSE(t.ContainsWeak(interned_foo_2.Get())); |
| 168 | EXPECT_EQ(interned_foo_1.Get(), interned_foo_2.Get()); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | { |
| 172 | // Interning a weak after a strong gets you the strong. |
| 173 | InternTable t; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 174 | StackHandleScope<3> hs(soa.Self()); |
| 175 | Handle<mirror::String> interned_foo_1(hs.NewHandle(t.InternStrong(3, "foo"))); |
| 176 | EXPECT_FALSE(t.ContainsWeak(interned_foo_1.Get())); |
| 177 | Handle<mirror::String> foo( |
| 178 | hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "foo"))); |
| 179 | Handle<mirror::String> interned_foo_2(hs.NewHandle(t.InternWeak(foo.Get()))); |
| 180 | EXPECT_FALSE(t.ContainsWeak(interned_foo_2.Get())); |
| 181 | EXPECT_EQ(interned_foo_1.Get(), interned_foo_2.Get()); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 185 | } // namespace art |