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