[glib/wip/smcv/2452-g-string-0-length-replace: 2/5] test_string_replace: Make the test table-driven




commit b87ddaf96fad2b4baa1238779132a06517e9c52f
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..57b7c71c8 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 },
+  };
+  guint i;
+
+  for (i = 0; i < G_N_ELEMENTS (tests); i++)
+    {
+      GString *s;
+      guint n;
+
+      s = g_string_new (tests[i].string);
+      g_test_message ("%u: 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]