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/mem/dyn_chunk.h"
20 #include "ecmascript/object_factory.h"
21
22 namespace panda::test {
23 using namespace panda::ecmascript;
24
25 class DynChunkTest : 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 chunk_ = thread->GetEcmaVM()->GetChunk();
41 }
42
TearDown()43 void TearDown() override
44 {
45 TestHelper::DestroyEcmaVMWithScope(instance, scope);
46 }
47
48 EcmaVM *instance {nullptr};
49 JSThread *thread {nullptr};
50 EcmaHandleScope *scope {nullptr};
51 Chunk *chunk_ {nullptr};
52 };
53
HWTEST_F_L0(DynChunkTest,EmitAndGet)54 HWTEST_F_L0(DynChunkTest, EmitAndGet)
55 {
56 DynChunk dynChunk = DynChunk(chunk_);
57 dynChunk.EmitChar(65);
58 dynChunk.EmitU16(66);
59 dynChunk.EmitU32(67);
60 ASSERT_EQ(dynChunk.GetSize(), 7U);
61 ASSERT_EQ(dynChunk.GetAllocatedSize(), DynChunk::ALLOCATE_MIN_SIZE);
62 ASSERT_EQ(dynChunk.GetError(), false);
63 dynChunk.Insert(1, 1);
64 uint32_t val1 = dynChunk.GetU8(0);
65 uint32_t val2 = dynChunk.GetU16(2);
66 uint32_t val3 = dynChunk.GetU32(4);
67 ASSERT_EQ(val1, 65U);
68 ASSERT_EQ(val2, 66U);
69 ASSERT_EQ(val3, 67U);
70 }
71
HWTEST_F_L0(DynChunkTest,EmitSelfAndGet)72 HWTEST_F_L0(DynChunkTest, EmitSelfAndGet)
73 {
74 DynChunk dynChunk = DynChunk(chunk_);
75 dynChunk.EmitChar(65);
76 dynChunk.EmitSelf(0, 1);
77 ASSERT_EQ(dynChunk.GetSize(), 2U);
78 ASSERT_EQ(dynChunk.GetAllocatedSize(), DynChunk::ALLOCATE_MIN_SIZE);
79 ASSERT_EQ(dynChunk.GetError(), false);
80 uint32_t val1 = dynChunk.GetU8(0);
81 uint32_t val2 = dynChunk.GetU8(1);
82 ASSERT_EQ(val1, 65U);
83 ASSERT_EQ(val2, 65U);
84 }
85
HWTEST_F_L0(DynChunkTest,EmitStrAndGet)86 HWTEST_F_L0(DynChunkTest, EmitStrAndGet)
87 {
88 DynChunk dynChunk = DynChunk(chunk_);
89 dynChunk.EmitStr("abc");
90 ASSERT_EQ(dynChunk.GetSize(), 4U);
91 ASSERT_EQ(dynChunk.GetAllocatedSize(), DynChunk::ALLOCATE_MIN_SIZE);
92 ASSERT_EQ(dynChunk.GetError(), false);
93 uint32_t val1 = dynChunk.GetU8(0);
94 uint32_t val2 = dynChunk.GetU8(1);
95 uint32_t val3 = dynChunk.GetU8(2);
96 uint32_t val4 = dynChunk.GetU8(3);
97 ASSERT_EQ(val1, 97U);
98 ASSERT_EQ(val2, 98U);
99 ASSERT_EQ(val3, 99U);
100 ASSERT_EQ(val4, 0U);
101 }
102 } // namespace panda::test
103