Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
Mathieu Chartier | c56057e | 2014-05-04 13:18:58 -0700 | [diff] [blame] | 17 | #include "indirect_reference_table-inl.h" |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 18 | |
| 19 | #include "common_runtime_test.h" |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame] | 20 | #include "mirror/object-inl.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 21 | #include "scoped_thread_state_change.h" |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 22 | |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 23 | namespace art { |
| 24 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 25 | class IndirectReferenceTableTest : public CommonRuntimeTest {}; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 26 | |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 27 | static void CheckDump(IndirectReferenceTable* irt, size_t num_objects, size_t num_unique) |
| 28 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 29 | std::ostringstream oss; |
| 30 | irt->Dump(oss); |
| 31 | if (num_objects == 0) { |
| 32 | EXPECT_EQ(oss.str().find("java.lang.Object"), std::string::npos) << oss.str(); |
| 33 | } else if (num_objects == 1) { |
| 34 | EXPECT_NE(oss.str().find("1 of java.lang.Object"), std::string::npos) << oss.str(); |
| 35 | } else { |
| 36 | EXPECT_NE(oss.str().find(StringPrintf("%zd of java.lang.Object (%zd unique instances)", |
| 37 | num_objects, num_unique)), |
| 38 | std::string::npos) |
| 39 | << "\n Expected number of objects: " << num_objects |
| 40 | << "\n Expected unique objects: " << num_unique << "\n" |
| 41 | << oss.str(); |
| 42 | } |
| 43 | } |
| 44 | |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 45 | TEST_F(IndirectReferenceTableTest, BasicTest) { |
Andreas Gampe | 369810a | 2015-01-14 19:53:31 -0800 | [diff] [blame] | 46 | // This will lead to error messages in the log. |
| 47 | ScopedLogSeverity sls(LogSeverity::FATAL); |
| 48 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 49 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 50 | static const size_t kTableInitial = 10; |
| 51 | static const size_t kTableMax = 20; |
| 52 | IndirectReferenceTable irt(kTableInitial, kTableMax, kGlobal); |
| 53 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 54 | mirror::Class* c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;"); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 55 | ASSERT_TRUE(c != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 56 | mirror::Object* obj0 = c->AllocObject(soa.Self()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 57 | ASSERT_TRUE(obj0 != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 58 | mirror::Object* obj1 = c->AllocObject(soa.Self()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 59 | ASSERT_TRUE(obj1 != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 60 | mirror::Object* obj2 = c->AllocObject(soa.Self()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 61 | ASSERT_TRUE(obj2 != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 62 | mirror::Object* obj3 = c->AllocObject(soa.Self()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 63 | ASSERT_TRUE(obj3 != nullptr); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 64 | |
| 65 | const uint32_t cookie = IRT_FIRST_SEGMENT; |
| 66 | |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 67 | CheckDump(&irt, 0, 0); |
| 68 | |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 69 | IndirectRef iref0 = (IndirectRef) 0x11110; |
| 70 | EXPECT_FALSE(irt.Remove(cookie, iref0)) << "unexpectedly successful removal"; |
| 71 | |
| 72 | // Add three, check, remove in the order in which they were added. |
| 73 | iref0 = irt.Add(cookie, obj0); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 74 | EXPECT_TRUE(iref0 != nullptr); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 75 | CheckDump(&irt, 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 76 | IndirectRef iref1 = irt.Add(cookie, obj1); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 77 | EXPECT_TRUE(iref1 != nullptr); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 78 | CheckDump(&irt, 2, 2); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 79 | IndirectRef iref2 = irt.Add(cookie, obj2); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 80 | EXPECT_TRUE(iref2 != nullptr); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 81 | CheckDump(&irt, 3, 3); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 82 | |
| 83 | EXPECT_EQ(obj0, irt.Get(iref0)); |
| 84 | EXPECT_EQ(obj1, irt.Get(iref1)); |
| 85 | EXPECT_EQ(obj2, irt.Get(iref2)); |
| 86 | |
| 87 | EXPECT_TRUE(irt.Remove(cookie, iref0)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 88 | CheckDump(&irt, 2, 2); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 89 | EXPECT_TRUE(irt.Remove(cookie, iref1)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 90 | CheckDump(&irt, 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 91 | EXPECT_TRUE(irt.Remove(cookie, iref2)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 92 | CheckDump(&irt, 0, 0); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 93 | |
| 94 | // Table should be empty now. |
| 95 | EXPECT_EQ(0U, irt.Capacity()); |
| 96 | |
| 97 | // Get invalid entry (off the end of the list). |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 98 | EXPECT_TRUE(irt.Get(iref0) == nullptr); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 99 | |
| 100 | // Add three, remove in the opposite order. |
| 101 | iref0 = irt.Add(cookie, obj0); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 102 | EXPECT_TRUE(iref0 != nullptr); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 103 | iref1 = irt.Add(cookie, obj1); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 104 | EXPECT_TRUE(iref1 != nullptr); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 105 | iref2 = irt.Add(cookie, obj2); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 106 | EXPECT_TRUE(iref2 != nullptr); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 107 | CheckDump(&irt, 3, 3); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 108 | |
| 109 | ASSERT_TRUE(irt.Remove(cookie, iref2)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 110 | CheckDump(&irt, 2, 2); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 111 | ASSERT_TRUE(irt.Remove(cookie, iref1)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 112 | CheckDump(&irt, 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 113 | ASSERT_TRUE(irt.Remove(cookie, iref0)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 114 | CheckDump(&irt, 0, 0); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 115 | |
| 116 | // Table should be empty now. |
| 117 | ASSERT_EQ(0U, irt.Capacity()); |
| 118 | |
| 119 | // Add three, remove middle / middle / bottom / top. (Second attempt |
| 120 | // to remove middle should fail.) |
| 121 | iref0 = irt.Add(cookie, obj0); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 122 | EXPECT_TRUE(iref0 != nullptr); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 123 | iref1 = irt.Add(cookie, obj1); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 124 | EXPECT_TRUE(iref1 != nullptr); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 125 | iref2 = irt.Add(cookie, obj2); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 126 | EXPECT_TRUE(iref2 != nullptr); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 127 | CheckDump(&irt, 3, 3); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 128 | |
| 129 | ASSERT_EQ(3U, irt.Capacity()); |
| 130 | |
| 131 | ASSERT_TRUE(irt.Remove(cookie, iref1)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 132 | CheckDump(&irt, 2, 2); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 133 | ASSERT_FALSE(irt.Remove(cookie, iref1)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 134 | CheckDump(&irt, 2, 2); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 135 | |
| 136 | // Get invalid entry (from hole). |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 137 | EXPECT_TRUE(irt.Get(iref1) == nullptr); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 138 | |
| 139 | ASSERT_TRUE(irt.Remove(cookie, iref2)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 140 | CheckDump(&irt, 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 141 | ASSERT_TRUE(irt.Remove(cookie, iref0)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 142 | CheckDump(&irt, 0, 0); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 143 | |
| 144 | // Table should be empty now. |
| 145 | ASSERT_EQ(0U, irt.Capacity()); |
| 146 | |
| 147 | // Add four entries. Remove #1, add new entry, verify that table size |
| 148 | // is still 4 (i.e. holes are getting filled). Remove #1 and #3, verify |
| 149 | // that we delete one and don't hole-compact the other. |
| 150 | iref0 = irt.Add(cookie, obj0); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 151 | EXPECT_TRUE(iref0 != nullptr); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 152 | iref1 = irt.Add(cookie, obj1); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 153 | EXPECT_TRUE(iref1 != nullptr); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 154 | iref2 = irt.Add(cookie, obj2); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 155 | EXPECT_TRUE(iref2 != nullptr); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 156 | IndirectRef iref3 = irt.Add(cookie, obj3); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 157 | EXPECT_TRUE(iref3 != nullptr); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 158 | CheckDump(&irt, 4, 4); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 159 | |
| 160 | ASSERT_TRUE(irt.Remove(cookie, iref1)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 161 | CheckDump(&irt, 3, 3); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 162 | |
| 163 | iref1 = irt.Add(cookie, obj1); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 164 | EXPECT_TRUE(iref1 != nullptr); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 165 | |
| 166 | ASSERT_EQ(4U, irt.Capacity()) << "hole not filled"; |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 167 | CheckDump(&irt, 4, 4); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 168 | |
| 169 | ASSERT_TRUE(irt.Remove(cookie, iref1)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 170 | CheckDump(&irt, 3, 3); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 171 | ASSERT_TRUE(irt.Remove(cookie, iref3)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 172 | CheckDump(&irt, 2, 2); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 173 | |
| 174 | ASSERT_EQ(3U, irt.Capacity()) << "should be 3 after two deletions"; |
| 175 | |
| 176 | ASSERT_TRUE(irt.Remove(cookie, iref2)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 177 | CheckDump(&irt, 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 178 | ASSERT_TRUE(irt.Remove(cookie, iref0)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 179 | CheckDump(&irt, 0, 0); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 180 | |
| 181 | ASSERT_EQ(0U, irt.Capacity()) << "not empty after split remove"; |
| 182 | |
| 183 | // Add an entry, remove it, add a new entry, and try to use the original |
| 184 | // iref. They have the same slot number but are for different objects. |
| 185 | // With the extended checks in place, this should fail. |
| 186 | iref0 = irt.Add(cookie, obj0); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 187 | EXPECT_TRUE(iref0 != nullptr); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 188 | CheckDump(&irt, 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 189 | ASSERT_TRUE(irt.Remove(cookie, iref0)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 190 | CheckDump(&irt, 0, 0); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 191 | iref1 = irt.Add(cookie, obj1); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 192 | EXPECT_TRUE(iref1 != nullptr); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 193 | CheckDump(&irt, 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 194 | ASSERT_FALSE(irt.Remove(cookie, iref0)) << "mismatched del succeeded"; |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 195 | CheckDump(&irt, 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 196 | ASSERT_TRUE(irt.Remove(cookie, iref1)) << "switched del failed"; |
| 197 | ASSERT_EQ(0U, irt.Capacity()) << "switching del not empty"; |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 198 | CheckDump(&irt, 0, 0); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 199 | |
| 200 | // Same as above, but with the same object. A more rigorous checker |
| 201 | // (e.g. with slot serialization) will catch this. |
| 202 | iref0 = irt.Add(cookie, obj0); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 203 | EXPECT_TRUE(iref0 != nullptr); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 204 | CheckDump(&irt, 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 205 | ASSERT_TRUE(irt.Remove(cookie, iref0)); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 206 | CheckDump(&irt, 0, 0); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 207 | iref1 = irt.Add(cookie, obj0); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 208 | EXPECT_TRUE(iref1 != nullptr); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 209 | CheckDump(&irt, 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 210 | if (iref0 != iref1) { |
| 211 | // Try 0, should not work. |
| 212 | ASSERT_FALSE(irt.Remove(cookie, iref0)) << "temporal del succeeded"; |
| 213 | } |
| 214 | ASSERT_TRUE(irt.Remove(cookie, iref1)) << "temporal cleanup failed"; |
| 215 | ASSERT_EQ(0U, irt.Capacity()) << "temporal del not empty"; |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 216 | CheckDump(&irt, 0, 0); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 217 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 218 | // nullptr isn't a valid iref. |
| 219 | ASSERT_TRUE(irt.Get(nullptr) == nullptr); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 220 | |
| 221 | // Stale lookup. |
| 222 | iref0 = irt.Add(cookie, obj0); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 223 | EXPECT_TRUE(iref0 != nullptr); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 224 | CheckDump(&irt, 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 225 | ASSERT_TRUE(irt.Remove(cookie, iref0)); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 226 | EXPECT_TRUE(irt.Get(iref0) == nullptr) << "stale lookup succeeded"; |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 227 | CheckDump(&irt, 0, 0); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 228 | |
| 229 | // Test table resizing. |
| 230 | // These ones fit... |
| 231 | IndirectRef manyRefs[kTableInitial]; |
| 232 | for (size_t i = 0; i < kTableInitial; i++) { |
| 233 | manyRefs[i] = irt.Add(cookie, obj0); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 234 | ASSERT_TRUE(manyRefs[i] != nullptr) << "Failed adding " << i; |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 235 | CheckDump(&irt, i + 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 236 | } |
| 237 | // ...this one causes overflow. |
| 238 | iref0 = irt.Add(cookie, obj0); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 239 | ASSERT_TRUE(iref0 != nullptr); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 240 | ASSERT_EQ(kTableInitial + 1, irt.Capacity()); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 241 | CheckDump(&irt, kTableInitial + 1, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 242 | |
| 243 | for (size_t i = 0; i < kTableInitial; i++) { |
| 244 | ASSERT_TRUE(irt.Remove(cookie, manyRefs[i])) << "failed removing " << i; |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 245 | CheckDump(&irt, kTableInitial - i, 1); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 246 | } |
| 247 | // Because of removal order, should have 11 entries, 10 of them holes. |
| 248 | ASSERT_EQ(kTableInitial + 1, irt.Capacity()); |
| 249 | |
| 250 | ASSERT_TRUE(irt.Remove(cookie, iref0)) << "multi-remove final failed"; |
| 251 | |
| 252 | ASSERT_EQ(0U, irt.Capacity()) << "multi-del not empty"; |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 253 | CheckDump(&irt, 0, 0); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | } // namespace art |