1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
20 * file for a list of people on the GLib Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GLib at ftp://ftp.gtk.org/pub/gtk/.
25 #undef G_DISABLE_ASSERT
34 sum_up (gpointer data,
37 gint *sum = (gint *)user_data;
39 *sum += GPOINTER_TO_INT (data);
49 garray = g_array_new (FALSE, FALSE, sizeof (gint));
50 for (i = 0; i < 10000; i++)
51 g_array_append_val (garray, i);
53 for (i = 0; i < 10000; i++)
54 g_assert_cmpint (g_array_index (garray, gint, i), ==, i);
56 segment = (gint*)g_array_free (garray, FALSE);
57 for (i = 0; i < 10000; i++)
58 g_assert_cmpint (segment[i], ==, i);
68 garray = g_array_new (FALSE, FALSE, sizeof (gint));
69 for (i = 0; i < 100; i++)
70 g_array_prepend_val (garray, i);
72 for (i = 0; i < 100; i++)
73 g_assert_cmpint (g_array_index (garray, gint, i), ==, (100 - i - 1));
75 g_array_free (garray, TRUE);
85 garray = g_array_new (FALSE, FALSE, sizeof (gint));
86 for (i = 0; i < 100; i++)
87 g_array_append_val (garray, i);
89 g_assert_cmpint (garray->len, ==, 100);
91 g_array_remove_index (garray, 1);
92 g_array_remove_index (garray, 3);
93 g_array_remove_index (garray, 21);
94 g_array_remove_index (garray, 57);
96 g_assert_cmpint (garray->len, ==, 96);
99 for (i = 0; i < garray->len; i++)
101 cur = g_array_index (garray, gint, i);
102 g_assert (cur != 1 && cur != 4 && cur != 23 && cur != 60);
103 g_assert_cmpint (prev, <, cur);
107 g_array_free (garray, TRUE);
111 array_remove_fast (void)
117 garray = g_array_new (FALSE, FALSE, sizeof (gint));
118 for (i = 0; i < 100; i++)
119 g_array_append_val (garray, i);
121 g_assert_cmpint (garray->len, ==, 100);
123 g_array_remove_index_fast (garray, 1);
124 g_array_remove_index_fast (garray, 3);
125 g_array_remove_index_fast (garray, 21);
126 g_array_remove_index_fast (garray, 57);
128 g_assert_cmpint (garray->len, ==, 96);
131 for (i = 0; i < garray->len; i++)
133 cur = g_array_index (garray, gint, i);
134 g_assert (cur != 1 && cur != 3 && cur != 21 && cur != 57);
137 g_assert_cmpint (prev, <, cur);
142 g_array_free (garray, TRUE);
146 array_remove_range (void)
152 garray = g_array_new (FALSE, FALSE, sizeof (gint));
153 for (i = 0; i < 100; i++)
154 g_array_append_val (garray, i);
156 g_assert_cmpint (garray->len, ==, 100);
158 g_array_remove_range (garray, 31, 4);
160 g_assert_cmpint (garray->len, ==, 96);
163 for (i = 0; i < garray->len; i++)
165 cur = g_array_index (garray, gint, i);
166 g_assert (cur < 31 || cur > 34);
167 g_assert_cmpint (prev, <, cur);
171 g_array_free (garray, TRUE);
175 array_ref_count (void)
181 garray = g_array_new (FALSE, FALSE, sizeof (gint));
182 g_assert_cmpint (g_array_get_element_size (garray), ==, sizeof (gint));
183 for (i = 0; i < 100; i++)
184 g_array_prepend_val (garray, i);
186 /* check we can ref, unref and still access the array */
187 garray2 = g_array_ref (garray);
188 g_assert (garray == garray2);
189 g_array_unref (garray2);
190 for (i = 0; i < 100; i++)
191 g_assert_cmpint (g_array_index (garray, gint, i), ==, (100 - i - 1));
193 /* garray2 should be an empty valid GArray wrapper */
194 garray2 = g_array_ref (garray);
195 g_array_free (garray, TRUE);
197 g_assert_cmpint (garray2->len, ==, 0);
198 g_array_unref (garray2);
202 array_large_size_remalloc_impl (gpointer mem,
205 /* We only care that g_array_set_size() doesn't hang before
206 * calling g_realloc(). So if we got here, we already won.
211 static GMemVTable array_large_size_mem_vtable = {
212 malloc, array_large_size_remalloc_impl, free,
217 array_large_size (void)
219 g_test_bug ("568760");
221 if (g_test_subprocess ())
225 array = g_array_new (FALSE, FALSE, sizeof (char));
227 g_mem_set_vtable (&array_large_size_mem_vtable);
228 g_array_set_size (array, 1073750016);
229 g_assert_not_reached ();
233 g_test_trap_subprocess (NULL, 5000000, 0);
234 g_test_trap_assert_passed ();
238 int_compare (gconstpointer p1, gconstpointer p2)
247 int_compare_data (gconstpointer p1, gconstpointer p2, gpointer data)
261 garray = g_array_new (FALSE, FALSE, sizeof (gint));
262 for (i = 0; i < 10000; i++)
264 cur = g_random_int_range (0, 10000);
265 g_array_append_val (garray, cur);
267 g_array_sort (garray, int_compare);
270 for (i = 0; i < garray->len; i++)
272 cur = g_array_index (garray, gint, i);
273 g_assert_cmpint (prev, <=, cur);
277 g_array_free (garray, TRUE);
281 array_sort_with_data (void)
287 garray = g_array_new (FALSE, FALSE, sizeof (gint));
288 for (i = 0; i < 10000; i++)
290 cur = g_random_int_range (0, 10000);
291 g_array_append_val (garray, cur);
293 g_array_sort_with_data (garray, int_compare_data, NULL);
296 for (i = 0; i < garray->len; i++)
298 cur = g_array_index (garray, gint, i);
299 g_assert_cmpint (prev, <=, cur);
303 g_array_free (garray, TRUE);
306 static gint num_clear_func_invocations = 0;
309 my_clear_func (gpointer data)
311 num_clear_func_invocations += 1;
315 array_clear_func (void)
321 garray = g_array_new (FALSE, FALSE, sizeof (gint));
322 g_array_set_clear_func (garray, my_clear_func);
324 for (i = 0; i < 10; i++)
326 cur = g_random_int_range (0, 100);
327 g_array_append_val (garray, cur);
330 g_array_remove_index (garray, 9);
331 g_assert_cmpint (num_clear_func_invocations, ==, 1);
333 g_array_remove_range (garray, 5, 3);
334 g_assert_cmpint (num_clear_func_invocations, ==, 4);
336 g_array_remove_index_fast (garray, 4);
337 g_assert_cmpint (num_clear_func_invocations, ==, 5);
339 g_array_free (garray, TRUE);
340 g_assert_cmpint (num_clear_func_invocations, ==, 10);
344 pointer_array_add (void)
351 gparray = g_ptr_array_sized_new (1000);
353 for (i = 0; i < 10000; i++)
354 g_ptr_array_add (gparray, GINT_TO_POINTER (i));
356 for (i = 0; i < 10000; i++)
357 g_assert (g_ptr_array_index (gparray, i) == GINT_TO_POINTER (i));
359 g_ptr_array_foreach (gparray, sum_up, &sum);
360 g_assert (sum == 49995000);
362 segment = g_ptr_array_free (gparray, FALSE);
363 for (i = 0; i < 10000; i++)
364 g_assert (segment[i] == GINT_TO_POINTER (i));
369 pointer_array_insert (void)
376 gparray = g_ptr_array_sized_new (1000);
378 for (i = 0; i < 10000; i++)
380 index = g_random_int_range (-1, i + 1);
381 g_ptr_array_insert (gparray, index, GINT_TO_POINTER (i));
384 g_ptr_array_foreach (gparray, sum_up, &sum);
385 g_assert (sum == 49995000);
387 g_ptr_array_free (gparray, TRUE);
391 pointer_array_ref_count (void)
398 gparray = g_ptr_array_new ();
399 for (i = 0; i < 10000; i++)
400 g_ptr_array_add (gparray, GINT_TO_POINTER (i));
402 /* check we can ref, unref and still access the array */
403 gparray2 = g_ptr_array_ref (gparray);
404 g_assert (gparray == gparray2);
405 g_ptr_array_unref (gparray2);
406 for (i = 0; i < 10000; i++)
407 g_assert (g_ptr_array_index (gparray, i) == GINT_TO_POINTER (i));
409 g_ptr_array_foreach (gparray, sum_up, &sum);
410 g_assert (sum == 49995000);
412 /* gparray2 should be an empty valid GPtrArray wrapper */
413 gparray2 = g_ptr_array_ref (gparray);
414 g_ptr_array_free (gparray, TRUE);
416 g_assert_cmpint (gparray2->len, ==, 0);
417 g_ptr_array_unref (gparray2);
420 static gint num_free_func_invocations = 0;
423 my_free_func (gpointer data)
425 num_free_func_invocations++;
430 pointer_array_free_func (void)
437 num_free_func_invocations = 0;
438 gparray = g_ptr_array_new_with_free_func (my_free_func);
439 g_ptr_array_unref (gparray);
440 g_assert_cmpint (num_free_func_invocations, ==, 0);
442 gparray = g_ptr_array_new_with_free_func (my_free_func);
443 g_ptr_array_free (gparray, TRUE);
444 g_assert_cmpint (num_free_func_invocations, ==, 0);
446 num_free_func_invocations = 0;
447 gparray = g_ptr_array_new_with_free_func (my_free_func);
448 g_ptr_array_add (gparray, g_strdup ("foo"));
449 g_ptr_array_add (gparray, g_strdup ("bar"));
450 g_ptr_array_add (gparray, g_strdup ("baz"));
451 g_ptr_array_remove_index (gparray, 0);
452 g_assert_cmpint (num_free_func_invocations, ==, 1);
453 g_ptr_array_remove_index_fast (gparray, 1);
454 g_assert_cmpint (num_free_func_invocations, ==, 2);
455 s = g_strdup ("frob");
456 g_ptr_array_add (gparray, s);
457 g_assert (g_ptr_array_remove (gparray, s));
458 g_assert (!g_ptr_array_remove (gparray, "nuun"));
459 g_assert (!g_ptr_array_remove_fast (gparray, "mlo"));
460 g_assert_cmpint (num_free_func_invocations, ==, 3);
461 s = g_strdup ("frob");
462 g_ptr_array_add (gparray, s);
463 g_ptr_array_set_size (gparray, 1);
464 g_assert_cmpint (num_free_func_invocations, ==, 4);
465 g_ptr_array_ref (gparray);
466 g_ptr_array_unref (gparray);
467 g_assert_cmpint (num_free_func_invocations, ==, 4);
468 g_ptr_array_unref (gparray);
469 g_assert_cmpint (num_free_func_invocations, ==, 5);
471 num_free_func_invocations = 0;
472 gparray = g_ptr_array_new_full (10, my_free_func);
473 g_ptr_array_add (gparray, g_strdup ("foo"));
474 g_ptr_array_add (gparray, g_strdup ("bar"));
475 g_ptr_array_add (gparray, g_strdup ("baz"));
476 g_ptr_array_set_size (gparray, 20);
477 g_ptr_array_add (gparray, NULL);
478 gparray2 = g_ptr_array_ref (gparray);
479 strv = (gchar **) g_ptr_array_free (gparray, FALSE);
480 g_assert_cmpint (num_free_func_invocations, ==, 0);
482 g_ptr_array_unref (gparray2);
483 g_assert_cmpint (num_free_func_invocations, ==, 0);
485 num_free_func_invocations = 0;
486 gparray = g_ptr_array_new_with_free_func (my_free_func);
487 g_ptr_array_add (gparray, g_strdup ("foo"));
488 g_ptr_array_add (gparray, g_strdup ("bar"));
489 g_ptr_array_add (gparray, g_strdup ("baz"));
490 g_ptr_array_remove_range (gparray, 1, 1);
491 g_ptr_array_unref (gparray);
492 g_assert_cmpint (num_free_func_invocations, ==, 3);
494 num_free_func_invocations = 0;
495 gparray = g_ptr_array_new_with_free_func (my_free_func);
496 g_ptr_array_add (gparray, g_strdup ("foo"));
497 g_ptr_array_add (gparray, g_strdup ("bar"));
498 g_ptr_array_add (gparray, g_strdup ("baz"));
499 g_ptr_array_free (gparray, TRUE);
500 g_assert_cmpint (num_free_func_invocations, ==, 3);
502 num_free_func_invocations = 0;
503 gparray = g_ptr_array_new_with_free_func (my_free_func);
504 g_ptr_array_add (gparray, "foo");
505 g_ptr_array_add (gparray, "bar");
506 g_ptr_array_add (gparray, "baz");
507 g_ptr_array_set_free_func (gparray, NULL);
508 g_ptr_array_free (gparray, TRUE);
509 g_assert_cmpint (num_free_func_invocations, ==, 0);
513 ptr_compare (gconstpointer p1, gconstpointer p2)
515 gpointer i1 = *(gpointer*)p1;
516 gpointer i2 = *(gpointer*)p2;
518 return GPOINTER_TO_INT (i1) - GPOINTER_TO_INT (i2);
522 ptr_compare_data (gconstpointer p1, gconstpointer p2, gpointer data)
524 gpointer i1 = *(gpointer*)p1;
525 gpointer i2 = *(gpointer*)p2;
527 return GPOINTER_TO_INT (i1) - GPOINTER_TO_INT (i2);
531 pointer_array_sort (void)
538 gparray = g_ptr_array_new ();
539 for (i = 0; i < 10000; i++)
541 val = g_random_int_range (0, 10000);
542 g_ptr_array_add (gparray, GINT_TO_POINTER (val));
545 g_ptr_array_sort (gparray, ptr_compare);
548 for (i = 0; i < 10000; i++)
550 cur = GPOINTER_TO_INT (g_ptr_array_index (gparray, i));
551 g_assert_cmpint (prev, <=, cur);
555 g_ptr_array_free (gparray, TRUE);
559 pointer_array_sort_with_data (void)
565 gparray = g_ptr_array_new ();
566 for (i = 0; i < 10000; i++)
567 g_ptr_array_add (gparray, GINT_TO_POINTER (g_random_int_range (0, 10000)));
569 g_ptr_array_sort_with_data (gparray, ptr_compare_data, NULL);
572 for (i = 0; i < 10000; i++)
574 cur = GPOINTER_TO_INT (g_ptr_array_index (gparray, i));
575 g_assert_cmpint (prev, <=, cur);
579 g_ptr_array_free (gparray, TRUE);
583 byte_array_append (void)
589 gbarray = g_byte_array_sized_new (1000);
590 for (i = 0; i < 10000; i++)
591 g_byte_array_append (gbarray, (guint8*) "abcd", 4);
593 for (i = 0; i < 10000; i++)
595 g_assert (gbarray->data[4*i] == 'a');
596 g_assert (gbarray->data[4*i+1] == 'b');
597 g_assert (gbarray->data[4*i+2] == 'c');
598 g_assert (gbarray->data[4*i+3] == 'd');
601 segment = g_byte_array_free (gbarray, FALSE);
603 for (i = 0; i < 10000; i++)
605 g_assert (segment[4*i] == 'a');
606 g_assert (segment[4*i+1] == 'b');
607 g_assert (segment[4*i+2] == 'c');
608 g_assert (segment[4*i+3] == 'd');
615 byte_array_prepend (void)
620 gbarray = g_byte_array_new ();
621 g_byte_array_set_size (gbarray, 1000);
623 for (i = 0; i < 10000; i++)
624 g_byte_array_prepend (gbarray, (guint8*) "abcd", 4);
626 for (i = 0; i < 10000; i++)
628 g_assert (gbarray->data[4*i] == 'a');
629 g_assert (gbarray->data[4*i+1] == 'b');
630 g_assert (gbarray->data[4*i+2] == 'c');
631 g_assert (gbarray->data[4*i+3] == 'd');
634 g_byte_array_free (gbarray, TRUE);
638 byte_array_ref_count (void)
641 GByteArray *gbarray2;
644 gbarray = g_byte_array_new ();
645 for (i = 0; i < 10000; i++)
646 g_byte_array_append (gbarray, (guint8*) "abcd", 4);
648 gbarray2 = g_byte_array_ref (gbarray);
649 g_assert (gbarray2 == gbarray);
650 g_byte_array_unref (gbarray2);
651 for (i = 0; i < 10000; i++)
653 g_assert (gbarray->data[4*i] == 'a');
654 g_assert (gbarray->data[4*i+1] == 'b');
655 g_assert (gbarray->data[4*i+2] == 'c');
656 g_assert (gbarray->data[4*i+3] == 'd');
659 gbarray2 = g_byte_array_ref (gbarray);
660 g_assert (gbarray2 == gbarray);
661 g_byte_array_free (gbarray, TRUE);
662 g_assert_cmpint (gbarray2->len, ==, 0);
663 g_byte_array_unref (gbarray2);
667 byte_array_remove (void)
672 gbarray = g_byte_array_new ();
673 for (i = 0; i < 100; i++)
674 g_byte_array_append (gbarray, (guint8*) "abcd", 4);
676 g_assert_cmpint (gbarray->len, ==, 400);
678 g_byte_array_remove_index (gbarray, 4);
679 g_byte_array_remove_index (gbarray, 4);
680 g_byte_array_remove_index (gbarray, 4);
681 g_byte_array_remove_index (gbarray, 4);
683 g_assert_cmpint (gbarray->len, ==, 396);
685 for (i = 0; i < 99; i++)
687 g_assert (gbarray->data[4*i] == 'a');
688 g_assert (gbarray->data[4*i+1] == 'b');
689 g_assert (gbarray->data[4*i+2] == 'c');
690 g_assert (gbarray->data[4*i+3] == 'd');
693 g_byte_array_free (gbarray, TRUE);
697 byte_array_remove_fast (void)
702 gbarray = g_byte_array_new ();
703 for (i = 0; i < 100; i++)
704 g_byte_array_append (gbarray, (guint8*) "abcd", 4);
706 g_assert_cmpint (gbarray->len, ==, 400);
708 g_byte_array_remove_index_fast (gbarray, 4);
709 g_byte_array_remove_index_fast (gbarray, 4);
710 g_byte_array_remove_index_fast (gbarray, 4);
711 g_byte_array_remove_index_fast (gbarray, 4);
713 g_assert_cmpint (gbarray->len, ==, 396);
715 for (i = 0; i < 99; i++)
717 g_assert (gbarray->data[4*i] == 'a');
718 g_assert (gbarray->data[4*i+1] == 'b');
719 g_assert (gbarray->data[4*i+2] == 'c');
720 g_assert (gbarray->data[4*i+3] == 'd');
723 g_byte_array_free (gbarray, TRUE);
727 byte_array_remove_range (void)
732 gbarray = g_byte_array_new ();
733 for (i = 0; i < 100; i++)
734 g_byte_array_append (gbarray, (guint8*) "abcd", 4);
736 g_assert_cmpint (gbarray->len, ==, 400);
738 g_byte_array_remove_range (gbarray, 12, 4);
740 g_assert_cmpint (gbarray->len, ==, 396);
742 for (i = 0; i < 99; i++)
744 g_assert (gbarray->data[4*i] == 'a');
745 g_assert (gbarray->data[4*i+1] == 'b');
746 g_assert (gbarray->data[4*i+2] == 'c');
747 g_assert (gbarray->data[4*i+3] == 'd');
750 g_byte_array_free (gbarray, TRUE);
754 byte_compare (gconstpointer p1, gconstpointer p2)
756 const guint8 *i1 = p1;
757 const guint8 *i2 = p2;
763 byte_compare_data (gconstpointer p1, gconstpointer p2, gpointer data)
765 const guint8 *i1 = p1;
766 const guint8 *i2 = p2;
772 byte_array_sort (void)
779 gbarray = g_byte_array_new ();
780 for (i = 0; i < 100; i++)
782 val = 'a' + g_random_int_range (0, 26);
783 g_byte_array_append (gbarray, (guint8*) &val, 1);
786 g_byte_array_sort (gbarray, byte_compare);
789 for (i = 0; i < gbarray->len; i++)
791 cur = gbarray->data[i];
792 g_assert_cmpint (prev, <=, cur);
796 g_byte_array_free (gbarray, TRUE);
800 byte_array_sort_with_data (void)
807 gbarray = g_byte_array_new ();
808 for (i = 0; i < 100; i++)
810 val = 'a' + g_random_int_range (0, 26);
811 g_byte_array_append (gbarray, (guint8*) &val, 1);
814 g_byte_array_sort_with_data (gbarray, byte_compare_data, NULL);
817 for (i = 0; i < gbarray->len; i++)
819 cur = gbarray->data[i];
820 g_assert_cmpint (prev, <=, cur);
824 g_byte_array_free (gbarray, TRUE);
828 byte_array_new_take (void)
833 data = g_memdup ("woooweeewow", 11);
834 gbarray = g_byte_array_new_take (data, 11);
835 g_assert (gbarray->data == data);
836 g_assert_cmpuint (gbarray->len, ==, 11);
837 g_byte_array_free (gbarray, TRUE);
841 byte_array_free_to_bytes (void)
848 gbarray = g_byte_array_new ();
849 g_byte_array_append (gbarray, (guint8 *)"woooweeewow", 11);
850 memory = gbarray->data;
852 bytes = g_byte_array_free_to_bytes (gbarray);
853 g_assert (bytes != NULL);
854 g_assert_cmpuint (g_bytes_get_size (bytes), ==, 11);
855 g_assert (g_bytes_get_data (bytes, &size) == memory);
856 g_assert_cmpuint (size, ==, 11);
858 g_bytes_unref (bytes);
861 main (int argc, char *argv[])
863 g_test_init (&argc, &argv, NULL);
865 g_test_bug_base ("http://bugs.gnome.org/");
868 g_test_add_func ("/array/append", array_append);
869 g_test_add_func ("/array/prepend", array_prepend);
870 g_test_add_func ("/array/remove", array_remove);
871 g_test_add_func ("/array/remove-fast", array_remove_fast);
872 g_test_add_func ("/array/remove-range", array_remove_range);
873 g_test_add_func ("/array/ref-count", array_ref_count);
874 g_test_add_func ("/array/large-size", array_large_size);
875 g_test_add_func ("/array/sort", array_sort);
876 g_test_add_func ("/array/sort-with-data", array_sort_with_data);
877 g_test_add_func ("/array/clear-func", array_clear_func);
880 g_test_add_func ("/pointerarray/add", pointer_array_add);
881 g_test_add_func ("/pointerarray/insert", pointer_array_insert);
882 g_test_add_func ("/pointerarray/ref-count", pointer_array_ref_count);
883 g_test_add_func ("/pointerarray/free-func", pointer_array_free_func);
884 g_test_add_func ("/pointerarray/sort", pointer_array_sort);
885 g_test_add_func ("/pointerarray/sort-with-data", pointer_array_sort_with_data);
888 g_test_add_func ("/bytearray/append", byte_array_append);
889 g_test_add_func ("/bytearray/prepend", byte_array_prepend);
890 g_test_add_func ("/bytearray/remove", byte_array_remove);
891 g_test_add_func ("/bytearray/remove-fast", byte_array_remove_fast);
892 g_test_add_func ("/bytearray/remove-range", byte_array_remove_range);
893 g_test_add_func ("/bytearray/ref-count", byte_array_ref_count);
894 g_test_add_func ("/bytearray/sort", byte_array_sort);
895 g_test_add_func ("/bytearray/sort-with-data", byte_array_sort_with_data);
896 g_test_add_func ("/bytearray/new-take", byte_array_new_take);
897 g_test_add_func ("/bytearray/free-to-bytes", byte_array_free_to_bytes);
899 return g_test_run ();