!1974 Add a unit-test for TaggedArray and Split case

Merge pull request !1974 from hjzhangcm/master
This commit is contained in:
openharmony_ci 2022-08-03 13:35:31 +00:00 committed by Gitee
commit 4d92305e9d
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
3 changed files with 227 additions and 6 deletions

View File

@ -102,6 +102,26 @@ host_unittest_action("EcmaVm_002_Test") {
"js_proxy_test.cpp",
"js_regexp_iterator_test.cpp",
"js_relative_time_format_test.cpp",
]
configs = [
"//arkcompiler/ets_runtime:ecma_test_config",
"//arkcompiler/ets_runtime:icu_path_test_config",
]
deps = [
"$third_party_gn_path/icu/icu4c:shared_icui18n",
"$third_party_gn_path/icu/icu4c:shared_icuuc",
"//arkcompiler/ets_runtime:libark_jsruntime_test",
sdk_libc_secshared_dep,
]
}
host_unittest_action("EcmaVm_003_Test") {
module_out_path = module_output_path
sources = [
# test file
"js_serializer_test.cpp",
"js_set_iterator_test.cpp",
"js_set_test.cpp",
@ -122,20 +142,16 @@ host_unittest_action("EcmaVm_002_Test") {
"rb_tree_node_test.cpp",
"read_only_space_test.cpp",
"symbol_table_test.cpp",
"tagged_array_test.cpp",
"tagged_tree_test.cpp",
"tagged_value_test.cpp",
"weak_ref_old_gc_test.cpp",
"weak_ref_semi_gc_test.cpp",
]
configs = [
"//arkcompiler/ets_runtime:ecma_test_config",
"//arkcompiler/ets_runtime:icu_path_test_config",
]
configs = [ "//arkcompiler/ets_runtime:ecma_test_config" ]
deps = [
"$third_party_gn_path/icu/icu4c:shared_icui18n",
"$third_party_gn_path/icu/icu4c:shared_icuuc",
"//arkcompiler/ets_runtime:libark_jsruntime_test",
sdk_libc_secshared_dep,
]
@ -148,6 +164,7 @@ group("unittest") {
deps = [
":EcmaVm_001_Test",
":EcmaVm_002_Test",
":EcmaVm_003_Test",
]
}
@ -158,5 +175,6 @@ group("host_unittest") {
deps = [
":EcmaVm_001_TestAction",
":EcmaVm_002_TestAction",
":EcmaVm_003_TestAction",
]
}

View File

@ -0,0 +1,198 @@
/*
* Copyright (c) 2022 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/tagged_array.h"
#include "ecmascript/global_env.h"
#include "ecmascript/tests/test_helper.h"
using namespace panda::ecmascript;
namespace panda::test {
class TaggedArrayTest : 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);
}
EcmaVM *instance {nullptr};
EcmaHandleScope *scope {nullptr};
JSThread *thread {nullptr};
};
static JSObject *JSArrayTestCreate(JSThread *thread)
{
[[maybe_unused]] ecmascript::EcmaHandleScope scope(thread);
EcmaVM *ecmaVM = thread->GetEcmaVM();
auto globalEnv = ecmaVM->GetGlobalEnv();
JSHandle<JSTaggedValue> jsFunc = globalEnv->GetArrayFunction();
JSHandle<JSObject> newArray =
ecmaVM->GetFactory()->NewJSObjectByConstructor(JSHandle<JSFunction>(jsFunc), jsFunc);
return *newArray;
}
HWTEST_F_L0(TaggedArrayTest, Create)
{
ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
uint32_t arrayLength = 2;
JSHandle<TaggedArray> taggedArray = factory->NewTaggedArray(arrayLength);
EXPECT_TRUE(*taggedArray != nullptr);
// every element of the taggedarray is JSTaggedValue::Hole()
for (uint32_t i = 0; i < arrayLength; i++) {
EXPECT_TRUE(taggedArray->Get(i).IsHole());
}
}
HWTEST_F_L0(TaggedArrayTest, SetAndGetIdx)
{
EcmaVM *ecmaVM = thread->GetEcmaVM();
ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
uint32_t arrayLength = 2;
JSHandle<TaggedArray> taggedArray = factory->NewTaggedArray(arrayLength);
JSHandle<JSObject> newObj1(thread, JSArrayTestCreate(thread));
JSObject *newObj2 = JSArrayTestCreate(thread);
JSTaggedValue objValue(newObj2);
taggedArray->Set(thread, 0, newObj1);
taggedArray->Set(thread, 1, objValue);
EXPECT_EQ(taggedArray->GetIdx(newObj1.GetTaggedValue()), 0U);
EXPECT_EQ(taggedArray->GetIdx(objValue), 1U);
// trigger gc
ecmaVM->CollectGarbage(TriggerGCType::OLD_GC);
EXPECT_EQ(taggedArray->GetIdx(newObj1.GetTaggedValue()), 0U);
EXPECT_EQ(taggedArray->GetIdx(objValue), TaggedArray::MAX_ARRAY_INDEX);
}
HWTEST_F_L0(TaggedArrayTest, Append)
{
EcmaVM *ecmaVM = thread->GetEcmaVM();
ecmaVM->SetEnableForceGC(false); // turn off GC
ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
uint32_t arrayLength = 2;
JSHandle<TaggedArray> taggedArray1 = factory->NewTaggedArray(arrayLength);
JSHandle<TaggedArray> taggedArray2 = factory->NewTaggedArray(arrayLength);
// create value of the taggedarray
JSHandle<JSObject> newObj1(thread, JSArrayTestCreate(thread));
JSObject *newObj2 = JSArrayTestCreate(thread);
JSTaggedValue objValue(newObj2);
// set value to the taggedarray
taggedArray1->Set(thread, 0, newObj1);
taggedArray1->Set(thread, 1, JSHandle<JSTaggedValue>(thread, JSTaggedValue::Undefined()));
taggedArray2->Set(thread, 0, objValue);
taggedArray2->Set(thread, 1, JSTaggedValue::Undefined());
// append two taggedarray
JSHandle<TaggedArray> appendArray = TaggedArray::Append(thread, taggedArray1, taggedArray2);
EXPECT_EQ(appendArray->GetLength(), arrayLength * 2);
EXPECT_EQ(appendArray->Get(0), newObj1.GetTaggedValue());
EXPECT_EQ(appendArray->Get(2), objValue); // 2: the second index
EXPECT_EQ(appendArray->Get(1), appendArray->Get(3)); // 3: the third index
ecmaVM->SetEnableForceGC(true); // turn on GC
}
HWTEST_F_L0(TaggedArrayTest, AppendSkipHole)
{
ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
uint32_t arrayLength = 2;
uint32_t twoArrayLength = arrayLength * 2 + 1;
JSHandle<TaggedArray> taggedArray1 = factory->NewTaggedArray(arrayLength + 1);
JSHandle<TaggedArray> taggedArray2 = factory->NewTaggedArray(arrayLength);
// create value of the taggedarray
JSHandle<JSObject> newObj1(thread, JSArrayTestCreate(thread));
// set value to the taggedarray
taggedArray1->Set(thread, 0, newObj1);
taggedArray1->Set(thread, 1, newObj1.GetTaggedValue());
taggedArray1->Set(thread, 2, JSHandle<JSTaggedValue>(thread, JSTaggedValue::Undefined()));
taggedArray2->Set(thread, 1, newObj1.GetTaggedValue());
// append two taggedarray
JSHandle<TaggedArray> appendArray = TaggedArray::AppendSkipHole(thread, taggedArray1, taggedArray2, twoArrayLength);
EXPECT_EQ(appendArray->GetLength(), twoArrayLength);
EXPECT_EQ(appendArray->Get(0), appendArray->Get(1));
EXPECT_TRUE(appendArray->Get(2).IsUndefined()); // 2: the second index
EXPECT_TRUE(appendArray->Get(3).IsHole()); // 3: the third index
EXPECT_TRUE(appendArray->Get(4).IsHole()); // 4: the fourth index
}
HWTEST_F_L0(TaggedArrayTest, HasDuplicateEntry)
{
EcmaVM *ecmaVM = thread->GetEcmaVM();
ObjectFactory *factory = ecmaVM->GetFactory();
uint32_t arrayLength = 10;
JSHandle<TaggedArray> taggedArray = factory->NewTaggedArray(arrayLength);
JSHandle<JSTaggedValue> value1(thread, JSTaggedValue(1)); // 1: means value
JSHandle<JSTaggedValue> value3(thread, JSTaggedValue(3)); // 3: means value
JSHandle<JSObject> newObj1(thread, JSArrayTestCreate(thread));
JSObject *newObj2 = JSArrayTestCreate(thread);
JSTaggedValue objValue(newObj2);
for (uint32_t i = 0; i < arrayLength; i++) {
JSHandle<JSTaggedValue> arrayValue(thread, JSTaggedValue(i));
taggedArray->Set(thread, i, arrayValue.GetTaggedValue());
}
EXPECT_FALSE(taggedArray->HasDuplicateEntry());
// set value that is the same as the thrid index
taggedArray->Set(thread, 1, value3.GetTaggedValue());
EXPECT_TRUE(taggedArray->HasDuplicateEntry());
// resert value in the second index
taggedArray->Set(thread, 1, value1.GetTaggedValue());
taggedArray->Set(thread, 5, newObj1); // 5: the fifth index
taggedArray->Set(thread, 6, newObj1.GetTaggedValue()); // 6: the sixth index
EXPECT_TRUE(taggedArray->HasDuplicateEntry());
}
HWTEST_F_L0(TaggedArrayTest, Trim)
{
uint32_t oldArrayLength = 10;
uint32_t newArrayLength = 5;
auto globalEnv = thread->GetEcmaVM()->GetGlobalEnv();
ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
JSHandle<JSTaggedValue> indexValue(thread, JSTaggedValue(6));
JSHandle<JSTaggedValue> undefinedValue(thread, JSTaggedValue::Undefined());
JSHandle<JSTaggedValue> jsFunc = globalEnv->GetObjectFunction();
JSHandle<JSObject> TaggedArrayObj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(jsFunc), jsFunc);
// Create taggedarray with oldArrayLength
JSHandle<TaggedArray> taggedArray = factory->NewTaggedArray(oldArrayLength);
for (uint32_t i = 0; i < oldArrayLength; i++) {
JSHandle<JSTaggedValue> arrayValue(thread, JSTaggedValue(i));
taggedArray->Set(thread, i, arrayValue.GetTaggedValue());
}
TaggedArrayObj->SetElements(thread, taggedArray);
EXPECT_EQ(JSObject::GetProperty(thread, TaggedArrayObj, indexValue).GetValue(), indexValue);
// trim taggedarray
taggedArray->Trim(thread, newArrayLength);
for (uint32_t i = 0; i < newArrayLength; i++) {
JSHandle<JSTaggedValue> arrayValue(thread, JSTaggedValue(i));
EXPECT_EQ(taggedArray->Get(i), arrayValue.GetTaggedValue());
}
EXPECT_EQ(taggedArray->GetLength(), newArrayLength);
EXPECT_EQ(JSObject::GetProperty(thread, TaggedArrayObj, indexValue).GetValue(), undefinedValue);
}
} // namespace panda::ecmascript

View File

@ -95,6 +95,11 @@
<option name="push" value="test/test/libark_jsruntime_test.so -> /data/test" src="out"/>
</preparer>
</target>
<target name="EcmaVm_003_Test">
<preparer>
<option name="push" value="test/test/libark_jsruntime_test.so -> /data/test" src="out"/>
</preparer>
</target>
<target name="StubTest">
<preparer>
<option name="push" value="test/test/libark_jsruntime_test.so -> /data/test" src="out"/>