#include "src/__support/CPP/span.h"
#include "src/string/stpncpy.h"
#include "test/UnitTest/Test.h"
#include <stddef.h>
class LlvmLibcStpncpyTest : public LIBC_NAMESPACE::testing::Test {
public:
void 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) {
ASSERT_GE(dst.size(), n);
ASSERT_EQ(LIBC_NAMESPACE::stpncpy(dst.data(), src.data(), n),
dst.data() + expectedCopied);
ASSERT_EQ(dst.size(), expected.size());
for (size_t i = 0; i < expected.size(); ++i)
ASSERT_EQ(expected[i], dst[i]);
}
};
TEST_F(LlvmLibcStpncpyTest, Untouched) {
char dst[] = {'a', 'b'};
const char src[] = {'x', '\0'};
const char expected[] = {'a', 'b'};
check_stpncpy(dst, src, 0, expected, 0);
}
TEST_F(LlvmLibcStpncpyTest, CopyOne) {
char dst[] = {'a', 'b'};
const char src[] = {'x', 'y'};
const char expected[] = {'x', 'b'};
check_stpncpy(dst, src, 1, expected, 1);
}
TEST_F(LlvmLibcStpncpyTest, CopyNull) {
char dst[] = {'a', 'b'};
const char src[] = {'\0', 'y'};
const char expected[] = {'\0', 'b'};
check_stpncpy(dst, src, 1, expected, 0);
}
TEST_F(LlvmLibcStpncpyTest, CopyPastSrc) {
char dst[] = {'a', 'b'};
const char src[] = {'\0', 'y'};
const char expected[] = {'\0', '\0'};
check_stpncpy(dst, src, 2, expected, 0);
}
TEST_F(LlvmLibcStpncpyTest, CopyTwoNoNull) {
char dst[] = {'a', 'b'};
const char src[] = {'x', 'y'};
const char expected[] = {'x', 'y'};
check_stpncpy(dst, src, 2, expected, 2);
}
TEST_F(LlvmLibcStpncpyTest, CopyTwoWithNull) {
char dst[] = {'a', 'b'};
const char src[] = {'x', '\0'};
const char expected[] = {'x', '\0'};
check_stpncpy(dst, src, 2, expected, 1);
}