blob: 1698f18612a120145c086fa22c25ff1a86ca8c1c [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) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070027 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes6c1a3942011-08-17 15:00:06 -070028 static const size_t kTableInitial = 10;
29 static const size_t kTableMax = 20;
30 IndirectReferenceTable irt(kTableInitial, kTableMax, kGlobal);
31
32 Class* c = class_linker_->FindSystemClass("Ljava/lang/Object;");
33 ASSERT_TRUE(c != NULL);
Brian Carlstrom1f870082011-08-23 16:02:11 -070034 Object* obj0 = c->AllocObject();
Elliott Hughes6c1a3942011-08-17 15:00:06 -070035 ASSERT_TRUE(obj0 != NULL);
Brian Carlstrom1f870082011-08-23 16:02:11 -070036 Object* obj1 = c->AllocObject();
Elliott Hughes6c1a3942011-08-17 15:00:06 -070037 ASSERT_TRUE(obj1 != NULL);
Brian Carlstrom1f870082011-08-23 16:02:11 -070038 Object* obj2 = c->AllocObject();
Elliott Hughes6c1a3942011-08-17 15:00:06 -070039 ASSERT_TRUE(obj2 != NULL);
Brian Carlstrom1f870082011-08-23 16:02:11 -070040 Object* obj3 = c->AllocObject();
Elliott Hughes6c1a3942011-08-17 15:00:06 -070041 ASSERT_TRUE(obj3 != NULL);
42
43 const uint32_t cookie = IRT_FIRST_SEGMENT;
44
45 IndirectRef iref0 = (IndirectRef) 0x11110;
46 EXPECT_FALSE(irt.Remove(cookie, iref0)) << "unexpectedly successful removal";
47
48 // Add three, check, remove in the order in which they were added.
49 iref0 = irt.Add(cookie, obj0);
50 EXPECT_TRUE(iref0 != NULL);
51 IndirectRef iref1 = irt.Add(cookie, obj1);
52 EXPECT_TRUE(iref1 != NULL);
53 IndirectRef iref2 = irt.Add(cookie, obj2);
54 EXPECT_TRUE(iref2 != NULL);
55
Elliott Hughes73e66f72012-05-09 09:34:45 -070056 irt.Dump(LOG(DEBUG));
Elliott Hughes6c1a3942011-08-17 15:00:06 -070057
58 EXPECT_EQ(obj0, irt.Get(iref0));
59 EXPECT_EQ(obj1, irt.Get(iref1));
60 EXPECT_EQ(obj2, irt.Get(iref2));
61
62 EXPECT_TRUE(irt.Remove(cookie, iref0));
63 EXPECT_TRUE(irt.Remove(cookie, iref1));
64 EXPECT_TRUE(irt.Remove(cookie, iref2));
65
66 // Table should be empty now.
67 EXPECT_EQ(0U, irt.Capacity());
68
69 // Get invalid entry (off the end of the list).
70 EXPECT_EQ(kInvalidIndirectRefObject, irt.Get(iref0));
71
72 // Add three, remove in the opposite order.
73 iref0 = irt.Add(cookie, obj0);
74 EXPECT_TRUE(iref0 != NULL);
75 iref1 = irt.Add(cookie, obj1);
76 EXPECT_TRUE(iref1 != NULL);
77 iref2 = irt.Add(cookie, obj2);
78 EXPECT_TRUE(iref2 != NULL);
79
80 ASSERT_TRUE(irt.Remove(cookie, iref2));
81 ASSERT_TRUE(irt.Remove(cookie, iref1));
82 ASSERT_TRUE(irt.Remove(cookie, iref0));
83
84 // Table should be empty now.
85 ASSERT_EQ(0U, irt.Capacity());
86
87 // Add three, remove middle / middle / bottom / top. (Second attempt
88 // to remove middle should fail.)
89 iref0 = irt.Add(cookie, obj0);
90 EXPECT_TRUE(iref0 != NULL);
91 iref1 = irt.Add(cookie, obj1);
92 EXPECT_TRUE(iref1 != NULL);
93 iref2 = irt.Add(cookie, obj2);
94 EXPECT_TRUE(iref2 != NULL);
95
96 ASSERT_EQ(3U, irt.Capacity());
97
98 ASSERT_TRUE(irt.Remove(cookie, iref1));
99 ASSERT_FALSE(irt.Remove(cookie, iref1));
100
101 // Get invalid entry (from hole).
102 EXPECT_EQ(kInvalidIndirectRefObject, irt.Get(iref1));
103
104 ASSERT_TRUE(irt.Remove(cookie, iref2));
105 ASSERT_TRUE(irt.Remove(cookie, iref0));
106
107 // Table should be empty now.
108 ASSERT_EQ(0U, irt.Capacity());
109
110 // Add four entries. Remove #1, add new entry, verify that table size
111 // is still 4 (i.e. holes are getting filled). Remove #1 and #3, verify
112 // that we delete one and don't hole-compact the other.
113 iref0 = irt.Add(cookie, obj0);
114 EXPECT_TRUE(iref0 != NULL);
115 iref1 = irt.Add(cookie, obj1);
116 EXPECT_TRUE(iref1 != NULL);
117 iref2 = irt.Add(cookie, obj2);
118 EXPECT_TRUE(iref2 != NULL);
119 IndirectRef iref3 = irt.Add(cookie, obj3);
120 EXPECT_TRUE(iref3 != NULL);
121
122 ASSERT_TRUE(irt.Remove(cookie, iref1));
123
124 iref1 = irt.Add(cookie, obj1);
125 EXPECT_TRUE(iref1 != NULL);
126
127 ASSERT_EQ(4U, irt.Capacity()) << "hole not filled";
128
129 ASSERT_TRUE(irt.Remove(cookie, iref1));
130 ASSERT_TRUE(irt.Remove(cookie, iref3));
131
132 ASSERT_EQ(3U, irt.Capacity()) << "should be 3 after two deletions";
133
134 ASSERT_TRUE(irt.Remove(cookie, iref2));
135 ASSERT_TRUE(irt.Remove(cookie, iref0));
136
137 ASSERT_EQ(0U, irt.Capacity()) << "not empty after split remove";
138
139 // Add an entry, remove it, add a new entry, and try to use the original
140 // iref. They have the same slot number but are for different objects.
141 // With the extended checks in place, this should fail.
142 iref0 = irt.Add(cookie, obj0);
143 EXPECT_TRUE(iref0 != NULL);
144 ASSERT_TRUE(irt.Remove(cookie, iref0));
145 iref1 = irt.Add(cookie, obj1);
146 EXPECT_TRUE(iref1 != NULL);
147 ASSERT_FALSE(irt.Remove(cookie, iref0)) << "mismatched del succeeded";
148 ASSERT_TRUE(irt.Remove(cookie, iref1)) << "switched del failed";
149 ASSERT_EQ(0U, irt.Capacity()) << "switching del not empty";
150
151 // Same as above, but with the same object. A more rigorous checker
152 // (e.g. with slot serialization) will catch this.
153 iref0 = irt.Add(cookie, obj0);
154 EXPECT_TRUE(iref0 != NULL);
155 ASSERT_TRUE(irt.Remove(cookie, iref0));
156 iref1 = irt.Add(cookie, obj0);
157 EXPECT_TRUE(iref1 != NULL);
158 if (iref0 != iref1) {
159 // Try 0, should not work.
160 ASSERT_FALSE(irt.Remove(cookie, iref0)) << "temporal del succeeded";
161 }
162 ASSERT_TRUE(irt.Remove(cookie, iref1)) << "temporal cleanup failed";
163 ASSERT_EQ(0U, irt.Capacity()) << "temporal del not empty";
164
165 // NULL isn't a valid iref.
166 ASSERT_EQ(kInvalidIndirectRefObject, irt.Get(NULL));
167
168 // Stale lookup.
169 iref0 = irt.Add(cookie, obj0);
170 EXPECT_TRUE(iref0 != NULL);
171 ASSERT_TRUE(irt.Remove(cookie, iref0));
172 EXPECT_EQ(kInvalidIndirectRefObject, irt.Get(iref0)) << "stale lookup succeeded";
173
174 // Test table resizing.
175 // These ones fit...
176 IndirectRef manyRefs[kTableInitial];
177 for (size_t i = 0; i < kTableInitial; i++) {
178 manyRefs[i] = irt.Add(cookie, obj0);
179 ASSERT_TRUE(manyRefs[i] != NULL) << "Failed adding " << i;
180 }
181 // ...this one causes overflow.
182 iref0 = irt.Add(cookie, obj0);
183 ASSERT_TRUE(iref0 != NULL);
184 ASSERT_EQ(kTableInitial + 1, irt.Capacity());
185
186 for (size_t i = 0; i < kTableInitial; i++) {
187 ASSERT_TRUE(irt.Remove(cookie, manyRefs[i])) << "failed removing " << i;
188 }
189 // Because of removal order, should have 11 entries, 10 of them holes.
190 ASSERT_EQ(kTableInitial + 1, irt.Capacity());
191
192 ASSERT_TRUE(irt.Remove(cookie, iref0)) << "multi-remove final failed";
193
194 ASSERT_EQ(0U, irt.Capacity()) << "multi-del not empty";
195}
196
197} // namespace art