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 | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 39 | class TestPredicate : public InternTable::Predicate { |
| 40 | public: |
| 41 | bool operator()(const String* s) const { |
| 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); |
| 52 | return true; |
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 | |
| 67 | TEST_F(InternTableTest, RemoveWeakIf) { |
| 68 | InternTable t; |
| 69 | t.InternStrong(3, "foo"); |
| 70 | t.InternStrong(3, "bar"); |
| 71 | const String* s0 = t.InternWeak(String::AllocFromModifiedUtf8("hello")); |
| 72 | const String* s1 = t.InternWeak(String::AllocFromModifiedUtf8("world")); |
| 73 | |
| 74 | EXPECT_EQ(4U, t.Size()); |
| 75 | |
| 76 | // We should traverse only the weaks... |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 77 | TestPredicate p; |
| 78 | p.Expect(s0); |
| 79 | p.Expect(s1); |
| 80 | t.RemoveWeakIf(p); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 81 | |
| 82 | EXPECT_EQ(2U, t.Size()); |
| 83 | |
| 84 | // Just check that we didn't corrupt the unordered_multimap. |
| 85 | t.InternWeak(String::AllocFromModifiedUtf8("still here")); |
| 86 | EXPECT_EQ(3U, t.Size()); |
| 87 | } |
| 88 | |
| 89 | TEST_F(InternTableTest, ContainsWeak) { |
| 90 | { |
| 91 | // Strongs are never weak. |
| 92 | InternTable t; |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 93 | String* foo_1 = t.InternStrong(3, "foo"); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 94 | EXPECT_FALSE(t.ContainsWeak(foo_1)); |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 95 | String* foo_2 = t.InternStrong(3, "foo"); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 96 | EXPECT_FALSE(t.ContainsWeak(foo_2)); |
| 97 | EXPECT_EQ(foo_1, foo_2); |
| 98 | } |
| 99 | |
| 100 | { |
| 101 | // Weaks are always weak. |
| 102 | InternTable t; |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 103 | String* foo_1 = t.InternWeak(String::AllocFromModifiedUtf8("foo")); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 104 | EXPECT_TRUE(t.ContainsWeak(foo_1)); |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 105 | String* foo_2 = t.InternWeak(String::AllocFromModifiedUtf8("foo")); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 106 | EXPECT_TRUE(t.ContainsWeak(foo_2)); |
| 107 | EXPECT_EQ(foo_1, foo_2); |
| 108 | } |
| 109 | |
| 110 | { |
| 111 | // A weak can be promoted to a strong. |
| 112 | InternTable t; |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 113 | String* foo_1 = t.InternWeak(String::AllocFromModifiedUtf8("foo")); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 114 | EXPECT_TRUE(t.ContainsWeak(foo_1)); |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 115 | String* foo_2 = t.InternStrong(3, "foo"); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 116 | EXPECT_FALSE(t.ContainsWeak(foo_2)); |
| 117 | EXPECT_EQ(foo_1, foo_2); |
| 118 | } |
| 119 | |
| 120 | { |
| 121 | // Interning a weak after a strong gets you the strong. |
| 122 | InternTable t; |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 123 | String* foo_1 = t.InternStrong(3, "foo"); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 124 | EXPECT_FALSE(t.ContainsWeak(foo_1)); |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 125 | String* foo_2 = t.InternWeak(String::AllocFromModifiedUtf8("foo")); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 126 | EXPECT_FALSE(t.ContainsWeak(foo_2)); |
| 127 | EXPECT_EQ(foo_1, foo_2); |
| 128 | } |
| 129 | } |
| 130 | |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 131 | } // namespace art |