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