Update contrib.
1 /* Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.*/
2 #undef G_DISABLE_ASSERT
14 #include "mrt2_glib2_test.h"
20 static gboolean verbose = FALSE;
24 check_integrity (GQueue *queue)
32 g_assert (queue->length < 4000000000u);
34 g_assert (g_queue_get_length (queue) == queue->length);
37 g_assert (!queue->tail);
39 g_assert (!queue->head);
43 for (list = queue->head; list != NULL; list = list->next)
49 g_assert (n == queue->length);
50 g_assert (last == queue->tail);
54 for (list = queue->tail; list != NULL; list = list->prev)
60 g_assert (n == queue->length);
61 g_assert (last == queue->head);
64 for (list = queue->head; list != NULL; list = list->next)
65 links = g_list_prepend (links, list);
68 for (list = queue->tail; list != NULL; list = list->prev)
70 g_assert (list == link->data);
76 for (list = queue->tail; list != NULL; list = list->prev)
77 links = g_list_prepend (links, list);
80 for (list = queue->head; list != NULL; list = list->next)
82 g_assert (list == link->data);
91 return g_random_int_range (0, 2);
95 check_max (gpointer elm, gpointer user_data)
97 gint *best = user_data;
98 gint element = GPOINTER_TO_INT (elm);
105 check_min (gpointer elm, gpointer user_data)
107 gint *best = user_data;
108 gint element = GPOINTER_TO_INT (elm);
115 find_min (GQueue *queue)
119 g_queue_foreach (queue, check_min, &min);
125 find_max (GQueue *queue)
129 g_queue_foreach (queue, check_max, &max);
135 delete_elm (gpointer elm, gpointer user_data)
137 g_queue_remove ((GQueue *)user_data, elm);
138 check_integrity ((GQueue *)user_data);
142 delete_all (GQueue *queue)
144 g_queue_foreach (queue, delete_elm, queue);
148 compare_int (gconstpointer a, gconstpointer b, gpointer data)
150 int ai = GPOINTER_TO_INT (a);
151 int bi = GPOINTER_TO_INT (b);
162 get_random_position (GQueue *queue, gboolean allow_offlist)
165 enum { OFF_QUEUE, HEAD, TAIL, MIDDLE, LAST } where;
168 where = g_random_int_range (OFF_QUEUE, LAST);
170 where = g_random_int_range (HEAD, LAST);
186 n = queue->length - 1;
190 if (queue->length == 0)
193 n = g_random_int_range (0, queue->length);
197 g_assert_not_reached();
207 random_test (int seed)
210 IS_EMPTY, GET_LENGTH, REVERSE, COPY,
211 FOREACH, FIND, FIND_CUSTOM, SORT,
212 PUSH_HEAD, PUSH_TAIL, PUSH_NTH, POP_HEAD,
213 POP_TAIL, POP_NTH, PEEK_HEAD, PEEK_TAIL,
214 PEEK_NTH, INDEX, REMOVE, REMOVE_ALL,
215 INSERT_BEFORE, INSERT_AFTER, INSERT_SORTED, PUSH_HEAD_LINK,
216 PUSH_TAIL_LINK, PUSH_NTH_LINK, POP_HEAD_LINK, POP_TAIL_LINK,
217 POP_NTH_LINK, PEEK_HEAD_LINK, PEEK_TAIL_LINK, PEEK_NTH_LINK,
218 LINK_INDEX, UNLINK, DELETE_LINK, LAST_OP
221 #define N_ITERATIONS 500
224 #define RANDOM_QUEUE() &(queues[g_random_int_range(0, N_QUEUES)])
226 typedef struct QueueInfo QueueInfo;
237 QueueInfo queues[N_QUEUES];
240 g_print ("seed: %d\n", seed);
242 g_random_set_seed (seed);
244 for (i = 0; i < N_QUEUES; ++i)
246 queues[i].queue = g_queue_new ();
247 queues[i].head = NULL;
248 queues[i].tail = NULL;
249 queues[i].length = 0;
252 for (i = 0; i < N_ITERATIONS; ++i)
255 QueueInfo *qinf = RANDOM_QUEUE();
256 GQueue *q = qinf->queue;
257 op = g_random_int_range (IS_EMPTY, LAST_OP);
259 g_assert (qinf->head == q->head);
260 g_assert (qinf->tail == q->tail);
261 g_assert (qinf->length == q->length);
267 if (g_queue_is_empty (qinf->queue))
269 g_assert (q->head == NULL);
270 g_assert (q->tail == NULL);
271 g_assert (q->length == 0);
277 g_assert (q->length > 0);
285 l = g_queue_get_length (q);
287 g_assert (qinf->length == q->length);
288 g_assert (qinf->length == l);
293 g_assert (qinf->tail == q->head);
294 g_assert (qinf->head == q->tail);
295 g_assert (qinf->length == q->length);
296 qinf->tail = q->tail;
297 qinf->head = q->head;
301 QueueInfo *random_queue = RANDOM_QUEUE();
302 GQueue *new_queue = g_queue_copy (random_queue->queue);
304 g_queue_free (qinf->queue);
305 q = qinf->queue = new_queue;
306 qinf->head = new_queue->head;
307 qinf->tail = g_list_last (new_queue->head);
308 qinf->length = new_queue->length;
319 gboolean find_existing = rnd_bool ();
320 int first = find_max (q);
321 int second = find_min (q);
324 find_existing = FALSE;
333 g_assert (g_queue_find (q, GINT_TO_POINTER (first)));
334 g_assert (g_queue_find (q, GINT_TO_POINTER (second)));
338 g_assert (!g_queue_find (q, GINT_TO_POINTER (first)));
339 g_assert (!g_queue_find (q, GINT_TO_POINTER (second)));
347 if (!g_queue_is_empty (q))
349 int max = find_max (q);
350 int min = find_min (q);
351 g_queue_remove_all (q, GINT_TO_POINTER (max));
353 g_queue_remove_all (q, GINT_TO_POINTER (min));
355 g_queue_push_head (q, GINT_TO_POINTER (max));
357 g_queue_push_head (q, GINT_TO_POINTER (min));
358 qinf->length = q->length;
363 g_queue_sort (q, compare_int, NULL);
367 qinf->head = g_queue_find (q, GINT_TO_POINTER (find_min(q)));
368 qinf->tail = g_queue_find (q, GINT_TO_POINTER (find_max(q)));
370 g_assert (qinf->tail == q->tail);
375 int x = g_random_int_range (0, 435435);
376 g_queue_push_head (q, GINT_TO_POINTER (x));
378 qinf->tail = qinf->head = q->head;
380 qinf->head = qinf->head->prev;
386 int x = g_random_int_range (0, 236546);
387 g_queue_push_tail (q, GINT_TO_POINTER (x));
389 qinf->tail = qinf->head = q->head;
391 qinf->tail = qinf->tail->next;
397 int pos = get_random_position (q, TRUE);
398 int x = g_random_int_range (0, 236546);
399 g_queue_push_nth (q, GINT_TO_POINTER (x), pos);
400 if (qinf->head && qinf->head->prev)
401 qinf->head = qinf->head->prev;
403 qinf->head = q->head;
404 if (qinf->tail && qinf->tail->next)
405 qinf->tail = qinf->tail->next;
407 qinf->tail = g_list_last (qinf->head);
413 qinf->head = qinf->head->next;
416 qinf->length = (qinf->length == 0)? 0 : qinf->length - 1;
417 g_queue_pop_head (q);
421 qinf->tail = qinf->tail->prev;
424 qinf->length = (qinf->length == 0)? 0 : qinf->length - 1;
425 g_queue_pop_tail (q);
428 if (!g_queue_is_empty (q))
430 int n = get_random_position (q, TRUE);
431 gpointer elm = g_queue_peek_nth (q, n);
433 if (n == q->length - 1)
434 qinf->tail = qinf->tail->prev;
437 qinf->head = qinf->head->next;
439 if (n >= 0 && n < q->length)
442 g_assert (elm == g_queue_pop_nth (q, n));
447 g_assert (qinf->head->data == g_queue_peek_head (q));
449 g_assert (g_queue_peek_head (q) == NULL);
453 g_assert (qinf->tail->data == g_queue_peek_tail (q));
455 g_assert (g_queue_peek_tail (q) == NULL);
458 if (g_queue_is_empty (q))
460 for (j = -10; j < 10; ++j)
461 g_assert (g_queue_peek_nth (q, j) == NULL);
466 int n = get_random_position (q, TRUE);
467 if (n < 0 || n >= q->length)
469 g_assert (g_queue_peek_nth (q, n) == NULL);
474 for (j = 0; j < n; ++j)
477 g_assert (list->data == g_queue_peek_nth (q, n));
484 int x = g_random_int_range (0, 386538);
488 g_queue_remove_all (q, GINT_TO_POINTER (x));
490 g_queue_push_tail (q, GINT_TO_POINTER (x));
492 g_queue_sort (q, compare_int, NULL);
496 for (list = q->head; list != NULL; list = list->next)
498 if (list->data == GINT_TO_POINTER (x))
503 g_assert (g_queue_index (q, GINT_TO_POINTER (x)) ==
504 g_queue_link_index (q, list));
505 g_assert (g_queue_link_index (q, list) == n);
507 qinf->head = q->head;
508 qinf->tail = q->tail;
509 qinf->length = q->length;
513 if (!g_queue_is_empty (q))
514 g_queue_remove (q, qinf->tail->data);
515 if (!g_queue_is_empty (q))
516 g_queue_remove (q, qinf->head->data);
517 if (!g_queue_is_empty (q))
518 g_queue_remove (q, g_queue_peek_nth (q, get_random_position (q, TRUE)));
520 qinf->head = q->head;
521 qinf->tail = q->tail;
522 qinf->length = q->length;
525 if (!g_queue_is_empty (q))
526 g_queue_remove_all (q, qinf->tail->data);
527 if (!g_queue_is_empty (q))
528 g_queue_remove_all (q, qinf->head->data);
529 if (!g_queue_is_empty (q))
530 g_queue_remove_all (q, g_queue_peek_nth (q, get_random_position (q, TRUE)));
532 qinf->head = q->head;
533 qinf->tail = q->tail;
534 qinf->length = q->length;
537 if (!g_queue_is_empty (q))
539 gpointer x = GINT_TO_POINTER (g_random_int_range (0, 386538));
541 g_queue_insert_before (q, qinf->tail, x);
542 g_queue_insert_before (q, qinf->head, x);
543 g_queue_insert_before (q, g_queue_find (q, x), x);
545 qinf->head = q->head;
546 qinf->tail = q->tail;
547 qinf->length = q->length;
550 if (!g_queue_is_empty (q))
552 gpointer x = GINT_TO_POINTER (g_random_int_range (0, 386538));
554 g_queue_insert_after (q, qinf->tail, x);
555 g_queue_insert_after (q, qinf->head, x);
556 g_queue_insert_after (q, g_queue_find (q, x), x);
558 qinf->head = q->head;
559 qinf->tail = q->tail;
560 qinf->length = q->length;
564 int max = find_max (q);
565 int min = find_min (q);
567 if (g_queue_is_empty (q))
573 g_queue_sort (q, compare_int, NULL);
575 g_queue_insert_sorted (q, GINT_TO_POINTER (max + 1), compare_int, NULL);
577 g_assert (GPOINTER_TO_INT (q->tail->data) == max + 1);
578 g_queue_insert_sorted (q, GINT_TO_POINTER (min - 1), compare_int, NULL);
580 g_assert (GPOINTER_TO_INT (q->head->data) == min - 1);
581 qinf->head = q->head;
582 qinf->tail = q->tail;
583 qinf->length = q->length;
588 GList *link = g_list_prepend (NULL, GINT_TO_POINTER (i));
589 g_queue_push_head_link (q, link);
598 GList *link = g_list_prepend (NULL, GINT_TO_POINTER (i));
599 g_queue_push_tail_link (q, link);
608 GList *link = g_list_prepend (NULL, GINT_TO_POINTER (i));
609 gint n = get_random_position (q, TRUE);
610 g_queue_push_nth_link (q, n, link);
612 if (qinf->head && qinf->head->prev)
613 qinf->head = qinf->head->prev;
615 qinf->head = q->head;
616 if (qinf->tail && qinf->tail->next)
617 qinf->tail = qinf->tail->next;
619 qinf->tail = g_list_last (qinf->head);
624 if (!g_queue_is_empty (q))
626 qinf->head = qinf->head->next;
630 g_list_free (g_queue_pop_head_link (q));
634 if (!g_queue_is_empty (q))
636 qinf->tail = qinf->tail->prev;
640 g_list_free (g_queue_pop_tail_link (q));
644 if (g_queue_is_empty (q))
645 g_assert (g_queue_pop_nth_link (q, 200) == NULL);
648 int n = get_random_position (q, FALSE);
650 if (n == g_queue_get_length (q) - 1)
651 qinf->tail = qinf->tail->prev;
654 qinf->head = qinf->head->next;
658 g_list_free (g_queue_pop_nth_link (q, n));
662 if (g_queue_is_empty (q))
663 g_assert (g_queue_peek_head_link (q) == NULL);
665 g_assert (g_queue_peek_head_link (q) == qinf->head);
668 if (g_queue_is_empty (q))
669 g_assert (g_queue_peek_tail_link (q) == NULL);
671 g_assert (g_queue_peek_tail_link (q) == qinf->tail);
674 if (g_queue_is_empty(q))
675 g_assert (g_queue_peek_nth_link (q, 1000) == NULL);
678 gint n = get_random_position (q, FALSE);
682 for (j = 0; j < n; ++j)
685 g_assert (g_queue_peek_nth_link (q, n) == link);
689 if (!g_queue_is_empty (q))
691 gint n = g_random_int_range (0, g_queue_get_length (q));
695 for (j = 0; j < n; ++j)
698 g_queue_unlink (q, link);
703 qinf->head = q->head;
704 qinf->tail = q->tail;
709 if (!g_queue_is_empty (q))
711 gint n = g_random_int_range (0, g_queue_get_length (q));
715 for (j = 0; j < n; ++j)
718 g_queue_delete_link (q, link);
721 qinf->head = q->head;
722 qinf->tail = q->tail;
728 g_assert_not_reached();
732 if (qinf->head != q->head ||
733 qinf->tail != q->tail ||
734 qinf->length != q->length)
735 g_print ("op: %d\n", op);
737 g_assert (qinf->head == q->head);
738 g_assert (qinf->tail == q->tail);
739 g_assert (qinf->length == q->length);
741 for (j = 0; j < N_QUEUES; ++j)
742 check_integrity (queues[j].queue);
745 for (i = 0; i < N_QUEUES; ++i)
746 g_queue_free (queues[i].queue);
750 remove_item (gpointer data, gpointer q)
754 g_queue_remove (queue, data);
757 int main(int argc, gchar *args[])
765 g_log_set_handler (NULL, G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL);
766 g_set_print_handler(mrtPrintHandler);
769 if (argc > 1 && args[1][0] == '-' && args[1][1] == 'v')
774 g_assert (g_queue_is_empty (q) == TRUE);
776 g_queue_push_head (q, GINT_TO_POINTER (2));
778 g_assert (g_queue_peek_head (q) == GINT_TO_POINTER (2));
780 g_assert (g_queue_is_empty (q) == FALSE);
782 g_assert (g_list_length (q->head) == 1);
783 g_assert (q->head == q->tail);
784 g_queue_push_head (q, GINT_TO_POINTER (1));
786 g_assert (q->head->next == q->tail);
787 g_assert (q->tail->prev == q->head);
788 g_assert (g_list_length (q->head) == 2);
790 g_assert (q->tail->data == GINT_TO_POINTER (2));
791 g_assert (q->head->data == GINT_TO_POINTER (1));
793 g_queue_push_tail (q, GINT_TO_POINTER (3));
794 g_assert (g_list_length (q->head) == 3);
795 g_assert (q->head->data == GINT_TO_POINTER (1));
796 g_assert (q->head->next->data == GINT_TO_POINTER (2));
797 g_assert (q->head->next->next == q->tail);
798 g_assert (q->head->next == q->tail->prev);
799 g_assert (q->tail->data == GINT_TO_POINTER (3));
800 g_queue_push_tail (q, GINT_TO_POINTER (4));
802 g_assert (g_list_length (q->head) == 4);
803 g_assert (q->head->data == GINT_TO_POINTER (1));
804 g_assert (g_queue_peek_tail (q) == GINT_TO_POINTER (4));
805 g_queue_push_tail (q, GINT_TO_POINTER (5));
807 g_assert (g_list_length (q->head) == 5);
809 g_assert (g_queue_is_empty (q) == FALSE);
812 g_assert (q->length == 5);
813 g_assert (q->head->prev == NULL);
814 g_assert (q->head->data == GINT_TO_POINTER (1));
815 g_assert (q->head->next->data == GINT_TO_POINTER (2));
816 g_assert (q->head->next->next->data == GINT_TO_POINTER (3));
817 g_assert (q->head->next->next->next->data == GINT_TO_POINTER (4));
818 g_assert (q->head->next->next->next->next->data == GINT_TO_POINTER (5));
819 g_assert (q->head->next->next->next->next->next == NULL);
820 g_assert (q->head->next->next->next->next == q->tail);
821 g_assert (q->tail->data == GINT_TO_POINTER (5));
822 g_assert (q->tail->prev->data == GINT_TO_POINTER (4));
823 g_assert (q->tail->prev->prev->data == GINT_TO_POINTER (3));
824 g_assert (q->tail->prev->prev->prev->data == GINT_TO_POINTER (2));
825 g_assert (q->tail->prev->prev->prev->prev->data == GINT_TO_POINTER (1));
826 g_assert (q->tail->prev->prev->prev->prev->prev == NULL);
827 g_assert (q->tail->prev->prev->prev->prev == q->head);
828 g_assert (g_queue_peek_tail (q) == GINT_TO_POINTER (5));
829 g_assert (g_queue_peek_head (q) == GINT_TO_POINTER (1));
831 g_assert (g_queue_pop_head (q) == GINT_TO_POINTER (1));
833 g_assert (g_list_length (q->head) == 4 && q->length == 4);
834 g_assert (g_queue_pop_tail (q) == GINT_TO_POINTER (5));
836 g_assert (g_list_length (q->head) == 3);
837 g_assert (g_queue_pop_head_link (q)->data == GINT_TO_POINTER (2));
839 g_assert (g_list_length (q->head) == 2);
840 g_assert (g_queue_pop_tail (q) == GINT_TO_POINTER (4));
842 g_assert (g_list_length (q->head) == 1);
843 g_assert (g_queue_pop_head_link (q)->data == GINT_TO_POINTER (3));
845 g_assert (g_list_length (q->head) == 0);
846 g_assert (g_queue_pop_tail (q) == NULL);
848 g_assert (g_list_length (q->head) == 0);
849 g_assert (g_queue_pop_head (q) == NULL);
851 g_assert (g_list_length (q->head) == 0);
853 g_assert (g_queue_is_empty (q) == TRUE);
856 /************************/
858 g_queue_push_head (q, GINT_TO_POINTER (1));
860 g_assert (g_list_length (q->head) == 1 && 1 == q->length);
861 g_queue_push_head (q, GINT_TO_POINTER (2));
863 g_assert (g_list_length (q->head) == 2 && 2 == q->length);
864 g_queue_push_head (q, GINT_TO_POINTER (3));
866 g_assert (g_list_length (q->head) == 3 && 3 == q->length);
867 g_queue_push_head (q, GINT_TO_POINTER (4));
869 g_assert (g_list_length (q->head) == 4 && 4 == q->length);
870 g_queue_push_head (q, GINT_TO_POINTER (5));
872 g_assert (g_list_length (q->head) == 5 && 5 == q->length);
874 g_assert (g_queue_pop_head (q) == GINT_TO_POINTER (5));
876 g_assert (g_list_length (q->head) == 4);
878 g_assert (node == g_queue_pop_tail_link (q));
880 g_assert (g_list_length (q->head) == 3);
881 data = q->head->data;
882 g_assert (data == g_queue_pop_head (q));
884 g_assert (g_list_length (q->head) == 2);
885 g_assert (g_queue_pop_tail (q) == GINT_TO_POINTER (2));
887 g_assert (g_list_length (q->head) == 1);
888 g_assert (q->head == q->tail);
889 g_assert (g_queue_pop_tail (q) == GINT_TO_POINTER (3));
891 g_assert (g_list_length (q->head) == 0);
892 g_assert (g_queue_pop_head (q) == NULL);
894 g_assert (g_queue_pop_head_link (q) == NULL);
896 g_assert (g_list_length (q->head) == 0);
897 g_assert (g_queue_pop_tail_link (q) == NULL);
899 g_assert (g_list_length (q->head) == 0);
904 g_assert (g_list_length (q->head) == 0);
906 q2 = g_queue_copy (q);
908 check_integrity (q2);
909 g_assert (g_list_length (q->head) == 0);
910 g_assert (g_list_length (q2->head) == 0);
911 g_queue_sort (q, compare_int, NULL);
912 check_integrity (q2);
914 g_queue_sort (q2, compare_int, NULL);
915 check_integrity (q2);
918 for (i = 0; i < 200; ++i)
920 g_queue_push_nth (q, GINT_TO_POINTER (i), i);
921 g_assert (g_queue_find (q, GINT_TO_POINTER (i)));
923 check_integrity (q2);
926 for (i = 0; i < 200; ++i)
928 g_queue_remove (q, GINT_TO_POINTER (i));
930 check_integrity (q2);
933 for (i = 0; i < 200; ++i)
935 GList *l = g_list_prepend (NULL, GINT_TO_POINTER (i));
937 g_queue_push_nth_link (q, i, l);
939 check_integrity (q2);
942 check_integrity (q2);
946 q2 = g_queue_copy (q);
948 g_queue_foreach (q2, remove_item, q2);
949 check_integrity (q2);
952 /* some checks for off by one errors */
953 g_queue_push_tail (q, GINT_TO_POINTER (1234));
955 node = g_queue_peek_tail_link (q);
956 g_assert (node != NULL && node->data == GINT_TO_POINTER (1234));
957 node = g_queue_peek_nth_link (q, g_queue_get_length (q));
958 g_assert (node == NULL);
959 node = g_queue_peek_nth_link (q, g_queue_get_length (q) - 1);
960 g_assert (node->data == GINT_TO_POINTER (1234));
961 node = g_queue_pop_nth_link (q, g_queue_get_length (q));
962 g_assert (node == NULL);
963 node = g_queue_pop_nth_link (q, g_queue_get_length (q) - 1);
964 g_assert (node != NULL && node->data == GINT_TO_POINTER (1234));
968 if (argc > 2 && args[1][0] == '-' && args[1][1] == 'v')
969 random_test (strtol (args[2], NULL, 0));
971 random_test (strtol (args[1], NULL, 0));
973 random_test (time (0));
975 testResultXml("queue-test");
976 #endif /* EMULATOR */