[glib: 2/3] tests: Use g_strv_contains() rather than a home-grown version




commit 4ef27174aff0afcea291453a8f3593127a2ba880
Author: Philip Withnall <pwithnall endlessos org>
Date:   Thu Mar 17 16:24:49 2022 +0000

    tests: Use g_strv_contains() rather than a home-grown version
    
    The public `g_strv_contains()` API didn’t exist at the time this code
    was originally written. Now, happily, it does.
    
    Signed-off-by: Philip Withnall <pwithnall endlessos org>

 gio/tests/actions.c      | 29 ++++++++-------------------
 gio/tests/gdbus-export.c | 52 ++++++++++++++++++------------------------------
 gio/tests/gsettings.c    | 30 ++++++++--------------------
 3 files changed, 35 insertions(+), 76 deletions(-)
---
diff --git a/gio/tests/actions.c b/gio/tests/actions.c
index 12538e341..a41e989bd 100644
--- a/gio/tests/actions.c
+++ b/gio/tests/actions.c
@@ -115,33 +115,20 @@ test_name (void)
 }
 
 static gboolean
-strv_has_string (gchar       **haystack,
-                 const gchar  *needle)
-{
-  guint n;
-
-  for (n = 0; haystack != NULL && haystack[n] != NULL; n++)
-    {
-      if (g_strcmp0 (haystack[n], needle) == 0)
-        return TRUE;
-    }
-  return FALSE;
-}
-
-static gboolean
-strv_strv_cmp (gchar **a, gchar **b)
+strv_strv_cmp (const gchar * const *a,
+               const gchar * const *b)
 {
   guint n;
 
   for (n = 0; a[n] != NULL; n++)
     {
-       if (!strv_has_string (b, a[n]))
+       if (!g_strv_contains (b, a[n]))
          return FALSE;
     }
 
   for (n = 0; b[n] != NULL; n++)
     {
-       if (!strv_has_string (a, b[n]))
+       if (!g_strv_contains (a, b[n]))
          return FALSE;
     }
 
@@ -149,7 +136,7 @@ strv_strv_cmp (gchar **a, gchar **b)
 }
 
 static gboolean
-strv_set_equal (gchar **strv, ...)
+strv_set_equal (const gchar * const *strv, ...)
 {
   guint count;
   va_list list;
@@ -164,7 +151,7 @@ strv_set_equal (gchar **strv, ...)
       str = va_arg (list, const gchar *);
       if (str == NULL)
         break;
-      if (!strv_has_string (strv, str))
+      if (!g_strv_contains (strv, str))
         {
           res = FALSE;
           break;
@@ -215,7 +202,7 @@ test_simple_group (void)
   g_assert_false (g_action_group_has_action (G_ACTION_GROUP (group), "baz"));
   actions = g_action_group_list_actions (G_ACTION_GROUP (group));
   g_assert_cmpint (g_strv_length (actions), ==, 2);
-  g_assert_true (strv_set_equal (actions, "foo", "bar", NULL));
+  g_assert_true (strv_set_equal ((const gchar * const *) actions, "foo", "bar", NULL));
   g_strfreev (actions);
   g_assert_true (g_action_group_get_action_enabled (G_ACTION_GROUP (group), "foo"));
   g_assert_true (g_action_group_get_action_enabled (G_ACTION_GROUP (group), "bar"));
@@ -599,7 +586,7 @@ compare_action_groups (GActionGroup *a, GActionGroup *b)
 
   alist = g_action_group_list_actions (a);
   blist = g_action_group_list_actions (b);
-  equal = strv_strv_cmp (alist, blist);
+  equal = strv_strv_cmp ((const gchar * const *) alist, (const gchar * const *) blist);
 
   for (i = 0; equal && alist[i]; i++)
     {
diff --git a/gio/tests/gdbus-export.c b/gio/tests/gdbus-export.c
index 4cdc24492..f66b1f74c 100644
--- a/gio/tests/gdbus-export.c
+++ b/gio/tests/gdbus-export.c
@@ -602,20 +602,6 @@ on_subtree_unregistered (gpointer user_data)
   data->num_unregistered_subtree_calls++;
 }
 
-static gboolean
-_g_strv_has_string (const gchar* const * haystack,
-                    const gchar *needle)
-{
-  guint n;
-
-  for (n = 0; haystack != NULL && haystack[n] != NULL; n++)
-    {
-      if (g_strcmp0 (haystack[n], needle) == 0)
-        return TRUE;
-    }
-  return FALSE;
-}
-
 /* -------------------- */
 
 static gchar **
@@ -1306,11 +1292,11 @@ test_object_registration (void)
   nodes = get_nodes_at (c, "/foo/boss");
   g_assert (nodes != NULL);
   g_assert_cmpint (g_strv_length (nodes), ==, 5);
-  g_assert (_g_strv_has_string ((const gchar* const *) nodes, "worker1"));
-  g_assert (_g_strv_has_string ((const gchar* const *) nodes, "worker1p1"));
-  g_assert (_g_strv_has_string ((const gchar* const *) nodes, "worker2"));
-  g_assert (_g_strv_has_string ((const gchar* const *) nodes, "interns"));
-  g_assert (_g_strv_has_string ((const gchar* const *) nodes, "executives"));
+  g_assert (g_strv_contains ((const gchar* const *) nodes, "worker1"));
+  g_assert (g_strv_contains ((const gchar* const *) nodes, "worker1p1"));
+  g_assert (g_strv_contains ((const gchar* const *) nodes, "worker2"));
+  g_assert (g_strv_contains ((const gchar* const *) nodes, "interns"));
+  g_assert (g_strv_contains ((const gchar* const *) nodes, "executives"));
   g_strfreev (nodes);
   /* any registered object always implement org.freedesktop.DBus.[Peer,Introspectable,Properties] */
   g_assert_cmpint (count_interfaces (c, "/foo/boss"), ==, 5);
@@ -1322,7 +1308,7 @@ test_object_registration (void)
    */
   nodes = get_nodes_at (c, "/foo/boss/executives");
   g_assert (nodes != NULL);
-  g_assert (_g_strv_has_string ((const gchar* const *) nodes, "non_subtree_object"));
+  g_assert (g_strv_contains ((const gchar* const *) nodes, "non_subtree_object"));
   g_assert_cmpint (g_strv_length (nodes), ==, 1);
   g_strfreev (nodes);
   g_assert_cmpint (count_interfaces (c, "/foo/boss/executives"), ==, 0);
@@ -1332,11 +1318,11 @@ test_object_registration (void)
   nodes = get_nodes_at (c, "/foo/boss/executives");
   g_assert (nodes != NULL);
   g_assert_cmpint (g_strv_length (nodes), ==, 5);
-  g_assert (_g_strv_has_string ((const gchar* const *) nodes, "non_subtree_object"));
-  g_assert (_g_strv_has_string ((const gchar* const *) nodes, "vp0"));
-  g_assert (_g_strv_has_string ((const gchar* const *) nodes, "vp1"));
-  g_assert (_g_strv_has_string ((const gchar* const *) nodes, "evp0"));
-  g_assert (_g_strv_has_string ((const gchar* const *) nodes, "evp1"));
+  g_assert (g_strv_contains ((const gchar* const *) nodes, "non_subtree_object"));
+  g_assert (g_strv_contains ((const gchar* const *) nodes, "vp0"));
+  g_assert (g_strv_contains ((const gchar* const *) nodes, "vp1"));
+  g_assert (g_strv_contains ((const gchar* const *) nodes, "evp0"));
+  g_assert (g_strv_contains ((const gchar* const *) nodes, "evp1"));
   /* check that /foo/boss/executives/non_subtree_object is not handled by the
    * subtree handlers - we can do this because objects from subtree handlers
    * has exactly one interface and non_subtree_object has two
@@ -1358,13 +1344,13 @@ test_object_registration (void)
   nodes = get_nodes_at (c, "/foo/boss/executives");
   g_assert (nodes != NULL);
   g_assert_cmpint (g_strv_length (nodes), ==, 7);
-  g_assert (_g_strv_has_string ((const gchar* const *) nodes, "non_subtree_object"));
-  g_assert (_g_strv_has_string ((const gchar* const *) nodes, "vp0"));
-  g_assert (_g_strv_has_string ((const gchar* const *) nodes, "vp1"));
-  g_assert (_g_strv_has_string ((const gchar* const *) nodes, "vp2"));
-  g_assert (_g_strv_has_string ((const gchar* const *) nodes, "evp0"));
-  g_assert (_g_strv_has_string ((const gchar* const *) nodes, "evp1"));
-  g_assert (_g_strv_has_string ((const gchar* const *) nodes, "evp2"));
+  g_assert (g_strv_contains ((const gchar* const *) nodes, "non_subtree_object"));
+  g_assert (g_strv_contains ((const gchar* const *) nodes, "vp0"));
+  g_assert (g_strv_contains ((const gchar* const *) nodes, "vp1"));
+  g_assert (g_strv_contains ((const gchar* const *) nodes, "vp2"));
+  g_assert (g_strv_contains ((const gchar* const *) nodes, "evp0"));
+  g_assert (g_strv_contains ((const gchar* const *) nodes, "evp1"));
+  g_assert (g_strv_contains ((const gchar* const *) nodes, "evp2"));
   g_strfreev (nodes);
 
   /* This is to check that a bug (rather, class of bugs) in gdbusconnection.c's
@@ -1401,7 +1387,7 @@ test_object_registration (void)
   nodes = get_nodes_at (c, "/foo/boss/executives");
   g_assert (nodes != NULL);
   g_assert_cmpint (g_strv_length (nodes), ==, 1);
-  g_assert (_g_strv_has_string ((const gchar* const *) nodes, "non_subtree_object"));
+  g_assert (g_strv_contains ((const gchar* const *) nodes, "non_subtree_object"));
   g_strfreev (nodes);
 
   g_assert (g_dbus_connection_unregister_object (c, boss_foo_reg_id));
diff --git a/gio/tests/gsettings.c b/gio/tests/gsettings.c
index dad1623b7..35d958e4d 100644
--- a/gio/tests/gsettings.c
+++ b/gio/tests/gsettings.c
@@ -2365,21 +2365,7 @@ G_GNUC_END_IGNORE_DEPRECATIONS
 }
 
 static gboolean
-strv_has_string (gchar       **haystack,
-                 const gchar  *needle)
-{
-  guint n;
-
-  for (n = 0; haystack != NULL && haystack[n] != NULL; n++)
-    {
-      if (g_strcmp0 (haystack[n], needle) == 0)
-        return TRUE;
-    }
-  return FALSE;
-}
-
-static gboolean
-strv_set_equal (gchar **strv, ...)
+strv_set_equal (const gchar * const *strv, ...)
 {
   gsize count;
   va_list list;
@@ -2394,7 +2380,7 @@ strv_set_equal (gchar **strv, ...)
       str = va_arg (list, const gchar *);
       if (str == NULL)
         break;
-      if (!strv_has_string (strv, str))
+      if (!g_strv_contains (strv, str))
         {
           res = FALSE;
           break;
@@ -2422,8 +2408,8 @@ test_list_items (void)
   children = g_settings_list_children (settings);
   keys = g_settings_schema_list_keys (schema);
 
-  g_assert_true (strv_set_equal (children, "basic-types", "complex-types", "localized", NULL));
-  g_assert_true (strv_set_equal (keys, "greeting", "farewell", NULL));
+  g_assert_true (strv_set_equal ((const gchar * const *) children, "basic-types", "complex-types", 
"localized", NULL));
+  g_assert_true (strv_set_equal ((const gchar * const *) keys, "greeting", "farewell", NULL));
 
   g_strfreev (children);
   g_strfreev (keys);
@@ -2443,13 +2429,13 @@ G_GNUC_BEGIN_IGNORE_DEPRECATIONS
   schemas = g_settings_list_schemas ();
 G_GNUC_END_IGNORE_DEPRECATIONS
 
-  g_assert_true (strv_set_equal ((gchar **)relocs,
+  g_assert_true (strv_set_equal (relocs,
                                  "org.gtk.test.no-path",
                                  "org.gtk.test.extends.base",
                                  "org.gtk.test.extends.extended",
                                  NULL));
 
-  g_assert_true (strv_set_equal ((gchar **)schemas,
+  g_assert_true (strv_set_equal (schemas,
                                  "org.gtk.test",
                                  "org.gtk.test.basic-types",
                                  "org.gtk.test.complex-types",
@@ -2658,7 +2644,7 @@ test_schema_list_keys (void)
 
   keys = g_settings_schema_list_keys (schema);
 
-  g_assert_true (strv_set_equal ((gchar **)keys,
+  g_assert_true (strv_set_equal ((const gchar * const *) keys,
                                  "greeting",
                                  "farewell",
                                  NULL));
@@ -2969,7 +2955,7 @@ test_extended_schema (void)
   settings = g_settings_new_with_path ("org.gtk.test.extends.extended", "/test/extendes/");
   g_object_get (settings, "settings-schema", &schema, NULL);
   keys = g_settings_schema_list_keys (schema);
-  g_assert_true (strv_set_equal (keys, "int32", "string", "another-int32", NULL));
+  g_assert_true (strv_set_equal ((const gchar * const *) keys, "int32", "string", "another-int32", NULL));
   g_strfreev (keys);
   g_object_unref (settings);
   g_settings_schema_unref (schema);


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