Tests: Add tests for calloc() and realloc()

This commit is contained in:
apio 2022-10-22 18:06:01 +02:00
parent c3828dd357
commit ea96c5f47b
3 changed files with 59 additions and 0 deletions

View File

@ -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); } \

View File

@ -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);
}

View File

@ -89,3 +89,52 @@ DEFINE_TEST(srand)
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();
}