5 /* Keep this in sync with gsequence.c !!! */
6 typedef struct _GSequenceNode GSequenceNode
;
10 GSequenceNode
* end_node
;
11 GDestroyNotify data_destroy_notify
;
12 gboolean access_prohibited
;
13 GSequence
* real_sequence
;
19 GSequenceNode
* parent
;
21 GSequenceNode
* right
;
26 get_priority (GSequenceNode
*node
)
28 guint key
= GPOINTER_TO_UINT (node
);
30 key
= (key
<< 15) - key
- 1;
31 key
= key
^ (key
>> 12);
32 key
= key
+ (key
<< 2);
33 key
= key
^ (key
>> 4);
34 key
= key
+ (key
<< 3) + (key
<< 11);
35 key
= key
^ (key
>> 16);
41 check_node (GSequenceNode
*node
)
45 g_assert (node
->parent
!= node
);
47 g_assert (node
->parent
->left
== node
|| node
->parent
->right
== node
);
48 g_assert (node
->n_nodes
== 1 + (node
->left
? node
->left
->n_nodes
: 0) + (node
->right
? node
->right
->n_nodes
: 0));
50 g_assert (get_priority (node
) >= get_priority (node
->left
));
52 g_assert (get_priority (node
) >= get_priority (node
->right
));
53 check_node (node
->left
);
54 check_node (node
->right
);
59 g_sequence_check (GSequence
*seq
)
61 GSequenceNode
*node
= seq
->end_node
;
71 g_assert (seq
->end_node
== node
);
72 g_assert (node
->data
== seq
);
78 NEW
, FREE
, GET_LENGTH
, FOREACH
, FOREACH_RANGE
, SORT
, SORT_ITER
,
81 GET_BEGIN_ITER
, GET_END_ITER
, GET_ITER_AT_POS
, APPEND
, PREPEND
,
82 INSERT_BEFORE
, MOVE
, SWAP
, INSERT_SORTED
, INSERT_SORTED_ITER
, SORT_CHANGED
,
83 SORT_CHANGED_ITER
, REMOVE
, REMOVE_RANGE
, MOVE_RANGE
, SEARCH
, SEARCH_ITER
,
89 /* operations on GSequenceIter * */
90 ITER_IS_BEGIN
, ITER_IS_END
, ITER_NEXT
, ITER_PREV
, ITER_GET_POSITION
,
91 ITER_MOVE
, ITER_GET_SEQUENCE
,
94 ITER_COMPARE
, RANGE_GET_MIDPOINT
,
98 typedef struct SequenceInfo
101 GSequence
* sequence
;
111 void g_sequence_check (GSequence
*sequence
);
114 fix_pointer (gconstpointer data
)
116 return (Item
*)((char *)data
- 1);
120 get_item (GSequenceIter
*iter
)
122 return fix_pointer (g_sequence_get (iter
));
126 check_integrity (SequenceInfo
*info
)
132 g_sequence_check (info
->sequence
);
135 if (g_sequence_get_length (info
->sequence
) != info
->n_items
)
136 g_printerr ("%d %d\n",
137 g_sequence_get_length (info
->sequence
), info
->n_items
);
139 g_assert (info
->n_items
== g_queue_get_length (info
->queue
));
140 g_assert (g_sequence_get_length (info
->sequence
) == info
->n_items
);
142 iter
= g_sequence_get_begin_iter (info
->sequence
);
143 list
= info
->queue
->head
;
145 while (iter
!= g_sequence_get_end_iter (info
->sequence
))
148 g_assert (list
->data
== iter
);
149 item
= get_item (list
->data
);
150 g_assert (item
->seq
== info
);
152 iter
= g_sequence_iter_next (iter
);
157 g_assert (info
->n_items
== g_queue_get_length (info
->queue
));
158 g_assert (g_sequence_get_length (info
->sequence
) == info
->n_items
);
162 new_item (SequenceInfo
*seq
)
164 Item
*item
= g_new (Item
, 1);
167 item
->number
= g_random_int ();
169 /* There have been bugs in the past where the GSequence would
170 * dereference the user pointers. This will make sure such
171 * behavior causes crashes
173 return ((char *)item
+ 1);
177 free_item (gpointer data
)
179 Item
*item
= fix_pointer (data
);
180 item
->seq
->n_items
--;
185 seq_foreach (gpointer data
,
188 Item
*item
= fix_pointer (data
);
189 GList
**link
= user_data
;
192 g_assert (*link
!= NULL
);
194 iter
= (*link
)->data
;
196 g_assert (get_item (iter
) == item
);
198 item
->number
= g_random_int();
200 *link
= (*link
)->next
;
204 simple_items_cmp (gconstpointer a
,
208 const Item
*item_a
= fix_pointer (a
);
209 const Item
*item_b
= fix_pointer (b
);
211 if (item_a
->number
> item_b
->number
)
213 else if (item_a
->number
< item_b
->number
)
220 simple_iters_cmp (gconstpointer a
,
224 GSequence
*seq
= data
;
225 GSequenceIter
*iter_a
= (GSequenceIter
*)a
;
226 GSequenceIter
*iter_b
= (GSequenceIter
*)b
;
227 gpointer item_a
= g_sequence_get (iter_a
);
228 gpointer item_b
= g_sequence_get (iter_b
);
232 g_assert (g_sequence_iter_get_sequence (iter_a
) == seq
);
233 g_assert (g_sequence_iter_get_sequence (iter_b
) == seq
);
236 return simple_items_cmp (item_a
, item_b
, data
);
240 compare_items (gconstpointer a
,
244 const Item
*item_a
= fix_pointer (a
);
245 const Item
*item_b
= fix_pointer (b
);
247 if (item_a
->number
< item_b
->number
)
251 else if (item_a
->number
== item_b
->number
)
253 /* Force an arbitrary order on the items
254 * We have to do this, since g_queue_insert_sorted() and
255 * g_sequence_insert_sorted() do not agree on the exact
256 * position the item is inserted if the new item is
257 * equal to an existing one.
261 else if (item_a
== item_b
)
273 check_sorted (SequenceInfo
*info
)
277 GSequenceIter
*last_iter
;
279 check_integrity (info
);
283 for (list
= info
->queue
->head
; list
!= NULL
; list
= list
->next
)
285 GSequenceIter
*iter
= list
->data
;
286 Item
*item
= get_item (iter
);
288 g_assert (item
->number
>= last
);
289 /* Check that the ordering is the same as that of the queue,
290 * ie. that the sort is stable
293 g_assert (iter
== g_sequence_iter_next (last_iter
));
301 compare_iters (gconstpointer a
,
305 GSequence
*seq
= data
;
306 GSequenceIter
*iter_a
= (GSequenceIter
*)a
;
307 GSequenceIter
*iter_b
= (GSequenceIter
*)b
;
308 /* compare_items() will fix up the pointers */
309 Item
*item_a
= g_sequence_get (iter_a
);
310 Item
*item_b
= g_sequence_get (iter_b
);
314 g_assert (g_sequence_iter_get_sequence (iter_a
) == seq
);
315 g_assert (g_sequence_iter_get_sequence (iter_b
) == seq
);
318 return compare_items (item_a
, item_b
, data
);
321 /* A version of g_queue_link_index() that treats NULL as just
325 queue_link_index (SequenceInfo
*seq
, GList
*link
)
328 return g_queue_link_index (seq
->queue
, link
);
330 return g_queue_get_length (seq
->queue
);
334 get_random_range (SequenceInfo
*seq
,
335 GSequenceIter
**begin_iter
,
336 GSequenceIter
**end_iter
,
340 int length
= g_queue_get_length (seq
->queue
);
341 int b
= g_random_int_range (0, length
+ 1);
342 int e
= g_random_int_range (b
, length
+ 1);
344 g_assert (length
== g_sequence_get_length (seq
->sequence
));
347 *begin_iter
= g_sequence_get_iter_at_pos (seq
->sequence
, b
);
349 *end_iter
= g_sequence_get_iter_at_pos (seq
->sequence
, e
);
351 *begin_link
= g_queue_peek_nth_link (seq
->queue
, b
);
353 *end_link
= g_queue_peek_nth_link (seq
->queue
, e
);
354 if (begin_iter
&& begin_link
)
357 queue_link_index (seq
, *begin_link
) ==
358 g_sequence_iter_get_position (*begin_iter
));
360 if (end_iter
&& end_link
)
363 queue_link_index (seq
, *end_link
) ==
364 g_sequence_iter_get_position (*end_iter
));
369 get_random_position (SequenceInfo
*seq
)
371 int length
= g_queue_get_length (seq
->queue
);
373 g_assert (length
== g_sequence_get_length (seq
->sequence
));
375 return g_random_int_range (-2, length
+ 5);
378 static GSequenceIter
*
379 get_random_iter (SequenceInfo
*seq
,
383 int pos
= get_random_position (seq
);
385 *link
= g_queue_peek_nth_link (seq
->queue
, pos
);
386 iter
= g_sequence_get_iter_at_pos (seq
->sequence
, pos
);
388 g_assert (queue_link_index (seq
, *link
) == g_sequence_iter_get_position (iter
));
393 dump_info (SequenceInfo
*seq
)
399 iter
= g_sequence_get_begin_iter (seq
->sequence
);
400 list
= seq
->queue
->head
;
402 while (iter
!= g_sequence_get_end_iter (seq
->sequence
))
404 Item
*item
= get_item (iter
);
405 g_printerr ("%p %p %d\n", list
->data
, iter
, item
->number
);
407 iter
= g_sequence_iter_next (iter
);
414 run_random_tests (gconstpointer d
)
416 guint32 seed
= GPOINTER_TO_UINT (d
);
417 #define N_ITERATIONS 60000
418 #define N_SEQUENCES 8
421 SequenceInfo sequences
[N_SEQUENCES
];
425 g_printerr (" seed: %u\n", seed
);
428 g_random_set_seed (seed
);
430 for (k
= 0; k
< N_SEQUENCES
; ++k
)
432 sequences
[k
].queue
= g_queue_new ();
433 sequences
[k
].sequence
= g_sequence_new (free_item
);
434 sequences
[k
].n_items
= 0;
437 #define RANDOM_SEQUENCE() &(sequences[g_random_int_range (0, N_SEQUENCES)])
439 for (k
= 0; k
< N_ITERATIONS
; ++k
)
442 SequenceInfo
*seq
= RANDOM_SEQUENCE();
443 int op
= g_random_int_range (0, N_OPS
);
446 g_printerr ("%d on %p\n", op
, seq
);
454 g_queue_free (seq
->queue
);
455 g_sequence_free (seq
->sequence
);
457 g_assert (seq
->n_items
== 0);
459 seq
->queue
= g_queue_new ();
460 seq
->sequence
= g_sequence_new (free_item
);
462 check_integrity (seq
);
467 int slen
= g_sequence_get_length (seq
->sequence
);
468 int qlen
= g_queue_get_length (seq
->queue
);
470 g_assert (slen
== qlen
);
475 GList
*link
= seq
->queue
->head
;
476 g_sequence_foreach (seq
->sequence
, seq_foreach
, &link
);
477 g_assert (link
== NULL
);
482 GSequenceIter
*begin_iter
, *end_iter
;
483 GList
*begin_link
, *end_link
;
485 get_random_range (seq
, &begin_iter
, &end_iter
, &begin_link
, &end_link
);
487 check_integrity (seq
);
489 g_sequence_foreach_range (begin_iter
, end_iter
, seq_foreach
, &begin_link
);
491 g_assert (begin_link
== end_link
);
498 g_sequence_sort (seq
->sequence
, compare_items
, NULL
);
499 g_queue_sort (seq
->queue
, compare_iters
, NULL
);
508 check_integrity (seq
);
509 g_sequence_sort_iter (seq
->sequence
,
510 (GSequenceIterCompareFunc
)compare_iters
, seq
->sequence
);
511 g_queue_sort (seq
->queue
, compare_iters
, NULL
);
520 GSequenceIter
*begin_iter
;
521 GSequenceIter
*end_iter
;
522 GSequenceIter
*penultimate_iter
;
524 begin_iter
= g_sequence_get_begin_iter (seq
->sequence
);
525 check_integrity (seq
);
527 end_iter
= g_sequence_get_end_iter (seq
->sequence
);
528 check_integrity (seq
);
530 penultimate_iter
= g_sequence_iter_prev (end_iter
);
531 check_integrity (seq
);
533 if (g_sequence_get_length (seq
->sequence
) > 0)
535 g_assert (seq
->queue
->head
);
536 g_assert (seq
->queue
->head
->data
== begin_iter
);
537 g_assert (seq
->queue
->tail
);
538 g_assert (seq
->queue
->tail
->data
== penultimate_iter
);
542 g_assert (penultimate_iter
== end_iter
);
543 g_assert (begin_iter
== end_iter
);
544 g_assert (penultimate_iter
== begin_iter
);
545 g_assert (seq
->queue
->head
== NULL
);
546 g_assert (seq
->queue
->tail
== NULL
);
550 case GET_ITER_AT_POS
:
554 g_assert (g_queue_get_length (seq
->queue
) == g_sequence_get_length (seq
->sequence
));
556 for (i
= 0; i
< 10; ++i
)
558 int pos
= get_random_position (seq
);
559 GSequenceIter
*iter
= g_sequence_get_iter_at_pos (seq
->sequence
, pos
);
560 GList
*link
= g_queue_peek_nth_link (seq
->queue
, pos
);
561 check_integrity (seq
);
562 if (pos
>= g_sequence_get_length (seq
->sequence
) || pos
< 0)
564 g_assert (iter
== g_sequence_get_end_iter (seq
->sequence
));
565 g_assert (link
== NULL
);
570 g_assert (link
->data
== iter
);
577 for (i
= 0; i
< 10; ++i
)
579 GSequenceIter
*iter
= g_sequence_append (seq
->sequence
, new_item (seq
));
580 g_queue_push_tail (seq
->queue
, iter
);
586 for (i
= 0; i
< 10; ++i
)
588 GSequenceIter
*iter
= g_sequence_prepend (seq
->sequence
, new_item (seq
));
589 g_queue_push_head (seq
->queue
, iter
);
595 for (i
= 0; i
< 10; ++i
)
598 GSequenceIter
*iter
= get_random_iter (seq
, &link
);
599 GSequenceIter
*new_iter
;
600 check_integrity (seq
);
602 new_iter
= g_sequence_insert_before (iter
, new_item (seq
));
604 g_queue_insert_before (seq
->queue
, link
, new_iter
);
610 GList
*link1
, *link2
;
611 SequenceInfo
*seq1
= RANDOM_SEQUENCE();
612 SequenceInfo
*seq2
= RANDOM_SEQUENCE();
613 GSequenceIter
*iter1
= get_random_iter (seq1
, &link1
);
614 GSequenceIter
*iter2
= get_random_iter (seq2
, &link2
);
616 if (!g_sequence_iter_is_end (iter1
))
618 g_sequence_move (iter1
, iter2
);
621 g_assert (g_sequence_iter_is_end (iter2
));
623 g_queue_insert_before (seq2
->queue
, link2
, link1
->data
);
625 g_queue_delete_link (seq1
->queue
, link1
);
627 get_item (iter1
)->seq
= seq2
;
633 check_integrity (seq
);
635 iter1
= get_random_iter (seq
, NULL
);
637 /* Moving an iter to itself should have no effect */
638 if (!g_sequence_iter_is_end (iter1
))
639 g_sequence_move (iter1
, iter1
);
644 GList
*link1
, *link2
;
645 SequenceInfo
*seq1
= RANDOM_SEQUENCE();
646 SequenceInfo
*seq2
= RANDOM_SEQUENCE();
647 GSequenceIter
*iter1
= get_random_iter (seq1
, &link1
);
648 GSequenceIter
*iter2
= get_random_iter (seq2
, &link2
);
650 if (!g_sequence_iter_is_end (iter1
) &&
651 !g_sequence_iter_is_end (iter2
))
655 g_sequence_swap (iter1
, iter2
);
657 get_item (iter1
)->seq
= seq2
;
658 get_item (iter2
)->seq
= seq1
;
661 link1
->data
= link2
->data
;
671 g_sequence_sort (seq
->sequence
, compare_items
, NULL
);
672 g_queue_sort (seq
->queue
, compare_iters
, NULL
);
676 for (i
= 0; i
< N_TIMES
; ++i
)
678 GSequenceIter
*iter
=
679 g_sequence_insert_sorted (seq
->sequence
, new_item(seq
), compare_items
, NULL
);
681 g_queue_insert_sorted (seq
->queue
, iter
, compare_iters
, NULL
);
689 case INSERT_SORTED_ITER
:
694 g_sequence_sort (seq
->sequence
, compare_items
, NULL
);
695 g_queue_sort (seq
->queue
, compare_iters
, NULL
);
699 for (i
= 0; i
< N_TIMES
; ++i
)
703 iter
= g_sequence_insert_sorted_iter (seq
->sequence
,
705 (GSequenceIterCompareFunc
)compare_iters
,
708 g_queue_insert_sorted (seq
->queue
, iter
, compare_iters
, NULL
);
720 g_sequence_sort (seq
->sequence
, compare_items
, NULL
);
721 g_queue_sort (seq
->queue
, compare_iters
, NULL
);
725 for (i
= 0; i
< N_TIMES
; ++i
)
728 GSequenceIter
*iter
= get_random_iter (seq
, &link
);
730 if (!g_sequence_iter_is_end (iter
))
732 g_sequence_set (iter
, new_item (seq
));
733 g_sequence_sort_changed (iter
, compare_items
, NULL
);
735 g_queue_delete_link (seq
->queue
, link
);
736 g_queue_insert_sorted (seq
->queue
, iter
, compare_iters
, NULL
);
743 case SORT_CHANGED_ITER
:
747 g_sequence_sort (seq
->sequence
, compare_items
, NULL
);
748 g_queue_sort (seq
->queue
, compare_iters
, NULL
);
752 for (i
= 0; i
< N_TIMES
; ++i
)
755 GSequenceIter
*iter
= get_random_iter (seq
, &link
);
757 if (!g_sequence_iter_is_end (iter
))
759 g_sequence_set (iter
, new_item (seq
));
760 g_sequence_sort_changed_iter (iter
,
761 (GSequenceIterCompareFunc
)compare_iters
, seq
->sequence
);
763 g_queue_delete_link (seq
->queue
, link
);
764 g_queue_insert_sorted (seq
->queue
, iter
, compare_iters
, NULL
);
775 for (i
= 0; i
< N_TIMES
; ++i
)
778 GSequenceIter
*iter
= get_random_iter (seq
, &link
);
780 if (!g_sequence_iter_is_end (iter
))
782 g_sequence_remove (iter
);
783 g_queue_delete_link (seq
->queue
, link
);
790 GSequenceIter
*begin_iter
, *end_iter
;
791 GList
*begin_link
, *end_link
;
794 get_random_range (seq
, &begin_iter
, &end_iter
, &begin_link
, &end_link
);
796 g_sequence_remove_range (begin_iter
, end_iter
);
799 while (list
!= end_link
)
801 GList
*next
= list
->next
;
803 g_queue_delete_link (seq
->queue
, list
);
811 SequenceInfo
*src
= RANDOM_SEQUENCE();
812 SequenceInfo
*dst
= RANDOM_SEQUENCE();
814 GSequenceIter
*begin_iter
, *end_iter
;
815 GList
*begin_link
, *end_link
;
817 GSequenceIter
*dst_iter
;
822 g_assert (src
->queue
);
823 g_assert (dst
->queue
);
825 get_random_range (src
, &begin_iter
, &end_iter
, &begin_link
, &end_link
);
826 dst_iter
= get_random_iter (dst
, &dst_link
);
828 g_sequence_move_range (dst_iter
, begin_iter
, end_iter
);
830 if (dst_link
== begin_link
|| (src
== dst
&& dst_link
== end_link
))
832 check_integrity (src
);
833 check_integrity (dst
);
837 if (queue_link_index (src
, begin_link
) >=
838 queue_link_index (src
, end_link
))
844 queue_link_index (src
, dst_link
) >= queue_link_index (src
, begin_link
) &&
845 queue_link_index (src
, dst_link
) <= queue_link_index (src
, end_link
))
851 while (list
!= end_link
)
853 GList
*next
= list
->next
;
854 Item
*item
= get_item (list
->data
);
856 g_assert (dst
->queue
);
857 g_queue_insert_before (dst
->queue
, dst_link
, list
->data
);
858 g_queue_delete_link (src
->queue
, list
);
860 g_assert (item
->seq
== src
);
873 GSequenceIter
*search_iter
;
874 GSequenceIter
*insert_iter
;
876 g_sequence_sort (seq
->sequence
, compare_items
, NULL
);
877 g_queue_sort (seq
->queue
, compare_iters
, NULL
);
881 item
= new_item (seq
);
882 search_iter
= g_sequence_search (seq
->sequence
, item
, compare_items
, NULL
);
884 insert_iter
= g_sequence_insert_sorted (seq
->sequence
, item
, compare_items
, NULL
);
886 g_assert (search_iter
== g_sequence_iter_next (insert_iter
));
888 g_queue_insert_sorted (seq
->queue
, insert_iter
, compare_iters
, NULL
);
894 GSequenceIter
*search_iter
;
895 GSequenceIter
*insert_iter
;
897 g_sequence_sort (seq
->sequence
, compare_items
, NULL
);
898 g_queue_sort (seq
->queue
, compare_iters
, NULL
);
902 item
= new_item (seq
);
903 search_iter
= g_sequence_search_iter (seq
->sequence
,
905 (GSequenceIterCompareFunc
)compare_iters
, seq
->sequence
);
907 insert_iter
= g_sequence_insert_sorted (seq
->sequence
, item
, compare_items
, NULL
);
909 g_assert (search_iter
== g_sequence_iter_next (insert_iter
));
911 g_queue_insert_sorted (seq
->queue
, insert_iter
, compare_iters
, NULL
);
917 GSequenceIter
*lookup_iter
;
918 GSequenceIter
*insert_iter
;
920 g_sequence_sort (seq
->sequence
, compare_items
, NULL
);
921 g_queue_sort (seq
->queue
, compare_iters
, NULL
);
925 item
= new_item (seq
);
926 insert_iter
= g_sequence_insert_sorted (seq
->sequence
, item
, compare_items
, NULL
);
927 g_queue_insert_sorted (seq
->queue
, insert_iter
, compare_iters
, NULL
);
929 lookup_iter
= g_sequence_lookup (seq
->sequence
, item
, simple_items_cmp
, NULL
);
930 g_assert (simple_iters_cmp (insert_iter
, lookup_iter
, NULL
) == 0);
936 GSequenceIter
*lookup_iter
;
937 GSequenceIter
*insert_iter
;
939 g_sequence_sort (seq
->sequence
, compare_items
, NULL
);
940 g_queue_sort (seq
->queue
, compare_iters
, NULL
);
944 item
= new_item (seq
);
945 insert_iter
= g_sequence_insert_sorted (seq
->sequence
, item
, compare_items
, NULL
);
946 g_queue_insert_sorted (seq
->queue
, insert_iter
, compare_iters
, NULL
);
948 lookup_iter
= g_sequence_lookup_iter (seq
->sequence
, item
,
949 (GSequenceIterCompareFunc
) simple_iters_cmp
, NULL
);
950 g_assert (simple_iters_cmp (insert_iter
, lookup_iter
, NULL
) == 0);
961 iter
= get_random_iter (seq
, &link
);
963 if (!g_sequence_iter_is_end (iter
))
968 check_integrity (seq
);
970 /* Test basic functionality */
971 item
= new_item (seq
);
972 g_sequence_set (iter
, item
);
973 g_assert (g_sequence_get (iter
) == item
);
975 /* Make sure that existing items are freed */
976 for (i
= 0; i
< N_TIMES
; ++i
)
977 g_sequence_set (iter
, new_item (seq
));
979 check_integrity (seq
);
981 g_sequence_set (iter
, new_item (seq
));
986 /* operations on GSequenceIter * */
991 iter
= g_sequence_get_iter_at_pos (seq
->sequence
, 0);
993 g_assert (g_sequence_iter_is_begin (iter
));
995 check_integrity (seq
);
997 if (g_sequence_get_length (seq
->sequence
) > 0)
999 g_assert (!g_sequence_iter_is_begin (g_sequence_get_end_iter (seq
->sequence
)));
1003 g_assert (g_sequence_iter_is_begin (g_sequence_get_end_iter (seq
->sequence
)));
1006 g_assert (g_sequence_iter_is_begin (g_sequence_get_begin_iter (seq
->sequence
)));
1011 GSequenceIter
*iter
;
1012 int len
= g_sequence_get_length (seq
->sequence
);
1014 iter
= g_sequence_get_iter_at_pos (seq
->sequence
, len
);
1016 g_assert (g_sequence_iter_is_end (iter
));
1020 g_assert (!g_sequence_iter_is_end (g_sequence_get_begin_iter (seq
->sequence
)));
1024 g_assert (g_sequence_iter_is_end (g_sequence_get_begin_iter (seq
->sequence
)));
1027 g_assert (g_sequence_iter_is_end (g_sequence_get_end_iter (seq
->sequence
)));
1032 GSequenceIter
*iter1
, *iter2
, *iter3
, *end
;
1034 iter1
= g_sequence_append (seq
->sequence
, new_item (seq
));
1035 iter2
= g_sequence_append (seq
->sequence
, new_item (seq
));
1036 iter3
= g_sequence_append (seq
->sequence
, new_item (seq
));
1038 end
= g_sequence_get_end_iter (seq
->sequence
);
1040 g_assert (g_sequence_iter_next (iter1
) == iter2
);
1041 g_assert (g_sequence_iter_next (iter2
) == iter3
);
1042 g_assert (g_sequence_iter_next (iter3
) == end
);
1043 g_assert (g_sequence_iter_next (end
) == end
);
1045 g_queue_push_tail (seq
->queue
, iter1
);
1046 g_queue_push_tail (seq
->queue
, iter2
);
1047 g_queue_push_tail (seq
->queue
, iter3
);
1052 GSequenceIter
*iter1
, *iter2
, *iter3
, *begin
;
1054 iter1
= g_sequence_prepend (seq
->sequence
, new_item (seq
));
1055 iter2
= g_sequence_prepend (seq
->sequence
, new_item (seq
));
1056 iter3
= g_sequence_prepend (seq
->sequence
, new_item (seq
));
1058 begin
= g_sequence_get_begin_iter (seq
->sequence
);
1060 g_assert (g_sequence_iter_prev (iter1
) == iter2
);
1061 g_assert (g_sequence_iter_prev (iter2
) == iter3
);
1062 g_assert (iter3
== begin
);
1063 g_assert (g_sequence_iter_prev (iter3
) == begin
);
1064 g_assert (g_sequence_iter_prev (begin
) == begin
);
1066 g_queue_push_head (seq
->queue
, iter1
);
1067 g_queue_push_head (seq
->queue
, iter2
);
1068 g_queue_push_head (seq
->queue
, iter3
);
1071 case ITER_GET_POSITION
:
1074 GSequenceIter
*iter
= get_random_iter (seq
, &link
);
1076 g_assert (g_sequence_iter_get_position (iter
) ==
1077 queue_link_index (seq
, link
));
1082 int len
= g_sequence_get_length (seq
->sequence
);
1083 GSequenceIter
*iter
;
1086 iter
= get_random_iter (seq
, NULL
);
1087 pos
= g_sequence_iter_get_position (iter
);
1088 iter
= g_sequence_iter_move (iter
, len
- pos
);
1089 g_assert (g_sequence_iter_is_end (iter
));
1092 iter
= get_random_iter (seq
, NULL
);
1093 pos
= g_sequence_iter_get_position (iter
);
1096 g_assert (!g_sequence_iter_is_end (iter
));
1098 iter
= g_sequence_iter_move (iter
, 1);
1100 g_assert (g_sequence_iter_is_end (iter
));
1103 case ITER_GET_SEQUENCE
:
1105 GSequenceIter
*iter
= get_random_iter (seq
, NULL
);
1107 g_assert (g_sequence_iter_get_sequence (iter
) == seq
->sequence
);
1114 GList
*link1
, *link2
;
1115 GSequenceIter
*iter1
= get_random_iter (seq
, &link1
);
1116 GSequenceIter
*iter2
= get_random_iter (seq
, &link2
);
1118 int cmp
= g_sequence_iter_compare (iter1
, iter2
);
1119 int pos1
= queue_link_index (seq
, link1
);
1120 int pos2
= queue_link_index (seq
, link2
);
1124 g_assert (pos1
== pos2
);
1128 g_assert (pos1
< pos2
);
1132 g_assert (pos1
> pos2
);
1136 case RANGE_GET_MIDPOINT
:
1138 GSequenceIter
*iter1
= get_random_iter (seq
, NULL
);
1139 GSequenceIter
*iter2
= get_random_iter (seq
, NULL
);
1140 GSequenceIter
*iter3
;
1143 cmp
= g_sequence_iter_compare (iter1
, iter2
);
1154 iter3
= g_sequence_range_get_midpoint (iter1
, iter2
);
1158 g_assert (iter3
== iter1
);
1159 g_assert (iter3
== iter2
);
1162 g_assert (g_sequence_iter_get_position (iter3
) >=
1163 g_sequence_iter_get_position (iter1
));
1164 g_assert (g_sequence_iter_get_position (iter2
) >=
1165 g_sequence_iter_get_position (iter3
));
1171 check_integrity (seq
);
1174 for (k
= 0; k
< N_SEQUENCES
; ++k
)
1176 g_queue_free (sequences
[k
].queue
);
1177 g_sequence_free (sequences
[k
].sequence
);
1178 sequences
[k
].n_items
= 0;
1182 /* Random seeds known to have failed at one point
1184 static gulong seeds
[] =
1198 /* Single, stand-alone tests */
1201 test_out_of_range_jump (void)
1203 GSequence
*seq
= g_sequence_new (NULL
);
1204 GSequenceIter
*iter
= g_sequence_get_begin_iter (seq
);
1206 g_sequence_iter_move (iter
, 5);
1208 g_assert (g_sequence_iter_is_begin (iter
));
1209 g_assert (g_sequence_iter_is_end (iter
));
1211 g_sequence_free (seq
);
1215 test_iter_move (void)
1217 GSequence
*seq
= g_sequence_new (NULL
);
1218 GSequenceIter
*iter
;
1221 for (i
= 0; i
< 10; ++i
)
1222 g_sequence_append (seq
, GINT_TO_POINTER (i
));
1224 iter
= g_sequence_get_begin_iter (seq
);
1225 iter
= g_sequence_iter_move (iter
, 5);
1226 g_assert_cmpint (GPOINTER_TO_INT (g_sequence_get (iter
)), ==, 5);
1228 iter
= g_sequence_iter_move (iter
, -10);
1229 g_assert (g_sequence_iter_is_begin (iter
));
1231 iter
= g_sequence_get_end_iter (seq
);
1232 iter
= g_sequence_iter_move (iter
, -5);
1233 g_assert_cmpint (GPOINTER_TO_INT (g_sequence_get (iter
)), ==, 5);
1235 iter
= g_sequence_iter_move (iter
, 10);
1236 g_assert (g_sequence_iter_is_end (iter
));
1238 g_sequence_free (seq
);
1242 compare (gconstpointer a
, gconstpointer b
, gpointer userdata
)
1246 ai
= GPOINTER_TO_INT (a
);
1247 bi
= GPOINTER_TO_INT (b
);
1258 compare_iter (GSequenceIter
*a
,
1262 return compare (g_sequence_get (a
),
1268 test_insert_sorted_non_pointer (void)
1272 for (i
= 0; i
< 10; i
++)
1274 GSequence
*seq
= g_sequence_new (NULL
);
1277 for (j
= 0; j
< 10000; j
++)
1279 g_sequence_insert_sorted (seq
, GINT_TO_POINTER (g_random_int()),
1282 g_sequence_insert_sorted_iter (seq
, GINT_TO_POINTER (g_random_int()),
1283 compare_iter
, NULL
);
1286 g_sequence_check (seq
);
1288 g_sequence_free (seq
);
1293 test_stable_sort (void)
1296 GSequence
*seq
= g_sequence_new (NULL
);
1298 #define N_ITEMS 1000
1300 GSequenceIter
*iters
[N_ITEMS
];
1301 GSequenceIter
*iter
;
1303 for (i
= 0; i
< N_ITEMS
; ++i
)
1305 iters
[i
] = g_sequence_append (seq
, GINT_TO_POINTER (3000));
1306 g_sequence_check (seq
);
1307 g_assert (g_sequence_iter_get_sequence (iters
[i
]) == seq
);
1311 iter
= g_sequence_get_begin_iter (seq
);
1312 g_assert (g_sequence_iter_get_sequence (iter
) == seq
);
1313 g_sequence_check (seq
);
1314 while (!g_sequence_iter_is_end (iter
))
1316 g_assert (g_sequence_iter_get_sequence (iters
[i
]) == seq
);
1317 g_assert (iters
[i
++] == iter
);
1319 iter
= g_sequence_iter_next (iter
);
1320 g_sequence_check (seq
);
1323 g_sequence_sort (seq
, compare
, NULL
);
1326 iter
= g_sequence_get_begin_iter (seq
);
1327 while (!g_sequence_iter_is_end (iter
))
1329 g_assert (g_sequence_iter_get_sequence (iters
[i
]) == seq
);
1330 g_assert (iters
[i
] == iter
);
1332 iter
= g_sequence_iter_next (iter
);
1333 g_sequence_check (seq
);
1338 for (i
= N_ITEMS
- 1; i
>= 0; --i
)
1340 g_sequence_check (seq
);
1341 g_assert (g_sequence_iter_get_sequence (iters
[i
]) == seq
);
1342 g_assert (g_sequence_get_end_iter (seq
) != iters
[i
]);
1343 g_sequence_sort_changed (iters
[i
], compare
, NULL
);
1347 iter
= g_sequence_get_begin_iter (seq
);
1348 while (!g_sequence_iter_is_end (iter
))
1350 g_assert (iters
[i
++] == iter
);
1352 iter
= g_sequence_iter_next (iter
);
1353 g_sequence_check (seq
);
1356 g_sequence_free (seq
);
1365 seq
= g_sequence_new (NULL
);
1366 g_assert_true (g_sequence_is_empty (seq
));
1368 for (i
= 0; i
< 1000; i
++)
1370 g_sequence_append (seq
, GINT_TO_POINTER (i
));
1371 g_assert_false (g_sequence_is_empty (seq
));
1374 for (i
= 0; i
< 1000; i
++)
1376 GSequenceIter
*end
= g_sequence_get_end_iter (seq
);
1377 g_assert_false (g_sequence_is_empty (seq
));
1378 g_sequence_remove (g_sequence_iter_prev (end
));
1381 g_assert_true (g_sequence_is_empty (seq
));
1383 g_sequence_free (seq
);
1394 g_test_init (&argc
, &argv
, NULL
);
1396 /* Standalone tests */
1397 g_test_add_func ("/sequence/out-of-range-jump", test_out_of_range_jump
);
1398 g_test_add_func ("/sequence/iter-move", test_iter_move
);
1399 g_test_add_func ("/sequence/insert-sorted-non-pointer", test_insert_sorted_non_pointer
);
1400 g_test_add_func ("/sequence/stable-sort", test_stable_sort
);
1401 g_test_add_func ("/sequence/is_empty", test_empty
);
1403 /* Regression tests */
1404 for (i
= 0; i
< G_N_ELEMENTS (seeds
); ++i
)
1406 path
= g_strdup_printf ("/sequence/random/seed:%lu", seeds
[i
]);
1407 g_test_add_data_func (path
, GUINT_TO_POINTER (seeds
[i
]), run_random_tests
);
1411 /* New random seed */
1412 seed
= g_test_rand_int_range (0, G_MAXINT
);
1413 path
= g_strdup_printf ("/sequence/random/seed:%u", seed
);
1414 g_test_add_data_func (path
, GUINT_TO_POINTER (seed
), run_random_tests
);
1417 return g_test_run ();