blob: 4bb5c97ec48fc1830919d13a2fee27f905a71426 [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());
Elliott Hughesbfaadc82011-08-18 17:36:58 -070028 Object* o1 = String::AllocFromModifiedUtf8("hello");
Elliott Hughes11e45072011-08-16 17:40:46 -070029 Object* o2 = ShortArray::Alloc(0);
30
Elliott Hughes11e45072011-08-16 17:40:46 -070031 ReferenceTable rt("test", 0, 4);
Elliott Hughes73e66f72012-05-09 09:34:45 -070032 std::ostringstream oss1;
33 rt.Dump(oss1);
34 EXPECT_TRUE(oss1.str().find("(empty)") != std::string::npos) << oss1.str();
Elliott Hughes11e45072011-08-16 17:40:46 -070035 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());
Elliott Hughes73e66f72012-05-09 09:34:45 -070044 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();
Elliott Hughes11e45072011-08-16 17:40:46 -070051 rt.Remove(o1);
Elliott Hughes73e66f72012-05-09 09:34:45 -070052 EXPECT_EQ(2U, rt.Size());
53 rt.Remove(o2);
Elliott Hughes11e45072011-08-16 17:40:46 -070054 EXPECT_EQ(1U, rt.Size());
55 rt.Remove(o2);
56 EXPECT_EQ(0U, rt.Size());
57}
58
59} // namespace art