diff --git a/ecmascript/tests/BUILD.gn b/ecmascript/tests/BUILD.gn index ce5365165d4d12eedf6c709ed65cd34fce35ec7f..067a17afa1e3d3c11c07f039ba7723fe8f61d8e6 100644 --- a/ecmascript/tests/BUILD.gn +++ b/ecmascript/tests/BUILD.gn @@ -98,33 +98,6 @@ host_unittest_action("DumpTest") { } } -host_unittest_action("EcmaModuleTest") { - module_out_path = module_output_path - - sources = [ - # test file - "ecma_module_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("EcmaStringTest") { module_out_path = module_output_path @@ -179,33 +152,6 @@ host_unittest_action("GlueRegsTest") { } } -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("JsArrayTest") { module_out_path = module_output_path @@ -233,12 +179,12 @@ host_unittest_action("JsArrayTest") { } } -host_unittest_action("JsDataViewTest") { +host_unittest_action("JsDateTest") { module_out_path = module_output_path sources = [ # test file - "js_dataview_test.cpp", + "js_date_test.cpp", ] configs = [ @@ -260,12 +206,12 @@ host_unittest_action("JsDataViewTest") { } } -host_unittest_action("JsDateTest") { +host_unittest_action("JsForinIteratorTest") { module_out_path = module_output_path sources = [ # test file - "js_date_test.cpp", + "js_forin_iterator_test.cpp", ] configs = [ @@ -287,12 +233,12 @@ host_unittest_action("JsDateTest") { } } -host_unittest_action("JsForinIteratorTest") { +host_unittest_action("JsFunctionTest") { module_out_path = module_output_path sources = [ # test file - "js_forin_iterator_test.cpp", + "js_function_test.cpp", ] configs = [ @@ -314,12 +260,12 @@ host_unittest_action("JsForinIteratorTest") { } } -host_unittest_action("JsFunctionTest") { +host_unittest_action("JsHandleTest") { module_out_path = module_output_path sources = [ # test file - "js_function_test.cpp", + "js_handle_test.cpp", ] configs = [ @@ -341,12 +287,12 @@ host_unittest_action("JsFunctionTest") { } } -host_unittest_action("JsHandleTest") { +host_unittest_action("JsIteratorTest") { module_out_path = module_output_path sources = [ # test file - "js_handle_test.cpp", + "js_iterator_test.cpp", ] configs = [ @@ -356,7 +302,6 @@ host_unittest_action("JsHandleTest") { "//ark/js_runtime:ark_jsruntime_common_config", "$ark_root/runtime:arkruntime_public_config", ] - deps = [ "$ark_root/libpandabase:libarkbase", "//ark/js_runtime:libark_jsruntime_test", @@ -368,12 +313,12 @@ host_unittest_action("JsHandleTest") { } } -host_unittest_action("JsIteratorTest") { +host_unittest_action("JsMapTest") { module_out_path = module_output_path sources = [ # test file - "js_iterator_test.cpp", + "js_map_test.cpp", ] configs = [ @@ -383,6 +328,7 @@ host_unittest_action("JsIteratorTest") { "//ark/js_runtime:ark_jsruntime_common_config", "$ark_root/runtime:arkruntime_public_config", ] + deps = [ "$ark_root/libpandabase:libarkbase", "//ark/js_runtime:libark_jsruntime_test", @@ -394,12 +340,12 @@ host_unittest_action("JsIteratorTest") { } } -host_unittest_action("JsMapTest") { +host_unittest_action("JsObjectTest") { module_out_path = module_output_path sources = [ # test file - "js_map_test.cpp", + "js_object_test.cpp", ] configs = [ @@ -421,12 +367,12 @@ host_unittest_action("JsMapTest") { } } -host_unittest_action("JsObjectTest") { +host_unittest_action("JsPrimitiveRefTest") { module_out_path = module_output_path sources = [ # test file - "js_object_test.cpp", + "js_primitive_ref_test.cpp", ] configs = [ @@ -448,12 +394,12 @@ host_unittest_action("JsObjectTest") { } } -host_unittest_action("JsPrimitiveRefTest") { +host_unittest_action("JsPromiseTest") { module_out_path = module_output_path sources = [ # test file - "js_primitive_ref_test.cpp", + "js_promise_test.cpp", ] configs = [ @@ -475,12 +421,12 @@ host_unittest_action("JsPrimitiveRefTest") { } } -host_unittest_action("JsPromiseTest") { +host_unittest_action("JsProxyTest") { module_out_path = module_output_path sources = [ # test file - "js_promise_test.cpp", + "js_proxy_test.cpp", ] configs = [ @@ -502,12 +448,12 @@ host_unittest_action("JsPromiseTest") { } } -host_unittest_action("JsProxyTest") { +host_unittest_action("JsSetTest") { module_out_path = module_output_path sources = [ # test file - "js_proxy_test.cpp", + "js_set_test.cpp", ] configs = [ @@ -529,12 +475,12 @@ host_unittest_action("JsProxyTest") { } } -host_unittest_action("JsSetTest") { +host_unittest_action("JsSetIteratorTest") { module_out_path = module_output_path sources = [ # test file - "js_set_test.cpp", + "js_set_iterator_test.cpp", ] configs = [ @@ -970,15 +916,12 @@ group("unittest") { ":AssertScopeTest", ":BuiltinsTest", ":DumpTest", - ":EcmaModuleTest", ":EcmaStringTest", ":GcTest", ":GlueRegsTest", ":HugeObjectTest", ":JsArgumentsTest", - ":JsArrayIteratorTest", ":JsArrayTest", - ":JsDataViewTest", ":JsDateTest", ":JsForinIteratorTest", ":JsFunctionTest", @@ -991,6 +934,7 @@ group("unittest") { ":JsProxyTest", ":JsSerializerTest", ":JsSetTest", + ":JsSetIteratorTest", ":JsSymbolTest", ":JsTaggedQueueTest", ":JsVerificationTest", @@ -1013,15 +957,12 @@ group("host_unittest") { ":AssertScopeTestAction", ":BuiltinsTestAction", ":DumpTestAction", - ":EcmaModuleTestAction", ":EcmaStringTestAction", ":GcTestAction", ":GlueRegsTestAction", ":HugeObjectTestAction", ":JsArgumentsTestAction", - ":JsArrayIteratorTestAction", ":JsArrayTestAction", - ":JsDataViewTestAction", ":JsDateTestAction", ":JsForinIteratorTestAction", ":JsFunctionTestAction", @@ -1034,6 +975,7 @@ group("host_unittest") { ":JsProxyTestAction", ":JsSerializerTestAction", ":JsSetTestAction", + ":JsSetIteratorTestAction", ":JsSymbolTestAction", ":JsTaggedQueueTestAction", ":JsVerificationTestAction", diff --git a/ecmascript/tests/js_set_iterator_test.cpp b/ecmascript/tests/js_set_iterator_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0a796eae1b433fcfa2ea6009d4dce6c9eb741381 --- /dev/null +++ b/ecmascript/tests/js_set_iterator_test.cpp @@ -0,0 +1,147 @@ +/* + * 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_set_iterator.h" +#include "ecmascript/js_set.h" +#include "ecmascript/js_thread.h" +#include "ecmascript/global_env.h" +#include "ecmascript/linked_hash_table-inl.h" +#include "ecmascript/object_factory.h" +#include "ecmascript/js_array.h" +#include "ecmascript/tests/test_helper.h" + +using namespace panda::ecmascript; + +namespace panda::test { +class JsSetIteratorTest : public testing::Test { +public: + static void SetUpTestCase() + { + GTEST_LOG_(INFO) << "SetUpTestCase"; + } + + static void TearDownTestCase() + { + GTEST_LOG_(INFO) << "TearDownCase"; + } + + void SetUp() override + { + TestHelper::CreateEcmaVMWithScope(instance, thread, scope); + } + + void TearDown() override + { + TestHelper::DestroyEcmaVMWithScope(instance, scope); + } + ecmascript::EcmaHandleScope *scope {nullptr}; + PandaVM *instance {nullptr}; + JSThread *thread {nullptr}; + +protected: + JSSet *CreateSet() + { + ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); + JSHandle env = thread->GetEcmaVM()->GetGlobalEnv(); + + JSHandle constructor = env->GetBuiltinsSetFunction(); + JSHandle set = + JSHandle::Cast(factory->NewJSObjectByConstructor(JSHandle(constructor), constructor)); + LinkedHashSet *hashSet = LinkedHashSet::Cast(LinkedHashSet::Create(thread).GetTaggedObject()); + set->SetLinkedSet(thread, JSTaggedValue(hashSet)); + return JSSet::Cast(set.GetTaggedValue().GetTaggedObject()); + } +}; + +HWTEST_F_L0(JsSetIteratorTest, CreateSetIterator) +{ + JSHandle jsSet(thread, CreateSet()); + EXPECT_TRUE(*jsSet != nullptr); + + JSHandle setIteratorValue = + JSSetIterator::CreateSetIterator(thread, JSHandle(jsSet), IterationKind::KEY); + + EXPECT_EQ(setIteratorValue->IsJSSetIterator(), true); + JSHandle setIterator(setIteratorValue); + EXPECT_EQ(JSTaggedValue::SameValue(setIterator->GetIteratedSet(), jsSet->GetLinkedSet()), true); + EXPECT_EQ(setIterator->GetNextIndex().GetInt(), 0); + EXPECT_EQ(JSTaggedValue::SameValue(setIterator->GetIterationKind(), JSTaggedValue(static_cast(IterationKind::KEY))), true); +} + +HWTEST_F_L0(JsSetIteratorTest, Update) +{ + JSHandle jsSet(thread, CreateSet()); + EXPECT_TRUE(*jsSet != nullptr); + + for (int i = 0; i < 3; i++) { + JSHandle key(thread, JSTaggedValue(i)); + JSSet::Add(thread, jsSet, key); + } + + JSHandle setIteratorValue = + JSSetIterator::CreateSetIterator(thread, JSHandle(jsSet), IterationKind::KEY); + JSHandle setIterator(setIteratorValue); + + JSHandle keyResult1 = JSIterator::IteratorStep(thread, setIteratorValue); + EXPECT_EQ(0, JSIterator::IteratorValue(thread, keyResult1)->GetInt()); + JSHandle keyResult2 = JSIterator::IteratorStep(thread, setIteratorValue); + EXPECT_EQ(1, JSIterator::IteratorValue(thread, keyResult2)->GetInt()); + setIterator->Update(thread); + EXPECT_EQ(0, JSIterator::IteratorValue(thread, keyResult1)->GetInt()); +} + +HWTEST_F_L0(JsSetIteratorTest, Next) +{ + JSHandle jsSet(thread, CreateSet()); + EXPECT_TRUE(*jsSet != nullptr); + + for (int i = 0; i < 3; i++) { + JSHandle key(thread, JSTaggedValue(i)); + JSSet::Add(thread, jsSet, key); + } + + // set IterationKind(key or value) + JSHandle setIteratorValue = + JSSetIterator::CreateSetIterator(thread, JSHandle(jsSet), IterationKind::KEY); + JSHandle setIterator(setIteratorValue); + + auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); + ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); + ecmaRuntimeCallInfo->SetThis(setIteratorValue.GetTaggedValue()); + ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue::Undefined()); + [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo.get()); + + JSTaggedValue result1 = JSSetIterator::Next(ecmaRuntimeCallInfo.get()); + EXPECT_EQ(setIterator->GetNextIndex().GetInt(), 1); + JSHandle resultObj1(thread, result1); + EXPECT_EQ(0, JSIterator::IteratorValue(thread, resultObj1)->GetInt()); + + JSTaggedValue result2 = JSSetIterator::Next(ecmaRuntimeCallInfo.get()); + EXPECT_EQ(setIterator->GetNextIndex().GetInt(), 2); + JSHandle resultObj2(thread, result2); + EXPECT_EQ(1, JSIterator::IteratorValue(thread, resultObj2)->GetInt()); + + JSTaggedValue result3 = JSSetIterator::Next(ecmaRuntimeCallInfo.get()); + EXPECT_EQ(setIterator->GetNextIndex().GetInt(), 3); + JSHandle resultObj3(thread, result3); + EXPECT_EQ(2, JSIterator::IteratorValue(thread, resultObj3)->GetInt()); + + JSTaggedValue result4 = JSSetIterator::Next(ecmaRuntimeCallInfo.get()); + JSHandle resultObj4(thread, result4); + EXPECT_EQ(JSIterator::IteratorValue(thread, resultObj4).GetTaggedValue(), JSTaggedValue::Undefined()); + + TestHelper::TearDownFrame(thread, prev); +} +} // namespace panda::test \ No newline at end of file