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>
This commit is contained in:
Philip Withnall 2022-03-17 16:24:49 +00:00
parent e4d77f7e89
commit 4ef27174af
3 changed files with 35 additions and 76 deletions

View File

@ -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++)
{

View File

@ -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));

View File

@ -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);