blob: b60b32d06b746ed7dab1db741f67cf46c9afe403 [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"
Ian Rogerse63db272014-07-15 15:36:11 -070022#include "mirror/string.h"
23#include "scoped_thread_state_change.h"
Brian Carlstrom7e93b502011-08-04 14:16:22 -070024
Brian Carlstrom7e93b502011-08-04 14:16:22 -070025namespace art {
26
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080027class InternTableTest : public CommonRuntimeTest {};
Brian Carlstrom7e93b502011-08-04 14:16:22 -070028
29TEST_F(InternTableTest, Intern) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070030 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom7e93b502011-08-04 14:16:22 -070031 InternTable intern_table;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070032 StackHandleScope<4> hs(soa.Self());
33 Handle<mirror::String> foo_1(hs.NewHandle(intern_table.InternStrong(3, "foo")));
34 Handle<mirror::String> foo_2(hs.NewHandle(intern_table.InternStrong(3, "foo")));
35 Handle<mirror::String> foo_3(
36 hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "foo")));
37 Handle<mirror::String> bar(hs.NewHandle(intern_table.InternStrong(3, "bar")));
Brian Carlstrom7e93b502011-08-04 14:16:22 -070038 EXPECT_TRUE(foo_1->Equals("foo"));
39 EXPECT_TRUE(foo_2->Equals("foo"));
40 EXPECT_TRUE(foo_3->Equals("foo"));
Mathieu Chartier2cebb242015-04-21 16:50:40 -070041 EXPECT_TRUE(foo_1.Get() != nullptr);
42 EXPECT_TRUE(foo_2.Get() != nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070043 EXPECT_EQ(foo_1.Get(), foo_2.Get());
44 EXPECT_NE(foo_1.Get(), bar.Get());
45 EXPECT_NE(foo_2.Get(), bar.Get());
46 EXPECT_NE(foo_3.Get(), bar.Get());
Brian Carlstrom7e93b502011-08-04 14:16:22 -070047}
48
Elliott Hughescf4c6c42011-09-01 15:16:42 -070049TEST_F(InternTableTest, Size) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070050 ScopedObjectAccess soa(Thread::Current());
Elliott Hughescf4c6c42011-09-01 15:16:42 -070051 InternTable t;
52 EXPECT_EQ(0U, t.Size());
53 t.InternStrong(3, "foo");
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070054 StackHandleScope<1> hs(soa.Self());
55 Handle<mirror::String> foo(
56 hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "foo")));
57 t.InternWeak(foo.Get());
Elliott Hughescf4c6c42011-09-01 15:16:42 -070058 EXPECT_EQ(1U, t.Size());
59 t.InternStrong(3, "bar");
60 EXPECT_EQ(2U, t.Size());
61}
62
Mathieu Chartier97509952015-07-13 14:35:43 -070063class TestPredicate : public IsMarkedVisitor {
Elliott Hughes410c0c82011-09-01 17:58:25 -070064 public:
Mathieu Chartier90443472015-07-16 20:32:27 -070065 mirror::Object* IsMarked(mirror::Object* s) OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
Elliott Hughes410c0c82011-09-01 17:58:25 -070066 bool erased = false;
Mathieu Chartier02e25112013-08-14 16:14:24 -070067 for (auto it = expected_.begin(), end = expected_.end(); it != end; ++it) {
Elliott Hughes410c0c82011-09-01 17:58:25 -070068 if (*it == s) {
69 expected_.erase(it);
70 erased = true;
71 break;
72 }
Elliott Hughescf4c6c42011-09-01 15:16:42 -070073 }
Elliott Hughes410c0c82011-09-01 17:58:25 -070074 EXPECT_TRUE(erased);
Mathieu Chartier97509952015-07-13 14:35:43 -070075 return nullptr;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070076 }
Elliott Hughes410c0c82011-09-01 17:58:25 -070077
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080078 void Expect(const mirror::String* s) {
Elliott Hughes410c0c82011-09-01 17:58:25 -070079 expected_.push_back(s);
80 }
81
82 ~TestPredicate() {
83 EXPECT_EQ(0U, expected_.size());
84 }
85
86 private:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080087 mutable std::vector<const mirror::String*> expected_;
Elliott Hughes410c0c82011-09-01 17:58:25 -070088};
Elliott Hughescf4c6c42011-09-01 15:16:42 -070089
Elliott Hughesc33a32b2011-10-11 18:18:07 -070090TEST_F(InternTableTest, SweepInternTableWeaks) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070091 ScopedObjectAccess soa(Thread::Current());
Elliott Hughescf4c6c42011-09-01 15:16:42 -070092 InternTable t;
93 t.InternStrong(3, "foo");
94 t.InternStrong(3, "bar");
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070095 StackHandleScope<5> hs(soa.Self());
96 Handle<mirror::String> hello(
97 hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "hello")));
98 Handle<mirror::String> world(
99 hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "world")));
100 Handle<mirror::String> s0(hs.NewHandle(t.InternWeak(hello.Get())));
101 Handle<mirror::String> s1(hs.NewHandle(t.InternWeak(world.Get())));
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700102
103 EXPECT_EQ(4U, t.Size());
104
105 // We should traverse only the weaks...
Elliott Hughes410c0c82011-09-01 17:58:25 -0700106 TestPredicate p;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700107 p.Expect(s0.Get());
108 p.Expect(s1.Get());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700109 {
Ian Rogers1f539342012-10-03 21:09:42 -0700110 ReaderMutexLock mu(soa.Self(), *Locks::heap_bitmap_lock_);
Mathieu Chartier97509952015-07-13 14:35:43 -0700111 t.SweepInternTableWeaks(&p);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700112 }
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700113
114 EXPECT_EQ(2U, t.Size());
115
Elliott Hughese5448b52012-01-18 16:44:06 -0800116 // Just check that we didn't corrupt the map.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700117 Handle<mirror::String> still_here(
118 hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "still here")));
119 t.InternWeak(still_here.Get());
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700120 EXPECT_EQ(3U, t.Size());
121}
122
123TEST_F(InternTableTest, ContainsWeak) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700124 ScopedObjectAccess soa(Thread::Current());
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700125 {
126 // Strongs are never weak.
127 InternTable t;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700128 StackHandleScope<2> hs(soa.Self());
129 Handle<mirror::String> interned_foo_1(hs.NewHandle(t.InternStrong(3, "foo")));
130 EXPECT_FALSE(t.ContainsWeak(interned_foo_1.Get()));
131 Handle<mirror::String> interned_foo_2(hs.NewHandle(t.InternStrong(3, "foo")));
132 EXPECT_FALSE(t.ContainsWeak(interned_foo_2.Get()));
133 EXPECT_EQ(interned_foo_1.Get(), interned_foo_2.Get());
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700134 }
135
136 {
137 // Weaks are always weak.
138 InternTable t;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700139 StackHandleScope<4> hs(soa.Self());
140 Handle<mirror::String> foo_1(
141 hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "foo")));
142 Handle<mirror::String> foo_2(
143 hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "foo")));
144 EXPECT_NE(foo_1.Get(), foo_2.Get());
145 Handle<mirror::String> interned_foo_1(hs.NewHandle(t.InternWeak(foo_1.Get())));
146 Handle<mirror::String> interned_foo_2(hs.NewHandle(t.InternWeak(foo_2.Get())));
147 EXPECT_TRUE(t.ContainsWeak(interned_foo_2.Get()));
148 EXPECT_EQ(interned_foo_1.Get(), interned_foo_2.Get());
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700149 }
150
151 {
152 // A weak can be promoted to a strong.
153 InternTable t;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700154 StackHandleScope<3> hs(soa.Self());
155 Handle<mirror::String> foo(
156 hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "foo")));
157 Handle<mirror::String> interned_foo_1(hs.NewHandle(t.InternWeak(foo.Get())));
158 EXPECT_TRUE(t.ContainsWeak(interned_foo_1.Get()));
159 Handle<mirror::String> interned_foo_2(hs.NewHandle(t.InternStrong(3, "foo")));
160 EXPECT_FALSE(t.ContainsWeak(interned_foo_2.Get()));
161 EXPECT_EQ(interned_foo_1.Get(), interned_foo_2.Get());
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700162 }
163
164 {
165 // Interning a weak after a strong gets you the strong.
166 InternTable t;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700167 StackHandleScope<3> hs(soa.Self());
168 Handle<mirror::String> interned_foo_1(hs.NewHandle(t.InternStrong(3, "foo")));
169 EXPECT_FALSE(t.ContainsWeak(interned_foo_1.Get()));
170 Handle<mirror::String> foo(
171 hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "foo")));
172 Handle<mirror::String> interned_foo_2(hs.NewHandle(t.InternWeak(foo.Get())));
173 EXPECT_FALSE(t.ContainsWeak(interned_foo_2.Get()));
174 EXPECT_EQ(interned_foo_1.Get(), interned_foo_2.Get());
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700175 }
176}
177
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700178} // namespace art