blob: 3f9fab0ac6020c7e8964911da3f178eb26dbdd0e [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;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070014 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 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"));
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 Hughescf4c6c42011-09-01 15:16:42 -070029TEST_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 Hughes410c0c82011-09-01 17:58:25 -070039class 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 Hughescf4c6c42011-09-01 15:16:42 -070050 }
Elliott Hughes410c0c82011-09-01 17:58:25 -070051 EXPECT_TRUE(erased);
52 return true;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070053 }
Elliott Hughes410c0c82011-09-01 17:58:25 -070054
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 Hughescf4c6c42011-09-01 15:16:42 -070066
67TEST_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 Hughes410c0c82011-09-01 17:58:25 -070077 TestPredicate p;
78 p.Expect(s0);
79 p.Expect(s1);
80 t.RemoveWeakIf(p);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070081
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
89TEST_F(InternTableTest, ContainsWeak) {
90 {
91 // Strongs are never weak.
92 InternTable t;
Brian Carlstromc74255f2011-09-11 22:47:39 -070093 String* foo_1 = t.InternStrong(3, "foo");
Elliott Hughescf4c6c42011-09-01 15:16:42 -070094 EXPECT_FALSE(t.ContainsWeak(foo_1));
Brian Carlstromc74255f2011-09-11 22:47:39 -070095 String* foo_2 = t.InternStrong(3, "foo");
Elliott Hughescf4c6c42011-09-01 15:16:42 -070096 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 Carlstromc74255f2011-09-11 22:47:39 -0700103 String* foo_1 = t.InternWeak(String::AllocFromModifiedUtf8("foo"));
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700104 EXPECT_TRUE(t.ContainsWeak(foo_1));
Brian Carlstromc74255f2011-09-11 22:47:39 -0700105 String* foo_2 = t.InternWeak(String::AllocFromModifiedUtf8("foo"));
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700106 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 Carlstromc74255f2011-09-11 22:47:39 -0700113 String* foo_1 = t.InternWeak(String::AllocFromModifiedUtf8("foo"));
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700114 EXPECT_TRUE(t.ContainsWeak(foo_1));
Brian Carlstromc74255f2011-09-11 22:47:39 -0700115 String* foo_2 = t.InternStrong(3, "foo");
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700116 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 Carlstromc74255f2011-09-11 22:47:39 -0700123 String* foo_1 = t.InternStrong(3, "foo");
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700124 EXPECT_FALSE(t.ContainsWeak(foo_1));
Brian Carlstromc74255f2011-09-11 22:47:39 -0700125 String* foo_2 = t.InternWeak(String::AllocFromModifiedUtf8("foo"));
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700126 EXPECT_FALSE(t.ContainsWeak(foo_2));
127 EXPECT_EQ(foo_1, foo_2);
128 }
129}
130
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700131} // namespace art