blob: fae8e722c328a28885db0cff0a5f1e75855100db [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"
Andreas Gampe8db4c882014-07-17 14:52:06 -070020#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070021#include "mirror/class-inl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070022#include "mirror/string.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070023#include "primitive.h"
Ian Rogerse63db272014-07-15 15:36:11 -070024#include "scoped_thread_state_change.h"
25#include "thread-inl.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080026
Elliott Hughes11e45072011-08-16 17:40:46 -070027namespace art {
28
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080029class ReferenceTableTest : public CommonRuntimeTest {};
Elliott Hughes11e45072011-08-16 17:40:46 -070030
31TEST_F(ReferenceTableTest, Basics) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070032 ScopedObjectAccess soa(Thread::Current());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033 mirror::Object* o1 = mirror::String::AllocFromModifiedUtf8(soa.Self(), "hello");
Elliott Hughes11e45072011-08-16 17:40:46 -070034
Ian Rogers63818dc2012-09-26 12:23:04 -070035 ReferenceTable rt("test", 0, 11);
36
37 // Check dumping the empty table.
38 {
39 std::ostringstream oss;
40 rt.Dump(oss);
41 EXPECT_NE(oss.str().find("(empty)"), std::string::npos) << oss.str();
42 EXPECT_EQ(0U, rt.Size());
43 }
44
Mathieu Chartier2cebb242015-04-21 16:50:40 -070045 // Check removal of all nullss in a empty table is a no-op.
46 rt.Remove(nullptr);
Elliott Hughes11e45072011-08-16 17:40:46 -070047 EXPECT_EQ(0U, rt.Size());
Ian Rogers63818dc2012-09-26 12:23:04 -070048
49 // Check removal of all o1 in a empty table is a no-op.
Elliott Hughes11e45072011-08-16 17:40:46 -070050 rt.Remove(o1);
51 EXPECT_EQ(0U, rt.Size());
Ian Rogers63818dc2012-09-26 12:23:04 -070052
53 // Add o1 and check we have 1 element and can dump.
54 {
55 rt.Add(o1);
56 EXPECT_EQ(1U, rt.Size());
57 std::ostringstream oss;
58 rt.Dump(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();
61 }
62
63 // Add a second object 10 times and check dumping is sane.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080064 mirror::Object* o2 = mirror::ShortArray::Alloc(soa.Self(), 0);
Ian Rogers63818dc2012-09-26 12:23:04 -070065 for (size_t i = 0; i < 10; ++i) {
66 rt.Add(o2);
67 EXPECT_EQ(i + 2, rt.Size());
68 std::ostringstream oss;
69 rt.Dump(oss);
70 EXPECT_NE(oss.str().find(StringPrintf("Last %zd entries (of %zd):",
71 i + 2 > 10 ? 10 : i + 2,
72 i + 2)),
73 std::string::npos) << oss.str();
74 EXPECT_NE(oss.str().find("1 of java.lang.String"), std::string::npos) << oss.str();
75 if (i == 0) {
76 EXPECT_NE(oss.str().find("1 of short[]"), std::string::npos) << oss.str();
77 } else {
78 EXPECT_NE(oss.str().find(StringPrintf("%zd of short[] (1 unique instances)", i + 1)),
79 std::string::npos) << oss.str();
80 }
81 }
82
83 // Remove o1 (first element).
84 {
85 rt.Remove(o1);
86 EXPECT_EQ(10U, rt.Size());
87 std::ostringstream oss;
88 rt.Dump(oss);
89 EXPECT_EQ(oss.str().find("java.lang.String"), std::string::npos) << oss.str();
90 }
91
92 // Remove o2 ten times.
93 for (size_t i = 0; i < 10; ++i) {
94 rt.Remove(o2);
95 EXPECT_EQ(9 - i, rt.Size());
96 std::ostringstream oss;
97 rt.Dump(oss);
98 if (i == 9) {
99 EXPECT_EQ(oss.str().find("short[]"), std::string::npos) << oss.str();
100 } else if (i == 8) {
101 EXPECT_NE(oss.str().find("1 of short[]"), std::string::npos) << oss.str();
102 } else {
103 EXPECT_NE(oss.str().find(StringPrintf("%zd of short[] (1 unique instances)", 10 - i - 1)),
104 std::string::npos) << oss.str();
105 }
106 }
Elliott Hughes11e45072011-08-16 17:40:46 -0700107}
108
109} // namespace art