1//===-- Unittests for memccpy ---------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "src/__support/CPP/span.h"
10#include "src/string/memccpy.h"
11#include "test/UnitTest/Test.h"
12#include <stddef.h> // For size_t.
13
14class LlvmLibcMemccpyTest : public LIBC_NAMESPACE::testing::Test {
15public:
16 void check_memccpy(LIBC_NAMESPACE::cpp::span<char> dst,
17 const LIBC_NAMESPACE::cpp::span<const char> src, int end,
18 size_t count,
19 const LIBC_NAMESPACE::cpp::span<const char> expected,
20 size_t expectedCopied, bool shouldReturnNull = false) {
21 // Making sure we don't overflow buffer.
22 ASSERT_GE(dst.size(), count);
23 // Making sure memccpy returns dst.
24 void *result = LIBC_NAMESPACE::memccpy(dest: dst.data(), src: src.data(), c: end, count);
25
26 if (shouldReturnNull) {
27 ASSERT_EQ(result, static_cast<void *>(nullptr));
28 } else {
29 ASSERT_EQ(result, static_cast<void *>(dst.data() + expectedCopied));
30 }
31
32 // Expected must be of the same size as dst.
33 ASSERT_EQ(dst.size(), expected.size());
34 // Expected and dst are the same.
35 for (size_t i = 0; i < expected.size(); ++i)
36 ASSERT_EQ(expected[i], dst[i]);
37 }
38};
39
40TEST_F(LlvmLibcMemccpyTest, UntouchedUnrelatedEnd) {
41 char dst[] = {'a', 'b'};
42 const char src[] = {'x', '\0'};
43 const char expected[] = {'a', 'b'};
44 check_memccpy(dst, src, end: 'z', count: 0, expected, expectedCopied: 0, shouldReturnNull: true);
45}
46
47TEST_F(LlvmLibcMemccpyTest, UntouchedStartsWithEnd) {
48 char dst[] = {'a', 'b'};
49 const char src[] = {'x', '\0'};
50 const char expected[] = {'a', 'b'};
51 check_memccpy(dst, src, end: 'x', count: 0, expected, expectedCopied: 0, shouldReturnNull: true);
52}
53
54TEST_F(LlvmLibcMemccpyTest, CopyOneUnrelatedEnd) {
55 char dst[] = {'a', 'b'};
56 const char src[] = {'x', 'y'};
57 const char expected[] = {'x', 'b'};
58 check_memccpy(dst, src, end: 'z', count: 1, expected, expectedCopied: 1, shouldReturnNull: true);
59}
60
61TEST_F(LlvmLibcMemccpyTest, CopyOneStartsWithEnd) {
62 char dst[] = {'a', 'b'};
63 const char src[] = {'x', 'y'};
64 const char expected[] = {'x', 'b'};
65 check_memccpy(dst, src, end: 'x', count: 1, expected, expectedCopied: 1);
66}
67
68TEST_F(LlvmLibcMemccpyTest, CopyTwoUnrelatedEnd) {
69 char dst[] = {'a', 'b'};
70 const char src[] = {'x', 'y'};
71 const char expected[] = {'x', 'y'};
72 check_memccpy(dst, src, end: 'z', count: 2, expected, expectedCopied: 2, shouldReturnNull: true);
73}
74
75TEST_F(LlvmLibcMemccpyTest, CopyTwoStartsWithEnd) {
76 char dst[] = {'a', 'b'};
77 const char src[] = {'x', 'y'};
78 const char expected[] = {'x', 'b'};
79 check_memccpy(dst, src, end: 'x', count: 2, expected, expectedCopied: 1);
80}
81

source code of libc/test/src/string/memccpy_test.cpp