aboutsummaryrefslogtreecommitdiff
path: root/libc/test
diff options
context:
space:
mode:
authorcgyurgyik <gyurgyikcp@gmail.com>2020-08-07 16:13:48 -0400
committercgyurgyik <gyurgyikcp@gmail.com>2020-08-07 16:14:32 -0400
commitdc13a9a7813768e01bddd03924d6cac6fa45cd7b (patch)
tree90b8b52c0d69a2777c882f0b049912fbbac0511c /libc/test
parentcc01194c2fac5ad400b862463c473f3ef924c932 (diff)
downloadllvm-project-dc13a9a7813768e01bddd03924d6cac6fa45cd7b.tar.gz
[libc] Add strcpsn and strpbrk implementation.
Reviewed By: sivachandra Differential Revision: https://reviews.llvm.org/D85386
Diffstat (limited to 'libc/test')
-rw-r--r--libc/test/src/string/CMakeLists.txt19
-rw-r--r--libc/test/src/string/strcspn_test.cpp50
-rw-r--r--libc/test/src/string/strpbrk_test.cpp62
3 files changed, 131 insertions, 0 deletions
diff --git a/libc/test/src/string/CMakeLists.txt b/libc/test/src/string/CMakeLists.txt
index e1db8d67becd..f7c580dd1876 100644
--- a/libc/test/src/string/CMakeLists.txt
+++ b/libc/test/src/string/CMakeLists.txt
@@ -103,6 +103,16 @@ add_libc_unittest(
)
add_libc_unittest(
+ strcspn_test
+ SUITE
+ libc_string_unittests
+ SRCS
+ strcspn_test.cpp
+ DEPENDS
+ libc.src.string.strcspn
+)
+
+add_libc_unittest(
strspn_test
SUITE
libc_string_unittests
@@ -112,6 +122,15 @@ add_libc_unittest(
libc.src.string.strspn
)
+add_libc_unittest(
+ strpbrk_test
+ SUITE
+ libc_string_unittests
+ SRCS
+ strpbrk_test.cpp
+ DEPENDS
+ libc.src.string.strpbrk
+)
# Tests all implementations that can run on the host.
function(add_libc_multi_impl_test name)
diff --git a/libc/test/src/string/strcspn_test.cpp b/libc/test/src/string/strcspn_test.cpp
new file mode 100644
index 000000000000..80de495f9623
--- /dev/null
+++ b/libc/test/src/string/strcspn_test.cpp
@@ -0,0 +1,50 @@
+//===-- Unittests for strcspn ---------------------------------------------===//
+//
+// 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/strcspn.h"
+
+#include "utils/UnitTest/Test.h"
+
+TEST(StrCSpnTest, ComplementarySpanShouldNotGoPastNullTerminator) {
+ const char src[5] = {'a', 'b', '\0', 'c', 'd'};
+ EXPECT_EQ(__llvm_libc::strcspn(src, "b"), size_t{1});
+ EXPECT_EQ(__llvm_libc::strcspn(src, "d"), size_t{2});
+
+ // Same goes for the segment to be searched for.
+ const char segment[5] = {'1', '2', '\0', '3', '4'};
+ EXPECT_EQ(__llvm_libc::strcspn("123", segment), size_t{0});
+}
+
+TEST(StrCSpnTest, ComplementarySpanForEachIndividualCharacter) {
+ const char *src = "12345";
+ // The complementary span size should increment accordingly.
+ EXPECT_EQ(__llvm_libc::strcspn(src, "1"), size_t{0});
+ EXPECT_EQ(__llvm_libc::strcspn(src, "2"), size_t{1});
+ EXPECT_EQ(__llvm_libc::strcspn(src, "3"), size_t{2});
+ EXPECT_EQ(__llvm_libc::strcspn(src, "4"), size_t{3});
+ EXPECT_EQ(__llvm_libc::strcspn(src, "5"), size_t{4});
+}
+
+TEST(StrCSpnTest, ComplementarySpanIsStringLengthIfNoCharacterFound) {
+ // Null terminator.
+ EXPECT_EQ(__llvm_libc::strcspn("", ""), size_t{0});
+ EXPECT_EQ(__llvm_libc::strcspn("", "_"), size_t{0});
+ // Single character.
+ EXPECT_EQ(__llvm_libc::strcspn("a", "b"), size_t{1});
+ // Multiple characters.
+ EXPECT_EQ(__llvm_libc::strcspn("abc", "1"), size_t{3});
+}
+
+TEST(StrCSpnTest, DuplicatedCharactersNotPartOfComplementarySpan) {
+ // Complementary span should be zero in all these cases.
+ EXPECT_EQ(__llvm_libc::strcspn("a", "aa"), size_t{0});
+ EXPECT_EQ(__llvm_libc::strcspn("aa", "a"), size_t{0});
+ EXPECT_EQ(__llvm_libc::strcspn("aaa", "aa"), size_t{0});
+ EXPECT_EQ(__llvm_libc::strcspn("aaaa", "aa"), size_t{0});
+ EXPECT_EQ(__llvm_libc::strcspn("aaaa", "baa"), size_t{0});
+}
diff --git a/libc/test/src/string/strpbrk_test.cpp b/libc/test/src/string/strpbrk_test.cpp
new file mode 100644
index 000000000000..498e8473a890
--- /dev/null
+++ b/libc/test/src/string/strpbrk_test.cpp
@@ -0,0 +1,62 @@
+//===-- Unittests for strpbrk ---------------------------------------------===//
+//
+// 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/strpbrk.h"
+
+#include "utils/UnitTest/Test.h"
+
+TEST(StrPBrkTest, EmptyStringShouldReturnNullptr) {
+ // The search should not include the null terminator.
+ EXPECT_STREQ(__llvm_libc::strpbrk("", ""), nullptr);
+ EXPECT_STREQ(__llvm_libc::strpbrk("_", ""), nullptr);
+ EXPECT_STREQ(__llvm_libc::strpbrk("", "_"), nullptr);
+}
+
+TEST(StrPBrkTest, ShouldNotFindAnythingAfterNullTerminator) {
+ const char src[4] = {'a', 'b', '\0', 'c'};
+ EXPECT_STREQ(__llvm_libc::strpbrk(src, "c"), nullptr);
+}
+
+TEST(StrPBrkTest, ShouldReturnNullptrIfNoCharactersFound) {
+ EXPECT_STREQ(__llvm_libc::strpbrk("12345", "abcdef"), nullptr);
+}
+
+TEST(StrPBrkTest, FindsFirstCharacter) {
+ const char *src = "12345";
+ EXPECT_STREQ(__llvm_libc::strpbrk(src, "1"), "12345");
+ EXPECT_STREQ(__llvm_libc::strpbrk(src, "-1"), "12345");
+ EXPECT_STREQ(__llvm_libc::strpbrk(src, "1_"), "12345");
+ EXPECT_STREQ(__llvm_libc::strpbrk(src, "f1_"), "12345");
+ ASSERT_STREQ(src, "12345");
+}
+
+TEST(StrPBrkTest, FindsMiddleCharacter) {
+ const char *src = "12345";
+ EXPECT_STREQ(__llvm_libc::strpbrk(src, "3"), "345");
+ EXPECT_STREQ(__llvm_libc::strpbrk(src, "?3"), "345");
+ EXPECT_STREQ(__llvm_libc::strpbrk(src, "3F"), "345");
+ EXPECT_STREQ(__llvm_libc::strpbrk(src, "z3_"), "345");
+ ASSERT_STREQ(src, "12345");
+}
+
+TEST(StrPBrkTest, FindsLastCharacter) {
+ const char *src = "12345";
+ EXPECT_STREQ(__llvm_libc::strpbrk(src, "5"), "5");
+ EXPECT_STREQ(__llvm_libc::strpbrk(src, "r5"), "5");
+ EXPECT_STREQ(__llvm_libc::strpbrk(src, "59"), "5");
+ EXPECT_STREQ(__llvm_libc::strpbrk(src, "n5_"), "5");
+ ASSERT_STREQ(src, "12345");
+}
+
+TEST(StrPBrkTest, FindsFirstOfRepeated) {
+ EXPECT_STREQ(__llvm_libc::strpbrk("A,B,C,D", ","), ",B,C,D");
+}
+
+TEST(StrPBrkTest, FindsFirstInBreakset) {
+ EXPECT_STREQ(__llvm_libc::strpbrk("12345", "34"), "345");
+}