19b6f8b98SMichael Jones //===-- Unittests for stpncpy ---------------------------------------------===//
29b6f8b98SMichael Jones //
39b6f8b98SMichael Jones // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
49b6f8b98SMichael Jones // See https://llvm.org/LICENSE.txt for license information.
59b6f8b98SMichael Jones // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
69b6f8b98SMichael Jones //
79b6f8b98SMichael Jones //===----------------------------------------------------------------------===//
89b6f8b98SMichael Jones
9a1c42cb8SGuillaume Chatelet #include "src/__support/CPP/span.h"
109b6f8b98SMichael Jones #include "src/string/stpncpy.h"
11af1315c2SSiva Chandra Reddy #include "test/UnitTest/Test.h"
129b6f8b98SMichael Jones #include <stddef.h> // For size_t.
139b6f8b98SMichael Jones
14*b6bc9d72SGuillaume Chatelet class LlvmLibcStpncpyTest : public LIBC_NAMESPACE::testing::Test {
159b6f8b98SMichael Jones public:
check_stpncpy(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,size_t expectedCopied)16*b6bc9d72SGuillaume Chatelet void check_stpncpy(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,
199b6f8b98SMichael Jones size_t expectedCopied) {
209b6f8b98SMichael Jones // Making sure we don't overflow buffer.
219b6f8b98SMichael Jones ASSERT_GE(dst.size(), n);
229b6f8b98SMichael Jones // Making sure stpncpy returns a pointer to the end of dst.
23*b6bc9d72SGuillaume Chatelet ASSERT_EQ(LIBC_NAMESPACE::stpncpy(dst.data(), src.data(), n),
249b6f8b98SMichael Jones dst.data() + expectedCopied);
259b6f8b98SMichael Jones // Expected must be of the same size as dst.
269b6f8b98SMichael Jones ASSERT_EQ(dst.size(), expected.size());
279b6f8b98SMichael Jones // Expected and dst are the same.
289b6f8b98SMichael Jones for (size_t i = 0; i < expected.size(); ++i)
299b6f8b98SMichael Jones ASSERT_EQ(expected[i], dst[i]);
309b6f8b98SMichael Jones }
319b6f8b98SMichael Jones };
329b6f8b98SMichael Jones
TEST_F(LlvmLibcStpncpyTest,Untouched)339b6f8b98SMichael Jones TEST_F(LlvmLibcStpncpyTest, Untouched) {
349b6f8b98SMichael Jones char dst[] = {'a', 'b'};
359b6f8b98SMichael Jones const char src[] = {'x', '\0'};
369b6f8b98SMichael Jones const char expected[] = {'a', 'b'};
379b6f8b98SMichael Jones check_stpncpy(dst, src, 0, expected, 0);
389b6f8b98SMichael Jones }
399b6f8b98SMichael Jones
TEST_F(LlvmLibcStpncpyTest,CopyOne)409b6f8b98SMichael Jones TEST_F(LlvmLibcStpncpyTest, CopyOne) {
419b6f8b98SMichael Jones char dst[] = {'a', 'b'};
429b6f8b98SMichael Jones const char src[] = {'x', 'y'};
439b6f8b98SMichael Jones const char expected[] = {'x', 'b'}; // no \0 is appended
449b6f8b98SMichael Jones check_stpncpy(dst, src, 1, expected, 1);
459b6f8b98SMichael Jones }
469b6f8b98SMichael Jones
TEST_F(LlvmLibcStpncpyTest,CopyNull)479b6f8b98SMichael Jones TEST_F(LlvmLibcStpncpyTest, CopyNull) {
489b6f8b98SMichael Jones char dst[] = {'a', 'b'};
499b6f8b98SMichael Jones const char src[] = {'\0', 'y'};
509b6f8b98SMichael Jones const char expected[] = {'\0', 'b'};
519b6f8b98SMichael Jones check_stpncpy(dst, src, 1, expected, 0);
529b6f8b98SMichael Jones }
539b6f8b98SMichael Jones
TEST_F(LlvmLibcStpncpyTest,CopyPastSrc)549b6f8b98SMichael Jones TEST_F(LlvmLibcStpncpyTest, CopyPastSrc) {
559b6f8b98SMichael Jones char dst[] = {'a', 'b'};
569b6f8b98SMichael Jones const char src[] = {'\0', 'y'};
579b6f8b98SMichael Jones const char expected[] = {'\0', '\0'};
589b6f8b98SMichael Jones check_stpncpy(dst, src, 2, expected, 0);
599b6f8b98SMichael Jones }
609b6f8b98SMichael Jones
TEST_F(LlvmLibcStpncpyTest,CopyTwoNoNull)619b6f8b98SMichael Jones TEST_F(LlvmLibcStpncpyTest, CopyTwoNoNull) {
629b6f8b98SMichael Jones char dst[] = {'a', 'b'};
639b6f8b98SMichael Jones const char src[] = {'x', 'y'};
649b6f8b98SMichael Jones const char expected[] = {'x', 'y'};
659b6f8b98SMichael Jones check_stpncpy(dst, src, 2, expected, 2);
669b6f8b98SMichael Jones }
679b6f8b98SMichael Jones
TEST_F(LlvmLibcStpncpyTest,CopyTwoWithNull)689b6f8b98SMichael Jones TEST_F(LlvmLibcStpncpyTest, CopyTwoWithNull) {
699b6f8b98SMichael Jones char dst[] = {'a', 'b'};
709b6f8b98SMichael Jones const char src[] = {'x', '\0'};
719b6f8b98SMichael Jones const char expected[] = {'x', '\0'};
729b6f8b98SMichael Jones check_stpncpy(dst, src, 2, expected, 1);
739b6f8b98SMichael Jones }
74