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