• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <cstdint>
17 #include <cstdlib>
18 
19 #include "common_components/base/mem_utils.h"
20 #include "common_components/tests/test_helper.h"
21 
22 using namespace common;
23 namespace common::test {
24 class MemUtilsTest : public common::test::BaseTestWithScope {
25 };
26 
HWTEST_F_L0(MemUtilsTest,CopyZeroBytes)27 HWTEST_F_L0(MemUtilsTest, CopyZeroBytes)
28 {
29     char dest[100] = {};
30     const char* src = "hello world";
31     MemoryCopy(reinterpret_cast<uintptr_t>(dest), 0,
32                reinterpret_cast<uintptr_t>(src), strlen(src) + 1);
33     EXPECT_EQ(dest[0], '\0');
34 }
35 
HWTEST_F_L0(MemUtilsTest,CopyTwoChunks)36 HWTEST_F_L0(MemUtilsTest, CopyTwoChunks)
37 {
38     constexpr size_t totalSize = 100;
39     char dest[totalSize] = {};
40     char src[totalSize] = {};
41     for (size_t i = 0; i < totalSize; ++i) {
42         src[i] = static_cast<char>('A' + (i % 26));
43     }
44 
45     MemoryCopy(reinterpret_cast<uintptr_t>(dest), totalSize,
46                reinterpret_cast<uintptr_t>(src), totalSize);
47 
48     EXPECT_EQ(memcmp(dest, src, totalSize), 0);
49 }
50 } // namespace common::test