From ea96c5f47bcd5de2723270eacc12268395406bb8 Mon Sep 17 00:00:00 2001 From: apio Date: Sat, 22 Oct 2022 18:06:01 +0200 Subject: [PATCH] Tests: Add tests for calloc() and realloc() --- tests/Test.h | 6 ++++++ tests/libc/Test.c | 4 ++++ tests/libc/stdlib.c | 49 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+) diff --git a/tests/Test.h b/tests/Test.h index 5d69714e..4836e95f 100644 --- a/tests/Test.h +++ b/tests/Test.h @@ -20,6 +20,12 @@ int printf(const char*, ...); return false; \ } while (0) +#define TEST_NOT_SURE(expr) \ + do { \ + printf("not sure (%s)\n", #expr); \ + return true; \ + } while (0) + #define EXPECT(expr) \ do { \ if (!(expr)) { TEST_FAIL(expr); } \ diff --git a/tests/libc/Test.c b/tests/libc/Test.c index df274377..5019ecf8 100644 --- a/tests/libc/Test.c +++ b/tests/libc/Test.c @@ -11,6 +11,8 @@ DEFINE_TEST(atoi); DEFINE_TEST(atol); DEFINE_TEST(atoll); DEFINE_TEST(srand); +DEFINE_TEST(malloc); +DEFINE_TEST(calloc); int main() { @@ -27,4 +29,6 @@ int main() RUN_TEST(atol); RUN_TEST(atoll); RUN_TEST(srand); + RUN_TEST(malloc); + RUN_TEST(calloc); } \ No newline at end of file diff --git a/tests/libc/stdlib.c b/tests/libc/stdlib.c index 0900b1f3..db5a56ce 100644 --- a/tests/libc/stdlib.c +++ b/tests/libc/stdlib.c @@ -87,5 +87,54 @@ DEFINE_TEST(srand) EXPECT_EQ(val, -1731894882); + TEST_SUCCESS(); +} + +DEFINE_TEST(malloc) +{ + START_TEST(malloc); + + int* ptr = malloc(6 * sizeof(int)); + + if (!ptr) + { + TEST_NOT_SURE(ptr); + return true; + } + + *ptr = 6; + + EXPECT_EQ(*ptr, 6); + + ptr[5] = 4; + + EXPECT_EQ(ptr[5], 4); + + free(ptr); + + TEST_SUCCESS(); +} + +DEFINE_TEST(calloc) +{ + START_TEST(calloc); + + int* ptr = calloc(6, sizeof(int)); + + if (!ptr) { TEST_NOT_SURE(ptr); } + + EXPECT_EQ(*ptr, 0); + EXPECT_EQ(ptr[5], 0); + + *ptr = 6; + + EXPECT_EQ(*ptr, 6); + + ptr[5] = 4; + + EXPECT_EQ(ptr[5], 4); + + free(ptr); + TEST_SUCCESS(); } \ No newline at end of file