reference_table_test.cc revision a1ce1fef2d49d1d537776a5308ace7102a815fe5
1/*
2 * Copyright (C) 2011 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 "reference_table.h"
18
19#include "common_runtime_test.h"
20
21namespace art {
22
23class ReferenceTableTest : public CommonRuntimeTest {};
24
25TEST_F(ReferenceTableTest, Basics) {
26  ScopedObjectAccess soa(Thread::Current());
27  mirror::Object* o1 = mirror::String::AllocFromModifiedUtf8(soa.Self(), "hello");
28
29  ReferenceTable rt("test", 0, 11);
30
31  // Check dumping the empty table.
32  {
33    std::ostringstream oss;
34    rt.Dump(oss);
35    EXPECT_NE(oss.str().find("(empty)"), std::string::npos) << oss.str();
36    EXPECT_EQ(0U, rt.Size());
37  }
38
39  // Check removal of all NULLs in a empty table is a no-op.
40  rt.Remove(NULL);
41  EXPECT_EQ(0U, rt.Size());
42
43  // Check removal of all o1 in a empty table is a no-op.
44  rt.Remove(o1);
45  EXPECT_EQ(0U, rt.Size());
46
47  // Add o1 and check we have 1 element and can dump.
48  {
49    rt.Add(o1);
50    EXPECT_EQ(1U, rt.Size());
51    std::ostringstream oss;
52    rt.Dump(oss);
53    EXPECT_NE(oss.str().find("1 of java.lang.String"), std::string::npos) << oss.str();
54    EXPECT_EQ(oss.str().find("short[]"), std::string::npos) << oss.str();
55  }
56
57  // Add a second object 10 times and check dumping is sane.
58  mirror::Object* o2 = mirror::ShortArray::Alloc(soa.Self(), 0);
59  for (size_t i = 0; i < 10; ++i) {
60    rt.Add(o2);
61    EXPECT_EQ(i + 2, rt.Size());
62    std::ostringstream oss;
63    rt.Dump(oss);
64    EXPECT_NE(oss.str().find(StringPrintf("Last %zd entries (of %zd):",
65                                          i + 2 > 10 ? 10 : i + 2,
66                                          i + 2)),
67              std::string::npos) << oss.str();
68    EXPECT_NE(oss.str().find("1 of java.lang.String"), std::string::npos) << oss.str();
69    if (i == 0) {
70      EXPECT_NE(oss.str().find("1 of short[]"), std::string::npos) << oss.str();
71    } else {
72      EXPECT_NE(oss.str().find(StringPrintf("%zd of short[] (1 unique instances)", i + 1)),
73                std::string::npos) << oss.str();
74    }
75  }
76
77  // Remove o1 (first element).
78  {
79    rt.Remove(o1);
80    EXPECT_EQ(10U, rt.Size());
81    std::ostringstream oss;
82    rt.Dump(oss);
83    EXPECT_EQ(oss.str().find("java.lang.String"), std::string::npos) << oss.str();
84  }
85
86  // Remove o2 ten times.
87  for (size_t i = 0; i < 10; ++i) {
88    rt.Remove(o2);
89    EXPECT_EQ(9 - i, rt.Size());
90    std::ostringstream oss;
91    rt.Dump(oss);
92    if (i == 9) {
93      EXPECT_EQ(oss.str().find("short[]"), std::string::npos) << oss.str();
94    } else if (i == 8) {
95      EXPECT_NE(oss.str().find("1 of short[]"), std::string::npos) << oss.str();
96    } else {
97      EXPECT_NE(oss.str().find(StringPrintf("%zd of short[] (1 unique instances)", 10 - i - 1)),
98                std::string::npos) << oss.str();
99    }
100  }
101}
102
103}  // namespace art
104