2015-05-24 07:20:26 -04:00
|
|
|
#include <check.h>
|
|
|
|
|
|
|
|
#include "xalloc.h"
|
|
|
|
|
|
|
|
Suite * xalloc_suite(void);
|
|
|
|
|
|
|
|
START_TEST(test_malloc)
|
|
|
|
{
|
|
|
|
void *p;
|
|
|
|
|
2015-05-26 02:04:04 -04:00
|
|
|
p = xmalloc(0UL);
|
2015-05-24 07:20:26 -04:00
|
|
|
ck_assert_ptr_ne(p, NULL);
|
|
|
|
xfree(p);
|
2015-05-26 02:04:04 -04:00
|
|
|
p = xmalloc(1UL);
|
2015-05-24 07:20:26 -04:00
|
|
|
ck_assert_ptr_ne(p, NULL);
|
|
|
|
xfree(p);
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
|
|
|
START_TEST(test_calloc)
|
|
|
|
{
|
|
|
|
void *p;
|
|
|
|
|
2015-05-26 02:04:04 -04:00
|
|
|
p = xcalloc(0UL, 0UL);
|
2015-05-24 07:20:26 -04:00
|
|
|
ck_assert_ptr_ne(p, NULL);
|
|
|
|
xfree(p);
|
2015-05-26 02:04:04 -04:00
|
|
|
p = xcalloc(1UL, 0UL);
|
2015-05-24 07:20:26 -04:00
|
|
|
ck_assert_ptr_ne(p, NULL);
|
|
|
|
xfree(p);
|
2015-05-26 02:04:04 -04:00
|
|
|
p = xcalloc(0UL, 1UL);
|
2015-05-24 07:20:26 -04:00
|
|
|
ck_assert_ptr_ne(p, NULL);
|
|
|
|
xfree(p);
|
2015-05-26 02:04:04 -04:00
|
|
|
p = xcalloc(1UL, 1UL);
|
2015-05-24 07:20:26 -04:00
|
|
|
ck_assert_ptr_ne(p, NULL);
|
|
|
|
xfree(p);
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
|
|
|
START_TEST(test_reallocarray)
|
|
|
|
{
|
|
|
|
void *p;
|
|
|
|
|
2015-05-26 02:04:04 -04:00
|
|
|
p = xreallocarray(NULL, 0UL, 0UL);
|
2015-05-24 07:20:26 -04:00
|
|
|
ck_assert_ptr_ne(p, NULL);
|
|
|
|
xfree(p);
|
2015-05-26 02:04:04 -04:00
|
|
|
p = xreallocarray(NULL, 1UL, 0UL);
|
2015-05-24 07:20:26 -04:00
|
|
|
ck_assert_ptr_ne(p, NULL);
|
|
|
|
xfree(p);
|
2015-05-26 02:04:04 -04:00
|
|
|
p = xreallocarray(NULL, 0UL, 1UL);
|
2015-05-24 07:20:26 -04:00
|
|
|
ck_assert_ptr_ne(p, NULL);
|
|
|
|
xfree(p);
|
2015-05-26 02:04:04 -04:00
|
|
|
p = xreallocarray(NULL, 1UL, 1UL);
|
2015-05-24 07:20:26 -04:00
|
|
|
ck_assert_ptr_ne(p, NULL);
|
2015-05-26 02:04:04 -04:00
|
|
|
p = xreallocarray(p, 2UL, 2UL);
|
2015-05-24 07:20:26 -04:00
|
|
|
ck_assert_ptr_ne(p, NULL);
|
2015-05-26 02:04:04 -04:00
|
|
|
p = xreallocarray(p, 1UL, 1UL);
|
2015-05-24 07:20:26 -04:00
|
|
|
ck_assert_ptr_ne(p, NULL);
|
|
|
|
xfree(p);
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
|
|
|
START_TEST(test_strdup)
|
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
s = xstrdup("test");
|
|
|
|
ck_assert_str_eq(s, "test");
|
|
|
|
xfree(s);
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
|
|
|
Suite *
|
|
|
|
xalloc_suite(void)
|
|
|
|
{
|
|
|
|
Suite *s;
|
|
|
|
TCase *tc_xalloc;
|
|
|
|
|
|
|
|
s = suite_create("Xalloc");
|
|
|
|
|
|
|
|
tc_xalloc = tcase_create("Xalloc");
|
|
|
|
tcase_add_test(tc_xalloc, test_malloc);
|
|
|
|
tcase_add_test(tc_xalloc, test_calloc);
|
|
|
|
tcase_add_test(tc_xalloc, test_reallocarray);
|
|
|
|
tcase_add_test(tc_xalloc, test_strdup);
|
|
|
|
suite_add_tcase(s, tc_xalloc);
|
|
|
|
|
|
|
|
return (s);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(void)
|
|
|
|
{
|
2017-10-04 09:23:21 -04:00
|
|
|
int num_failed;
|
|
|
|
Suite *s;
|
|
|
|
SRunner *sr;
|
2015-05-24 07:20:26 -04:00
|
|
|
|
|
|
|
s = xalloc_suite();
|
|
|
|
sr = srunner_create(s);
|
|
|
|
|
|
|
|
srunner_run_all(sr, CK_NORMAL);
|
|
|
|
num_failed = srunner_ntests_failed(sr);
|
|
|
|
srunner_free(sr);
|
|
|
|
|
|
|
|
if (num_failed)
|
|
|
|
return (1);
|
|
|
|
return (0);
|
|
|
|
}
|