• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 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 "intrinsics.h"
17 #include <gtest/gtest.h>
18 #include "ets_coroutine.h"
19 #include "types/ets_string.h"
20 #include "types/ets_array.h"
21 #include "tests/runtime/types/ets_test_mirror_classes.h"
22 #include "types/ets_string_builder.h"
23 #include "libpandabase/utils/utf.h"
24 
25 // NOLINTBEGIN(readability-magic-numbers)
26 // NOLINTBEGIN(cppcoreguidelines-pro-bounds-pointer-arithmetic)
27 
28 namespace ark::ets::test {
29 
30 class EtsStringBuilderMembersTest : public testing::Test {
31 public:
EtsStringBuilderMembersTest()32     EtsStringBuilderMembersTest()
33     {
34         options_.SetShouldLoadBootPandaFiles(true);
35         options_.SetShouldInitializeIntrinsics(false);
36         options_.SetCompilerEnableJit(false);
37         options_.SetGcType("epsilon");
38         options_.SetLoadRuntimes({"ets"});
39 
40         auto stdlib = std::getenv("PANDA_STD_LIB");
41         if (stdlib == nullptr) {
42             std::cerr << "PANDA_STD_LIB env variable should be set and point to mock_stdlib.abc" << std::endl;
43             std::abort();
44         }
45         options_.SetBootPandaFiles({stdlib});
46 
47         Runtime::Create(options_);
48         vm_ = EtsCoroutine::GetCurrent()->GetPandaVM();
49     }
50 
~EtsStringBuilderMembersTest()51     ~EtsStringBuilderMembersTest() override
52     {
53         Runtime::Destroy();
54     }
55 
56     NO_COPY_SEMANTIC(EtsStringBuilderMembersTest);
57     NO_MOVE_SEMANTIC(EtsStringBuilderMembersTest);
58 
SetUp()59     void SetUp() override
60     {
61         coroutine_ = EtsCoroutine::GetCurrent();
62         coroutine_->ManagedCodeBegin();
63     }
64 
TearDown()65     void TearDown() override
66     {
67         coroutine_->ManagedCodeEnd();
68     }
69 
GetMembers()70     static std::vector<MirrorFieldInfo> GetMembers()
71     {
72         return std::vector<MirrorFieldInfo> {MIRROR_FIELD_INFO(EtsStringBuilder, buf_, "buf"),
73                                              MIRROR_FIELD_INFO(EtsStringBuilder, index_, "index"),
74                                              MIRROR_FIELD_INFO(EtsStringBuilder, length_, "length"),
75                                              MIRROR_FIELD_INFO(EtsStringBuilder, compress_, "compress")};
76     }
77 
78 protected:
79     PandaEtsVM *vm_ = nullptr;  // NOLINT(misc-non-private-member-variables-in-classes)
80 
81 private:
82     RuntimeOptions options_;
83     EtsCoroutine *coroutine_ = nullptr;
84 };
85 
TEST_F(EtsStringBuilderMembersTest,MemoryLayout)86 TEST_F(EtsStringBuilderMembersTest, MemoryLayout)
87 {
88     EtsClass *klass = vm_->GetClassLinker()->GetStringBuilderClass();
89     MirrorFieldInfo::CompareMemberOffsets(klass, GetMembers());
90 }
91 
92 }  // namespace ark::ets::test
93 
94 // NOLINTEND(cppcoreguidelines-pro-bounds-pointer-arithmetic)
95 // NOLINTEND(readability-magic-numbers)
96