blob: c60720f4d3c59efbf184c0d793fa313cd2dd8612 [file] [log] [blame]
Mingyao Yang063fc772016-08-02 11:02:54 -07001/*
2 * Copyright (C) 2016 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 */
16
17#include "cha.h"
18
19#include "common_runtime_test.h"
20
21namespace art {
22
23class CHATest : public CommonRuntimeTest {};
24
25// Mocks some methods.
26#define METHOD1 (reinterpret_cast<ArtMethod*>(8u))
27#define METHOD2 (reinterpret_cast<ArtMethod*>(16u))
28#define METHOD3 (reinterpret_cast<ArtMethod*>(24u))
29
30// Mocks some method headers.
31#define METHOD_HEADER1 (reinterpret_cast<OatQuickMethodHeader*>(128u))
32#define METHOD_HEADER2 (reinterpret_cast<OatQuickMethodHeader*>(136u))
33#define METHOD_HEADER3 (reinterpret_cast<OatQuickMethodHeader*>(144u))
34
35TEST_F(CHATest, CHACheckDependency) {
36 ClassHierarchyAnalysis cha;
37 MutexLock cha_mu(Thread::Current(), *Locks::cha_lock_);
38
Mingyao Yangcc104502017-05-24 17:13:03 -070039 ASSERT_TRUE(cha.GetDependents(METHOD1).empty());
40 ASSERT_TRUE(cha.GetDependents(METHOD2).empty());
41 ASSERT_TRUE(cha.GetDependents(METHOD3).empty());
Mingyao Yang063fc772016-08-02 11:02:54 -070042
43 cha.AddDependency(METHOD1, METHOD2, METHOD_HEADER2);
Mingyao Yangcc104502017-05-24 17:13:03 -070044 ASSERT_TRUE(cha.GetDependents(METHOD2).empty());
45 ASSERT_TRUE(cha.GetDependents(METHOD3).empty());
Mingyao Yang063fc772016-08-02 11:02:54 -070046 auto dependents = cha.GetDependents(METHOD1);
Mingyao Yangcc104502017-05-24 17:13:03 -070047 ASSERT_EQ(dependents.size(), 1u);
48 ASSERT_EQ(dependents[0].first, METHOD2);
49 ASSERT_EQ(dependents[0].second, METHOD_HEADER2);
Mingyao Yang063fc772016-08-02 11:02:54 -070050
51 cha.AddDependency(METHOD1, METHOD3, METHOD_HEADER3);
Mingyao Yangcc104502017-05-24 17:13:03 -070052 ASSERT_TRUE(cha.GetDependents(METHOD2).empty());
53 ASSERT_TRUE(cha.GetDependents(METHOD3).empty());
Mingyao Yang063fc772016-08-02 11:02:54 -070054 dependents = cha.GetDependents(METHOD1);
Mingyao Yangcc104502017-05-24 17:13:03 -070055 ASSERT_EQ(dependents.size(), 2u);
56 ASSERT_EQ(dependents[0].first, METHOD2);
57 ASSERT_EQ(dependents[0].second, METHOD_HEADER2);
58 ASSERT_EQ(dependents[1].first, METHOD3);
59 ASSERT_EQ(dependents[1].second, METHOD_HEADER3);
Mingyao Yang063fc772016-08-02 11:02:54 -070060
61 std::unordered_set<OatQuickMethodHeader*> headers;
62 headers.insert(METHOD_HEADER2);
63 cha.RemoveDependentsWithMethodHeaders(headers);
Mingyao Yangcc104502017-05-24 17:13:03 -070064 ASSERT_TRUE(cha.GetDependents(METHOD2).empty());
65 ASSERT_TRUE(cha.GetDependents(METHOD3).empty());
Mingyao Yang063fc772016-08-02 11:02:54 -070066 dependents = cha.GetDependents(METHOD1);
Mingyao Yangcc104502017-05-24 17:13:03 -070067 ASSERT_EQ(dependents.size(), 1u);
68 ASSERT_EQ(dependents[0].first, METHOD3);
69 ASSERT_EQ(dependents[0].second, METHOD_HEADER3);
Mingyao Yang063fc772016-08-02 11:02:54 -070070
71 cha.AddDependency(METHOD2, METHOD1, METHOD_HEADER1);
Mingyao Yangcc104502017-05-24 17:13:03 -070072 ASSERT_TRUE(cha.GetDependents(METHOD3).empty());
Mingyao Yang063fc772016-08-02 11:02:54 -070073 dependents = cha.GetDependents(METHOD1);
Mingyao Yangcc104502017-05-24 17:13:03 -070074 ASSERT_EQ(dependents.size(), 1u);
Mingyao Yang063fc772016-08-02 11:02:54 -070075 dependents = cha.GetDependents(METHOD2);
Mingyao Yangcc104502017-05-24 17:13:03 -070076 ASSERT_EQ(dependents.size(), 1u);
Mingyao Yang063fc772016-08-02 11:02:54 -070077
78 headers.insert(METHOD_HEADER3);
79 cha.RemoveDependentsWithMethodHeaders(headers);
Mingyao Yangcc104502017-05-24 17:13:03 -070080 ASSERT_TRUE(cha.GetDependents(METHOD1).empty());
81 ASSERT_TRUE(cha.GetDependents(METHOD3).empty());
Mingyao Yang063fc772016-08-02 11:02:54 -070082 dependents = cha.GetDependents(METHOD2);
Mingyao Yangcc104502017-05-24 17:13:03 -070083 ASSERT_EQ(dependents.size(), 1u);
84 ASSERT_EQ(dependents[0].first, METHOD1);
85 ASSERT_EQ(dependents[0].second, METHOD_HEADER1);
Mingyao Yang063fc772016-08-02 11:02:54 -070086
Mingyao Yangcc104502017-05-24 17:13:03 -070087 cha.RemoveAllDependenciesFor(METHOD2);
88 ASSERT_TRUE(cha.GetDependents(METHOD1).empty());
89 ASSERT_TRUE(cha.GetDependents(METHOD2).empty());
90 ASSERT_TRUE(cha.GetDependents(METHOD3).empty());
Mingyao Yang063fc772016-08-02 11:02:54 -070091}
92
93} // namespace art