[glib/glib-2-68: 3/8] test_string_replace: Make the test table-driven




commit 5162562903333a8607a6f8def49170eed94a1a36
Author: Simon McVittie <smcv collabora com>
Date:   Mon Aug 2 11:25:56 2021 +0100

    test_string_replace: Make the test table-driven
    
    This makes it straightforward to add more test-cases.
    
    Signed-off-by: Simon McVittie <smcv collabora com>

 glib/tests/string.c | 66 ++++++++++++++++++++++++++++++++++-------------------
 1 file changed, 42 insertions(+), 24 deletions(-)
---
diff --git a/glib/tests/string.c b/glib/tests/string.c
index b222d6194..72c9016dd 100644
--- a/glib/tests/string.c
+++ b/glib/tests/string.c
@@ -498,30 +498,48 @@ test_string_to_bytes (void)
 static void
 test_string_replace (void)
 {
-  GString *s;
-  guint n;
-
-  s = g_string_new ("foo bar foo baz foo bar foobarbaz");
-
-  n = g_string_replace (s, "bar", "baz", 0);
-  g_assert_cmpstr ("foo baz foo baz foo baz foobazbaz", ==, s->str);
-  g_assert_cmpuint (n, ==, 3);
-
-  n = g_string_replace (s, "baz", "bar", 3);
-  g_assert_cmpstr ("foo bar foo bar foo bar foobazbaz", ==, s->str);
-  g_assert_cmpuint (n, ==, 3);
-
-  n = g_string_replace (s, "foobar", "bar", 1);
-  g_assert_cmpstr ("foo bar foo bar foo bar foobazbaz", ==, s->str);
-  g_assert_cmpuint (n, ==, 0);
-
-  s = g_string_assign (s, "aaaaaaaa");
-  n = g_string_replace (s, "a", "abcdefghijkl", 0);
-  g_assert_cmpstr 
("abcdefghijklabcdefghijklabcdefghijklabcdefghijklabcdefghijklabcdefghijklabcdefghijklabcdefghijkl",
-                   ==, s->str);
-  g_assert_cmpuint (n, ==, 8);
-
-  g_string_free (s, TRUE);
+  static const struct
+  {
+    const char *string;
+    const char *original;
+    const char *replacement;
+    guint limit;
+    const char *expected;
+    guint expected_n;
+  }
+  tests[] =
+  {
+    { "foo bar foo baz foo bar foobarbaz", "bar", "baz", 0,
+      "foo baz foo baz foo baz foobazbaz", 3 },
+    { "foo baz foo baz foo baz foobazbaz", "baz", "bar", 3,
+      "foo bar foo bar foo bar foobazbaz", 3 },
+    { "foo bar foo bar foo bar foobazbaz", "foobar", "bar", 1,
+      "foo bar foo bar foo bar foobazbaz", 0 },
+    { "aaaaaaaa", "a", "abcdefghijkl", 0,
+      "abcdefghijklabcdefghijklabcdefghijklabcdefghijklabcdefghijklabcdefghijklabcdefghijklabcdefghijkl",
+      8 },
+  };
+  gsize i;
+
+  for (i = 0; i < G_N_ELEMENTS (tests); i++)
+    {
+      GString *s;
+      guint n;
+
+      s = g_string_new (tests[i].string);
+      g_test_message ("%" G_GSIZE_FORMAT ": Replacing \"%s\" with \"%s\" (limit %u) in \"%s\"",
+                      i, tests[i].original, tests[i].replacement,
+                      tests[i].limit, tests[i].string);
+      n = g_string_replace (s, tests[i].original, tests[i].replacement,
+                            tests[i].limit);
+      g_test_message ("-> %u replacements, \"%s\"",
+                      n, s->str);
+      g_assert_cmpstr (tests[i].expected, ==, s->str);
+      g_assert_cmpuint (strlen (tests[i].expected), ==, s->len);
+      g_assert_cmpuint (strlen (tests[i].expected) + 1, <=, s->allocated_len);
+      g_assert_cmpuint (tests[i].expected_n, ==, n);
+      g_string_free (s, TRUE);
+    }
 }
 
 int


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]