blob: d230a01736010b6078a22a45b9c0b5edfeec30b4 [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
39std::vector<const String*> gExpectedWeakStrings;
40bool TestPredicate(const String* s) {
41 bool erased = false;
42 typedef std::vector<const String*>::iterator It; // TODO: C++0x auto
43 for (It it = gExpectedWeakStrings.begin(), end = gExpectedWeakStrings.end(); it != end; ++it) {
44 if (*it == s) {
45 gExpectedWeakStrings.erase(it);
46 erased = true;
47 break;
48 }
49 }
50 EXPECT_TRUE(erased);
51 return true;
52}
53
54TEST_F(InternTableTest, RemoveWeakIf) {
55 InternTable t;
56 t.InternStrong(3, "foo");
57 t.InternStrong(3, "bar");
58 const String* s0 = t.InternWeak(String::AllocFromModifiedUtf8("hello"));
59 const String* s1 = t.InternWeak(String::AllocFromModifiedUtf8("world"));
60
61 EXPECT_EQ(4U, t.Size());
62
63 // We should traverse only the weaks...
64 gExpectedWeakStrings.clear();
65 gExpectedWeakStrings.push_back(s0);
66 gExpectedWeakStrings.push_back(s1);
67 t.RemoveWeakIf(TestPredicate);
68 EXPECT_EQ(0U, gExpectedWeakStrings.size());
69
70 EXPECT_EQ(2U, t.Size());
71
72 // Just check that we didn't corrupt the unordered_multimap.
73 t.InternWeak(String::AllocFromModifiedUtf8("still here"));
74 EXPECT_EQ(3U, t.Size());
75}
76
77TEST_F(InternTableTest, ContainsWeak) {
78 {
79 // Strongs are never weak.
80 InternTable t;
81 const String* foo_1 = t.InternStrong(3, "foo");
82 EXPECT_FALSE(t.ContainsWeak(foo_1));
83 const String* foo_2 = t.InternStrong(3, "foo");
84 EXPECT_FALSE(t.ContainsWeak(foo_2));
85 EXPECT_EQ(foo_1, foo_2);
86 }
87
88 {
89 // Weaks are always weak.
90 InternTable t;
91 const String* foo_1 = t.InternWeak(String::AllocFromModifiedUtf8("foo"));
92 EXPECT_TRUE(t.ContainsWeak(foo_1));
93 const String* foo_2 = t.InternWeak(String::AllocFromModifiedUtf8("foo"));
94 EXPECT_TRUE(t.ContainsWeak(foo_2));
95 EXPECT_EQ(foo_1, foo_2);
96 }
97
98 {
99 // A weak can be promoted to a strong.
100 InternTable t;
101 const String* foo_1 = t.InternWeak(String::AllocFromModifiedUtf8("foo"));
102 EXPECT_TRUE(t.ContainsWeak(foo_1));
103 const String* foo_2 = t.InternStrong(3, "foo");
104 EXPECT_FALSE(t.ContainsWeak(foo_2));
105 EXPECT_EQ(foo_1, foo_2);
106 }
107
108 {
109 // Interning a weak after a strong gets you the strong.
110 InternTable t;
111 const String* foo_1 = t.InternStrong(3, "foo");
112 EXPECT_FALSE(t.ContainsWeak(foo_1));
113 const String* foo_2 = t.InternWeak(String::AllocFromModifiedUtf8("foo"));
114 EXPECT_FALSE(t.ContainsWeak(foo_2));
115 EXPECT_EQ(foo_1, foo_2);
116 }
117}
118
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700119} // namespace art