1 /**
2 * Copyright (c) 2021-2024 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 "gtest/gtest.h"
17
18 #include "runtime/include/runtime.h"
19 #include "runtime/regexp/ecmascript/mem/dyn_chunk.h"
20
21 namespace ark::mem::test {
22
23 // NOLINTNEXTLINE(google-build-using-namespace)
24 using namespace ark::coretypes;
25
26 class DynBufferTest : public testing::Test {
27 public:
DynBufferTest()28 DynBufferTest()
29 {
30 RuntimeOptions options;
31 options.SetShouldLoadBootPandaFiles(false);
32 options.SetShouldInitializeIntrinsics(false);
33 Runtime::Create(options);
34 thread_ = ark::MTManagedThread::GetCurrent();
35 thread_->ManagedCodeBegin();
36 }
37
~DynBufferTest()38 ~DynBufferTest() override
39 {
40 thread_->ManagedCodeEnd();
41 Runtime::Destroy();
42 }
43
44 NO_COPY_SEMANTIC(DynBufferTest);
45 NO_MOVE_SEMANTIC(DynBufferTest);
46
47 private:
48 ark::MTManagedThread *thread_ {};
49 };
50
TEST_F(DynBufferTest,EmitAndGet)51 TEST_F(DynBufferTest, EmitAndGet)
52 {
53 DynChunk dynChunk = DynChunk();
54 // NOLINTNEXTLINE(readability-magic-numbers)
55 dynChunk.EmitChar(65U);
56 // NOLINTNEXTLINE(readability-magic-numbers)
57 dynChunk.EmitU16(66U);
58 // NOLINTNEXTLINE(readability-magic-numbers)
59 dynChunk.EmitU32(67U);
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(0U);
65 uint32_t val2 = dynChunk.GetU16(2U);
66 uint32_t val3 = dynChunk.GetU32(4U);
67 ASSERT_EQ(val1, 65U);
68 ASSERT_EQ(val2, 66U);
69 ASSERT_EQ(val3, 67U);
70 }
71
TEST_F(DynBufferTest,EmitSelfAndGet)72 TEST_F(DynBufferTest, EmitSelfAndGet)
73 {
74 DynChunk dynChunk = DynChunk();
75 // NOLINTNEXTLINE(readability-magic-numbers)
76 dynChunk.EmitChar(65U);
77 dynChunk.EmitSelf(0, 1);
78 ASSERT_EQ(dynChunk.GetSize(), 2U);
79 ASSERT_EQ(dynChunk.GetAllocatedSize(), DynChunk::ALLOCATE_MIN_SIZE);
80 ASSERT_EQ(dynChunk.GetError(), false);
81 uint32_t val1 = dynChunk.GetU8(0);
82 uint32_t val2 = dynChunk.GetU8(1);
83 ASSERT_EQ(val1, 65U);
84 ASSERT_EQ(val2, 65U);
85 }
86
TEST_F(DynBufferTest,EmitStrAndGet)87 TEST_F(DynBufferTest, EmitStrAndGet)
88 {
89 DynChunk dynChunk = DynChunk();
90 dynChunk.EmitStr("abc");
91 ASSERT_EQ(dynChunk.GetSize(), 4U);
92 ASSERT_EQ(dynChunk.GetAllocatedSize(), DynChunk::ALLOCATE_MIN_SIZE);
93 ASSERT_EQ(dynChunk.GetError(), false);
94 uint32_t val1 = dynChunk.GetU8(0U);
95 uint32_t val2 = dynChunk.GetU8(1U);
96 uint32_t val3 = dynChunk.GetU8(2U);
97 uint32_t val4 = dynChunk.GetU8(3U);
98 ASSERT_EQ(val1, 97U);
99 ASSERT_EQ(val2, 98U);
100 ASSERT_EQ(val3, 99U);
101 ASSERT_EQ(val4, 0);
102 }
103 } // namespace ark::mem::test
104