2 * Copyright (C) 2011 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "reference_table.h"
19 #include "common_runtime_test.h"
20 #include "mirror/array-inl.h"
21 #include "mirror/class-inl.h"
22 #include "mirror/string.h"
23 #include "primitive.h"
24 #include "scoped_thread_state_change.h"
25 #include "thread-inl.h"
29 class ReferenceTableTest : public CommonRuntimeTest {};
31 TEST_F(ReferenceTableTest, Basics) {
32 ScopedObjectAccess soa(Thread::Current());
33 mirror::Object* o1 = mirror::String::AllocFromModifiedUtf8(soa.Self(), "hello");
35 ReferenceTable rt("test", 0, 11);
37 // Check dumping the empty table.
39 std::ostringstream oss;
41 EXPECT_NE(oss.str().find("(empty)"), std::string::npos) << oss.str();
42 EXPECT_EQ(0U, rt.Size());
45 // Check removal of all nullss in a empty table is a no-op.
47 EXPECT_EQ(0U, rt.Size());
49 // Check removal of all o1 in a empty table is a no-op.
51 EXPECT_EQ(0U, rt.Size());
53 // Add o1 and check we have 1 element and can dump.
56 EXPECT_EQ(1U, rt.Size());
57 std::ostringstream oss;
59 EXPECT_NE(oss.str().find("1 of java.lang.String"), std::string::npos) << oss.str();
60 EXPECT_EQ(oss.str().find("short[]"), std::string::npos) << oss.str();
63 // Add a second object 10 times and check dumping is sane.
64 mirror::Object* o2 = mirror::ShortArray::Alloc(soa.Self(), 0);
65 for (size_t i = 0; i < 10; ++i) {
67 EXPECT_EQ(i + 2, rt.Size());
68 std::ostringstream oss;
70 EXPECT_NE(oss.str().find(StringPrintf("Last %zd entries (of %zd):",
71 i + 2 > 10 ? 10 : i + 2,
73 std::string::npos) << oss.str();
74 EXPECT_NE(oss.str().find("1 of java.lang.String"), std::string::npos) << oss.str();
76 EXPECT_NE(oss.str().find("1 of short[]"), std::string::npos) << oss.str();
78 EXPECT_NE(oss.str().find(StringPrintf("%zd of short[] (1 unique instances)", i + 1)),
79 std::string::npos) << oss.str();
83 // Remove o1 (first element).
86 EXPECT_EQ(10U, rt.Size());
87 std::ostringstream oss;
89 EXPECT_EQ(oss.str().find("java.lang.String"), std::string::npos) << oss.str();
92 // Remove o2 ten times.
93 for (size_t i = 0; i < 10; ++i) {
95 EXPECT_EQ(9 - i, rt.Size());
96 std::ostringstream oss;
99 EXPECT_EQ(oss.str().find("short[]"), std::string::npos) << oss.str();
101 EXPECT_NE(oss.str().find("1 of short[]"), std::string::npos) << oss.str();
103 EXPECT_NE(oss.str().find(StringPrintf("%zd of short[] (1 unique instances)", 10 - i - 1)),
104 std::string::npos) << oss.str();
109 static std::vector<size_t> FindAll(const std::string& haystack, const char* needle) {
110 std::vector<size_t> res;
113 size_t pos = haystack.find(needle, start);
114 if (pos == std::string::npos) {
119 } while (start < haystack.size());
123 TEST_F(ReferenceTableTest, SummaryOrder) {
124 // Check that the summary statistics are sorted.
125 ScopedObjectAccess soa(Thread::Current());
127 ReferenceTable rt("test", 0, 20);
130 mirror::Object* s1 = mirror::String::AllocFromModifiedUtf8(soa.Self(), "hello");
131 mirror::Object* s2 = mirror::String::AllocFromModifiedUtf8(soa.Self(), "world");
133 // 3 copies of s1, 2 copies of s2, interleaved.
134 for (size_t i = 0; i != 2; ++i) {
142 // Differently sized byte arrays. Should be sorted by identical (non-unique cound).
143 mirror::Object* b1_1 = mirror::ByteArray::Alloc(soa.Self(), 1);
145 rt.Add(mirror::ByteArray::Alloc(soa.Self(), 2));
147 rt.Add(mirror::ByteArray::Alloc(soa.Self(), 2));
148 rt.Add(mirror::ByteArray::Alloc(soa.Self(), 1));
149 rt.Add(mirror::ByteArray::Alloc(soa.Self(), 2));
152 rt.Add(mirror::CharArray::Alloc(soa.Self(), 0));
154 // Now dump, and ensure order.
155 std::ostringstream oss;
158 // Only do this on the part after Summary.
159 std::string base = oss.str();
160 size_t summary_pos = base.find("Summary:");
161 ASSERT_NE(summary_pos, std::string::npos);
163 std::string haystack = base.substr(summary_pos);
165 std::vector<size_t> strCounts = FindAll(haystack, "java.lang.String");
166 std::vector<size_t> b1Counts = FindAll(haystack, "byte[] (1 elements)");
167 std::vector<size_t> b2Counts = FindAll(haystack, "byte[] (2 elements)");
168 std::vector<size_t> cCounts = FindAll(haystack, "char[]");
171 EXPECT_EQ(1u, strCounts.size());
172 EXPECT_EQ(1u, b1Counts.size());
173 EXPECT_EQ(1u, b2Counts.size());
174 EXPECT_EQ(1u, cCounts.size());
176 // Expect them to be in order.
177 EXPECT_LT(strCounts[0], b1Counts[0]);
178 EXPECT_LT(b1Counts[0], b2Counts[0]);
179 EXPECT_LT(b2Counts[0], cCounts[0]);