summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2010-10-22 13:59:26 +0100
committerLars Wirzenius <liw@liw.fi>2010-10-22 13:59:26 +0100
commitef2598daee4ad8321a2313dbdbeb5e4664e3842b (patch)
treed3e82d6d94ab66251112270d45146819a41f2c25
parent4009cb17590afba409dafe0e19cacbd4c97ddb88 (diff)
downloaddynstr-ef2598daee4ad8321a2313dbdbeb5e4664e3842b.tar.gz
Add test to verify that alloc() calls error handler, when appropriate.
-rw-r--r--dynstr.c13
-rw-r--r--dynstr.h8
-rw-r--r--unittests.c66
3 files changed, 85 insertions, 2 deletions
diff --git a/dynstr.c b/dynstr.c
index 73426a3..8dcffeb 100644
--- a/dynstr.c
+++ b/dynstr.c
@@ -16,13 +16,24 @@ struct Dynstr {
static dynstr_error_handler *error_handler = dynstr_malloc_error_indicate;
+static void *(*current_malloc)(size_t) = malloc;
void dynstr_init(void)
{
error_handler = dynstr_malloc_error_indicate;
+ current_malloc = malloc;
}
+void *(*dynstr_get_malloc(void))(size_t)
+{
+ return current_malloc;
+}
+
+void dynstr_set_malloc(void *(*new_malloc)(size_t))
+{
+ current_malloc = new_malloc;
+}
dynstr_error_handler *dynstr_get_malloc_error_handler(void)
{
@@ -48,7 +59,7 @@ static void *alloc(size_t size)
{
void *p;
- p = malloc(size);
+ p = current_malloc(size);
if (p == NULL)
error_handler(errno, size, NULL);
return p;
diff --git a/dynstr.h b/dynstr.h
index 39d2a61..0dbf13e 100644
--- a/dynstr.h
+++ b/dynstr.h
@@ -47,6 +47,14 @@ typedef struct Dynstr Dynstr;
* tests. */
void dynstr_init(void);
+/* Set the memory allocator to use, instead of malloc. This is mainly
+ * useful for unit tests. */
+void dynstr_set_malloc(void *(*allocator)(size_t));
+
+/* Get the memory allocator being used. This is mainly useful for unit
+ * tests. */
+void *(*dynstr_get_malloc(void))(size_t);
+
/* 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 8452125..4e7c15c 100644
--- a/unittests.c
+++ b/unittests.c
@@ -20,7 +20,8 @@
#define FAIL_UNLESS_EQUAL(a,b) \
do { \
if ((a) != (b)) { \
- printf("FAIL: not equal: '%s' and '%s'\n", #a, #b); \
+ printf("FAIL: %s:%d: not equal: '%s' and '%s'\n", \
+ __FUNCTION__, __LINE__, #a, #b); \
return false;\
} \
} while (0)
@@ -64,6 +65,13 @@ static int test_init_resets_error_handler(void)
}
+static int test_indicate_handler_returns(void)
+{
+ dynstr_malloc_error_indicate(0, 0, NULL);
+ return true;
+}
+
+
static jmp_buf env;
static void abort_handler(int signo)
{
@@ -90,6 +98,57 @@ static int test_abort_handler_calls_abort(void)
}
+static bool error_handler_called;
+
+static void error_handler(int error, size_t size, void *oldptr)
+{
+ error_handler_called = true;
+}
+
+static void *fail_malloc(size_t size)
+{
+ return NULL;
+}
+
+static int test_alloc_calls_error_handler(void)
+{
+ Dynstr *dynstr;
+
+ error_handler_called = false;
+ dynstr_set_malloc_error_handler(error_handler);
+ dynstr_set_malloc(fail_malloc);
+ dynstr = dynstr_new_empty();
+ FAIL_UNLESS_EQUAL(dynstr, NULL);
+ FAIL_UNLESS_EQUAL(error_handler_called, true);
+ return true;
+}
+
+
+static int test_uses_malloc_by_default(void)
+{
+ dynstr_init();
+ FAIL_UNLESS_EQUAL(dynstr_get_malloc(), malloc);
+ return true;
+}
+
+
+static int test_sets_malloc(void)
+{
+ dynstr_set_malloc(fail_malloc);
+ FAIL_UNLESS_EQUAL(dynstr_get_malloc(), fail_malloc);
+ return true;
+}
+
+
+static int test_init_resets_malloc(void)
+{
+ dynstr_set_malloc(fail_malloc);
+ dynstr_init();
+ FAIL_UNLESS_EQUAL(dynstr_get_malloc(), malloc);
+ return true;
+}
+
+
static int test_empty_string_is_empty(void)
{
Dynstr *dynstr;
@@ -203,6 +262,11 @@ 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_uses_malloc_by_default),
+ TEST(test_sets_malloc),
+ TEST(test_init_resets_malloc),
+ TEST(test_alloc_calls_error_handler),
+ TEST(test_indicate_handler_returns),
TEST(test_abort_handler_calls_abort),
TEST(test_empty_string_is_empty),
TEST(test_creates_from_cstring),