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) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 27 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 28 | mirror::Object* o1 = mirror::String::AllocFromModifiedUtf8(soa.Self(), "hello"); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 29 | |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 30 | ReferenceTable rt("test", 0, 11); |
| 31 | |
| 32 | // Check dumping the empty table. |
| 33 | { |
| 34 | std::ostringstream oss; |
| 35 | rt.Dump(oss); |
| 36 | EXPECT_NE(oss.str().find("(empty)"), std::string::npos) << oss.str(); |
| 37 | EXPECT_EQ(0U, rt.Size()); |
| 38 | } |
| 39 | |
| 40 | // Check removal of all NULLs in a empty table is a no-op. |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 41 | rt.Remove(NULL); |
| 42 | EXPECT_EQ(0U, rt.Size()); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 43 | |
| 44 | // Check removal of all o1 in a empty table is a no-op. |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 45 | rt.Remove(o1); |
| 46 | EXPECT_EQ(0U, rt.Size()); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 47 | |
| 48 | // Add o1 and check we have 1 element and can dump. |
| 49 | { |
| 50 | rt.Add(o1); |
| 51 | EXPECT_EQ(1U, rt.Size()); |
| 52 | std::ostringstream oss; |
| 53 | rt.Dump(oss); |
| 54 | EXPECT_NE(oss.str().find("1 of java.lang.String"), std::string::npos) << oss.str(); |
| 55 | EXPECT_EQ(oss.str().find("short[]"), std::string::npos) << oss.str(); |
| 56 | } |
| 57 | |
| 58 | // Add a second object 10 times and check dumping is sane. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 59 | mirror::Object* o2 = mirror::ShortArray::Alloc(soa.Self(), 0); |
Ian Rogers | 63818dc | 2012-09-26 12:23:04 -0700 | [diff] [blame] | 60 | for (size_t i = 0; i < 10; ++i) { |
| 61 | rt.Add(o2); |
| 62 | EXPECT_EQ(i + 2, rt.Size()); |
| 63 | std::ostringstream oss; |
| 64 | rt.Dump(oss); |
| 65 | EXPECT_NE(oss.str().find(StringPrintf("Last %zd entries (of %zd):", |
| 66 | i + 2 > 10 ? 10 : i + 2, |
| 67 | i + 2)), |
| 68 | std::string::npos) << oss.str(); |
| 69 | EXPECT_NE(oss.str().find("1 of java.lang.String"), std::string::npos) << oss.str(); |
| 70 | if (i == 0) { |
| 71 | EXPECT_NE(oss.str().find("1 of short[]"), std::string::npos) << oss.str(); |
| 72 | } else { |
| 73 | EXPECT_NE(oss.str().find(StringPrintf("%zd of short[] (1 unique instances)", i + 1)), |
| 74 | std::string::npos) << oss.str(); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // Remove o1 (first element). |
| 79 | { |
| 80 | rt.Remove(o1); |
| 81 | EXPECT_EQ(10U, rt.Size()); |
| 82 | std::ostringstream oss; |
| 83 | rt.Dump(oss); |
| 84 | EXPECT_EQ(oss.str().find("java.lang.String"), std::string::npos) << oss.str(); |
| 85 | } |
| 86 | |
| 87 | // Remove o2 ten times. |
| 88 | for (size_t i = 0; i < 10; ++i) { |
| 89 | rt.Remove(o2); |
| 90 | EXPECT_EQ(9 - i, rt.Size()); |
| 91 | std::ostringstream oss; |
| 92 | rt.Dump(oss); |
| 93 | if (i == 9) { |
| 94 | EXPECT_EQ(oss.str().find("short[]"), std::string::npos) << oss.str(); |
| 95 | } else if (i == 8) { |
| 96 | EXPECT_NE(oss.str().find("1 of short[]"), std::string::npos) << oss.str(); |
| 97 | } else { |
| 98 | EXPECT_NE(oss.str().find(StringPrintf("%zd of short[] (1 unique instances)", 10 - i - 1)), |
| 99 | std::string::npos) << oss.str(); |
| 100 | } |
| 101 | } |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | } // namespace art |