blob: 16fbd94b212a259f27f3b9b2d99a0abc8c705609 [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
17#include "common_test.h"
18
19#include "reference_table.h"
20
Elliott Hughes11e45072011-08-16 17:40:46 -070021namespace art {
22
Brian Carlstromf734cf52011-08-17 16:28:14 -070023class ReferenceTableTest : public CommonTest {
Elliott Hughes11e45072011-08-16 17:40:46 -070024};
25
26TEST_F(ReferenceTableTest, Basics) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070027 ScopedObjectAccess soa(Thread::Current());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028 mirror::Object* o1 = mirror::String::AllocFromModifiedUtf8(soa.Self(), "hello");
Elliott Hughes11e45072011-08-16 17:40:46 -070029
Ian Rogers63818dc2012-09-26 12:23:04 -070030 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.
Elliott Hughes11e45072011-08-16 17:40:46 -070041 rt.Remove(NULL);
42 EXPECT_EQ(0U, rt.Size());
Ian Rogers63818dc2012-09-26 12:23:04 -070043
44 // Check removal of all o1 in a empty table is a no-op.
Elliott Hughes11e45072011-08-16 17:40:46 -070045 rt.Remove(o1);
46 EXPECT_EQ(0U, rt.Size());
Ian Rogers63818dc2012-09-26 12:23:04 -070047
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.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080059 mirror::Object* o2 = mirror::ShortArray::Alloc(soa.Self(), 0);
Ian Rogers63818dc2012-09-26 12:23:04 -070060 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 }
Elliott Hughes11e45072011-08-16 17:40:46 -0700102}
103
104} // namespace art