/* * Copyright (c) 2021-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "ecmascript/base/array_helper.h" #include "ecmascript/global_env.h" #include "ecmascript/js_array.h" #include "ecmascript/tests/test_helper.h" #include "ecmascript/js_typed_array.h" using namespace panda::ecmascript; using namespace panda::ecmascript::base; namespace panda::test { class ArrayHelperTest : public BaseTestWithScope { }; /** * @tc.name: IsConcatSpreadable * @tc.desc: Check whether the second parameter is a JsArray type through "IsConcatSpreadable" function. * @tc.type: FUNC * @tc.require: */ HWTEST_F_L0(ArrayHelperTest, IsConcatSpreadable) { ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); JSHandle globalEnv = thread->GetEcmaVM()->GetGlobalEnv(); JSHandle icConcatSpreadableSymbol(factory->NewWellKnownSymbolWithChar("icConcatSpreadableSymbol")); JSHandle icSymbolValue(thread, JSTaggedValue(1)); globalEnv->SetIsConcatSpreadableSymbol(thread, icConcatSpreadableSymbol); JSHandle objFunc(globalEnv->GetArrayFunction()); JSHandle handleValue(thread, JSTaggedValue(1)); JSHandle handleArray(factory->NewJSArray()); JSHandle handleObjectArr(factory->NewJSObjectByConstructor(JSHandle(objFunc), objFunc)); JSObject::SetProperty(thread, handleObjectArr, icConcatSpreadableSymbol, icSymbolValue); EXPECT_FALSE(ArrayHelper::IsConcatSpreadable(thread, handleValue)); EXPECT_TRUE(ArrayHelper::IsConcatSpreadable(thread, handleArray)); EXPECT_TRUE(ArrayHelper::IsConcatSpreadable(thread, handleObjectArr)); } /** * @tc.name: SortCompare * @tc.desc: Check whether the two data(X,Y) are sorted from large two smalle,both X and Y are Undefined return zero,if * X or Y is Undefined return -1,if X more than the Y return 1,otherwrise return 0. * @tc.type: FUNC * @tc.require: */ HWTEST_F_L0(ArrayHelperTest, SortCompare) { JSHandle callbackfnHandle(thread, JSTaggedValue::Undefined()); // callbackfnHandle is Undefined JSHandle handleValueX1(thread, JSTaggedValue::Undefined()); JSHandle handleValueX2(thread, JSTaggedValue(11)); JSHandle handleValueX3(thread, JSTaggedValue(12)); JSHandle handleValueY1(thread, JSTaggedValue::Undefined()); JSHandle handleValueY2(thread, JSTaggedValue(10)); JSHandle handleValueY3(thread, JSTaggedValue(12)); int32_t resultValue1 = ArrayHelper::SortCompare(thread, callbackfnHandle, handleValueX1, handleValueY1); int32_t resultValue2 = ArrayHelper::SortCompare(thread, callbackfnHandle, handleValueX1, handleValueY2); int32_t resultValue3 = ArrayHelper::SortCompare(thread, callbackfnHandle, handleValueX2, handleValueY1); int32_t resultValue4 = ArrayHelper::SortCompare(thread, callbackfnHandle, handleValueX2, handleValueY2); int32_t resultValue5 = ArrayHelper::SortCompare(thread, callbackfnHandle, handleValueX3, handleValueY3); int32_t resultValue6 = ArrayHelper::SortCompare(thread, callbackfnHandle, handleValueX2, handleValueY3); EXPECT_EQ(resultValue1, 0); // both X and Y is Undefined EXPECT_EQ(resultValue2, 1); // X is Undefined EXPECT_EQ(resultValue3, -1); // Y is Undefined EXPECT_EQ(resultValue4, 1); // X > Y EXPECT_EQ(resultValue5, 0); // X = Y EXPECT_EQ(resultValue6, -1); // X < Y } /** * @tc.name: GetLength * @tc.desc: Check whether the result returned through "GetLength" function is within expectations. * @tc.type: FUNC * @tc.require: */ HWTEST_F_L0(ArrayHelperTest, GetLength) { ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); JSHandle globalEnv = thread->GetEcmaVM()->GetGlobalEnv(); JSHandle lengthKey = thread->GlobalConstants()->GetHandledLengthString(); JSHandle lengthValue(thread, JSTaggedValue(100.0)); JSArray *handleArr = JSArray::ArrayCreate(thread, JSTaggedNumber(10)).GetObject(); JSHandle arrayHandle(thread, handleArr); EXPECT_EQ(ArrayHelper::GetLength(thread, arrayHandle), 10U); JSHandle HandleInt8ArrayFunc(globalEnv->GetInt8ArrayFunction()); JSHandle handleTypeArray = JSHandle::Cast( factory->NewJSObjectByConstructor(JSHandle(HandleInt8ArrayFunc), HandleInt8ArrayFunc)); handleTypeArray->SetArrayLength(11); JSHandle typeArrayHandle(handleTypeArray); EXPECT_EQ(ArrayHelper::GetLength(thread, typeArrayHandle), 11U); JSHandle objFunc(globalEnv->GetArrayFunction()); JSHandle objectHandle = factory->NewJSObjectByConstructor(JSHandle(objFunc), objFunc); EXPECT_EQ(ArrayHelper::GetLength(thread, JSHandle(objectHandle)), 0U); JSObject::SetProperty(thread, objectHandle, lengthKey, lengthValue); EXPECT_EQ(ArrayHelper::GetLength(thread, JSHandle(objectHandle)), JSTaggedNumber(100.0).GetNumber()); } /** * @tc.name: GetArrayLength * @tc.desc: Check whether the result returned through "GetArrayLength" function is within expectations. * @tc.type: FUNC * @tc.require: */ HWTEST_F_L0(ArrayHelperTest, GetArrayLength) { ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); JSHandle globalEnv = thread->GetEcmaVM()->GetGlobalEnv(); JSHandle lengthKey = thread->GlobalConstants()->GetHandledLengthString(); JSHandle lengthValue(thread, JSTaggedValue(10.0)); JSArray *handleArr = JSArray::ArrayCreate(thread, JSTaggedNumber(0)).GetObject(); JSHandle arrayHandle(thread, handleArr); EXPECT_EQ(ArrayHelper::GetLength(thread, arrayHandle), 0U); JSHandle objFunc(globalEnv->GetArrayFunction()); JSHandle objectHandle = factory->NewJSObjectByConstructor(JSHandle(objFunc), objFunc); EXPECT_EQ(ArrayHelper::GetArrayLength(thread, JSHandle(objectHandle)), 0U); JSObject::SetProperty(thread, objectHandle, lengthKey, lengthValue); EXPECT_EQ(ArrayHelper::GetArrayLength(thread, JSHandle(objectHandle)), JSTaggedNumber(10.0).GetNumber()); } } // namespace panda::test