[glib] tests: add test for GListStore inserted sort



commit 26af7c152f602896cabf9ab6cb6ba42a47a5b992
Author: Ryan Lortie <desrt desrt ca>
Date:   Tue Feb 3 13:42:59 2015 +0100

    tests: add test for GListStore inserted sort
    
    https://bugzilla.gnome.org/show_bug.cgi?id=743927

 gio/tests/glistmodel.c |   96 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 96 insertions(+), 0 deletions(-)
---
diff --git a/gio/tests/glistmodel.c b/gio/tests/glistmodel.c
index 728a2fa..6c35127 100644
--- a/gio/tests/glistmodel.c
+++ b/gio/tests/glistmodel.c
@@ -19,6 +19,8 @@
 
 #include <gio/gio.h>
 
+#include <string.h>
+
 static void
 test_store_boundaries (void)
 {
@@ -121,12 +123,106 @@ test_store_refcounts (void)
     g_assert_null (items[i]);
 }
 
+static gchar *
+make_random_string (void)
+{
+  gchar *str = g_malloc (10);
+  gint i;
+
+  for (i = 0; i < 9; i++)
+    str[i] = g_test_rand_int_range ('a', 'z');
+  str[i] = '\0';
+
+  return str;
+}
+
+static gint
+compare_items (gconstpointer a_p,
+               gconstpointer b_p,
+               gpointer      user_data)
+{
+  GObject *a_o = (GObject *) a_p;
+  GObject *b_o = (GObject *) b_p;
+
+  gchar *a = g_object_get_data (a_o, "key");
+  gchar *b = g_object_get_data (b_o, "key");
+
+  g_assert (user_data == GUINT_TO_POINTER(0x1234u));
+
+  return strcmp (a, b);
+}
+
+static void
+insert_string (GListStore  *store,
+               const gchar *str)
+{
+  GObject *obj;
+
+  obj = g_object_new (G_TYPE_OBJECT, NULL);
+  g_object_set_data_full (obj, "key", g_strdup (str), g_free);
+
+  g_list_store_insert_sorted (store, obj, compare_items, GUINT_TO_POINTER(0x1234u));
+
+  g_object_unref (obj);
+}
+
+static void
+test_store_sorted (void)
+{
+  GListStore *store;
+  guint i;
+
+  store = g_list_store_new (G_TYPE_OBJECT);
+
+  for (i = 0; i < 1000; i++)
+    {
+      gchar *str = make_random_string ();
+      insert_string (store, str);
+      insert_string (store, str); /* multiple copies of the same are OK */
+      g_free (str);
+    }
+
+  g_assert_cmpint (g_list_model_get_n_items (G_LIST_MODEL (store)), ==, 2000);
+
+  for (i = 0; i < 1000; i++)
+    {
+      GObject *a, *b;
+
+      /* should see our two copies */
+      a = g_list_model_get_item (G_LIST_MODEL (store), i * 2);
+      b = g_list_model_get_item (G_LIST_MODEL (store), i * 2 + 1);
+
+      g_assert (compare_items (a, b, GUINT_TO_POINTER(0x1234)) == 0);
+      g_assert (a != b);
+
+      if (i)
+        {
+          GObject *c;
+
+          c = g_list_model_get_item (G_LIST_MODEL (store), i * 2 - 1);
+          g_assert (c != a);
+          g_assert (c != b);
+
+          g_assert (compare_items (b, c, GUINT_TO_POINTER(0x1234)) > 0);
+          g_assert (compare_items (a, c, GUINT_TO_POINTER(0x1234)) > 0);
+
+          g_object_unref (c);
+        }
+
+      g_object_unref (a);
+      g_object_unref (b);
+    }
+
+  g_object_unref (store);
+}
+
 int main (int argc, char *argv[])
 {
   g_test_init (&argc, &argv, NULL);
 
   g_test_add_func ("/glistmodel/store/boundaries", test_store_boundaries);
   g_test_add_func ("/glistmodel/store/refcounts", test_store_refcounts);
+  g_test_add_func ("/glistmodel/store/sorted", test_store_sorted);
 
   return g_test_run ();
 }


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