GQueue: accept a NULL sibling for insert_before() and insert_after()
[platform/upstream/glib.git] / glib / tests / queue.c
index 3aeea56..4f5e753 100644 (file)
@@ -498,8 +498,9 @@ random_test (gconstpointer d)
         case REMOVE:
           if (!g_queue_is_empty (q))
             g_queue_remove (q, qinf->tail->data);
+          /* qinf->head/qinf->tail may be invalid at this point */
           if (!g_queue_is_empty (q))
-            g_queue_remove (q, qinf->head->data);
+            g_queue_remove (q, q->head->data);
           if (!g_queue_is_empty (q))
             g_queue_remove (q, g_queue_peek_nth (q, get_random_position (q, TRUE)));
 
@@ -510,8 +511,9 @@ random_test (gconstpointer d)
         case REMOVE_ALL:
           if (!g_queue_is_empty (q))
             g_queue_remove_all (q, qinf->tail->data);
+          /* qinf->head/qinf->tail may be invalid at this point */
           if (!g_queue_is_empty (q))
-            g_queue_remove_all (q, qinf->head->data);
+            g_queue_remove_all (q, q->head->data);
           if (!g_queue_is_empty (q))
             g_queue_remove_all (q, g_queue_peek_nth (q, get_random_position (q, TRUE)));
 
@@ -527,6 +529,7 @@ random_test (gconstpointer d)
               g_queue_insert_before (q, qinf->tail, x);
               g_queue_insert_before (q, qinf->head, x);
               g_queue_insert_before (q, g_queue_find (q, x), x);
+              g_queue_insert_before (q, NULL, x);
             }
           qinf->head = q->head;
           qinf->tail = q->tail;
@@ -540,6 +543,7 @@ random_test (gconstpointer d)
               g_queue_insert_after (q, qinf->tail, x);
               g_queue_insert_after (q, qinf->head, x);
               g_queue_insert_after (q, g_queue_find (q, x), x);
+              g_queue_insert_after (q, NULL, x);
             }
           qinf->head = q->head;
           qinf->tail = q->tail;
@@ -1028,6 +1032,55 @@ test_clear (void)
   g_queue_free (q);
 }
 
+typedef struct
+{
+  gboolean freed;
+  int x;
+} QueueItem;
+
+static void
+free_func (gpointer data)
+{
+  QueueItem *item = data;
+
+  item->freed = TRUE;
+}
+
+static QueueItem *
+new_item (int x)
+{
+  QueueItem *item;
+
+  item = g_slice_new (QueueItem);
+  item->freed = FALSE;
+  item->x = x;
+
+  return item;
+}
+
+static void
+test_free_full (void)
+{
+  QueueItem *one, *two, *three;
+  GQueue *queue = NULL;
+
+  queue = g_queue_new();
+  g_queue_push_tail (queue, one = new_item (1));
+  g_queue_push_tail (queue, two = new_item (2));
+  g_queue_push_tail (queue, three = new_item (3));
+  g_assert (!one->freed);
+  g_assert (!two->freed);
+  g_assert (!three->freed);
+  g_queue_free_full (queue, free_func);
+  g_assert (one->freed);
+  g_assert (two->freed);
+  g_assert (three->freed);
+  g_slice_free (QueueItem, one);
+  g_slice_free (QueueItem, two);
+  g_slice_free (QueueItem, three);
+}
+
+
 int main (int argc, char *argv[])
 {
   guint32 seed;
@@ -1041,6 +1094,7 @@ int main (int argc, char *argv[])
   g_test_add_func ("/queue/find-custom", test_find_custom);
   g_test_add_func ("/queue/static", test_static);
   g_test_add_func ("/queue/clear", test_clear);
+  g_test_add_func ("/queue/free-full", test_free_full);
 
   seed = g_test_rand_int_range (0, G_MAXINT);
   path = g_strdup_printf ("/queue/random/seed:%u", seed);