Imported Upstream version 2.65.0
[platform/upstream/glib.git] / glib / tests / array-test.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 /*
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/. 
23  */
24
25 #undef G_DISABLE_ASSERT
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include "glib.h"
31
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. */
35 typedef struct
36 {
37   gboolean zero_terminated;
38   gboolean clear_;
39 } ArrayTestData;
40
41 /* Assert that @garray contains @n_expected_elements as given in @expected_data.
42  * @garray must contain #gint elements. */
43 static void
44 assert_int_array_equal (GArray     *garray,
45                         const gint *expected_data,
46                         gsize       n_expected_elements)
47 {
48   gsize i;
49
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]);
53 }
54
55 /* Iff config->zero_terminated is %TRUE, assert that the final element of
56  * @garray is zero. @garray must contain #gint elements. */
57 static void
58 assert_int_array_zero_terminated (const ArrayTestData *config,
59                                   GArray              *garray)
60 {
61   if (config->zero_terminated)
62     {
63       gint *data = (gint *) garray->data;
64       g_assert_cmpint (data[garray->len], ==, 0);
65     }
66 }
67
68 static void
69 sum_up (gpointer data,
70         gpointer user_data)
71 {
72   gint *sum = (gint *)user_data;
73
74   *sum += GPOINTER_TO_INT (data);
75 }
76
77 /* Check that expanding an array with g_array_set_size() clears the new elements
78  * if @clear_ was specified during construction. */
79 static void
80 array_set_size (gconstpointer test_data)
81 {
82   const ArrayTestData *config = test_data;
83   GArray *garray;
84   gsize i;
85
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);
89
90   g_array_set_size (garray, 5);
91   g_assert_cmpuint (garray->len, ==, 5);
92   assert_int_array_zero_terminated (config, garray);
93
94   if (config->clear_)
95     for (i = 0; i < 5; i++)
96       g_assert_cmpint (g_array_index (garray, gint, i), ==, 0);
97
98   g_array_unref (garray);
99 }
100
101 /* As with array_set_size(), but with a sized array. */
102 static void
103 array_set_size_sized (gconstpointer test_data)
104 {
105   const ArrayTestData *config = test_data;
106   GArray *garray;
107   gsize i;
108
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);
112
113   g_array_set_size (garray, 5);
114   g_assert_cmpuint (garray->len, ==, 5);
115   assert_int_array_zero_terminated (config, garray);
116
117   if (config->clear_)
118     for (i = 0; i < 5; i++)
119       g_assert_cmpint (g_array_index (garray, gint, i), ==, 0);
120
121   g_array_unref (garray);
122 }
123
124 /* Check that a zero-terminated array does actually have a zero terminator. */
125 static void
126 array_new_zero_terminated (void)
127 {
128   GArray *garray;
129   gchar *out_str = NULL;
130
131   garray = g_array_new (TRUE, FALSE, sizeof (gchar));
132   g_assert_cmpuint (garray->len, ==, 0);
133
134   g_array_append_vals (garray, "hello", strlen ("hello"));
135   g_assert_cmpuint (garray->len, ==, 5);
136   g_assert_cmpstr (garray->data, ==, "hello");
137
138   out_str = g_array_free (garray, FALSE);
139   g_assert_cmpstr (out_str, ==, "hello");
140   g_free (out_str);
141 }
142
143 /* Check g_array_steal() function */
144 static void
145 array_steal (void)
146 {
147   const guint array_size = 10000;
148   GArray *garray;
149   gint *adata;
150   guint i;
151   gsize len, past_len;
152
153   garray = g_array_new (FALSE, FALSE, sizeof (gint));
154   adata = (gint *) g_array_steal (garray, NULL);
155   g_assert_null (adata);
156
157   adata = (gint *) g_array_steal (garray, &len);
158   g_assert_null (adata);
159   g_assert_cmpint (len, ==, 0);
160
161   for (i = 0; i < array_size; i++)
162     g_array_append_val (garray, i);
163
164   for (i = 0; i < array_size; i++)
165     g_assert_cmpint (g_array_index (garray, gint, i), ==, i);
166
167
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);
172
173   g_assert_cmpint (past_len, ==, len);
174   g_assert_cmpint (garray->len, ==, 0);
175
176   g_array_append_val (garray, i);
177
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);
181
182   g_array_remove_index (garray, 0);
183
184   for (i = 0; i < array_size; i++)
185     g_array_append_val (garray, i);
186
187   g_assert_cmpint (garray->len, ==, array_size);
188   g_assert_cmpmem (adata, array_size * sizeof (gint),
189                    garray->data, array_size * sizeof (gint));
190   g_free (adata);
191   g_array_free (garray, TRUE);
192 }
193
194 /* Check that g_array_append_val() works correctly for various #GArray
195  * configurations. */
196 static void
197 array_append_val (gconstpointer test_data)
198 {
199   const ArrayTestData *config = test_data;
200   GArray *garray;
201   gint i;
202   gint *segment;
203
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);
208
209   for (i = 0; i < 10000; i++)
210     g_assert_cmpint (g_array_index (garray, gint, i), ==, i);
211
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);
217
218   g_free (segment);
219 }
220
221 /* Check that g_array_prepend_val() works correctly for various #GArray
222  * configurations. */
223 static void
224 array_prepend_val (gconstpointer test_data)
225 {
226   const ArrayTestData *config = test_data;
227   GArray *garray;
228   gint i;
229
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);
234
235   for (i = 0; i < 100; i++)
236     g_assert_cmpint (g_array_index (garray, gint, i), ==, (100 - i - 1));
237
238   g_array_free (garray, TRUE);
239 }
240
241 /* Test that g_array_prepend_vals() works correctly with various array
242  * configurations. */
243 static void
244 array_prepend_vals (gconstpointer test_data)
245 {
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 };
252
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);
256
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);
262
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);
268
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);
274
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);
280
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);
286
287   g_array_free (garray, TRUE);
288 }
289
290 /* Test that g_array_insert_vals() works correctly with various array
291  * configurations. */
292 static void
293 array_insert_vals (gconstpointer test_data)
294 {
295   const ArrayTestData *config = test_data;
296   GArray *garray, *garray_out;
297   gsize i;
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 };
304
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);
308
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);
314
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);
320
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);
326
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);
332
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);
338
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);
344
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);
349
350   g_assert_cmpuint (garray->len, ==, G_N_ELEMENTS (expected_vals5));
351   for (i = 0; i < G_N_ELEMENTS (expected_vals5); i++)
352     {
353       if (config->clear_ || i < 6 || i > 9)
354         g_assert_cmpint (g_array_index (garray, gint, i), ==, expected_vals5[i]);
355     }
356
357   assert_int_array_zero_terminated (config, garray);
358
359   g_array_free (garray, TRUE);
360 }
361
362 /* Check that g_array_remove_index() works correctly for various #GArray
363  * configurations. */
364 static void
365 array_remove_index (gconstpointer test_data)
366 {
367   const ArrayTestData *config = test_data;
368   GArray *garray;
369   gint i;
370   gint prev, cur;
371
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);
376
377   g_assert_cmpint (garray->len, ==, 100);
378
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);
383
384   g_assert_cmpint (garray->len, ==, 96);
385   assert_int_array_zero_terminated (config, garray);
386
387   prev = -1;
388   for (i = 0; i < garray->len; i++)
389     {
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);
393       prev = cur;
394     }
395
396   g_array_free (garray, TRUE);
397 }
398
399 /* Check that g_array_remove_index_fast() works correctly for various #GArray
400  * configurations. */
401 static void
402 array_remove_index_fast (gconstpointer test_data)
403 {
404   const ArrayTestData *config = test_data;
405   GArray *garray;
406   gint i;
407   gint prev, cur;
408
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);
412
413   g_assert_cmpint (garray->len, ==, 100);
414   assert_int_array_zero_terminated (config, garray);
415
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);
420
421   g_assert_cmpint (garray->len, ==, 96);
422   assert_int_array_zero_terminated (config, garray);
423
424   prev = -1;
425   for (i = 0; i < garray->len; i++)
426     {
427       cur = g_array_index (garray, gint, i);
428       g_assert (cur != 1 &&  cur != 3 && cur != 21 && cur != 57);
429       if (cur < 96)
430         {
431           g_assert_cmpint (prev, <, cur);
432           prev = cur;
433         }
434     }
435
436   g_array_free (garray, TRUE);
437 }
438
439 /* Check that g_array_remove_range() works correctly for various #GArray
440  * configurations. */
441 static void
442 array_remove_range (gconstpointer test_data)
443 {
444   const ArrayTestData *config = test_data;
445   GArray *garray;
446   gint i;
447   gint prev, cur;
448
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);
452
453   g_assert_cmpint (garray->len, ==, 100);
454   assert_int_array_zero_terminated (config, garray);
455
456   g_array_remove_range (garray, 31, 4);
457
458   g_assert_cmpint (garray->len, ==, 96);
459   assert_int_array_zero_terminated (config, garray);
460
461   prev = -1;
462   for (i = 0; i < garray->len; i++)
463     {
464       cur = g_array_index (garray, gint, i);
465       g_assert (cur < 31 || cur > 34);
466       g_assert_cmpint (prev, <, cur);
467       prev = cur;
468     }
469
470   /* Ensure the entire array can be cleared, even when empty. */
471   g_array_remove_range (garray, 0, garray->len);
472
473   g_assert_cmpint (garray->len, ==, 0);
474   assert_int_array_zero_terminated (config, garray);
475
476   g_array_remove_range (garray, 0, garray->len);
477
478   g_assert_cmpint (garray->len, ==, 0);
479   assert_int_array_zero_terminated (config, garray);
480
481   g_array_free (garray, TRUE);
482 }
483
484 static void
485 array_ref_count (void)
486 {
487   GArray *garray;
488   GArray *garray2;
489   gint i;
490
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);
495
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));
502
503   /* garray2 should be an empty valid GArray wrapper */
504   garray2 = g_array_ref (garray);
505   g_array_free (garray, TRUE);
506
507   g_assert_cmpint (garray2->len, ==, 0);
508   g_array_unref (garray2);
509 }
510
511 static int
512 int_compare (gconstpointer p1, gconstpointer p2)
513 {
514   const gint *i1 = p1;
515   const gint *i2 = p2;
516
517   return *i1 - *i2;
518 }
519
520 static void
521 array_copy (gconstpointer test_data)
522 {
523   GArray *array, *array_copy;
524   gsize i;
525   const ArrayTestData *config = test_data;
526   const gsize array_size = 100;
527
528   /* Testing degenerated cases */
529   if (g_test_undefined ())
530     {
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 ();
535
536       g_assert_null (array);
537     }
538
539   /* Testing simple copy */
540   array = g_array_new (config->zero_terminated, config->clear_, sizeof (gint));
541
542   for (i = 0; i < array_size; i++)
543     g_array_append_val (array, i);
544
545   array_copy = g_array_copy (array);
546
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));
551
552   /* Check internal parameters ('zero_terminated' flag) */
553   if (config->zero_terminated)
554     {
555       const gint *data = (const gint *) array_copy->data;
556       g_assert_cmpint (data[array_copy->len], ==, 0);
557     }
558
559   /* Check internal parameters ('clear' flag) */
560   if (config->clear_)
561     {
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);
566     }
567
568   /* Clean-up */
569   g_array_unref (array);
570   g_array_unref (array_copy);
571 }
572
573 static int
574 int_compare_data (gconstpointer p1, gconstpointer p2, gpointer data)
575 {
576   const gint *i1 = p1;
577   const gint *i2 = p2;
578
579   return *i1 - *i2;
580 }
581
582 /* Check that g_array_sort() works correctly for various #GArray
583  * configurations. */
584 static void
585 array_sort (gconstpointer test_data)
586 {
587   const ArrayTestData *config = test_data;
588   GArray *garray;
589   gint i;
590   gint prev, cur;
591
592   garray = g_array_new (config->zero_terminated, config->clear_, sizeof (gint));
593   for (i = 0; i < 10000; i++)
594     {
595       cur = g_random_int_range (0, 10000);
596       g_array_append_val (garray, cur);
597     }
598   assert_int_array_zero_terminated (config, garray);
599
600   g_array_sort (garray, int_compare);
601   assert_int_array_zero_terminated (config, garray);
602
603   prev = -1;
604   for (i = 0; i < garray->len; i++)
605     {
606       cur = g_array_index (garray, gint, i);
607       g_assert_cmpint (prev, <=, cur);
608       prev = cur;
609     }
610
611   g_array_free (garray, TRUE);
612 }
613
614 /* Check that g_array_sort_with_data() works correctly for various #GArray
615  * configurations. */
616 static void
617 array_sort_with_data (gconstpointer test_data)
618 {
619   const ArrayTestData *config = test_data;
620   GArray *garray;
621   gint i;
622   gint prev, cur;
623
624   garray = g_array_new (config->zero_terminated, config->clear_, sizeof (gint));
625   for (i = 0; i < 10000; i++)
626     {
627       cur = g_random_int_range (0, 10000);
628       g_array_append_val (garray, cur);
629     }
630   assert_int_array_zero_terminated (config, garray);
631
632   g_array_sort_with_data (garray, int_compare_data, NULL);
633   assert_int_array_zero_terminated (config, garray);
634
635   prev = -1;
636   for (i = 0; i < garray->len; i++)
637     {
638       cur = g_array_index (garray, gint, i);
639       g_assert_cmpint (prev, <=, cur);
640       prev = cur;
641     }
642
643   g_array_free (garray, TRUE);
644 }
645
646 static gint num_clear_func_invocations = 0;
647
648 static void
649 my_clear_func (gpointer data)
650 {
651   num_clear_func_invocations += 1;
652 }
653
654 static void
655 array_clear_func (void)
656 {
657   GArray *garray;
658   gint i;
659   gint cur;
660
661   garray = g_array_new (FALSE, FALSE, sizeof (gint));
662   g_array_set_clear_func (garray, my_clear_func);
663
664   for (i = 0; i < 10; i++)
665     {
666       cur = g_random_int_range (0, 100);
667       g_array_append_val (garray, cur);
668     }
669
670   g_array_remove_index (garray, 9);
671   g_assert_cmpint (num_clear_func_invocations, ==, 1);
672
673   g_array_remove_range (garray, 5, 3);
674   g_assert_cmpint (num_clear_func_invocations, ==, 4);
675
676   g_array_remove_index_fast (garray, 4);
677   g_assert_cmpint (num_clear_func_invocations, ==, 5);
678
679   g_array_free (garray, TRUE);
680   g_assert_cmpint (num_clear_func_invocations, ==, 10);
681 }
682
683 /* Defining a comparison function for testing g_array_binary_search() */
684 static gint
685 cmpint (gconstpointer a, gconstpointer b)
686 {
687   const gint *_a = a;
688   const gint *_b = b;
689
690   return *_a - *_b;
691 }
692
693 /* Testing g_array_binary_search() function */
694 static void
695 test_array_binary_search (void)
696 {
697   GArray *garray;
698   guint i, matched_index;
699
700   if (g_test_undefined ())
701     {
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 ();
708
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);
714     }
715
716   /* Testing array of size 0 */
717   garray = g_array_sized_new (FALSE, FALSE, sizeof (guint), 0);
718
719   i = 1;
720   g_assert_false (g_array_binary_search (garray, &i, cmpint, NULL));
721
722   g_array_free (garray, TRUE);
723
724   /* Testing array of size 1 */
725   garray = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
726   i = 1;
727   g_array_append_val (garray, i);
728
729   g_assert_true (g_array_binary_search (garray, &i, cmpint, NULL));
730
731   i = 0;
732   g_assert_false (g_array_binary_search (garray, &i, cmpint, NULL));
733
734   i = 2;
735   g_assert_false (g_array_binary_search (garray, &i, cmpint, NULL));
736
737   g_array_free (garray, TRUE);
738
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);
743
744   for (i = 1; i < 3; i++)
745     g_assert_true (g_array_binary_search (garray, &i, cmpint, NULL));
746
747   i = 0;
748   g_assert_false (g_array_binary_search (garray, &i, cmpint, NULL));
749
750   i = 4;
751   g_assert_false (g_array_binary_search (garray, &i, cmpint, NULL));
752
753   g_array_free (garray, TRUE);
754
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);
759
760   for (i = 1; i < 4; i++)
761     g_assert_true (g_array_binary_search (garray, &i, cmpint, NULL));
762
763   i = 0;
764   g_assert_false (g_array_binary_search (garray, &i, cmpint, NULL));
765
766   i = 5;
767   g_assert_false (g_array_binary_search (garray, &i, cmpint, NULL));
768
769   g_array_free (garray, TRUE);
770
771   /* Testing array of size 10000 */
772   garray = g_array_sized_new (FALSE, FALSE, sizeof (guint), 10000);
773
774   for (i = 1; i < 10001; i++)
775     g_array_append_val (garray, i);
776
777   for (i = 1; i < 10001; i++)
778     g_assert_true (g_array_binary_search (garray, &i, cmpint, NULL));
779
780   for (i = 1; i < 10001; i++)
781     {
782       g_assert_true (g_array_binary_search (garray, &i, cmpint, &matched_index));
783       g_assert_cmpint (i, ==, matched_index + 1);
784     }
785
786   /* Testing negative result */
787   i = 0;
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));
790
791   i = 10002;
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));
794
795   g_array_free (garray, TRUE);
796
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);
801
802   i = 0;
803   g_assert_false (g_array_binary_search (garray, &i, cmpint, NULL));
804
805   i = 2;
806   g_assert_false (g_array_binary_search (garray, &i, cmpint, NULL));
807
808   i = 10;
809   g_assert_false (g_array_binary_search (garray, &i, cmpint, NULL));
810
811   g_array_free (garray, TRUE);
812 }
813
814 static void
815 test_array_copy_sized (void)
816 {
817   GArray *array1 = NULL, *array2 = NULL, *array3 = NULL;
818   int val = 5;
819
820   g_test_summary ("Test that copying a newly-allocated sized array works.");
821
822   array1 = g_array_sized_new (FALSE, FALSE, sizeof (int), 1);
823   array2 = g_array_copy (array1);
824
825   g_assert_cmpuint (array2->len, ==, array1->len);
826
827   g_array_append_val (array1, val);
828   array3 = g_array_copy (array1);
829
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);
834
835   g_array_unref (array3);
836   g_array_unref (array2);
837   g_array_unref (array1);
838 }
839
840 /* Check g_ptr_array_steal() function */
841 static void
842 pointer_array_steal (void)
843 {
844   const guint array_size = 10000;
845   GPtrArray *gparray;
846   gpointer *pdata;
847   guint i;
848   gsize len, past_len;
849
850   gparray = g_ptr_array_new ();
851   pdata = g_ptr_array_steal (gparray, NULL);
852   g_assert_null (pdata);
853
854   pdata = g_ptr_array_steal (gparray, &len);
855   g_assert_null (pdata);
856   g_assert_cmpint (len, ==, 0);
857
858   for (i = 0; i < array_size; i++)
859     g_ptr_array_add (gparray, GINT_TO_POINTER (i));
860
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));
866
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);
871
872   g_ptr_array_remove_index (gparray, 0);
873
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));
878   g_free (pdata);
879
880   g_ptr_array_free (gparray, TRUE);
881 }
882
883 static void
884 pointer_array_add (void)
885 {
886   GPtrArray *gparray;
887   gint i;
888   gint sum = 0;
889   gpointer *segment;
890
891   gparray = g_ptr_array_sized_new (1000);
892
893   for (i = 0; i < 10000; i++)
894     g_ptr_array_add (gparray, GINT_TO_POINTER (i));
895
896   for (i = 0; i < 10000; i++)
897     g_assert (g_ptr_array_index (gparray, i) == GINT_TO_POINTER (i));
898   
899   g_ptr_array_foreach (gparray, sum_up, &sum);
900   g_assert (sum == 49995000);
901
902   segment = g_ptr_array_free (gparray, FALSE);
903   for (i = 0; i < 10000; i++)
904     g_assert (segment[i] == GINT_TO_POINTER (i));
905   g_free (segment);
906 }
907
908 static void
909 pointer_array_insert (void)
910 {
911   GPtrArray *gparray;
912   gint i;
913   gint sum = 0;
914   gint index;
915
916   gparray = g_ptr_array_sized_new (1000);
917
918   for (i = 0; i < 10000; i++)
919     {
920       index = g_random_int_range (-1, i + 1);
921       g_ptr_array_insert (gparray, index, GINT_TO_POINTER (i));
922     }
923
924   g_ptr_array_foreach (gparray, sum_up, &sum);
925   g_assert (sum == 49995000);
926
927   g_ptr_array_free (gparray, TRUE);
928 }
929
930 static void
931 pointer_array_ref_count (void)
932 {
933   GPtrArray *gparray;
934   GPtrArray *gparray2;
935   gint i;
936   gint sum = 0;
937
938   gparray = g_ptr_array_new ();
939   for (i = 0; i < 10000; i++)
940     g_ptr_array_add (gparray, GINT_TO_POINTER (i));
941
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));
948
949   g_ptr_array_foreach (gparray, sum_up, &sum);
950   g_assert (sum == 49995000);
951
952   /* gparray2 should be an empty valid GPtrArray wrapper */
953   gparray2 = g_ptr_array_ref (gparray);
954   g_ptr_array_free (gparray, TRUE);
955
956   g_assert_cmpint (gparray2->len, ==, 0);
957   g_ptr_array_unref (gparray2);
958 }
959
960 static gint num_free_func_invocations = 0;
961
962 static void
963 my_free_func (gpointer data)
964 {
965   num_free_func_invocations++;
966   g_free (data);
967 }
968
969 static void
970 pointer_array_free_func (void)
971 {
972   GPtrArray *gparray;
973   GPtrArray *gparray2;
974   gchar **strv;
975   gchar *s;
976
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);
981
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);
985
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);
1010
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);
1021   g_strfreev (strv);
1022   g_ptr_array_unref (gparray2);
1023   g_assert_cmpint (num_free_func_invocations, ==, 0);
1024
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);
1033
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);
1041
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);
1050 }
1051
1052 static gpointer
1053 ptr_array_copy_func (gconstpointer src, gpointer userdata)
1054 {
1055   gsize *dst = g_malloc (sizeof (gsize));
1056   *dst = *((gsize *) src);
1057   return dst;
1058 }
1059
1060 /* Test the g_ptr_array_copy() function */
1061 static void
1062 pointer_array_copy (void)
1063 {
1064   GPtrArray *ptr_array, *ptr_array2;
1065   gsize i;
1066   const gsize array_size = 100;
1067   gsize *array_test = g_malloc (array_size * sizeof (gsize));
1068
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");
1072
1073   if (g_test_undefined ())
1074     {
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);
1081     }
1082
1083   /* Initializing array_test */
1084   for (i = 0; i < array_size; i++)
1085     array_test[i] = i;
1086
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);
1090
1091   g_assert_cmpuint (ptr_array2->len, ==, ptr_array->len);
1092
1093   g_ptr_array_unref (ptr_array);
1094   g_ptr_array_unref (ptr_array2);
1095
1096   /* Test simple copy */
1097   ptr_array = g_ptr_array_sized_new (array_size);
1098
1099   for (i = 0; i < array_size; i++)
1100     g_ptr_array_add (ptr_array, &array_test[i]);
1101
1102   ptr_array2 = g_ptr_array_copy (ptr_array, NULL, NULL);
1103
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);
1107
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));
1111
1112   g_ptr_array_free (ptr_array2, TRUE);
1113
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);
1117
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);
1121
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));
1125
1126   g_ptr_array_free (ptr_array2, TRUE);
1127
1128   /* Final cleanup */
1129   g_ptr_array_free (ptr_array, TRUE);
1130   g_free (array_test);
1131 }
1132
1133 /* Test the g_ptr_array_extend() function */
1134 static void
1135 pointer_array_extend (void)
1136 {
1137   GPtrArray *ptr_array, *ptr_array2;
1138   gsize i;
1139   const gsize array_size = 100;
1140   gsize *array_test = g_malloc (array_size * sizeof (gsize));
1141
1142   if (g_test_undefined ())
1143     {
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 ();
1150
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 ();
1155
1156       g_ptr_array_unref (ptr_array);
1157     }
1158
1159   /* Initializing array_test */
1160   for (i = 0; i < array_size; i++)
1161     array_test[i] = i;
1162
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);
1166
1167   g_ptr_array_extend (ptr_array, ptr_array2, NULL, NULL);
1168
1169   g_assert_cmpuint (ptr_array->len, ==, 0);
1170   g_assert_cmpuint (ptr_array2->len, ==, 0);
1171
1172   g_ptr_array_unref (ptr_array);
1173   g_ptr_array_unref (ptr_array2);
1174
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);
1178
1179   for (i = 0; i < array_size; i++)
1180     {
1181       g_ptr_array_add (ptr_array, &array_test[i]);
1182     }
1183
1184   g_ptr_array_extend (ptr_array, ptr_array2, NULL, NULL);
1185
1186   for (i = 0; i < array_size; i++)
1187     g_assert_cmpuint (*((gsize *) g_ptr_array_index (ptr_array, i)), ==, i);
1188
1189   g_ptr_array_unref (ptr_array);
1190   g_ptr_array_unref (ptr_array2);
1191
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);
1195
1196   for (i = 0; i < array_size; i++)
1197     {
1198       g_ptr_array_add (ptr_array2, &array_test[i]);
1199     }
1200
1201   g_ptr_array_extend (ptr_array, ptr_array2, NULL, NULL);
1202
1203   for (i = 0; i < array_size; i++)
1204     g_assert_cmpuint (*((gsize *) g_ptr_array_index (ptr_array, i)), ==, i);
1205
1206   g_ptr_array_unref (ptr_array);
1207   g_ptr_array_unref (ptr_array2);
1208
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);
1212
1213   for (i = 0; i < array_size / 2; i++)
1214     {
1215       g_ptr_array_add (ptr_array, &array_test[i]);
1216       g_ptr_array_add (ptr_array2, &array_test[i + (array_size / 2)]);
1217     }
1218
1219   g_ptr_array_extend (ptr_array, ptr_array2, NULL, NULL);
1220
1221   for (i = 0; i < array_size; i++)
1222     g_assert_cmpuint (*((gsize *) g_ptr_array_index (ptr_array, i)), ==, i);
1223
1224   g_ptr_array_unref (ptr_array);
1225   g_ptr_array_unref (ptr_array2);
1226
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);
1230
1231   for (i = 0; i < array_size / 2; i++)
1232     {
1233       g_ptr_array_add (ptr_array, &array_test[i]);
1234       g_ptr_array_add (ptr_array2, &array_test[i + (array_size / 2)]);
1235     }
1236
1237   g_ptr_array_extend (ptr_array, ptr_array2, ptr_array_copy_func, NULL);
1238
1239   for (i = 0; i < array_size; i++)
1240     g_assert_cmpuint (*((gsize *) g_ptr_array_index (ptr_array, i)), ==, i);
1241
1242   /* Clean-up memory */
1243   for (i = array_size / 2; i < array_size; i++)
1244     g_free (g_ptr_array_index (ptr_array, i));
1245
1246   g_ptr_array_unref (ptr_array);
1247   g_ptr_array_unref (ptr_array2);
1248   g_free (array_test);
1249 }
1250
1251 /* Test the g_ptr_array_extend_and_steal() function */
1252 static void
1253 pointer_array_extend_and_steal (void)
1254 {
1255   GPtrArray *ptr_array, *ptr_array2, *ptr_array3;
1256   gsize i;
1257   const gsize array_size = 100;
1258   gsize *array_test = g_malloc (array_size * sizeof (gsize));
1259
1260   /* Initializing array_test */
1261   for (i = 0; i < array_size; i++)
1262     array_test[i] = i;
1263
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);
1267
1268   for (i = 0; i < array_size / 2; i++)
1269     {
1270       g_ptr_array_add (ptr_array, &array_test[i]);
1271       g_ptr_array_add (ptr_array2, &array_test[i + (array_size / 2)]);
1272     }
1273
1274   g_ptr_array_extend_and_steal (ptr_array, ptr_array2);
1275
1276   for (i = 0; i < array_size; i++)
1277     g_assert_cmpuint (*((gsize *) g_ptr_array_index (ptr_array, i)), ==, i);
1278
1279   g_ptr_array_free (ptr_array, TRUE);
1280
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);
1284
1285   for (i = 0; i < array_size / 2; i++)
1286     {
1287       g_ptr_array_add (ptr_array, &array_test[i]);
1288       g_ptr_array_add (ptr_array2, &array_test[i + (array_size / 2)]);
1289     }
1290
1291   ptr_array3 = g_ptr_array_ref (ptr_array2);
1292
1293   g_ptr_array_extend_and_steal (ptr_array, ptr_array2);
1294
1295   for (i = 0; i < array_size; i++)
1296     g_assert_cmpuint (*((gsize *) g_ptr_array_index (ptr_array, i)), ==, i);
1297
1298   g_assert_cmpuint (ptr_array3->len, ==, 0);
1299   g_assert_null (ptr_array3->pdata);
1300
1301   g_ptr_array_add (ptr_array2, NULL);
1302
1303   g_ptr_array_free (ptr_array, TRUE);
1304   g_ptr_array_free (ptr_array3, TRUE);
1305
1306   /* Final memory clean-up */
1307   g_free (array_test);
1308 }
1309
1310 static gint
1311 ptr_compare (gconstpointer p1, gconstpointer p2)
1312 {
1313   gpointer i1 = *(gpointer*)p1;
1314   gpointer i2 = *(gpointer*)p2;
1315
1316   return GPOINTER_TO_INT (i1) - GPOINTER_TO_INT (i2);
1317 }
1318
1319 static gint
1320 ptr_compare_data (gconstpointer p1, gconstpointer p2, gpointer data)
1321 {
1322   gpointer i1 = *(gpointer*)p1;
1323   gpointer i2 = *(gpointer*)p2;
1324
1325   return GPOINTER_TO_INT (i1) - GPOINTER_TO_INT (i2);
1326 }
1327
1328 static void
1329 pointer_array_sort (void)
1330 {
1331   GPtrArray *gparray;
1332   gint i;
1333   gint val;
1334   gint prev, cur;
1335
1336   gparray = g_ptr_array_new ();
1337   for (i = 0; i < 10000; i++)
1338     {
1339       val = g_random_int_range (0, 10000);
1340       g_ptr_array_add (gparray, GINT_TO_POINTER (val));
1341     }
1342
1343   g_ptr_array_sort (gparray, ptr_compare);
1344
1345   prev = -1;
1346   for (i = 0; i < 10000; i++)
1347     {
1348       cur = GPOINTER_TO_INT (g_ptr_array_index (gparray, i));
1349       g_assert_cmpint (prev, <=, cur);
1350       prev = cur;
1351     }
1352
1353   g_ptr_array_free (gparray, TRUE);
1354 }
1355
1356 /* Please keep pointer_array_sort_example() in sync with the doc-comment
1357  * of g_ptr_array_sort() */
1358
1359 typedef struct
1360 {
1361   gchar *name;
1362   gint size;
1363 } FileListEntry;
1364
1365 static void
1366 file_list_entry_free (gpointer p)
1367 {
1368   FileListEntry *entry = p;
1369
1370   g_free (entry->name);
1371   g_free (entry);
1372 }
1373
1374 static gint
1375 sort_filelist (gconstpointer a, gconstpointer b)
1376 {
1377    const FileListEntry *entry1 = *((FileListEntry **) a);
1378    const FileListEntry *entry2 = *((FileListEntry **) b);
1379
1380    return g_ascii_strcasecmp (entry1->name, entry2->name);
1381 }
1382
1383 static void
1384 pointer_array_sort_example (void)
1385 {
1386   GPtrArray *file_list = NULL;
1387   FileListEntry *entry;
1388
1389   g_test_summary ("Check that the doc-comment for g_ptr_array_sort() is correct");
1390
1391   file_list = g_ptr_array_new_with_free_func (file_list_entry_free);
1392
1393   entry = g_new0 (FileListEntry, 1);
1394   entry->name = g_strdup ("README");
1395   entry->size = 42;
1396   g_ptr_array_add (file_list, g_steal_pointer (&entry));
1397
1398   entry = g_new0 (FileListEntry, 1);
1399   entry->name = g_strdup ("empty");
1400   entry->size = 0;
1401   g_ptr_array_add (file_list, g_steal_pointer (&entry));
1402
1403   entry = g_new0 (FileListEntry, 1);
1404   entry->name = g_strdup ("aardvark");
1405   entry->size = 23;
1406   g_ptr_array_add (file_list, g_steal_pointer (&entry));
1407
1408   g_ptr_array_sort (file_list, sort_filelist);
1409
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");
1417
1418   g_ptr_array_unref (file_list);
1419 }
1420
1421 /* Please keep pointer_array_sort_with_data_example() in sync with the
1422  * doc-comment of g_ptr_array_sort_with_data() */
1423
1424 typedef enum { SORT_NAME, SORT_SIZE } SortMode;
1425
1426 static gint
1427 sort_filelist_how (gconstpointer a, gconstpointer b, gpointer user_data)
1428 {
1429   gint order;
1430   const SortMode sort_mode = GPOINTER_TO_INT (user_data);
1431   const FileListEntry *entry1 = *((FileListEntry **) a);
1432   const FileListEntry *entry2 = *((FileListEntry **) b);
1433
1434   switch (sort_mode)
1435     {
1436     case SORT_NAME:
1437       order = g_ascii_strcasecmp (entry1->name, entry2->name);
1438       break;
1439     case SORT_SIZE:
1440       order = entry1->size - entry2->size;
1441       break;
1442     default:
1443       order = 0;
1444       break;
1445     }
1446   return order;
1447 }
1448
1449 static void
1450 pointer_array_sort_with_data_example (void)
1451 {
1452   GPtrArray *file_list = NULL;
1453   FileListEntry *entry;
1454   SortMode sort_mode;
1455
1456   g_test_summary ("Check that the doc-comment for g_ptr_array_sort_with_data() is correct");
1457
1458   file_list = g_ptr_array_new_with_free_func (file_list_entry_free);
1459
1460   entry = g_new0 (FileListEntry, 1);
1461   entry->name = g_strdup ("README");
1462   entry->size = 42;
1463   g_ptr_array_add (file_list, g_steal_pointer (&entry));
1464
1465   entry = g_new0 (FileListEntry, 1);
1466   entry->name = g_strdup ("empty");
1467   entry->size = 0;
1468   g_ptr_array_add (file_list, g_steal_pointer (&entry));
1469
1470   entry = g_new0 (FileListEntry, 1);
1471   entry->name = g_strdup ("aardvark");
1472   entry->size = 23;
1473   g_ptr_array_add (file_list, g_steal_pointer (&entry));
1474
1475   sort_mode = SORT_NAME;
1476   g_ptr_array_sort_with_data (file_list, sort_filelist_how, GINT_TO_POINTER (sort_mode));
1477
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");
1485
1486   sort_mode = SORT_SIZE;
1487   g_ptr_array_sort_with_data (file_list, sort_filelist_how, GINT_TO_POINTER (sort_mode));
1488
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");
1496
1497   g_ptr_array_unref (file_list);
1498 }
1499
1500 static void
1501 pointer_array_sort_with_data (void)
1502 {
1503   GPtrArray *gparray;
1504   gint i;
1505   gint prev, cur;
1506
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)));
1510
1511   g_ptr_array_sort_with_data (gparray, ptr_compare_data, NULL);
1512
1513   prev = -1;
1514   for (i = 0; i < 10000; i++)
1515     {
1516       cur = GPOINTER_TO_INT (g_ptr_array_index (gparray, i));
1517       g_assert_cmpint (prev, <=, cur);
1518       prev = cur;
1519     }
1520
1521   g_ptr_array_free (gparray, TRUE);
1522 }
1523
1524 static void
1525 pointer_array_find_empty (void)
1526 {
1527   GPtrArray *array;
1528   guint idx;
1529
1530   array = g_ptr_array_new ();
1531
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 */
1536
1537   g_ptr_array_free (array, TRUE);
1538 }
1539
1540 static void
1541 pointer_array_find_non_empty (void)
1542 {
1543   GPtrArray *array;
1544   guint idx;
1545   const gchar *str_pointer = "static-string";
1546
1547   array = g_ptr_array_new ();
1548
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);
1555
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);
1559
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);
1562
1563   g_assert_false (g_ptr_array_find_with_equal_func (array, "nope", g_str_equal, NULL));
1564
1565   g_assert_true (g_ptr_array_find_with_equal_func (array, str_pointer, g_str_equal, &idx));
1566   g_assert_cmpuint (idx, ==, 5);
1567   idx = G_MAXUINT;
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);
1570   idx = G_MAXUINT;
1571   g_assert_true (g_ptr_array_find (array, str_pointer, &idx));  /* NULL equal func */
1572   g_assert_cmpuint (idx, ==, 5);
1573
1574   g_ptr_array_free (array, TRUE);
1575 }
1576
1577 static void
1578 steal_destroy_notify (gpointer data)
1579 {
1580   guint *counter = data;
1581   *counter = *counter + 1;
1582 }
1583
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. */
1586 static void
1587 pointer_array_steal_index (void)
1588 {
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);
1592
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);
1597
1598   g_assert_cmpuint (array->len, ==, 4);
1599
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 */
1604
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);
1610
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 */
1615
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);
1620
1621   /* Check that destroying the pointer array doesn’t affect the stolen elements. */
1622   g_ptr_array_unref (array);
1623
1624   g_assert_cmpuint (i1, ==, 0);
1625   g_assert_cmpuint (i2, ==, 0);
1626   g_assert_cmpuint (i3, ==, 1);
1627   g_assert_cmpuint (i4, ==, 1);
1628 }
1629
1630 static void
1631 byte_array_steal (void)
1632 {
1633   const guint array_size = 10000;
1634   GByteArray *gbarray;
1635   guint8 *bdata;
1636   guint i;
1637   gsize len, past_len;
1638
1639   gbarray = g_byte_array_new ();
1640   bdata = g_byte_array_steal (gbarray, NULL);
1641   g_assert_cmpint ((gsize) bdata, ==, (gsize) gbarray->data);
1642   g_free (bdata);
1643
1644   for (i = 0; i < array_size; i++)
1645     g_byte_array_append (gbarray, (guint8 *) "abcd", 4);
1646
1647   past_len = gbarray->len;
1648   bdata = g_byte_array_steal (gbarray, &len);
1649
1650   g_assert_cmpint (len, ==, past_len);
1651   g_assert_cmpint (gbarray->len, ==, 0);
1652
1653   g_byte_array_append (gbarray, (guint8 *) "@", 1);
1654
1655   g_assert_cmpint (bdata[0], ==, 'a');
1656   g_assert_cmpint (gbarray->data[0], ==, '@');
1657   g_assert_cmpint (gbarray->len, ==, 1);
1658
1659   g_byte_array_remove_index (gbarray, 0);
1660
1661   g_free (bdata);
1662   g_byte_array_free (gbarray, TRUE);
1663 }
1664
1665 static void
1666 byte_array_append (void)
1667 {
1668   GByteArray *gbarray;
1669   gint i;
1670   guint8 *segment;
1671
1672   gbarray = g_byte_array_sized_new (1000);
1673   for (i = 0; i < 10000; i++)
1674     g_byte_array_append (gbarray, (guint8*) "abcd", 4);
1675
1676   for (i = 0; i < 10000; i++)
1677     {
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');
1682     }
1683
1684   segment = g_byte_array_free (gbarray, FALSE);
1685
1686   for (i = 0; i < 10000; i++)
1687     {
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');
1692     }
1693
1694   g_free (segment);
1695 }
1696
1697 static void
1698 byte_array_prepend (void)
1699 {
1700   GByteArray *gbarray;
1701   gint i;
1702
1703   gbarray = g_byte_array_new ();
1704   g_byte_array_set_size (gbarray, 1000);
1705
1706   for (i = 0; i < 10000; i++)
1707     g_byte_array_prepend (gbarray, (guint8*) "abcd", 4);
1708
1709   for (i = 0; i < 10000; i++)
1710     {
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');
1715     }
1716
1717   g_byte_array_free (gbarray, TRUE);
1718 }
1719
1720 static void
1721 byte_array_ref_count (void)
1722 {
1723   GByteArray *gbarray;
1724   GByteArray *gbarray2;
1725   gint i;
1726
1727   gbarray = g_byte_array_new ();
1728   for (i = 0; i < 10000; i++)
1729     g_byte_array_append (gbarray, (guint8*) "abcd", 4);
1730
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++)
1735     {
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');
1740     }
1741
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);
1747 }
1748
1749 static void
1750 byte_array_remove (void)
1751 {
1752   GByteArray *gbarray;
1753   gint i;
1754
1755   gbarray = g_byte_array_new ();
1756   for (i = 0; i < 100; i++)
1757     g_byte_array_append (gbarray, (guint8*) "abcd", 4);
1758
1759   g_assert_cmpint (gbarray->len, ==, 400);
1760
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);
1765
1766   g_assert_cmpint (gbarray->len, ==, 396);
1767
1768   for (i = 0; i < 99; i++)
1769     {
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');
1774     }
1775
1776   g_byte_array_free (gbarray, TRUE);
1777 }
1778
1779 static void
1780 byte_array_remove_fast (void)
1781 {
1782   GByteArray *gbarray;
1783   gint i;
1784
1785   gbarray = g_byte_array_new ();
1786   for (i = 0; i < 100; i++)
1787     g_byte_array_append (gbarray, (guint8*) "abcd", 4);
1788
1789   g_assert_cmpint (gbarray->len, ==, 400);
1790
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);
1795
1796   g_assert_cmpint (gbarray->len, ==, 396);
1797
1798   for (i = 0; i < 99; i++)
1799     {
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');
1804     }
1805
1806   g_byte_array_free (gbarray, TRUE);
1807 }
1808
1809 static void
1810 byte_array_remove_range (void)
1811 {
1812   GByteArray *gbarray;
1813   gint i;
1814
1815   gbarray = g_byte_array_new ();
1816   for (i = 0; i < 100; i++)
1817     g_byte_array_append (gbarray, (guint8*) "abcd", 4);
1818
1819   g_assert_cmpint (gbarray->len, ==, 400);
1820
1821   g_byte_array_remove_range (gbarray, 12, 4);
1822
1823   g_assert_cmpint (gbarray->len, ==, 396);
1824
1825   for (i = 0; i < 99; i++)
1826     {
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');
1831     }
1832
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);
1836
1837   g_byte_array_free (gbarray, TRUE);
1838 }
1839
1840 static int
1841 byte_compare (gconstpointer p1, gconstpointer p2)
1842 {
1843   const guint8 *i1 = p1;
1844   const guint8 *i2 = p2;
1845
1846   return *i1 - *i2;
1847 }
1848
1849 static int
1850 byte_compare_data (gconstpointer p1, gconstpointer p2, gpointer data)
1851 {
1852   const guint8 *i1 = p1;
1853   const guint8 *i2 = p2;
1854
1855   return *i1 - *i2;
1856 }
1857
1858 static void
1859 byte_array_sort (void)
1860 {
1861   GByteArray *gbarray;
1862   gint i;
1863   guint8 val;
1864   guint8 prev, cur;
1865
1866   gbarray = g_byte_array_new ();
1867   for (i = 0; i < 100; i++)
1868     {
1869       val = 'a' + g_random_int_range (0, 26);
1870       g_byte_array_append (gbarray, (guint8*) &val, 1);
1871     }
1872
1873   g_byte_array_sort (gbarray, byte_compare);
1874
1875   prev = 'a';
1876   for (i = 0; i < gbarray->len; i++)
1877     {
1878       cur = gbarray->data[i];
1879       g_assert_cmpint (prev, <=, cur);
1880       prev = cur;
1881     }
1882
1883   g_byte_array_free (gbarray, TRUE);
1884 }
1885
1886 static void
1887 byte_array_sort_with_data (void)
1888 {
1889   GByteArray *gbarray;
1890   gint i;
1891   guint8 val;
1892   guint8 prev, cur;
1893
1894   gbarray = g_byte_array_new ();
1895   for (i = 0; i < 100; i++)
1896     {
1897       val = 'a' + g_random_int_range (0, 26);
1898       g_byte_array_append (gbarray, (guint8*) &val, 1);
1899     }
1900
1901   g_byte_array_sort_with_data (gbarray, byte_compare_data, NULL);
1902
1903   prev = 'a';
1904   for (i = 0; i < gbarray->len; i++)
1905     {
1906       cur = gbarray->data[i];
1907       g_assert_cmpint (prev, <=, cur);
1908       prev = cur;
1909     }
1910
1911   g_byte_array_free (gbarray, TRUE);
1912 }
1913
1914 static void
1915 byte_array_new_take (void)
1916 {
1917   GByteArray *gbarray;
1918   guint8 *data;
1919
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);
1925 }
1926
1927 static void
1928 byte_array_free_to_bytes (void)
1929 {
1930   GByteArray *gbarray;
1931   gpointer memory;
1932   GBytes *bytes;
1933   gsize size;
1934
1935   gbarray = g_byte_array_new ();
1936   g_byte_array_append (gbarray, (guint8 *)"woooweeewow", 11);
1937   memory = gbarray->data;
1938
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);
1944
1945   g_bytes_unref (bytes);
1946 }
1947
1948 static void
1949 add_array_test (const gchar         *test_path,
1950                 const ArrayTestData *config,
1951                 GTestDataFunc        test_func)
1952 {
1953   gchar *test_name = NULL;
1954
1955   test_name = g_strdup_printf ("%s/%s-%s",
1956                                test_path,
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);
1960   g_free (test_name);
1961 }
1962
1963 int
1964 main (int argc, char *argv[])
1965 {
1966   /* Test all possible combinations of g_array_new() parameters. */
1967   const ArrayTestData array_configurations[] =
1968     {
1969       { FALSE, FALSE },
1970       { FALSE, TRUE },
1971       { TRUE, FALSE },
1972       { TRUE, TRUE },
1973     };
1974   gsize i;
1975
1976   g_test_init (&argc, &argv, NULL);
1977
1978   g_test_bug_base ("https://bugzilla.gnome.org/");
1979
1980   /* array tests */
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);
1987
1988   for (i = 0; i < G_N_ELEMENTS (array_configurations); i++)
1989     {
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);
2002     }
2003
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);
2020
2021   /* byte arrays */
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);
2033
2034   return g_test_run ();
2035 }