]> granicus.if.org Git - neomutt/commitdiff
test: improve test_mutt_str_find_word()
authorRichard Russon <rich@flatcap.org>
Sun, 26 May 2019 15:02:23 +0000 (16:02 +0100)
committerRichard Russon <rich@flatcap.org>
Sun, 26 May 2019 16:18:13 +0000 (17:18 +0100)
mutt/string.c
test/string/mutt_str_find_word.c

index 87fbc5395aa7415880e04ff3aa103ff36b7912bf..203007e201cdb9a10b76cd12dc1cfeafe699ebbe 100644 (file)
@@ -1020,9 +1020,9 @@ bool mutt_str_is_ascii(const char *p, size_t len)
 }
 
 /**
- * mutt_str_find_word - Find the next word (non-space)
+ * mutt_str_find_word - Find the end of a word (non-space)
  * @param src String to search
- * @retval ptr Beginning of the next word
+ * @retval ptr End of the word
  *
  * Skip to the end of the current word.
  * Skip past any whitespace characters.
@@ -1032,13 +1032,14 @@ bool mutt_str_is_ascii(const char *p, size_t len)
  */
 const char *mutt_str_find_word(const char *src)
 {
-  const char *p = src;
+  if (!src)
+    return NULL;
 
-  while (p && *p && strchr(" \t\n", *p))
-    p++;
-  while (p && *p && !strchr(" \t\n", *p))
-    p++;
-  return p;
+  while (*src && strchr(" \t\n", *src))
+    src++;
+  while (*src && !strchr(" \t\n", *src))
+    src++;
+  return src;
 }
 
 /**
index d45cdd22d00becabd9fffc49aa41ee540cfdf9ad..bfffe2c88b82741279bdf91ef579c160cebe128a 100644 (file)
 #include "config.h"
 #include "mutt/mutt.h"
 
+struct FindWordTest
+{
+  const char *str;
+  size_t offset;
+};
+
 void test_mutt_str_find_word(void)
 {
   // const char *mutt_str_find_word(const char *src);
@@ -32,4 +38,34 @@ void test_mutt_str_find_word(void)
   {
     TEST_CHECK(mutt_str_find_word(NULL) == NULL);
   }
+
+  // clang-format off
+  struct FindWordTest find_tests[] =
+  {
+    { "apple banana",    5 },
+    { "apple\tbanana",   5 },
+    { "apple\nbanana",   5 },
+
+    { "apple\t banana",  5 },
+    { "apple\n\nbanana", 5 },
+    { "apple   banana",  5 },
+
+    { "\t banana",       8 },
+    { "\n\nbanana",      8 },
+    { "   banana",       9 },
+
+    { " \t\n ",          4 },
+  };
+  // clang-format on
+
+  {
+    for (size_t i = 0; i < mutt_array_size(find_tests); i++)
+    {
+      struct FindWordTest *t = &find_tests[i];
+
+      TEST_CASE_("'%s'", t->str);
+      const char *result = mutt_str_find_word(t->str);
+      TEST_CHECK(result == (t->str + t->offset));
+    }
+  }
 }