blob: f6b040def2876452375e07b02b208af36b5154d0 [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 */
Brian Carlstrom7e93b502011-08-04 14:16:22 -070016
17#include "intern_table.h"
18
19#include "common_test.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080020#include "mirror/object.h"
Ian Rogers1f539342012-10-03 21:09:42 -070021#include "sirt_ref.h"
Brian Carlstrom7e93b502011-08-04 14:16:22 -070022
Brian Carlstrom7e93b502011-08-04 14:16:22 -070023namespace art {
24
Brian Carlstromf734cf52011-08-17 16:28:14 -070025class InternTableTest : public CommonTest {};
Brian Carlstrom7e93b502011-08-04 14:16:22 -070026
27TEST_F(InternTableTest, Intern) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070028 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom7e93b502011-08-04 14:16:22 -070029 InternTable intern_table;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030 SirtRef<mirror::String> foo_1(soa.Self(), intern_table.InternStrong(3, "foo"));
31 SirtRef<mirror::String> foo_2(soa.Self(), intern_table.InternStrong(3, "foo"));
32 SirtRef<mirror::String> foo_3(soa.Self(), mirror::String::AllocFromModifiedUtf8(soa.Self(), "foo"));
33 SirtRef<mirror::String> bar(soa.Self(), intern_table.InternStrong(3, "bar"));
Brian Carlstrom7e93b502011-08-04 14:16:22 -070034 EXPECT_TRUE(foo_1->Equals("foo"));
35 EXPECT_TRUE(foo_2->Equals("foo"));
36 EXPECT_TRUE(foo_3->Equals("foo"));
Brian Carlstrom40381fb2011-10-19 14:13:40 -070037 EXPECT_TRUE(foo_1.get() != NULL);
38 EXPECT_TRUE(foo_2.get() != NULL);
39 EXPECT_EQ(foo_1.get(), foo_2.get());
40 EXPECT_NE(foo_1.get(), bar.get());
41 EXPECT_NE(foo_2.get(), bar.get());
42 EXPECT_NE(foo_3.get(), bar.get());
Brian Carlstrom7e93b502011-08-04 14:16:22 -070043}
44
Elliott Hughescf4c6c42011-09-01 15:16:42 -070045TEST_F(InternTableTest, Size) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070046 ScopedObjectAccess soa(Thread::Current());
Elliott Hughescf4c6c42011-09-01 15:16:42 -070047 InternTable t;
48 EXPECT_EQ(0U, t.Size());
49 t.InternStrong(3, "foo");
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080050 SirtRef<mirror::String> foo(soa.Self(), mirror::String::AllocFromModifiedUtf8(soa.Self(), "foo"));
Brian Carlstrom40381fb2011-10-19 14:13:40 -070051 t.InternWeak(foo.get());
Elliott Hughescf4c6c42011-09-01 15:16:42 -070052 EXPECT_EQ(1U, t.Size());
53 t.InternStrong(3, "bar");
54 EXPECT_EQ(2U, t.Size());
55}
56
Elliott Hughesc33a32b2011-10-11 18:18:07 -070057class TestPredicate {
Elliott Hughes410c0c82011-09-01 17:58:25 -070058 public:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080059 bool IsMarked(const mirror::Object* s) const {
Elliott Hughes410c0c82011-09-01 17:58:25 -070060 bool erased = false;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080061 typedef std::vector<const mirror::String*>::iterator It; // TODO: C++0x auto
Elliott Hughes410c0c82011-09-01 17:58:25 -070062 for (It it = expected_.begin(), end = expected_.end(); it != end; ++it) {
63 if (*it == s) {
64 expected_.erase(it);
65 erased = true;
66 break;
67 }
Elliott Hughescf4c6c42011-09-01 15:16:42 -070068 }
Elliott Hughes410c0c82011-09-01 17:58:25 -070069 EXPECT_TRUE(erased);
Elliott Hughesc33a32b2011-10-11 18:18:07 -070070 return false;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070071 }
Elliott Hughes410c0c82011-09-01 17:58:25 -070072
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080073 void Expect(const mirror::String* s) {
Elliott Hughes410c0c82011-09-01 17:58:25 -070074 expected_.push_back(s);
75 }
76
77 ~TestPredicate() {
78 EXPECT_EQ(0U, expected_.size());
79 }
80
81 private:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080082 mutable std::vector<const mirror::String*> expected_;
Elliott Hughes410c0c82011-09-01 17:58:25 -070083};
Elliott Hughescf4c6c42011-09-01 15:16:42 -070084
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080085bool IsMarked(const mirror::Object* object, void* arg) {
Elliott Hughesc33a32b2011-10-11 18:18:07 -070086 return reinterpret_cast<TestPredicate*>(arg)->IsMarked(object);
87}
88
89TEST_F(InternTableTest, SweepInternTableWeaks) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070090 ScopedObjectAccess soa(Thread::Current());
Elliott Hughescf4c6c42011-09-01 15:16:42 -070091 InternTable t;
92 t.InternStrong(3, "foo");
93 t.InternStrong(3, "bar");
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080094 SirtRef<mirror::String> hello(soa.Self(),
95 mirror::String::AllocFromModifiedUtf8(soa.Self(), "hello"));
96 SirtRef<mirror::String> world(soa.Self(),
97 mirror::String::AllocFromModifiedUtf8(soa.Self(), "world"));
98 SirtRef<mirror::String> s0(soa.Self(), t.InternWeak(hello.get()));
99 SirtRef<mirror::String> s1(soa.Self(), t.InternWeak(world.get()));
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700100
101 EXPECT_EQ(4U, t.Size());
102
103 // We should traverse only the weaks...
Elliott Hughes410c0c82011-09-01 17:58:25 -0700104 TestPredicate p;
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700105 p.Expect(s0.get());
106 p.Expect(s1.get());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700107 {
Ian Rogers1f539342012-10-03 21:09:42 -0700108 ReaderMutexLock mu(soa.Self(), *Locks::heap_bitmap_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700109 t.SweepInternTableWeaks(IsMarked, &p);
110 }
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700111
112 EXPECT_EQ(2U, t.Size());
113
Elliott Hughese5448b52012-01-18 16:44:06 -0800114 // Just check that we didn't corrupt the map.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800115 SirtRef<mirror::String> still_here(soa.Self(),
116 mirror::String::AllocFromModifiedUtf8(soa.Self(), "still here"));
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700117 t.InternWeak(still_here.get());
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700118 EXPECT_EQ(3U, t.Size());
119}
120
121TEST_F(InternTableTest, ContainsWeak) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700122 ScopedObjectAccess soa(Thread::Current());
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700123 {
124 // Strongs are never weak.
125 InternTable t;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800126 SirtRef<mirror::String> interned_foo_1(soa.Self(), t.InternStrong(3, "foo"));
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700127 EXPECT_FALSE(t.ContainsWeak(interned_foo_1.get()));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800128 SirtRef<mirror::String> interned_foo_2(soa.Self(), t.InternStrong(3, "foo"));
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700129 EXPECT_FALSE(t.ContainsWeak(interned_foo_2.get()));
130 EXPECT_EQ(interned_foo_1.get(), interned_foo_2.get());
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700131 }
132
133 {
134 // Weaks are always weak.
135 InternTable t;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800136 SirtRef<mirror::String> foo_1(soa.Self(),
137 mirror::String::AllocFromModifiedUtf8(soa.Self(), "foo"));
138 SirtRef<mirror::String> foo_2(soa.Self(),
139 mirror::String::AllocFromModifiedUtf8(soa.Self(), "foo"));
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700140 EXPECT_NE(foo_1.get(), foo_2.get());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800141 SirtRef<mirror::String> interned_foo_1(soa.Self(), t.InternWeak(foo_1.get()));
142 SirtRef<mirror::String> interned_foo_2(soa.Self(), t.InternWeak(foo_2.get()));
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700143 EXPECT_TRUE(t.ContainsWeak(interned_foo_2.get()));
144 EXPECT_EQ(interned_foo_1.get(), interned_foo_2.get());
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700145 }
146
147 {
148 // A weak can be promoted to a strong.
149 InternTable t;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800150 SirtRef<mirror::String> foo(soa.Self(), mirror::String::AllocFromModifiedUtf8(soa.Self(), "foo"));
151 SirtRef<mirror::String> interned_foo_1(soa.Self(), t.InternWeak(foo.get()));
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700152 EXPECT_TRUE(t.ContainsWeak(interned_foo_1.get()));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800153 SirtRef<mirror::String> interned_foo_2(soa.Self(), t.InternStrong(3, "foo"));
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700154 EXPECT_FALSE(t.ContainsWeak(interned_foo_2.get()));
155 EXPECT_EQ(interned_foo_1.get(), interned_foo_2.get());
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700156 }
157
158 {
159 // Interning a weak after a strong gets you the strong.
160 InternTable t;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800161 SirtRef<mirror::String> interned_foo_1(soa.Self(), t.InternStrong(3, "foo"));
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700162 EXPECT_FALSE(t.ContainsWeak(interned_foo_1.get()));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800163 SirtRef<mirror::String> foo(soa.Self(),
164 mirror::String::AllocFromModifiedUtf8(soa.Self(), "foo"));
165 SirtRef<mirror::String> interned_foo_2(soa.Self(), t.InternWeak(foo.get()));
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700166 EXPECT_FALSE(t.ContainsWeak(interned_foo_2.get()));
167 EXPECT_EQ(interned_foo_1.get(), interned_foo_2.get());
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700168 }
169}
170
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700171} // namespace art