[glib] Add unit test for g_sequence_lookup() and g_sequence_lookup_iter().



commit 50f96ae79b329154e55ac63b1ab8b0ff650bf871
Author: Xavier Claessens <xclaesse gmail com>
Date:   Fri Dec 10 10:14:06 2010 +0100

    Add unit test for g_sequence_lookup() and g_sequence_lookup_iter().

 glib/tests/sequence.c |   76 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 76 insertions(+), 0 deletions(-)
---
diff --git a/glib/tests/sequence.c b/glib/tests/sequence.c
index f46761a..e2a58f3 100644
--- a/glib/tests/sequence.c
+++ b/glib/tests/sequence.c
@@ -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,
@@ -200,6 +201,42 @@ seq_foreach (gpointer data,
 }
 
 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,
                gpointer      data)
@@ -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:



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