blob: 5995d9e5d34e49eebd73d155725f95f5c96a8fac [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
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080019#include "common_runtime_test.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080020#include "mirror/object.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070021#include "handle_scope-inl.h"
Brian Carlstrom7e93b502011-08-04 14:16:22 -070022
Brian Carlstrom7e93b502011-08-04 14:16:22 -070023namespace art {
24
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080025class InternTableTest : public CommonRuntimeTest {};
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;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070030 StackHandleScope<4> hs(soa.Self());
31 Handle<mirror::String> foo_1(hs.NewHandle(intern_table.InternStrong(3, "foo")));
32 Handle<mirror::String> foo_2(hs.NewHandle(intern_table.InternStrong(3, "foo")));
33 Handle<mirror::String> foo_3(
34 hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "foo")));
35 Handle<mirror::String> bar(hs.NewHandle(intern_table.InternStrong(3, "bar")));
Brian Carlstrom7e93b502011-08-04 14:16:22 -070036 EXPECT_TRUE(foo_1->Equals("foo"));
37 EXPECT_TRUE(foo_2->Equals("foo"));
38 EXPECT_TRUE(foo_3->Equals("foo"));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070039 EXPECT_TRUE(foo_1.Get() != NULL);
40 EXPECT_TRUE(foo_2.Get() != NULL);
41 EXPECT_EQ(foo_1.Get(), foo_2.Get());
42 EXPECT_NE(foo_1.Get(), bar.Get());
43 EXPECT_NE(foo_2.Get(), bar.Get());
44 EXPECT_NE(foo_3.Get(), bar.Get());
Brian Carlstrom7e93b502011-08-04 14:16:22 -070045}
46
Elliott Hughescf4c6c42011-09-01 15:16:42 -070047TEST_F(InternTableTest, Size) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070048 ScopedObjectAccess soa(Thread::Current());
Elliott Hughescf4c6c42011-09-01 15:16:42 -070049 InternTable t;
50 EXPECT_EQ(0U, t.Size());
51 t.InternStrong(3, "foo");
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070052 StackHandleScope<1> hs(soa.Self());
53 Handle<mirror::String> foo(
54 hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "foo")));
55 t.InternWeak(foo.Get());
Elliott Hughescf4c6c42011-09-01 15:16:42 -070056 EXPECT_EQ(1U, t.Size());
57 t.InternStrong(3, "bar");
58 EXPECT_EQ(2U, t.Size());
59}
60
Elliott Hughesc33a32b2011-10-11 18:18:07 -070061class TestPredicate {
Elliott Hughes410c0c82011-09-01 17:58:25 -070062 public:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080063 bool IsMarked(const mirror::Object* s) const {
Elliott Hughes410c0c82011-09-01 17:58:25 -070064 bool erased = false;
Mathieu Chartier02e25112013-08-14 16:14:24 -070065 for (auto it = expected_.begin(), end = expected_.end(); it != end; ++it) {
Elliott Hughes410c0c82011-09-01 17:58:25 -070066 if (*it == s) {
67 expected_.erase(it);
68 erased = true;
69 break;
70 }
Elliott Hughescf4c6c42011-09-01 15:16:42 -070071 }
Elliott Hughes410c0c82011-09-01 17:58:25 -070072 EXPECT_TRUE(erased);
Elliott Hughesc33a32b2011-10-11 18:18:07 -070073 return false;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070074 }
Elliott Hughes410c0c82011-09-01 17:58:25 -070075
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080076 void Expect(const mirror::String* s) {
Elliott Hughes410c0c82011-09-01 17:58:25 -070077 expected_.push_back(s);
78 }
79
80 ~TestPredicate() {
81 EXPECT_EQ(0U, expected_.size());
82 }
83
84 private:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080085 mutable std::vector<const mirror::String*> expected_;
Elliott Hughes410c0c82011-09-01 17:58:25 -070086};
Elliott Hughescf4c6c42011-09-01 15:16:42 -070087
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080088mirror::Object* IsMarkedSweepingCallback(mirror::Object* object, void* arg) {
Mathieu Chartier6aa3df92013-09-17 15:17:28 -070089 if (reinterpret_cast<TestPredicate*>(arg)->IsMarked(object)) {
90 return object;
91 }
92 return nullptr;
Elliott Hughesc33a32b2011-10-11 18:18:07 -070093}
94
95TEST_F(InternTableTest, SweepInternTableWeaks) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070096 ScopedObjectAccess soa(Thread::Current());
Elliott Hughescf4c6c42011-09-01 15:16:42 -070097 InternTable t;
98 t.InternStrong(3, "foo");
99 t.InternStrong(3, "bar");
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700100 StackHandleScope<5> hs(soa.Self());
101 Handle<mirror::String> hello(
102 hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "hello")));
103 Handle<mirror::String> world(
104 hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "world")));
105 Handle<mirror::String> s0(hs.NewHandle(t.InternWeak(hello.Get())));
106 Handle<mirror::String> s1(hs.NewHandle(t.InternWeak(world.Get())));
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700107
108 EXPECT_EQ(4U, t.Size());
109
110 // We should traverse only the weaks...
Elliott Hughes410c0c82011-09-01 17:58:25 -0700111 TestPredicate p;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700112 p.Expect(s0.Get());
113 p.Expect(s1.Get());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700114 {
Ian Rogers1f539342012-10-03 21:09:42 -0700115 ReaderMutexLock mu(soa.Self(), *Locks::heap_bitmap_lock_);
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800116 t.SweepInternTableWeaks(IsMarkedSweepingCallback, &p);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700117 }
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700118
119 EXPECT_EQ(2U, t.Size());
120
Elliott Hughese5448b52012-01-18 16:44:06 -0800121 // Just check that we didn't corrupt the map.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700122 Handle<mirror::String> still_here(
123 hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "still here")));
124 t.InternWeak(still_here.Get());
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700125 EXPECT_EQ(3U, t.Size());
126}
127
128TEST_F(InternTableTest, ContainsWeak) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700129 ScopedObjectAccess soa(Thread::Current());
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700130 {
131 // Strongs are never weak.
132 InternTable t;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700133 StackHandleScope<2> hs(soa.Self());
134 Handle<mirror::String> interned_foo_1(hs.NewHandle(t.InternStrong(3, "foo")));
135 EXPECT_FALSE(t.ContainsWeak(interned_foo_1.Get()));
136 Handle<mirror::String> interned_foo_2(hs.NewHandle(t.InternStrong(3, "foo")));
137 EXPECT_FALSE(t.ContainsWeak(interned_foo_2.Get()));
138 EXPECT_EQ(interned_foo_1.Get(), interned_foo_2.Get());
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700139 }
140
141 {
142 // Weaks are always weak.
143 InternTable t;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700144 StackHandleScope<4> hs(soa.Self());
145 Handle<mirror::String> foo_1(
146 hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "foo")));
147 Handle<mirror::String> foo_2(
148 hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "foo")));
149 EXPECT_NE(foo_1.Get(), foo_2.Get());
150 Handle<mirror::String> interned_foo_1(hs.NewHandle(t.InternWeak(foo_1.Get())));
151 Handle<mirror::String> interned_foo_2(hs.NewHandle(t.InternWeak(foo_2.Get())));
152 EXPECT_TRUE(t.ContainsWeak(interned_foo_2.Get()));
153 EXPECT_EQ(interned_foo_1.Get(), interned_foo_2.Get());
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700154 }
155
156 {
157 // A weak can be promoted to a strong.
158 InternTable t;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700159 StackHandleScope<3> hs(soa.Self());
160 Handle<mirror::String> foo(
161 hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "foo")));
162 Handle<mirror::String> interned_foo_1(hs.NewHandle(t.InternWeak(foo.Get())));
163 EXPECT_TRUE(t.ContainsWeak(interned_foo_1.Get()));
164 Handle<mirror::String> interned_foo_2(hs.NewHandle(t.InternStrong(3, "foo")));
165 EXPECT_FALSE(t.ContainsWeak(interned_foo_2.Get()));
166 EXPECT_EQ(interned_foo_1.Get(), interned_foo_2.Get());
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700167 }
168
169 {
170 // Interning a weak after a strong gets you the strong.
171 InternTable t;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700172 StackHandleScope<3> hs(soa.Self());
173 Handle<mirror::String> interned_foo_1(hs.NewHandle(t.InternStrong(3, "foo")));
174 EXPECT_FALSE(t.ContainsWeak(interned_foo_1.Get()));
175 Handle<mirror::String> foo(
176 hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "foo")));
177 Handle<mirror::String> interned_foo_2(hs.NewHandle(t.InternWeak(foo.Get())));
178 EXPECT_FALSE(t.ContainsWeak(interned_foo_2.Get()));
179 EXPECT_EQ(interned_foo_1.Get(), interned_foo_2.Get());
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700180 }
181}
182
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700183} // namespace art