summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2010-10-22 14:06:54 +0100
committerLars Wirzenius <liw@liw.fi>2010-10-22 14:06:54 +0100
commit667e88379831f9ac960fc19c8838b6547ffc2710 (patch)
tree9a49f4422d03778799ed21a9028dba8f7f3c89a1
parent553742bf21b03047556d6d8fa05658cfe7e00ca1 (diff)
downloaddynstr-667e88379831f9ac960fc19c8838b6547ffc2710.tar.gz
Add test for second allocation failure for new().
-rw-r--r--unittests.c57
1 files changed, 45 insertions, 12 deletions
diff --git a/unittests.c b/unittests.c
index 11a5acb..04a2964 100644
--- a/unittests.c
+++ b/unittests.c
@@ -27,17 +27,6 @@
} 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(),
@@ -105,9 +94,14 @@ static void error_handler(int error, size_t size, void *oldptr)
error_handler_called = true;
}
+static int fail_malloc_after;
+
static void *fail_malloc(size_t size)
{
- return NULL;
+ if (fail_malloc_after <= 0)
+ return NULL;
+ --fail_malloc_after;
+ return malloc(size);
}
static int test_alloc_calls_error_handler(void)
@@ -172,6 +166,31 @@ static int test_empty_string_is_empty(void)
}
+static int test_new_returns_NULL_upon_first_allocation_failure(void)
+{
+ Dynstr *dynstr;
+
+ dynstr_init();
+ dynstr_set_malloc(fail_malloc);
+ dynstr = dynstr_new_empty();
+ FAIL_UNLESS_EQUAL(dynstr, NULL);
+ return true;
+}
+
+
+static int test_new_returns_NULL_upon_second_allocation_failure(void)
+{
+ Dynstr *dynstr;
+
+ dynstr_init();
+ dynstr_set_malloc(fail_malloc);
+ fail_malloc_after = 1;
+ dynstr = dynstr_new_empty();
+ FAIL_UNLESS_EQUAL(dynstr, NULL);
+ return true;
+}
+
+
static int test_creates_from_cstring(void)
{
const char bytes[] = "asdfasdfafdasdfasdfqw4tb";
@@ -262,6 +281,18 @@ static int test_memcpy_copies_substring_ok(void)
}
+static void setup(void)
+{
+ dynstr_init();
+ fail_malloc_after = 0;
+}
+
+
+static void teardown(void)
+{
+}
+
+
struct test {
const char *name;
int (*test)(void);
@@ -282,6 +313,8 @@ static const struct test tests[] = {
TEST(test_indicate_handler_returns),
TEST(test_abort_handler_calls_abort),
TEST(test_empty_string_is_empty),
+ TEST(test_new_returns_NULL_upon_first_allocation_failure),
+ TEST(test_new_returns_NULL_upon_second_allocation_failure),
TEST(test_creates_from_cstring),
TEST(test_creates_from_memory),
TEST(test_memcpy_returns_zero_if_offset_is_too_large),