#include "src/__support/CPP/ArrayRef.h"
#include "src/string/strncpy.h"
#include "utils/UnitTest/Test.h"
#include <stddef.h>
class LlvmLibcStrncpyTest : public __llvm_libc::testing::Test {
public:
void check_strncpy(__llvm_libc::cpp::MutableArrayRef<char> dst,
const __llvm_libc::cpp::ArrayRef<char> src, size_t n,
const __llvm_libc::cpp::ArrayRef<char> expected) {
ASSERT_GE(dst.size(), n);
ASSERT_EQ(__llvm_libc::strncpy(dst.data(), src.data(), n), dst.data());
ASSERT_EQ(dst.size(), expected.size());
for (size_t i = 0; i < expected.size(); ++i)
ASSERT_EQ(expected[i], dst[i]);
}
};
TEST_F(LlvmLibcStrncpyTest, Untouched) {
char dst[] = {'a', 'b'};
const char src[] = {'x', '\0'};
const char expected[] = {'a', 'b'};
check_strncpy(dst, src, 0, expected);
}
TEST_F(LlvmLibcStrncpyTest, CopyOne) {
char dst[] = {'a', 'b'};
const char src[] = {'x', 'y'};
const char expected[] = {'x', 'b'};
check_strncpy(dst, src, 1, expected);
}
TEST_F(LlvmLibcStrncpyTest, CopyNull) {
char dst[] = {'a', 'b'};
const char src[] = {'\0', 'y'};
const char expected[] = {'\0', 'b'};
check_strncpy(dst, src, 1, expected);
}
TEST_F(LlvmLibcStrncpyTest, CopyPastSrc) {
char dst[] = {'a', 'b'};
const char src[] = {'\0', 'y'};
const char expected[] = {'\0', '\0'};
check_strncpy(dst, src, 2, expected);
}