1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "ecmascript/tests/test_helper.h"
17
18 #include "ecmascript/ecma_vm.h"
19 #include "ecmascript/global_env.h"
20 #include "ecmascript/js_handle.h"
21
22 using namespace panda::ecmascript;
23
24 namespace panda::test {
25 class ConcurrentSweepTest : public testing::Test {
26 public:
SetUpTestCase()27 static void SetUpTestCase()
28 {
29 GTEST_LOG_(INFO) << "SetUpTestCase";
30 }
31
TearDownTestCase()32 static void TearDownTestCase()
33 {
34 GTEST_LOG_(INFO) << "TearDownCase";
35 }
36
SetUp()37 void SetUp() override
38 {
39 TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
40 }
41
TearDown()42 void TearDown() override
43 {
44 TestHelper::DestroyEcmaVMWithScope(instance, scope);
45 }
46
47 PandaVM *instance {nullptr};
48 EcmaHandleScope *scope {nullptr};
49 JSThread *thread;
50 };
51
TEST_F(ConcurrentSweepTest,ConcurrentSweep)52 TEST_F(ConcurrentSweepTest, ConcurrentSweep)
53 {
54 auto vm = EcmaVM::Cast(instance);
55 const uint8_t *utf8 = reinterpret_cast<const uint8_t *>("test");
56 JSHandle<EcmaString> test1(thread, EcmaString::CreateFromUtf8(utf8, 4, vm, true));
57 if (vm->IsInitialized()) {
58 vm->CollectGarbage(ecmascript::TriggerGCType::OLD_GC);
59 }
60 JSHandle<EcmaString> test2(thread, EcmaString::CreateFromUtf8(utf8, 4, vm, true));
61 ASSERT_EQ(test1->GetLength(), 4);
62 ASSERT_NE(test1.GetTaggedValue().GetHeapObject(), test2.GetTaggedValue().GetHeapObject());
63 }
64 } // namespace panda::test
65