diff options
Diffstat (limited to 'unittests.c')
-rw-r--r-- | unittests.c | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/unittests.c b/unittests.c index 0a44542..95ce355 100644 --- a/unittests.c +++ b/unittests.c @@ -19,6 +19,16 @@ } while (0) +#define FAIL_UNLESS(cond) \ + do { \ + if (!(cond)) { \ + printf("FAIL: %s:%d: %s\n", \ + __FUNCTION__, __LINE__, #cond); \ + return false;\ + } \ + } while (0) + + #define FAIL_UNLESS_EQUAL(a,b) \ do { \ if ((a) != (b)) { \ @@ -875,6 +885,79 @@ static int test_last_string_does_not_find_empty_pattern(void) return true; } +static int test_cmp_returns_0_for_equal_strings(void) +{ + Dynstr *a; + Dynstr *b; + + a = new_from_cstring("foo"); + b = new_from_cstring("foo"); + FAIL_UNLESS_EQUAL(dynstr_cmp(a, b), 0); + return true; +} + + +static int test_cmp_returns_0_for_empty_strings(void) +{ + Dynstr *a; + Dynstr *b; + + a = dynstr_new_empty(); + b = dynstr_new_empty(); + FAIL_UNLESS_EQUAL(dynstr_cmp(a, b), 0); + dynstr_free(a); + dynstr_free(b); + return true; +} + + +static int test_cmp_returns_negative_if_only_first_string_is_empty(void) +{ + Dynstr *a; + Dynstr *b; + + a = new_from_cstring(""); + b = new_from_cstring("foo"); + FAIL_UNLESS(dynstr_cmp(a, b) < 0); + return true; +} + + +static int test_cmp_returns_positive_if_only_second_string_is_empty(void) +{ + Dynstr *a; + Dynstr *b; + + a = new_from_cstring("foo"); + b = new_from_cstring(""); + FAIL_UNLESS(dynstr_cmp(a, b) > 0); + return true; +} + + +static int test_cmp_returns_negative_if_first_string_comes_first(void) +{ + Dynstr *a; + Dynstr *b; + + a = new_from_cstring("x"); + b = new_from_cstring("\xff"); /* Check for unsigned comparision, too. */ + FAIL_UNLESS(dynstr_cmp(a, b) < 0); + return true; +} + + +static int test_cmp_returns_positive_if_second_string_comes_first(void) +{ + Dynstr *a; + Dynstr *b; + + a = new_from_cstring("\xff"); + b = new_from_cstring("x"); + FAIL_UNLESS(dynstr_cmp(a, b) > 0); + return true; +} + static int test_fwrite_writes_string(void) { @@ -983,6 +1066,12 @@ static const struct test tests[] = { TEST(test_last_string_does_not_find_pattern_outside_range), TEST(test_last_string_only_finds_pattern_inside_range), TEST(test_last_string_does_not_find_empty_pattern), + TEST(test_cmp_returns_0_for_equal_strings), + TEST(test_cmp_returns_0_for_empty_strings), + TEST(test_cmp_returns_negative_if_only_first_string_is_empty), + TEST(test_cmp_returns_positive_if_only_second_string_is_empty), + TEST(test_cmp_returns_negative_if_first_string_comes_first), + TEST(test_cmp_returns_positive_if_second_string_comes_first), TEST(test_fwrite_writes_string), }; static const int num_tests = sizeof(tests) / sizeof(tests[0]); |