blob: b6ecdc6d010e486055110c0ed7f4f0fc45a925a5 [file] [log] [blame]
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001// 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 Carlstrom7e93b502011-08-04 14:16:22 -07008namespace art {
9
Brian Carlstromf734cf52011-08-17 16:28:14 -070010class InternTableTest : public CommonTest {};
Brian Carlstrom7e93b502011-08-04 14:16:22 -070011
12TEST_F(InternTableTest, Intern) {
13 InternTable intern_table;
Brian Carlstrom40381fb2011-10-19 14:13:40 -070014 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 Carlstrom7e93b502011-08-04 14:16:22 -070018 EXPECT_TRUE(foo_1->Equals("foo"));
19 EXPECT_TRUE(foo_2->Equals("foo"));
20 EXPECT_TRUE(foo_3->Equals("foo"));
Brian Carlstrom40381fb2011-10-19 14:13:40 -070021 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 Carlstrom7e93b502011-08-04 14:16:22 -070027}
28
Elliott Hughescf4c6c42011-09-01 15:16:42 -070029TEST_F(InternTableTest, Size) {
30 InternTable t;
31 EXPECT_EQ(0U, t.Size());
32 t.InternStrong(3, "foo");
Brian Carlstrom40381fb2011-10-19 14:13:40 -070033 SirtRef<String> foo(String::AllocFromModifiedUtf8("foo"));
34 t.InternWeak(foo.get());
Elliott Hughescf4c6c42011-09-01 15:16:42 -070035 EXPECT_EQ(1U, t.Size());
36 t.InternStrong(3, "bar");
37 EXPECT_EQ(2U, t.Size());
38}
39
Elliott Hughesc33a32b2011-10-11 18:18:07 -070040class TestPredicate {
Elliott Hughes410c0c82011-09-01 17:58:25 -070041 public:
Elliott Hughesc33a32b2011-10-11 18:18:07 -070042 bool IsMarked(const Object* s) const {
Elliott Hughes410c0c82011-09-01 17:58:25 -070043 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 Hughescf4c6c42011-09-01 15:16:42 -070051 }
Elliott Hughes410c0c82011-09-01 17:58:25 -070052 EXPECT_TRUE(erased);
Elliott Hughesc33a32b2011-10-11 18:18:07 -070053 return false;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070054 }
Elliott Hughes410c0c82011-09-01 17:58:25 -070055
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 Hughescf4c6c42011-09-01 15:16:42 -070067
Elliott Hughesc33a32b2011-10-11 18:18:07 -070068bool IsMarked(const Object* object, void* arg) {
69 return reinterpret_cast<TestPredicate*>(arg)->IsMarked(object);
70}
71
72TEST_F(InternTableTest, SweepInternTableWeaks) {
Elliott Hughescf4c6c42011-09-01 15:16:42 -070073 InternTable t;
74 t.InternStrong(3, "foo");
75 t.InternStrong(3, "bar");
Brian Carlstrom40381fb2011-10-19 14:13:40 -070076 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 Hughescf4c6c42011-09-01 15:16:42 -070080
81 EXPECT_EQ(4U, t.Size());
82
83 // We should traverse only the weaks...
Elliott Hughes410c0c82011-09-01 17:58:25 -070084 TestPredicate p;
Brian Carlstrom40381fb2011-10-19 14:13:40 -070085 p.Expect(s0.get());
86 p.Expect(s1.get());
Elliott Hughesc33a32b2011-10-11 18:18:07 -070087 t.SweepInternTableWeaks(IsMarked, &p);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070088
89 EXPECT_EQ(2U, t.Size());
90
Elliott Hughese5448b52012-01-18 16:44:06 -080091 // Just check that we didn't corrupt the map.
Brian Carlstrom40381fb2011-10-19 14:13:40 -070092 SirtRef<String> still_here(String::AllocFromModifiedUtf8("still here"));
93 t.InternWeak(still_here.get());
Elliott Hughescf4c6c42011-09-01 15:16:42 -070094 EXPECT_EQ(3U, t.Size());
95}
96
97TEST_F(InternTableTest, ContainsWeak) {
98 {
99 // Strongs are never weak.
100 InternTable t;
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700101 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 Hughescf4c6c42011-09-01 15:16:42 -0700106 }
107
108 {
109 // Weaks are always weak.
110 InternTable t;
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700111 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 Hughescf4c6c42011-09-01 15:16:42 -0700118 }
119
120 {
121 // A weak can be promoted to a strong.
122 InternTable t;
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700123 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 Hughescf4c6c42011-09-01 15:16:42 -0700129 }
130
131 {
132 // Interning a weak after a strong gets you the strong.
133 InternTable t;
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700134 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 Hughescf4c6c42011-09-01 15:16:42 -0700140 }
141}
142
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700143} // namespace art