diff options
author | Lars Wirzenius <liw@liw.fi> | 2010-10-20 20:53:19 +0100 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2010-10-20 20:53:19 +0100 |
commit | 1945a17b30ef084d79a11f4954c85104748e074d (patch) | |
tree | 4f2aa26fd44b79985f805aaf56e6299ae01d9cd4 | |
parent | 4664f19026381b2791a3cd65966686787809e821 (diff) | |
download | dynstr-1945a17b30ef084d79a11f4954c85104748e074d.tar.gz |
Add dynstr_init so unit tests can reset library.
This means that unit tests should not depend on the order in which
they run.
-rw-r--r-- | dynstr.c | 6 | ||||
-rw-r--r-- | dynstr.h | 5 | ||||
-rw-r--r-- | unittests.c | 24 |
3 files changed, 35 insertions, 0 deletions
@@ -18,6 +18,12 @@ struct Dynstr { static dynstr_error_handler *error_handler = dynstr_malloc_error_indicate; +void dynstr_init(void) +{ + error_handler = dynstr_malloc_error_indicate; +} + + dynstr_error_handler *dynstr_get_malloc_error_handler(void) { return error_handler; @@ -42,6 +42,11 @@ typedef struct Dynstr Dynstr; +/* Initialize or re-initialize the library. This sets all global variables + * used internally to their defaults. This is mainly useful for unit + * tests. */ +void dynstr_init(void); + /* Type of callback functions for handling malloc failures. */ typedef void dynstr_error_handler(int error, size_t size, void *oldptr); diff --git a/unittests.c b/unittests.c index 28a9e9d..e0880d1 100644 --- a/unittests.c +++ b/unittests.c @@ -25,6 +25,17 @@ } while (0) +static void setup(void) +{ + dynstr_init(); +} + + +static void teardown(void) +{ +} + + static int test_default_error_handler_is_indicate(void) { FAIL_UNLESS_EQUAL(dynstr_get_malloc_error_handler(), @@ -42,6 +53,16 @@ static int test_sets_error_handler(void) } +static int test_init_resets_error_handler(void) +{ + dynstr_set_malloc_error_handler(dynstr_malloc_error_abort); + dynstr_init(); + FAIL_UNLESS_EQUAL(dynstr_get_malloc_error_handler(), + dynstr_malloc_error_indicate); + return true; +} + + static jmp_buf env; static void abort_handler(int signo) { @@ -79,6 +100,7 @@ struct test { static const struct test tests[] = { TEST(test_default_error_handler_is_indicate), TEST(test_sets_error_handler), + TEST(test_init_resets_error_handler), TEST(test_abort_handler_calls_abort), }; static const int num_tests = sizeof(tests) / sizeof(tests[0]); @@ -89,9 +111,11 @@ int main(void) exit = EXIT_SUCCESS; for (int i = 0; i < num_tests; ++i) { + setup(); if (!tests[i].test()) { exit = EXIT_FAILURE; } + teardown(); } return exit; } |