From 111f219f17b7893727cc254b22e4469c7327f2cd Mon Sep 17 00:00:00 2001 From: Moritz Grimm Date: Sun, 24 May 2015 13:20:26 +0200 Subject: [PATCH] Test xalloc (as much as possible) --- tests/Makefile.am | 10 +++- tests/check_xalloc.c | 109 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+), 2 deletions(-) create mode 100644 tests/check_xalloc.c diff --git a/tests/Makefile.am b/tests/Makefile.am index 89bb297..aa5e7fe 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -3,7 +3,8 @@ AUTOMAKE_OPTIONS = 1.10 foreign subdir-objects TESTS = \ check_cfg \ check_cfg_xmlfile \ - check_log + check_log \ + check_xalloc check_PROGRAMS = $(TESTS) check_cfg_SOURCES = \ @@ -19,7 +20,12 @@ check_cfg_xmlfile_LDADD = $(check_cfg_xmlfile_DEPENDENCIES) @CHECK_LIBS@ check_log_SOURCES = \ check_log.c check_log_DEPENDENCIES = $(top_builddir)/src/libezstream.la -check_log_LDADD = $(check_cfg_xmlfile_DEPENDENCIES) @CHECK_LIBS@ +check_log_LDADD = $(check_log_DEPENDENCIES) @CHECK_LIBS@ + +check_xalloc_SOURCES = \ + check_xalloc.c +check_xalloc_DEPENDENCIES = $(top_builddir)/src/libezstream.la +check_xalloc_LDADD = $(check_xalloc_DEPENDENCIES) @CHECK_LIBS@ AM_CPPFLAGS = @EZ_CPPFLAGS@ \ -I$(top_srcdir)/compat \ diff --git a/tests/check_xalloc.c b/tests/check_xalloc.c new file mode 100644 index 0000000..deacb39 --- /dev/null +++ b/tests/check_xalloc.c @@ -0,0 +1,109 @@ +#include + +#include "xalloc.h" + +Suite * xalloc_suite(void); +void setup_checked(void); +void teardown_checked(void); + +START_TEST(test_malloc) +{ + void *p; + + p = xmalloc(0); + ck_assert_ptr_ne(p, NULL); + xfree(p); + p = xmalloc(1); + ck_assert_ptr_ne(p, NULL); + xfree(p); +} +END_TEST + +START_TEST(test_calloc) +{ + void *p; + + p = xcalloc(0, 0); + ck_assert_ptr_ne(p, NULL); + xfree(p); + p = xcalloc(1, 0); + ck_assert_ptr_ne(p, NULL); + xfree(p); + p = xcalloc(0, 1); + ck_assert_ptr_ne(p, NULL); + xfree(p); + p = xcalloc(1, 1); + ck_assert_ptr_ne(p, NULL); + xfree(p); +} +END_TEST + +START_TEST(test_reallocarray) +{ + void *p; + + p = xreallocarray(NULL, 0, 0); + ck_assert_ptr_ne(p, NULL); + xfree(p); + p = xreallocarray(NULL, 1, 0); + ck_assert_ptr_ne(p, NULL); + xfree(p); + p = xreallocarray(NULL, 0, 1); + ck_assert_ptr_ne(p, NULL); + xfree(p); + p = xreallocarray(NULL, 1, 1); + ck_assert_ptr_ne(p, NULL); + p = xreallocarray(p, 2, 2); + ck_assert_ptr_ne(p, NULL); + p = xreallocarray(p, 1, 1); + 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) +{ + unsigned int num_failed; + Suite *s; + SRunner *sr; + + 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); +}