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