74 changes: 3 additions & 71 deletions libc/test/src/string/strchr_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,77 +6,9 @@
//
//===----------------------------------------------------------------------===//

#include "StrchrTest.h"

#include "src/string/strchr.h"
#include "test/UnitTest/Test.h"

TEST(LlvmLibcStrChrTest, FindsFirstCharacter) {
const char *src = "abcde";

// Should return original string since 'a' is the first character.
ASSERT_STREQ(__llvm_libc::strchr(src, 'a'), "abcde");
// Source string should not change.
ASSERT_STREQ(src, "abcde");
}

TEST(LlvmLibcStrChrTest, FindsMiddleCharacter) {
const char *src = "abcde";

// Should return characters after (and including) 'c'.
ASSERT_STREQ(__llvm_libc::strchr(src, 'c'), "cde");
// Source string should not change.
ASSERT_STREQ(src, "abcde");
}

TEST(LlvmLibcStrChrTest, FindsLastCharacterThatIsNotNullTerminator) {
const char *src = "abcde";

// Should return 'e' and null-terminator.
ASSERT_STREQ(__llvm_libc::strchr(src, 'e'), "e");
// Source string should not change.
ASSERT_STREQ(src, "abcde");
}

TEST(LlvmLibcStrChrTest, FindsNullTerminator) {
const char *src = "abcde";

// Should return null terminator.
ASSERT_STREQ(__llvm_libc::strchr(src, '\0'), "");
// Source string should not change.
ASSERT_STREQ(src, "abcde");
}

TEST(LlvmLibcStrChrTest, CharacterNotWithinStringShouldReturnNullptr) {
// Since 'z' is not within the string, should return nullptr.
ASSERT_STREQ(__llvm_libc::strchr("123?", 'z'), nullptr);
}

TEST(LlvmLibcStrChrTest, TheSourceShouldNotChange) {
const char *src = "abcde";
// When the character is found, the source string should not change.
__llvm_libc::strchr(src, 'd');
ASSERT_STREQ(src, "abcde");
// Same case for when the character is not found.
__llvm_libc::strchr(src, 'z');
ASSERT_STREQ(src, "abcde");
// Same case for when looking for null terminator.
__llvm_libc::strchr(src, '\0');
ASSERT_STREQ(src, "abcde");
}

TEST(LlvmLibcStrChrTest, ShouldFindFirstOfDuplicates) {
// '1' is duplicated in the string, but it should find the first copy.
ASSERT_STREQ(__llvm_libc::strchr("abc1def1ghi", '1'), "1def1ghi");

const char *dups = "XXXXX";
// Should return original string since 'X' is the first character.
ASSERT_STREQ(__llvm_libc::strchr(dups, 'X'), dups);
}

TEST(LlvmLibcStrChrTest, EmptyStringShouldOnlyMatchNullTerminator) {
// Null terminator should match.
ASSERT_STREQ(__llvm_libc::strchr("", '\0'), "");
// All other characters should not match.
ASSERT_STREQ(__llvm_libc::strchr("", 'Z'), nullptr);
ASSERT_STREQ(__llvm_libc::strchr("", '3'), nullptr);
ASSERT_STREQ(__llvm_libc::strchr("", '*'), nullptr);
}
STRCHR_TEST(Strchr, __llvm_libc::strchr)
72 changes: 3 additions & 69 deletions libc/test/src/string/strrchr_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,75 +6,9 @@
//
//===----------------------------------------------------------------------===//

#include "StrchrTest.h"

#include "src/string/strrchr.h"
#include "test/UnitTest/Test.h"

TEST(LlvmLibcStrRChrTest, FindsFirstCharacter) {
const char *src = "abcde";

// Should return original string since 'a' is the first character.
ASSERT_STREQ(__llvm_libc::strrchr(src, 'a'), "abcde");
// Source string should not change.
ASSERT_STREQ(src, "abcde");
}

TEST(LlvmLibcStrRChrTest, FindsMiddleCharacter) {
const char *src = "abcde";

// Should return characters after (and including) 'c'.
ASSERT_STREQ(__llvm_libc::strrchr(src, 'c'), "cde");
// Source string should not change.
ASSERT_STREQ(src, "abcde");
}

TEST(LlvmLibcStrRChrTest, FindsLastCharacterThatIsNotNullTerminator) {
const char *src = "abcde";

// Should return 'e' and null-terminator.
ASSERT_STREQ(__llvm_libc::strrchr(src, 'e'), "e");
// Source string should not change.
ASSERT_STREQ(src, "abcde");
}

TEST(LlvmLibcStrRChrTest, FindsNullTerminator) {
const char *src = "abcde";

// Should return null terminator.
ASSERT_STREQ(__llvm_libc::strrchr(src, '\0'), "");
// Source string should not change.
ASSERT_STREQ(src, "abcde");
}

TEST(LlvmLibcStrRChrTest, FindsLastBehindFirstNullTerminator) {
const char src[6] = {'a', 'a', '\0', 'b', '\0', 'c'};
// 'b' is behind a null terminator, so should not be found.
ASSERT_STREQ(__llvm_libc::strrchr(src, 'b'), nullptr);
// Same goes for 'c'.
ASSERT_STREQ(__llvm_libc::strrchr(src, 'c'), nullptr);

// Should find the second of the two a's.
ASSERT_STREQ(__llvm_libc::strrchr(src, 'a'), "a");
}

TEST(LlvmLibcStrRChrTest, CharacterNotWithinStringShouldReturnNullptr) {
// Since 'z' is not within the string, should return nullptr.
ASSERT_STREQ(__llvm_libc::strrchr("123?", 'z'), nullptr);
}

TEST(LlvmLibcStrRChrTest, ShouldFindLastOfDuplicates) {
// '1' is duplicated in the string, but it should find the last copy.
ASSERT_STREQ(__llvm_libc::strrchr("abc1def1ghi", '1'), "1ghi");

const char *dups = "XXXXX";
// Should return the last occurrence of 'X'.
ASSERT_STREQ(__llvm_libc::strrchr(dups, 'X'), "X");
}

TEST(LlvmLibcStrRChrTest, EmptyStringShouldOnlyMatchNullTerminator) {
// Null terminator should match.
ASSERT_STREQ(__llvm_libc::strrchr("", '\0'), "");
// All other characters should not match.
ASSERT_STREQ(__llvm_libc::strrchr("", 'A'), nullptr);
ASSERT_STREQ(__llvm_libc::strrchr("", '2'), nullptr);
ASSERT_STREQ(__llvm_libc::strrchr("", '*'), nullptr);
}
STRRCHR_TEST(Strrchr, __llvm_libc::strrchr)
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,19 @@ libc_test(
],
)

cc_library(
name = "strchr_test_helper",
hdrs = ["StrchrTest.h"],
deps = ["//libc/test/UnitTest:LibcUnitTest"],
)

libc_test(
name = "strchr_test",
srcs = ["strchr_test.cpp"],
libc_function_deps = [
"//libc:strchr",
],
deps = [":strchr_test_helper"],
)

libc_test(
Expand Down Expand Up @@ -80,6 +87,7 @@ libc_test(
libc_function_deps = [
"//libc:strrchr",
],
deps = [":strchr_test_helper"],
)

libc_test(
Expand Down