blob: 32290393ce279fb4284dfe516b06bc210b13a08b [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 */
Elliott Hughes11e45072011-08-16 17:40:46 -070016
Elliott Hughes11e45072011-08-16 17:40:46 -070017#include "reference_table.h"
18
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080019#include "common_runtime_test.h"
20
Elliott Hughes11e45072011-08-16 17:40:46 -070021namespace art {
22
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080023class ReferenceTableTest : public CommonRuntimeTest {};
Elliott Hughes11e45072011-08-16 17:40:46 -070024
25TEST_F(ReferenceTableTest, Basics) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070026 ScopedObjectAccess soa(Thread::Current());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027 mirror::Object* o1 = mirror::String::AllocFromModifiedUtf8(soa.Self(), "hello");
Elliott Hughes11e45072011-08-16 17:40:46 -070028
Ian Rogers63818dc2012-09-26 12:23:04 -070029 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.
Elliott Hughes11e45072011-08-16 17:40:46 -070040 rt.Remove(NULL);
41 EXPECT_EQ(0U, rt.Size());
Ian Rogers63818dc2012-09-26 12:23:04 -070042
43 // Check removal of all o1 in a empty table is a no-op.
Elliott Hughes11e45072011-08-16 17:40:46 -070044 rt.Remove(o1);
45 EXPECT_EQ(0U, rt.Size());
Ian Rogers63818dc2012-09-26 12:23:04 -070046
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.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080058 mirror::Object* o2 = mirror::ShortArray::Alloc(soa.Self(), 0);
Ian Rogers63818dc2012-09-26 12:23:04 -070059 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 }
Elliott Hughes11e45072011-08-16 17:40:46 -0700101}
102
103} // namespace art