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