1 /**
2 * Copyright (c) 2024-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 "ets_coroutine.h"
19
20 #include "types/ets_class.h"
21 #include "types/ets_arraybuffer.h"
22 #include "tests/runtime/types/ets_test_mirror_classes.h"
23 #include "types/ets_escompat_array.h"
24
25 namespace ark::ets::test {
26
27 template <class ElementType>
28 class EtsArrayObjectMembers : public testing::Test {
29 public:
EtsArrayObjectMembers()30 EtsArrayObjectMembers()
31 {
32 RuntimeOptions options;
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 EtsCoroutine *coroutine = EtsCoroutine::GetCurrent();
48 vm_ = coroutine->GetPandaVM();
49 }
50
~EtsArrayObjectMembers()51 ~EtsArrayObjectMembers() override
52 {
53 Runtime::Destroy();
54 }
55
56 NO_COPY_SEMANTIC(EtsArrayObjectMembers);
57 NO_MOVE_SEMANTIC(EtsArrayObjectMembers);
58
GetMembers()59 static std::vector<MirrorFieldInfo> GetMembers()
60 {
61 return std::vector<MirrorFieldInfo> {
62 MIRROR_FIELD_INFO(EtsArrayObject<ElementType>, actualLength_, "actualLength"),
63 MIRROR_FIELD_INFO(EtsArrayObject<ElementType>, buffer_, "buffer")};
64 }
65
66 protected:
67 PandaEtsVM *vm_ = nullptr; // NOLINT(misc-non-private-member-variables-in-classes)
68 };
69
70 using EtsArrayBoxedBooleanTest = EtsArrayObjectMembers<EtsBoxPrimitive<EtsBoolean>>;
71 using EtsArrayBoxedByteTest = EtsArrayObjectMembers<EtsBoxPrimitive<EtsByte>>;
72 using EtsArrayBoxedCharTest = EtsArrayObjectMembers<EtsBoxPrimitive<EtsChar>>;
73 using EtsArrayBoxedShortTest = EtsArrayObjectMembers<EtsBoxPrimitive<EtsShort>>;
74 using EtsArrayBoxedIntTest = EtsArrayObjectMembers<EtsBoxPrimitive<EtsInt>>;
75 using EtsArrayBoxedLongTest = EtsArrayObjectMembers<EtsBoxPrimitive<EtsLong>>;
76 using EtsArrayBoxedFloatTest = EtsArrayObjectMembers<EtsBoxPrimitive<EtsFloat>>;
77 using EtsArrayBoxedDoubleTest = EtsArrayObjectMembers<EtsBoxPrimitive<EtsDouble>>;
78
TEST_F(EtsArrayBoxedBooleanTest,MemoryLayout)79 TEST_F(EtsArrayBoxedBooleanTest, MemoryLayout)
80 {
81 EtsClass *klass = PlatformTypes(vm_)->escompatArray;
82 MirrorFieldInfo::CompareMemberOffsets(klass, GetMembers());
83 }
84
TEST_F(EtsArrayBoxedByteTest,MemoryLayout)85 TEST_F(EtsArrayBoxedByteTest, MemoryLayout)
86 {
87 EtsClass *klass = PlatformTypes(vm_)->escompatArray;
88 MirrorFieldInfo::CompareMemberOffsets(klass, GetMembers());
89 }
90
TEST_F(EtsArrayBoxedCharTest,MemoryLayout)91 TEST_F(EtsArrayBoxedCharTest, MemoryLayout)
92 {
93 EtsClass *klass = PlatformTypes(vm_)->escompatArray;
94 MirrorFieldInfo::CompareMemberOffsets(klass, GetMembers());
95 }
96
TEST_F(EtsArrayBoxedShortTest,MemoryLayout)97 TEST_F(EtsArrayBoxedShortTest, MemoryLayout)
98 {
99 EtsClass *klass = PlatformTypes(vm_)->escompatArray;
100 MirrorFieldInfo::CompareMemberOffsets(klass, GetMembers());
101 }
102
TEST_F(EtsArrayBoxedIntTest,MemoryLayout)103 TEST_F(EtsArrayBoxedIntTest, MemoryLayout)
104 {
105 EtsClass *klass = PlatformTypes(vm_)->escompatArray;
106 MirrorFieldInfo::CompareMemberOffsets(klass, GetMembers());
107 }
108
TEST_F(EtsArrayBoxedLongTest,MemoryLayout)109 TEST_F(EtsArrayBoxedLongTest, MemoryLayout)
110 {
111 EtsClass *klass = PlatformTypes(vm_)->escompatArray;
112 MirrorFieldInfo::CompareMemberOffsets(klass, GetMembers());
113 }
114
TEST_F(EtsArrayBoxedFloatTest,MemoryLayout)115 TEST_F(EtsArrayBoxedFloatTest, MemoryLayout)
116 {
117 EtsClass *klass = PlatformTypes(vm_)->escompatArray;
118 MirrorFieldInfo::CompareMemberOffsets(klass, GetMembers());
119 }
120
TEST_F(EtsArrayBoxedDoubleTest,MemoryLayout)121 TEST_F(EtsArrayBoxedDoubleTest, MemoryLayout)
122 {
123 EtsClass *klass = PlatformTypes(vm_)->escompatArray;
124 MirrorFieldInfo::CompareMemberOffsets(klass, GetMembers());
125 }
126
127 } // namespace ark::ets::test
128