1 #undef G_DISABLE_ASSERT
10 static gboolean verbose = FALSE;
14 check_integrity (GQueue *queue)
22 g_assert (queue->length < 4000000000u);
24 g_assert (g_queue_get_length (queue) == queue->length);
27 g_assert (!queue->tail);
29 g_assert (!queue->head);
33 for (list = queue->head; list != NULL; list = list->next)
39 g_assert (n == queue->length);
40 g_assert (last == queue->tail);
44 for (list = queue->tail; list != NULL; list = list->prev)
50 g_assert (n == queue->length);
51 g_assert (last == queue->head);
54 for (list = queue->head; list != NULL; list = list->next)
55 links = g_list_prepend (links, list);
58 for (list = queue->tail; list != NULL; list = list->prev)
60 g_assert (list == link->data);
66 for (list = queue->tail; list != NULL; list = list->prev)
67 links = g_list_prepend (links, list);
70 for (list = queue->head; list != NULL; list = list->next)
72 g_assert (list == link->data);
81 return g_random_int_range (0, 2);
85 check_max (gpointer elm, gpointer user_data)
87 gint *best = user_data;
88 gint element = GPOINTER_TO_INT (elm);
95 check_min (gpointer elm, gpointer user_data)
97 gint *best = user_data;
98 gint element = GPOINTER_TO_INT (elm);
105 find_min (GQueue *queue)
109 g_queue_foreach (queue, check_min, &min);
115 find_max (GQueue *queue)
119 g_queue_foreach (queue, check_max, &max);
125 delete_elm (gpointer elm, gpointer user_data)
127 g_queue_remove ((GQueue *)user_data, elm);
128 check_integrity ((GQueue *)user_data);
132 delete_all (GQueue *queue)
134 g_queue_foreach (queue, delete_elm, queue);
138 compare_int (gconstpointer a, gconstpointer b, gpointer data)
140 int ai = GPOINTER_TO_INT (a);
141 int bi = GPOINTER_TO_INT (b);
152 get_random_position (GQueue *queue, gboolean allow_offlist)
155 enum { OFF_QUEUE, HEAD, TAIL, MIDDLE, LAST } where;
158 where = g_random_int_range (OFF_QUEUE, LAST);
160 where = g_random_int_range (HEAD, LAST);
176 n = queue->length - 1;
180 if (queue->length == 0)
183 n = g_random_int_range (0, queue->length);
187 g_assert_not_reached();
197 random_test (int seed)
200 IS_EMPTY, GET_LENGTH, REVERSE, COPY,
201 FOREACH, FIND, FIND_CUSTOM, SORT,
202 PUSH_HEAD, PUSH_TAIL, PUSH_NTH, POP_HEAD,
203 POP_TAIL, POP_NTH, PEEK_HEAD, PEEK_TAIL,
204 PEEK_NTH, INDEX, REMOVE, REMOVE_ALL,
205 INSERT_BEFORE, INSERT_AFTER, INSERT_SORTED, PUSH_HEAD_LINK,
206 PUSH_TAIL_LINK, PUSH_NTH_LINK, POP_HEAD_LINK, POP_TAIL_LINK,
207 POP_NTH_LINK, PEEK_HEAD_LINK, PEEK_TAIL_LINK, PEEK_NTH_LINK,
208 LINK_INDEX, UNLINK, DELETE_LINK, LAST_OP
211 #define N_ITERATIONS 500000
214 #define RANDOM_QUEUE() &(queues[g_random_int_range(0, N_QUEUES)])
216 typedef struct QueueInfo QueueInfo;
227 QueueInfo queues[N_QUEUES];
230 g_print ("seed: %d\n", seed);
232 g_random_set_seed (seed);
234 for (i = 0; i < N_QUEUES; ++i)
236 queues[i].queue = g_queue_new ();
237 queues[i].head = NULL;
238 queues[i].tail = NULL;
239 queues[i].length = 0;
242 for (i = 0; i < N_ITERATIONS; ++i)
245 QueueInfo *qinf = RANDOM_QUEUE();
246 GQueue *q = qinf->queue;
247 op = g_random_int_range (IS_EMPTY, LAST_OP);
249 g_assert (qinf->head == q->head);
250 g_assert (qinf->tail == q->tail);
251 g_assert (qinf->length == q->length);
257 if (g_queue_is_empty (qinf->queue))
259 g_assert (q->head == NULL);
260 g_assert (q->tail == NULL);
261 g_assert (q->length == 0);
267 g_assert (q->length > 0);
275 l = g_queue_get_length (q);
277 g_assert (qinf->length == q->length);
278 g_assert (qinf->length == l);
283 g_assert (qinf->tail == q->head);
284 g_assert (qinf->head == q->tail);
285 g_assert (qinf->length == q->length);
286 qinf->tail = q->tail;
287 qinf->head = q->head;
291 QueueInfo *random_queue = RANDOM_QUEUE();
292 GQueue *new_queue = g_queue_copy (random_queue->queue);
294 g_queue_free (qinf->queue);
295 q = qinf->queue = new_queue;
296 qinf->head = new_queue->head;
297 qinf->tail = g_list_last (new_queue->head);
298 qinf->length = new_queue->length;
309 gboolean find_existing = rnd_bool ();
310 int first = find_max (q);
311 int second = find_min (q);
314 find_existing = FALSE;
323 g_assert (g_queue_find (q, GINT_TO_POINTER (first)));
324 g_assert (g_queue_find (q, GINT_TO_POINTER (second)));
328 g_assert (!g_queue_find (q, GINT_TO_POINTER (first)));
329 g_assert (!g_queue_find (q, GINT_TO_POINTER (second)));
337 if (!g_queue_is_empty (q))
339 int max = find_max (q);
340 int min = find_min (q);
341 g_queue_remove_all (q, GINT_TO_POINTER (max));
343 g_queue_remove_all (q, GINT_TO_POINTER (min));
345 g_queue_push_head (q, GINT_TO_POINTER (max));
347 g_queue_push_head (q, GINT_TO_POINTER (min));
348 qinf->length = q->length;
353 g_queue_sort (q, compare_int, NULL);
357 qinf->head = g_queue_find (q, GINT_TO_POINTER (find_min(q)));
358 qinf->tail = g_queue_find (q, GINT_TO_POINTER (find_max(q)));
360 g_assert (qinf->tail == q->tail);
365 int x = g_random_int_range (0, 435435);
366 g_queue_push_head (q, GINT_TO_POINTER (x));
368 qinf->tail = qinf->head = q->head;
370 qinf->head = qinf->head->prev;
376 int x = g_random_int_range (0, 236546);
377 g_queue_push_tail (q, GINT_TO_POINTER (x));
379 qinf->tail = qinf->head = q->head;
381 qinf->tail = qinf->tail->next;
387 int pos = get_random_position (q, TRUE);
388 int x = g_random_int_range (0, 236546);
389 g_queue_push_nth (q, GINT_TO_POINTER (x), pos);
390 if (qinf->head && qinf->head->prev)
391 qinf->head = qinf->head->prev;
393 qinf->head = q->head;
394 if (qinf->tail && qinf->tail->next)
395 qinf->tail = qinf->tail->next;
397 qinf->tail = g_list_last (qinf->head);
403 qinf->head = qinf->head->next;
406 qinf->length = (qinf->length == 0)? 0 : qinf->length - 1;
407 g_queue_pop_head (q);
411 qinf->tail = qinf->tail->prev;
414 qinf->length = (qinf->length == 0)? 0 : qinf->length - 1;
415 g_queue_pop_tail (q);
418 if (!g_queue_is_empty (q))
420 int n = get_random_position (q, TRUE);
421 gpointer elm = g_queue_peek_nth (q, n);
423 if (n == q->length - 1)
424 qinf->tail = qinf->tail->prev;
427 qinf->head = qinf->head->next;
429 if (n >= 0 && n < q->length)
432 g_assert (elm == g_queue_pop_nth (q, n));
437 g_assert (qinf->head->data == g_queue_peek_head (q));
439 g_assert (g_queue_peek_head (q) == NULL);
443 g_assert (qinf->tail->data == g_queue_peek_tail (q));
445 g_assert (g_queue_peek_tail (q) == NULL);
448 if (g_queue_is_empty (q))
450 for (j = -10; j < 10; ++j)
451 g_assert (g_queue_peek_nth (q, j) == NULL);
456 int n = get_random_position (q, TRUE);
457 if (n < 0 || n >= q->length)
459 g_assert (g_queue_peek_nth (q, n) == NULL);
464 for (j = 0; j < n; ++j)
467 g_assert (list->data == g_queue_peek_nth (q, n));
474 int x = g_random_int_range (0, 386538);
478 g_queue_remove_all (q, GINT_TO_POINTER (x));
480 g_queue_push_tail (q, GINT_TO_POINTER (x));
482 g_queue_sort (q, compare_int, NULL);
486 for (list = q->head; list != NULL; list = list->next)
488 if (list->data == GINT_TO_POINTER (x))
493 g_assert (g_queue_index (q, GINT_TO_POINTER (x)) ==
494 g_queue_link_index (q, list));
495 g_assert (g_queue_link_index (q, list) == n);
497 qinf->head = q->head;
498 qinf->tail = q->tail;
499 qinf->length = q->length;
503 if (!g_queue_is_empty (q))
504 g_queue_remove (q, qinf->tail->data);
505 if (!g_queue_is_empty (q))
506 g_queue_remove (q, qinf->head->data);
507 if (!g_queue_is_empty (q))
508 g_queue_remove (q, g_queue_peek_nth (q, get_random_position (q, TRUE)));
510 qinf->head = q->head;
511 qinf->tail = q->tail;
512 qinf->length = q->length;
515 if (!g_queue_is_empty (q))
516 g_queue_remove_all (q, qinf->tail->data);
517 if (!g_queue_is_empty (q))
518 g_queue_remove_all (q, qinf->head->data);
519 if (!g_queue_is_empty (q))
520 g_queue_remove_all (q, g_queue_peek_nth (q, get_random_position (q, TRUE)));
522 qinf->head = q->head;
523 qinf->tail = q->tail;
524 qinf->length = q->length;
527 if (!g_queue_is_empty (q))
529 gpointer x = GINT_TO_POINTER (g_random_int_range (0, 386538));
531 g_queue_insert_before (q, qinf->tail, x);
532 g_queue_insert_before (q, qinf->head, x);
533 g_queue_insert_before (q, g_queue_find (q, x), x);
535 qinf->head = q->head;
536 qinf->tail = q->tail;
537 qinf->length = q->length;
540 if (!g_queue_is_empty (q))
542 gpointer x = GINT_TO_POINTER (g_random_int_range (0, 386538));
544 g_queue_insert_after (q, qinf->tail, x);
545 g_queue_insert_after (q, qinf->head, x);
546 g_queue_insert_after (q, g_queue_find (q, x), x);
548 qinf->head = q->head;
549 qinf->tail = q->tail;
550 qinf->length = q->length;
554 int max = find_max (q);
555 int min = find_min (q);
557 if (g_queue_is_empty (q))
563 g_queue_sort (q, compare_int, NULL);
565 g_queue_insert_sorted (q, GINT_TO_POINTER (max + 1), compare_int, NULL);
567 g_assert (GPOINTER_TO_INT (q->tail->data) == max + 1);
568 g_queue_insert_sorted (q, GINT_TO_POINTER (min - 1), compare_int, NULL);
570 g_assert (GPOINTER_TO_INT (q->head->data) == min - 1);
571 qinf->head = q->head;
572 qinf->tail = q->tail;
573 qinf->length = q->length;
578 GList *link = g_list_prepend (NULL, GINT_TO_POINTER (i));
579 g_queue_push_head_link (q, link);
588 GList *link = g_list_prepend (NULL, GINT_TO_POINTER (i));
589 g_queue_push_tail_link (q, link);
598 GList *link = g_list_prepend (NULL, GINT_TO_POINTER (i));
599 gint n = get_random_position (q, TRUE);
600 g_queue_push_nth_link (q, n, link);
602 if (qinf->head && qinf->head->prev)
603 qinf->head = qinf->head->prev;
605 qinf->head = q->head;
606 if (qinf->tail && qinf->tail->next)
607 qinf->tail = qinf->tail->next;
609 qinf->tail = g_list_last (qinf->head);
614 if (!g_queue_is_empty (q))
616 qinf->head = qinf->head->next;
620 g_list_free (g_queue_pop_head_link (q));
624 if (!g_queue_is_empty (q))
626 qinf->tail = qinf->tail->prev;
630 g_list_free (g_queue_pop_tail_link (q));
634 if (g_queue_is_empty (q))
635 g_assert (g_queue_pop_nth_link (q, 200) == NULL);
638 int n = get_random_position (q, FALSE);
640 if (n == g_queue_get_length (q) - 1)
641 qinf->tail = qinf->tail->prev;
644 qinf->head = qinf->head->next;
648 g_list_free (g_queue_pop_nth_link (q, n));
652 if (g_queue_is_empty (q))
653 g_assert (g_queue_peek_head_link (q) == NULL);
655 g_assert (g_queue_peek_head_link (q) == qinf->head);
658 if (g_queue_is_empty (q))
659 g_assert (g_queue_peek_tail_link (q) == NULL);
661 g_assert (g_queue_peek_tail_link (q) == qinf->tail);
664 if (g_queue_is_empty(q))
665 g_assert (g_queue_peek_nth_link (q, 1000) == NULL);
668 gint n = get_random_position (q, FALSE);
672 for (j = 0; j < n; ++j)
675 g_assert (g_queue_peek_nth_link (q, n) == link);
679 if (!g_queue_is_empty (q))
681 gint n = g_random_int_range (0, g_queue_get_length (q));
685 for (j = 0; j < n; ++j)
688 g_queue_unlink (q, link);
693 qinf->head = q->head;
694 qinf->tail = q->tail;
699 if (!g_queue_is_empty (q))
701 gint n = g_random_int_range (0, g_queue_get_length (q));
705 for (j = 0; j < n; ++j)
708 g_queue_delete_link (q, link);
711 qinf->head = q->head;
712 qinf->tail = q->tail;
718 g_assert_not_reached();
722 if (qinf->head != q->head ||
723 qinf->tail != q->tail ||
724 qinf->length != q->length)
725 g_print ("op: %d\n", op);
727 g_assert (qinf->head == q->head);
728 g_assert (qinf->tail == q->tail);
729 g_assert (qinf->length == q->length);
731 for (j = 0; j < N_QUEUES; ++j)
732 check_integrity (queues[j].queue);
735 for (i = 0; i < N_QUEUES; ++i)
736 g_queue_free (queues[i].queue);
740 remove_item (gpointer data, gpointer q)
744 g_queue_remove (queue, data);
747 int main(int argc, gchar *args[])
754 if (argc > 1 && args[1][0] == '-' && args[1][1] == 'v')
759 g_assert (g_queue_is_empty (q) == TRUE);
761 g_queue_push_head (q, GINT_TO_POINTER (2));
763 g_assert (g_queue_peek_head (q) == GINT_TO_POINTER (2));
765 g_assert (g_queue_is_empty (q) == FALSE);
767 g_assert (g_list_length (q->head) == 1);
768 g_assert (q->head == q->tail);
769 g_queue_push_head (q, GINT_TO_POINTER (1));
771 g_assert (q->head->next == q->tail);
772 g_assert (q->tail->prev == q->head);
773 g_assert (g_list_length (q->head) == 2);
775 g_assert (q->tail->data == GINT_TO_POINTER (2));
776 g_assert (q->head->data == GINT_TO_POINTER (1));
778 g_queue_push_tail (q, GINT_TO_POINTER (3));
779 g_assert (g_list_length (q->head) == 3);
780 g_assert (q->head->data == GINT_TO_POINTER (1));
781 g_assert (q->head->next->data == GINT_TO_POINTER (2));
782 g_assert (q->head->next->next == q->tail);
783 g_assert (q->head->next == q->tail->prev);
784 g_assert (q->tail->data == GINT_TO_POINTER (3));
785 g_queue_push_tail (q, GINT_TO_POINTER (4));
787 g_assert (g_list_length (q->head) == 4);
788 g_assert (q->head->data == GINT_TO_POINTER (1));
789 g_assert (g_queue_peek_tail (q) == GINT_TO_POINTER (4));
790 g_queue_push_tail (q, GINT_TO_POINTER (5));
792 g_assert (g_list_length (q->head) == 5);
794 g_assert (g_queue_is_empty (q) == FALSE);
797 g_assert (q->length == 5);
798 g_assert (q->head->prev == NULL);
799 g_assert (q->head->data == GINT_TO_POINTER (1));
800 g_assert (q->head->next->data == GINT_TO_POINTER (2));
801 g_assert (q->head->next->next->data == GINT_TO_POINTER (3));
802 g_assert (q->head->next->next->next->data == GINT_TO_POINTER (4));
803 g_assert (q->head->next->next->next->next->data == GINT_TO_POINTER (5));
804 g_assert (q->head->next->next->next->next->next == NULL);
805 g_assert (q->head->next->next->next->next == q->tail);
806 g_assert (q->tail->data == GINT_TO_POINTER (5));
807 g_assert (q->tail->prev->data == GINT_TO_POINTER (4));
808 g_assert (q->tail->prev->prev->data == GINT_TO_POINTER (3));
809 g_assert (q->tail->prev->prev->prev->data == GINT_TO_POINTER (2));
810 g_assert (q->tail->prev->prev->prev->prev->data == GINT_TO_POINTER (1));
811 g_assert (q->tail->prev->prev->prev->prev->prev == NULL);
812 g_assert (q->tail->prev->prev->prev->prev == q->head);
813 g_assert (g_queue_peek_tail (q) == GINT_TO_POINTER (5));
814 g_assert (g_queue_peek_head (q) == GINT_TO_POINTER (1));
816 g_assert (g_queue_pop_head (q) == GINT_TO_POINTER (1));
818 g_assert (g_list_length (q->head) == 4 && q->length == 4);
819 g_assert (g_queue_pop_tail (q) == GINT_TO_POINTER (5));
821 g_assert (g_list_length (q->head) == 3);
822 g_assert (g_queue_pop_head_link (q)->data == GINT_TO_POINTER (2));
824 g_assert (g_list_length (q->head) == 2);
825 g_assert (g_queue_pop_tail (q) == GINT_TO_POINTER (4));
827 g_assert (g_list_length (q->head) == 1);
828 g_assert (g_queue_pop_head_link (q)->data == GINT_TO_POINTER (3));
830 g_assert (g_list_length (q->head) == 0);
831 g_assert (g_queue_pop_tail (q) == NULL);
833 g_assert (g_list_length (q->head) == 0);
834 g_assert (g_queue_pop_head (q) == NULL);
836 g_assert (g_list_length (q->head) == 0);
838 g_assert (g_queue_is_empty (q) == TRUE);
841 /************************/
843 g_queue_push_head (q, GINT_TO_POINTER (1));
845 g_assert (g_list_length (q->head) == 1 && 1 == q->length);
846 g_queue_push_head (q, GINT_TO_POINTER (2));
848 g_assert (g_list_length (q->head) == 2 && 2 == q->length);
849 g_queue_push_head (q, GINT_TO_POINTER (3));
851 g_assert (g_list_length (q->head) == 3 && 3 == q->length);
852 g_queue_push_head (q, GINT_TO_POINTER (4));
854 g_assert (g_list_length (q->head) == 4 && 4 == q->length);
855 g_queue_push_head (q, GINT_TO_POINTER (5));
857 g_assert (g_list_length (q->head) == 5 && 5 == q->length);
859 g_assert (g_queue_pop_head (q) == GINT_TO_POINTER (5));
861 g_assert (g_list_length (q->head) == 4);
863 g_assert (node == g_queue_pop_tail_link (q));
865 g_assert (g_list_length (q->head) == 3);
866 data = q->head->data;
867 g_assert (data == g_queue_pop_head (q));
869 g_assert (g_list_length (q->head) == 2);
870 g_assert (g_queue_pop_tail (q) == GINT_TO_POINTER (2));
872 g_assert (g_list_length (q->head) == 1);
873 g_assert (q->head == q->tail);
874 g_assert (g_queue_pop_tail (q) == GINT_TO_POINTER (3));
876 g_assert (g_list_length (q->head) == 0);
877 g_assert (g_queue_pop_head (q) == NULL);
879 g_assert (g_queue_pop_head_link (q) == NULL);
881 g_assert (g_list_length (q->head) == 0);
882 g_assert (g_queue_pop_tail_link (q) == NULL);
884 g_assert (g_list_length (q->head) == 0);
889 g_assert (g_list_length (q->head) == 0);
891 q2 = g_queue_copy (q);
893 check_integrity (q2);
894 g_assert (g_list_length (q->head) == 0);
895 g_assert (g_list_length (q2->head) == 0);
896 g_queue_sort (q, compare_int, NULL);
897 check_integrity (q2);
899 g_queue_sort (q2, compare_int, NULL);
900 check_integrity (q2);
903 for (i = 0; i < 200; ++i)
905 g_queue_push_nth (q, GINT_TO_POINTER (i), i);
906 g_assert (g_queue_find (q, GINT_TO_POINTER (i)));
908 check_integrity (q2);
911 for (i = 0; i < 200; ++i)
913 g_queue_remove (q, GINT_TO_POINTER (i));
915 check_integrity (q2);
918 for (i = 0; i < 200; ++i)
920 GList *l = g_list_prepend (NULL, GINT_TO_POINTER (i));
922 g_queue_push_nth_link (q, i, l);
924 check_integrity (q2);
927 check_integrity (q2);
931 q2 = g_queue_copy (q);
933 g_queue_foreach (q2, remove_item, q2);
934 check_integrity (q2);
937 /* some checks for off by one errors */
938 g_queue_push_tail (q, GINT_TO_POINTER (1234));
940 node = g_queue_peek_tail_link (q);
941 g_assert (node != NULL && node->data == GINT_TO_POINTER (1234));
942 node = g_queue_peek_nth_link (q, g_queue_get_length (q));
943 g_assert (node == NULL);
944 node = g_queue_peek_nth_link (q, g_queue_get_length (q) - 1);
945 g_assert (node->data == GINT_TO_POINTER (1234));
946 node = g_queue_pop_nth_link (q, g_queue_get_length (q));
947 g_assert (node == NULL);
948 node = g_queue_pop_nth_link (q, g_queue_get_length (q) - 1);
949 g_assert (node != NULL && node->data == GINT_TO_POINTER (1234));
953 if (argc > 2 && args[1][0] == '-' && args[1][1] == 'v')
954 random_test (strtol (args[2], NULL, 0));
956 random_test (strtol (args[1], NULL, 0));
958 random_test (time (0));