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 "get_test_class.h"
19 #include "ets_coroutine.h"
20
21 #include "types/ets_class.h"
22 #include "types/ets_method.h"
23 #include "tests/runtime/types/ets_test_mirror_classes.h"
24
25 // NOLINTBEGIN(readability-magic-numbers)
26
27 namespace ark::ets::test {
28
29 class EtsClassTest : public testing::Test {
30 public:
EtsClassTest()31 EtsClassTest()
32 {
33 options_.SetShouldLoadBootPandaFiles(true);
34 options_.SetShouldInitializeIntrinsics(false);
35 options_.SetCompilerEnableJit(false);
36 options_.SetGcType("epsilon");
37 options_.SetLoadRuntimes({"ets"});
38
39 auto stdlib = std::getenv("PANDA_STD_LIB");
40 if (stdlib == nullptr) {
41 std::cerr << "PANDA_STD_LIB env variable should be set and point to mock_stdlib.abc" << std::endl;
42 std::abort();
43 }
44 options_.SetBootPandaFiles({stdlib});
45
46 Runtime::Create(options_);
47 }
48
~EtsClassTest()49 ~EtsClassTest() override
50 {
51 Runtime::Destroy();
52 }
53
54 NO_COPY_SEMANTIC(EtsClassTest);
55 NO_MOVE_SEMANTIC(EtsClassTest);
56
SetUp()57 void SetUp() override
58 {
59 coroutine_ = EtsCoroutine::GetCurrent();
60 vm_ = coroutine_->GetPandaVM();
61 coroutine_->ManagedCodeBegin();
62 }
63
TearDown()64 void TearDown() override
65 {
66 coroutine_->ManagedCodeEnd();
67 }
68
GetClassMembers()69 static std::vector<MirrorFieldInfo> GetClassMembers()
70 {
71 return std::vector<MirrorFieldInfo> {MIRROR_FIELD_INFO(EtsClass, name_, "name"),
72 MIRROR_FIELD_INFO(EtsClass, superClass_, "superClass"),
73 MIRROR_FIELD_INFO(EtsClass, flags_, "flags")};
74 }
75
76 protected:
77 PandaEtsVM *vm_ = nullptr; // NOLINT(misc-non-private-member-variables-in-classes)
78
79 private:
80 RuntimeOptions options_;
81 EtsCoroutine *coroutine_ = nullptr;
82 };
83
TEST_F(EtsClassTest,ClassMemoryLayout)84 TEST_F(EtsClassTest, ClassMemoryLayout)
85 {
86 EtsClass *classClass = vm_->GetClassLinker()->GetClassRoot(EtsClassRoot::CLASS);
87 MirrorFieldInfo::CompareMemberOffsets(classClass, GetClassMembers());
88 }
89
90 } // namespace ark::ets::test
91
92 // NOLINTEND(readability-magic-numbers)
93