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));
593 for (i = 0; i < 10000; i++)
595 cur = g_random_int_range (0, 10000);
596 g_array_append_val (garray, cur);
598 assert_int_array_zero_terminated (config, garray);
600 g_array_sort (garray, int_compare);
601 assert_int_array_zero_terminated (config, garray);
604 for (i = 0; i < garray->len; i++)
606 cur = g_array_index (garray, gint, i);
607 g_assert_cmpint (prev, <=, cur);
611 g_array_free (garray, TRUE);
614 /* Check that g_array_sort_with_data() works correctly for various #GArray
617 array_sort_with_data (gconstpointer test_data)
619 const ArrayTestData *config = test_data;
624 garray = g_array_new (config->zero_terminated, config->clear_, sizeof (gint));
625 for (i = 0; i < 10000; i++)
627 cur = g_random_int_range (0, 10000);
628 g_array_append_val (garray, cur);
630 assert_int_array_zero_terminated (config, garray);
632 g_array_sort_with_data (garray, int_compare_data, NULL);
633 assert_int_array_zero_terminated (config, garray);
636 for (i = 0; i < garray->len; i++)
638 cur = g_array_index (garray, gint, i);
639 g_assert_cmpint (prev, <=, cur);
643 g_array_free (garray, TRUE);
646 static gint num_clear_func_invocations = 0;
649 my_clear_func (gpointer data)
651 num_clear_func_invocations += 1;
655 array_clear_func (void)
661 garray = g_array_new (FALSE, FALSE, sizeof (gint));
662 g_array_set_clear_func (garray, my_clear_func);
664 for (i = 0; i < 10; i++)
666 cur = g_random_int_range (0, 100);
667 g_array_append_val (garray, cur);
670 g_array_remove_index (garray, 9);
671 g_assert_cmpint (num_clear_func_invocations, ==, 1);
673 g_array_remove_range (garray, 5, 3);
674 g_assert_cmpint (num_clear_func_invocations, ==, 4);
676 g_array_remove_index_fast (garray, 4);
677 g_assert_cmpint (num_clear_func_invocations, ==, 5);
679 g_array_free (garray, TRUE);
680 g_assert_cmpint (num_clear_func_invocations, ==, 10);
683 /* Defining a comparison function for testing g_array_binary_search() */
685 cmpint (gconstpointer a, gconstpointer b)
693 /* Testing g_array_binary_search() function */
695 test_array_binary_search (void)
698 guint i, matched_index;
700 if (g_test_undefined ())
702 /* Testing degenerated cases */
703 garray = g_array_sized_new (FALSE, FALSE, sizeof (guint), 0);
704 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
705 "*assertion*!= NULL*");
706 g_assert_false (g_array_binary_search (NULL, &i, cmpint, NULL));
707 g_test_assert_expected_messages ();
709 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
710 "*assertion*!= NULL*");
711 g_assert_false (g_array_binary_search (garray, &i, NULL, NULL));
712 g_test_assert_expected_messages ();
713 g_array_free (garray, TRUE);
716 /* Testing array of size 0 */
717 garray = g_array_sized_new (FALSE, FALSE, sizeof (guint), 0);
720 g_assert_false (g_array_binary_search (garray, &i, cmpint, NULL));
722 g_array_free (garray, TRUE);
724 /* Testing array of size 1 */
725 garray = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
727 g_array_append_val (garray, i);
729 g_assert_true (g_array_binary_search (garray, &i, cmpint, NULL));
732 g_assert_false (g_array_binary_search (garray, &i, cmpint, NULL));
735 g_assert_false (g_array_binary_search (garray, &i, cmpint, NULL));
737 g_array_free (garray, TRUE);
739 /* Testing array of size 2 */
740 garray = g_array_sized_new (FALSE, FALSE, sizeof (guint), 2);
741 for (i = 1; i < 3; i++)
742 g_array_append_val (garray, i);
744 for (i = 1; i < 3; i++)
745 g_assert_true (g_array_binary_search (garray, &i, cmpint, NULL));
748 g_assert_false (g_array_binary_search (garray, &i, cmpint, NULL));
751 g_assert_false (g_array_binary_search (garray, &i, cmpint, NULL));
753 g_array_free (garray, TRUE);
755 /* Testing array of size 3 */
756 garray = g_array_sized_new (FALSE, FALSE, sizeof (guint), 3);
757 for (i = 1; i < 4; i++)
758 g_array_append_val (garray, i);
760 for (i = 1; i < 4; i++)
761 g_assert_true (g_array_binary_search (garray, &i, cmpint, NULL));
764 g_assert_false (g_array_binary_search (garray, &i, cmpint, NULL));
767 g_assert_false (g_array_binary_search (garray, &i, cmpint, NULL));
769 g_array_free (garray, TRUE);
771 /* Testing array of size 10000 */
772 garray = g_array_sized_new (FALSE, FALSE, sizeof (guint), 10000);
774 for (i = 1; i < 10001; i++)
775 g_array_append_val (garray, i);
777 for (i = 1; i < 10001; i++)
778 g_assert_true (g_array_binary_search (garray, &i, cmpint, NULL));
780 for (i = 1; i < 10001; i++)
782 g_assert_true (g_array_binary_search (garray, &i, cmpint, &matched_index));
783 g_assert_cmpint (i, ==, matched_index + 1);
786 /* Testing negative result */
788 g_assert_false (g_array_binary_search (garray, &i, cmpint, NULL));
789 g_assert_false (g_array_binary_search (garray, &i, cmpint, &matched_index));
792 g_assert_false (g_array_binary_search (garray, &i, cmpint, NULL));
793 g_assert_false (g_array_binary_search (garray, &i, cmpint, &matched_index));
795 g_array_free (garray, TRUE);
797 /* Test for a not-found element in the middle of the array. */
798 garray = g_array_sized_new (FALSE, FALSE, sizeof (guint), 3);
799 for (i = 1; i < 10; i += 2)
800 g_array_append_val (garray, i);
803 g_assert_false (g_array_binary_search (garray, &i, cmpint, NULL));
806 g_assert_false (g_array_binary_search (garray, &i, cmpint, NULL));
809 g_assert_false (g_array_binary_search (garray, &i, cmpint, NULL));
811 g_array_free (garray, TRUE);
815 test_array_copy_sized (void)
817 GArray *array1 = NULL, *array2 = NULL, *array3 = NULL;
820 g_test_summary ("Test that copying a newly-allocated sized array works.");
822 array1 = g_array_sized_new (FALSE, FALSE, sizeof (int), 1);
823 array2 = g_array_copy (array1);
825 g_assert_cmpuint (array2->len, ==, array1->len);
827 g_array_append_val (array1, val);
828 array3 = g_array_copy (array1);
830 g_assert_cmpuint (array3->len, ==, array1->len);
831 g_assert_cmpuint (g_array_index (array3, int, 0), ==, g_array_index (array1, int, 0));
832 g_assert_cmpuint (array3->len, ==, 1);
833 g_assert_cmpuint (g_array_index (array3, int, 0), ==, val);
835 g_array_unref (array3);
836 g_array_unref (array2);
837 g_array_unref (array1);
840 /* Check g_ptr_array_steal() function */
842 pointer_array_steal (void)
844 const guint array_size = 10000;
850 gparray = g_ptr_array_new ();
851 pdata = g_ptr_array_steal (gparray, NULL);
852 g_assert_null (pdata);
854 pdata = g_ptr_array_steal (gparray, &len);
855 g_assert_null (pdata);
856 g_assert_cmpint (len, ==, 0);
858 for (i = 0; i < array_size; i++)
859 g_ptr_array_add (gparray, GINT_TO_POINTER (i));
861 past_len = gparray->len;
862 pdata = g_ptr_array_steal (gparray, &len);
863 g_assert_cmpint (gparray->len, ==, 0);
864 g_assert_cmpint (past_len, ==, len);
865 g_ptr_array_add (gparray, GINT_TO_POINTER (10));
867 g_assert_cmpint ((gsize) pdata[0], ==, (gsize) GINT_TO_POINTER (0));
868 g_assert_cmpint ((gsize) g_ptr_array_index (gparray, 0), ==,
869 (gsize) GINT_TO_POINTER (10));
870 g_assert_cmpint (gparray->len, ==, 1);
872 g_ptr_array_remove_index (gparray, 0);
874 for (i = 0; i < array_size; i++)
875 g_ptr_array_add (gparray, GINT_TO_POINTER (i));
876 g_assert_cmpmem (pdata, array_size * sizeof (gpointer),
877 gparray->pdata, array_size * sizeof (gpointer));
880 g_ptr_array_free (gparray, TRUE);
884 pointer_array_add (void)
891 gparray = g_ptr_array_sized_new (1000);
893 for (i = 0; i < 10000; i++)
894 g_ptr_array_add (gparray, GINT_TO_POINTER (i));
896 for (i = 0; i < 10000; i++)
897 g_assert (g_ptr_array_index (gparray, i) == GINT_TO_POINTER (i));
899 g_ptr_array_foreach (gparray, sum_up, &sum);
900 g_assert (sum == 49995000);
902 segment = g_ptr_array_free (gparray, FALSE);
903 for (i = 0; i < 10000; i++)
904 g_assert (segment[i] == GINT_TO_POINTER (i));
909 pointer_array_insert (void)
916 gparray = g_ptr_array_sized_new (1000);
918 for (i = 0; i < 10000; i++)
920 index = g_random_int_range (-1, i + 1);
921 g_ptr_array_insert (gparray, index, GINT_TO_POINTER (i));
924 g_ptr_array_foreach (gparray, sum_up, &sum);
925 g_assert (sum == 49995000);
927 g_ptr_array_free (gparray, TRUE);
931 pointer_array_ref_count (void)
938 gparray = g_ptr_array_new ();
939 for (i = 0; i < 10000; i++)
940 g_ptr_array_add (gparray, GINT_TO_POINTER (i));
942 /* check we can ref, unref and still access the array */
943 gparray2 = g_ptr_array_ref (gparray);
944 g_assert (gparray == gparray2);
945 g_ptr_array_unref (gparray2);
946 for (i = 0; i < 10000; i++)
947 g_assert (g_ptr_array_index (gparray, i) == GINT_TO_POINTER (i));
949 g_ptr_array_foreach (gparray, sum_up, &sum);
950 g_assert (sum == 49995000);
952 /* gparray2 should be an empty valid GPtrArray wrapper */
953 gparray2 = g_ptr_array_ref (gparray);
954 g_ptr_array_free (gparray, TRUE);
956 g_assert_cmpint (gparray2->len, ==, 0);
957 g_ptr_array_unref (gparray2);
960 static gint num_free_func_invocations = 0;
963 my_free_func (gpointer data)
965 num_free_func_invocations++;
970 pointer_array_free_func (void)
977 num_free_func_invocations = 0;
978 gparray = g_ptr_array_new_with_free_func (my_free_func);
979 g_ptr_array_unref (gparray);
980 g_assert_cmpint (num_free_func_invocations, ==, 0);
982 gparray = g_ptr_array_new_with_free_func (my_free_func);
983 g_ptr_array_free (gparray, TRUE);
984 g_assert_cmpint (num_free_func_invocations, ==, 0);
986 num_free_func_invocations = 0;
987 gparray = g_ptr_array_new_with_free_func (my_free_func);
988 g_ptr_array_add (gparray, g_strdup ("foo"));
989 g_ptr_array_add (gparray, g_strdup ("bar"));
990 g_ptr_array_add (gparray, g_strdup ("baz"));
991 g_ptr_array_remove_index (gparray, 0);
992 g_assert_cmpint (num_free_func_invocations, ==, 1);
993 g_ptr_array_remove_index_fast (gparray, 1);
994 g_assert_cmpint (num_free_func_invocations, ==, 2);
995 s = g_strdup ("frob");
996 g_ptr_array_add (gparray, s);
997 g_assert (g_ptr_array_remove (gparray, s));
998 g_assert (!g_ptr_array_remove (gparray, "nuun"));
999 g_assert (!g_ptr_array_remove_fast (gparray, "mlo"));
1000 g_assert_cmpint (num_free_func_invocations, ==, 3);
1001 s = g_strdup ("frob");
1002 g_ptr_array_add (gparray, s);
1003 g_ptr_array_set_size (gparray, 1);
1004 g_assert_cmpint (num_free_func_invocations, ==, 4);
1005 g_ptr_array_ref (gparray);
1006 g_ptr_array_unref (gparray);
1007 g_assert_cmpint (num_free_func_invocations, ==, 4);
1008 g_ptr_array_unref (gparray);
1009 g_assert_cmpint (num_free_func_invocations, ==, 5);
1011 num_free_func_invocations = 0;
1012 gparray = g_ptr_array_new_full (10, my_free_func);
1013 g_ptr_array_add (gparray, g_strdup ("foo"));
1014 g_ptr_array_add (gparray, g_strdup ("bar"));
1015 g_ptr_array_add (gparray, g_strdup ("baz"));
1016 g_ptr_array_set_size (gparray, 20);
1017 g_ptr_array_add (gparray, NULL);
1018 gparray2 = g_ptr_array_ref (gparray);
1019 strv = (gchar **) g_ptr_array_free (gparray, FALSE);
1020 g_assert_cmpint (num_free_func_invocations, ==, 0);
1022 g_ptr_array_unref (gparray2);
1023 g_assert_cmpint (num_free_func_invocations, ==, 0);
1025 num_free_func_invocations = 0;
1026 gparray = g_ptr_array_new_with_free_func (my_free_func);
1027 g_ptr_array_add (gparray, g_strdup ("foo"));
1028 g_ptr_array_add (gparray, g_strdup ("bar"));
1029 g_ptr_array_add (gparray, g_strdup ("baz"));
1030 g_ptr_array_remove_range (gparray, 1, 1);
1031 g_ptr_array_unref (gparray);
1032 g_assert_cmpint (num_free_func_invocations, ==, 3);
1034 num_free_func_invocations = 0;
1035 gparray = g_ptr_array_new_with_free_func (my_free_func);
1036 g_ptr_array_add (gparray, g_strdup ("foo"));
1037 g_ptr_array_add (gparray, g_strdup ("bar"));
1038 g_ptr_array_add (gparray, g_strdup ("baz"));
1039 g_ptr_array_free (gparray, TRUE);
1040 g_assert_cmpint (num_free_func_invocations, ==, 3);
1042 num_free_func_invocations = 0;
1043 gparray = g_ptr_array_new_with_free_func (my_free_func);
1044 g_ptr_array_add (gparray, "foo");
1045 g_ptr_array_add (gparray, "bar");
1046 g_ptr_array_add (gparray, "baz");
1047 g_ptr_array_set_free_func (gparray, NULL);
1048 g_ptr_array_free (gparray, TRUE);
1049 g_assert_cmpint (num_free_func_invocations, ==, 0);
1053 ptr_array_copy_func (gconstpointer src, gpointer userdata)
1055 gsize *dst = g_malloc (sizeof (gsize));
1056 *dst = *((gsize *) src);
1060 /* Test the g_ptr_array_copy() function */
1062 pointer_array_copy (void)
1064 GPtrArray *ptr_array, *ptr_array2;
1066 const gsize array_size = 100;
1067 gsize *array_test = g_malloc (array_size * sizeof (gsize));
1069 g_test_summary ("Check all normal behaviour of stealing elements from one "
1070 "array to append to another, covering different array sizes "
1071 "and element copy functions");
1073 if (g_test_undefined ())
1075 /* Testing degenerated cases */
1076 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1077 "*assertion*!= NULL*");
1078 ptr_array = g_ptr_array_copy (NULL, NULL, NULL);
1079 g_test_assert_expected_messages ();
1080 g_assert_cmpuint ((gsize) ptr_array, ==, (gsize) NULL);
1083 /* Initializing array_test */
1084 for (i = 0; i < array_size; i++)
1087 /* Test copy an empty array */
1088 ptr_array = g_ptr_array_sized_new (0);
1089 ptr_array2 = g_ptr_array_copy (ptr_array, NULL, NULL);
1091 g_assert_cmpuint (ptr_array2->len, ==, ptr_array->len);
1093 g_ptr_array_unref (ptr_array);
1094 g_ptr_array_unref (ptr_array2);
1096 /* Test simple copy */
1097 ptr_array = g_ptr_array_sized_new (array_size);
1099 for (i = 0; i < array_size; i++)
1100 g_ptr_array_add (ptr_array, &array_test[i]);
1102 ptr_array2 = g_ptr_array_copy (ptr_array, NULL, NULL);
1104 g_assert_cmpuint (ptr_array2->len, ==, ptr_array->len);
1105 for (i = 0; i < array_size; i++)
1106 g_assert_cmpuint (*((gsize *) g_ptr_array_index (ptr_array2, i)), ==, i);
1108 for (i = 0; i < array_size; i++)
1109 g_assert_cmpuint ((gsize) g_ptr_array_index (ptr_array, i), ==,
1110 (gsize) g_ptr_array_index (ptr_array2, i));
1112 g_ptr_array_free (ptr_array2, TRUE);
1114 /* Test copy through GCopyFunc */
1115 ptr_array2 = g_ptr_array_copy (ptr_array, ptr_array_copy_func, NULL);
1116 g_ptr_array_set_free_func (ptr_array2, g_free);
1118 g_assert_cmpuint (ptr_array2->len, ==, ptr_array->len);
1119 for (i = 0; i < array_size; i++)
1120 g_assert_cmpuint (*((gsize *) g_ptr_array_index (ptr_array2, i)), ==, i);
1122 for (i = 0; i < array_size; i++)
1123 g_assert_cmpuint ((gsize) g_ptr_array_index (ptr_array, i), !=,
1124 (gsize) g_ptr_array_index (ptr_array2, i));
1126 g_ptr_array_free (ptr_array2, TRUE);
1129 g_ptr_array_free (ptr_array, TRUE);
1130 g_free (array_test);
1133 /* Test the g_ptr_array_extend() function */
1135 pointer_array_extend (void)
1137 GPtrArray *ptr_array, *ptr_array2;
1139 const gsize array_size = 100;
1140 gsize *array_test = g_malloc (array_size * sizeof (gsize));
1142 if (g_test_undefined ())
1144 /* Testing degenerated cases */
1145 ptr_array = g_ptr_array_sized_new (0);
1146 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1147 "*assertion*!= NULL*");
1148 g_ptr_array_extend (NULL, ptr_array, NULL, NULL);
1149 g_test_assert_expected_messages ();
1151 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1152 "*assertion*!= NULL*");
1153 g_ptr_array_extend (ptr_array, NULL, NULL, NULL);
1154 g_test_assert_expected_messages ();
1156 g_ptr_array_unref (ptr_array);
1159 /* Initializing array_test */
1160 for (i = 0; i < array_size; i++)
1163 /* Testing extend with array of size zero */
1164 ptr_array = g_ptr_array_sized_new (0);
1165 ptr_array2 = g_ptr_array_sized_new (0);
1167 g_ptr_array_extend (ptr_array, ptr_array2, NULL, NULL);
1169 g_assert_cmpuint (ptr_array->len, ==, 0);
1170 g_assert_cmpuint (ptr_array2->len, ==, 0);
1172 g_ptr_array_unref (ptr_array);
1173 g_ptr_array_unref (ptr_array2);
1175 /* Testing extend an array of size zero */
1176 ptr_array = g_ptr_array_sized_new (array_size);
1177 ptr_array2 = g_ptr_array_sized_new (0);
1179 for (i = 0; i < array_size; i++)
1181 g_ptr_array_add (ptr_array, &array_test[i]);
1184 g_ptr_array_extend (ptr_array, ptr_array2, NULL, NULL);
1186 for (i = 0; i < array_size; i++)
1187 g_assert_cmpuint (*((gsize *) g_ptr_array_index (ptr_array, i)), ==, i);
1189 g_ptr_array_unref (ptr_array);
1190 g_ptr_array_unref (ptr_array2);
1192 /* Testing extend an array of size zero */
1193 ptr_array = g_ptr_array_sized_new (0);
1194 ptr_array2 = g_ptr_array_sized_new (array_size);
1196 for (i = 0; i < array_size; i++)
1198 g_ptr_array_add (ptr_array2, &array_test[i]);
1201 g_ptr_array_extend (ptr_array, ptr_array2, NULL, NULL);
1203 for (i = 0; i < array_size; i++)
1204 g_assert_cmpuint (*((gsize *) g_ptr_array_index (ptr_array, i)), ==, i);
1206 g_ptr_array_unref (ptr_array);
1207 g_ptr_array_unref (ptr_array2);
1209 /* Testing simple extend */
1210 ptr_array = g_ptr_array_sized_new (array_size / 2);
1211 ptr_array2 = g_ptr_array_sized_new (array_size / 2);
1213 for (i = 0; i < array_size / 2; i++)
1215 g_ptr_array_add (ptr_array, &array_test[i]);
1216 g_ptr_array_add (ptr_array2, &array_test[i + (array_size / 2)]);
1219 g_ptr_array_extend (ptr_array, ptr_array2, NULL, NULL);
1221 for (i = 0; i < array_size; i++)
1222 g_assert_cmpuint (*((gsize *) g_ptr_array_index (ptr_array, i)), ==, i);
1224 g_ptr_array_unref (ptr_array);
1225 g_ptr_array_unref (ptr_array2);
1227 /* Testing extend with GCopyFunc */
1228 ptr_array = g_ptr_array_sized_new (array_size / 2);
1229 ptr_array2 = g_ptr_array_sized_new (array_size / 2);
1231 for (i = 0; i < array_size / 2; i++)
1233 g_ptr_array_add (ptr_array, &array_test[i]);
1234 g_ptr_array_add (ptr_array2, &array_test[i + (array_size / 2)]);
1237 g_ptr_array_extend (ptr_array, ptr_array2, ptr_array_copy_func, NULL);
1239 for (i = 0; i < array_size; i++)
1240 g_assert_cmpuint (*((gsize *) g_ptr_array_index (ptr_array, i)), ==, i);
1242 /* Clean-up memory */
1243 for (i = array_size / 2; i < array_size; i++)
1244 g_free (g_ptr_array_index (ptr_array, i));
1246 g_ptr_array_unref (ptr_array);
1247 g_ptr_array_unref (ptr_array2);
1248 g_free (array_test);
1251 /* Test the g_ptr_array_extend_and_steal() function */
1253 pointer_array_extend_and_steal (void)
1255 GPtrArray *ptr_array, *ptr_array2, *ptr_array3;
1257 const gsize array_size = 100;
1258 gsize *array_test = g_malloc (array_size * sizeof (gsize));
1260 /* Initializing array_test */
1261 for (i = 0; i < array_size; i++)
1264 /* Testing simple extend_and_steal() */
1265 ptr_array = g_ptr_array_sized_new (array_size / 2);
1266 ptr_array2 = g_ptr_array_sized_new (array_size / 2);
1268 for (i = 0; i < array_size / 2; i++)
1270 g_ptr_array_add (ptr_array, &array_test[i]);
1271 g_ptr_array_add (ptr_array2, &array_test[i + (array_size / 2)]);
1274 g_ptr_array_extend_and_steal (ptr_array, ptr_array2);
1276 for (i = 0; i < array_size; i++)
1277 g_assert_cmpuint (*((gsize *) g_ptr_array_index (ptr_array, i)), ==, i);
1279 g_ptr_array_free (ptr_array, TRUE);
1281 /* Testing extend_and_steal() with a pending reference to stolen array */
1282 ptr_array = g_ptr_array_sized_new (array_size / 2);
1283 ptr_array2 = g_ptr_array_sized_new (array_size / 2);
1285 for (i = 0; i < array_size / 2; i++)
1287 g_ptr_array_add (ptr_array, &array_test[i]);
1288 g_ptr_array_add (ptr_array2, &array_test[i + (array_size / 2)]);
1291 ptr_array3 = g_ptr_array_ref (ptr_array2);
1293 g_ptr_array_extend_and_steal (ptr_array, ptr_array2);
1295 for (i = 0; i < array_size; i++)
1296 g_assert_cmpuint (*((gsize *) g_ptr_array_index (ptr_array, i)), ==, i);
1298 g_assert_cmpuint (ptr_array3->len, ==, 0);
1299 g_assert_null (ptr_array3->pdata);
1301 g_ptr_array_add (ptr_array2, NULL);
1303 g_ptr_array_free (ptr_array, TRUE);
1304 g_ptr_array_free (ptr_array3, TRUE);
1306 /* Final memory clean-up */
1307 g_free (array_test);
1311 ptr_compare (gconstpointer p1, gconstpointer p2)
1313 gpointer i1 = *(gpointer*)p1;
1314 gpointer i2 = *(gpointer*)p2;
1316 return GPOINTER_TO_INT (i1) - GPOINTER_TO_INT (i2);
1320 ptr_compare_data (gconstpointer p1, gconstpointer p2, gpointer data)
1322 gpointer i1 = *(gpointer*)p1;
1323 gpointer i2 = *(gpointer*)p2;
1325 return GPOINTER_TO_INT (i1) - GPOINTER_TO_INT (i2);
1329 pointer_array_sort (void)
1336 gparray = g_ptr_array_new ();
1337 for (i = 0; i < 10000; i++)
1339 val = g_random_int_range (0, 10000);
1340 g_ptr_array_add (gparray, GINT_TO_POINTER (val));
1343 g_ptr_array_sort (gparray, ptr_compare);
1346 for (i = 0; i < 10000; i++)
1348 cur = GPOINTER_TO_INT (g_ptr_array_index (gparray, i));
1349 g_assert_cmpint (prev, <=, cur);
1353 g_ptr_array_free (gparray, TRUE);
1356 /* Please keep pointer_array_sort_example() in sync with the doc-comment
1357 * of g_ptr_array_sort() */
1366 file_list_entry_free (gpointer p)
1368 FileListEntry *entry = p;
1370 g_free (entry->name);
1375 sort_filelist (gconstpointer a, gconstpointer b)
1377 const FileListEntry *entry1 = *((FileListEntry **) a);
1378 const FileListEntry *entry2 = *((FileListEntry **) b);
1380 return g_ascii_strcasecmp (entry1->name, entry2->name);
1384 pointer_array_sort_example (void)
1386 GPtrArray *file_list = NULL;
1387 FileListEntry *entry;
1389 g_test_summary ("Check that the doc-comment for g_ptr_array_sort() is correct");
1391 file_list = g_ptr_array_new_with_free_func (file_list_entry_free);
1393 entry = g_new0 (FileListEntry, 1);
1394 entry->name = g_strdup ("README");
1396 g_ptr_array_add (file_list, g_steal_pointer (&entry));
1398 entry = g_new0 (FileListEntry, 1);
1399 entry->name = g_strdup ("empty");
1401 g_ptr_array_add (file_list, g_steal_pointer (&entry));
1403 entry = g_new0 (FileListEntry, 1);
1404 entry->name = g_strdup ("aardvark");
1406 g_ptr_array_add (file_list, g_steal_pointer (&entry));
1408 g_ptr_array_sort (file_list, sort_filelist);
1410 g_assert_cmpuint (file_list->len, ==, 3);
1411 entry = g_ptr_array_index (file_list, 0);
1412 g_assert_cmpstr (entry->name, ==, "aardvark");
1413 entry = g_ptr_array_index (file_list, 1);
1414 g_assert_cmpstr (entry->name, ==, "empty");
1415 entry = g_ptr_array_index (file_list, 2);
1416 g_assert_cmpstr (entry->name, ==, "README");
1418 g_ptr_array_unref (file_list);
1421 /* Please keep pointer_array_sort_with_data_example() in sync with the
1422 * doc-comment of g_ptr_array_sort_with_data() */
1424 typedef enum { SORT_NAME, SORT_SIZE } SortMode;
1427 sort_filelist_how (gconstpointer a, gconstpointer b, gpointer user_data)
1430 const SortMode sort_mode = GPOINTER_TO_INT (user_data);
1431 const FileListEntry *entry1 = *((FileListEntry **) a);
1432 const FileListEntry *entry2 = *((FileListEntry **) b);
1437 order = g_ascii_strcasecmp (entry1->name, entry2->name);
1440 order = entry1->size - entry2->size;
1450 pointer_array_sort_with_data_example (void)
1452 GPtrArray *file_list = NULL;
1453 FileListEntry *entry;
1456 g_test_summary ("Check that the doc-comment for g_ptr_array_sort_with_data() is correct");
1458 file_list = g_ptr_array_new_with_free_func (file_list_entry_free);
1460 entry = g_new0 (FileListEntry, 1);
1461 entry->name = g_strdup ("README");
1463 g_ptr_array_add (file_list, g_steal_pointer (&entry));
1465 entry = g_new0 (FileListEntry, 1);
1466 entry->name = g_strdup ("empty");
1468 g_ptr_array_add (file_list, g_steal_pointer (&entry));
1470 entry = g_new0 (FileListEntry, 1);
1471 entry->name = g_strdup ("aardvark");
1473 g_ptr_array_add (file_list, g_steal_pointer (&entry));
1475 sort_mode = SORT_NAME;
1476 g_ptr_array_sort_with_data (file_list, sort_filelist_how, GINT_TO_POINTER (sort_mode));
1478 g_assert_cmpuint (file_list->len, ==, 3);
1479 entry = g_ptr_array_index (file_list, 0);
1480 g_assert_cmpstr (entry->name, ==, "aardvark");
1481 entry = g_ptr_array_index (file_list, 1);
1482 g_assert_cmpstr (entry->name, ==, "empty");
1483 entry = g_ptr_array_index (file_list, 2);
1484 g_assert_cmpstr (entry->name, ==, "README");
1486 sort_mode = SORT_SIZE;
1487 g_ptr_array_sort_with_data (file_list, sort_filelist_how, GINT_TO_POINTER (sort_mode));
1489 g_assert_cmpuint (file_list->len, ==, 3);
1490 entry = g_ptr_array_index (file_list, 0);
1491 g_assert_cmpstr (entry->name, ==, "empty");
1492 entry = g_ptr_array_index (file_list, 1);
1493 g_assert_cmpstr (entry->name, ==, "aardvark");
1494 entry = g_ptr_array_index (file_list, 2);
1495 g_assert_cmpstr (entry->name, ==, "README");
1497 g_ptr_array_unref (file_list);
1501 pointer_array_sort_with_data (void)
1507 gparray = g_ptr_array_new ();
1508 for (i = 0; i < 10000; i++)
1509 g_ptr_array_add (gparray, GINT_TO_POINTER (g_random_int_range (0, 10000)));
1511 g_ptr_array_sort_with_data (gparray, ptr_compare_data, NULL);
1514 for (i = 0; i < 10000; i++)
1516 cur = GPOINTER_TO_INT (g_ptr_array_index (gparray, i));
1517 g_assert_cmpint (prev, <=, cur);
1521 g_ptr_array_free (gparray, TRUE);
1525 pointer_array_find_empty (void)
1530 array = g_ptr_array_new ();
1532 g_assert_false (g_ptr_array_find (array, "some-value", NULL)); /* NULL index */
1533 g_assert_false (g_ptr_array_find (array, "some-value", &idx)); /* non-NULL index */
1534 g_assert_false (g_ptr_array_find_with_equal_func (array, "some-value", g_str_equal, NULL)); /* NULL index */
1535 g_assert_false (g_ptr_array_find_with_equal_func (array, "some-value", g_str_equal, &idx)); /* non-NULL index */
1537 g_ptr_array_free (array, TRUE);
1541 pointer_array_find_non_empty (void)
1545 const gchar *str_pointer = "static-string";
1547 array = g_ptr_array_new ();
1549 g_ptr_array_add (array, "some");
1550 g_ptr_array_add (array, "random");
1551 g_ptr_array_add (array, "values");
1552 g_ptr_array_add (array, "some");
1553 g_ptr_array_add (array, "duplicated");
1554 g_ptr_array_add (array, (gpointer) str_pointer);
1556 g_assert_true (g_ptr_array_find_with_equal_func (array, "random", g_str_equal, NULL)); /* NULL index */
1557 g_assert_true (g_ptr_array_find_with_equal_func (array, "random", g_str_equal, &idx)); /* non-NULL index */
1558 g_assert_cmpuint (idx, ==, 1);
1560 g_assert_true (g_ptr_array_find_with_equal_func (array, "some", g_str_equal, &idx)); /* duplicate element */
1561 g_assert_cmpuint (idx, ==, 0);
1563 g_assert_false (g_ptr_array_find_with_equal_func (array, "nope", g_str_equal, NULL));
1565 g_assert_true (g_ptr_array_find_with_equal_func (array, str_pointer, g_str_equal, &idx));
1566 g_assert_cmpuint (idx, ==, 5);
1568 g_assert_true (g_ptr_array_find_with_equal_func (array, str_pointer, NULL, &idx)); /* NULL equal func */
1569 g_assert_cmpuint (idx, ==, 5);
1571 g_assert_true (g_ptr_array_find (array, str_pointer, &idx)); /* NULL equal func */
1572 g_assert_cmpuint (idx, ==, 5);
1574 g_ptr_array_free (array, TRUE);
1578 steal_destroy_notify (gpointer data)
1580 guint *counter = data;
1581 *counter = *counter + 1;
1584 /* Test that g_ptr_array_steal_index() and g_ptr_array_steal_index_fast() can
1585 * remove elements from a pointer array without the #GDestroyNotify being called. */
1587 pointer_array_steal_index (void)
1589 guint i1 = 0, i2 = 0, i3 = 0, i4 = 0;
1590 gpointer out1, out2;
1591 GPtrArray *array = g_ptr_array_new_with_free_func (steal_destroy_notify);
1593 g_ptr_array_add (array, &i1);
1594 g_ptr_array_add (array, &i2);
1595 g_ptr_array_add (array, &i3);
1596 g_ptr_array_add (array, &i4);
1598 g_assert_cmpuint (array->len, ==, 4);
1600 /* Remove a single element. */
1601 out1 = g_ptr_array_steal_index (array, 0);
1602 g_assert_true (out1 == &i1);
1603 g_assert_cmpuint (i1, ==, 0); /* should not have been destroyed */
1605 /* Following elements should have been moved down. */
1606 g_assert_cmpuint (array->len, ==, 3);
1607 g_assert_true (g_ptr_array_index (array, 0) == &i2);
1608 g_assert_true (g_ptr_array_index (array, 1) == &i3);
1609 g_assert_true (g_ptr_array_index (array, 2) == &i4);
1611 /* Remove another element, quickly. */
1612 out2 = g_ptr_array_steal_index_fast (array, 0);
1613 g_assert_true (out2 == &i2);
1614 g_assert_cmpuint (i2, ==, 0); /* should not have been destroyed */
1616 /* Last element should have been swapped in place. */
1617 g_assert_cmpuint (array->len, ==, 2);
1618 g_assert_true (g_ptr_array_index (array, 0) == &i4);
1619 g_assert_true (g_ptr_array_index (array, 1) == &i3);
1621 /* Check that destroying the pointer array doesn’t affect the stolen elements. */
1622 g_ptr_array_unref (array);
1624 g_assert_cmpuint (i1, ==, 0);
1625 g_assert_cmpuint (i2, ==, 0);
1626 g_assert_cmpuint (i3, ==, 1);
1627 g_assert_cmpuint (i4, ==, 1);
1631 byte_array_steal (void)
1633 const guint array_size = 10000;
1634 GByteArray *gbarray;
1637 gsize len, past_len;
1639 gbarray = g_byte_array_new ();
1640 bdata = g_byte_array_steal (gbarray, NULL);
1641 g_assert_cmpint ((gsize) bdata, ==, (gsize) gbarray->data);
1644 for (i = 0; i < array_size; i++)
1645 g_byte_array_append (gbarray, (guint8 *) "abcd", 4);
1647 past_len = gbarray->len;
1648 bdata = g_byte_array_steal (gbarray, &len);
1650 g_assert_cmpint (len, ==, past_len);
1651 g_assert_cmpint (gbarray->len, ==, 0);
1653 g_byte_array_append (gbarray, (guint8 *) "@", 1);
1655 g_assert_cmpint (bdata[0], ==, 'a');
1656 g_assert_cmpint (gbarray->data[0], ==, '@');
1657 g_assert_cmpint (gbarray->len, ==, 1);
1659 g_byte_array_remove_index (gbarray, 0);
1662 g_byte_array_free (gbarray, TRUE);
1666 byte_array_append (void)
1668 GByteArray *gbarray;
1672 gbarray = g_byte_array_sized_new (1000);
1673 for (i = 0; i < 10000; i++)
1674 g_byte_array_append (gbarray, (guint8*) "abcd", 4);
1676 for (i = 0; i < 10000; i++)
1678 g_assert (gbarray->data[4*i] == 'a');
1679 g_assert (gbarray->data[4*i+1] == 'b');
1680 g_assert (gbarray->data[4*i+2] == 'c');
1681 g_assert (gbarray->data[4*i+3] == 'd');
1684 segment = g_byte_array_free (gbarray, FALSE);
1686 for (i = 0; i < 10000; i++)
1688 g_assert (segment[4*i] == 'a');
1689 g_assert (segment[4*i+1] == 'b');
1690 g_assert (segment[4*i+2] == 'c');
1691 g_assert (segment[4*i+3] == 'd');
1698 byte_array_prepend (void)
1700 GByteArray *gbarray;
1703 gbarray = g_byte_array_new ();
1704 g_byte_array_set_size (gbarray, 1000);
1706 for (i = 0; i < 10000; i++)
1707 g_byte_array_prepend (gbarray, (guint8*) "abcd", 4);
1709 for (i = 0; i < 10000; i++)
1711 g_assert (gbarray->data[4*i] == 'a');
1712 g_assert (gbarray->data[4*i+1] == 'b');
1713 g_assert (gbarray->data[4*i+2] == 'c');
1714 g_assert (gbarray->data[4*i+3] == 'd');
1717 g_byte_array_free (gbarray, TRUE);
1721 byte_array_ref_count (void)
1723 GByteArray *gbarray;
1724 GByteArray *gbarray2;
1727 gbarray = g_byte_array_new ();
1728 for (i = 0; i < 10000; i++)
1729 g_byte_array_append (gbarray, (guint8*) "abcd", 4);
1731 gbarray2 = g_byte_array_ref (gbarray);
1732 g_assert (gbarray2 == gbarray);
1733 g_byte_array_unref (gbarray2);
1734 for (i = 0; i < 10000; i++)
1736 g_assert (gbarray->data[4*i] == 'a');
1737 g_assert (gbarray->data[4*i+1] == 'b');
1738 g_assert (gbarray->data[4*i+2] == 'c');
1739 g_assert (gbarray->data[4*i+3] == 'd');
1742 gbarray2 = g_byte_array_ref (gbarray);
1743 g_assert (gbarray2 == gbarray);
1744 g_byte_array_free (gbarray, TRUE);
1745 g_assert_cmpint (gbarray2->len, ==, 0);
1746 g_byte_array_unref (gbarray2);
1750 byte_array_remove (void)
1752 GByteArray *gbarray;
1755 gbarray = g_byte_array_new ();
1756 for (i = 0; i < 100; i++)
1757 g_byte_array_append (gbarray, (guint8*) "abcd", 4);
1759 g_assert_cmpint (gbarray->len, ==, 400);
1761 g_byte_array_remove_index (gbarray, 4);
1762 g_byte_array_remove_index (gbarray, 4);
1763 g_byte_array_remove_index (gbarray, 4);
1764 g_byte_array_remove_index (gbarray, 4);
1766 g_assert_cmpint (gbarray->len, ==, 396);
1768 for (i = 0; i < 99; i++)
1770 g_assert (gbarray->data[4*i] == 'a');
1771 g_assert (gbarray->data[4*i+1] == 'b');
1772 g_assert (gbarray->data[4*i+2] == 'c');
1773 g_assert (gbarray->data[4*i+3] == 'd');
1776 g_byte_array_free (gbarray, TRUE);
1780 byte_array_remove_fast (void)
1782 GByteArray *gbarray;
1785 gbarray = g_byte_array_new ();
1786 for (i = 0; i < 100; i++)
1787 g_byte_array_append (gbarray, (guint8*) "abcd", 4);
1789 g_assert_cmpint (gbarray->len, ==, 400);
1791 g_byte_array_remove_index_fast (gbarray, 4);
1792 g_byte_array_remove_index_fast (gbarray, 4);
1793 g_byte_array_remove_index_fast (gbarray, 4);
1794 g_byte_array_remove_index_fast (gbarray, 4);
1796 g_assert_cmpint (gbarray->len, ==, 396);
1798 for (i = 0; i < 99; i++)
1800 g_assert (gbarray->data[4*i] == 'a');
1801 g_assert (gbarray->data[4*i+1] == 'b');
1802 g_assert (gbarray->data[4*i+2] == 'c');
1803 g_assert (gbarray->data[4*i+3] == 'd');
1806 g_byte_array_free (gbarray, TRUE);
1810 byte_array_remove_range (void)
1812 GByteArray *gbarray;
1815 gbarray = g_byte_array_new ();
1816 for (i = 0; i < 100; i++)
1817 g_byte_array_append (gbarray, (guint8*) "abcd", 4);
1819 g_assert_cmpint (gbarray->len, ==, 400);
1821 g_byte_array_remove_range (gbarray, 12, 4);
1823 g_assert_cmpint (gbarray->len, ==, 396);
1825 for (i = 0; i < 99; i++)
1827 g_assert (gbarray->data[4*i] == 'a');
1828 g_assert (gbarray->data[4*i+1] == 'b');
1829 g_assert (gbarray->data[4*i+2] == 'c');
1830 g_assert (gbarray->data[4*i+3] == 'd');
1833 /* Ensure the entire array can be cleared, even when empty. */
1834 g_byte_array_remove_range (gbarray, 0, gbarray->len);
1835 g_byte_array_remove_range (gbarray, 0, gbarray->len);
1837 g_byte_array_free (gbarray, TRUE);
1841 byte_compare (gconstpointer p1, gconstpointer p2)
1843 const guint8 *i1 = p1;
1844 const guint8 *i2 = p2;
1850 byte_compare_data (gconstpointer p1, gconstpointer p2, gpointer data)
1852 const guint8 *i1 = p1;
1853 const guint8 *i2 = p2;
1859 byte_array_sort (void)
1861 GByteArray *gbarray;
1866 gbarray = g_byte_array_new ();
1867 for (i = 0; i < 100; i++)
1869 val = 'a' + g_random_int_range (0, 26);
1870 g_byte_array_append (gbarray, (guint8*) &val, 1);
1873 g_byte_array_sort (gbarray, byte_compare);
1876 for (i = 0; i < gbarray->len; i++)
1878 cur = gbarray->data[i];
1879 g_assert_cmpint (prev, <=, cur);
1883 g_byte_array_free (gbarray, TRUE);
1887 byte_array_sort_with_data (void)
1889 GByteArray *gbarray;
1894 gbarray = g_byte_array_new ();
1895 for (i = 0; i < 100; i++)
1897 val = 'a' + g_random_int_range (0, 26);
1898 g_byte_array_append (gbarray, (guint8*) &val, 1);
1901 g_byte_array_sort_with_data (gbarray, byte_compare_data, NULL);
1904 for (i = 0; i < gbarray->len; i++)
1906 cur = gbarray->data[i];
1907 g_assert_cmpint (prev, <=, cur);
1911 g_byte_array_free (gbarray, TRUE);
1915 byte_array_new_take (void)
1917 GByteArray *gbarray;
1920 data = g_memdup ("woooweeewow", 11);
1921 gbarray = g_byte_array_new_take (data, 11);
1922 g_assert (gbarray->data == data);
1923 g_assert_cmpuint (gbarray->len, ==, 11);
1924 g_byte_array_free (gbarray, TRUE);
1928 byte_array_free_to_bytes (void)
1930 GByteArray *gbarray;
1935 gbarray = g_byte_array_new ();
1936 g_byte_array_append (gbarray, (guint8 *)"woooweeewow", 11);
1937 memory = gbarray->data;
1939 bytes = g_byte_array_free_to_bytes (gbarray);
1940 g_assert (bytes != NULL);
1941 g_assert_cmpuint (g_bytes_get_size (bytes), ==, 11);
1942 g_assert (g_bytes_get_data (bytes, &size) == memory);
1943 g_assert_cmpuint (size, ==, 11);
1945 g_bytes_unref (bytes);
1949 add_array_test (const gchar *test_path,
1950 const ArrayTestData *config,
1951 GTestDataFunc test_func)
1953 gchar *test_name = NULL;
1955 test_name = g_strdup_printf ("%s/%s-%s",
1957 config->zero_terminated ? "zero-terminated" : "non-zero-terminated",
1958 config->clear_ ? "clear" : "no-clear");
1959 g_test_add_data_func (test_name, config, test_func);
1964 main (int argc, char *argv[])
1966 /* Test all possible combinations of g_array_new() parameters. */
1967 const ArrayTestData array_configurations[] =
1976 g_test_init (&argc, &argv, NULL);
1978 g_test_bug_base ("https://bugzilla.gnome.org/");
1981 g_test_add_func ("/array/new/zero-terminated", array_new_zero_terminated);
1982 g_test_add_func ("/array/ref-count", array_ref_count);
1983 g_test_add_func ("/array/steal", array_steal);
1984 g_test_add_func ("/array/clear-func", array_clear_func);
1985 g_test_add_func ("/array/binary-search", test_array_binary_search);
1986 g_test_add_func ("/array/copy-sized", test_array_copy_sized);
1988 for (i = 0; i < G_N_ELEMENTS (array_configurations); i++)
1990 add_array_test ("/array/set-size", &array_configurations[i], array_set_size);
1991 add_array_test ("/array/set-size/sized", &array_configurations[i], array_set_size_sized);
1992 add_array_test ("/array/append-val", &array_configurations[i], array_append_val);
1993 add_array_test ("/array/prepend-val", &array_configurations[i], array_prepend_val);
1994 add_array_test ("/array/prepend-vals", &array_configurations[i], array_prepend_vals);
1995 add_array_test ("/array/insert-vals", &array_configurations[i], array_insert_vals);
1996 add_array_test ("/array/remove-index", &array_configurations[i], array_remove_index);
1997 add_array_test ("/array/remove-index-fast", &array_configurations[i], array_remove_index_fast);
1998 add_array_test ("/array/remove-range", &array_configurations[i], array_remove_range);
1999 add_array_test ("/array/copy", &array_configurations[i], array_copy);
2000 add_array_test ("/array/sort", &array_configurations[i], array_sort);
2001 add_array_test ("/array/sort-with-data", &array_configurations[i], array_sort_with_data);
2004 /* pointer arrays */
2005 g_test_add_func ("/pointerarray/add", pointer_array_add);
2006 g_test_add_func ("/pointerarray/insert", pointer_array_insert);
2007 g_test_add_func ("/pointerarray/ref-count", pointer_array_ref_count);
2008 g_test_add_func ("/pointerarray/free-func", pointer_array_free_func);
2009 g_test_add_func ("/pointerarray/array_copy", pointer_array_copy);
2010 g_test_add_func ("/pointerarray/array_extend", pointer_array_extend);
2011 g_test_add_func ("/pointerarray/array_extend_and_steal", pointer_array_extend_and_steal);
2012 g_test_add_func ("/pointerarray/sort", pointer_array_sort);
2013 g_test_add_func ("/pointerarray/sort/example", pointer_array_sort_example);
2014 g_test_add_func ("/pointerarray/sort-with-data", pointer_array_sort_with_data);
2015 g_test_add_func ("/pointerarray/sort-with-data/example", pointer_array_sort_with_data_example);
2016 g_test_add_func ("/pointerarray/find/empty", pointer_array_find_empty);
2017 g_test_add_func ("/pointerarray/find/non-empty", pointer_array_find_non_empty);
2018 g_test_add_func ("/pointerarray/steal", pointer_array_steal);
2019 g_test_add_func ("/pointerarray/steal_index", pointer_array_steal_index);
2022 g_test_add_func ("/bytearray/steal", byte_array_steal);
2023 g_test_add_func ("/bytearray/append", byte_array_append);
2024 g_test_add_func ("/bytearray/prepend", byte_array_prepend);
2025 g_test_add_func ("/bytearray/remove", byte_array_remove);
2026 g_test_add_func ("/bytearray/remove-fast", byte_array_remove_fast);
2027 g_test_add_func ("/bytearray/remove-range", byte_array_remove_range);
2028 g_test_add_func ("/bytearray/ref-count", byte_array_ref_count);
2029 g_test_add_func ("/bytearray/sort", byte_array_sort);
2030 g_test_add_func ("/bytearray/sort-with-data", byte_array_sort_with_data);
2031 g_test_add_func ("/bytearray/new-take", byte_array_new_take);
2032 g_test_add_func ("/bytearray/free-to-bytes", byte_array_free_to_bytes);
2034 return g_test_run ();