From bfbe8e847b945ea62c350bcf729c36369c55aae3 Mon Sep 17 00:00:00 2001 From: apio Date: Sat, 22 Oct 2022 18:33:28 +0200 Subject: [PATCH] Tests: Add tests for memset, memcpy and memchr --- tests/libc/Test.c | 8 ++++++ tests/libc/string.c | 66 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/tests/libc/Test.c b/tests/libc/Test.c index ae79c7cf..f37459b8 100644 --- a/tests/libc/Test.c +++ b/tests/libc/Test.c @@ -1,5 +1,9 @@ #include "Test.h" +// string.h +DEFINE_TEST(memset); +DEFINE_TEST(memcpy); +DEFINE_TEST(memchr); DEFINE_TEST(strlen); DEFINE_TEST(strnlen); DEFINE_TEST(strcspn); @@ -8,6 +12,7 @@ DEFINE_TEST(strchr); DEFINE_TEST(strrchr); DEFINE_TEST(strpbrk); +// stdlib.h DEFINE_TEST(atoi); DEFINE_TEST(atol); DEFINE_TEST(atoll); @@ -18,6 +23,9 @@ DEFINE_TEST(calloc); int main() { START_TEST_CASE(string.h); + RUN_TEST(memset); + RUN_TEST(memcpy); + RUN_TEST(memchr); RUN_TEST(strlen); RUN_TEST(strnlen); RUN_TEST(strcspn); diff --git a/tests/libc/string.c b/tests/libc/string.c index f885e5b1..542b401a 100644 --- a/tests/libc/string.c +++ b/tests/libc/string.c @@ -1,6 +1,72 @@ #include "Test.h" #include +DEFINE_TEST(memset) +{ + START_TEST(memset); + + char test[10]; + char* ptr = memset(test, 0, 10); + + EXPECT_EQ(ptr, test); + + for (int i = 0; i < 10; i++) { EXPECT_EQ(test[i], 0); } + + ptr = memset(test, 42, 10); + + EXPECT_EQ(ptr, test); + + for (int i = 0; i < 10; i++) { EXPECT_EQ(test[i], 42); } + + TEST_SUCCESS(); +} + +DEFINE_TEST(memcpy) +{ + START_TEST(memcpy); + + char buf[20] = "Nothing is going on"; + const char* str = "Something is going!"; + + char* ptr = memcpy(buf, str, 20); + + EXPECT_EQ(ptr, buf); + + for (int i = 0; i < 20; i++) { EXPECT_EQ(buf[i], str[i]); } + + const char* new = "Well..."; + + ptr = memcpy(buf, new, 7); + + EXPECT_EQ(ptr, buf); + + for (int i = 0; i < 7; i++) { EXPECT_EQ(buf[i], new[i]); } + for (int i = 7; i < 20; i++) { EXPECT_EQ(buf[i], str[i]); } + + TEST_SUCCESS(); +} + +DEFINE_TEST(memchr) +{ + START_TEST(memchr); + + char buf[20] = "abcdefghijklmnopqrs"; + + char* ptr = memchr(buf, 'z', 20); + + EXPECT_EQ(ptr, NULL); + + ptr = memchr(buf, 'd', 20); + + EXPECT_EQ(ptr, buf + 3); + + ptr = memchr(buf, 's', 20); + + EXPECT_EQ(ptr, buf + 18); + + TEST_SUCCESS(); +} + DEFINE_TEST(strlen) { START_TEST(strlen);