summaryrefslogtreecommitdiffstats
path: root/src/indirect_reference_table_test.cc
blob: cea4afd87e272ee3c0445a0aad70ba88622a9c1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "common_test.h"

#include "indirect_reference_table.h"

namespace art {

class IndirectReferenceTableTest : public CommonTest {
};

TEST_F(IndirectReferenceTableTest, BasicTest) {
  static const size_t kTableInitial = 10;
  static const size_t kTableMax = 20;
  IndirectReferenceTable irt(kTableInitial, kTableMax, kGlobal);

  Class* c = class_linker_->FindSystemClass("Ljava/lang/Object;");
  ASSERT_TRUE(c != NULL);
  Object* obj0 = c->AllocObject();
  ASSERT_TRUE(obj0 != NULL);
  Object* obj1 = c->AllocObject();
  ASSERT_TRUE(obj1 != NULL);
  Object* obj2 = c->AllocObject();
  ASSERT_TRUE(obj2 != NULL);
  Object* obj3 = c->AllocObject();
  ASSERT_TRUE(obj3 != NULL);

  const uint32_t cookie = IRT_FIRST_SEGMENT;

  IndirectRef iref0 = (IndirectRef) 0x11110;
  EXPECT_FALSE(irt.Remove(cookie, iref0)) << "unexpectedly successful removal";

  // Add three, check, remove in the order in which they were added.
  iref0 = irt.Add(cookie, obj0);
  EXPECT_TRUE(iref0 != NULL);
  IndirectRef iref1 = irt.Add(cookie, obj1);
  EXPECT_TRUE(iref1 != NULL);
  IndirectRef iref2 = irt.Add(cookie, obj2);
  EXPECT_TRUE(iref2 != NULL);

  irt.Dump();

  EXPECT_EQ(obj0, irt.Get(iref0));
  EXPECT_EQ(obj1, irt.Get(iref1));
  EXPECT_EQ(obj2, irt.Get(iref2));

  EXPECT_TRUE(irt.Remove(cookie, iref0));
  EXPECT_TRUE(irt.Remove(cookie, iref1));
  EXPECT_TRUE(irt.Remove(cookie, iref2));

  // Table should be empty now.
  EXPECT_EQ(0U, irt.Capacity());

  // Get invalid entry (off the end of the list).
  EXPECT_EQ(kInvalidIndirectRefObject, irt.Get(iref0));

  // Add three, remove in the opposite order.
  iref0 = irt.Add(cookie, obj0);
  EXPECT_TRUE(iref0 != NULL);
  iref1 = irt.Add(cookie, obj1);
  EXPECT_TRUE(iref1 != NULL);
  iref2 = irt.Add(cookie, obj2);
  EXPECT_TRUE(iref2 != NULL);

  ASSERT_TRUE(irt.Remove(cookie, iref2));
  ASSERT_TRUE(irt.Remove(cookie, iref1));
  ASSERT_TRUE(irt.Remove(cookie, iref0));

  // Table should be empty now.
  ASSERT_EQ(0U, irt.Capacity());

  // Add three, remove middle / middle / bottom / top.  (Second attempt
  // to remove middle should fail.)
  iref0 = irt.Add(cookie, obj0);
  EXPECT_TRUE(iref0 != NULL);
  iref1 = irt.Add(cookie, obj1);
  EXPECT_TRUE(iref1 != NULL);
  iref2 = irt.Add(cookie, obj2);
  EXPECT_TRUE(iref2 != NULL);

  ASSERT_EQ(3U, irt.Capacity());

  ASSERT_TRUE(irt.Remove(cookie, iref1));
  ASSERT_FALSE(irt.Remove(cookie, iref1));

  // Get invalid entry (from hole).
  EXPECT_EQ(kInvalidIndirectRefObject, irt.Get(iref1));

  ASSERT_TRUE(irt.Remove(cookie, iref2));
  ASSERT_TRUE(irt.Remove(cookie, iref0));

  // Table should be empty now.
  ASSERT_EQ(0U, irt.Capacity());

  // Add four entries.  Remove #1, add new entry, verify that table size
  // is still 4 (i.e. holes are getting filled).  Remove #1 and #3, verify
  // that we delete one and don't hole-compact the other.
  iref0 = irt.Add(cookie, obj0);
  EXPECT_TRUE(iref0 != NULL);
  iref1 = irt.Add(cookie, obj1);
  EXPECT_TRUE(iref1 != NULL);
  iref2 = irt.Add(cookie, obj2);
  EXPECT_TRUE(iref2 != NULL);
  IndirectRef iref3 = irt.Add(cookie, obj3);
  EXPECT_TRUE(iref3 != NULL);

  ASSERT_TRUE(irt.Remove(cookie, iref1));

  iref1 = irt.Add(cookie, obj1);
  EXPECT_TRUE(iref1 != NULL);

  ASSERT_EQ(4U, irt.Capacity()) << "hole not filled";

  ASSERT_TRUE(irt.Remove(cookie, iref1));
  ASSERT_TRUE(irt.Remove(cookie, iref3));

  ASSERT_EQ(3U, irt.Capacity()) << "should be 3 after two deletions";

  ASSERT_TRUE(irt.Remove(cookie, iref2));
  ASSERT_TRUE(irt.Remove(cookie, iref0));

  ASSERT_EQ(0U, irt.Capacity()) << "not empty after split remove";

  // Add an entry, remove it, add a new entry, and try to use the original
  // iref.  They have the same slot number but are for different objects.
  // With the extended checks in place, this should fail.
  iref0 = irt.Add(cookie, obj0);
  EXPECT_TRUE(iref0 != NULL);
  ASSERT_TRUE(irt.Remove(cookie, iref0));
  iref1 = irt.Add(cookie, obj1);
  EXPECT_TRUE(iref1 != NULL);
  ASSERT_FALSE(irt.Remove(cookie, iref0)) << "mismatched del succeeded";
  ASSERT_TRUE(irt.Remove(cookie, iref1)) << "switched del failed";
  ASSERT_EQ(0U, irt.Capacity()) << "switching del not empty";

  // Same as above, but with the same object.  A more rigorous checker
  // (e.g. with slot serialization) will catch this.
  iref0 = irt.Add(cookie, obj0);
  EXPECT_TRUE(iref0 != NULL);
  ASSERT_TRUE(irt.Remove(cookie, iref0));
  iref1 = irt.Add(cookie, obj0);
  EXPECT_TRUE(iref1 != NULL);
  if (iref0 != iref1) {
    // Try 0, should not work.
    ASSERT_FALSE(irt.Remove(cookie, iref0)) << "temporal del succeeded";
  }
  ASSERT_TRUE(irt.Remove(cookie, iref1)) << "temporal cleanup failed";
  ASSERT_EQ(0U, irt.Capacity()) << "temporal del not empty";

  // NULL isn't a valid iref.
  ASSERT_EQ(kInvalidIndirectRefObject, irt.Get(NULL));

  // Stale lookup.
  iref0 = irt.Add(cookie, obj0);
  EXPECT_TRUE(iref0 != NULL);
  ASSERT_TRUE(irt.Remove(cookie, iref0));
  EXPECT_EQ(kInvalidIndirectRefObject, irt.Get(iref0)) << "stale lookup succeeded";

  // Test table resizing.
  // These ones fit...
  IndirectRef manyRefs[kTableInitial];
  for (size_t i = 0; i < kTableInitial; i++) {
    manyRefs[i] = irt.Add(cookie, obj0);
    ASSERT_TRUE(manyRefs[i] != NULL) << "Failed adding " << i;
  }
  // ...this one causes overflow.
  iref0 = irt.Add(cookie, obj0);
  ASSERT_TRUE(iref0 != NULL);
  ASSERT_EQ(kTableInitial + 1, irt.Capacity());

  for (size_t i = 0; i < kTableInitial; i++) {
    ASSERT_TRUE(irt.Remove(cookie, manyRefs[i])) << "failed removing " << i;
  }
  // Because of removal order, should have 11 entries, 10 of them holes.
  ASSERT_EQ(kTableInitial + 1, irt.Capacity());

  ASSERT_TRUE(irt.Remove(cookie, iref0)) << "multi-remove final failed";

  ASSERT_EQ(0U, irt.Capacity()) << "multi-del not empty";
}

}  // namespace art