Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
Vladimir Marko | 76990bb | 2022-10-13 08:08:28 +0000 | [diff] [blame] | 19 | #include "base/common_art_test.h" |
| 20 | #include "thread-current-inl.h" |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 21 | |
| 22 | namespace art { |
| 23 | |
Vladimir Marko | 76990bb | 2022-10-13 08:08:28 +0000 | [diff] [blame] | 24 | class CHATest : public CommonArtTest {}; |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 25 | |
| 26 | // Mocks some methods. |
| 27 | #define METHOD1 (reinterpret_cast<ArtMethod*>(8u)) |
| 28 | #define METHOD2 (reinterpret_cast<ArtMethod*>(16u)) |
| 29 | #define METHOD3 (reinterpret_cast<ArtMethod*>(24u)) |
| 30 | |
| 31 | // Mocks some method headers. |
| 32 | #define METHOD_HEADER1 (reinterpret_cast<OatQuickMethodHeader*>(128u)) |
| 33 | #define METHOD_HEADER2 (reinterpret_cast<OatQuickMethodHeader*>(136u)) |
| 34 | #define METHOD_HEADER3 (reinterpret_cast<OatQuickMethodHeader*>(144u)) |
| 35 | |
| 36 | TEST_F(CHATest, CHACheckDependency) { |
| 37 | ClassHierarchyAnalysis cha; |
| 38 | MutexLock cha_mu(Thread::Current(), *Locks::cha_lock_); |
| 39 | |
Mingyao Yang | cc10450 | 2017-05-24 17:13:03 -0700 | [diff] [blame] | 40 | ASSERT_TRUE(cha.GetDependents(METHOD1).empty()); |
| 41 | ASSERT_TRUE(cha.GetDependents(METHOD2).empty()); |
| 42 | ASSERT_TRUE(cha.GetDependents(METHOD3).empty()); |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 43 | |
| 44 | cha.AddDependency(METHOD1, METHOD2, METHOD_HEADER2); |
Mingyao Yang | cc10450 | 2017-05-24 17:13:03 -0700 | [diff] [blame] | 45 | ASSERT_TRUE(cha.GetDependents(METHOD2).empty()); |
| 46 | ASSERT_TRUE(cha.GetDependents(METHOD3).empty()); |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 47 | auto dependents = cha.GetDependents(METHOD1); |
Mingyao Yang | cc10450 | 2017-05-24 17:13:03 -0700 | [diff] [blame] | 48 | ASSERT_EQ(dependents.size(), 1u); |
| 49 | ASSERT_EQ(dependents[0].first, METHOD2); |
| 50 | ASSERT_EQ(dependents[0].second, METHOD_HEADER2); |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 51 | |
| 52 | cha.AddDependency(METHOD1, METHOD3, METHOD_HEADER3); |
Mingyao Yang | cc10450 | 2017-05-24 17:13:03 -0700 | [diff] [blame] | 53 | ASSERT_TRUE(cha.GetDependents(METHOD2).empty()); |
| 54 | ASSERT_TRUE(cha.GetDependents(METHOD3).empty()); |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 55 | dependents = cha.GetDependents(METHOD1); |
Mingyao Yang | cc10450 | 2017-05-24 17:13:03 -0700 | [diff] [blame] | 56 | ASSERT_EQ(dependents.size(), 2u); |
| 57 | ASSERT_EQ(dependents[0].first, METHOD2); |
| 58 | ASSERT_EQ(dependents[0].second, METHOD_HEADER2); |
| 59 | ASSERT_EQ(dependents[1].first, METHOD3); |
| 60 | ASSERT_EQ(dependents[1].second, METHOD_HEADER3); |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 61 | |
| 62 | std::unordered_set<OatQuickMethodHeader*> headers; |
| 63 | headers.insert(METHOD_HEADER2); |
| 64 | cha.RemoveDependentsWithMethodHeaders(headers); |
Mingyao Yang | cc10450 | 2017-05-24 17:13:03 -0700 | [diff] [blame] | 65 | ASSERT_TRUE(cha.GetDependents(METHOD2).empty()); |
| 66 | ASSERT_TRUE(cha.GetDependents(METHOD3).empty()); |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 67 | dependents = cha.GetDependents(METHOD1); |
Mingyao Yang | cc10450 | 2017-05-24 17:13:03 -0700 | [diff] [blame] | 68 | ASSERT_EQ(dependents.size(), 1u); |
| 69 | ASSERT_EQ(dependents[0].first, METHOD3); |
| 70 | ASSERT_EQ(dependents[0].second, METHOD_HEADER3); |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 71 | |
| 72 | cha.AddDependency(METHOD2, METHOD1, METHOD_HEADER1); |
Mingyao Yang | cc10450 | 2017-05-24 17:13:03 -0700 | [diff] [blame] | 73 | ASSERT_TRUE(cha.GetDependents(METHOD3).empty()); |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 74 | dependents = cha.GetDependents(METHOD1); |
Mingyao Yang | cc10450 | 2017-05-24 17:13:03 -0700 | [diff] [blame] | 75 | ASSERT_EQ(dependents.size(), 1u); |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 76 | dependents = cha.GetDependents(METHOD2); |
Mingyao Yang | cc10450 | 2017-05-24 17:13:03 -0700 | [diff] [blame] | 77 | ASSERT_EQ(dependents.size(), 1u); |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 78 | |
| 79 | headers.insert(METHOD_HEADER3); |
| 80 | cha.RemoveDependentsWithMethodHeaders(headers); |
Mingyao Yang | cc10450 | 2017-05-24 17:13:03 -0700 | [diff] [blame] | 81 | ASSERT_TRUE(cha.GetDependents(METHOD1).empty()); |
| 82 | ASSERT_TRUE(cha.GetDependents(METHOD3).empty()); |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 83 | dependents = cha.GetDependents(METHOD2); |
Mingyao Yang | cc10450 | 2017-05-24 17:13:03 -0700 | [diff] [blame] | 84 | ASSERT_EQ(dependents.size(), 1u); |
| 85 | ASSERT_EQ(dependents[0].first, METHOD1); |
| 86 | ASSERT_EQ(dependents[0].second, METHOD_HEADER1); |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 87 | |
Mingyao Yang | cc10450 | 2017-05-24 17:13:03 -0700 | [diff] [blame] | 88 | cha.RemoveAllDependenciesFor(METHOD2); |
| 89 | ASSERT_TRUE(cha.GetDependents(METHOD1).empty()); |
| 90 | ASSERT_TRUE(cha.GetDependents(METHOD2).empty()); |
| 91 | ASSERT_TRUE(cha.GetDependents(METHOD3).empty()); |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | } // namespace art |