Add unit test for g_sequence_lookup() and g_sequence_lookup_iter().
authorXavier Claessens <xclaesse@gmail.com>
Fri, 10 Dec 2010 09:14:06 +0000 (10:14 +0100)
committerXavier Claessens <xclaesse@gmail.com>
Mon, 20 Dec 2010 16:30:58 +0000 (17:30 +0100)
glib/tests/sequence.c

index f46761a907101a285325a4082ecb6db85b03fb34..e2a58f3338739a13c1a8b36dab0aa0323a768f72 100644 (file)
@@ -81,6 +81,7 @@ enum {
   GET_BEGIN_ITER, GET_END_ITER, GET_ITER_AT_POS, APPEND, PREPEND,
   INSERT_BEFORE, MOVE, SWAP, INSERT_SORTED, INSERT_SORTED_ITER, SORT_CHANGED,
   SORT_CHANGED_ITER, REMOVE, REMOVE_RANGE, MOVE_RANGE, SEARCH, SEARCH_ITER,
+  LOOKUP, LOOKUP_ITER,
 
   /* dereferencing */
   GET, SET,
@@ -199,6 +200,42 @@ seq_foreach (gpointer data,
   *link = (*link)->next;
 }
 
+static gint
+simple_items_cmp (gconstpointer a,
+                 gconstpointer b,
+                 gpointer data)
+{
+  const Item *item_a = fix_pointer (a);
+  const Item *item_b = fix_pointer (b);
+
+  if (item_a->number > item_b->number)
+    return +1;
+  else if (item_a->number < item_b->number)
+    return -1;
+  else
+    return 0;
+}
+
+static gint
+simple_iters_cmp (gconstpointer a,
+                 gconstpointer b,
+                 gpointer data)
+{
+  GSequence *seq = data;
+  GSequenceIter *iter_a = (GSequenceIter *)a;
+  GSequenceIter *iter_b = (GSequenceIter *)b;
+  gpointer item_a = g_sequence_get (iter_a);
+  gpointer item_b = g_sequence_get (iter_b);
+
+  if (seq)
+    {
+      g_assert (g_sequence_iter_get_sequence (iter_a) == seq);
+      g_assert (g_sequence_iter_get_sequence (iter_b) == seq);
+    }
+
+  return simple_items_cmp (item_a, item_b, data);
+}
+
 static gint
 compare_items (gconstpointer a,
                gconstpointer b,
@@ -884,6 +921,45 @@ run_random_tests (gconstpointer d)
             g_queue_insert_sorted (seq->queue, insert_iter, compare_iters, NULL);
           }
           break;
+        case LOOKUP:
+          {
+            Item *item;
+            GSequenceIter *lookup_iter;
+            GSequenceIter *insert_iter;
+
+            g_sequence_sort (seq->sequence, compare_items, NULL);
+            g_queue_sort (seq->queue, compare_iters, NULL);
+
+            check_sorted (seq);
+
+            item = new_item (seq);
+            insert_iter = g_sequence_insert_sorted (seq->sequence, item, compare_items, NULL);
+            g_queue_insert_sorted (seq->queue, insert_iter, compare_iters, NULL);
+
+            lookup_iter = g_sequence_lookup (seq->sequence, item, simple_items_cmp, NULL);
+            g_assert (simple_iters_cmp (insert_iter, lookup_iter, NULL) == 0);
+          }
+          break;
+        case LOOKUP_ITER:
+          {
+            Item *item;
+            GSequenceIter *lookup_iter;
+            GSequenceIter *insert_iter;
+
+            g_sequence_sort (seq->sequence, compare_items, NULL);
+            g_queue_sort (seq->queue, compare_iters, NULL);
+
+            check_sorted (seq);
+
+            item = new_item (seq);
+            insert_iter = g_sequence_insert_sorted (seq->sequence, item, compare_items, NULL);
+            g_queue_insert_sorted (seq->queue, insert_iter, compare_iters, NULL);
+
+            lookup_iter = g_sequence_lookup_iter (seq->sequence, item,
+                (GSequenceIterCompareFunc) simple_iters_cmp, NULL);
+            g_assert (simple_iters_cmp (insert_iter, lookup_iter, NULL) == 0);
+          }
+          break;
 
           /* dereferencing */
         case GET: