14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_ci#include "ecmascript/base/array_helper.h"
174514f5e3Sopenharmony_ci#include "ecmascript/global_env.h"
184514f5e3Sopenharmony_ci#include "ecmascript/js_array.h"
194514f5e3Sopenharmony_ci#include "ecmascript/tests/test_helper.h"
204514f5e3Sopenharmony_ci#include "ecmascript/js_typed_array.h"
214514f5e3Sopenharmony_ci
224514f5e3Sopenharmony_ciusing namespace panda::ecmascript;
234514f5e3Sopenharmony_ciusing namespace panda::ecmascript::base;
244514f5e3Sopenharmony_ci
254514f5e3Sopenharmony_cinamespace panda::test {
264514f5e3Sopenharmony_ciclass ArrayHelperTest : public BaseTestWithScope<false> {
274514f5e3Sopenharmony_ci};
284514f5e3Sopenharmony_ci
294514f5e3Sopenharmony_ci/**
304514f5e3Sopenharmony_ci * @tc.name: IsConcatSpreadable
314514f5e3Sopenharmony_ci * @tc.desc: Check whether the second parameter is a JsArray type through "IsConcatSpreadable" function.
324514f5e3Sopenharmony_ci * @tc.type: FUNC
334514f5e3Sopenharmony_ci * @tc.require:
344514f5e3Sopenharmony_ci */
354514f5e3Sopenharmony_ciHWTEST_F_L0(ArrayHelperTest, IsConcatSpreadable)
364514f5e3Sopenharmony_ci{
374514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
384514f5e3Sopenharmony_ci    JSHandle<GlobalEnv> globalEnv = thread->GetEcmaVM()->GetGlobalEnv();
394514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> icConcatSpreadableSymbol(factory->NewWellKnownSymbolWithChar("icConcatSpreadableSymbol"));
404514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> icSymbolValue(thread, JSTaggedValue(1));
414514f5e3Sopenharmony_ci    globalEnv->SetIsConcatSpreadableSymbol(thread, icConcatSpreadableSymbol);
424514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> objFunc(globalEnv->GetArrayFunction());
434514f5e3Sopenharmony_ci
444514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> handleValue(thread, JSTaggedValue(1));
454514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> handleArray(factory->NewJSArray());
464514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> handleObjectArr(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objFunc), objFunc));
474514f5e3Sopenharmony_ci    JSObject::SetProperty(thread, handleObjectArr, icConcatSpreadableSymbol, icSymbolValue);
484514f5e3Sopenharmony_ci
494514f5e3Sopenharmony_ci    EXPECT_FALSE(ArrayHelper::IsConcatSpreadable(thread, handleValue));
504514f5e3Sopenharmony_ci    EXPECT_TRUE(ArrayHelper::IsConcatSpreadable(thread, handleArray));
514514f5e3Sopenharmony_ci    EXPECT_TRUE(ArrayHelper::IsConcatSpreadable(thread, handleObjectArr));
524514f5e3Sopenharmony_ci}
534514f5e3Sopenharmony_ci
544514f5e3Sopenharmony_ci/**
554514f5e3Sopenharmony_ci * @tc.name: SortCompare
564514f5e3Sopenharmony_ci * @tc.desc: Check whether the two data(X,Y) are sorted from large two smalle,both X and Y are Undefined return zero,if
574514f5e3Sopenharmony_ci *           X or Y is Undefined return -1,if X more than the Y return 1,otherwrise return 0.
584514f5e3Sopenharmony_ci * @tc.type: FUNC
594514f5e3Sopenharmony_ci * @tc.require:
604514f5e3Sopenharmony_ci */
614514f5e3Sopenharmony_ciHWTEST_F_L0(ArrayHelperTest, SortCompare)
624514f5e3Sopenharmony_ci{
634514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> callbackfnHandle(thread, JSTaggedValue::Undefined());
644514f5e3Sopenharmony_ci    // callbackfnHandle is Undefined
654514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> handleValueX1(thread, JSTaggedValue::Undefined());
664514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> handleValueX2(thread, JSTaggedValue(11));
674514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> handleValueX3(thread, JSTaggedValue(12));
684514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> handleValueY1(thread, JSTaggedValue::Undefined());
694514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> handleValueY2(thread, JSTaggedValue(10));
704514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> handleValueY3(thread, JSTaggedValue(12));
714514f5e3Sopenharmony_ci
724514f5e3Sopenharmony_ci    int32_t resultValue1 = ArrayHelper::SortCompare(thread, callbackfnHandle, handleValueX1, handleValueY1);
734514f5e3Sopenharmony_ci    int32_t resultValue2 = ArrayHelper::SortCompare(thread, callbackfnHandle, handleValueX1, handleValueY2);
744514f5e3Sopenharmony_ci    int32_t resultValue3 = ArrayHelper::SortCompare(thread, callbackfnHandle, handleValueX2, handleValueY1);
754514f5e3Sopenharmony_ci    int32_t resultValue4 = ArrayHelper::SortCompare(thread, callbackfnHandle, handleValueX2, handleValueY2);
764514f5e3Sopenharmony_ci    int32_t resultValue5 = ArrayHelper::SortCompare(thread, callbackfnHandle, handleValueX3, handleValueY3);
774514f5e3Sopenharmony_ci    int32_t resultValue6 = ArrayHelper::SortCompare(thread, callbackfnHandle, handleValueX2, handleValueY3);
784514f5e3Sopenharmony_ci
794514f5e3Sopenharmony_ci    EXPECT_EQ(resultValue1, 0); // both X and Y is Undefined
804514f5e3Sopenharmony_ci    EXPECT_EQ(resultValue2, 1); // X is Undefined
814514f5e3Sopenharmony_ci    EXPECT_EQ(resultValue3, -1); // Y is Undefined
824514f5e3Sopenharmony_ci    EXPECT_EQ(resultValue4, 1);  // X > Y
834514f5e3Sopenharmony_ci    EXPECT_EQ(resultValue5, 0); // X = Y
844514f5e3Sopenharmony_ci    EXPECT_EQ(resultValue6, -1); // X < Y
854514f5e3Sopenharmony_ci}
864514f5e3Sopenharmony_ci
874514f5e3Sopenharmony_ci/**
884514f5e3Sopenharmony_ci * @tc.name: GetLength
894514f5e3Sopenharmony_ci * @tc.desc: Check whether the result returned through "GetLength" function is within expectations.
904514f5e3Sopenharmony_ci * @tc.type: FUNC
914514f5e3Sopenharmony_ci * @tc.require:
924514f5e3Sopenharmony_ci */
934514f5e3Sopenharmony_ciHWTEST_F_L0(ArrayHelperTest, GetLength)
944514f5e3Sopenharmony_ci{
954514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
964514f5e3Sopenharmony_ci    JSHandle<GlobalEnv> globalEnv = thread->GetEcmaVM()->GetGlobalEnv();
974514f5e3Sopenharmony_ci
984514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> lengthKey = thread->GlobalConstants()->GetHandledLengthString();
994514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> lengthValue(thread, JSTaggedValue(100.0));
1004514f5e3Sopenharmony_ci
1014514f5e3Sopenharmony_ci    JSArray *handleArr = JSArray::ArrayCreate(thread, JSTaggedNumber(10)).GetObject<JSArray>();
1024514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> arrayHandle(thread, handleArr);
1034514f5e3Sopenharmony_ci    EXPECT_EQ(ArrayHelper::GetLength(thread, arrayHandle), 10U);
1044514f5e3Sopenharmony_ci
1054514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> HandleInt8ArrayFunc(globalEnv->GetInt8ArrayFunction());
1064514f5e3Sopenharmony_ci    JSHandle<JSTypedArray> handleTypeArray = JSHandle<JSTypedArray>::Cast(
1074514f5e3Sopenharmony_ci        factory->NewJSObjectByConstructor(JSHandle<JSFunction>(HandleInt8ArrayFunc), HandleInt8ArrayFunc));
1084514f5e3Sopenharmony_ci    handleTypeArray->SetArrayLength(11);
1094514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> typeArrayHandle(handleTypeArray);
1104514f5e3Sopenharmony_ci    EXPECT_EQ(ArrayHelper::GetLength(thread, typeArrayHandle), 11U);
1114514f5e3Sopenharmony_ci
1124514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> objFunc(globalEnv->GetArrayFunction());
1134514f5e3Sopenharmony_ci    JSHandle<JSObject> objectHandle = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objFunc), objFunc);
1144514f5e3Sopenharmony_ci    EXPECT_EQ(ArrayHelper::GetLength(thread, JSHandle<JSTaggedValue>(objectHandle)), 0U);
1154514f5e3Sopenharmony_ci
1164514f5e3Sopenharmony_ci    JSObject::SetProperty(thread, objectHandle, lengthKey, lengthValue);
1174514f5e3Sopenharmony_ci    EXPECT_EQ(ArrayHelper::GetLength(thread, JSHandle<JSTaggedValue>(objectHandle)),
1184514f5e3Sopenharmony_ci                                                                     JSTaggedNumber(100.0).GetNumber());
1194514f5e3Sopenharmony_ci}
1204514f5e3Sopenharmony_ci
1214514f5e3Sopenharmony_ci/**
1224514f5e3Sopenharmony_ci * @tc.name: GetArrayLength
1234514f5e3Sopenharmony_ci * @tc.desc: Check whether the result returned through "GetArrayLength" function is within expectations.
1244514f5e3Sopenharmony_ci * @tc.type: FUNC
1254514f5e3Sopenharmony_ci * @tc.require:
1264514f5e3Sopenharmony_ci */
1274514f5e3Sopenharmony_ciHWTEST_F_L0(ArrayHelperTest, GetArrayLength)
1284514f5e3Sopenharmony_ci{
1294514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1304514f5e3Sopenharmony_ci    JSHandle<GlobalEnv> globalEnv = thread->GetEcmaVM()->GetGlobalEnv();
1314514f5e3Sopenharmony_ci
1324514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> lengthKey = thread->GlobalConstants()->GetHandledLengthString();
1334514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> lengthValue(thread, JSTaggedValue(10.0));
1344514f5e3Sopenharmony_ci
1354514f5e3Sopenharmony_ci    JSArray *handleArr = JSArray::ArrayCreate(thread, JSTaggedNumber(0)).GetObject<JSArray>();
1364514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> arrayHandle(thread, handleArr);
1374514f5e3Sopenharmony_ci    EXPECT_EQ(ArrayHelper::GetLength(thread, arrayHandle), 0U);
1384514f5e3Sopenharmony_ci
1394514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> objFunc(globalEnv->GetArrayFunction());
1404514f5e3Sopenharmony_ci    JSHandle<JSObject> objectHandle = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objFunc), objFunc);
1414514f5e3Sopenharmony_ci    EXPECT_EQ(ArrayHelper::GetArrayLength(thread, JSHandle<JSTaggedValue>(objectHandle)), 0U);
1424514f5e3Sopenharmony_ci
1434514f5e3Sopenharmony_ci    JSObject::SetProperty(thread, objectHandle, lengthKey, lengthValue);
1444514f5e3Sopenharmony_ci    EXPECT_EQ(ArrayHelper::GetArrayLength(thread, JSHandle<JSTaggedValue>(objectHandle)),
1454514f5e3Sopenharmony_ci                                                                          JSTaggedNumber(10.0).GetNumber());
1464514f5e3Sopenharmony_ci}
1474514f5e3Sopenharmony_ci}  // namespace panda::test
148