From 4367eab8f75c452f559039c84db464571fef3e41 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Mon, 25 Oct 2010 09:52:54 +0100 Subject: Refactor unittest more, to automatically free substrings. --- unittests.c | 43 +++++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/unittests.c b/unittests.c index 72b548c..177b954 100644 --- a/unittests.c +++ b/unittests.c @@ -31,18 +31,32 @@ static Dynstr *strings[MAX_STRINGS]; static int num_strings; -static Dynstr *new_from_cstring(const char *cstring) +static void add(Dynstr *dynstr) { - Dynstr *dynstr; - - dynstr = dynstr_new_from_cstring(cstring); if (num_strings < MAX_STRINGS) { strings[num_strings] = dynstr; num_strings++; } +} + +static Dynstr *new_from_cstring(const char *cstring) +{ + Dynstr *dynstr; + + dynstr = dynstr_new_from_cstring(cstring); + add(dynstr); return dynstr; } +static Dynstr *substr(Dynstr *dynstr, size_t offset, size_t size) +{ + Dynstr *sub; + + sub = dynstr_substr(dynstr, offset, size); + add(sub); + return sub; +} + static void free_all_strings(void) { while (num_strings > 0) { @@ -366,10 +380,9 @@ static int test_copies_empty_substring_ok(void) char buf[1024]; dynstr = new_from_cstring("abcdef"); - sub = dynstr_substr(dynstr, 1, 0); + sub = substr(dynstr, 1, 0); size = dynstr_memcpy(buf, sub, 0, dynstr_len(sub)); FAIL_UNLESS_EQUAL(size, 0); - dynstr_free(sub); return true; } @@ -382,11 +395,10 @@ static int test_copies_single_byte_substring_ok(void) char buf[1024]; dynstr = new_from_cstring("abcdef"); - sub = dynstr_substr(dynstr, 1, 1); + sub = substr(dynstr, 1, 1); size = dynstr_memcpy(buf, sub, 0, dynstr_len(sub)); FAIL_UNLESS_EQUAL(size, 1); FAIL_UNLESS_EQUAL(memcmp(buf, "b", 1), 0); - dynstr_free(sub); return true; } @@ -399,11 +411,10 @@ static int test_copies_middle_substring_ok(void) char buf[1024]; dynstr = new_from_cstring("abcdef"); - sub = dynstr_substr(dynstr, 1, 4); + sub = substr(dynstr, 1, 4); size = dynstr_memcpy(buf, sub, 0, dynstr_len(sub)); FAIL_UNLESS_EQUAL(size, 4); FAIL_UNLESS_EQUAL(memcmp(buf, "bcde", 4), 0); - dynstr_free(sub); return true; } @@ -416,11 +427,10 @@ static int test_copies_initial_substring_ok(void) char buf[1024]; dynstr = new_from_cstring("abcdef"); - sub = dynstr_substr(dynstr, 0, 5); + sub = substr(dynstr, 0, 5); size = dynstr_memcpy(buf, sub, 0, dynstr_len(sub)); FAIL_UNLESS_EQUAL(size, 5); FAIL_UNLESS_EQUAL(memcmp(buf, "abcde", 5), 0); - dynstr_free(sub); return true; } @@ -433,11 +443,10 @@ static int test_copies_final_substring_ok(void) char buf[1024]; dynstr = new_from_cstring("abcdef"); - sub = dynstr_substr(dynstr, 1, 5); + sub = substr(dynstr, 1, 5); size = dynstr_memcpy(buf, sub, 0, dynstr_len(sub)); FAIL_UNLESS_EQUAL(size, 5); FAIL_UNLESS_EQUAL(memcmp(buf, "bcdef", 5), 0); - dynstr_free(sub); return true; } @@ -450,10 +459,9 @@ static int test_copies_empty_substring_when_offset_too_big(void) char buf[1024]; dynstr = new_from_cstring("abcdef"); - sub = dynstr_substr(dynstr, 100, 1); + sub = substr(dynstr, 100, 1); size = dynstr_memcpy(buf, sub, 0, dynstr_len(sub)); FAIL_UNLESS_EQUAL(size, 0); - dynstr_free(sub); return true; } @@ -466,11 +474,10 @@ static int test_copies_partial_substring_when_length_too_big(void) char buf[1024]; dynstr = new_from_cstring("abcdef"); - sub = dynstr_substr(dynstr, 1, 6); + sub = substr(dynstr, 1, 6); size = dynstr_memcpy(buf, sub, 0, dynstr_len(sub)); FAIL_UNLESS_EQUAL(size, 5); FAIL_UNLESS_EQUAL(memcmp(buf, "bcdef", 5), 0); - dynstr_free(sub); return true; } -- cgit v1.2.1