Add some more cases to the app-id unit tests
[glib.git] / glib / tests / array-test.c
bloba0074d4c373a09932870959346c5efc616caa43e
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
26 #undef G_LOG_DOMAIN
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include "glib.h"
33 static void
34 sum_up (gpointer data,
35 gpointer user_data)
37 gint *sum = (gint *)user_data;
39 *sum += GPOINTER_TO_INT (data);
42 static void
43 array_append (void)
45 GArray *garray;
46 gint i;
47 gint *segment;
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);
59 g_free (segment);
62 static void
63 array_prepend (void)
65 GArray *garray;
66 gint 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);
78 static void
79 array_remove (void)
81 GArray *garray;
82 gint i;
83 gint prev, cur;
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);
98 prev = -1;
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);
104 prev = cur;
107 g_array_free (garray, TRUE);
110 static void
111 array_remove_fast (void)
113 GArray *garray;
114 gint i;
115 gint prev, cur;
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);
130 prev = -1;
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);
135 if (cur < 96)
137 g_assert_cmpint (prev, <, cur);
138 prev = cur;
142 g_array_free (garray, TRUE);
145 static void
146 array_remove_range (void)
148 GArray *garray;
149 gint i;
150 gint prev, cur;
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);
162 prev = -1;
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);
168 prev = cur;
171 /* Ensure the entire array can be cleared, even when empty. */
172 g_array_remove_range (garray, 0, garray->len);
173 g_array_remove_range (garray, 0, garray->len);
175 g_array_free (garray, TRUE);
178 static void
179 array_ref_count (void)
181 GArray *garray;
182 GArray *garray2;
183 gint i;
185 garray = g_array_new (FALSE, FALSE, sizeof (gint));
186 g_assert_cmpint (g_array_get_element_size (garray), ==, sizeof (gint));
187 for (i = 0; i < 100; i++)
188 g_array_prepend_val (garray, i);
190 /* check we can ref, unref and still access the array */
191 garray2 = g_array_ref (garray);
192 g_assert (garray == garray2);
193 g_array_unref (garray2);
194 for (i = 0; i < 100; i++)
195 g_assert_cmpint (g_array_index (garray, gint, i), ==, (100 - i - 1));
197 /* garray2 should be an empty valid GArray wrapper */
198 garray2 = g_array_ref (garray);
199 g_array_free (garray, TRUE);
201 g_assert_cmpint (garray2->len, ==, 0);
202 g_array_unref (garray2);
205 static int
206 int_compare (gconstpointer p1, gconstpointer p2)
208 const gint *i1 = p1;
209 const gint *i2 = p2;
211 return *i1 - *i2;
214 static int
215 int_compare_data (gconstpointer p1, gconstpointer p2, gpointer data)
217 const gint *i1 = p1;
218 const gint *i2 = p2;
220 return *i1 - *i2;
222 static void
223 array_sort (void)
225 GArray *garray;
226 gint i;
227 gint prev, cur;
229 garray = g_array_new (FALSE, FALSE, sizeof (gint));
230 for (i = 0; i < 10000; i++)
232 cur = g_random_int_range (0, 10000);
233 g_array_append_val (garray, cur);
235 g_array_sort (garray, int_compare);
237 prev = -1;
238 for (i = 0; i < garray->len; i++)
240 cur = g_array_index (garray, gint, i);
241 g_assert_cmpint (prev, <=, cur);
242 prev = cur;
245 g_array_free (garray, TRUE);
248 static void
249 array_sort_with_data (void)
251 GArray *garray;
252 gint i;
253 gint prev, cur;
255 garray = g_array_new (FALSE, FALSE, sizeof (gint));
256 for (i = 0; i < 10000; i++)
258 cur = g_random_int_range (0, 10000);
259 g_array_append_val (garray, cur);
261 g_array_sort_with_data (garray, int_compare_data, NULL);
263 prev = -1;
264 for (i = 0; i < garray->len; i++)
266 cur = g_array_index (garray, gint, i);
267 g_assert_cmpint (prev, <=, cur);
268 prev = cur;
271 g_array_free (garray, TRUE);
274 static gint num_clear_func_invocations = 0;
276 static void
277 my_clear_func (gpointer data)
279 num_clear_func_invocations += 1;
282 static void
283 array_clear_func (void)
285 GArray *garray;
286 gint i;
287 gint cur;
289 garray = g_array_new (FALSE, FALSE, sizeof (gint));
290 g_array_set_clear_func (garray, my_clear_func);
292 for (i = 0; i < 10; i++)
294 cur = g_random_int_range (0, 100);
295 g_array_append_val (garray, cur);
298 g_array_remove_index (garray, 9);
299 g_assert_cmpint (num_clear_func_invocations, ==, 1);
301 g_array_remove_range (garray, 5, 3);
302 g_assert_cmpint (num_clear_func_invocations, ==, 4);
304 g_array_remove_index_fast (garray, 4);
305 g_assert_cmpint (num_clear_func_invocations, ==, 5);
307 g_array_free (garray, TRUE);
308 g_assert_cmpint (num_clear_func_invocations, ==, 10);
311 static void
312 pointer_array_add (void)
314 GPtrArray *gparray;
315 gint i;
316 gint sum = 0;
317 gpointer *segment;
319 gparray = g_ptr_array_sized_new (1000);
321 for (i = 0; i < 10000; i++)
322 g_ptr_array_add (gparray, GINT_TO_POINTER (i));
324 for (i = 0; i < 10000; i++)
325 g_assert (g_ptr_array_index (gparray, i) == GINT_TO_POINTER (i));
327 g_ptr_array_foreach (gparray, sum_up, &sum);
328 g_assert (sum == 49995000);
330 segment = g_ptr_array_free (gparray, FALSE);
331 for (i = 0; i < 10000; i++)
332 g_assert (segment[i] == GINT_TO_POINTER (i));
333 g_free (segment);
336 static void
337 pointer_array_insert (void)
339 GPtrArray *gparray;
340 gint i;
341 gint sum = 0;
342 gint index;
344 gparray = g_ptr_array_sized_new (1000);
346 for (i = 0; i < 10000; i++)
348 index = g_random_int_range (-1, i + 1);
349 g_ptr_array_insert (gparray, index, GINT_TO_POINTER (i));
352 g_ptr_array_foreach (gparray, sum_up, &sum);
353 g_assert (sum == 49995000);
355 g_ptr_array_free (gparray, TRUE);
358 static void
359 pointer_array_ref_count (void)
361 GPtrArray *gparray;
362 GPtrArray *gparray2;
363 gint i;
364 gint sum = 0;
366 gparray = g_ptr_array_new ();
367 for (i = 0; i < 10000; i++)
368 g_ptr_array_add (gparray, GINT_TO_POINTER (i));
370 /* check we can ref, unref and still access the array */
371 gparray2 = g_ptr_array_ref (gparray);
372 g_assert (gparray == gparray2);
373 g_ptr_array_unref (gparray2);
374 for (i = 0; i < 10000; i++)
375 g_assert (g_ptr_array_index (gparray, i) == GINT_TO_POINTER (i));
377 g_ptr_array_foreach (gparray, sum_up, &sum);
378 g_assert (sum == 49995000);
380 /* gparray2 should be an empty valid GPtrArray wrapper */
381 gparray2 = g_ptr_array_ref (gparray);
382 g_ptr_array_free (gparray, TRUE);
384 g_assert_cmpint (gparray2->len, ==, 0);
385 g_ptr_array_unref (gparray2);
388 static gint num_free_func_invocations = 0;
390 static void
391 my_free_func (gpointer data)
393 num_free_func_invocations++;
394 g_free (data);
397 static void
398 pointer_array_free_func (void)
400 GPtrArray *gparray;
401 GPtrArray *gparray2;
402 gchar **strv;
403 gchar *s;
405 num_free_func_invocations = 0;
406 gparray = g_ptr_array_new_with_free_func (my_free_func);
407 g_ptr_array_unref (gparray);
408 g_assert_cmpint (num_free_func_invocations, ==, 0);
410 gparray = g_ptr_array_new_with_free_func (my_free_func);
411 g_ptr_array_free (gparray, TRUE);
412 g_assert_cmpint (num_free_func_invocations, ==, 0);
414 num_free_func_invocations = 0;
415 gparray = g_ptr_array_new_with_free_func (my_free_func);
416 g_ptr_array_add (gparray, g_strdup ("foo"));
417 g_ptr_array_add (gparray, g_strdup ("bar"));
418 g_ptr_array_add (gparray, g_strdup ("baz"));
419 g_ptr_array_remove_index (gparray, 0);
420 g_assert_cmpint (num_free_func_invocations, ==, 1);
421 g_ptr_array_remove_index_fast (gparray, 1);
422 g_assert_cmpint (num_free_func_invocations, ==, 2);
423 s = g_strdup ("frob");
424 g_ptr_array_add (gparray, s);
425 g_assert (g_ptr_array_remove (gparray, s));
426 g_assert (!g_ptr_array_remove (gparray, "nuun"));
427 g_assert (!g_ptr_array_remove_fast (gparray, "mlo"));
428 g_assert_cmpint (num_free_func_invocations, ==, 3);
429 s = g_strdup ("frob");
430 g_ptr_array_add (gparray, s);
431 g_ptr_array_set_size (gparray, 1);
432 g_assert_cmpint (num_free_func_invocations, ==, 4);
433 g_ptr_array_ref (gparray);
434 g_ptr_array_unref (gparray);
435 g_assert_cmpint (num_free_func_invocations, ==, 4);
436 g_ptr_array_unref (gparray);
437 g_assert_cmpint (num_free_func_invocations, ==, 5);
439 num_free_func_invocations = 0;
440 gparray = g_ptr_array_new_full (10, my_free_func);
441 g_ptr_array_add (gparray, g_strdup ("foo"));
442 g_ptr_array_add (gparray, g_strdup ("bar"));
443 g_ptr_array_add (gparray, g_strdup ("baz"));
444 g_ptr_array_set_size (gparray, 20);
445 g_ptr_array_add (gparray, NULL);
446 gparray2 = g_ptr_array_ref (gparray);
447 strv = (gchar **) g_ptr_array_free (gparray, FALSE);
448 g_assert_cmpint (num_free_func_invocations, ==, 0);
449 g_strfreev (strv);
450 g_ptr_array_unref (gparray2);
451 g_assert_cmpint (num_free_func_invocations, ==, 0);
453 num_free_func_invocations = 0;
454 gparray = g_ptr_array_new_with_free_func (my_free_func);
455 g_ptr_array_add (gparray, g_strdup ("foo"));
456 g_ptr_array_add (gparray, g_strdup ("bar"));
457 g_ptr_array_add (gparray, g_strdup ("baz"));
458 g_ptr_array_remove_range (gparray, 1, 1);
459 g_ptr_array_unref (gparray);
460 g_assert_cmpint (num_free_func_invocations, ==, 3);
462 num_free_func_invocations = 0;
463 gparray = g_ptr_array_new_with_free_func (my_free_func);
464 g_ptr_array_add (gparray, g_strdup ("foo"));
465 g_ptr_array_add (gparray, g_strdup ("bar"));
466 g_ptr_array_add (gparray, g_strdup ("baz"));
467 g_ptr_array_free (gparray, TRUE);
468 g_assert_cmpint (num_free_func_invocations, ==, 3);
470 num_free_func_invocations = 0;
471 gparray = g_ptr_array_new_with_free_func (my_free_func);
472 g_ptr_array_add (gparray, "foo");
473 g_ptr_array_add (gparray, "bar");
474 g_ptr_array_add (gparray, "baz");
475 g_ptr_array_set_free_func (gparray, NULL);
476 g_ptr_array_free (gparray, TRUE);
477 g_assert_cmpint (num_free_func_invocations, ==, 0);
480 static gint
481 ptr_compare (gconstpointer p1, gconstpointer p2)
483 gpointer i1 = *(gpointer*)p1;
484 gpointer i2 = *(gpointer*)p2;
486 return GPOINTER_TO_INT (i1) - GPOINTER_TO_INT (i2);
489 static gint
490 ptr_compare_data (gconstpointer p1, gconstpointer p2, gpointer data)
492 gpointer i1 = *(gpointer*)p1;
493 gpointer i2 = *(gpointer*)p2;
495 return GPOINTER_TO_INT (i1) - GPOINTER_TO_INT (i2);
498 static void
499 pointer_array_sort (void)
501 GPtrArray *gparray;
502 gint i;
503 gint val;
504 gint prev, cur;
506 gparray = g_ptr_array_new ();
507 for (i = 0; i < 10000; i++)
509 val = g_random_int_range (0, 10000);
510 g_ptr_array_add (gparray, GINT_TO_POINTER (val));
513 g_ptr_array_sort (gparray, ptr_compare);
515 prev = -1;
516 for (i = 0; i < 10000; i++)
518 cur = GPOINTER_TO_INT (g_ptr_array_index (gparray, i));
519 g_assert_cmpint (prev, <=, cur);
520 prev = cur;
523 g_ptr_array_free (gparray, TRUE);
526 static void
527 pointer_array_sort_with_data (void)
529 GPtrArray *gparray;
530 gint i;
531 gint prev, cur;
533 gparray = g_ptr_array_new ();
534 for (i = 0; i < 10000; i++)
535 g_ptr_array_add (gparray, GINT_TO_POINTER (g_random_int_range (0, 10000)));
537 g_ptr_array_sort_with_data (gparray, ptr_compare_data, NULL);
539 prev = -1;
540 for (i = 0; i < 10000; i++)
542 cur = GPOINTER_TO_INT (g_ptr_array_index (gparray, i));
543 g_assert_cmpint (prev, <=, cur);
544 prev = cur;
547 g_ptr_array_free (gparray, TRUE);
550 static void
551 byte_array_append (void)
553 GByteArray *gbarray;
554 gint i;
555 guint8 *segment;
557 gbarray = g_byte_array_sized_new (1000);
558 for (i = 0; i < 10000; i++)
559 g_byte_array_append (gbarray, (guint8*) "abcd", 4);
561 for (i = 0; i < 10000; i++)
563 g_assert (gbarray->data[4*i] == 'a');
564 g_assert (gbarray->data[4*i+1] == 'b');
565 g_assert (gbarray->data[4*i+2] == 'c');
566 g_assert (gbarray->data[4*i+3] == 'd');
569 segment = g_byte_array_free (gbarray, FALSE);
571 for (i = 0; i < 10000; i++)
573 g_assert (segment[4*i] == 'a');
574 g_assert (segment[4*i+1] == 'b');
575 g_assert (segment[4*i+2] == 'c');
576 g_assert (segment[4*i+3] == 'd');
579 g_free (segment);
582 static void
583 byte_array_prepend (void)
585 GByteArray *gbarray;
586 gint i;
588 gbarray = g_byte_array_new ();
589 g_byte_array_set_size (gbarray, 1000);
591 for (i = 0; i < 10000; i++)
592 g_byte_array_prepend (gbarray, (guint8*) "abcd", 4);
594 for (i = 0; i < 10000; i++)
596 g_assert (gbarray->data[4*i] == 'a');
597 g_assert (gbarray->data[4*i+1] == 'b');
598 g_assert (gbarray->data[4*i+2] == 'c');
599 g_assert (gbarray->data[4*i+3] == 'd');
602 g_byte_array_free (gbarray, TRUE);
605 static void
606 byte_array_ref_count (void)
608 GByteArray *gbarray;
609 GByteArray *gbarray2;
610 gint i;
612 gbarray = g_byte_array_new ();
613 for (i = 0; i < 10000; i++)
614 g_byte_array_append (gbarray, (guint8*) "abcd", 4);
616 gbarray2 = g_byte_array_ref (gbarray);
617 g_assert (gbarray2 == gbarray);
618 g_byte_array_unref (gbarray2);
619 for (i = 0; i < 10000; i++)
621 g_assert (gbarray->data[4*i] == 'a');
622 g_assert (gbarray->data[4*i+1] == 'b');
623 g_assert (gbarray->data[4*i+2] == 'c');
624 g_assert (gbarray->data[4*i+3] == 'd');
627 gbarray2 = g_byte_array_ref (gbarray);
628 g_assert (gbarray2 == gbarray);
629 g_byte_array_free (gbarray, TRUE);
630 g_assert_cmpint (gbarray2->len, ==, 0);
631 g_byte_array_unref (gbarray2);
634 static void
635 byte_array_remove (void)
637 GByteArray *gbarray;
638 gint i;
640 gbarray = g_byte_array_new ();
641 for (i = 0; i < 100; i++)
642 g_byte_array_append (gbarray, (guint8*) "abcd", 4);
644 g_assert_cmpint (gbarray->len, ==, 400);
646 g_byte_array_remove_index (gbarray, 4);
647 g_byte_array_remove_index (gbarray, 4);
648 g_byte_array_remove_index (gbarray, 4);
649 g_byte_array_remove_index (gbarray, 4);
651 g_assert_cmpint (gbarray->len, ==, 396);
653 for (i = 0; i < 99; i++)
655 g_assert (gbarray->data[4*i] == 'a');
656 g_assert (gbarray->data[4*i+1] == 'b');
657 g_assert (gbarray->data[4*i+2] == 'c');
658 g_assert (gbarray->data[4*i+3] == 'd');
661 g_byte_array_free (gbarray, TRUE);
664 static void
665 byte_array_remove_fast (void)
667 GByteArray *gbarray;
668 gint i;
670 gbarray = g_byte_array_new ();
671 for (i = 0; i < 100; i++)
672 g_byte_array_append (gbarray, (guint8*) "abcd", 4);
674 g_assert_cmpint (gbarray->len, ==, 400);
676 g_byte_array_remove_index_fast (gbarray, 4);
677 g_byte_array_remove_index_fast (gbarray, 4);
678 g_byte_array_remove_index_fast (gbarray, 4);
679 g_byte_array_remove_index_fast (gbarray, 4);
681 g_assert_cmpint (gbarray->len, ==, 396);
683 for (i = 0; i < 99; i++)
685 g_assert (gbarray->data[4*i] == 'a');
686 g_assert (gbarray->data[4*i+1] == 'b');
687 g_assert (gbarray->data[4*i+2] == 'c');
688 g_assert (gbarray->data[4*i+3] == 'd');
691 g_byte_array_free (gbarray, TRUE);
694 static void
695 byte_array_remove_range (void)
697 GByteArray *gbarray;
698 gint i;
700 gbarray = g_byte_array_new ();
701 for (i = 0; i < 100; i++)
702 g_byte_array_append (gbarray, (guint8*) "abcd", 4);
704 g_assert_cmpint (gbarray->len, ==, 400);
706 g_byte_array_remove_range (gbarray, 12, 4);
708 g_assert_cmpint (gbarray->len, ==, 396);
710 for (i = 0; i < 99; i++)
712 g_assert (gbarray->data[4*i] == 'a');
713 g_assert (gbarray->data[4*i+1] == 'b');
714 g_assert (gbarray->data[4*i+2] == 'c');
715 g_assert (gbarray->data[4*i+3] == 'd');
718 /* Ensure the entire array can be cleared, even when empty. */
719 g_byte_array_remove_range (gbarray, 0, gbarray->len);
720 g_byte_array_remove_range (gbarray, 0, gbarray->len);
722 g_byte_array_free (gbarray, TRUE);
725 static int
726 byte_compare (gconstpointer p1, gconstpointer p2)
728 const guint8 *i1 = p1;
729 const guint8 *i2 = p2;
731 return *i1 - *i2;
734 static int
735 byte_compare_data (gconstpointer p1, gconstpointer p2, gpointer data)
737 const guint8 *i1 = p1;
738 const guint8 *i2 = p2;
740 return *i1 - *i2;
743 static void
744 byte_array_sort (void)
746 GByteArray *gbarray;
747 gint i;
748 guint8 val;
749 guint8 prev, cur;
751 gbarray = g_byte_array_new ();
752 for (i = 0; i < 100; i++)
754 val = 'a' + g_random_int_range (0, 26);
755 g_byte_array_append (gbarray, (guint8*) &val, 1);
758 g_byte_array_sort (gbarray, byte_compare);
760 prev = 'a';
761 for (i = 0; i < gbarray->len; i++)
763 cur = gbarray->data[i];
764 g_assert_cmpint (prev, <=, cur);
765 prev = cur;
768 g_byte_array_free (gbarray, TRUE);
771 static void
772 byte_array_sort_with_data (void)
774 GByteArray *gbarray;
775 gint i;
776 guint8 val;
777 guint8 prev, cur;
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_with_data (gbarray, byte_compare_data, NULL);
788 prev = 'a';
789 for (i = 0; i < gbarray->len; i++)
791 cur = gbarray->data[i];
792 g_assert_cmpint (prev, <=, cur);
793 prev = cur;
796 g_byte_array_free (gbarray, TRUE);
799 static void
800 byte_array_new_take (void)
802 GByteArray *gbarray;
803 guint8 *data;
805 data = g_memdup ("woooweeewow", 11);
806 gbarray = g_byte_array_new_take (data, 11);
807 g_assert (gbarray->data == data);
808 g_assert_cmpuint (gbarray->len, ==, 11);
809 g_byte_array_free (gbarray, TRUE);
812 static void
813 byte_array_free_to_bytes (void)
815 GByteArray *gbarray;
816 gpointer memory;
817 GBytes *bytes;
818 gsize size;
820 gbarray = g_byte_array_new ();
821 g_byte_array_append (gbarray, (guint8 *)"woooweeewow", 11);
822 memory = gbarray->data;
824 bytes = g_byte_array_free_to_bytes (gbarray);
825 g_assert (bytes != NULL);
826 g_assert_cmpuint (g_bytes_get_size (bytes), ==, 11);
827 g_assert (g_bytes_get_data (bytes, &size) == memory);
828 g_assert_cmpuint (size, ==, 11);
830 g_bytes_unref (bytes);
833 main (int argc, char *argv[])
835 g_test_init (&argc, &argv, NULL);
837 g_test_bug_base ("http://bugs.gnome.org/");
839 /* array tests */
840 g_test_add_func ("/array/append", array_append);
841 g_test_add_func ("/array/prepend", array_prepend);
842 g_test_add_func ("/array/remove", array_remove);
843 g_test_add_func ("/array/remove-fast", array_remove_fast);
844 g_test_add_func ("/array/remove-range", array_remove_range);
845 g_test_add_func ("/array/ref-count", array_ref_count);
846 g_test_add_func ("/array/sort", array_sort);
847 g_test_add_func ("/array/sort-with-data", array_sort_with_data);
848 g_test_add_func ("/array/clear-func", array_clear_func);
850 /* pointer arrays */
851 g_test_add_func ("/pointerarray/add", pointer_array_add);
852 g_test_add_func ("/pointerarray/insert", pointer_array_insert);
853 g_test_add_func ("/pointerarray/ref-count", pointer_array_ref_count);
854 g_test_add_func ("/pointerarray/free-func", pointer_array_free_func);
855 g_test_add_func ("/pointerarray/sort", pointer_array_sort);
856 g_test_add_func ("/pointerarray/sort-with-data", pointer_array_sort_with_data);
858 /* byte arrays */
859 g_test_add_func ("/bytearray/append", byte_array_append);
860 g_test_add_func ("/bytearray/prepend", byte_array_prepend);
861 g_test_add_func ("/bytearray/remove", byte_array_remove);
862 g_test_add_func ("/bytearray/remove-fast", byte_array_remove_fast);
863 g_test_add_func ("/bytearray/remove-range", byte_array_remove_range);
864 g_test_add_func ("/bytearray/ref-count", byte_array_ref_count);
865 g_test_add_func ("/bytearray/sort", byte_array_sort);
866 g_test_add_func ("/bytearray/sort-with-data", byte_array_sort_with_data);
867 g_test_add_func ("/bytearray/new-take", byte_array_new_take);
868 g_test_add_func ("/bytearray/free-to-bytes", byte_array_free_to_bytes);
870 return g_test_run ();