From: Austin Ray Date: Sat, 18 Aug 2018 22:38:53 +0000 (-0400) Subject: Testing: mutt_str_strcasestr X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=6de884e0eb27afec9a784a2da50b4b3533f278a9;p=neomutt Testing: mutt_str_strcasestr Included alongside this implementation are tests for NULL input, empty string inputs, non-existent needles, and different size haystacks. --- diff --git a/test/main.c b/test/main.c index ea1c620f6..5251f2d6e 100644 --- a/test/main.c +++ b/test/main.c @@ -13,6 +13,7 @@ NEOMUTT_TEST_ITEM(test_md5_ctx_bytes) \ NEOMUTT_TEST_ITEM(test_string_strfcpy) \ NEOMUTT_TEST_ITEM(test_string_strnfcpy) \ + NEOMUTT_TEST_ITEM(test_string_strcasestr) \ NEOMUTT_TEST_ITEM(test_addr_mbox_to_udomain) \ NEOMUTT_TEST_ITEM(test_mutt_path_tidy_slash) \ NEOMUTT_TEST_ITEM(test_mutt_path_tidy_dotdot) \ diff --git a/test/string.c b/test/string.c index 00b783844..c684f9bc9 100644 --- a/test/string.c +++ b/test/string.c @@ -107,3 +107,84 @@ void test_string_strnfcpy(void) } } } + +void test_string_strcasestr(void) +{ + char *haystack_same_size = "hello"; + char *haystack_larger = "hello, world!"; + char *haystack_smaller = "heck"; + char *haystack_mid = "test! hello, world"; + char *haystack_end = ", world! hello"; + + char *empty = ""; + + const char *needle = "hEllo"; + const char *needle_lower = "hello"; + const char *nonexistent = "goodbye"; + + { // Check NULL conditions + const char *retval1 = mutt_str_strcasestr(NULL, NULL); + const char *retval2 = mutt_str_strcasestr(NULL, needle); + const char *retval3 = mutt_str_strcasestr(haystack_same_size, NULL); + + TEST_CHECK_(retval1 == NULL, "Expected: %s, Actual %s", NULL, retval1); + TEST_CHECK_(retval2 == NULL, "Expected: %s, Actual %s", NULL, retval2); + TEST_CHECK_(retval3 == NULL, "Expected: %s, Actual %s", NULL, retval3); + } + + { // Check empty strings + const char *retval1 = mutt_str_strcasestr(empty, empty); + const char *retval2 = mutt_str_strcasestr(empty, needle); + const char *retval3 = mutt_str_strcasestr(haystack_same_size, empty); + + const char *empty_expected = strstr(empty, empty); + + TEST_CHECK_(retval1 == empty_expected, "Expected: %s, Actual %s", "", retval1); + TEST_CHECK_(retval2 == NULL, "Expected: %s, Actual %s", NULL, retval2); + TEST_CHECK_(retval3 == haystack_same_size, "Expected: %s, Actual %s", haystack_same_size, retval3); + } + + { // Check instance where needle is not in haystack. + const char *retval1 = mutt_str_strcasestr(haystack_same_size, nonexistent); + const char *retval2 = mutt_str_strcasestr(haystack_smaller, nonexistent); + const char *retval3 = mutt_str_strcasestr(haystack_larger, nonexistent); + + TEST_CHECK_(retval1 == NULL, "Expected: %s, Actual %s", NULL, retval1); + TEST_CHECK_(retval2 == NULL, "Expected: %s, Actual %s", NULL, retval2); + TEST_CHECK_(retval3 == NULL, "Expected: %s, Actual %s", NULL, retval3); + } + + { // Check instance haystack is the same length as the needle and needle exists. + const char *retval = mutt_str_strcasestr(haystack_same_size, needle); + + TEST_CHECK_(retval == haystack_same_size, "Expected: %s, Actual: %s", haystack_same_size, retval); + } + + { // Check instance haystack is larger and needle exists. + const char *retval = mutt_str_strcasestr(haystack_larger, needle); + const char *expected = strstr(haystack_larger, needle_lower); + + TEST_CHECK_(retval == expected, "Expected: %s, Actual: %s", haystack_larger, retval); + } + + { // Check instance where word is in the middle + const char *retval = mutt_str_strcasestr(haystack_mid, needle); + const char *expected = strstr(haystack_mid, needle_lower); + + TEST_CHECK_(retval == expected, "Expected: %s, Actual: %s", expected, retval); + } + + { // Check instance where needle is at the end, + const char *retval = mutt_str_strcasestr(haystack_end, needle); + const char *expected = strstr(haystack_end, needle_lower); + + TEST_CHECK_(retval == expected, "Expected: %s, Actual: %s", expected, retval); + } + + { // Check instance where haystack is smaller than needle. + const char *retval = mutt_str_strcasestr(haystack_smaller, needle); + + TEST_CHECK_(retval == NULL, "Expected: %s, Actual: %s", NULL, retval); + } +} +