1 /**
2 * Copyright (c) 2021-2024 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 <vector>
19
20 #include "assembly-parser.h"
21 #include "runtime/include/method.h"
22 #include "runtime/include/runtime.h"
23
24 #include "runtime/compiler.h"
25
26 namespace ark::test {
27
28 class CompilationStatusTest : public testing::Test {
29 public:
30 static const size_t METHOD_COUNT = 1;
CompilationStatusTest()31 CompilationStatusTest()
32 {
33 RuntimeOptions options;
34 options.SetShouldLoadBootPandaFiles(false);
35 options.SetShouldInitializeIntrinsics(false);
36 Runtime::Create(options);
37 thread_ = ark::MTManagedThread::GetCurrent();
38 thread_->ManagedCodeBegin();
39 }
40
41 Method *GetMethod(Class *klass, size_t num);
42 Class *GetClass();
43
~CompilationStatusTest()44 ~CompilationStatusTest() override
45 {
46 thread_->ManagedCodeEnd();
47 Runtime::Destroy();
48 }
49
50 NO_COPY_SEMANTIC(CompilationStatusTest);
51 NO_MOVE_SEMANTIC(CompilationStatusTest);
52
53 private:
54 ark::MTManagedThread *thread_;
55 };
56
GetMethod(Class * klass,size_t num)57 Method *CompilationStatusTest::GetMethod(Class *klass, size_t num)
58 {
59 PandaStringStream ss;
60 ss << "CompilationStatusTest" << num;
61 Method *method = klass->GetDirectMethod(utf::CStringAsMutf8(ss.str().c_str()));
62 return method;
63 }
64
GetClass()65 Class *CompilationStatusTest::GetClass()
66 {
67 pandasm::Parser p;
68
69 PandaStringStream ss;
70
71 for (size_t i = 0; i < CompilationStatusTest::METHOD_COUNT; i++) {
72 ss << ".function void CompilationStatusTest" << i << "() {" << std::endl;
73 ss << " return.void" << std::endl;
74 ss << "}" << std::endl;
75 }
76
77 auto source = ss.str();
78 // NOLINTNEXTLINE(readability-redundant-string-cstr)
79 auto res = p.Parse(source.c_str());
80 auto pf = pandasm::AsmEmitter::Emit(res.Value());
81
82 ClassLinker *classLinker = Runtime::GetCurrent()->GetClassLinker();
83 classLinker->AddPandaFile(std::move(pf));
84 auto *extension = classLinker->GetExtension(panda_file::SourceLang::PANDA_ASSEMBLY);
85
86 PandaString descriptor;
87
88 return extension->GetClass(ClassHelper::GetDescriptor(utf::CStringAsMutf8("_GLOBAL"), &descriptor));
89 }
90
TEST_F(CompilationStatusTest,Status)91 TEST_F(CompilationStatusTest, Status)
92 {
93 auto *klass = GetClass();
94 ASSERT_NE(klass, nullptr);
95 Method *method = GetMethod(klass, 0);
96 auto status = method->GetCompilationStatus();
97 ASSERT_EQ(status, Method::NOT_COMPILED);
98
99 ASSERT_EQ(method->AtomicSetCompilationStatus(status, Method::WAITING), true);
100 status = method->GetCompilationStatus();
101 ASSERT_EQ(status, Method::WAITING);
102
103 ASSERT_EQ(method->AtomicSetCompilationStatus(status, Method::COMPILATION), true);
104 status = method->GetCompilationStatus();
105 ASSERT_EQ(status, Method::COMPILATION);
106
107 ASSERT_EQ(method->AtomicSetCompilationStatus(status, Method::COMPILED), true);
108 status = method->GetCompilationStatus();
109 ASSERT_EQ(status, Method::COMPILED);
110
111 ASSERT_EQ(method->AtomicSetCompilationStatus(status, Method::FAILED), true);
112 status = method->GetCompilationStatus();
113 ASSERT_EQ(status, Method::FAILED);
114 }
115
116 } // namespace ark::test
117