aboutsummaryrefslogtreecommitdiff
path: root/libc/test
diff options
context:
space:
mode:
authorCheng Wang <chennngwang@gmail.com>2020-11-12 11:42:04 +0800
committerCheng Wang <chennngwang@gmail.com>2020-12-02 20:45:51 +0800
commit60cef893627be169f22dc540834c4d085847d94a (patch)
tree2f9a83d088225383e06444895ac7c084a4b6c477 /libc/test
parentd055e3a0eb4e957159b075c0937a960beb75c975 (diff)
downloadllvm-project-60cef893627be169f22dc540834c4d085847d94a.tar.gz
[libc] Add strncpy implementation.
Add libc strncpy implementation. Reviewed By: sivachandra, gchatelet Differential Revision: https://reviews.llvm.org/D91399
Diffstat (limited to 'libc/test')
-rw-r--r--libc/test/src/string/CMakeLists.txt10
-rw-r--r--libc/test/src/string/strncpy_test.cpp57
2 files changed, 67 insertions, 0 deletions
diff --git a/libc/test/src/string/CMakeLists.txt b/libc/test/src/string/CMakeLists.txt
index df824cd18ecc..8b78fcfdd020 100644
--- a/libc/test/src/string/CMakeLists.txt
+++ b/libc/test/src/string/CMakeLists.txt
@@ -73,6 +73,16 @@ add_libc_unittest(
)
add_libc_unittest(
+ strncpy_test
+ SUITE
+ libc_string_unittests
+ SRCS
+ strncpy_test.cpp
+ DEPENDS
+ libc.src.string.strncpy
+)
+
+add_libc_unittest(
strnlen_test
SUITE
libc_string_unittests
diff --git a/libc/test/src/string/strncpy_test.cpp b/libc/test/src/string/strncpy_test.cpp
new file mode 100644
index 000000000000..814870613251
--- /dev/null
+++ b/libc/test/src/string/strncpy_test.cpp
@@ -0,0 +1,57 @@
+//===-- Unittests for strncpy ---------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/string/strncpy.h"
+#include "utils/CPP/ArrayRef.h"
+#include "utils/UnitTest/Test.h"
+#include <stddef.h> // For size_t.
+
+class StrncpyTest : 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) {
+ // Making sure we don't overflow buffer.
+ ASSERT_GE(dst.size(), n);
+ // Making sure strncpy returns dst.
+ ASSERT_EQ(__llvm_libc::strncpy(dst.data(), src.data(), n), dst.data());
+ // Expected must be of the same size as dst.
+ ASSERT_EQ(dst.size(), expected.size());
+ // Expected and dst are the same.
+ for (size_t i = 0; i < expected.size(); ++i)
+ ASSERT_EQ(expected[i], dst[i]);
+ }
+};
+
+TEST_F(StrncpyTest, Untouched) {
+ char dst[] = {'a', 'b'};
+ const char src[] = {'x', '\0'};
+ const char expected[] = {'a', 'b'};
+ check_strncpy(dst, src, 0, expected);
+}
+
+TEST_F(StrncpyTest, CopyOne) {
+ char dst[] = {'a', 'b'};
+ const char src[] = {'x', 'y'};
+ const char expected[] = {'x', 'b'}; // no \0 is appended
+ check_strncpy(dst, src, 1, expected);
+}
+
+TEST_F(StrncpyTest, CopyNull) {
+ char dst[] = {'a', 'b'};
+ const char src[] = {'\0', 'y'};
+ const char expected[] = {'\0', 'b'};
+ check_strncpy(dst, src, 1, expected);
+}
+
+TEST_F(StrncpyTest, CopyPastSrc) {
+ char dst[] = {'a', 'b'};
+ const char src[] = {'\0', 'y'};
+ const char expected[] = {'\0', '\0'};
+ check_strncpy(dst, src, 2, expected);
+}