• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_field.h"
22 #include "types/ets_string.h"
23 #include "types/ets_class.h"
24 
25 namespace ark::ets::test {
26 
27 class EtsFieldTest : public testing::Test {
28 public:
EtsFieldTest()29     EtsFieldTest()
30     {
31         options_.SetShouldLoadBootPandaFiles(true);
32         options_.SetShouldInitializeIntrinsics(false);
33         options_.SetCompilerEnableJit(false);
34         options_.SetGcType("epsilon");
35         options_.SetLoadRuntimes({"ets"});
36 
37         auto stdlib = std::getenv("PANDA_STD_LIB");
38         if (stdlib == nullptr) {
39             std::cerr << "PANDA_STD_LIB env variable should be set and point to mock_stdlib.abc" << std::endl;
40             std::abort();
41         }
42         options_.SetBootPandaFiles({stdlib});
43 
44         Runtime::Create(options_);
45     }
46 
~EtsFieldTest()47     ~EtsFieldTest() override
48     {
49         Runtime::Destroy();
50     }
51 
52     NO_COPY_SEMANTIC(EtsFieldTest);
53     NO_MOVE_SEMANTIC(EtsFieldTest);
54 
SetUp()55     void SetUp() override
56     {
57         coroutine_ = EtsCoroutine::GetCurrent();
58         coroutine_->ManagedCodeBegin();
59     }
60 
TearDown()61     void TearDown() override
62     {
63         coroutine_->ManagedCodeEnd();
64     }
65 
66 private:
67     RuntimeOptions options_;
68     EtsCoroutine *coroutine_ = nullptr;
69 };
70 
TestFieldMethods(const char * source,const char * className,const char * fieldName,const char * fieldTypeDescriptorName,bool isStaticField)71 static void TestFieldMethods(const char *source, const char *className, const char *fieldName,
72                              const char *fieldTypeDescriptorName, bool isStaticField)
73 {
74     EtsClass *klass = GetTestClass(source, className);
75     ASSERT_NE(klass, nullptr);
76 
77     EtsString *fieldNameString = EtsString::CreateFromMUtf8(fieldName);
78     EtsField *field1 = nullptr;
79     EtsField *field2 = nullptr;
80 
81     if (isStaticField) {
82         field1 = klass->GetStaticFieldIDByName(fieldName);
83         field2 = klass->GetStaticFieldIDByOffset(field1->GetOffset());
84     } else {
85         field1 = klass->GetDeclaredFieldIDByName(fieldName);
86         field2 = klass->GetFieldIDByOffset(field1->GetOffset());
87     }
88 
89     ASSERT_EQ(field1, field2);
90     ASSERT_TRUE(fieldNameString->StringsAreEqual(reinterpret_cast<EtsObject *>(field1->GetNameString())));
91     ASSERT_TRUE(!strcmp(field1->GetName(), fieldName));
92     ASSERT_TRUE(!strcmp(field1->GetTypeDescriptor(), fieldTypeDescriptorName));
93     ASSERT_EQ(field1->IsStatic(), isStaticField);
94     ASSERT_EQ(klass, field1->GetDeclaringClass());
95 }
96 
TEST_F(EtsFieldTest,AllMethods)97 TEST_F(EtsFieldTest, AllMethods)
98 {
99     const char *source = R"(
100         .language eTS
101         .record Ball {
102             f32 BALL_RADIUS <static>
103             i32 BALL_SPEED <static>
104             f32 vx
105             i32 vy
106         }
107     )";
108 
109     TestFieldMethods(source, "LBall;", "BALL_RADIUS", "F", true);
110     TestFieldMethods(source, "LBall;", "BALL_SPEED", "I", true);
111     TestFieldMethods(source, "LBall;", "vx", "F", false);
112     TestFieldMethods(source, "LBall;", "vy", "I", false);
113 
114     source = R"(
115         .language eTS
116         .record Drawer {
117             i32 frameWidth
118             i32 frameHeight
119             i64 startTime <static>
120             i32 fps
121             any drawCounter
122         }
123     )";
124 
125     TestFieldMethods(source, "LDrawer;", "frameWidth", "I", false);
126     TestFieldMethods(source, "LDrawer;", "frameHeight", "I", false);
127     TestFieldMethods(source, "LDrawer;", "startTime", "J", true);
128     TestFieldMethods(source, "LDrawer;", "fps", "I", false);
129     TestFieldMethods(source, "LDrawer;", "drawCounter", "A", false);
130 }
131 
132 }  // namespace ark::ets::test
133