From c3828dd35789dedb6294d477b9d3739aabe3debe Mon Sep 17 00:00:00 2001 From: apio Date: Sat, 22 Oct 2022 17:58:54 +0200 Subject: [PATCH] Tests: add tests for strchr() and strrchr() --- tests/libc/Test.c | 4 ++++ tests/libc/string.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/tests/libc/Test.c b/tests/libc/Test.c index 8b808383..df274377 100644 --- a/tests/libc/Test.c +++ b/tests/libc/Test.c @@ -4,6 +4,8 @@ DEFINE_TEST(strlen); DEFINE_TEST(strnlen); DEFINE_TEST(strcspn); DEFINE_TEST(strspn); +DEFINE_TEST(strchr); +DEFINE_TEST(strrchr); DEFINE_TEST(atoi); DEFINE_TEST(atol); @@ -17,6 +19,8 @@ int main() RUN_TEST(strnlen); RUN_TEST(strcspn); RUN_TEST(strspn); + RUN_TEST(strchr); + RUN_TEST(strrchr); START_TEST_CASE(stdlib.h); RUN_TEST(atoi); diff --git a/tests/libc/string.c b/tests/libc/string.c index d3c75626..b6e52258 100644 --- a/tests/libc/string.c +++ b/tests/libc/string.c @@ -83,5 +83,47 @@ DEFINE_TEST(strspn) EXPECT_EQ(len, 10); + TEST_SUCCESS(); +} + +DEFINE_TEST(strchr) +{ + START_TEST(strchr); + + const char* str = "Hello, world!"; + + char* ptr = strchr(str, 'l'); + + EXPECT_EQ(ptr, str + 2); + + ptr = strchr(str, 'u'); + + EXPECT_EQ(ptr, NULL); + + ptr = strchr(str, '!'); + + EXPECT_EQ(ptr, str + 12); + + TEST_SUCCESS(); +} + +DEFINE_TEST(strrchr) +{ + START_TEST(strrchr); + + const char* str = "Hello, world!"; + + char* ptr = strrchr(str, 'l'); + + EXPECT_EQ(ptr, str + 10); + + ptr = strrchr(str, 'u'); + + EXPECT_EQ(ptr, NULL); + + ptr = strrchr(str, '!'); + + EXPECT_EQ(ptr, str + 12); + TEST_SUCCESS(); } \ No newline at end of file