blob: cea4afd87e272ee3c0445a0aad70ba88622a9c1c [file] [log] [blame]
Elliott Hughes6c1a3942011-08-17 15:00:06 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "common_test.h"
18
19#include "indirect_reference_table.h"
20
Elliott Hughes6c1a3942011-08-17 15:00:06 -070021namespace art {
22
Brian Carlstromf734cf52011-08-17 16:28:14 -070023class IndirectReferenceTableTest : public CommonTest {
Elliott Hughes6c1a3942011-08-17 15:00:06 -070024};
25
26TEST_F(IndirectReferenceTableTest, BasicTest) {
27 static const size_t kTableInitial = 10;
28 static const size_t kTableMax = 20;
29 IndirectReferenceTable irt(kTableInitial, kTableMax, kGlobal);
30
31 Class* c = class_linker_->FindSystemClass("Ljava/lang/Object;");
32 ASSERT_TRUE(c != NULL);
Brian Carlstrom1f870082011-08-23 16:02:11 -070033 Object* obj0 = c->AllocObject();
Elliott Hughes6c1a3942011-08-17 15:00:06 -070034 ASSERT_TRUE(obj0 != NULL);
Brian Carlstrom1f870082011-08-23 16:02:11 -070035 Object* obj1 = c->AllocObject();
Elliott Hughes6c1a3942011-08-17 15:00:06 -070036 ASSERT_TRUE(obj1 != NULL);
Brian Carlstrom1f870082011-08-23 16:02:11 -070037 Object* obj2 = c->AllocObject();
Elliott Hughes6c1a3942011-08-17 15:00:06 -070038 ASSERT_TRUE(obj2 != NULL);
Brian Carlstrom1f870082011-08-23 16:02:11 -070039 Object* obj3 = c->AllocObject();
Elliott Hughes6c1a3942011-08-17 15:00:06 -070040 ASSERT_TRUE(obj3 != NULL);
41
42 const uint32_t cookie = IRT_FIRST_SEGMENT;
43
44 IndirectRef iref0 = (IndirectRef) 0x11110;
45 EXPECT_FALSE(irt.Remove(cookie, iref0)) << "unexpectedly successful removal";
46
47 // Add three, check, remove in the order in which they were added.
48 iref0 = irt.Add(cookie, obj0);
49 EXPECT_TRUE(iref0 != NULL);
50 IndirectRef iref1 = irt.Add(cookie, obj1);
51 EXPECT_TRUE(iref1 != NULL);
52 IndirectRef iref2 = irt.Add(cookie, obj2);
53 EXPECT_TRUE(iref2 != NULL);
54
55 irt.Dump();
56
57 EXPECT_EQ(obj0, irt.Get(iref0));
58 EXPECT_EQ(obj1, irt.Get(iref1));
59 EXPECT_EQ(obj2, irt.Get(iref2));
60
61 EXPECT_TRUE(irt.Remove(cookie, iref0));
62 EXPECT_TRUE(irt.Remove(cookie, iref1));
63 EXPECT_TRUE(irt.Remove(cookie, iref2));
64
65 // Table should be empty now.
66 EXPECT_EQ(0U, irt.Capacity());
67
68 // Get invalid entry (off the end of the list).
69 EXPECT_EQ(kInvalidIndirectRefObject, irt.Get(iref0));
70
71 // Add three, remove in the opposite order.
72 iref0 = irt.Add(cookie, obj0);
73 EXPECT_TRUE(iref0 != NULL);
74 iref1 = irt.Add(cookie, obj1);
75 EXPECT_TRUE(iref1 != NULL);
76 iref2 = irt.Add(cookie, obj2);
77 EXPECT_TRUE(iref2 != NULL);
78
79 ASSERT_TRUE(irt.Remove(cookie, iref2));
80 ASSERT_TRUE(irt.Remove(cookie, iref1));
81 ASSERT_TRUE(irt.Remove(cookie, iref0));
82
83 // Table should be empty now.
84 ASSERT_EQ(0U, irt.Capacity());
85
86 // Add three, remove middle / middle / bottom / top. (Second attempt
87 // to remove middle should fail.)
88 iref0 = irt.Add(cookie, obj0);
89 EXPECT_TRUE(iref0 != NULL);
90 iref1 = irt.Add(cookie, obj1);
91 EXPECT_TRUE(iref1 != NULL);
92 iref2 = irt.Add(cookie, obj2);
93 EXPECT_TRUE(iref2 != NULL);
94
95 ASSERT_EQ(3U, irt.Capacity());
96
97 ASSERT_TRUE(irt.Remove(cookie, iref1));
98 ASSERT_FALSE(irt.Remove(cookie, iref1));
99
100 // Get invalid entry (from hole).
101 EXPECT_EQ(kInvalidIndirectRefObject, irt.Get(iref1));
102
103 ASSERT_TRUE(irt.Remove(cookie, iref2));
104 ASSERT_TRUE(irt.Remove(cookie, iref0));
105
106 // Table should be empty now.
107 ASSERT_EQ(0U, irt.Capacity());
108
109 // Add four entries. Remove #1, add new entry, verify that table size
110 // is still 4 (i.e. holes are getting filled). Remove #1 and #3, verify
111 // that we delete one and don't hole-compact the other.
112 iref0 = irt.Add(cookie, obj0);
113 EXPECT_TRUE(iref0 != NULL);
114 iref1 = irt.Add(cookie, obj1);
115 EXPECT_TRUE(iref1 != NULL);
116 iref2 = irt.Add(cookie, obj2);
117 EXPECT_TRUE(iref2 != NULL);
118 IndirectRef iref3 = irt.Add(cookie, obj3);
119 EXPECT_TRUE(iref3 != NULL);
120
121 ASSERT_TRUE(irt.Remove(cookie, iref1));
122
123 iref1 = irt.Add(cookie, obj1);
124 EXPECT_TRUE(iref1 != NULL);
125
126 ASSERT_EQ(4U, irt.Capacity()) << "hole not filled";
127
128 ASSERT_TRUE(irt.Remove(cookie, iref1));
129 ASSERT_TRUE(irt.Remove(cookie, iref3));
130
131 ASSERT_EQ(3U, irt.Capacity()) << "should be 3 after two deletions";
132
133 ASSERT_TRUE(irt.Remove(cookie, iref2));
134 ASSERT_TRUE(irt.Remove(cookie, iref0));
135
136 ASSERT_EQ(0U, irt.Capacity()) << "not empty after split remove";
137
138 // Add an entry, remove it, add a new entry, and try to use the original
139 // iref. They have the same slot number but are for different objects.
140 // With the extended checks in place, this should fail.
141 iref0 = irt.Add(cookie, obj0);
142 EXPECT_TRUE(iref0 != NULL);
143 ASSERT_TRUE(irt.Remove(cookie, iref0));
144 iref1 = irt.Add(cookie, obj1);
145 EXPECT_TRUE(iref1 != NULL);
146 ASSERT_FALSE(irt.Remove(cookie, iref0)) << "mismatched del succeeded";
147 ASSERT_TRUE(irt.Remove(cookie, iref1)) << "switched del failed";
148 ASSERT_EQ(0U, irt.Capacity()) << "switching del not empty";
149
150 // Same as above, but with the same object. A more rigorous checker
151 // (e.g. with slot serialization) will catch this.
152 iref0 = irt.Add(cookie, obj0);
153 EXPECT_TRUE(iref0 != NULL);
154 ASSERT_TRUE(irt.Remove(cookie, iref0));
155 iref1 = irt.Add(cookie, obj0);
156 EXPECT_TRUE(iref1 != NULL);
157 if (iref0 != iref1) {
158 // Try 0, should not work.
159 ASSERT_FALSE(irt.Remove(cookie, iref0)) << "temporal del succeeded";
160 }
161 ASSERT_TRUE(irt.Remove(cookie, iref1)) << "temporal cleanup failed";
162 ASSERT_EQ(0U, irt.Capacity()) << "temporal del not empty";
163
164 // NULL isn't a valid iref.
165 ASSERT_EQ(kInvalidIndirectRefObject, irt.Get(NULL));
166
167 // Stale lookup.
168 iref0 = irt.Add(cookie, obj0);
169 EXPECT_TRUE(iref0 != NULL);
170 ASSERT_TRUE(irt.Remove(cookie, iref0));
171 EXPECT_EQ(kInvalidIndirectRefObject, irt.Get(iref0)) << "stale lookup succeeded";
172
173 // Test table resizing.
174 // These ones fit...
175 IndirectRef manyRefs[kTableInitial];
176 for (size_t i = 0; i < kTableInitial; i++) {
177 manyRefs[i] = irt.Add(cookie, obj0);
178 ASSERT_TRUE(manyRefs[i] != NULL) << "Failed adding " << i;
179 }
180 // ...this one causes overflow.
181 iref0 = irt.Add(cookie, obj0);
182 ASSERT_TRUE(iref0 != NULL);
183 ASSERT_EQ(kTableInitial + 1, irt.Capacity());
184
185 for (size_t i = 0; i < kTableInitial; i++) {
186 ASSERT_TRUE(irt.Remove(cookie, manyRefs[i])) << "failed removing " << i;
187 }
188 // Because of removal order, should have 11 entries, 10 of them holes.
189 ASSERT_EQ(kTableInitial + 1, irt.Capacity());
190
191 ASSERT_TRUE(irt.Remove(cookie, iref0)) << "multi-remove final failed";
192
193 ASSERT_EQ(0U, irt.Capacity()) << "multi-del not empty";
194}
195
196} // namespace art