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
32 /* Test data to be passed to any function which calls g_array_new(), providing
33 * the parameters for that call. Most #GArray tests should be repeated for all
34 * possible values of #ArrayTestData. */
37 gboolean zero_terminated;
41 /* Assert that @garray contains @n_expected_elements as given in @expected_data.
42 * @garray must contain #gint elements. */
44 assert_int_array_equal (GArray *garray,
45 const gint *expected_data,
46 gsize n_expected_elements)
50 g_assert_cmpuint (garray->len, ==, n_expected_elements);
51 for (i = 0; i < garray->len; i++)
52 g_assert_cmpint (g_array_index (garray, gint, i), ==, expected_data[i]);
55 /* Iff config->zero_terminated is %TRUE, assert that the final element of
56 * @garray is zero. @garray must contain #gint elements. */
58 assert_int_array_zero_terminated (const ArrayTestData *config,
61 if (config->zero_terminated)
63 gint *data = (gint *) garray->data;
64 g_assert_cmpint (data[garray->len], ==, 0);
69 sum_up (gpointer data,
72 gint *sum = (gint *)user_data;
74 *sum += GPOINTER_TO_INT (data);
77 /* Check that expanding an array with g_array_set_size() clears the new elements
78 * if @clear_ was specified during construction. */
80 array_set_size (gconstpointer test_data)
82 const ArrayTestData *config = test_data;
86 garray = g_array_new (config->zero_terminated, config->clear_, sizeof (gint));
87 g_assert_cmpuint (garray->len, ==, 0);
88 assert_int_array_zero_terminated (config, garray);
90 g_array_set_size (garray, 5);
91 g_assert_cmpuint (garray->len, ==, 5);
92 assert_int_array_zero_terminated (config, garray);
95 for (i = 0; i < 5; i++)
96 g_assert_cmpint (g_array_index (garray, gint, i), ==, 0);
98 g_array_unref (garray);
101 /* As with array_set_size(), but with a sized array. */
103 array_set_size_sized (gconstpointer test_data)
105 const ArrayTestData *config = test_data;
109 garray = g_array_sized_new (config->zero_terminated, config->clear_, sizeof (gint), 10);
110 g_assert_cmpuint (garray->len, ==, 0);
111 assert_int_array_zero_terminated (config, garray);
113 g_array_set_size (garray, 5);
114 g_assert_cmpuint (garray->len, ==, 5);
115 assert_int_array_zero_terminated (config, garray);
118 for (i = 0; i < 5; i++)
119 g_assert_cmpint (g_array_index (garray, gint, i), ==, 0);
121 g_array_unref (garray);
124 /* Check that a zero-terminated array does actually have a zero terminator. */
126 array_new_zero_terminated (void)
129 gchar *out_str = NULL;
131 garray = g_array_new (TRUE, FALSE, sizeof (gchar));
132 g_assert_cmpuint (garray->len, ==, 0);
134 g_array_append_vals (garray, "hello", strlen ("hello"));
135 g_assert_cmpuint (garray->len, ==, 5);
136 g_assert_cmpstr (garray->data, ==, "hello");
138 out_str = g_array_free (garray, FALSE);
139 g_assert_cmpstr (out_str, ==, "hello");
143 /* Check g_array_steal() function */
147 const guint array_size = 10000;
153 garray = g_array_new (FALSE, FALSE, sizeof (gint));
154 adata = (gint *) g_array_steal (garray, NULL);
155 g_assert_null (adata);
157 adata = (gint *) g_array_steal (garray, &len);
158 g_assert_null (adata);
159 g_assert_cmpint (len, ==, 0);
161 for (i = 0; i < array_size; i++)
162 g_array_append_val (garray, i);
164 for (i = 0; i < array_size; i++)
165 g_assert_cmpint (g_array_index (garray, gint, i), ==, i);
168 past_len = garray->len;
169 adata = (gint *) g_array_steal (garray, &len);
170 for (i = 0; i < array_size; i++)
171 g_assert_cmpint (adata[i], ==, i);
173 g_assert_cmpint (past_len, ==, len);
174 g_assert_cmpint (garray->len, ==, 0);
176 g_array_append_val (garray, i);
178 g_assert_cmpint (adata[0], ==, 0);
179 g_assert_cmpint (g_array_index (garray, gint, 0), ==, array_size);
180 g_assert_cmpint (garray->len, ==, 1);
182 g_array_remove_index (garray, 0);
184 for (i = 0; i < array_size; i++)
185 g_array_append_val (garray, i);
187 g_assert_cmpint (garray->len, ==, array_size);
188 g_assert_cmpmem (adata, array_size * sizeof (gint),
189 garray->data, array_size * sizeof (gint));
191 g_array_free (garray, TRUE);
194 /* Check that g_array_append_val() works correctly for various #GArray
197 array_append_val (gconstpointer test_data)
199 const ArrayTestData *config = test_data;
204 garray = g_array_new (config->zero_terminated, config->clear_, sizeof (gint));
205 for (i = 0; i < 10000; i++)
206 g_array_append_val (garray, i);
207 assert_int_array_zero_terminated (config, garray);
209 for (i = 0; i < 10000; i++)
210 g_assert_cmpint (g_array_index (garray, gint, i), ==, i);
212 segment = (gint*)g_array_free (garray, FALSE);
213 for (i = 0; i < 10000; i++)
214 g_assert_cmpint (segment[i], ==, i);
215 if (config->zero_terminated)
216 g_assert_cmpint (segment[10000], ==, 0);
221 /* Check that g_array_prepend_val() works correctly for various #GArray
224 array_prepend_val (gconstpointer test_data)
226 const ArrayTestData *config = test_data;
230 garray = g_array_new (config->zero_terminated, config->clear_, sizeof (gint));
231 for (i = 0; i < 100; i++)
232 g_array_prepend_val (garray, i);
233 assert_int_array_zero_terminated (config, garray);
235 for (i = 0; i < 100; i++)
236 g_assert_cmpint (g_array_index (garray, gint, i), ==, (100 - i - 1));
238 g_array_free (garray, TRUE);
241 /* Test that g_array_prepend_vals() works correctly with various array
244 array_prepend_vals (gconstpointer test_data)
246 const ArrayTestData *config = test_data;
247 GArray *garray, *garray_out;
248 const gint vals[] = { 0, 1, 2, 3, 4 };
249 const gint expected_vals1[] = { 0, 1 };
250 const gint expected_vals2[] = { 2, 0, 1 };
251 const gint expected_vals3[] = { 3, 4, 2, 0, 1 };
253 /* Set up an array. */
254 garray = g_array_new (config->zero_terminated, config->clear_, sizeof (gint));
255 assert_int_array_zero_terminated (config, garray);
257 /* Prepend several values to an empty array. */
258 garray_out = g_array_prepend_vals (garray, vals, 2);
259 g_assert_true (garray == garray_out);
260 assert_int_array_equal (garray, expected_vals1, G_N_ELEMENTS (expected_vals1));
261 assert_int_array_zero_terminated (config, garray);
263 /* Prepend a single value. */
264 garray_out = g_array_prepend_vals (garray, vals + 2, 1);
265 g_assert_true (garray == garray_out);
266 assert_int_array_equal (garray, expected_vals2, G_N_ELEMENTS (expected_vals2));
267 assert_int_array_zero_terminated (config, garray);
269 /* Prepend several values to a non-empty array. */
270 garray_out = g_array_prepend_vals (garray, vals + 3, 2);
271 g_assert_true (garray == garray_out);
272 assert_int_array_equal (garray, expected_vals3, G_N_ELEMENTS (expected_vals3));
273 assert_int_array_zero_terminated (config, garray);
275 /* Prepend no values. */
276 garray_out = g_array_prepend_vals (garray, vals, 0);
277 g_assert_true (garray == garray_out);
278 assert_int_array_equal (garray, expected_vals3, G_N_ELEMENTS (expected_vals3));
279 assert_int_array_zero_terminated (config, garray);
281 /* Prepend no values with %NULL data. */
282 garray_out = g_array_prepend_vals (garray, NULL, 0);
283 g_assert_true (garray == garray_out);
284 assert_int_array_equal (garray, expected_vals3, G_N_ELEMENTS (expected_vals3));
285 assert_int_array_zero_terminated (config, garray);
287 g_array_free (garray, TRUE);
290 /* Test that g_array_insert_vals() works correctly with various array
293 array_insert_vals (gconstpointer test_data)
295 const ArrayTestData *config = test_data;
296 GArray *garray, *garray_out;
298 const gint vals[] = { 0, 1, 2, 3, 4, 5, 6, 7 };
299 const gint expected_vals1[] = { 0, 1 };
300 const gint expected_vals2[] = { 0, 2, 3, 1 };
301 const gint expected_vals3[] = { 0, 2, 3, 1, 4 };
302 const gint expected_vals4[] = { 5, 0, 2, 3, 1, 4 };
303 const gint expected_vals5[] = { 5, 0, 2, 3, 1, 4, 0, 0, 0, 0, 6, 7 };
305 /* Set up an array. */
306 garray = g_array_new (config->zero_terminated, config->clear_, sizeof (gint));
307 assert_int_array_zero_terminated (config, garray);
309 /* Insert several values at the beginning. */
310 garray_out = g_array_insert_vals (garray, 0, vals, 2);
311 g_assert_true (garray == garray_out);
312 assert_int_array_equal (garray, expected_vals1, G_N_ELEMENTS (expected_vals1));
313 assert_int_array_zero_terminated (config, garray);
315 /* Insert some more part-way through. */
316 garray_out = g_array_insert_vals (garray, 1, vals + 2, 2);
317 g_assert_true (garray == garray_out);
318 assert_int_array_equal (garray, expected_vals2, G_N_ELEMENTS (expected_vals2));
319 assert_int_array_zero_terminated (config, garray);
321 /* And at the end. */
322 garray_out = g_array_insert_vals (garray, garray->len, vals + 4, 1);
323 g_assert_true (garray == garray_out);
324 assert_int_array_equal (garray, expected_vals3, G_N_ELEMENTS (expected_vals3));
325 assert_int_array_zero_terminated (config, garray);
327 /* Then back at the beginning again. */
328 garray_out = g_array_insert_vals (garray, 0, vals + 5, 1);
329 g_assert_true (garray == garray_out);
330 assert_int_array_equal (garray, expected_vals4, G_N_ELEMENTS (expected_vals4));
331 assert_int_array_zero_terminated (config, garray);
333 /* Insert zero elements. */
334 garray_out = g_array_insert_vals (garray, 0, vals, 0);
335 g_assert_true (garray == garray_out);
336 assert_int_array_equal (garray, expected_vals4, G_N_ELEMENTS (expected_vals4));
337 assert_int_array_zero_terminated (config, garray);
339 /* Insert zero elements with a %NULL pointer. */
340 garray_out = g_array_insert_vals (garray, 0, NULL, 0);
341 g_assert_true (garray == garray_out);
342 assert_int_array_equal (garray, expected_vals4, G_N_ELEMENTS (expected_vals4));
343 assert_int_array_zero_terminated (config, garray);
345 /* Insert some elements off the end of the array. The behaviour here depends
346 * on whether the array clears entries. */
347 garray_out = g_array_insert_vals (garray, garray->len + 4, vals + 6, 2);
348 g_assert_true (garray == garray_out);
350 g_assert_cmpuint (garray->len, ==, G_N_ELEMENTS (expected_vals5));
351 for (i = 0; i < G_N_ELEMENTS (expected_vals5); i++)
353 if (config->clear_ || i < 6 || i > 9)
354 g_assert_cmpint (g_array_index (garray, gint, i), ==, expected_vals5[i]);
357 assert_int_array_zero_terminated (config, garray);
359 g_array_free (garray, TRUE);
362 /* Check that g_array_remove_index() works correctly for various #GArray
365 array_remove_index (gconstpointer test_data)
367 const ArrayTestData *config = test_data;
372 garray = g_array_new (config->zero_terminated, config->clear_, sizeof (gint));
373 for (i = 0; i < 100; i++)
374 g_array_append_val (garray, i);
375 assert_int_array_zero_terminated (config, garray);
377 g_assert_cmpint (garray->len, ==, 100);
379 g_array_remove_index (garray, 1);
380 g_array_remove_index (garray, 3);
381 g_array_remove_index (garray, 21);
382 g_array_remove_index (garray, 57);
384 g_assert_cmpint (garray->len, ==, 96);
385 assert_int_array_zero_terminated (config, garray);
388 for (i = 0; i < garray->len; i++)
390 cur = g_array_index (garray, gint, i);
391 g_assert (cur != 1 && cur != 4 && cur != 23 && cur != 60);
392 g_assert_cmpint (prev, <, cur);
396 g_array_free (garray, TRUE);
399 /* Check that g_array_remove_index_fast() works correctly for various #GArray
402 array_remove_index_fast (gconstpointer test_data)
404 const ArrayTestData *config = test_data;
409 garray = g_array_new (config->zero_terminated, config->clear_, sizeof (gint));
410 for (i = 0; i < 100; i++)
411 g_array_append_val (garray, i);
413 g_assert_cmpint (garray->len, ==, 100);
414 assert_int_array_zero_terminated (config, garray);
416 g_array_remove_index_fast (garray, 1);
417 g_array_remove_index_fast (garray, 3);
418 g_array_remove_index_fast (garray, 21);
419 g_array_remove_index_fast (garray, 57);
421 g_assert_cmpint (garray->len, ==, 96);
422 assert_int_array_zero_terminated (config, garray);
425 for (i = 0; i < garray->len; i++)
427 cur = g_array_index (garray, gint, i);
428 g_assert (cur != 1 && cur != 3 && cur != 21 && cur != 57);
431 g_assert_cmpint (prev, <, cur);
436 g_array_free (garray, TRUE);
439 /* Check that g_array_remove_range() works correctly for various #GArray
442 array_remove_range (gconstpointer test_data)
444 const ArrayTestData *config = test_data;
449 garray = g_array_new (config->zero_terminated, config->clear_, sizeof (gint));
450 for (i = 0; i < 100; i++)
451 g_array_append_val (garray, i);
453 g_assert_cmpint (garray->len, ==, 100);
454 assert_int_array_zero_terminated (config, garray);
456 g_array_remove_range (garray, 31, 4);
458 g_assert_cmpint (garray->len, ==, 96);
459 assert_int_array_zero_terminated (config, garray);
462 for (i = 0; i < garray->len; i++)
464 cur = g_array_index (garray, gint, i);
465 g_assert (cur < 31 || cur > 34);
466 g_assert_cmpint (prev, <, cur);
470 /* Ensure the entire array can be cleared, even when empty. */
471 g_array_remove_range (garray, 0, garray->len);
473 g_assert_cmpint (garray->len, ==, 0);
474 assert_int_array_zero_terminated (config, garray);
476 g_array_remove_range (garray, 0, garray->len);
478 g_assert_cmpint (garray->len, ==, 0);
479 assert_int_array_zero_terminated (config, garray);
481 g_array_free (garray, TRUE);
485 array_ref_count (void)
491 garray = g_array_new (FALSE, FALSE, sizeof (gint));
492 g_assert_cmpint (g_array_get_element_size (garray), ==, sizeof (gint));
493 for (i = 0; i < 100; i++)
494 g_array_prepend_val (garray, i);
496 /* check we can ref, unref and still access the array */
497 garray2 = g_array_ref (garray);
498 g_assert (garray == garray2);
499 g_array_unref (garray2);
500 for (i = 0; i < 100; i++)
501 g_assert_cmpint (g_array_index (garray, gint, i), ==, (100 - i - 1));
503 /* garray2 should be an empty valid GArray wrapper */
504 garray2 = g_array_ref (garray);
505 g_array_free (garray, TRUE);
507 g_assert_cmpint (garray2->len, ==, 0);
508 g_array_unref (garray2);
512 int_compare (gconstpointer p1, gconstpointer p2)
521 array_copy (gconstpointer test_data)
523 GArray *array, *array_copy;
525 const ArrayTestData *config = test_data;
526 const gsize array_size = 100;
528 /* Testing degenerated cases */
529 if (g_test_undefined ())
531 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
532 "*assertion*!= NULL*");
533 array = g_array_copy (NULL);
534 g_test_assert_expected_messages ();
536 g_assert_null (array);
539 /* Testing simple copy */
540 array = g_array_new (config->zero_terminated, config->clear_, sizeof (gint));
542 for (i = 0; i < array_size; i++)
543 g_array_append_val (array, i);
545 array_copy = g_array_copy (array);
547 /* Check internal data */
548 for (i = 0; i < array_size; i++)
549 g_assert_cmpuint (g_array_index (array, gint, i), ==,
550 g_array_index (array_copy, gint, i));
552 /* Check internal parameters ('zero_terminated' flag) */
553 if (config->zero_terminated)
555 const gint *data = (const gint *) array_copy->data;
556 g_assert_cmpint (data[array_copy->len], ==, 0);
559 /* Check internal parameters ('clear' flag) */
562 guint old_length = array_copy->len;
563 g_array_set_size (array_copy, old_length + 5);
564 for (i = old_length; i < old_length + 5; i++)
565 g_assert_cmpint (g_array_index (array_copy, gint, i), ==, 0);
569 g_array_unref (array);
570 g_array_unref (array_copy);
574 int_compare_data (gconstpointer p1, gconstpointer p2, gpointer data)
582 /* Check that g_array_sort() works correctly for various #GArray
585 array_sort (gconstpointer test_data)
587 const ArrayTestData *config = test_data;
592 garray = g_array_new (config->zero_terminated, config->clear_, sizeof (gint));
594 /* Sort empty array */
595 g_array_sort (garray, int_compare);
597 for (i = 0; i < 10000; i++)
599 cur = g_random_int_range (0, 10000);
600 g_array_append_val (garray, cur);
602 assert_int_array_zero_terminated (config, garray);
604 g_array_sort (garray, int_compare);
605 assert_int_array_zero_terminated (config, garray);
608 for (i = 0; i < garray->len; i++)
610 cur = g_array_index (garray, gint, i);
611 g_assert_cmpint (prev, <=, cur);
615 g_array_free (garray, TRUE);
618 /* Check that g_array_sort_with_data() works correctly for various #GArray
621 array_sort_with_data (gconstpointer test_data)
623 const ArrayTestData *config = test_data;
628 garray = g_array_new (config->zero_terminated, config->clear_, sizeof (gint));
630 /* Sort empty array */
631 g_array_sort_with_data (garray, int_compare_data, NULL);
633 for (i = 0; i < 10000; i++)
635 cur = g_random_int_range (0, 10000);
636 g_array_append_val (garray, cur);
638 assert_int_array_zero_terminated (config, garray);
640 g_array_sort_with_data (garray, int_compare_data, NULL);
641 assert_int_array_zero_terminated (config, garray);
644 for (i = 0; i < garray->len; i++)
646 cur = g_array_index (garray, gint, i);
647 g_assert_cmpint (prev, <=, cur);
651 g_array_free (garray, TRUE);
654 static gint num_clear_func_invocations = 0;
657 my_clear_func (gpointer data)
659 num_clear_func_invocations += 1;
663 array_clear_func (void)
669 garray = g_array_new (FALSE, FALSE, sizeof (gint));
670 g_array_set_clear_func (garray, my_clear_func);
672 for (i = 0; i < 10; i++)
674 cur = g_random_int_range (0, 100);
675 g_array_append_val (garray, cur);
678 g_array_remove_index (garray, 9);
679 g_assert_cmpint (num_clear_func_invocations, ==, 1);
681 g_array_remove_range (garray, 5, 3);
682 g_assert_cmpint (num_clear_func_invocations, ==, 4);
684 g_array_remove_index_fast (garray, 4);
685 g_assert_cmpint (num_clear_func_invocations, ==, 5);
687 g_array_free (garray, TRUE);
688 g_assert_cmpint (num_clear_func_invocations, ==, 10);
691 /* Defining a comparison function for testing g_array_binary_search() */
693 cmpint (gconstpointer a, gconstpointer b)
701 /* Testing g_array_binary_search() function */
703 test_array_binary_search (void)
706 guint i, matched_index;
708 if (g_test_undefined ())
710 /* Testing degenerated cases */
711 garray = g_array_sized_new (FALSE, FALSE, sizeof (guint), 0);
712 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
713 "*assertion*!= NULL*");
714 g_assert_false (g_array_binary_search (NULL, &i, cmpint, NULL));
715 g_test_assert_expected_messages ();
717 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
718 "*assertion*!= NULL*");
719 g_assert_false (g_array_binary_search (garray, &i, NULL, NULL));
720 g_test_assert_expected_messages ();
721 g_array_free (garray, TRUE);
724 /* Testing array of size 0 */
725 garray = g_array_sized_new (FALSE, FALSE, sizeof (guint), 0);
728 g_assert_false (g_array_binary_search (garray, &i, cmpint, NULL));
730 g_array_free (garray, TRUE);
732 /* Testing array of size 1 */
733 garray = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
735 g_array_append_val (garray, i);
737 g_assert_true (g_array_binary_search (garray, &i, cmpint, NULL));
740 g_assert_false (g_array_binary_search (garray, &i, cmpint, NULL));
743 g_assert_false (g_array_binary_search (garray, &i, cmpint, NULL));
745 g_array_free (garray, TRUE);
747 /* Testing array of size 2 */
748 garray = g_array_sized_new (FALSE, FALSE, sizeof (guint), 2);
749 for (i = 1; i < 3; i++)
750 g_array_append_val (garray, i);
752 for (i = 1; i < 3; i++)
753 g_assert_true (g_array_binary_search (garray, &i, cmpint, NULL));
756 g_assert_false (g_array_binary_search (garray, &i, cmpint, NULL));
759 g_assert_false (g_array_binary_search (garray, &i, cmpint, NULL));
761 g_array_free (garray, TRUE);
763 /* Testing array of size 3 */
764 garray = g_array_sized_new (FALSE, FALSE, sizeof (guint), 3);
765 for (i = 1; i < 4; i++)
766 g_array_append_val (garray, i);
768 for (i = 1; i < 4; i++)
769 g_assert_true (g_array_binary_search (garray, &i, cmpint, NULL));
772 g_assert_false (g_array_binary_search (garray, &i, cmpint, NULL));
775 g_assert_false (g_array_binary_search (garray, &i, cmpint, NULL));
777 g_array_free (garray, TRUE);
779 /* Testing array of size 10000 */
780 garray = g_array_sized_new (FALSE, FALSE, sizeof (guint), 10000);
782 for (i = 1; i < 10001; i++)
783 g_array_append_val (garray, i);
785 for (i = 1; i < 10001; i++)
786 g_assert_true (g_array_binary_search (garray, &i, cmpint, NULL));
788 for (i = 1; i < 10001; i++)
790 g_assert_true (g_array_binary_search (garray, &i, cmpint, &matched_index));
791 g_assert_cmpint (i, ==, matched_index + 1);
794 /* Testing negative result */
796 g_assert_false (g_array_binary_search (garray, &i, cmpint, NULL));
797 g_assert_false (g_array_binary_search (garray, &i, cmpint, &matched_index));
800 g_assert_false (g_array_binary_search (garray, &i, cmpint, NULL));
801 g_assert_false (g_array_binary_search (garray, &i, cmpint, &matched_index));
803 g_array_free (garray, TRUE);
805 /* Test for a not-found element in the middle of the array. */
806 garray = g_array_sized_new (FALSE, FALSE, sizeof (guint), 3);
807 for (i = 1; i < 10; i += 2)
808 g_array_append_val (garray, i);
811 g_assert_false (g_array_binary_search (garray, &i, cmpint, NULL));
814 g_assert_false (g_array_binary_search (garray, &i, cmpint, NULL));
817 g_assert_false (g_array_binary_search (garray, &i, cmpint, NULL));
819 g_array_free (garray, TRUE);
823 test_array_copy_sized (void)
825 GArray *array1 = NULL, *array2 = NULL, *array3 = NULL;
828 g_test_summary ("Test that copying a newly-allocated sized array works.");
830 array1 = g_array_sized_new (FALSE, FALSE, sizeof (int), 1);
831 array2 = g_array_copy (array1);
833 g_assert_cmpuint (array2->len, ==, array1->len);
835 g_array_append_val (array1, val);
836 array3 = g_array_copy (array1);
838 g_assert_cmpuint (array3->len, ==, array1->len);
839 g_assert_cmpuint (g_array_index (array3, int, 0), ==, g_array_index (array1, int, 0));
840 g_assert_cmpuint (array3->len, ==, 1);
841 g_assert_cmpuint (g_array_index (array3, int, 0), ==, val);
843 g_array_unref (array3);
844 g_array_unref (array2);
845 g_array_unref (array1);
849 array_overflow_append_vals (void)
851 if (!g_test_undefined ())
854 if (g_test_subprocess ())
856 GArray *array = g_array_new (TRUE, FALSE, 1);
857 /* Check for overflow should happen before data is accessed. */
858 g_array_append_vals (array, NULL, G_MAXUINT);
862 g_test_trap_subprocess (NULL, 0, 0);
863 g_test_trap_assert_failed ();
864 g_test_trap_assert_stderr ("*adding 4294967295 to array would overflow*");
869 array_overflow_set_size (void)
871 if (!g_test_undefined ())
874 if (g_test_subprocess ())
876 GArray *array = g_array_new (TRUE, FALSE, 1);
877 g_array_set_size (array, G_MAXUINT);
881 g_test_trap_subprocess (NULL, 0, 0);
882 g_test_trap_assert_failed ();
883 g_test_trap_assert_stderr ("*adding 4294967295 to array would overflow*");
887 /* Check g_ptr_array_steal() function */
889 pointer_array_steal (void)
891 const guint array_size = 10000;
897 gparray = g_ptr_array_new ();
898 pdata = g_ptr_array_steal (gparray, NULL);
899 g_assert_null (pdata);
901 pdata = g_ptr_array_steal (gparray, &len);
902 g_assert_null (pdata);
903 g_assert_cmpint (len, ==, 0);
905 for (i = 0; i < array_size; i++)
906 g_ptr_array_add (gparray, GINT_TO_POINTER (i));
908 past_len = gparray->len;
909 pdata = g_ptr_array_steal (gparray, &len);
910 g_assert_cmpint (gparray->len, ==, 0);
911 g_assert_cmpint (past_len, ==, len);
912 g_ptr_array_add (gparray, GINT_TO_POINTER (10));
914 g_assert_cmpint ((gsize) pdata[0], ==, (gsize) GINT_TO_POINTER (0));
915 g_assert_cmpint ((gsize) g_ptr_array_index (gparray, 0), ==,
916 (gsize) GINT_TO_POINTER (10));
917 g_assert_cmpint (gparray->len, ==, 1);
919 g_ptr_array_remove_index (gparray, 0);
921 for (i = 0; i < array_size; i++)
922 g_ptr_array_add (gparray, GINT_TO_POINTER (i));
923 g_assert_cmpmem (pdata, array_size * sizeof (gpointer),
924 gparray->pdata, array_size * sizeof (gpointer));
927 g_ptr_array_free (gparray, TRUE);
931 pointer_array_add (void)
938 gparray = g_ptr_array_sized_new (1000);
940 for (i = 0; i < 10000; i++)
941 g_ptr_array_add (gparray, GINT_TO_POINTER (i));
943 for (i = 0; i < 10000; i++)
944 g_assert (g_ptr_array_index (gparray, i) == GINT_TO_POINTER (i));
946 g_ptr_array_foreach (gparray, sum_up, &sum);
947 g_assert (sum == 49995000);
949 segment = g_ptr_array_free (gparray, FALSE);
950 for (i = 0; i < 10000; i++)
951 g_assert (segment[i] == GINT_TO_POINTER (i));
956 pointer_array_insert (void)
963 gparray = g_ptr_array_sized_new (1000);
965 for (i = 0; i < 10000; i++)
967 index = g_random_int_range (-1, i + 1);
968 g_ptr_array_insert (gparray, index, GINT_TO_POINTER (i));
971 g_ptr_array_foreach (gparray, sum_up, &sum);
972 g_assert (sum == 49995000);
974 g_ptr_array_free (gparray, TRUE);
978 pointer_array_ref_count (void)
985 gparray = g_ptr_array_new ();
986 for (i = 0; i < 10000; i++)
987 g_ptr_array_add (gparray, GINT_TO_POINTER (i));
989 /* check we can ref, unref and still access the array */
990 gparray2 = g_ptr_array_ref (gparray);
991 g_assert (gparray == gparray2);
992 g_ptr_array_unref (gparray2);
993 for (i = 0; i < 10000; i++)
994 g_assert (g_ptr_array_index (gparray, i) == GINT_TO_POINTER (i));
996 g_ptr_array_foreach (gparray, sum_up, &sum);
997 g_assert (sum == 49995000);
999 /* gparray2 should be an empty valid GPtrArray wrapper */
1000 gparray2 = g_ptr_array_ref (gparray);
1001 g_ptr_array_free (gparray, TRUE);
1003 g_assert_cmpint (gparray2->len, ==, 0);
1004 g_ptr_array_unref (gparray2);
1007 static gint num_free_func_invocations = 0;
1010 my_free_func (gpointer data)
1012 num_free_func_invocations++;
1017 pointer_array_free_func (void)
1020 GPtrArray *gparray2;
1024 num_free_func_invocations = 0;
1025 gparray = g_ptr_array_new_with_free_func (my_free_func);
1026 g_ptr_array_unref (gparray);
1027 g_assert_cmpint (num_free_func_invocations, ==, 0);
1029 gparray = g_ptr_array_new_with_free_func (my_free_func);
1030 g_ptr_array_free (gparray, TRUE);
1031 g_assert_cmpint (num_free_func_invocations, ==, 0);
1033 num_free_func_invocations = 0;
1034 gparray = g_ptr_array_new_with_free_func (my_free_func);
1035 g_ptr_array_add (gparray, g_strdup ("foo"));
1036 g_ptr_array_add (gparray, g_strdup ("bar"));
1037 g_ptr_array_add (gparray, g_strdup ("baz"));
1038 g_ptr_array_remove_index (gparray, 0);
1039 g_assert_cmpint (num_free_func_invocations, ==, 1);
1040 g_ptr_array_remove_index_fast (gparray, 1);
1041 g_assert_cmpint (num_free_func_invocations, ==, 2);
1042 s = g_strdup ("frob");
1043 g_ptr_array_add (gparray, s);
1044 g_assert (g_ptr_array_remove (gparray, s));
1045 g_assert (!g_ptr_array_remove (gparray, "nuun"));
1046 g_assert (!g_ptr_array_remove_fast (gparray, "mlo"));
1047 g_assert_cmpint (num_free_func_invocations, ==, 3);
1048 s = g_strdup ("frob");
1049 g_ptr_array_add (gparray, s);
1050 g_ptr_array_set_size (gparray, 1);
1051 g_assert_cmpint (num_free_func_invocations, ==, 4);
1052 g_ptr_array_ref (gparray);
1053 g_ptr_array_unref (gparray);
1054 g_assert_cmpint (num_free_func_invocations, ==, 4);
1055 g_ptr_array_unref (gparray);
1056 g_assert_cmpint (num_free_func_invocations, ==, 5);
1058 num_free_func_invocations = 0;
1059 gparray = g_ptr_array_new_full (10, my_free_func);
1060 g_ptr_array_add (gparray, g_strdup ("foo"));
1061 g_ptr_array_add (gparray, g_strdup ("bar"));
1062 g_ptr_array_add (gparray, g_strdup ("baz"));
1063 g_ptr_array_set_size (gparray, 20);
1064 g_ptr_array_add (gparray, NULL);
1065 gparray2 = g_ptr_array_ref (gparray);
1066 strv = (gchar **) g_ptr_array_free (gparray, FALSE);
1067 g_assert_cmpint (num_free_func_invocations, ==, 0);
1069 g_ptr_array_unref (gparray2);
1070 g_assert_cmpint (num_free_func_invocations, ==, 0);
1072 num_free_func_invocations = 0;
1073 gparray = g_ptr_array_new_with_free_func (my_free_func);
1074 g_ptr_array_add (gparray, g_strdup ("foo"));
1075 g_ptr_array_add (gparray, g_strdup ("bar"));
1076 g_ptr_array_add (gparray, g_strdup ("baz"));
1077 g_ptr_array_remove_range (gparray, 1, 1);
1078 g_ptr_array_unref (gparray);
1079 g_assert_cmpint (num_free_func_invocations, ==, 3);
1081 num_free_func_invocations = 0;
1082 gparray = g_ptr_array_new_with_free_func (my_free_func);
1083 g_ptr_array_add (gparray, g_strdup ("foo"));
1084 g_ptr_array_add (gparray, g_strdup ("bar"));
1085 g_ptr_array_add (gparray, g_strdup ("baz"));
1086 g_ptr_array_free (gparray, TRUE);
1087 g_assert_cmpint (num_free_func_invocations, ==, 3);
1089 num_free_func_invocations = 0;
1090 gparray = g_ptr_array_new_with_free_func (my_free_func);
1091 g_ptr_array_add (gparray, "foo");
1092 g_ptr_array_add (gparray, "bar");
1093 g_ptr_array_add (gparray, "baz");
1094 g_ptr_array_set_free_func (gparray, NULL);
1095 g_ptr_array_free (gparray, TRUE);
1096 g_assert_cmpint (num_free_func_invocations, ==, 0);
1100 ptr_array_copy_func (gconstpointer src, gpointer userdata)
1102 gsize *dst = g_malloc (sizeof (gsize));
1103 *dst = *((gsize *) src);
1107 /* Test the g_ptr_array_copy() function */
1109 pointer_array_copy (void)
1111 GPtrArray *ptr_array, *ptr_array2;
1113 const gsize array_size = 100;
1114 gsize *array_test = g_malloc (array_size * sizeof (gsize));
1116 g_test_summary ("Check all normal behaviour of stealing elements from one "
1117 "array to append to another, covering different array sizes "
1118 "and element copy functions");
1120 if (g_test_undefined ())
1122 /* Testing degenerated cases */
1123 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1124 "*assertion*!= NULL*");
1125 ptr_array = g_ptr_array_copy (NULL, NULL, NULL);
1126 g_test_assert_expected_messages ();
1127 g_assert_cmpuint ((gsize) ptr_array, ==, (gsize) NULL);
1130 /* Initializing array_test */
1131 for (i = 0; i < array_size; i++)
1134 /* Test copy an empty array */
1135 ptr_array = g_ptr_array_sized_new (0);
1136 ptr_array2 = g_ptr_array_copy (ptr_array, NULL, NULL);
1138 g_assert_cmpuint (ptr_array2->len, ==, ptr_array->len);
1140 g_ptr_array_unref (ptr_array);
1141 g_ptr_array_unref (ptr_array2);
1143 /* Test simple copy */
1144 ptr_array = g_ptr_array_sized_new (array_size);
1146 for (i = 0; i < array_size; i++)
1147 g_ptr_array_add (ptr_array, &array_test[i]);
1149 ptr_array2 = g_ptr_array_copy (ptr_array, NULL, NULL);
1151 g_assert_cmpuint (ptr_array2->len, ==, ptr_array->len);
1152 for (i = 0; i < array_size; i++)
1153 g_assert_cmpuint (*((gsize *) g_ptr_array_index (ptr_array2, i)), ==, i);
1155 for (i = 0; i < array_size; i++)
1156 g_assert_cmpuint ((gsize) g_ptr_array_index (ptr_array, i), ==,
1157 (gsize) g_ptr_array_index (ptr_array2, i));
1159 g_ptr_array_free (ptr_array2, TRUE);
1161 /* Test copy through GCopyFunc */
1162 ptr_array2 = g_ptr_array_copy (ptr_array, ptr_array_copy_func, NULL);
1163 g_ptr_array_set_free_func (ptr_array2, g_free);
1165 g_assert_cmpuint (ptr_array2->len, ==, ptr_array->len);
1166 for (i = 0; i < array_size; i++)
1167 g_assert_cmpuint (*((gsize *) g_ptr_array_index (ptr_array2, i)), ==, i);
1169 for (i = 0; i < array_size; i++)
1170 g_assert_cmpuint ((gsize) g_ptr_array_index (ptr_array, i), !=,
1171 (gsize) g_ptr_array_index (ptr_array2, i));
1173 g_ptr_array_free (ptr_array2, TRUE);
1176 g_ptr_array_free (ptr_array, TRUE);
1177 g_free (array_test);
1180 /* Test the g_ptr_array_extend() function */
1182 pointer_array_extend (void)
1184 GPtrArray *ptr_array, *ptr_array2;
1186 const gsize array_size = 100;
1187 gsize *array_test = g_malloc (array_size * sizeof (gsize));
1189 if (g_test_undefined ())
1191 /* Testing degenerated cases */
1192 ptr_array = g_ptr_array_sized_new (0);
1193 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1194 "*assertion*!= NULL*");
1195 g_ptr_array_extend (NULL, ptr_array, NULL, NULL);
1196 g_test_assert_expected_messages ();
1198 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1199 "*assertion*!= NULL*");
1200 g_ptr_array_extend (ptr_array, NULL, NULL, NULL);
1201 g_test_assert_expected_messages ();
1203 g_ptr_array_unref (ptr_array);
1206 /* Initializing array_test */
1207 for (i = 0; i < array_size; i++)
1210 /* Testing extend with array of size zero */
1211 ptr_array = g_ptr_array_sized_new (0);
1212 ptr_array2 = g_ptr_array_sized_new (0);
1214 g_ptr_array_extend (ptr_array, ptr_array2, NULL, NULL);
1216 g_assert_cmpuint (ptr_array->len, ==, 0);
1217 g_assert_cmpuint (ptr_array2->len, ==, 0);
1219 g_ptr_array_unref (ptr_array);
1220 g_ptr_array_unref (ptr_array2);
1222 /* Testing extend an array of size zero */
1223 ptr_array = g_ptr_array_sized_new (array_size);
1224 ptr_array2 = g_ptr_array_sized_new (0);
1226 for (i = 0; i < array_size; i++)
1228 g_ptr_array_add (ptr_array, &array_test[i]);
1231 g_ptr_array_extend (ptr_array, ptr_array2, NULL, NULL);
1233 for (i = 0; i < array_size; i++)
1234 g_assert_cmpuint (*((gsize *) g_ptr_array_index (ptr_array, i)), ==, i);
1236 g_ptr_array_unref (ptr_array);
1237 g_ptr_array_unref (ptr_array2);
1239 /* Testing extend an array of size zero */
1240 ptr_array = g_ptr_array_sized_new (0);
1241 ptr_array2 = g_ptr_array_sized_new (array_size);
1243 for (i = 0; i < array_size; i++)
1245 g_ptr_array_add (ptr_array2, &array_test[i]);
1248 g_ptr_array_extend (ptr_array, ptr_array2, NULL, NULL);
1250 for (i = 0; i < array_size; i++)
1251 g_assert_cmpuint (*((gsize *) g_ptr_array_index (ptr_array, i)), ==, i);
1253 g_ptr_array_unref (ptr_array);
1254 g_ptr_array_unref (ptr_array2);
1256 /* Testing simple extend */
1257 ptr_array = g_ptr_array_sized_new (array_size / 2);
1258 ptr_array2 = g_ptr_array_sized_new (array_size / 2);
1260 for (i = 0; i < array_size / 2; i++)
1262 g_ptr_array_add (ptr_array, &array_test[i]);
1263 g_ptr_array_add (ptr_array2, &array_test[i + (array_size / 2)]);
1266 g_ptr_array_extend (ptr_array, ptr_array2, NULL, NULL);
1268 for (i = 0; i < array_size; i++)
1269 g_assert_cmpuint (*((gsize *) g_ptr_array_index (ptr_array, i)), ==, i);
1271 g_ptr_array_unref (ptr_array);
1272 g_ptr_array_unref (ptr_array2);
1274 /* Testing extend with GCopyFunc */
1275 ptr_array = g_ptr_array_sized_new (array_size / 2);
1276 ptr_array2 = g_ptr_array_sized_new (array_size / 2);
1278 for (i = 0; i < array_size / 2; i++)
1280 g_ptr_array_add (ptr_array, &array_test[i]);
1281 g_ptr_array_add (ptr_array2, &array_test[i + (array_size / 2)]);
1284 g_ptr_array_extend (ptr_array, ptr_array2, ptr_array_copy_func, NULL);
1286 for (i = 0; i < array_size; i++)
1287 g_assert_cmpuint (*((gsize *) g_ptr_array_index (ptr_array, i)), ==, i);
1289 /* Clean-up memory */
1290 for (i = array_size / 2; i < array_size; i++)
1291 g_free (g_ptr_array_index (ptr_array, i));
1293 g_ptr_array_unref (ptr_array);
1294 g_ptr_array_unref (ptr_array2);
1295 g_free (array_test);
1298 /* Test the g_ptr_array_extend_and_steal() function */
1300 pointer_array_extend_and_steal (void)
1302 GPtrArray *ptr_array, *ptr_array2, *ptr_array3;
1304 const gsize array_size = 100;
1305 gsize *array_test = g_malloc (array_size * sizeof (gsize));
1307 /* Initializing array_test */
1308 for (i = 0; i < array_size; i++)
1311 /* Testing simple extend_and_steal() */
1312 ptr_array = g_ptr_array_sized_new (array_size / 2);
1313 ptr_array2 = g_ptr_array_sized_new (array_size / 2);
1315 for (i = 0; i < array_size / 2; i++)
1317 g_ptr_array_add (ptr_array, &array_test[i]);
1318 g_ptr_array_add (ptr_array2, &array_test[i + (array_size / 2)]);
1321 g_ptr_array_extend_and_steal (ptr_array, ptr_array2);
1323 for (i = 0; i < array_size; i++)
1324 g_assert_cmpuint (*((gsize *) g_ptr_array_index (ptr_array, i)), ==, i);
1326 g_ptr_array_free (ptr_array, TRUE);
1328 /* Testing extend_and_steal() with a pending reference to stolen array */
1329 ptr_array = g_ptr_array_sized_new (array_size / 2);
1330 ptr_array2 = g_ptr_array_sized_new (array_size / 2);
1332 for (i = 0; i < array_size / 2; i++)
1334 g_ptr_array_add (ptr_array, &array_test[i]);
1335 g_ptr_array_add (ptr_array2, &array_test[i + (array_size / 2)]);
1338 ptr_array3 = g_ptr_array_ref (ptr_array2);
1340 g_ptr_array_extend_and_steal (ptr_array, ptr_array2);
1342 for (i = 0; i < array_size; i++)
1343 g_assert_cmpuint (*((gsize *) g_ptr_array_index (ptr_array, i)), ==, i);
1345 g_assert_cmpuint (ptr_array3->len, ==, 0);
1346 g_assert_null (ptr_array3->pdata);
1348 g_ptr_array_add (ptr_array2, NULL);
1350 g_ptr_array_free (ptr_array, TRUE);
1351 g_ptr_array_free (ptr_array3, TRUE);
1353 /* Final memory clean-up */
1354 g_free (array_test);
1358 ptr_compare (gconstpointer p1, gconstpointer p2)
1360 gpointer i1 = *(gpointer*)p1;
1361 gpointer i2 = *(gpointer*)p2;
1363 return GPOINTER_TO_INT (i1) - GPOINTER_TO_INT (i2);
1367 ptr_compare_data (gconstpointer p1, gconstpointer p2, gpointer data)
1369 gpointer i1 = *(gpointer*)p1;
1370 gpointer i2 = *(gpointer*)p2;
1372 return GPOINTER_TO_INT (i1) - GPOINTER_TO_INT (i2);
1376 pointer_array_sort (void)
1383 gparray = g_ptr_array_new ();
1385 /* Sort empty array */
1386 g_ptr_array_sort (gparray, ptr_compare);
1388 for (i = 0; i < 10000; i++)
1390 val = g_random_int_range (0, 10000);
1391 g_ptr_array_add (gparray, GINT_TO_POINTER (val));
1394 g_ptr_array_sort (gparray, ptr_compare);
1397 for (i = 0; i < 10000; i++)
1399 cur = GPOINTER_TO_INT (g_ptr_array_index (gparray, i));
1400 g_assert_cmpint (prev, <=, cur);
1404 g_ptr_array_free (gparray, TRUE);
1407 /* Please keep pointer_array_sort_example() in sync with the doc-comment
1408 * of g_ptr_array_sort() */
1417 file_list_entry_free (gpointer p)
1419 FileListEntry *entry = p;
1421 g_free (entry->name);
1426 sort_filelist (gconstpointer a, gconstpointer b)
1428 const FileListEntry *entry1 = *((FileListEntry **) a);
1429 const FileListEntry *entry2 = *((FileListEntry **) b);
1431 return g_ascii_strcasecmp (entry1->name, entry2->name);
1435 pointer_array_sort_example (void)
1437 GPtrArray *file_list = NULL;
1438 FileListEntry *entry;
1440 g_test_summary ("Check that the doc-comment for g_ptr_array_sort() is correct");
1442 file_list = g_ptr_array_new_with_free_func (file_list_entry_free);
1444 entry = g_new0 (FileListEntry, 1);
1445 entry->name = g_strdup ("README");
1447 g_ptr_array_add (file_list, g_steal_pointer (&entry));
1449 entry = g_new0 (FileListEntry, 1);
1450 entry->name = g_strdup ("empty");
1452 g_ptr_array_add (file_list, g_steal_pointer (&entry));
1454 entry = g_new0 (FileListEntry, 1);
1455 entry->name = g_strdup ("aardvark");
1457 g_ptr_array_add (file_list, g_steal_pointer (&entry));
1459 g_ptr_array_sort (file_list, sort_filelist);
1461 g_assert_cmpuint (file_list->len, ==, 3);
1462 entry = g_ptr_array_index (file_list, 0);
1463 g_assert_cmpstr (entry->name, ==, "aardvark");
1464 entry = g_ptr_array_index (file_list, 1);
1465 g_assert_cmpstr (entry->name, ==, "empty");
1466 entry = g_ptr_array_index (file_list, 2);
1467 g_assert_cmpstr (entry->name, ==, "README");
1469 g_ptr_array_unref (file_list);
1472 /* Please keep pointer_array_sort_with_data_example() in sync with the
1473 * doc-comment of g_ptr_array_sort_with_data() */
1475 typedef enum { SORT_NAME, SORT_SIZE } SortMode;
1478 sort_filelist_how (gconstpointer a, gconstpointer b, gpointer user_data)
1481 const SortMode sort_mode = GPOINTER_TO_INT (user_data);
1482 const FileListEntry *entry1 = *((FileListEntry **) a);
1483 const FileListEntry *entry2 = *((FileListEntry **) b);
1488 order = g_ascii_strcasecmp (entry1->name, entry2->name);
1491 order = entry1->size - entry2->size;
1501 pointer_array_sort_with_data_example (void)
1503 GPtrArray *file_list = NULL;
1504 FileListEntry *entry;
1507 g_test_summary ("Check that the doc-comment for g_ptr_array_sort_with_data() is correct");
1509 file_list = g_ptr_array_new_with_free_func (file_list_entry_free);
1511 entry = g_new0 (FileListEntry, 1);
1512 entry->name = g_strdup ("README");
1514 g_ptr_array_add (file_list, g_steal_pointer (&entry));
1516 entry = g_new0 (FileListEntry, 1);
1517 entry->name = g_strdup ("empty");
1519 g_ptr_array_add (file_list, g_steal_pointer (&entry));
1521 entry = g_new0 (FileListEntry, 1);
1522 entry->name = g_strdup ("aardvark");
1524 g_ptr_array_add (file_list, g_steal_pointer (&entry));
1526 sort_mode = SORT_NAME;
1527 g_ptr_array_sort_with_data (file_list, sort_filelist_how, GINT_TO_POINTER (sort_mode));
1529 g_assert_cmpuint (file_list->len, ==, 3);
1530 entry = g_ptr_array_index (file_list, 0);
1531 g_assert_cmpstr (entry->name, ==, "aardvark");
1532 entry = g_ptr_array_index (file_list, 1);
1533 g_assert_cmpstr (entry->name, ==, "empty");
1534 entry = g_ptr_array_index (file_list, 2);
1535 g_assert_cmpstr (entry->name, ==, "README");
1537 sort_mode = SORT_SIZE;
1538 g_ptr_array_sort_with_data (file_list, sort_filelist_how, GINT_TO_POINTER (sort_mode));
1540 g_assert_cmpuint (file_list->len, ==, 3);
1541 entry = g_ptr_array_index (file_list, 0);
1542 g_assert_cmpstr (entry->name, ==, "empty");
1543 entry = g_ptr_array_index (file_list, 1);
1544 g_assert_cmpstr (entry->name, ==, "aardvark");
1545 entry = g_ptr_array_index (file_list, 2);
1546 g_assert_cmpstr (entry->name, ==, "README");
1548 g_ptr_array_unref (file_list);
1552 pointer_array_sort_with_data (void)
1558 gparray = g_ptr_array_new ();
1560 /* Sort empty array */
1561 g_ptr_array_sort_with_data (gparray, ptr_compare_data, NULL);
1563 for (i = 0; i < 10000; i++)
1564 g_ptr_array_add (gparray, GINT_TO_POINTER (g_random_int_range (0, 10000)));
1566 g_ptr_array_sort_with_data (gparray, ptr_compare_data, NULL);
1569 for (i = 0; i < 10000; i++)
1571 cur = GPOINTER_TO_INT (g_ptr_array_index (gparray, i));
1572 g_assert_cmpint (prev, <=, cur);
1576 g_ptr_array_free (gparray, TRUE);
1580 pointer_array_find_empty (void)
1585 array = g_ptr_array_new ();
1587 g_assert_false (g_ptr_array_find (array, "some-value", NULL)); /* NULL index */
1588 g_assert_false (g_ptr_array_find (array, "some-value", &idx)); /* non-NULL index */
1589 g_assert_false (g_ptr_array_find_with_equal_func (array, "some-value", g_str_equal, NULL)); /* NULL index */
1590 g_assert_false (g_ptr_array_find_with_equal_func (array, "some-value", g_str_equal, &idx)); /* non-NULL index */
1592 g_ptr_array_free (array, TRUE);
1596 pointer_array_find_non_empty (void)
1600 const gchar *str_pointer = "static-string";
1602 array = g_ptr_array_new ();
1604 g_ptr_array_add (array, "some");
1605 g_ptr_array_add (array, "random");
1606 g_ptr_array_add (array, "values");
1607 g_ptr_array_add (array, "some");
1608 g_ptr_array_add (array, "duplicated");
1609 g_ptr_array_add (array, (gpointer) str_pointer);
1611 g_assert_true (g_ptr_array_find_with_equal_func (array, "random", g_str_equal, NULL)); /* NULL index */
1612 g_assert_true (g_ptr_array_find_with_equal_func (array, "random", g_str_equal, &idx)); /* non-NULL index */
1613 g_assert_cmpuint (idx, ==, 1);
1615 g_assert_true (g_ptr_array_find_with_equal_func (array, "some", g_str_equal, &idx)); /* duplicate element */
1616 g_assert_cmpuint (idx, ==, 0);
1618 g_assert_false (g_ptr_array_find_with_equal_func (array, "nope", g_str_equal, NULL));
1620 g_assert_true (g_ptr_array_find_with_equal_func (array, str_pointer, g_str_equal, &idx));
1621 g_assert_cmpuint (idx, ==, 5);
1623 g_assert_true (g_ptr_array_find_with_equal_func (array, str_pointer, NULL, &idx)); /* NULL equal func */
1624 g_assert_cmpuint (idx, ==, 5);
1626 g_assert_true (g_ptr_array_find (array, str_pointer, &idx)); /* NULL equal func */
1627 g_assert_cmpuint (idx, ==, 5);
1629 g_ptr_array_free (array, TRUE);
1633 steal_destroy_notify (gpointer data)
1635 guint *counter = data;
1636 *counter = *counter + 1;
1639 /* Test that g_ptr_array_steal_index() and g_ptr_array_steal_index_fast() can
1640 * remove elements from a pointer array without the #GDestroyNotify being called. */
1642 pointer_array_steal_index (void)
1644 guint i1 = 0, i2 = 0, i3 = 0, i4 = 0;
1645 gpointer out1, out2;
1646 GPtrArray *array = g_ptr_array_new_with_free_func (steal_destroy_notify);
1648 g_ptr_array_add (array, &i1);
1649 g_ptr_array_add (array, &i2);
1650 g_ptr_array_add (array, &i3);
1651 g_ptr_array_add (array, &i4);
1653 g_assert_cmpuint (array->len, ==, 4);
1655 /* Remove a single element. */
1656 out1 = g_ptr_array_steal_index (array, 0);
1657 g_assert_true (out1 == &i1);
1658 g_assert_cmpuint (i1, ==, 0); /* should not have been destroyed */
1660 /* Following elements should have been moved down. */
1661 g_assert_cmpuint (array->len, ==, 3);
1662 g_assert_true (g_ptr_array_index (array, 0) == &i2);
1663 g_assert_true (g_ptr_array_index (array, 1) == &i3);
1664 g_assert_true (g_ptr_array_index (array, 2) == &i4);
1666 /* Remove another element, quickly. */
1667 out2 = g_ptr_array_steal_index_fast (array, 0);
1668 g_assert_true (out2 == &i2);
1669 g_assert_cmpuint (i2, ==, 0); /* should not have been destroyed */
1671 /* Last element should have been swapped in place. */
1672 g_assert_cmpuint (array->len, ==, 2);
1673 g_assert_true (g_ptr_array_index (array, 0) == &i4);
1674 g_assert_true (g_ptr_array_index (array, 1) == &i3);
1676 /* Check that destroying the pointer array doesn’t affect the stolen elements. */
1677 g_ptr_array_unref (array);
1679 g_assert_cmpuint (i1, ==, 0);
1680 g_assert_cmpuint (i2, ==, 0);
1681 g_assert_cmpuint (i3, ==, 1);
1682 g_assert_cmpuint (i4, ==, 1);
1686 byte_array_new_take_overflow (void)
1688 #if SIZE_WIDTH <= UINT_WIDTH
1689 g_test_skip ("Overflow test requires G_MAXSIZE > G_MAXUINT.");
1693 if (!g_test_undefined ())
1696 /* Check for overflow should happen before data is accessed. */
1697 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1698 "*assertion 'len <= G_MAXUINT' failed");
1699 arr = g_byte_array_new_take (NULL, (gsize)G_MAXUINT + 1);
1700 g_assert_null (arr);
1701 g_test_assert_expected_messages ();
1706 byte_array_steal (void)
1708 const guint array_size = 10000;
1709 GByteArray *gbarray;
1712 gsize len, past_len;
1714 gbarray = g_byte_array_new ();
1715 bdata = g_byte_array_steal (gbarray, NULL);
1716 g_assert_cmpint ((gsize) bdata, ==, (gsize) gbarray->data);
1719 for (i = 0; i < array_size; i++)
1720 g_byte_array_append (gbarray, (guint8 *) "abcd", 4);
1722 past_len = gbarray->len;
1723 bdata = g_byte_array_steal (gbarray, &len);
1725 g_assert_cmpint (len, ==, past_len);
1726 g_assert_cmpint (gbarray->len, ==, 0);
1728 g_byte_array_append (gbarray, (guint8 *) "@", 1);
1730 g_assert_cmpint (bdata[0], ==, 'a');
1731 g_assert_cmpint (gbarray->data[0], ==, '@');
1732 g_assert_cmpint (gbarray->len, ==, 1);
1734 g_byte_array_remove_index (gbarray, 0);
1737 g_byte_array_free (gbarray, TRUE);
1741 byte_array_append (void)
1743 GByteArray *gbarray;
1747 gbarray = g_byte_array_sized_new (1000);
1748 for (i = 0; i < 10000; i++)
1749 g_byte_array_append (gbarray, (guint8*) "abcd", 4);
1751 for (i = 0; i < 10000; i++)
1753 g_assert (gbarray->data[4*i] == 'a');
1754 g_assert (gbarray->data[4*i+1] == 'b');
1755 g_assert (gbarray->data[4*i+2] == 'c');
1756 g_assert (gbarray->data[4*i+3] == 'd');
1759 segment = g_byte_array_free (gbarray, FALSE);
1761 for (i = 0; i < 10000; i++)
1763 g_assert (segment[4*i] == 'a');
1764 g_assert (segment[4*i+1] == 'b');
1765 g_assert (segment[4*i+2] == 'c');
1766 g_assert (segment[4*i+3] == 'd');
1773 byte_array_prepend (void)
1775 GByteArray *gbarray;
1778 gbarray = g_byte_array_new ();
1779 g_byte_array_set_size (gbarray, 1000);
1781 for (i = 0; i < 10000; i++)
1782 g_byte_array_prepend (gbarray, (guint8*) "abcd", 4);
1784 for (i = 0; i < 10000; i++)
1786 g_assert (gbarray->data[4*i] == 'a');
1787 g_assert (gbarray->data[4*i+1] == 'b');
1788 g_assert (gbarray->data[4*i+2] == 'c');
1789 g_assert (gbarray->data[4*i+3] == 'd');
1792 g_byte_array_free (gbarray, TRUE);
1796 byte_array_ref_count (void)
1798 GByteArray *gbarray;
1799 GByteArray *gbarray2;
1802 gbarray = g_byte_array_new ();
1803 for (i = 0; i < 10000; i++)
1804 g_byte_array_append (gbarray, (guint8*) "abcd", 4);
1806 gbarray2 = g_byte_array_ref (gbarray);
1807 g_assert (gbarray2 == gbarray);
1808 g_byte_array_unref (gbarray2);
1809 for (i = 0; i < 10000; i++)
1811 g_assert (gbarray->data[4*i] == 'a');
1812 g_assert (gbarray->data[4*i+1] == 'b');
1813 g_assert (gbarray->data[4*i+2] == 'c');
1814 g_assert (gbarray->data[4*i+3] == 'd');
1817 gbarray2 = g_byte_array_ref (gbarray);
1818 g_assert (gbarray2 == gbarray);
1819 g_byte_array_free (gbarray, TRUE);
1820 g_assert_cmpint (gbarray2->len, ==, 0);
1821 g_byte_array_unref (gbarray2);
1825 byte_array_remove (void)
1827 GByteArray *gbarray;
1830 gbarray = g_byte_array_new ();
1831 for (i = 0; i < 100; i++)
1832 g_byte_array_append (gbarray, (guint8*) "abcd", 4);
1834 g_assert_cmpint (gbarray->len, ==, 400);
1836 g_byte_array_remove_index (gbarray, 4);
1837 g_byte_array_remove_index (gbarray, 4);
1838 g_byte_array_remove_index (gbarray, 4);
1839 g_byte_array_remove_index (gbarray, 4);
1841 g_assert_cmpint (gbarray->len, ==, 396);
1843 for (i = 0; i < 99; i++)
1845 g_assert (gbarray->data[4*i] == 'a');
1846 g_assert (gbarray->data[4*i+1] == 'b');
1847 g_assert (gbarray->data[4*i+2] == 'c');
1848 g_assert (gbarray->data[4*i+3] == 'd');
1851 g_byte_array_free (gbarray, TRUE);
1855 byte_array_remove_fast (void)
1857 GByteArray *gbarray;
1860 gbarray = g_byte_array_new ();
1861 for (i = 0; i < 100; i++)
1862 g_byte_array_append (gbarray, (guint8*) "abcd", 4);
1864 g_assert_cmpint (gbarray->len, ==, 400);
1866 g_byte_array_remove_index_fast (gbarray, 4);
1867 g_byte_array_remove_index_fast (gbarray, 4);
1868 g_byte_array_remove_index_fast (gbarray, 4);
1869 g_byte_array_remove_index_fast (gbarray, 4);
1871 g_assert_cmpint (gbarray->len, ==, 396);
1873 for (i = 0; i < 99; i++)
1875 g_assert (gbarray->data[4*i] == 'a');
1876 g_assert (gbarray->data[4*i+1] == 'b');
1877 g_assert (gbarray->data[4*i+2] == 'c');
1878 g_assert (gbarray->data[4*i+3] == 'd');
1881 g_byte_array_free (gbarray, TRUE);
1885 byte_array_remove_range (void)
1887 GByteArray *gbarray;
1890 gbarray = g_byte_array_new ();
1891 for (i = 0; i < 100; i++)
1892 g_byte_array_append (gbarray, (guint8*) "abcd", 4);
1894 g_assert_cmpint (gbarray->len, ==, 400);
1896 g_byte_array_remove_range (gbarray, 12, 4);
1898 g_assert_cmpint (gbarray->len, ==, 396);
1900 for (i = 0; i < 99; i++)
1902 g_assert (gbarray->data[4*i] == 'a');
1903 g_assert (gbarray->data[4*i+1] == 'b');
1904 g_assert (gbarray->data[4*i+2] == 'c');
1905 g_assert (gbarray->data[4*i+3] == 'd');
1908 /* Ensure the entire array can be cleared, even when empty. */
1909 g_byte_array_remove_range (gbarray, 0, gbarray->len);
1910 g_byte_array_remove_range (gbarray, 0, gbarray->len);
1912 g_byte_array_free (gbarray, TRUE);
1916 byte_compare (gconstpointer p1, gconstpointer p2)
1918 const guint8 *i1 = p1;
1919 const guint8 *i2 = p2;
1925 byte_compare_data (gconstpointer p1, gconstpointer p2, gpointer data)
1927 const guint8 *i1 = p1;
1928 const guint8 *i2 = p2;
1934 byte_array_sort (void)
1936 GByteArray *gbarray;
1941 gbarray = g_byte_array_new ();
1942 for (i = 0; i < 100; i++)
1944 val = 'a' + g_random_int_range (0, 26);
1945 g_byte_array_append (gbarray, (guint8*) &val, 1);
1948 g_byte_array_sort (gbarray, byte_compare);
1951 for (i = 0; i < gbarray->len; i++)
1953 cur = gbarray->data[i];
1954 g_assert_cmpint (prev, <=, cur);
1958 g_byte_array_free (gbarray, TRUE);
1962 byte_array_sort_with_data (void)
1964 GByteArray *gbarray;
1969 gbarray = g_byte_array_new ();
1970 for (i = 0; i < 100; i++)
1972 val = 'a' + g_random_int_range (0, 26);
1973 g_byte_array_append (gbarray, (guint8*) &val, 1);
1976 g_byte_array_sort_with_data (gbarray, byte_compare_data, NULL);
1979 for (i = 0; i < gbarray->len; i++)
1981 cur = gbarray->data[i];
1982 g_assert_cmpint (prev, <=, cur);
1986 g_byte_array_free (gbarray, TRUE);
1990 byte_array_new_take (void)
1992 GByteArray *gbarray;
1995 data = g_memdup2 ("woooweeewow", 11);
1996 gbarray = g_byte_array_new_take (data, 11);
1997 g_assert (gbarray->data == data);
1998 g_assert_cmpuint (gbarray->len, ==, 11);
1999 g_byte_array_free (gbarray, TRUE);
2003 byte_array_free_to_bytes (void)
2005 GByteArray *gbarray;
2010 gbarray = g_byte_array_new ();
2011 g_byte_array_append (gbarray, (guint8 *)"woooweeewow", 11);
2012 memory = gbarray->data;
2014 bytes = g_byte_array_free_to_bytes (gbarray);
2015 g_assert (bytes != NULL);
2016 g_assert_cmpuint (g_bytes_get_size (bytes), ==, 11);
2017 g_assert (g_bytes_get_data (bytes, &size) == memory);
2018 g_assert_cmpuint (size, ==, 11);
2020 g_bytes_unref (bytes);
2024 add_array_test (const gchar *test_path,
2025 const ArrayTestData *config,
2026 GTestDataFunc test_func)
2028 gchar *test_name = NULL;
2030 test_name = g_strdup_printf ("%s/%s-%s",
2032 config->zero_terminated ? "zero-terminated" : "non-zero-terminated",
2033 config->clear_ ? "clear" : "no-clear");
2034 g_test_add_data_func (test_name, config, test_func);
2039 main (int argc, char *argv[])
2041 /* Test all possible combinations of g_array_new() parameters. */
2042 const ArrayTestData array_configurations[] =
2051 g_test_init (&argc, &argv, NULL);
2054 g_test_add_func ("/array/new/zero-terminated", array_new_zero_terminated);
2055 g_test_add_func ("/array/ref-count", array_ref_count);
2056 g_test_add_func ("/array/steal", array_steal);
2057 g_test_add_func ("/array/clear-func", array_clear_func);
2058 g_test_add_func ("/array/binary-search", test_array_binary_search);
2059 g_test_add_func ("/array/copy-sized", test_array_copy_sized);
2060 g_test_add_func ("/array/overflow-append-vals", array_overflow_append_vals);
2061 g_test_add_func ("/array/overflow-set-size", array_overflow_set_size);
2063 for (i = 0; i < G_N_ELEMENTS (array_configurations); i++)
2065 add_array_test ("/array/set-size", &array_configurations[i], array_set_size);
2066 add_array_test ("/array/set-size/sized", &array_configurations[i], array_set_size_sized);
2067 add_array_test ("/array/append-val", &array_configurations[i], array_append_val);
2068 add_array_test ("/array/prepend-val", &array_configurations[i], array_prepend_val);
2069 add_array_test ("/array/prepend-vals", &array_configurations[i], array_prepend_vals);
2070 add_array_test ("/array/insert-vals", &array_configurations[i], array_insert_vals);
2071 add_array_test ("/array/remove-index", &array_configurations[i], array_remove_index);
2072 add_array_test ("/array/remove-index-fast", &array_configurations[i], array_remove_index_fast);
2073 add_array_test ("/array/remove-range", &array_configurations[i], array_remove_range);
2074 add_array_test ("/array/copy", &array_configurations[i], array_copy);
2075 add_array_test ("/array/sort", &array_configurations[i], array_sort);
2076 add_array_test ("/array/sort-with-data", &array_configurations[i], array_sort_with_data);
2079 /* pointer arrays */
2080 g_test_add_func ("/pointerarray/add", pointer_array_add);
2081 g_test_add_func ("/pointerarray/insert", pointer_array_insert);
2082 g_test_add_func ("/pointerarray/ref-count", pointer_array_ref_count);
2083 g_test_add_func ("/pointerarray/free-func", pointer_array_free_func);
2084 g_test_add_func ("/pointerarray/array_copy", pointer_array_copy);
2085 g_test_add_func ("/pointerarray/array_extend", pointer_array_extend);
2086 g_test_add_func ("/pointerarray/array_extend_and_steal", pointer_array_extend_and_steal);
2087 g_test_add_func ("/pointerarray/sort", pointer_array_sort);
2088 g_test_add_func ("/pointerarray/sort/example", pointer_array_sort_example);
2089 g_test_add_func ("/pointerarray/sort-with-data", pointer_array_sort_with_data);
2090 g_test_add_func ("/pointerarray/sort-with-data/example", pointer_array_sort_with_data_example);
2091 g_test_add_func ("/pointerarray/find/empty", pointer_array_find_empty);
2092 g_test_add_func ("/pointerarray/find/non-empty", pointer_array_find_non_empty);
2093 g_test_add_func ("/pointerarray/steal", pointer_array_steal);
2094 g_test_add_func ("/pointerarray/steal_index", pointer_array_steal_index);
2097 g_test_add_func ("/bytearray/steal", byte_array_steal);
2098 g_test_add_func ("/bytearray/append", byte_array_append);
2099 g_test_add_func ("/bytearray/prepend", byte_array_prepend);
2100 g_test_add_func ("/bytearray/remove", byte_array_remove);
2101 g_test_add_func ("/bytearray/remove-fast", byte_array_remove_fast);
2102 g_test_add_func ("/bytearray/remove-range", byte_array_remove_range);
2103 g_test_add_func ("/bytearray/ref-count", byte_array_ref_count);
2104 g_test_add_func ("/bytearray/sort", byte_array_sort);
2105 g_test_add_func ("/bytearray/sort-with-data", byte_array_sort_with_data);
2106 g_test_add_func ("/bytearray/new-take", byte_array_new_take);
2107 g_test_add_func ("/bytearray/new-take-overflow", byte_array_new_take_overflow);
2108 g_test_add_func ("/bytearray/free-to-bytes", byte_array_free_to_bytes);
2110 return g_test_run ();