1 /*
2 * Copyright (c) 2025 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 #include "ecmascript/compiler/aot_file/binary_buffer_parser.h"
18 #include "ecmascript/tests/test_helper.h"
19
20 using namespace panda::ecmascript;
21
22 namespace panda::test {
23 class BinaryBufferParserTest : public testing::Test {
24 public:
SetUpTestCase()25 static void SetUpTestCase()
26 {
27 GTEST_LOG_(INFO) << "SetUpTestCase";
28 }
TearDownTestCase()29 static void TearDownTestCase()
30 {
31 GTEST_LOG_(INFO) << "TearDownTestCase";
32 }
SetUp()33 void SetUp() override {}
TearDown()34 void TearDown() override {}
35 };
36
HWTEST_F_L0(BinaryBufferParserTest,BasicParse)37 HWTEST_F_L0(BinaryBufferParserTest, BasicParse)
38 {
39 // This test verifies the basic functionality of BinaryBufferParser:
40 // 1. ParseBuffer copies the first 4 bytes from the buffer to dst.
41 // 2. ParseBuffer with offset copies bytes 5 and 6 to dst2.
42 // 3. ParseBuffer with src pointer copies bytes 7 and 8 to dst3.
43 // 4. GetAddr returns the original buffer pointer.
44 uint8_t buf[8] = {1, 2, 3, 4, 5, 6, 7, 8};
45 BinaryBufferParser parser(buf, 8);
46
47 uint8_t dst[4] = {0};
48 parser.ParseBuffer(dst, 4);
49 ASSERT_EQ(dst[0], 1);
50 ASSERT_EQ(dst[1], 2);
51 ASSERT_EQ(dst[2], 3);
52 ASSERT_EQ(dst[3], 4);
53
54 // Use ParseBuffer with offset
55 uint8_t dst2[2] = {0};
56 parser.ParseBuffer(dst2, 2, 4);
57 ASSERT_EQ(dst2[0], 5);
58 ASSERT_EQ(dst2[1], 6);
59
60 // Use ParseBuffer with src pointer
61 uint8_t dst3[2] = {0};
62 parser.ParseBuffer(dst3, 2, buf+6);
63 ASSERT_EQ(dst3[0], 7);
64 ASSERT_EQ(dst3[1], 8);
65
66 // GetAddr should return the original buffer pointer
67 ASSERT_EQ(parser.GetAddr(), buf);
68 }
69
70 } // namespace panda::test
71