xref: /llvm-project/libc/test/src/string/strncpy_test.cpp (revision b6bc9d72f65a5086f310f321e969d96e9a559e75)
160cef893SCheng Wang //===-- Unittests for strncpy ---------------------------------------------===//
260cef893SCheng Wang //
360cef893SCheng Wang // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
460cef893SCheng Wang // See https://llvm.org/LICENSE.txt for license information.
560cef893SCheng Wang // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
660cef893SCheng Wang //
760cef893SCheng Wang //===----------------------------------------------------------------------===//
860cef893SCheng Wang 
922bc0fdeSGuillaume Chatelet #include "src/__support/CPP/span.h"
1060cef893SCheng Wang #include "src/string/strncpy.h"
11af1315c2SSiva Chandra Reddy #include "test/UnitTest/Test.h"
1260cef893SCheng Wang #include <stddef.h> // For size_t.
1360cef893SCheng Wang 
14*b6bc9d72SGuillaume Chatelet class LlvmLibcStrncpyTest : public LIBC_NAMESPACE::testing::Test {
1560cef893SCheng Wang public:
check_strncpy(LIBC_NAMESPACE::cpp::span<char> dst,const LIBC_NAMESPACE::cpp::span<const char> src,size_t n,const LIBC_NAMESPACE::cpp::span<const char> expected)16*b6bc9d72SGuillaume Chatelet   void check_strncpy(LIBC_NAMESPACE::cpp::span<char> dst,
17*b6bc9d72SGuillaume Chatelet                      const LIBC_NAMESPACE::cpp::span<const char> src, size_t n,
18*b6bc9d72SGuillaume Chatelet                      const LIBC_NAMESPACE::cpp::span<const char> expected) {
1960cef893SCheng Wang     // Making sure we don't overflow buffer.
2060cef893SCheng Wang     ASSERT_GE(dst.size(), n);
2160cef893SCheng Wang     // Making sure strncpy returns dst.
22*b6bc9d72SGuillaume Chatelet     ASSERT_EQ(LIBC_NAMESPACE::strncpy(dst.data(), src.data(), n), dst.data());
2360cef893SCheng Wang     // Expected must be of the same size as dst.
2460cef893SCheng Wang     ASSERT_EQ(dst.size(), expected.size());
2560cef893SCheng Wang     // Expected and dst are the same.
2660cef893SCheng Wang     for (size_t i = 0; i < expected.size(); ++i)
2760cef893SCheng Wang       ASSERT_EQ(expected[i], dst[i]);
2860cef893SCheng Wang   }
2960cef893SCheng Wang };
3060cef893SCheng Wang 
TEST_F(LlvmLibcStrncpyTest,Untouched)311df0dbfcSMichael Jones TEST_F(LlvmLibcStrncpyTest, Untouched) {
3260cef893SCheng Wang   char dst[] = {'a', 'b'};
3360cef893SCheng Wang   const char src[] = {'x', '\0'};
3460cef893SCheng Wang   const char expected[] = {'a', 'b'};
3560cef893SCheng Wang   check_strncpy(dst, src, 0, expected);
3660cef893SCheng Wang }
3760cef893SCheng Wang 
TEST_F(LlvmLibcStrncpyTest,CopyOne)381df0dbfcSMichael Jones TEST_F(LlvmLibcStrncpyTest, CopyOne) {
3960cef893SCheng Wang   char dst[] = {'a', 'b'};
4060cef893SCheng Wang   const char src[] = {'x', 'y'};
4160cef893SCheng Wang   const char expected[] = {'x', 'b'}; // no \0 is appended
4260cef893SCheng Wang   check_strncpy(dst, src, 1, expected);
4360cef893SCheng Wang }
4460cef893SCheng Wang 
TEST_F(LlvmLibcStrncpyTest,CopyNull)451df0dbfcSMichael Jones TEST_F(LlvmLibcStrncpyTest, CopyNull) {
4660cef893SCheng Wang   char dst[] = {'a', 'b'};
4760cef893SCheng Wang   const char src[] = {'\0', 'y'};
4860cef893SCheng Wang   const char expected[] = {'\0', 'b'};
4960cef893SCheng Wang   check_strncpy(dst, src, 1, expected);
5060cef893SCheng Wang }
5160cef893SCheng Wang 
TEST_F(LlvmLibcStrncpyTest,CopyPastSrc)521df0dbfcSMichael Jones TEST_F(LlvmLibcStrncpyTest, CopyPastSrc) {
5360cef893SCheng Wang   char dst[] = {'a', 'b'};
5460cef893SCheng Wang   const char src[] = {'\0', 'y'};
5560cef893SCheng Wang   const char expected[] = {'\0', '\0'};
5660cef893SCheng Wang   check_strncpy(dst, src, 2, expected);
5760cef893SCheng Wang }
58