blob: c7c1cc624869578ce9b929efeff7c4bc324e1c9f [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) {
Elliott Hughesbfaadc82011-08-18 17:36:58 -070027 Object* o1 = String::AllocFromModifiedUtf8("hello");
Elliott Hughes11e45072011-08-16 17:40:46 -070028 Object* o2 = ShortArray::Alloc(0);
29
Elliott Hughes11e45072011-08-16 17:40:46 -070030 ReferenceTable rt("test", 0, 4);
Elliott Hughes73e66f72012-05-09 09:34:45 -070031 std::ostringstream oss1;
32 rt.Dump(oss1);
33 EXPECT_TRUE(oss1.str().find("(empty)") != std::string::npos) << oss1.str();
Elliott Hughes11e45072011-08-16 17:40:46 -070034 EXPECT_EQ(0U, rt.Size());
35 rt.Remove(NULL);
36 EXPECT_EQ(0U, rt.Size());
37 rt.Remove(o1);
38 EXPECT_EQ(0U, rt.Size());
39 rt.Add(o1);
40 EXPECT_EQ(1U, rt.Size());
41 rt.Add(o2);
42 EXPECT_EQ(2U, rt.Size());
Elliott Hughes73e66f72012-05-09 09:34:45 -070043 rt.Add(o2);
44 EXPECT_EQ(3U, rt.Size());
45 std::ostringstream oss2;
46 rt.Dump(oss2);
47 EXPECT_TRUE(oss2.str().find("Last 3 entries (of 3):") != std::string::npos) << oss2.str();
48 EXPECT_TRUE(oss2.str().find("1 of java.lang.String") != std::string::npos) << oss2.str();
49 EXPECT_TRUE(oss2.str().find("2 of short[] (1 unique instances)") != std::string::npos) << oss2.str();
Elliott Hughes11e45072011-08-16 17:40:46 -070050 rt.Remove(o1);
Elliott Hughes73e66f72012-05-09 09:34:45 -070051 EXPECT_EQ(2U, rt.Size());
52 rt.Remove(o2);
Elliott Hughes11e45072011-08-16 17:40:46 -070053 EXPECT_EQ(1U, rt.Size());
54 rt.Remove(o2);
55 EXPECT_EQ(0U, rt.Size());
56}
57
58} // namespace art