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