| 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 | |
| 14 | class LlvmLibcMemccpyTest : public LIBC_NAMESPACE::testing::Test { |
| 15 | public: |
| 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(dst.data(), src.data(), 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 | |
| 40 | TEST_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, 'z', 0, expected, 0, true); |
| 45 | } |
| 46 | |
| 47 | TEST_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, 'x', 0, expected, 0, true); |
| 52 | } |
| 53 | |
| 54 | TEST_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, 'z', 1, expected, 1, true); |
| 59 | } |
| 60 | |
| 61 | TEST_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, 'x', 1, expected, 1); |
| 66 | } |
| 67 | |
| 68 | TEST_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, 'z', 2, expected, 2, true); |
| 73 | } |
| 74 | |
| 75 | TEST_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, 'x', 2, expected, 1); |
| 80 | } |
| 81 | |