Fix segv in indirect reference table dump.

ReferenceTable::Dump is common code but can't handle NULLs. Process
IndirectReferenceTable's use so that it doesn't pass NULLs for removed
slots.
Improve unit tests.

Change-Id: Ia41375f8f2ecc907ceda8c39d146ce6ef607fb08
diff --git a/src/indirect_reference_table_test.cc b/src/indirect_reference_table_test.cc
index 1698f18..971e42a 100644
--- a/src/indirect_reference_table_test.cc
+++ b/src/indirect_reference_table_test.cc
@@ -23,6 +23,24 @@
 class IndirectReferenceTableTest : public CommonTest {
 };
 
+static void CheckDump(IndirectReferenceTable* irt, size_t num_objects, size_t num_unique)
+    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
+  std::ostringstream oss;
+  irt->Dump(oss);
+  if (num_objects == 0) {
+    EXPECT_EQ(oss.str().find("java.lang.Object"), std::string::npos) << oss.str();
+  } else if (num_objects == 1) {
+    EXPECT_NE(oss.str().find("1 of java.lang.Object"), std::string::npos) << oss.str();
+  } else {
+    EXPECT_NE(oss.str().find(StringPrintf("%zd of java.lang.Object (%zd unique instances)",
+                                          num_objects, num_unique)),
+              std::string::npos)
+                  << "\n Expected number of objects: " << num_objects
+                  << "\n Expected unique objects: " << num_unique << "\n"
+                  << oss.str();
+  }
+}
+
 TEST_F(IndirectReferenceTableTest, BasicTest) {
   ScopedObjectAccess soa(Thread::Current());
   static const size_t kTableInitial = 10;
@@ -42,26 +60,32 @@
 
   const uint32_t cookie = IRT_FIRST_SEGMENT;
 
+  CheckDump(&irt, 0, 0);
+
   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);
+  CheckDump(&irt, 1, 1);
   IndirectRef iref1 = irt.Add(cookie, obj1);
   EXPECT_TRUE(iref1 != NULL);
+  CheckDump(&irt, 2, 2);
   IndirectRef iref2 = irt.Add(cookie, obj2);
   EXPECT_TRUE(iref2 != NULL);
-
-  irt.Dump(LOG(DEBUG));
+  CheckDump(&irt, 3, 3);
 
   EXPECT_EQ(obj0, irt.Get(iref0));
   EXPECT_EQ(obj1, irt.Get(iref1));
   EXPECT_EQ(obj2, irt.Get(iref2));
 
   EXPECT_TRUE(irt.Remove(cookie, iref0));
+  CheckDump(&irt, 2, 2);
   EXPECT_TRUE(irt.Remove(cookie, iref1));
+  CheckDump(&irt, 1, 1);
   EXPECT_TRUE(irt.Remove(cookie, iref2));
+  CheckDump(&irt, 0, 0);
 
   // Table should be empty now.
   EXPECT_EQ(0U, irt.Capacity());
@@ -76,10 +100,14 @@
   EXPECT_TRUE(iref1 != NULL);
   iref2 = irt.Add(cookie, obj2);
   EXPECT_TRUE(iref2 != NULL);
+  CheckDump(&irt, 3, 3);
 
   ASSERT_TRUE(irt.Remove(cookie, iref2));
+  CheckDump(&irt, 2, 2);
   ASSERT_TRUE(irt.Remove(cookie, iref1));
+  CheckDump(&irt, 1, 1);
   ASSERT_TRUE(irt.Remove(cookie, iref0));
+  CheckDump(&irt, 0, 0);
 
   // Table should be empty now.
   ASSERT_EQ(0U, irt.Capacity());
@@ -92,17 +120,22 @@
   EXPECT_TRUE(iref1 != NULL);
   iref2 = irt.Add(cookie, obj2);
   EXPECT_TRUE(iref2 != NULL);
+  CheckDump(&irt, 3, 3);
 
   ASSERT_EQ(3U, irt.Capacity());
 
   ASSERT_TRUE(irt.Remove(cookie, iref1));
+  CheckDump(&irt, 2, 2);
   ASSERT_FALSE(irt.Remove(cookie, iref1));
+  CheckDump(&irt, 2, 2);
 
   // Get invalid entry (from hole).
   EXPECT_EQ(kInvalidIndirectRefObject, irt.Get(iref1));
 
   ASSERT_TRUE(irt.Remove(cookie, iref2));
+  CheckDump(&irt, 1, 1);
   ASSERT_TRUE(irt.Remove(cookie, iref0));
+  CheckDump(&irt, 0, 0);
 
   // Table should be empty now.
   ASSERT_EQ(0U, irt.Capacity());
@@ -118,21 +151,28 @@
   EXPECT_TRUE(iref2 != NULL);
   IndirectRef iref3 = irt.Add(cookie, obj3);
   EXPECT_TRUE(iref3 != NULL);
+  CheckDump(&irt, 4, 4);
 
   ASSERT_TRUE(irt.Remove(cookie, iref1));
+  CheckDump(&irt, 3, 3);
 
   iref1 = irt.Add(cookie, obj1);
   EXPECT_TRUE(iref1 != NULL);
 
   ASSERT_EQ(4U, irt.Capacity()) << "hole not filled";
+  CheckDump(&irt, 4, 4);
 
   ASSERT_TRUE(irt.Remove(cookie, iref1));
+  CheckDump(&irt, 3, 3);
   ASSERT_TRUE(irt.Remove(cookie, iref3));
+  CheckDump(&irt, 2, 2);
 
   ASSERT_EQ(3U, irt.Capacity()) << "should be 3 after two deletions";
 
   ASSERT_TRUE(irt.Remove(cookie, iref2));
+  CheckDump(&irt, 1, 1);
   ASSERT_TRUE(irt.Remove(cookie, iref0));
+  CheckDump(&irt, 0, 0);
 
   ASSERT_EQ(0U, irt.Capacity()) << "not empty after split remove";
 
@@ -141,26 +181,35 @@
   // With the extended checks in place, this should fail.
   iref0 = irt.Add(cookie, obj0);
   EXPECT_TRUE(iref0 != NULL);
+  CheckDump(&irt, 1, 1);
   ASSERT_TRUE(irt.Remove(cookie, iref0));
+  CheckDump(&irt, 0, 0);
   iref1 = irt.Add(cookie, obj1);
   EXPECT_TRUE(iref1 != NULL);
+  CheckDump(&irt, 1, 1);
   ASSERT_FALSE(irt.Remove(cookie, iref0)) << "mismatched del succeeded";
+  CheckDump(&irt, 1, 1);
   ASSERT_TRUE(irt.Remove(cookie, iref1)) << "switched del failed";
   ASSERT_EQ(0U, irt.Capacity()) << "switching del not empty";
+  CheckDump(&irt, 0, 0);
 
   // 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);
+  CheckDump(&irt, 1, 1);
   ASSERT_TRUE(irt.Remove(cookie, iref0));
+  CheckDump(&irt, 0, 0);
   iref1 = irt.Add(cookie, obj0);
   EXPECT_TRUE(iref1 != NULL);
+  CheckDump(&irt, 1, 1);
   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";
+  CheckDump(&irt, 0, 0);
 
   // NULL isn't a valid iref.
   ASSERT_EQ(kInvalidIndirectRefObject, irt.Get(NULL));
@@ -168,8 +217,10 @@
   // Stale lookup.
   iref0 = irt.Add(cookie, obj0);
   EXPECT_TRUE(iref0 != NULL);
+  CheckDump(&irt, 1, 1);
   ASSERT_TRUE(irt.Remove(cookie, iref0));
   EXPECT_EQ(kInvalidIndirectRefObject, irt.Get(iref0)) << "stale lookup succeeded";
+  CheckDump(&irt, 0, 0);
 
   // Test table resizing.
   // These ones fit...
@@ -177,14 +228,17 @@
   for (size_t i = 0; i < kTableInitial; i++) {
     manyRefs[i] = irt.Add(cookie, obj0);
     ASSERT_TRUE(manyRefs[i] != NULL) << "Failed adding " << i;
+    CheckDump(&irt, i + 1, 1);
   }
   // ...this one causes overflow.
   iref0 = irt.Add(cookie, obj0);
   ASSERT_TRUE(iref0 != NULL);
   ASSERT_EQ(kTableInitial + 1, irt.Capacity());
+  CheckDump(&irt, kTableInitial + 1, 1);
 
   for (size_t i = 0; i < kTableInitial; i++) {
     ASSERT_TRUE(irt.Remove(cookie, manyRefs[i])) << "failed removing " << i;
+    CheckDump(&irt, kTableInitial - i, 1);
   }
   // Because of removal order, should have 11 entries, 10 of them holes.
   ASSERT_EQ(kTableInitial + 1, irt.Capacity());
@@ -192,6 +246,7 @@
   ASSERT_TRUE(irt.Remove(cookie, iref0)) << "multi-remove final failed";
 
   ASSERT_EQ(0U, irt.Capacity()) << "multi-del not empty";
+  CheckDump(&irt, 0, 0);
 }
 
 }  // namespace art