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 960cef893SCheng Wang #include "src/string/strncpy.h" 1060cef893SCheng Wang #include "utils/CPP/ArrayRef.h" 1160cef893SCheng Wang #include "utils/UnitTest/Test.h" 1260cef893SCheng Wang #include <stddef.h> // For size_t. 1360cef893SCheng Wang 14*1df0dbfcSMichael Jones class LlvmLibcStrncpyTest : public __llvm_libc::testing::Test { 1560cef893SCheng Wang public: 1660cef893SCheng Wang void check_strncpy(__llvm_libc::cpp::MutableArrayRef<char> dst, 1760cef893SCheng Wang const __llvm_libc::cpp::ArrayRef<char> src, size_t n, 1860cef893SCheng Wang const __llvm_libc::cpp::ArrayRef<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. 2260cef893SCheng Wang ASSERT_EQ(__llvm_libc::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 31*1df0dbfcSMichael 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 38*1df0dbfcSMichael 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 45*1df0dbfcSMichael 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 52*1df0dbfcSMichael 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