| 1 | #include <check.h> |
| 2 | |
| 3 | #include "xalloc.h" |
| 4 | |
| 5 | Suite * xalloc_suite(void); |
| 6 | |
| 7 | START_TEST(test_malloc) |
| 8 | { |
| 9 | void *p; |
| 10 | |
| 11 | p = xmalloc(0UL); |
| 12 | ck_assert_ptr_ne(p, NULL); |
| 13 | xfree(p); |
| 14 | p = xmalloc(1UL); |
| 15 | ck_assert_ptr_ne(p, NULL); |
| 16 | xfree(p); |
| 17 | } |
| 18 | END_TEST |
| 19 | |
| 20 | START_TEST(test_calloc) |
| 21 | { |
| 22 | void *p; |
| 23 | |
| 24 | p = xcalloc(0UL, 0UL); |
| 25 | ck_assert_ptr_ne(p, NULL); |
| 26 | xfree(p); |
| 27 | p = xcalloc(1UL, 0UL); |
| 28 | ck_assert_ptr_ne(p, NULL); |
| 29 | xfree(p); |
| 30 | p = xcalloc(0UL, 1UL); |
| 31 | ck_assert_ptr_ne(p, NULL); |
| 32 | xfree(p); |
| 33 | p = xcalloc(1UL, 1UL); |
| 34 | ck_assert_ptr_ne(p, NULL); |
| 35 | xfree(p); |
| 36 | } |
| 37 | END_TEST |
| 38 | |
| 39 | START_TEST(test_reallocarray) |
| 40 | { |
| 41 | void *p; |
| 42 | |
| 43 | p = xreallocarray(NULL, 0UL, 0UL); |
| 44 | ck_assert_ptr_ne(p, NULL); |
| 45 | xfree(p); |
| 46 | p = xreallocarray(NULL, 1UL, 0UL); |
| 47 | ck_assert_ptr_ne(p, NULL); |
| 48 | xfree(p); |
| 49 | p = xreallocarray(NULL, 0UL, 1UL); |
| 50 | ck_assert_ptr_ne(p, NULL); |
| 51 | xfree(p); |
| 52 | p = xreallocarray(NULL, 1UL, 1UL); |
| 53 | ck_assert_ptr_ne(p, NULL); |
| 54 | p = xreallocarray(p, 2UL, 2UL); |
| 55 | ck_assert_ptr_ne(p, NULL); |
| 56 | p = xreallocarray(p, 1UL, 1UL); |
| 57 | ck_assert_ptr_ne(p, NULL); |
| 58 | xfree(p); |
| 59 | } |
| 60 | END_TEST |
| 61 | |
| 62 | START_TEST(test_strdup) |
| 63 | { |
| 64 | char *s; |
| 65 | |
| 66 | s = xstrdup("test"); |
| 67 | ck_assert_str_eq(s, "test"); |
| 68 | xfree(s); |
| 69 | } |
| 70 | END_TEST |
| 71 | |
| 72 | Suite * |
| 73 | xalloc_suite(void) |
| 74 | { |
| 75 | Suite *s; |
| 76 | TCase *tc_xalloc; |
| 77 | |
| 78 | s = suite_create("Xalloc"); |
| 79 | |
| 80 | tc_xalloc = tcase_create("Xalloc"); |
| 81 | tcase_add_test(tc_xalloc, test_malloc); |
| 82 | tcase_add_test(tc_xalloc, test_calloc); |
| 83 | tcase_add_test(tc_xalloc, test_reallocarray); |
| 84 | tcase_add_test(tc_xalloc, test_strdup); |
| 85 | suite_add_tcase(s, tc_xalloc); |
| 86 | |
| 87 | return (s); |
| 88 | } |
| 89 | |
| 90 | int |
| 91 | main(void) |
| 92 | { |
| 93 | int num_failed; |
| 94 | Suite *s; |
| 95 | SRunner *sr; |
| 96 | |
| 97 | s = xalloc_suite(); |
| 98 | sr = srunner_create(s); |
| 99 | |
| 100 | srunner_run_all(sr, CK_NORMAL); |
| 101 | num_failed = srunner_ntests_failed(sr); |
| 102 | srunner_free(sr); |
| 103 | |
| 104 | if (num_failed) |
| 105 | return (1); |
| 106 | return (0); |
| 107 | } |