]> granicus.if.org Git - neomutt/commitdiff
Testing: mutt_str_strcasestr
authorAustin Ray <austin@austinray.io>
Sat, 18 Aug 2018 22:38:53 +0000 (18:38 -0400)
committerRichard Russon <rich@flatcap.org>
Thu, 4 Oct 2018 14:48:45 +0000 (15:48 +0100)
Included alongside this implementation are tests for NULL input, empty
string inputs, non-existent needles, and different size haystacks.

test/main.c
test/string.c

index ea1c620f61afe7feecc6ac0e6606cc2a1134b2df..5251f2d6e4e54072371c36a13d0c43a343d491bf 100644 (file)
@@ -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)                                \
index 00b7838445f5bb76f16d0febcdc759bc4902e8e9..c684f9bc913fa6d73b3a28abe50f678b4dd1f9a6 100644 (file)
@@ -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);
+  }
+}
+