arkcompiler_ets_runtime/ecmascript/tests/huge_object_test.cpp
Riachel 96adb30222 Descriptor: delete native_stack.h, trace.h and mem.h
details: delete native_stack.h, trace.h and mem.h
issue:https://gitee.com/openharmony/arkcompiler_runtime_core/issues/I5IS38

Signed-off-by: Riachel <caolili14@huawei.com>
2022-07-28 19:15:13 +08:00

123 lines
4.0 KiB
C++

/*
* 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/ecma_vm.h"
#include "ecmascript/global_env.h"
#include "ecmascript/js_handle.h"
#include "ecmascript/mem/mem_common.h"
#include "ecmascript/mem/space.h"
#include "ecmascript/mem/verification.h"
#include "ecmascript/object_factory.h"
#include "ecmascript/tagged_array-inl.h"
#include "ecmascript/tests/test_helper.h"
using namespace panda::ecmascript;
namespace panda::test {
class HugeObjectTest : 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);
thread->GetEcmaVM()->SetEnableForceGC(false);
const_cast<Heap *>(thread->GetEcmaVM()->GetHeap())->SetMarkType(MarkType::MARK_FULL);
}
void TearDown() override
{
TestHelper::DestroyEcmaVMWithScope(instance, scope);
}
JSThread *thread {nullptr};
EcmaVM *instance {nullptr};
ecmascript::EcmaHandleScope *scope {nullptr};
};
static JSObject *JSObjectTestCreate(JSThread *thread)
{
[[maybe_unused]] ecmascript::EcmaHandleScope scope(thread);
EcmaVM *ecmaVM = thread->GetEcmaVM();
auto globalEnv = ecmaVM->GetGlobalEnv();
JSHandle<JSTaggedValue> jsFunc = globalEnv->GetObjectFunction();
JSHandle<JSObject> newObj =
ecmaVM->GetFactory()->NewJSObjectByConstructor(JSHandle<JSFunction>(jsFunc), jsFunc);
return *newObj;
}
static TaggedArray *LargeArrayTestCreate(JSThread *thread)
{
[[maybe_unused]] ecmascript::EcmaHandleScope scope(thread);
static constexpr size_t SIZE = 1_MB;
JSHandle<TaggedArray> array = thread->GetEcmaVM()->GetFactory()->NewTaggedArray(SIZE);
return *array;
}
HWTEST_F_L0(HugeObjectTest, LargeArrayKeep)
{
TaggedArray *array = LargeArrayTestCreate(thread);
EXPECT_TRUE(array != nullptr);
JSHandle<TaggedArray> arrayHandle(thread, array);
JSHandle<JSObject> newObj(thread, JSObjectTestCreate(thread));
arrayHandle->Set(thread, 0, newObj.GetTaggedValue());
auto ecmaVm = thread->GetEcmaVM();
EXPECT_EQ(*arrayHandle, reinterpret_cast<TaggedObject *>(array));
ecmaVm->CollectGarbage(TriggerGCType::YOUNG_GC); // Trigger GC.
ecmaVm->CollectGarbage(TriggerGCType::OLD_GC); // Trigger GC.
EXPECT_EQ(*newObj, array->Get(0).GetTaggedObject());
EXPECT_EQ(*arrayHandle, reinterpret_cast<TaggedObject *>(array));
}
HWTEST_F_L0(HugeObjectTest, MultipleArrays)
{
auto ecmaVm = thread->GetEcmaVM();
auto heap = ecmaVm->GetHeap();
{
[[maybe_unused]] ecmascript::EcmaHandleScope scope(thread);
for (int i = 0; i <= 14; i++) {
JSHandle<TaggedArray> array1(thread, LargeArrayTestCreate(thread));
}
}
{
[[maybe_unused]] ecmascript::EcmaHandleScope scope(thread);
for (int i = 0; i <= 14; i++) {
JSHandle<TaggedArray> array2(thread, LargeArrayTestCreate(thread));
}
}
{
[[maybe_unused]] ecmascript::EcmaHandleScope scope(thread);
for (int i = 0; i <= 14; i++) {
JSHandle<TaggedArray> array2(thread, LargeArrayTestCreate(thread));
}
}
size_t failCount = 0;
VerifyObjectVisitor objVerifier(heap, &failCount);
heap->GetHugeObjectSpace()->IterateOverObjects(objVerifier); // newspace reference the old space
EXPECT_EQ(failCount, 0U);
}
} // namespace panda::test