1fb299fa2Sopenharmony_ci/*
2fb299fa2Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3fb299fa2Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4fb299fa2Sopenharmony_ci * you may not use this file except in compliance with the License.
5fb299fa2Sopenharmony_ci * You may obtain a copy of the License at
6fb299fa2Sopenharmony_ci *
7fb299fa2Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8fb299fa2Sopenharmony_ci *
9fb299fa2Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10fb299fa2Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11fb299fa2Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fb299fa2Sopenharmony_ci * See the License for the specific language governing permissions and
13fb299fa2Sopenharmony_ci * limitations under the License.
14fb299fa2Sopenharmony_ci */
15fb299fa2Sopenharmony_ci
16fb299fa2Sopenharmony_ci#include <cstring>
17fb299fa2Sopenharmony_ci#include <fcntl.h>
18fb299fa2Sopenharmony_ci#include <gtest/gtest.h>
19fb299fa2Sopenharmony_ci#include <iostream>
20fb299fa2Sopenharmony_ci#include <sys/mman.h>
21fb299fa2Sopenharmony_ci#include <sys/stat.h>
22fb299fa2Sopenharmony_ci#include <unistd.h>
23fb299fa2Sopenharmony_ci#include "log.h"
24fb299fa2Sopenharmony_ci#include "script_context.h"
25fb299fa2Sopenharmony_ci#include "script_expression.h"
26fb299fa2Sopenharmony_ci#include "script_instruction.h"
27fb299fa2Sopenharmony_ci#include "script_manager.h"
28fb299fa2Sopenharmony_ci#include "unittest_comm.h"
29fb299fa2Sopenharmony_ci
30fb299fa2Sopenharmony_ciusing namespace std;
31fb299fa2Sopenharmony_ciusing namespace Hpackage;
32fb299fa2Sopenharmony_ciusing namespace Uscript;
33fb299fa2Sopenharmony_ciusing namespace Updater;
34fb299fa2Sopenharmony_ciusing namespace testing::ext;
35fb299fa2Sopenharmony_ci
36fb299fa2Sopenharmony_cinamespace {
37fb299fa2Sopenharmony_ciclass ScriptInterpreterUnitTest : public ::testing::Test {
38fb299fa2Sopenharmony_cipublic:
39fb299fa2Sopenharmony_ci    ScriptInterpreterUnitTest() {}
40fb299fa2Sopenharmony_ci    ~ScriptInterpreterUnitTest() {}
41fb299fa2Sopenharmony_ci
42fb299fa2Sopenharmony_ci    int TestScriptInterpreterScriptValue() const
43fb299fa2Sopenharmony_ci    {
44fb299fa2Sopenharmony_ci        int32_t action = 0;
45fb299fa2Sopenharmony_ci        UScriptValuePtr rightValue = std::make_shared<UScriptValue>(UScriptValue::VALUE_TYPE_RETURN);
46fb299fa2Sopenharmony_ci        std::unique_ptr<UScriptValue> value = std::make_unique<UScriptValue>(UScriptValue::VALUE_TYPE_RETURN);
47fb299fa2Sopenharmony_ci        if (value == nullptr) {
48fb299fa2Sopenharmony_ci            return -1;
49fb299fa2Sopenharmony_ci        }
50fb299fa2Sopenharmony_ci        value->Computer(action, rightValue);
51fb299fa2Sopenharmony_ci        value->IsTrue();
52fb299fa2Sopenharmony_ci        std::string tmpStr = value->ToString();
53fb299fa2Sopenharmony_ci        EXPECT_STREQ("null", tmpStr.c_str());
54fb299fa2Sopenharmony_ci        value.reset();
55fb299fa2Sopenharmony_ci
56fb299fa2Sopenharmony_ci        int32_t intValue = 10;
57fb299fa2Sopenharmony_ci        std::unique_ptr<IntegerValue> iv = std::make_unique<IntegerValue>(intValue);
58fb299fa2Sopenharmony_ci        if (iv == nullptr) {
59fb299fa2Sopenharmony_ci            return -1;
60fb299fa2Sopenharmony_ci        }
61fb299fa2Sopenharmony_ci        iv->IsTrue();
62fb299fa2Sopenharmony_ci        iv.reset();
63fb299fa2Sopenharmony_ci
64fb299fa2Sopenharmony_ci        float floatValue = 10.0;
65fb299fa2Sopenharmony_ci        std::unique_ptr<FloatValue> fv = std::make_unique<FloatValue>(floatValue);
66fb299fa2Sopenharmony_ci        if (fv == nullptr) {
67fb299fa2Sopenharmony_ci            return -1;
68fb299fa2Sopenharmony_ci        }
69fb299fa2Sopenharmony_ci        fv->IsTrue();
70fb299fa2Sopenharmony_ci        fv.reset();
71fb299fa2Sopenharmony_ci
72fb299fa2Sopenharmony_ci        std::unique_ptr<StringValue> sv = std::make_unique<StringValue>("2222222222222");
73fb299fa2Sopenharmony_ci        if (sv == nullptr) {
74fb299fa2Sopenharmony_ci            return -1;
75fb299fa2Sopenharmony_ci        }
76fb299fa2Sopenharmony_ci        sv->IsTrue();
77fb299fa2Sopenharmony_ci        sv.reset();
78fb299fa2Sopenharmony_ci
79fb299fa2Sopenharmony_ci        std::unique_ptr<ReturnValue> rv = std::make_unique<ReturnValue>();
80fb299fa2Sopenharmony_ci        if (rv == nullptr) {
81fb299fa2Sopenharmony_ci            return -1;
82fb299fa2Sopenharmony_ci        }
83fb299fa2Sopenharmony_ci        rv->IsTrue();
84fb299fa2Sopenharmony_ci        rv->Computer(action, rightValue);
85fb299fa2Sopenharmony_ci        rv->ToString();
86fb299fa2Sopenharmony_ci        rv.reset();
87fb299fa2Sopenharmony_ci
88fb299fa2Sopenharmony_ci        constexpr int32_t retCode = 100;
89fb299fa2Sopenharmony_ci        std::unique_ptr<ErrorValue> ev = std::make_unique<ErrorValue>(retCode);
90fb299fa2Sopenharmony_ci        if (ev == nullptr) {
91fb299fa2Sopenharmony_ci            return -1;
92fb299fa2Sopenharmony_ci        }
93fb299fa2Sopenharmony_ci        ev->IsTrue();
94fb299fa2Sopenharmony_ci        ev->ToString();
95fb299fa2Sopenharmony_ci        ev->Computer(action, rightValue);
96fb299fa2Sopenharmony_ci        ev.reset();
97fb299fa2Sopenharmony_ci        return 0;
98fb299fa2Sopenharmony_ci    }
99fb299fa2Sopenharmony_ci
100fb299fa2Sopenharmony_ci    int TestScriptInstructionContext() const
101fb299fa2Sopenharmony_ci    {
102fb299fa2Sopenharmony_ci        std::unique_ptr<UScriptInstructionContext> funcContext = std::make_unique<UScriptInstructionContext>();
103fb299fa2Sopenharmony_ci        int intValue = 100;
104fb299fa2Sopenharmony_ci        int ret1 = funcContext->PushParam(intValue);
105fb299fa2Sopenharmony_ci        float floatValue = 100.0;
106fb299fa2Sopenharmony_ci        int ret2 = funcContext->PushParam(floatValue);
107fb299fa2Sopenharmony_ci        std::string str = std::string("333333333");
108fb299fa2Sopenharmony_ci        int ret3 = funcContext->PushParam(str);
109fb299fa2Sopenharmony_ci        EXPECT_EQ(0, ret1 || ret2 || ret3);
110fb299fa2Sopenharmony_ci
111fb299fa2Sopenharmony_ci        int32_t outOfIndex = 3;
112fb299fa2Sopenharmony_ci        int ret = funcContext->GetParamType(outOfIndex);
113fb299fa2Sopenharmony_ci        EXPECT_EQ(UScriptContext::PARAM_TYPE_INVALID, ret);
114fb299fa2Sopenharmony_ci        return 0;
115fb299fa2Sopenharmony_ci    }
116fb299fa2Sopenharmony_ci
117fb299fa2Sopenharmony_ci    int TestIntegerValueComputer() const
118fb299fa2Sopenharmony_ci    {
119fb299fa2Sopenharmony_ci        int intValue = 100;
120fb299fa2Sopenharmony_ci        UScriptValuePtr rightValue = std::make_shared<IntegerValue>(0);
121fb299fa2Sopenharmony_ci        UScriptValuePtr value = std::make_shared<IntegerValue>(intValue);
122fb299fa2Sopenharmony_ci        if (value == nullptr || rightValue == nullptr) {
123fb299fa2Sopenharmony_ci            return -1;
124fb299fa2Sopenharmony_ci        }
125fb299fa2Sopenharmony_ci        value->Computer(UScriptExpression::DIV_OPERATOR, rightValue);
126fb299fa2Sopenharmony_ci        // 除浮点数
127fb299fa2Sopenharmony_ci        float floatValue = 100.0;
128fb299fa2Sopenharmony_ci        UScriptValuePtr rightValue2 = std::make_shared<FloatValue>(floatValue);
129fb299fa2Sopenharmony_ci        value->Computer(UScriptExpression::DIV_OPERATOR, rightValue2);
130fb299fa2Sopenharmony_ci
131fb299fa2Sopenharmony_ci        // 除浮点数
132fb299fa2Sopenharmony_ci        UScriptValuePtr rightValue3 = std::make_shared<FloatValue>(0);
133fb299fa2Sopenharmony_ci        value->Computer(UScriptExpression::DIV_OPERATOR, rightValue3);
134fb299fa2Sopenharmony_ci
135fb299fa2Sopenharmony_ci        int32_t action = 100;
136fb299fa2Sopenharmony_ci        value->Computer(action, rightValue3);
137fb299fa2Sopenharmony_ci        return 0;
138fb299fa2Sopenharmony_ci    }
139fb299fa2Sopenharmony_ci
140fb299fa2Sopenharmony_ci    int TestFloatValueComputer() const
141fb299fa2Sopenharmony_ci    {
142fb299fa2Sopenharmony_ci        float floatValue = 100;
143fb299fa2Sopenharmony_ci        UScriptValuePtr rightValue = std::make_shared<IntegerValue>(0);
144fb299fa2Sopenharmony_ci        UScriptValuePtr value = std::make_shared<FloatValue>(floatValue);
145fb299fa2Sopenharmony_ci        if (value == nullptr || rightValue == nullptr) {
146fb299fa2Sopenharmony_ci            return -1;
147fb299fa2Sopenharmony_ci        }
148fb299fa2Sopenharmony_ci        value->Computer(UScriptExpression::DIV_OPERATOR, rightValue);
149fb299fa2Sopenharmony_ci        // 除浮点数
150fb299fa2Sopenharmony_ci        UScriptValuePtr rightValue2 = std::make_shared<FloatValue>(floatValue);
151fb299fa2Sopenharmony_ci        value->Computer(UScriptExpression::DIV_OPERATOR, rightValue2);
152fb299fa2Sopenharmony_ci
153fb299fa2Sopenharmony_ci        // 除浮点数
154fb299fa2Sopenharmony_ci        UScriptValuePtr rightValue3 = std::make_shared<FloatValue>(0);
155fb299fa2Sopenharmony_ci        value->Computer(UScriptExpression::DIV_OPERATOR, rightValue3);
156fb299fa2Sopenharmony_ci
157fb299fa2Sopenharmony_ci        value->Computer(UScriptExpression::EQ_OPERATOR, rightValue3);
158fb299fa2Sopenharmony_ci        return 0;
159fb299fa2Sopenharmony_ci    }
160fb299fa2Sopenharmony_ci
161fb299fa2Sopenharmony_ci    int TestStringValueComputer() const
162fb299fa2Sopenharmony_ci    {
163fb299fa2Sopenharmony_ci        UScriptValuePtr rightValue = std::make_shared<IntegerValue>(0);
164fb299fa2Sopenharmony_ci        UScriptValuePtr value = std::make_shared<StringValue>("100");
165fb299fa2Sopenharmony_ci        if (value == nullptr || rightValue == nullptr) {
166fb299fa2Sopenharmony_ci            return -1;
167fb299fa2Sopenharmony_ci        }
168fb299fa2Sopenharmony_ci        value->Computer(UScriptExpression::ADD_OPERATOR, rightValue);
169fb299fa2Sopenharmony_ci
170fb299fa2Sopenharmony_ci        float floatValue = 100.0;
171fb299fa2Sopenharmony_ci        UScriptValuePtr rightValue2 = std::make_shared<FloatValue>(floatValue);
172fb299fa2Sopenharmony_ci        value->Computer(UScriptExpression::ADD_OPERATOR, rightValue2);
173fb299fa2Sopenharmony_ci
174fb299fa2Sopenharmony_ci        UScriptValuePtr rightValue3 = std::make_shared<StringValue>("666666");
175fb299fa2Sopenharmony_ci        value->Computer(UScriptExpression::ADD_OPERATOR, rightValue3);
176fb299fa2Sopenharmony_ci
177fb299fa2Sopenharmony_ci        // string 比较
178fb299fa2Sopenharmony_ci        value->Computer(UScriptExpression::EQ_OPERATOR, rightValue2);
179fb299fa2Sopenharmony_ci
180fb299fa2Sopenharmony_ci        value->Computer(UScriptExpression::GT_OPERATOR, rightValue3);
181fb299fa2Sopenharmony_ci        value->Computer(UScriptExpression::GE_OPERATOR, rightValue3);
182fb299fa2Sopenharmony_ci        value->Computer(UScriptExpression::LT_OPERATOR, rightValue3);
183fb299fa2Sopenharmony_ci        value->Computer(UScriptExpression::LE_OPERATOR, rightValue3);
184fb299fa2Sopenharmony_ci        value->Computer(UScriptExpression::EQ_OPERATOR, rightValue3);
185fb299fa2Sopenharmony_ci        value->Computer(UScriptExpression::NE_OPERATOR, rightValue3);
186fb299fa2Sopenharmony_ci        return 0;
187fb299fa2Sopenharmony_ci    }
188fb299fa2Sopenharmony_ci
189fb299fa2Sopenharmony_ciprotected:
190fb299fa2Sopenharmony_ci    void SetUp() {}
191fb299fa2Sopenharmony_ci    void TearDown() {}
192fb299fa2Sopenharmony_ci    void TestBody() {}
193fb299fa2Sopenharmony_ci};
194fb299fa2Sopenharmony_ci
195fb299fa2Sopenharmony_ciHWTEST_F(ScriptInterpreterUnitTest, TestScriptInterpreterScriptValue, TestSize.Level0)
196fb299fa2Sopenharmony_ci{
197fb299fa2Sopenharmony_ci    ScriptInterpreterUnitTest test;
198fb299fa2Sopenharmony_ci    EXPECT_EQ(0, test.TestScriptInterpreterScriptValue());
199fb299fa2Sopenharmony_ci}
200fb299fa2Sopenharmony_ci
201fb299fa2Sopenharmony_ciHWTEST_F(ScriptInterpreterUnitTest, TestScriptInstructionContext, TestSize.Level0)
202fb299fa2Sopenharmony_ci{
203fb299fa2Sopenharmony_ci    ScriptInterpreterUnitTest test;
204fb299fa2Sopenharmony_ci    EXPECT_EQ(0, test.TestScriptInstructionContext());
205fb299fa2Sopenharmony_ci}
206fb299fa2Sopenharmony_ci
207fb299fa2Sopenharmony_ciHWTEST_F(ScriptInterpreterUnitTest, TestIntegerValueComputer, TestSize.Level0)
208fb299fa2Sopenharmony_ci{
209fb299fa2Sopenharmony_ci    ScriptInterpreterUnitTest test;
210fb299fa2Sopenharmony_ci    EXPECT_EQ(0, test.TestIntegerValueComputer());
211fb299fa2Sopenharmony_ci}
212fb299fa2Sopenharmony_ci
213fb299fa2Sopenharmony_ciHWTEST_F(ScriptInterpreterUnitTest, TestFloatValueComputer, TestSize.Level0)
214fb299fa2Sopenharmony_ci{
215fb299fa2Sopenharmony_ci    ScriptInterpreterUnitTest test;
216fb299fa2Sopenharmony_ci    EXPECT_EQ(0, test.TestFloatValueComputer());
217fb299fa2Sopenharmony_ci}
218fb299fa2Sopenharmony_ci
219fb299fa2Sopenharmony_ciHWTEST_F(ScriptInterpreterUnitTest, TestStringValueComputer, TestSize.Level0)
220fb299fa2Sopenharmony_ci{
221fb299fa2Sopenharmony_ci    ScriptInterpreterUnitTest test;
222fb299fa2Sopenharmony_ci    EXPECT_EQ(0, test.TestStringValueComputer());
223fb299fa2Sopenharmony_ci}
224fb299fa2Sopenharmony_ci
225fb299fa2Sopenharmony_ciHWTEST_F(ScriptInterpreterUnitTest, SomeDestructor, TestSize.Level0)
226fb299fa2Sopenharmony_ci{
227fb299fa2Sopenharmony_ci    IntegerValue a1(0);
228fb299fa2Sopenharmony_ci    FloatValue a2(0.0);
229fb299fa2Sopenharmony_ci    ErrorValue a3(0);
230fb299fa2Sopenharmony_ci    char a5[1000] = {1};
231fb299fa2Sopenharmony_ci    UScriptContextPtr local;
232fb299fa2Sopenharmony_ci    UScriptExpression a4(UScriptExpression::EXPRESSION_TYPE_INTERGER);
233fb299fa2Sopenharmony_ci    a4.Execute(*(ScriptInterpreter*)a5, local);
234fb299fa2Sopenharmony_ci    // just for executing these destructor
235fb299fa2Sopenharmony_ci    IntegerValue *a6 = new IntegerValue(1);
236fb299fa2Sopenharmony_ci    bool b1 = a6->IsTrue();
237fb299fa2Sopenharmony_ci    delete a6;
238fb299fa2Sopenharmony_ci    FloatValue *a7 = new FloatValue(0.0);
239fb299fa2Sopenharmony_ci    delete a7;
240fb299fa2Sopenharmony_ci    ErrorValue *a8 = new ErrorValue(0);
241fb299fa2Sopenharmony_ci    delete a8;
242fb299fa2Sopenharmony_ci    EXPECT_TRUE(b1);
243fb299fa2Sopenharmony_ci}
244fb299fa2Sopenharmony_ci}
245