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.1 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
);
42 /* Check that expanding an array with g_array_set_size() clears the new elements
43 * if @clear_ was specified during construction. */
45 array_new_cleared (void)
50 garray
= g_array_new (FALSE
, TRUE
, sizeof (gint
));
51 g_assert_cmpuint (garray
->len
, ==, 0);
53 g_array_set_size (garray
, 5);
54 g_assert_cmpuint (garray
->len
, ==, 5);
56 for (i
= 0; i
< 5; i
++)
57 g_assert_cmpint (g_array_index (garray
, gint
, i
), ==, 0);
59 g_array_unref (garray
);
62 /* As with array_new_cleared(), but with a sized array. */
64 array_new_sized_cleared (void)
69 garray
= g_array_sized_new (FALSE
, TRUE
, sizeof (gint
), 10);
70 g_assert_cmpuint (garray
->len
, ==, 0);
72 g_array_set_size (garray
, 5);
73 g_assert_cmpuint (garray
->len
, ==, 5);
75 for (i
= 0; i
< 5; i
++)
76 g_assert_cmpint (g_array_index (garray
, gint
, i
), ==, 0);
78 g_array_unref (garray
);
81 /* Check that a zero-terminated array does actually have a zero terminator. */
83 array_new_zero_terminated (void)
86 gchar
*out_str
= NULL
;
88 garray
= g_array_new (TRUE
, FALSE
, sizeof (gchar
));
89 g_assert_cmpuint (garray
->len
, ==, 0);
91 g_array_append_vals (garray
, "hello", strlen ("hello"));
92 g_assert_cmpuint (garray
->len
, ==, 5);
93 g_assert_cmpstr (garray
->data
, ==, "hello");
95 out_str
= g_array_free (garray
, FALSE
);
96 g_assert_cmpstr (out_str
, ==, "hello");
107 garray
= g_array_new (FALSE
, FALSE
, sizeof (gint
));
108 for (i
= 0; i
< 10000; i
++)
109 g_array_append_val (garray
, i
);
111 for (i
= 0; i
< 10000; i
++)
112 g_assert_cmpint (g_array_index (garray
, gint
, i
), ==, i
);
114 segment
= (gint
*)g_array_free (garray
, FALSE
);
115 for (i
= 0; i
< 10000; i
++)
116 g_assert_cmpint (segment
[i
], ==, i
);
126 garray
= g_array_new (FALSE
, FALSE
, sizeof (gint
));
127 for (i
= 0; i
< 100; i
++)
128 g_array_prepend_val (garray
, i
);
130 for (i
= 0; i
< 100; i
++)
131 g_assert_cmpint (g_array_index (garray
, gint
, i
), ==, (100 - i
- 1));
133 g_array_free (garray
, TRUE
);
143 garray
= g_array_new (FALSE
, FALSE
, sizeof (gint
));
144 for (i
= 0; i
< 100; i
++)
145 g_array_append_val (garray
, i
);
147 g_assert_cmpint (garray
->len
, ==, 100);
149 g_array_remove_index (garray
, 1);
150 g_array_remove_index (garray
, 3);
151 g_array_remove_index (garray
, 21);
152 g_array_remove_index (garray
, 57);
154 g_assert_cmpint (garray
->len
, ==, 96);
157 for (i
= 0; i
< garray
->len
; i
++)
159 cur
= g_array_index (garray
, gint
, i
);
160 g_assert (cur
!= 1 && cur
!= 4 && cur
!= 23 && cur
!= 60);
161 g_assert_cmpint (prev
, <, cur
);
165 g_array_free (garray
, TRUE
);
169 array_remove_fast (void)
175 garray
= g_array_new (FALSE
, FALSE
, sizeof (gint
));
176 for (i
= 0; i
< 100; i
++)
177 g_array_append_val (garray
, i
);
179 g_assert_cmpint (garray
->len
, ==, 100);
181 g_array_remove_index_fast (garray
, 1);
182 g_array_remove_index_fast (garray
, 3);
183 g_array_remove_index_fast (garray
, 21);
184 g_array_remove_index_fast (garray
, 57);
186 g_assert_cmpint (garray
->len
, ==, 96);
189 for (i
= 0; i
< garray
->len
; i
++)
191 cur
= g_array_index (garray
, gint
, i
);
192 g_assert (cur
!= 1 && cur
!= 3 && cur
!= 21 && cur
!= 57);
195 g_assert_cmpint (prev
, <, cur
);
200 g_array_free (garray
, TRUE
);
204 array_remove_range (void)
210 garray
= g_array_new (FALSE
, FALSE
, sizeof (gint
));
211 for (i
= 0; i
< 100; i
++)
212 g_array_append_val (garray
, i
);
214 g_assert_cmpint (garray
->len
, ==, 100);
216 g_array_remove_range (garray
, 31, 4);
218 g_assert_cmpint (garray
->len
, ==, 96);
221 for (i
= 0; i
< garray
->len
; i
++)
223 cur
= g_array_index (garray
, gint
, i
);
224 g_assert (cur
< 31 || cur
> 34);
225 g_assert_cmpint (prev
, <, cur
);
229 /* Ensure the entire array can be cleared, even when empty. */
230 g_array_remove_range (garray
, 0, garray
->len
);
231 g_array_remove_range (garray
, 0, garray
->len
);
233 g_array_free (garray
, TRUE
);
237 array_ref_count (void)
243 garray
= g_array_new (FALSE
, FALSE
, sizeof (gint
));
244 g_assert_cmpint (g_array_get_element_size (garray
), ==, sizeof (gint
));
245 for (i
= 0; i
< 100; i
++)
246 g_array_prepend_val (garray
, i
);
248 /* check we can ref, unref and still access the array */
249 garray2
= g_array_ref (garray
);
250 g_assert (garray
== garray2
);
251 g_array_unref (garray2
);
252 for (i
= 0; i
< 100; i
++)
253 g_assert_cmpint (g_array_index (garray
, gint
, i
), ==, (100 - i
- 1));
255 /* garray2 should be an empty valid GArray wrapper */
256 garray2
= g_array_ref (garray
);
257 g_array_free (garray
, TRUE
);
259 g_assert_cmpint (garray2
->len
, ==, 0);
260 g_array_unref (garray2
);
264 int_compare (gconstpointer p1
, gconstpointer p2
)
273 int_compare_data (gconstpointer p1
, gconstpointer p2
, gpointer data
)
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 (garray
, int_compare
);
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
);
307 array_sort_with_data (void)
313 garray
= g_array_new (FALSE
, FALSE
, sizeof (gint
));
314 for (i
= 0; i
< 10000; i
++)
316 cur
= g_random_int_range (0, 10000);
317 g_array_append_val (garray
, cur
);
319 g_array_sort_with_data (garray
, int_compare_data
, NULL
);
322 for (i
= 0; i
< garray
->len
; i
++)
324 cur
= g_array_index (garray
, gint
, i
);
325 g_assert_cmpint (prev
, <=, cur
);
329 g_array_free (garray
, TRUE
);
332 static gint num_clear_func_invocations
= 0;
335 my_clear_func (gpointer data
)
337 num_clear_func_invocations
+= 1;
341 array_clear_func (void)
347 garray
= g_array_new (FALSE
, FALSE
, sizeof (gint
));
348 g_array_set_clear_func (garray
, my_clear_func
);
350 for (i
= 0; i
< 10; i
++)
352 cur
= g_random_int_range (0, 100);
353 g_array_append_val (garray
, cur
);
356 g_array_remove_index (garray
, 9);
357 g_assert_cmpint (num_clear_func_invocations
, ==, 1);
359 g_array_remove_range (garray
, 5, 3);
360 g_assert_cmpint (num_clear_func_invocations
, ==, 4);
362 g_array_remove_index_fast (garray
, 4);
363 g_assert_cmpint (num_clear_func_invocations
, ==, 5);
365 g_array_free (garray
, TRUE
);
366 g_assert_cmpint (num_clear_func_invocations
, ==, 10);
370 pointer_array_add (void)
377 gparray
= g_ptr_array_sized_new (1000);
379 for (i
= 0; i
< 10000; i
++)
380 g_ptr_array_add (gparray
, GINT_TO_POINTER (i
));
382 for (i
= 0; i
< 10000; i
++)
383 g_assert (g_ptr_array_index (gparray
, i
) == GINT_TO_POINTER (i
));
385 g_ptr_array_foreach (gparray
, sum_up
, &sum
);
386 g_assert (sum
== 49995000);
388 segment
= g_ptr_array_free (gparray
, FALSE
);
389 for (i
= 0; i
< 10000; i
++)
390 g_assert (segment
[i
] == GINT_TO_POINTER (i
));
395 pointer_array_insert (void)
402 gparray
= g_ptr_array_sized_new (1000);
404 for (i
= 0; i
< 10000; i
++)
406 index
= g_random_int_range (-1, i
+ 1);
407 g_ptr_array_insert (gparray
, index
, GINT_TO_POINTER (i
));
410 g_ptr_array_foreach (gparray
, sum_up
, &sum
);
411 g_assert (sum
== 49995000);
413 g_ptr_array_free (gparray
, TRUE
);
417 pointer_array_ref_count (void)
424 gparray
= g_ptr_array_new ();
425 for (i
= 0; i
< 10000; i
++)
426 g_ptr_array_add (gparray
, GINT_TO_POINTER (i
));
428 /* check we can ref, unref and still access the array */
429 gparray2
= g_ptr_array_ref (gparray
);
430 g_assert (gparray
== gparray2
);
431 g_ptr_array_unref (gparray2
);
432 for (i
= 0; i
< 10000; i
++)
433 g_assert (g_ptr_array_index (gparray
, i
) == GINT_TO_POINTER (i
));
435 g_ptr_array_foreach (gparray
, sum_up
, &sum
);
436 g_assert (sum
== 49995000);
438 /* gparray2 should be an empty valid GPtrArray wrapper */
439 gparray2
= g_ptr_array_ref (gparray
);
440 g_ptr_array_free (gparray
, TRUE
);
442 g_assert_cmpint (gparray2
->len
, ==, 0);
443 g_ptr_array_unref (gparray2
);
446 static gint num_free_func_invocations
= 0;
449 my_free_func (gpointer data
)
451 num_free_func_invocations
++;
456 pointer_array_free_func (void)
463 num_free_func_invocations
= 0;
464 gparray
= g_ptr_array_new_with_free_func (my_free_func
);
465 g_ptr_array_unref (gparray
);
466 g_assert_cmpint (num_free_func_invocations
, ==, 0);
468 gparray
= g_ptr_array_new_with_free_func (my_free_func
);
469 g_ptr_array_free (gparray
, TRUE
);
470 g_assert_cmpint (num_free_func_invocations
, ==, 0);
472 num_free_func_invocations
= 0;
473 gparray
= g_ptr_array_new_with_free_func (my_free_func
);
474 g_ptr_array_add (gparray
, g_strdup ("foo"));
475 g_ptr_array_add (gparray
, g_strdup ("bar"));
476 g_ptr_array_add (gparray
, g_strdup ("baz"));
477 g_ptr_array_remove_index (gparray
, 0);
478 g_assert_cmpint (num_free_func_invocations
, ==, 1);
479 g_ptr_array_remove_index_fast (gparray
, 1);
480 g_assert_cmpint (num_free_func_invocations
, ==, 2);
481 s
= g_strdup ("frob");
482 g_ptr_array_add (gparray
, s
);
483 g_assert (g_ptr_array_remove (gparray
, s
));
484 g_assert (!g_ptr_array_remove (gparray
, "nuun"));
485 g_assert (!g_ptr_array_remove_fast (gparray
, "mlo"));
486 g_assert_cmpint (num_free_func_invocations
, ==, 3);
487 s
= g_strdup ("frob");
488 g_ptr_array_add (gparray
, s
);
489 g_ptr_array_set_size (gparray
, 1);
490 g_assert_cmpint (num_free_func_invocations
, ==, 4);
491 g_ptr_array_ref (gparray
);
492 g_ptr_array_unref (gparray
);
493 g_assert_cmpint (num_free_func_invocations
, ==, 4);
494 g_ptr_array_unref (gparray
);
495 g_assert_cmpint (num_free_func_invocations
, ==, 5);
497 num_free_func_invocations
= 0;
498 gparray
= g_ptr_array_new_full (10, my_free_func
);
499 g_ptr_array_add (gparray
, g_strdup ("foo"));
500 g_ptr_array_add (gparray
, g_strdup ("bar"));
501 g_ptr_array_add (gparray
, g_strdup ("baz"));
502 g_ptr_array_set_size (gparray
, 20);
503 g_ptr_array_add (gparray
, NULL
);
504 gparray2
= g_ptr_array_ref (gparray
);
505 strv
= (gchar
**) g_ptr_array_free (gparray
, FALSE
);
506 g_assert_cmpint (num_free_func_invocations
, ==, 0);
508 g_ptr_array_unref (gparray2
);
509 g_assert_cmpint (num_free_func_invocations
, ==, 0);
511 num_free_func_invocations
= 0;
512 gparray
= g_ptr_array_new_with_free_func (my_free_func
);
513 g_ptr_array_add (gparray
, g_strdup ("foo"));
514 g_ptr_array_add (gparray
, g_strdup ("bar"));
515 g_ptr_array_add (gparray
, g_strdup ("baz"));
516 g_ptr_array_remove_range (gparray
, 1, 1);
517 g_ptr_array_unref (gparray
);
518 g_assert_cmpint (num_free_func_invocations
, ==, 3);
520 num_free_func_invocations
= 0;
521 gparray
= g_ptr_array_new_with_free_func (my_free_func
);
522 g_ptr_array_add (gparray
, g_strdup ("foo"));
523 g_ptr_array_add (gparray
, g_strdup ("bar"));
524 g_ptr_array_add (gparray
, g_strdup ("baz"));
525 g_ptr_array_free (gparray
, TRUE
);
526 g_assert_cmpint (num_free_func_invocations
, ==, 3);
528 num_free_func_invocations
= 0;
529 gparray
= g_ptr_array_new_with_free_func (my_free_func
);
530 g_ptr_array_add (gparray
, "foo");
531 g_ptr_array_add (gparray
, "bar");
532 g_ptr_array_add (gparray
, "baz");
533 g_ptr_array_set_free_func (gparray
, NULL
);
534 g_ptr_array_free (gparray
, TRUE
);
535 g_assert_cmpint (num_free_func_invocations
, ==, 0);
539 ptr_compare (gconstpointer p1
, gconstpointer p2
)
541 gpointer i1
= *(gpointer
*)p1
;
542 gpointer i2
= *(gpointer
*)p2
;
544 return GPOINTER_TO_INT (i1
) - GPOINTER_TO_INT (i2
);
548 ptr_compare_data (gconstpointer p1
, gconstpointer p2
, gpointer data
)
550 gpointer i1
= *(gpointer
*)p1
;
551 gpointer i2
= *(gpointer
*)p2
;
553 return GPOINTER_TO_INT (i1
) - GPOINTER_TO_INT (i2
);
557 pointer_array_sort (void)
564 gparray
= g_ptr_array_new ();
565 for (i
= 0; i
< 10000; i
++)
567 val
= g_random_int_range (0, 10000);
568 g_ptr_array_add (gparray
, GINT_TO_POINTER (val
));
571 g_ptr_array_sort (gparray
, ptr_compare
);
574 for (i
= 0; i
< 10000; i
++)
576 cur
= GPOINTER_TO_INT (g_ptr_array_index (gparray
, i
));
577 g_assert_cmpint (prev
, <=, cur
);
581 g_ptr_array_free (gparray
, TRUE
);
585 pointer_array_sort_with_data (void)
591 gparray
= g_ptr_array_new ();
592 for (i
= 0; i
< 10000; i
++)
593 g_ptr_array_add (gparray
, GINT_TO_POINTER (g_random_int_range (0, 10000)));
595 g_ptr_array_sort_with_data (gparray
, ptr_compare_data
, NULL
);
598 for (i
= 0; i
< 10000; i
++)
600 cur
= GPOINTER_TO_INT (g_ptr_array_index (gparray
, i
));
601 g_assert_cmpint (prev
, <=, cur
);
605 g_ptr_array_free (gparray
, TRUE
);
609 pointer_array_find_empty (void)
614 array
= g_ptr_array_new ();
616 g_assert_false (g_ptr_array_find (array
, "some-value", NULL
)); /* NULL index */
617 g_assert_false (g_ptr_array_find (array
, "some-value", &idx
)); /* non-NULL index */
618 g_assert_false (g_ptr_array_find_with_equal_func (array
, "some-value", g_str_equal
, NULL
)); /* NULL index */
619 g_assert_false (g_ptr_array_find_with_equal_func (array
, "some-value", g_str_equal
, &idx
)); /* non-NULL index */
621 g_ptr_array_free (array
, TRUE
);
625 pointer_array_find_non_empty (void)
629 const gchar
*str_pointer
= "static-string";
631 array
= g_ptr_array_new ();
633 g_ptr_array_add (array
, "some");
634 g_ptr_array_add (array
, "random");
635 g_ptr_array_add (array
, "values");
636 g_ptr_array_add (array
, "some");
637 g_ptr_array_add (array
, "duplicated");
638 g_ptr_array_add (array
, (gpointer
) str_pointer
);
640 g_assert_true (g_ptr_array_find_with_equal_func (array
, "random", g_str_equal
, NULL
)); /* NULL index */
641 g_assert_true (g_ptr_array_find_with_equal_func (array
, "random", g_str_equal
, &idx
)); /* non-NULL index */
642 g_assert_cmpuint (idx
, ==, 1);
644 g_assert_true (g_ptr_array_find_with_equal_func (array
, "some", g_str_equal
, &idx
)); /* duplicate element */
645 g_assert_cmpuint (idx
, ==, 0);
647 g_assert_false (g_ptr_array_find_with_equal_func (array
, "nope", g_str_equal
, NULL
));
649 g_assert_true (g_ptr_array_find_with_equal_func (array
, str_pointer
, g_str_equal
, &idx
));
650 g_assert_cmpuint (idx
, ==, 5);
652 g_assert_true (g_ptr_array_find_with_equal_func (array
, str_pointer
, NULL
, &idx
)); /* NULL equal func */
653 g_assert_cmpuint (idx
, ==, 5);
655 g_assert_true (g_ptr_array_find (array
, str_pointer
, &idx
)); /* NULL equal func */
656 g_assert_cmpuint (idx
, ==, 5);
658 g_ptr_array_free (array
, TRUE
);
662 byte_array_append (void)
668 gbarray
= g_byte_array_sized_new (1000);
669 for (i
= 0; i
< 10000; i
++)
670 g_byte_array_append (gbarray
, (guint8
*) "abcd", 4);
672 for (i
= 0; i
< 10000; i
++)
674 g_assert (gbarray
->data
[4*i
] == 'a');
675 g_assert (gbarray
->data
[4*i
+1] == 'b');
676 g_assert (gbarray
->data
[4*i
+2] == 'c');
677 g_assert (gbarray
->data
[4*i
+3] == 'd');
680 segment
= g_byte_array_free (gbarray
, FALSE
);
682 for (i
= 0; i
< 10000; i
++)
684 g_assert (segment
[4*i
] == 'a');
685 g_assert (segment
[4*i
+1] == 'b');
686 g_assert (segment
[4*i
+2] == 'c');
687 g_assert (segment
[4*i
+3] == 'd');
694 byte_array_prepend (void)
699 gbarray
= g_byte_array_new ();
700 g_byte_array_set_size (gbarray
, 1000);
702 for (i
= 0; i
< 10000; i
++)
703 g_byte_array_prepend (gbarray
, (guint8
*) "abcd", 4);
705 for (i
= 0; i
< 10000; i
++)
707 g_assert (gbarray
->data
[4*i
] == 'a');
708 g_assert (gbarray
->data
[4*i
+1] == 'b');
709 g_assert (gbarray
->data
[4*i
+2] == 'c');
710 g_assert (gbarray
->data
[4*i
+3] == 'd');
713 g_byte_array_free (gbarray
, TRUE
);
717 byte_array_ref_count (void)
720 GByteArray
*gbarray2
;
723 gbarray
= g_byte_array_new ();
724 for (i
= 0; i
< 10000; i
++)
725 g_byte_array_append (gbarray
, (guint8
*) "abcd", 4);
727 gbarray2
= g_byte_array_ref (gbarray
);
728 g_assert (gbarray2
== gbarray
);
729 g_byte_array_unref (gbarray2
);
730 for (i
= 0; i
< 10000; i
++)
732 g_assert (gbarray
->data
[4*i
] == 'a');
733 g_assert (gbarray
->data
[4*i
+1] == 'b');
734 g_assert (gbarray
->data
[4*i
+2] == 'c');
735 g_assert (gbarray
->data
[4*i
+3] == 'd');
738 gbarray2
= g_byte_array_ref (gbarray
);
739 g_assert (gbarray2
== gbarray
);
740 g_byte_array_free (gbarray
, TRUE
);
741 g_assert_cmpint (gbarray2
->len
, ==, 0);
742 g_byte_array_unref (gbarray2
);
746 byte_array_remove (void)
751 gbarray
= g_byte_array_new ();
752 for (i
= 0; i
< 100; i
++)
753 g_byte_array_append (gbarray
, (guint8
*) "abcd", 4);
755 g_assert_cmpint (gbarray
->len
, ==, 400);
757 g_byte_array_remove_index (gbarray
, 4);
758 g_byte_array_remove_index (gbarray
, 4);
759 g_byte_array_remove_index (gbarray
, 4);
760 g_byte_array_remove_index (gbarray
, 4);
762 g_assert_cmpint (gbarray
->len
, ==, 396);
764 for (i
= 0; i
< 99; i
++)
766 g_assert (gbarray
->data
[4*i
] == 'a');
767 g_assert (gbarray
->data
[4*i
+1] == 'b');
768 g_assert (gbarray
->data
[4*i
+2] == 'c');
769 g_assert (gbarray
->data
[4*i
+3] == 'd');
772 g_byte_array_free (gbarray
, TRUE
);
776 byte_array_remove_fast (void)
781 gbarray
= g_byte_array_new ();
782 for (i
= 0; i
< 100; i
++)
783 g_byte_array_append (gbarray
, (guint8
*) "abcd", 4);
785 g_assert_cmpint (gbarray
->len
, ==, 400);
787 g_byte_array_remove_index_fast (gbarray
, 4);
788 g_byte_array_remove_index_fast (gbarray
, 4);
789 g_byte_array_remove_index_fast (gbarray
, 4);
790 g_byte_array_remove_index_fast (gbarray
, 4);
792 g_assert_cmpint (gbarray
->len
, ==, 396);
794 for (i
= 0; i
< 99; i
++)
796 g_assert (gbarray
->data
[4*i
] == 'a');
797 g_assert (gbarray
->data
[4*i
+1] == 'b');
798 g_assert (gbarray
->data
[4*i
+2] == 'c');
799 g_assert (gbarray
->data
[4*i
+3] == 'd');
802 g_byte_array_free (gbarray
, TRUE
);
806 byte_array_remove_range (void)
811 gbarray
= g_byte_array_new ();
812 for (i
= 0; i
< 100; i
++)
813 g_byte_array_append (gbarray
, (guint8
*) "abcd", 4);
815 g_assert_cmpint (gbarray
->len
, ==, 400);
817 g_byte_array_remove_range (gbarray
, 12, 4);
819 g_assert_cmpint (gbarray
->len
, ==, 396);
821 for (i
= 0; i
< 99; i
++)
823 g_assert (gbarray
->data
[4*i
] == 'a');
824 g_assert (gbarray
->data
[4*i
+1] == 'b');
825 g_assert (gbarray
->data
[4*i
+2] == 'c');
826 g_assert (gbarray
->data
[4*i
+3] == 'd');
829 /* Ensure the entire array can be cleared, even when empty. */
830 g_byte_array_remove_range (gbarray
, 0, gbarray
->len
);
831 g_byte_array_remove_range (gbarray
, 0, gbarray
->len
);
833 g_byte_array_free (gbarray
, TRUE
);
837 byte_compare (gconstpointer p1
, gconstpointer p2
)
839 const guint8
*i1
= p1
;
840 const guint8
*i2
= p2
;
846 byte_compare_data (gconstpointer p1
, gconstpointer p2
, gpointer data
)
848 const guint8
*i1
= p1
;
849 const guint8
*i2
= p2
;
855 byte_array_sort (void)
862 gbarray
= g_byte_array_new ();
863 for (i
= 0; i
< 100; i
++)
865 val
= 'a' + g_random_int_range (0, 26);
866 g_byte_array_append (gbarray
, (guint8
*) &val
, 1);
869 g_byte_array_sort (gbarray
, byte_compare
);
872 for (i
= 0; i
< gbarray
->len
; i
++)
874 cur
= gbarray
->data
[i
];
875 g_assert_cmpint (prev
, <=, cur
);
879 g_byte_array_free (gbarray
, TRUE
);
883 byte_array_sort_with_data (void)
890 gbarray
= g_byte_array_new ();
891 for (i
= 0; i
< 100; i
++)
893 val
= 'a' + g_random_int_range (0, 26);
894 g_byte_array_append (gbarray
, (guint8
*) &val
, 1);
897 g_byte_array_sort_with_data (gbarray
, byte_compare_data
, NULL
);
900 for (i
= 0; i
< gbarray
->len
; i
++)
902 cur
= gbarray
->data
[i
];
903 g_assert_cmpint (prev
, <=, cur
);
907 g_byte_array_free (gbarray
, TRUE
);
911 byte_array_new_take (void)
916 data
= g_memdup ("woooweeewow", 11);
917 gbarray
= g_byte_array_new_take (data
, 11);
918 g_assert (gbarray
->data
== data
);
919 g_assert_cmpuint (gbarray
->len
, ==, 11);
920 g_byte_array_free (gbarray
, TRUE
);
924 byte_array_free_to_bytes (void)
931 gbarray
= g_byte_array_new ();
932 g_byte_array_append (gbarray
, (guint8
*)"woooweeewow", 11);
933 memory
= gbarray
->data
;
935 bytes
= g_byte_array_free_to_bytes (gbarray
);
936 g_assert (bytes
!= NULL
);
937 g_assert_cmpuint (g_bytes_get_size (bytes
), ==, 11);
938 g_assert (g_bytes_get_data (bytes
, &size
) == memory
);
939 g_assert_cmpuint (size
, ==, 11);
941 g_bytes_unref (bytes
);
944 main (int argc
, char *argv
[])
946 g_test_init (&argc
, &argv
, NULL
);
948 g_test_bug_base ("https://bugzilla.gnome.org/");
951 g_test_add_func ("/array/new/cleared", array_new_cleared
);
952 g_test_add_func ("/array/new/sized-cleared", array_new_sized_cleared
);
953 g_test_add_func ("/array/new/zero-terminated", array_new_zero_terminated
);
954 g_test_add_func ("/array/append", array_append
);
955 g_test_add_func ("/array/prepend", array_prepend
);
956 g_test_add_func ("/array/remove", array_remove
);
957 g_test_add_func ("/array/remove-fast", array_remove_fast
);
958 g_test_add_func ("/array/remove-range", array_remove_range
);
959 g_test_add_func ("/array/ref-count", array_ref_count
);
960 g_test_add_func ("/array/sort", array_sort
);
961 g_test_add_func ("/array/sort-with-data", array_sort_with_data
);
962 g_test_add_func ("/array/clear-func", array_clear_func
);
965 g_test_add_func ("/pointerarray/add", pointer_array_add
);
966 g_test_add_func ("/pointerarray/insert", pointer_array_insert
);
967 g_test_add_func ("/pointerarray/ref-count", pointer_array_ref_count
);
968 g_test_add_func ("/pointerarray/free-func", pointer_array_free_func
);
969 g_test_add_func ("/pointerarray/sort", pointer_array_sort
);
970 g_test_add_func ("/pointerarray/sort-with-data", pointer_array_sort_with_data
);
971 g_test_add_func ("/pointerarray/find/empty", pointer_array_find_empty
);
972 g_test_add_func ("/pointerarray/find/non-empty", pointer_array_find_non_empty
);
975 g_test_add_func ("/bytearray/append", byte_array_append
);
976 g_test_add_func ("/bytearray/prepend", byte_array_prepend
);
977 g_test_add_func ("/bytearray/remove", byte_array_remove
);
978 g_test_add_func ("/bytearray/remove-fast", byte_array_remove_fast
);
979 g_test_add_func ("/bytearray/remove-range", byte_array_remove_range
);
980 g_test_add_func ("/bytearray/ref-count", byte_array_ref_count
);
981 g_test_add_func ("/bytearray/sort", byte_array_sort
);
982 g_test_add_func ("/bytearray/sort-with-data", byte_array_sort_with_data
);
983 g_test_add_func ("/bytearray/new-take", byte_array_new_take
);
984 g_test_add_func ("/bytearray/free-to-bytes", byte_array_free_to_bytes
);
986 return g_test_run ();