From 28f3315e86fb91926ec2ebd217af5a7a106e7d2a Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Fri, 6 Feb 2015 11:50:41 +0100 Subject: [PATCH] tests: Add tests for test_malloc() and test_realloc(). Signed-off-by: Andreas Schneider --- tests/CMakeLists.txt | 1 + tests/test_alloc.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 tests/test_alloc.c diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 9c00846..a058b5f 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -7,6 +7,7 @@ include_directories( ) set(CMOCKA_TESTS + test_alloc test_fixtures test_group_fixtures test_assert_macros diff --git a/tests/test_alloc.c b/tests/test_alloc.c new file mode 100644 index 0000000..6008499 --- /dev/null +++ b/tests/test_alloc.c @@ -0,0 +1,71 @@ +#include +#include +#include +#include + +#include +#include +#include + +static void torture_test_malloc(void **state) +{ + char *str; + size_t str_len; + size_t len; + + (void)state; /* unsused */ + + str_len = 12; + str = (char *)test_malloc(str_len); + assert_non_null(str); + + len = snprintf(str, str_len, "test string"); + assert_int_equal(len, 11); + + len = strlen(str); + assert_int_equal(len, 11); + + test_free(str); +} + +static void torture_test_realloc(void **state) +{ + char *str; + char *tmp; + size_t str_len; + size_t len; + + (void)state; /* unsused */ + + str_len = 16; + str = (char *)test_malloc(str_len); + assert_non_null(str); + + len = snprintf(str, str_len, "test string 123"); + assert_int_equal(len, 15); + + len = strlen(str); + assert_int_equal(len, 15); + + str_len = 20; + tmp = test_realloc(str, str_len); + assert_non_null(tmp); + + str = tmp; + len = strlen(str); + assert_string_equal(tmp, "test string 123"); + + snprintf(str + len, str_len - len, "4567"); + assert_string_equal(tmp, "test string 1234567"); + + test_free(str); +} + +int main(void) { + const struct CMUnitTest alloc_tests[] = { + cmocka_unit_test(torture_test_malloc), + cmocka_unit_test(torture_test_realloc), + }; + + return cmocka_run_group_tests(alloc_tests, NULL, NULL); +} -- 2.7.4