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