From 98c88c48dc7328ec0e13575202c69e40974fd1d5 Mon Sep 17 00:00:00 2001 From: Gongyuhang <517563583@qq.com> Date: Wed, 20 Oct 2021 16:57:08 +0800 Subject: [PATCH 1/2] Add a cpp that tests the public functions of JsArrayIterator class Signed-off-by: Gongyuhang <517563583@qq.com> --- ecmascript/tests/BUILD.gn | 29 ++++ ecmascript/tests/ecma_module_test.cpp | 1 - ecmascript/tests/js_array_iterator_test.cpp | 178 ++++++++++++++++++++ 3 files changed, 207 insertions(+), 1 deletion(-) create mode 100644 ecmascript/tests/js_array_iterator_test.cpp diff --git a/ecmascript/tests/BUILD.gn b/ecmascript/tests/BUILD.gn index c8abe686b6..1f815da2d6 100644 --- a/ecmascript/tests/BUILD.gn +++ b/ecmascript/tests/BUILD.gn @@ -206,6 +206,33 @@ host_unittest_action("JsArrayTest") { } } +host_unittest_action("JsArrayIteratorTest") { + module_out_path = module_output_path + + sources = [ + # test file + "js_array_iterator_test.cpp", + ] + + configs = [ + "//ark/js_runtime:ecma_test_config", + "//ark/js_runtime:ark_jsruntime_public_config", # should add before + # arkruntime_public_config + "//ark/js_runtime:ark_jsruntime_common_config", + "$ark_root/runtime:arkruntime_public_config", + ] + + deps = [ + "$ark_root/libpandabase:libarkbase", + "//ark/js_runtime:libark_jsruntime_test", + sdk_libc_secshared_dep, + ] + + if (!is_standard_system) { + deps += [ "$ark_root/runtime:libarkruntime" ] + } +} + host_unittest_action("JsDateTest") { module_out_path = module_output_path @@ -923,6 +950,7 @@ group("unittest") { ":HugeObjectTest", ":JsArgumentsTest", ":JsArrayTest", + ":JsArrayIteratorTest", ":JsDateTest", ":JsForinIteratorTest", ":JsFunctionTest", @@ -964,6 +992,7 @@ group("host_unittest") { ":HugeObjectTestAction", ":JsArgumentsTestAction", ":JsArrayTestAction", + ":JsArrayIteratorTestAction", ":JsDateTestAction", ":JsForinIteratorTestAction", ":JsFunctionTestAction", diff --git a/ecmascript/tests/ecma_module_test.cpp b/ecmascript/tests/ecma_module_test.cpp index f214c0bc4e..58be0d46b0 100644 --- a/ecmascript/tests/ecma_module_test.cpp +++ b/ecmascript/tests/ecma_module_test.cpp @@ -465,7 +465,6 @@ HWTEST_F_L0(EcmaModuleTest, ModuleManager_CopyModule) moduleManager->GetModuleItem(thread, handleTagValEcmaModuleCopyTo2, handleTagValItemName22)->GetNumber()); } -// ModuleStack /* * Feature: ModuleStack * Function: PushModule diff --git a/ecmascript/tests/js_array_iterator_test.cpp b/ecmascript/tests/js_array_iterator_test.cpp new file mode 100644 index 0000000000..170b6eec5c --- /dev/null +++ b/ecmascript/tests/js_array_iterator_test.cpp @@ -0,0 +1,178 @@ +/* + * Copyright (c) 2021 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/js_array_iterator.h" +#include "ecmascript/js_array.h" +#include "ecmascript/global_env.h" +#include "ecmascript/object_factory.h" +#include "ecmascript/tests/test_helper.h" + +using namespace panda::ecmascript; + +namespace panda::test { +class JSArrayIteratorTest : public testing::Test { +public: + static void SetUpTestCase() + { + GTEST_LOG_(INFO) << "SetUpTestCase"; + } + + static void TesrDownTestCase() + { + GTEST_LOG_(INFO) << "TearDownCase"; + } + + void SetUp() override + { + TestHelper::CreateEcmaVMWithScope(instance, thread, scope); + } + + void TearDown() override + { + TestHelper::DestroyEcmaVMWithScope(instance, scope); + } + PandaVM *instance {nullptr}; + ecmascript::EcmaHandleScope *scope {nullptr}; + JSThread *thread {nullptr}; +}; + +/* + * Feature: JSArrayIterator + * Function: SetIteratedArray + * SubFunction: GetIteratedArray + * FunctionPoints: Set Iterated Array + * CaseDescription: Call the "SetIteratedArray" function, check whether the result returned through "GetIteratedArray" + * function from the JSArrayIterator is within expectations. + */ +HWTEST_F_L0(JSArrayIteratorTest, SetIteratedArray) +{ + EcmaVM *ecmaVMPtr = thread->GetEcmaVM(); + ObjectFactory *factory = ecmaVMPtr->GetFactory(); + + uint32_t arrayFrom1[10] = {0, 6, 8, 99, 200, 1, -1, -199, 33, 100}; + uint32_t arrayFrom2[10] = {1111, 3211, 737, 0, 1267, 174, 2763, 832, 11, 93}; + int numArrayFrom1 = sizeof(arrayFrom1)/sizeof(arrayFrom1[0]); + int numArrayFrom2 = sizeof(arrayFrom2)/sizeof(arrayFrom2[0]); + JSHandle handleTaggedArrayFrom1(factory->NewTaggedArray(numArrayFrom1)); + JSHandle handleTaggedArrayFrom2(factory->NewTaggedArray(numArrayFrom2)); + for (int i = 0; i < numArrayFrom1; i++) { + handleTaggedArrayFrom1->Set(thread, i, JSTaggedValue(arrayFrom1[i])); + } + for (int i = 0; i < numArrayFrom2; i++) { + handleTaggedArrayFrom2->Set(thread, i, JSTaggedValue(arrayFrom2[i])); + } + JSHandle handleJSObjectTaggedArrayFrom1(JSArray::CreateArrayFromList(thread, handleTaggedArrayFrom1)); + JSHandle handleJSObjectTaggedArrayFrom2(JSArray::CreateArrayFromList(thread, handleTaggedArrayFrom2)); + + // Call "SetIteratedArray" function through "NewJSArrayIterator" function of "object_factory.cpp". + JSHandle handleJSArrayIter = factory->NewJSArrayIterator(handleJSObjectTaggedArrayFrom1, + IterationKind::KEY); + + JSHandle handleJSArrayTo1(thread, JSArray::Cast(handleJSArrayIter->GetIteratedArray().GetTaggedObject())); + EXPECT_EQ(handleJSArrayTo1->GetArrayLength(), numArrayFrom1); + for (int i = 0; i < numArrayFrom1; i++) { + EXPECT_EQ(JSArray::FastGetPropertyByValue(thread, JSHandle(handleJSArrayTo1), i)->GetNumber(), + arrayFrom1[i]); + } + + // Call "SetIteratedArray" function in this HWTEST_F_L0. + handleJSArrayIter->SetIteratedArray(thread, handleJSObjectTaggedArrayFrom2); + + JSHandle handleJSArrayTo2(thread, JSArray::Cast(handleJSArrayIter->GetIteratedArray().GetTaggedObject())); + EXPECT_EQ(handleJSArrayTo2->GetArrayLength(), numArrayFrom2); + for (int i = 0; i < numArrayFrom2; i++) { + EXPECT_EQ(JSArray::FastGetPropertyByValue(thread, JSHandle(handleJSArrayTo2), i)->GetNumber(), + arrayFrom2[i]); + } +} + +/* + * Feature: JSArrayIterator + * Function: SetNextIndex + * SubFunction: GetNextIndex + * FunctionPoints: Set Iterated Array + * CaseDescription: Call the "SetNextIndex" function, check whether the result returned through "GetNextIndex" function + * from the JSArrayIterator is within expectations. + */ +HWTEST_F_L0(JSArrayIteratorTest, SetNextIndex) +{ + EcmaVM *ecmaVMPtr = thread->GetEcmaVM(); + ObjectFactory *factory = ecmaVMPtr->GetFactory(); + + uint32_t array[10] = {0, 6, 8, 99, 200, 1, -1, -199, 33, 100}; + int numArray = sizeof(array)/sizeof(array[0]); + JSHandle handleTaggedArray(factory->NewTaggedArray(numArray)); + for (int i = 0; i < numArray; i++) { + handleTaggedArray->Set(thread, i, JSTaggedValue(array[i])); + } + JSHandle handleJSObjectTaggedArray(JSArray::CreateArrayFromList(thread, handleTaggedArray)); + + // Call "SetNextIndex" function through "NewJSArrayIterator" function of "object_factory.cpp". + JSHandle handleJSArrayIter = factory->NewJSArrayIterator(handleJSObjectTaggedArray, + IterationKind::KEY); + EXPECT_EQ(handleJSArrayIter->GetNextIndex().GetNumber(), 0); + + int testQuantity = 100; + for (int i = 1; i <= testQuantity; i++) { + JSHandle handleTagValNextIndex(thread, JSTaggedValue(i)); + + // Call "SetNextIndex" function in this HWTEST_F_L0. + handleJSArrayIter->SetNextIndex(thread, handleTagValNextIndex); + EXPECT_EQ(handleJSArrayIter->GetNextIndex().GetNumber(), i); + } +} + +/* + * Feature: JSArrayIterator + * Function: SetIterationKind + * SubFunction: GetIterationKind + * FunctionPoints: Set Iterated Array + * CaseDescription: Call the "SetIterationKind" function, check whether the result returned through "GetIterationKind" + * function from the JSArrayIterator is within expectations. + */ +HWTEST_F_L0(JSArrayIteratorTest, SetIterationKind) +{ + EcmaVM *ecmaVMPtr = thread->GetEcmaVM(); + ObjectFactory *factory = ecmaVMPtr->GetFactory(); + + uint32_t array[10] = {0, 6, 8, 99, 200, 1, -1, -199, 33, 100}; + int numArray = sizeof(array)/sizeof(array[0]); + JSHandle handleTaggedArray(factory->NewTaggedArray(numArray)); + for (int i = 0; i < numArray; i++) { + handleTaggedArray->Set(thread, i, JSTaggedValue(array[i])); + } + JSHandle handleTagVal0(thread, JSTaggedValue(0)); + JSHandle handleTagVal1(thread, JSTaggedValue(1)); + JSHandle handleTagVal2(thread, JSTaggedValue(2)); + JSHandle handleJSObjectTaggedArray(JSArray::CreateArrayFromList(thread, handleTaggedArray)); + + // Call "SetIterationKind" function through "NewJSArrayIterator" function of "object_factory.cpp". + JSHandle handleJSArrayIter = factory->NewJSArrayIterator(handleJSObjectTaggedArray, + IterationKind::KEY); + EXPECT_EQ(handleJSArrayIter->GetIterationKind().GetNumber(), 0); + handleJSArrayIter = factory->NewJSArrayIterator(handleJSObjectTaggedArray, IterationKind::VALUE); + EXPECT_EQ(handleJSArrayIter->GetIterationKind().GetNumber(), 1); + handleJSArrayIter = factory->NewJSArrayIterator(handleJSObjectTaggedArray, IterationKind::KEY_AND_VALUE); + EXPECT_EQ(handleJSArrayIter->GetIterationKind().GetNumber(), 2); + + // Call "SetIterationKind" function in this HWTEST_F_L0. + handleJSArrayIter->SetIterationKind(thread, handleTagVal0); + EXPECT_EQ(handleJSArrayIter->GetIterationKind().GetNumber(), 0); + handleJSArrayIter->SetIterationKind(thread, handleTagVal1); + EXPECT_EQ(handleJSArrayIter->GetIterationKind().GetNumber(), 1); + handleJSArrayIter->SetIterationKind(thread, handleTagVal2); + EXPECT_EQ(handleJSArrayIter->GetIterationKind().GetNumber(), 2); +} +} // namespace panda::ecmascript -- Gitee From bb131c90058efbd38b0f09c679d4cbe797b58611 Mon Sep 17 00:00:00 2001 From: Gongyuhang <517563583@qq.com> Date: Wed, 20 Oct 2021 17:44:18 +0800 Subject: [PATCH 2/2] change the including order of the head files and the format of the BUILD.gn file Signed-off-by: Gongyuhang <517563583@qq.com> --- ecmascript/tests/BUILD.gn | 12 ++++++------ ecmascript/tests/js_array_iterator_test.cpp | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/ecmascript/tests/BUILD.gn b/ecmascript/tests/BUILD.gn index 1f815da2d6..97fccc250b 100644 --- a/ecmascript/tests/BUILD.gn +++ b/ecmascript/tests/BUILD.gn @@ -179,12 +179,12 @@ host_unittest_action("GlueRegsTest") { } } -host_unittest_action("JsArrayTest") { +host_unittest_action("JsArrayIteratorTest") { module_out_path = module_output_path sources = [ # test file - "js_array_test.cpp", + "js_array_iterator_test.cpp", ] configs = [ @@ -206,12 +206,12 @@ host_unittest_action("JsArrayTest") { } } -host_unittest_action("JsArrayIteratorTest") { +host_unittest_action("JsArrayTest") { module_out_path = module_output_path sources = [ # test file - "js_array_iterator_test.cpp", + "js_array_test.cpp", ] configs = [ @@ -949,8 +949,8 @@ group("unittest") { ":GlueRegsTest", ":HugeObjectTest", ":JsArgumentsTest", - ":JsArrayTest", ":JsArrayIteratorTest", + ":JsArrayTest", ":JsDateTest", ":JsForinIteratorTest", ":JsFunctionTest", @@ -991,8 +991,8 @@ group("host_unittest") { ":GlueRegsTestAction", ":HugeObjectTestAction", ":JsArgumentsTestAction", - ":JsArrayTestAction", ":JsArrayIteratorTestAction", + ":JsArrayTestAction", ":JsDateTestAction", ":JsForinIteratorTestAction", ":JsFunctionTestAction", diff --git a/ecmascript/tests/js_array_iterator_test.cpp b/ecmascript/tests/js_array_iterator_test.cpp index 170b6eec5c..1097e15aa4 100644 --- a/ecmascript/tests/js_array_iterator_test.cpp +++ b/ecmascript/tests/js_array_iterator_test.cpp @@ -13,9 +13,9 @@ * limitations under the License. */ +#include "ecmascript/global_env.h" #include "ecmascript/js_array_iterator.h" #include "ecmascript/js_array.h" -#include "ecmascript/global_env.h" #include "ecmascript/object_factory.h" #include "ecmascript/tests/test_helper.h" -- Gitee