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