[gtksourceview] testsuite: compare strv using set comparison



commit 23f615b6cbffe7fc474f5238eb0d2bd089dadf9a
Author: Christian Hergert <chergert redhat com>
Date:   Wed May 25 13:14:57 2022 -0700

    testsuite: compare strv using set comparison
    
    The two strv should be compared in such a way that the complete set is in
    both strv but in any order and no more. Just a general set theory
    comparison of the two strv.
    
    Based on suggestion from Sébastien Wilmet on discourse.gnome.org.

 testsuite/test-language.c | 53 +++++++++++++++++++++++++++++++++++------------
 1 file changed, 40 insertions(+), 13 deletions(-)
---
diff --git a/testsuite/test-language.c b/testsuite/test-language.c
index b1b7eeb9..29b40a21 100644
--- a/testsuite/test-language.c
+++ b/testsuite/test-language.c
@@ -78,26 +78,53 @@ test_fixture_teardown (TestFixture   *fixture,
 {
 }
 
+static int
+sort_strv_strcmpptr (gconstpointer a,
+                    gconstpointer b,
+                    gpointer      user_data)
+{
+       const char *stra = *(const char **)a;
+       const char *strb = *(const char **)b;
+
+       return g_strcmp0 (stra, strb);
+}
+
+static char **
+sort_strv (const char * const *strv)
+{
+       char **copy = g_strdupv ((char **)strv);
+       gsize n_elements = g_strv_length (copy);
+       g_qsort_with_data (copy, n_elements, sizeof (char *), sort_strv_strcmpptr, NULL);
+       return copy;
+}
+
 static void
-compare_strv_unordered (const gchar **strv,
-                        const gchar **expected_strv)
+compare_strv_unordered (const gchar * const *strv,
+                        const gchar * const *expected_strv)
 {
-       if (expected_strv != NULL)
+       char **strv_sorted;
+       char **expected_strv_sorted;
+
+       if (expected_strv == NULL)
        {
-               guint i;
+               g_assert_true (strv == NULL || strv[0] == NULL);
+               return;
+       }
 
-               g_assert_cmpuint (g_strv_length ((gchar **) strv), ==,
-                                 g_strv_length ((gchar **) expected_strv));
+       g_assert_nonnull (strv);
 
-               for (i = 0; expected_strv[i] != NULL; i++)
-               {
-                       g_assert_true (g_strv_contains (strv, expected_strv[i]));
-               }
-       }
-       else
+       g_assert_cmpint (g_strv_length ((char **)strv), ==, g_strv_length ((char **)expected_strv));
+
+       strv_sorted = sort_strv (strv);
+       expected_strv_sorted = sort_strv (strv);
+
+       for (guint i = 0; strv_sorted[i]; i++)
        {
-               g_assert_null (strv);
+               g_assert_cmpstr (strv_sorted[i], ==, expected_strv_sorted[i]);
        }
+
+       g_strfreev (strv_sorted);
+       g_strfreev (expected_strv_sorted);
 }
 
 static void


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