reference_table_test.cc revision 00f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abac
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  Object* o1 = String::AllocFromModifiedUtf8("hello");
29  Object* o2 = ShortArray::Alloc(0);
30
31  ReferenceTable rt("test", 0, 4);
32  std::ostringstream oss1;
33  rt.Dump(oss1);
34  EXPECT_TRUE(oss1.str().find("(empty)") != std::string::npos) << oss1.str();
35  EXPECT_EQ(0U, rt.Size());
36  rt.Remove(NULL);
37  EXPECT_EQ(0U, rt.Size());
38  rt.Remove(o1);
39  EXPECT_EQ(0U, rt.Size());
40  rt.Add(o1);
41  EXPECT_EQ(1U, rt.Size());
42  rt.Add(o2);
43  EXPECT_EQ(2U, rt.Size());
44  rt.Add(o2);
45  EXPECT_EQ(3U, rt.Size());
46  std::ostringstream oss2;
47  rt.Dump(oss2);
48  EXPECT_TRUE(oss2.str().find("Last 3 entries (of 3):") != std::string::npos) << oss2.str();
49  EXPECT_TRUE(oss2.str().find("1 of java.lang.String") != std::string::npos) << oss2.str();
50  EXPECT_TRUE(oss2.str().find("2 of short[] (1 unique instances)") != std::string::npos) << oss2.str();
51  rt.Remove(o1);
52  EXPECT_EQ(2U, rt.Size());
53  rt.Remove(o2);
54  EXPECT_EQ(1U, rt.Size());
55  rt.Remove(o2);
56  EXPECT_EQ(0U, rt.Size());
57}
58
59}  // namespace art
60