1 /**
2 * Copyright (c) 2023-2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "gtest/gtest.h"
17
18 #include "runtime.h"
19 #include "runtime_options.h"
20 #include "source_manager.h"
21
22 // NOLINTBEGIN
23
24 namespace ark::tooling::inspector::test {
25 class SourceManagerTest : public testing::Test {
26 protected:
SetUp()27 void SetUp()
28 {
29 RuntimeOptions options;
30 options.SetShouldInitializeIntrinsics(false);
31 options.SetShouldLoadBootPandaFiles(false);
32 Runtime::Create(options);
33 }
TearDown()34 void TearDown()
35 {
36 Runtime::Destroy();
37 }
38
39 SourceManager sm_;
40 };
41
RunManagedThread(std::atomic<bool> * sync_flag,PtThread * thread)42 void RunManagedThread(std::atomic<bool> *sync_flag, [[maybe_unused]] PtThread *thread)
43 {
44 auto *m_thr = MTManagedThread::Create(Runtime::GetCurrent(), Runtime::GetCurrent()->GetPandaVM());
45 auto pt_thr = PtThread(m_thr);
46 *thread = pt_thr;
47
48 *sync_flag = true;
49
50 while (*sync_flag) {
51 }
52 m_thr->Destroy();
53 }
54
TEST_F(SourceManagerTest,General)55 TEST_F(SourceManagerTest, General)
56 {
57 std::atomic<bool> sync_flag1 = false;
58 PtThread pt_thread1 = PtThread::NONE;
59 std::thread mthread1(RunManagedThread, &sync_flag1, &pt_thread1);
60
61 while (!sync_flag1) {
62 ;
63 }
64
65 auto test_id0 = sm_.GetScriptId("test.pa");
66 ASSERT_EQ(test_id0.first, ScriptId(0));
67 ASSERT_EQ(test_id0.second, true);
68
69 ASSERT_EQ(sm_.GetSourceFileName(test_id0.first), "test.pa");
70 ASSERT_EQ(sm_.GetSourceFileName(ScriptId(1)), "");
71
72 test_id0 = sm_.GetScriptId("test.pa");
73 ASSERT_EQ(test_id0.first, ScriptId(0));
74 ASSERT_EQ(test_id0.second, false);
75
76 auto test_id1 = sm_.GetScriptId("test1.pa");
77 ASSERT_EQ(test_id1.first, ScriptId(1));
78 ASSERT_EQ(test_id1.second, true);
79
80 auto test_id2 = sm_.GetScriptId("test2.pa");
81 auto test_id3 = sm_.GetScriptId("test3.pa");
82 ASSERT_EQ(sm_.GetSourceFileName(test_id2.first), "test2.pa");
83 ASSERT_EQ(sm_.GetSourceFileName(test_id3.first), "test3.pa");
84
85 ASSERT_EQ(sm_.GetSourceFileName(ScriptId(5U)), "");
86
87 sync_flag1 = false;
88
89 mthread1.join();
90 }
91
92 } // namespace ark::tooling::inspector::test
93
94 // NOLINTEND
95