Tests: Add tests for calloc() and realloc()
This commit is contained in:
parent
c3828dd357
commit
ea96c5f47b
@ -20,6 +20,12 @@ int printf(const char*, ...);
|
|||||||
return false; \
|
return false; \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
|
#define TEST_NOT_SURE(expr) \
|
||||||
|
do { \
|
||||||
|
printf("not sure (%s)\n", #expr); \
|
||||||
|
return true; \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
#define EXPECT(expr) \
|
#define EXPECT(expr) \
|
||||||
do { \
|
do { \
|
||||||
if (!(expr)) { TEST_FAIL(expr); } \
|
if (!(expr)) { TEST_FAIL(expr); } \
|
||||||
|
@ -11,6 +11,8 @@ DEFINE_TEST(atoi);
|
|||||||
DEFINE_TEST(atol);
|
DEFINE_TEST(atol);
|
||||||
DEFINE_TEST(atoll);
|
DEFINE_TEST(atoll);
|
||||||
DEFINE_TEST(srand);
|
DEFINE_TEST(srand);
|
||||||
|
DEFINE_TEST(malloc);
|
||||||
|
DEFINE_TEST(calloc);
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
@ -27,4 +29,6 @@ int main()
|
|||||||
RUN_TEST(atol);
|
RUN_TEST(atol);
|
||||||
RUN_TEST(atoll);
|
RUN_TEST(atoll);
|
||||||
RUN_TEST(srand);
|
RUN_TEST(srand);
|
||||||
|
RUN_TEST(malloc);
|
||||||
|
RUN_TEST(calloc);
|
||||||
}
|
}
|
@ -89,3 +89,52 @@ DEFINE_TEST(srand)
|
|||||||
|
|
||||||
TEST_SUCCESS();
|
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();
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user