1 /**
2 * Copyright (c) 2021-2022 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 panda::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_ = panda::MTManagedThread::GetCurrent();
38 thread_->ManagedCodeBegin();
39 }
40
41 Method *GetMethod(Class *klass, size_t num);
42 Class *GetClass();
43
~CompilationStatusTest()44 ~CompilationStatusTest()
45 {
46 thread_->ManagedCodeEnd();
47 Runtime::Destroy();
48 }
49
50 protected:
51 panda::MTManagedThread *thread_;
52 };
53
GetMethod(Class * klass,size_t num)54 Method *CompilationStatusTest::GetMethod(Class *klass, size_t num)
55 {
56 PandaStringStream ss;
57 ss << "CompilationStatusTest" << num;
58 Method *method = klass->GetDirectMethod(utf::CStringAsMutf8(ss.str().c_str()));
59 return method;
60 }
61
GetClass()62 Class *CompilationStatusTest::GetClass()
63 {
64 pandasm::Parser p;
65
66 PandaStringStream ss;
67
68 for (size_t i = 0; i < CompilationStatusTest::METHOD_COUNT; i++) {
69 ss << ".function void CompilationStatusTest" << i << "() {" << std::endl;
70 ss << " return.void" << std::endl;
71 ss << "}" << std::endl;
72 }
73
74 auto source = ss.str();
75 auto res = p.Parse(source.c_str());
76 auto pf = pandasm::AsmEmitter::Emit(res.Value());
77
78 ClassLinker *class_linker = Runtime::GetCurrent()->GetClassLinker();
79 class_linker->AddPandaFile(std::move(pf));
80 auto *extension = class_linker->GetExtension(panda_file::SourceLang::PANDA_ASSEMBLY);
81
82 PandaString descriptor;
83
84 return extension->GetClass(ClassHelper::GetDescriptor(utf::CStringAsMutf8("_GLOBAL"), &descriptor));
85 }
86
TEST_F(CompilationStatusTest,Status)87 TEST_F(CompilationStatusTest, Status)
88 {
89 auto *klass = GetClass();
90 ASSERT_NE(klass, nullptr);
91 Method *method = GetMethod(klass, 0);
92 auto status = method->GetCompilationStatus();
93 ASSERT_EQ(status, Method::NOT_COMPILED);
94
95 ASSERT_EQ(method->AtomicSetCompilationStatus(status, Method::WAITING), true);
96 status = method->GetCompilationStatus();
97 ASSERT_EQ(status, Method::WAITING);
98
99 ASSERT_EQ(method->AtomicSetCompilationStatus(status, Method::COMPILATION), true);
100 status = method->GetCompilationStatus();
101 ASSERT_EQ(status, Method::COMPILATION);
102
103 ASSERT_EQ(method->AtomicSetCompilationStatus(status, Method::COMPILED), true);
104 status = method->GetCompilationStatus();
105 ASSERT_EQ(status, Method::COMPILED);
106
107 ASSERT_EQ(method->AtomicSetCompilationStatus(status, Method::FAILED), true);
108 status = method->GetCompilationStatus();
109 ASSERT_EQ(status, Method::FAILED);
110 }
111
112 } // namespace panda::test
113