Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 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 | */ |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 16 | |
| 17 | #include "common_test.h" |
| 18 | |
| 19 | #include "reference_table.h" |
| 20 | |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 21 | namespace art { |
| 22 | |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 23 | class ReferenceTableTest : public CommonTest { |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 24 | }; |
| 25 | |
| 26 | TEST_F(ReferenceTableTest, Basics) { |
Elliott Hughes | bfaadc8 | 2011-08-18 17:36:58 -0700 | [diff] [blame] | 27 | Object* o1 = String::AllocFromModifiedUtf8("hello"); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 28 | Object* o2 = ShortArray::Alloc(0); |
| 29 | |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 30 | ReferenceTable rt("test", 0, 4); |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 31 | std::ostringstream oss1; |
| 32 | rt.Dump(oss1); |
| 33 | EXPECT_TRUE(oss1.str().find("(empty)") != std::string::npos) << oss1.str(); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 34 | 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 Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 43 | 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 Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 50 | rt.Remove(o1); |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 51 | EXPECT_EQ(2U, rt.Size()); |
| 52 | rt.Remove(o2); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 53 | EXPECT_EQ(1U, rt.Size()); |
| 54 | rt.Remove(o2); |
| 55 | EXPECT_EQ(0U, rt.Size()); |
| 56 | } |
| 57 | |
| 58 | } // namespace art |