"Fossies" - the Fresh Open Source Software Archive

Member "glib-2.66.8/glib/tests/array-test.c" (18 Mar 2021, 59415 Bytes) of package /linux/misc/glib-2.66.8.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "array-test.c": 2.67.2_vs_2.67.3.

    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 #include "gstrfuncsprivate.h"
   33 
   34 /* Test data to be passed to any function which calls g_array_new(), providing
   35  * the parameters for that call. Most #GArray tests should be repeated for all
   36  * possible values of #ArrayTestData. */
   37 typedef struct
   38 {
   39   gboolean zero_terminated;
   40   gboolean clear_;
   41 } ArrayTestData;
   42 
   43 /* Assert that @garray contains @n_expected_elements as given in @expected_data.
   44  * @garray must contain #gint elements. */
   45 static void
   46 assert_int_array_equal (GArray     *garray,
   47                         const gint *expected_data,
   48                         gsize       n_expected_elements)
   49 {
   50   gsize i;
   51 
   52   g_assert_cmpuint (garray->len, ==, n_expected_elements);
   53   for (i = 0; i < garray->len; i++)
   54     g_assert_cmpint (g_array_index (garray, gint, i), ==, expected_data[i]);
   55 }
   56 
   57 /* Iff config->zero_terminated is %TRUE, assert that the final element of
   58  * @garray is zero. @garray must contain #gint elements. */
   59 static void
   60 assert_int_array_zero_terminated (const ArrayTestData *config,
   61                                   GArray              *garray)
   62 {
   63   if (config->zero_terminated)
   64     {
   65       gint *data = (gint *) garray->data;
   66       g_assert_cmpint (data[garray->len], ==, 0);
   67     }
   68 }
   69 
   70 static void
   71 sum_up (gpointer data,
   72     gpointer user_data)
   73 {
   74   gint *sum = (gint *)user_data;
   75 
   76   *sum += GPOINTER_TO_INT (data);
   77 }
   78 
   79 /* Check that expanding an array with g_array_set_size() clears the new elements
   80  * if @clear_ was specified during construction. */
   81 static void
   82 array_set_size (gconstpointer test_data)
   83 {
   84   const ArrayTestData *config = test_data;
   85   GArray *garray;
   86   gsize i;
   87 
   88   garray = g_array_new (config->zero_terminated, config->clear_, sizeof (gint));
   89   g_assert_cmpuint (garray->len, ==, 0);
   90   assert_int_array_zero_terminated (config, garray);
   91 
   92   g_array_set_size (garray, 5);
   93   g_assert_cmpuint (garray->len, ==, 5);
   94   assert_int_array_zero_terminated (config, garray);
   95 
   96   if (config->clear_)
   97     for (i = 0; i < 5; i++)
   98       g_assert_cmpint (g_array_index (garray, gint, i), ==, 0);
   99 
  100   g_array_unref (garray);
  101 }
  102 
  103 /* As with array_set_size(), but with a sized array. */
  104 static void
  105 array_set_size_sized (gconstpointer test_data)
  106 {
  107   const ArrayTestData *config = test_data;
  108   GArray *garray;
  109   gsize i;
  110 
  111   garray = g_array_sized_new (config->zero_terminated, config->clear_, sizeof (gint), 10);
  112   g_assert_cmpuint (garray->len, ==, 0);
  113   assert_int_array_zero_terminated (config, garray);
  114 
  115   g_array_set_size (garray, 5);
  116   g_assert_cmpuint (garray->len, ==, 5);
  117   assert_int_array_zero_terminated (config, garray);
  118 
  119   if (config->clear_)
  120     for (i = 0; i < 5; i++)
  121       g_assert_cmpint (g_array_index (garray, gint, i), ==, 0);
  122 
  123   g_array_unref (garray);
  124 }
  125 
  126 /* Check that a zero-terminated array does actually have a zero terminator. */
  127 static void
  128 array_new_zero_terminated (void)
  129 {
  130   GArray *garray;
  131   gchar *out_str = NULL;
  132 
  133   garray = g_array_new (TRUE, FALSE, sizeof (gchar));
  134   g_assert_cmpuint (garray->len, ==, 0);
  135 
  136   g_array_append_vals (garray, "hello", strlen ("hello"));
  137   g_assert_cmpuint (garray->len, ==, 5);
  138   g_assert_cmpstr (garray->data, ==, "hello");
  139 
  140   out_str = g_array_free (garray, FALSE);
  141   g_assert_cmpstr (out_str, ==, "hello");
  142   g_free (out_str);
  143 }
  144 
  145 /* Check g_array_steal() function */
  146 static void
  147 array_steal (void)
  148 {
  149   const guint array_size = 10000;
  150   GArray *garray;
  151   gint *adata;
  152   guint i;
  153   gsize len, past_len;
  154 
  155   garray = g_array_new (FALSE, FALSE, sizeof (gint));
  156   adata = (gint *) g_array_steal (garray, NULL);
  157   g_assert_null (adata);
  158 
  159   adata = (gint *) g_array_steal (garray, &len);
  160   g_assert_null (adata);
  161   g_assert_cmpint (len, ==, 0);
  162 
  163   for (i = 0; i < array_size; i++)
  164     g_array_append_val (garray, i);
  165 
  166   for (i = 0; i < array_size; i++)
  167     g_assert_cmpint (g_array_index (garray, gint, i), ==, i);
  168 
  169 
  170   past_len = garray->len;
  171   adata = (gint *) g_array_steal (garray, &len);
  172   for (i = 0; i < array_size; i++)
  173     g_assert_cmpint (adata[i], ==, i);
  174 
  175   g_assert_cmpint (past_len, ==, len);
  176   g_assert_cmpint (garray->len, ==, 0);
  177 
  178   g_array_append_val (garray, i);
  179 
  180   g_assert_cmpint (adata[0], ==, 0);
  181   g_assert_cmpint (g_array_index (garray, gint, 0), ==, array_size);
  182   g_assert_cmpint (garray->len, ==, 1);
  183 
  184   g_array_remove_index (garray, 0);
  185 
  186   for (i = 0; i < array_size; i++)
  187     g_array_append_val (garray, i);
  188 
  189   g_assert_cmpint (garray->len, ==, array_size);
  190   g_assert_cmpmem (adata, array_size * sizeof (gint),
  191                    garray->data, array_size * sizeof (gint));
  192   g_free (adata);
  193   g_array_free (garray, TRUE);
  194 }
  195 
  196 /* Check that g_array_append_val() works correctly for various #GArray
  197  * configurations. */
  198 static void
  199 array_append_val (gconstpointer test_data)
  200 {
  201   const ArrayTestData *config = test_data;
  202   GArray *garray;
  203   gint i;
  204   gint *segment;
  205 
  206   garray = g_array_new (config->zero_terminated, config->clear_, sizeof (gint));
  207   for (i = 0; i < 10000; i++)
  208     g_array_append_val (garray, i);
  209   assert_int_array_zero_terminated (config, garray);
  210 
  211   for (i = 0; i < 10000; i++)
  212     g_assert_cmpint (g_array_index (garray, gint, i), ==, i);
  213 
  214   segment = (gint*)g_array_free (garray, FALSE);
  215   for (i = 0; i < 10000; i++)
  216     g_assert_cmpint (segment[i], ==, i);
  217   if (config->zero_terminated)
  218     g_assert_cmpint (segment[10000], ==, 0);
  219 
  220   g_free (segment);
  221 }
  222 
  223 /* Check that g_array_prepend_val() works correctly for various #GArray
  224  * configurations. */
  225 static void
  226 array_prepend_val (gconstpointer test_data)
  227 {
  228   const ArrayTestData *config = test_data;
  229   GArray *garray;
  230   gint i;
  231 
  232   garray = g_array_new (config->zero_terminated, config->clear_, sizeof (gint));
  233   for (i = 0; i < 100; i++)
  234     g_array_prepend_val (garray, i);
  235   assert_int_array_zero_terminated (config, garray);
  236 
  237   for (i = 0; i < 100; i++)
  238     g_assert_cmpint (g_array_index (garray, gint, i), ==, (100 - i - 1));
  239 
  240   g_array_free (garray, TRUE);
  241 }
  242 
  243 /* Test that g_array_prepend_vals() works correctly with various array
  244  * configurations. */
  245 static void
  246 array_prepend_vals (gconstpointer test_data)
  247 {
  248   const ArrayTestData *config = test_data;
  249   GArray *garray, *garray_out;
  250   const gint vals[] = { 0, 1, 2, 3, 4 };
  251   const gint expected_vals1[] = { 0, 1 };
  252   const gint expected_vals2[] = { 2, 0, 1 };
  253   const gint expected_vals3[] = { 3, 4, 2, 0, 1 };
  254 
  255   /* Set up an array. */
  256   garray = g_array_new (config->zero_terminated, config->clear_, sizeof (gint));
  257   assert_int_array_zero_terminated (config, garray);
  258 
  259   /* Prepend several values to an empty array. */
  260   garray_out = g_array_prepend_vals (garray, vals, 2);
  261   g_assert_true (garray == garray_out);
  262   assert_int_array_equal (garray, expected_vals1, G_N_ELEMENTS (expected_vals1));
  263   assert_int_array_zero_terminated (config, garray);
  264 
  265   /* Prepend a single value. */
  266   garray_out = g_array_prepend_vals (garray, vals + 2, 1);
  267   g_assert_true (garray == garray_out);
  268   assert_int_array_equal (garray, expected_vals2, G_N_ELEMENTS (expected_vals2));
  269   assert_int_array_zero_terminated (config, garray);
  270 
  271   /* Prepend several values to a non-empty array. */
  272   garray_out = g_array_prepend_vals (garray, vals + 3, 2);
  273   g_assert_true (garray == garray_out);
  274   assert_int_array_equal (garray, expected_vals3, G_N_ELEMENTS (expected_vals3));
  275   assert_int_array_zero_terminated (config, garray);
  276 
  277   /* Prepend no values. */
  278   garray_out = g_array_prepend_vals (garray, vals, 0);
  279   g_assert_true (garray == garray_out);
  280   assert_int_array_equal (garray, expected_vals3, G_N_ELEMENTS (expected_vals3));
  281   assert_int_array_zero_terminated (config, garray);
  282 
  283   /* Prepend no values with %NULL data. */
  284   garray_out = g_array_prepend_vals (garray, NULL, 0);
  285   g_assert_true (garray == garray_out);
  286   assert_int_array_equal (garray, expected_vals3, G_N_ELEMENTS (expected_vals3));
  287   assert_int_array_zero_terminated (config, garray);
  288 
  289   g_array_free (garray, TRUE);
  290 }
  291 
  292 /* Test that g_array_insert_vals() works correctly with various array
  293  * configurations. */
  294 static void
  295 array_insert_vals (gconstpointer test_data)
  296 {
  297   const ArrayTestData *config = test_data;
  298   GArray *garray, *garray_out;
  299   gsize i;
  300   const gint vals[] = { 0, 1, 2, 3, 4, 5, 6, 7 };
  301   const gint expected_vals1[] = { 0, 1 };
  302   const gint expected_vals2[] = { 0, 2, 3, 1 };
  303   const gint expected_vals3[] = { 0, 2, 3, 1, 4 };
  304   const gint expected_vals4[] = { 5, 0, 2, 3, 1, 4 };
  305   const gint expected_vals5[] = { 5, 0, 2, 3, 1, 4, 0, 0, 0, 0, 6, 7 };
  306 
  307   /* Set up an array. */
  308   garray = g_array_new (config->zero_terminated, config->clear_, sizeof (gint));
  309   assert_int_array_zero_terminated (config, garray);
  310 
  311   /* Insert several values at the beginning. */
  312   garray_out = g_array_insert_vals (garray, 0, vals, 2);
  313   g_assert_true (garray == garray_out);
  314   assert_int_array_equal (garray, expected_vals1, G_N_ELEMENTS (expected_vals1));
  315   assert_int_array_zero_terminated (config, garray);
  316 
  317   /* Insert some more part-way through. */
  318   garray_out = g_array_insert_vals (garray, 1, vals + 2, 2);
  319   g_assert_true (garray == garray_out);
  320   assert_int_array_equal (garray, expected_vals2, G_N_ELEMENTS (expected_vals2));
  321   assert_int_array_zero_terminated (config, garray);
  322 
  323   /* And at the end. */
  324   garray_out = g_array_insert_vals (garray, garray->len, vals + 4, 1);
  325   g_assert_true (garray == garray_out);
  326   assert_int_array_equal (garray, expected_vals3, G_N_ELEMENTS (expected_vals3));
  327   assert_int_array_zero_terminated (config, garray);
  328 
  329   /* Then back at the beginning again. */
  330   garray_out = g_array_insert_vals (garray, 0, vals + 5, 1);
  331   g_assert_true (garray == garray_out);
  332   assert_int_array_equal (garray, expected_vals4, G_N_ELEMENTS (expected_vals4));
  333   assert_int_array_zero_terminated (config, garray);
  334 
  335   /* Insert zero elements. */
  336   garray_out = g_array_insert_vals (garray, 0, vals, 0);
  337   g_assert_true (garray == garray_out);
  338   assert_int_array_equal (garray, expected_vals4, G_N_ELEMENTS (expected_vals4));
  339   assert_int_array_zero_terminated (config, garray);
  340 
  341   /* Insert zero elements with a %NULL pointer. */
  342   garray_out = g_array_insert_vals (garray, 0, NULL, 0);
  343   g_assert_true (garray == garray_out);
  344   assert_int_array_equal (garray, expected_vals4, G_N_ELEMENTS (expected_vals4));
  345   assert_int_array_zero_terminated (config, garray);
  346 
  347   /* Insert some elements off the end of the array. The behaviour here depends
  348    * on whether the array clears entries. */
  349   garray_out = g_array_insert_vals (garray, garray->len + 4, vals + 6, 2);
  350   g_assert_true (garray == garray_out);
  351 
  352   g_assert_cmpuint (garray->len, ==, G_N_ELEMENTS (expected_vals5));
  353   for (i = 0; i < G_N_ELEMENTS (expected_vals5); i++)
  354     {
  355       if (config->clear_ || i < 6 || i > 9)
  356         g_assert_cmpint (g_array_index (garray, gint, i), ==, expected_vals5[i]);
  357     }
  358 
  359   assert_int_array_zero_terminated (config, garray);
  360 
  361   g_array_free (garray, TRUE);
  362 }
  363 
  364 /* Check that g_array_remove_index() works correctly for various #GArray
  365  * configurations. */
  366 static void
  367 array_remove_index (gconstpointer test_data)
  368 {
  369   const ArrayTestData *config = test_data;
  370   GArray *garray;
  371   gint i;
  372   gint prev, cur;
  373 
  374   garray = g_array_new (config->zero_terminated, config->clear_, sizeof (gint));
  375   for (i = 0; i < 100; i++)
  376     g_array_append_val (garray, i);
  377   assert_int_array_zero_terminated (config, garray);
  378 
  379   g_assert_cmpint (garray->len, ==, 100);
  380 
  381   g_array_remove_index (garray, 1);
  382   g_array_remove_index (garray, 3);
  383   g_array_remove_index (garray, 21);
  384   g_array_remove_index (garray, 57);
  385 
  386   g_assert_cmpint (garray->len, ==, 96);
  387   assert_int_array_zero_terminated (config, garray);
  388 
  389   prev = -1;
  390   for (i = 0; i < garray->len; i++)
  391     {
  392       cur = g_array_index (garray, gint, i);
  393       g_assert (cur != 1 &&  cur != 4 && cur != 23 && cur != 60);
  394       g_assert_cmpint (prev, <, cur);
  395       prev = cur;
  396     }
  397 
  398   g_array_free (garray, TRUE);
  399 }
  400 
  401 /* Check that g_array_remove_index_fast() works correctly for various #GArray
  402  * configurations. */
  403 static void
  404 array_remove_index_fast (gconstpointer test_data)
  405 {
  406   const ArrayTestData *config = test_data;
  407   GArray *garray;
  408   gint i;
  409   gint prev, cur;
  410 
  411   garray = g_array_new (config->zero_terminated, config->clear_, sizeof (gint));
  412   for (i = 0; i < 100; i++)
  413     g_array_append_val (garray, i);
  414 
  415   g_assert_cmpint (garray->len, ==, 100);
  416   assert_int_array_zero_terminated (config, garray);
  417 
  418   g_array_remove_index_fast (garray, 1);
  419   g_array_remove_index_fast (garray, 3);
  420   g_array_remove_index_fast (garray, 21);
  421   g_array_remove_index_fast (garray, 57);
  422 
  423   g_assert_cmpint (garray->len, ==, 96);
  424   assert_int_array_zero_terminated (config, garray);
  425 
  426   prev = -1;
  427   for (i = 0; i < garray->len; i++)
  428     {
  429       cur = g_array_index (garray, gint, i);
  430       g_assert (cur != 1 &&  cur != 3 && cur != 21 && cur != 57);
  431       if (cur < 96)
  432         {
  433           g_assert_cmpint (prev, <, cur);
  434           prev = cur;
  435         }
  436     }
  437 
  438   g_array_free (garray, TRUE);
  439 }
  440 
  441 /* Check that g_array_remove_range() works correctly for various #GArray
  442  * configurations. */
  443 static void
  444 array_remove_range (gconstpointer test_data)
  445 {
  446   const ArrayTestData *config = test_data;
  447   GArray *garray;
  448   gint i;
  449   gint prev, cur;
  450 
  451   garray = g_array_new (config->zero_terminated, config->clear_, sizeof (gint));
  452   for (i = 0; i < 100; i++)
  453     g_array_append_val (garray, i);
  454 
  455   g_assert_cmpint (garray->len, ==, 100);
  456   assert_int_array_zero_terminated (config, garray);
  457 
  458   g_array_remove_range (garray, 31, 4);
  459 
  460   g_assert_cmpint (garray->len, ==, 96);
  461   assert_int_array_zero_terminated (config, garray);
  462 
  463   prev = -1;
  464   for (i = 0; i < garray->len; i++)
  465     {
  466       cur = g_array_index (garray, gint, i);
  467       g_assert (cur < 31 || cur > 34);
  468       g_assert_cmpint (prev, <, cur);
  469       prev = cur;
  470     }
  471 
  472   /* Ensure the entire array can be cleared, even when empty. */
  473   g_array_remove_range (garray, 0, garray->len);
  474 
  475   g_assert_cmpint (garray->len, ==, 0);
  476   assert_int_array_zero_terminated (config, garray);
  477 
  478   g_array_remove_range (garray, 0, garray->len);
  479 
  480   g_assert_cmpint (garray->len, ==, 0);
  481   assert_int_array_zero_terminated (config, garray);
  482 
  483   g_array_free (garray, TRUE);
  484 }
  485 
  486 static void
  487 array_ref_count (void)
  488 {
  489   GArray *garray;
  490   GArray *garray2;
  491   gint i;
  492 
  493   garray = g_array_new (FALSE, FALSE, sizeof (gint));
  494   g_assert_cmpint (g_array_get_element_size (garray), ==, sizeof (gint));
  495   for (i = 0; i < 100; i++)
  496     g_array_prepend_val (garray, i);
  497 
  498   /* check we can ref, unref and still access the array */
  499   garray2 = g_array_ref (garray);
  500   g_assert (garray == garray2);
  501   g_array_unref (garray2);
  502   for (i = 0; i < 100; i++)
  503     g_assert_cmpint (g_array_index (garray, gint, i), ==, (100 - i - 1));
  504 
  505   /* garray2 should be an empty valid GArray wrapper */
  506   garray2 = g_array_ref (garray);
  507   g_array_free (garray, TRUE);
  508 
  509   g_assert_cmpint (garray2->len, ==, 0);
  510   g_array_unref (garray2);
  511 }
  512 
  513 static int
  514 int_compare (gconstpointer p1, gconstpointer p2)
  515 {
  516   const gint *i1 = p1;
  517   const gint *i2 = p2;
  518 
  519   return *i1 - *i2;
  520 }
  521 
  522 static void
  523 array_copy (gconstpointer test_data)
  524 {
  525   GArray *array, *array_copy;
  526   gsize i;
  527   const ArrayTestData *config = test_data;
  528   const gsize array_size = 100;
  529 
  530   /* Testing degenerated cases */
  531   if (g_test_undefined ())
  532     {
  533       g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
  534                              "*assertion*!= NULL*");
  535       array = g_array_copy (NULL);
  536       g_test_assert_expected_messages ();
  537 
  538       g_assert_null (array);
  539     }
  540 
  541   /* Testing simple copy */
  542   array = g_array_new (config->zero_terminated, config->clear_, sizeof (gint));
  543 
  544   for (i = 0; i < array_size; i++)
  545     g_array_append_val (array, i);
  546 
  547   array_copy = g_array_copy (array);
  548 
  549   /* Check internal data */
  550   for (i = 0; i < array_size; i++)
  551     g_assert_cmpuint (g_array_index (array, gint, i), ==,
  552                       g_array_index (array_copy, gint, i));
  553 
  554   /* Check internal parameters ('zero_terminated' flag) */
  555   if (config->zero_terminated)
  556     {
  557       const gint *data = (const gint *) array_copy->data;
  558       g_assert_cmpint (data[array_copy->len], ==, 0);
  559     }
  560 
  561   /* Check internal parameters ('clear' flag) */
  562   if (config->clear_)
  563     {
  564       guint old_length = array_copy->len;
  565       g_array_set_size (array_copy, old_length + 5);
  566       for (i = old_length; i < old_length + 5; i++)
  567         g_assert_cmpint (g_array_index (array_copy, gint, i), ==, 0);
  568     }
  569 
  570   /* Clean-up */
  571   g_array_unref (array);
  572   g_array_unref (array_copy);
  573 }
  574 
  575 static int
  576 int_compare_data (gconstpointer p1, gconstpointer p2, gpointer data)
  577 {
  578   const gint *i1 = p1;
  579   const gint *i2 = p2;
  580 
  581   return *i1 - *i2;
  582 }
  583 
  584 /* Check that g_array_sort() works correctly for various #GArray
  585  * configurations. */
  586 static void
  587 array_sort (gconstpointer test_data)
  588 {
  589   const ArrayTestData *config = test_data;
  590   GArray *garray;
  591   gint i;
  592   gint prev, cur;
  593 
  594   garray = g_array_new (config->zero_terminated, config->clear_, sizeof (gint));
  595 
  596   /* Sort empty array */
  597   g_array_sort (garray, int_compare);
  598 
  599   for (i = 0; i < 10000; i++)
  600     {
  601       cur = g_random_int_range (0, 10000);
  602       g_array_append_val (garray, cur);
  603     }
  604   assert_int_array_zero_terminated (config, garray);
  605 
  606   g_array_sort (garray, int_compare);
  607   assert_int_array_zero_terminated (config, garray);
  608 
  609   prev = -1;
  610   for (i = 0; i < garray->len; i++)
  611     {
  612       cur = g_array_index (garray, gint, i);
  613       g_assert_cmpint (prev, <=, cur);
  614       prev = cur;
  615     }
  616 
  617   g_array_free (garray, TRUE);
  618 }
  619 
  620 /* Check that g_array_sort_with_data() works correctly for various #GArray
  621  * configurations. */
  622 static void
  623 array_sort_with_data (gconstpointer test_data)
  624 {
  625   const ArrayTestData *config = test_data;
  626   GArray *garray;
  627   gint i;
  628   gint prev, cur;
  629 
  630   garray = g_array_new (config->zero_terminated, config->clear_, sizeof (gint));
  631 
  632   /* Sort empty array */
  633   g_array_sort_with_data (garray, int_compare_data, NULL);
  634 
  635   for (i = 0; i < 10000; i++)
  636     {
  637       cur = g_random_int_range (0, 10000);
  638       g_array_append_val (garray, cur);
  639     }
  640   assert_int_array_zero_terminated (config, garray);
  641 
  642   g_array_sort_with_data (garray, int_compare_data, NULL);
  643   assert_int_array_zero_terminated (config, garray);
  644 
  645   prev = -1;
  646   for (i = 0; i < garray->len; i++)
  647     {
  648       cur = g_array_index (garray, gint, i);
  649       g_assert_cmpint (prev, <=, cur);
  650       prev = cur;
  651     }
  652 
  653   g_array_free (garray, TRUE);
  654 }
  655 
  656 static gint num_clear_func_invocations = 0;
  657 
  658 static void
  659 my_clear_func (gpointer data)
  660 {
  661   num_clear_func_invocations += 1;
  662 }
  663 
  664 static void
  665 array_clear_func (void)
  666 {
  667   GArray *garray;
  668   gint i;
  669   gint cur;
  670 
  671   garray = g_array_new (FALSE, FALSE, sizeof (gint));
  672   g_array_set_clear_func (garray, my_clear_func);
  673 
  674   for (i = 0; i < 10; i++)
  675     {
  676       cur = g_random_int_range (0, 100);
  677       g_array_append_val (garray, cur);
  678     }
  679 
  680   g_array_remove_index (garray, 9);
  681   g_assert_cmpint (num_clear_func_invocations, ==, 1);
  682 
  683   g_array_remove_range (garray, 5, 3);
  684   g_assert_cmpint (num_clear_func_invocations, ==, 4);
  685 
  686   g_array_remove_index_fast (garray, 4);
  687   g_assert_cmpint (num_clear_func_invocations, ==, 5);
  688 
  689   g_array_free (garray, TRUE);
  690   g_assert_cmpint (num_clear_func_invocations, ==, 10);
  691 }
  692 
  693 /* Defining a comparison function for testing g_array_binary_search() */
  694 static gint
  695 cmpint (gconstpointer a, gconstpointer b)
  696 {
  697   const gint *_a = a;
  698   const gint *_b = b;
  699 
  700   return *_a - *_b;
  701 }
  702 
  703 /* Testing g_array_binary_search() function */
  704 static void
  705 test_array_binary_search (void)
  706 {
  707   GArray *garray;
  708   guint i, matched_index;
  709 
  710   if (g_test_undefined ())
  711     {
  712       /* Testing degenerated cases */
  713       garray = g_array_sized_new (FALSE, FALSE, sizeof (guint), 0);
  714       g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
  715                              "*assertion*!= NULL*");
  716       g_assert_false (g_array_binary_search (NULL, &i, cmpint, NULL));
  717       g_test_assert_expected_messages ();
  718 
  719       g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
  720                              "*assertion*!= NULL*");
  721       g_assert_false (g_array_binary_search (garray, &i, NULL, NULL));
  722       g_test_assert_expected_messages ();
  723       g_array_free (garray, TRUE);
  724     }
  725 
  726   /* Testing array of size 0 */
  727   garray = g_array_sized_new (FALSE, FALSE, sizeof (guint), 0);
  728 
  729   i = 1;
  730   g_assert_false (g_array_binary_search (garray, &i, cmpint, NULL));
  731 
  732   g_array_free (garray, TRUE);
  733 
  734   /* Testing array of size 1 */
  735   garray = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
  736   i = 1;
  737   g_array_append_val (garray, i);
  738 
  739   g_assert_true (g_array_binary_search (garray, &i, cmpint, NULL));
  740 
  741   i = 0;
  742   g_assert_false (g_array_binary_search (garray, &i, cmpint, NULL));
  743 
  744   i = 2;
  745   g_assert_false (g_array_binary_search (garray, &i, cmpint, NULL));
  746 
  747   g_array_free (garray, TRUE);
  748 
  749   /* Testing array of size 2 */
  750   garray = g_array_sized_new (FALSE, FALSE, sizeof (guint), 2);
  751   for (i = 1; i < 3; i++)
  752     g_array_append_val (garray, i);
  753 
  754   for (i = 1; i < 3; i++)
  755     g_assert_true (g_array_binary_search (garray, &i, cmpint, NULL));
  756 
  757   i = 0;
  758   g_assert_false (g_array_binary_search (garray, &i, cmpint, NULL));
  759 
  760   i = 4;
  761   g_assert_false (g_array_binary_search (garray, &i, cmpint, NULL));
  762 
  763   g_array_free (garray, TRUE);
  764 
  765   /* Testing array of size 3 */
  766   garray = g_array_sized_new (FALSE, FALSE, sizeof (guint), 3);
  767   for (i = 1; i < 4; i++)
  768     g_array_append_val (garray, i);
  769 
  770   for (i = 1; i < 4; i++)
  771     g_assert_true (g_array_binary_search (garray, &i, cmpint, NULL));
  772 
  773   i = 0;
  774   g_assert_false (g_array_binary_search (garray, &i, cmpint, NULL));
  775 
  776   i = 5;
  777   g_assert_false (g_array_binary_search (garray, &i, cmpint, NULL));
  778 
  779   g_array_free (garray, TRUE);
  780 
  781   /* Testing array of size 10000 */
  782   garray = g_array_sized_new (FALSE, FALSE, sizeof (guint), 10000);
  783 
  784   for (i = 1; i < 10001; i++)
  785     g_array_append_val (garray, i);
  786 
  787   for (i = 1; i < 10001; i++)
  788     g_assert_true (g_array_binary_search (garray, &i, cmpint, NULL));
  789 
  790   for (i = 1; i < 10001; i++)
  791     {
  792       g_assert_true (g_array_binary_search (garray, &i, cmpint, &matched_index));
  793       g_assert_cmpint (i, ==, matched_index + 1);
  794     }
  795 
  796   /* Testing negative result */
  797   i = 0;
  798   g_assert_false (g_array_binary_search (garray, &i, cmpint, NULL));
  799   g_assert_false (g_array_binary_search (garray, &i, cmpint, &matched_index));
  800 
  801   i = 10002;
  802   g_assert_false (g_array_binary_search (garray, &i, cmpint, NULL));
  803   g_assert_false (g_array_binary_search (garray, &i, cmpint, &matched_index));
  804 
  805   g_array_free (garray, TRUE);
  806 
  807   /* Test for a not-found element in the middle of the array. */
  808   garray = g_array_sized_new (FALSE, FALSE, sizeof (guint), 3);
  809   for (i = 1; i < 10; i += 2)
  810     g_array_append_val (garray, i);
  811 
  812   i = 0;
  813   g_assert_false (g_array_binary_search (garray, &i, cmpint, NULL));
  814 
  815   i = 2;
  816   g_assert_false (g_array_binary_search (garray, &i, cmpint, NULL));
  817 
  818   i = 10;
  819   g_assert_false (g_array_binary_search (garray, &i, cmpint, NULL));
  820 
  821   g_array_free (garray, TRUE);
  822 }
  823 
  824 static void
  825 test_array_copy_sized (void)
  826 {
  827   GArray *array1 = NULL, *array2 = NULL, *array3 = NULL;
  828   int val = 5;
  829 
  830   g_test_summary ("Test that copying a newly-allocated sized array works.");
  831 
  832   array1 = g_array_sized_new (FALSE, FALSE, sizeof (int), 1);
  833   array2 = g_array_copy (array1);
  834 
  835   g_assert_cmpuint (array2->len, ==, array1->len);
  836 
  837   g_array_append_val (array1, val);
  838   array3 = g_array_copy (array1);
  839 
  840   g_assert_cmpuint (array3->len, ==, array1->len);
  841   g_assert_cmpuint (g_array_index (array3, int, 0), ==, g_array_index (array1, int, 0));
  842   g_assert_cmpuint (array3->len, ==, 1);
  843   g_assert_cmpuint (g_array_index (array3, int, 0), ==, val);
  844 
  845   g_array_unref (array3);
  846   g_array_unref (array2);
  847   g_array_unref (array1);
  848 }
  849 
  850 /* Check g_ptr_array_steal() function */
  851 static void
  852 pointer_array_steal (void)
  853 {
  854   const guint array_size = 10000;
  855   GPtrArray *gparray;
  856   gpointer *pdata;
  857   guint i;
  858   gsize len, past_len;
  859 
  860   gparray = g_ptr_array_new ();
  861   pdata = g_ptr_array_steal (gparray, NULL);
  862   g_assert_null (pdata);
  863 
  864   pdata = g_ptr_array_steal (gparray, &len);
  865   g_assert_null (pdata);
  866   g_assert_cmpint (len, ==, 0);
  867 
  868   for (i = 0; i < array_size; i++)
  869     g_ptr_array_add (gparray, GINT_TO_POINTER (i));
  870 
  871   past_len = gparray->len;
  872   pdata = g_ptr_array_steal (gparray, &len);
  873   g_assert_cmpint (gparray->len, ==, 0);
  874   g_assert_cmpint (past_len, ==, len);
  875   g_ptr_array_add (gparray, GINT_TO_POINTER (10));
  876 
  877   g_assert_cmpint ((gsize) pdata[0], ==, (gsize) GINT_TO_POINTER (0));
  878   g_assert_cmpint ((gsize) g_ptr_array_index (gparray, 0), ==,
  879                    (gsize) GINT_TO_POINTER (10));
  880   g_assert_cmpint (gparray->len, ==, 1);
  881 
  882   g_ptr_array_remove_index (gparray, 0);
  883 
  884   for (i = 0; i < array_size; i++)
  885     g_ptr_array_add (gparray, GINT_TO_POINTER (i));
  886   g_assert_cmpmem (pdata, array_size * sizeof (gpointer),
  887                    gparray->pdata, array_size * sizeof (gpointer));
  888   g_free (pdata);
  889 
  890   g_ptr_array_free (gparray, TRUE);
  891 }
  892 
  893 static void
  894 pointer_array_add (void)
  895 {
  896   GPtrArray *gparray;
  897   gint i;
  898   gint sum = 0;
  899   gpointer *segment;
  900 
  901   gparray = g_ptr_array_sized_new (1000);
  902 
  903   for (i = 0; i < 10000; i++)
  904     g_ptr_array_add (gparray, GINT_TO_POINTER (i));
  905 
  906   for (i = 0; i < 10000; i++)
  907     g_assert (g_ptr_array_index (gparray, i) == GINT_TO_POINTER (i));
  908   
  909   g_ptr_array_foreach (gparray, sum_up, &sum);
  910   g_assert (sum == 49995000);
  911 
  912   segment = g_ptr_array_free (gparray, FALSE);
  913   for (i = 0; i < 10000; i++)
  914     g_assert (segment[i] == GINT_TO_POINTER (i));
  915   g_free (segment);
  916 }
  917 
  918 static void
  919 pointer_array_insert (void)
  920 {
  921   GPtrArray *gparray;
  922   gint i;
  923   gint sum = 0;
  924   gint index;
  925 
  926   gparray = g_ptr_array_sized_new (1000);
  927 
  928   for (i = 0; i < 10000; i++)
  929     {
  930       index = g_random_int_range (-1, i + 1);
  931       g_ptr_array_insert (gparray, index, GINT_TO_POINTER (i));
  932     }
  933 
  934   g_ptr_array_foreach (gparray, sum_up, &sum);
  935   g_assert (sum == 49995000);
  936 
  937   g_ptr_array_free (gparray, TRUE);
  938 }
  939 
  940 static void
  941 pointer_array_ref_count (void)
  942 {
  943   GPtrArray *gparray;
  944   GPtrArray *gparray2;
  945   gint i;
  946   gint sum = 0;
  947 
  948   gparray = g_ptr_array_new ();
  949   for (i = 0; i < 10000; i++)
  950     g_ptr_array_add (gparray, GINT_TO_POINTER (i));
  951 
  952   /* check we can ref, unref and still access the array */
  953   gparray2 = g_ptr_array_ref (gparray);
  954   g_assert (gparray == gparray2);
  955   g_ptr_array_unref (gparray2);
  956   for (i = 0; i < 10000; i++)
  957     g_assert (g_ptr_array_index (gparray, i) == GINT_TO_POINTER (i));
  958 
  959   g_ptr_array_foreach (gparray, sum_up, &sum);
  960   g_assert (sum == 49995000);
  961 
  962   /* gparray2 should be an empty valid GPtrArray wrapper */
  963   gparray2 = g_ptr_array_ref (gparray);
  964   g_ptr_array_free (gparray, TRUE);
  965 
  966   g_assert_cmpint (gparray2->len, ==, 0);
  967   g_ptr_array_unref (gparray2);
  968 }
  969 
  970 static gint num_free_func_invocations = 0;
  971 
  972 static void
  973 my_free_func (gpointer data)
  974 {
  975   num_free_func_invocations++;
  976   g_free (data);
  977 }
  978 
  979 static void
  980 pointer_array_free_func (void)
  981 {
  982   GPtrArray *gparray;
  983   GPtrArray *gparray2;
  984   gchar **strv;
  985   gchar *s;
  986 
  987   num_free_func_invocations = 0;
  988   gparray = g_ptr_array_new_with_free_func (my_free_func);
  989   g_ptr_array_unref (gparray);
  990   g_assert_cmpint (num_free_func_invocations, ==, 0);
  991 
  992   gparray = g_ptr_array_new_with_free_func (my_free_func);
  993   g_ptr_array_free (gparray, TRUE);
  994   g_assert_cmpint (num_free_func_invocations, ==, 0);
  995 
  996   num_free_func_invocations = 0;
  997   gparray = g_ptr_array_new_with_free_func (my_free_func);
  998   g_ptr_array_add (gparray, g_strdup ("foo"));
  999   g_ptr_array_add (gparray, g_strdup ("bar"));
 1000   g_ptr_array_add (gparray, g_strdup ("baz"));
 1001   g_ptr_array_remove_index (gparray, 0);
 1002   g_assert_cmpint (num_free_func_invocations, ==, 1);
 1003   g_ptr_array_remove_index_fast (gparray, 1);
 1004   g_assert_cmpint (num_free_func_invocations, ==, 2);
 1005   s = g_strdup ("frob");
 1006   g_ptr_array_add (gparray, s);
 1007   g_assert (g_ptr_array_remove (gparray, s));
 1008   g_assert (!g_ptr_array_remove (gparray, "nuun"));
 1009   g_assert (!g_ptr_array_remove_fast (gparray, "mlo"));
 1010   g_assert_cmpint (num_free_func_invocations, ==, 3);
 1011   s = g_strdup ("frob");
 1012   g_ptr_array_add (gparray, s);
 1013   g_ptr_array_set_size (gparray, 1);
 1014   g_assert_cmpint (num_free_func_invocations, ==, 4);
 1015   g_ptr_array_ref (gparray);
 1016   g_ptr_array_unref (gparray);
 1017   g_assert_cmpint (num_free_func_invocations, ==, 4);
 1018   g_ptr_array_unref (gparray);
 1019   g_assert_cmpint (num_free_func_invocations, ==, 5);
 1020 
 1021   num_free_func_invocations = 0;
 1022   gparray = g_ptr_array_new_full (10, my_free_func);
 1023   g_ptr_array_add (gparray, g_strdup ("foo"));
 1024   g_ptr_array_add (gparray, g_strdup ("bar"));
 1025   g_ptr_array_add (gparray, g_strdup ("baz"));
 1026   g_ptr_array_set_size (gparray, 20);
 1027   g_ptr_array_add (gparray, NULL);
 1028   gparray2 = g_ptr_array_ref (gparray);
 1029   strv = (gchar **) g_ptr_array_free (gparray, FALSE);
 1030   g_assert_cmpint (num_free_func_invocations, ==, 0);
 1031   g_strfreev (strv);
 1032   g_ptr_array_unref (gparray2);
 1033   g_assert_cmpint (num_free_func_invocations, ==, 0);
 1034 
 1035   num_free_func_invocations = 0;
 1036   gparray = g_ptr_array_new_with_free_func (my_free_func);
 1037   g_ptr_array_add (gparray, g_strdup ("foo"));
 1038   g_ptr_array_add (gparray, g_strdup ("bar"));
 1039   g_ptr_array_add (gparray, g_strdup ("baz"));
 1040   g_ptr_array_remove_range (gparray, 1, 1);
 1041   g_ptr_array_unref (gparray);
 1042   g_assert_cmpint (num_free_func_invocations, ==, 3);
 1043 
 1044   num_free_func_invocations = 0;
 1045   gparray = g_ptr_array_new_with_free_func (my_free_func);
 1046   g_ptr_array_add (gparray, g_strdup ("foo"));
 1047   g_ptr_array_add (gparray, g_strdup ("bar"));
 1048   g_ptr_array_add (gparray, g_strdup ("baz"));
 1049   g_ptr_array_free (gparray, TRUE);
 1050   g_assert_cmpint (num_free_func_invocations, ==, 3);
 1051 
 1052   num_free_func_invocations = 0;
 1053   gparray = g_ptr_array_new_with_free_func (my_free_func);
 1054   g_ptr_array_add (gparray, "foo");
 1055   g_ptr_array_add (gparray, "bar");
 1056   g_ptr_array_add (gparray, "baz");
 1057   g_ptr_array_set_free_func (gparray, NULL);
 1058   g_ptr_array_free (gparray, TRUE);
 1059   g_assert_cmpint (num_free_func_invocations, ==, 0);
 1060 }
 1061 
 1062 static gpointer
 1063 ptr_array_copy_func (gconstpointer src, gpointer userdata)
 1064 {
 1065   gsize *dst = g_malloc (sizeof (gsize));
 1066   *dst = *((gsize *) src);
 1067   return dst;
 1068 }
 1069 
 1070 /* Test the g_ptr_array_copy() function */
 1071 static void
 1072 pointer_array_copy (void)
 1073 {
 1074   GPtrArray *ptr_array, *ptr_array2;
 1075   gsize i;
 1076   const gsize array_size = 100;
 1077   gsize *array_test = g_malloc (array_size * sizeof (gsize));
 1078 
 1079   g_test_summary ("Check all normal behaviour of stealing elements from one "
 1080                   "array to append to another, covering different array sizes "
 1081                   "and element copy functions");
 1082 
 1083   if (g_test_undefined ())
 1084     {
 1085       /* Testing degenerated cases */
 1086       g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
 1087                              "*assertion*!= NULL*");
 1088       ptr_array = g_ptr_array_copy (NULL, NULL, NULL);
 1089       g_test_assert_expected_messages ();
 1090       g_assert_cmpuint ((gsize) ptr_array, ==, (gsize) NULL);
 1091     }
 1092 
 1093   /* Initializing array_test */
 1094   for (i = 0; i < array_size; i++)
 1095     array_test[i] = i;
 1096 
 1097   /* Test copy an empty array */
 1098   ptr_array = g_ptr_array_sized_new (0);
 1099   ptr_array2 = g_ptr_array_copy (ptr_array, NULL, NULL);
 1100 
 1101   g_assert_cmpuint (ptr_array2->len, ==, ptr_array->len);
 1102 
 1103   g_ptr_array_unref (ptr_array);
 1104   g_ptr_array_unref (ptr_array2);
 1105 
 1106   /* Test simple copy */
 1107   ptr_array = g_ptr_array_sized_new (array_size);
 1108 
 1109   for (i = 0; i < array_size; i++)
 1110     g_ptr_array_add (ptr_array, &array_test[i]);
 1111 
 1112   ptr_array2 = g_ptr_array_copy (ptr_array, NULL, NULL);
 1113 
 1114   g_assert_cmpuint (ptr_array2->len, ==, ptr_array->len);
 1115   for (i = 0; i < array_size; i++)
 1116     g_assert_cmpuint (*((gsize *) g_ptr_array_index (ptr_array2, i)), ==, i);
 1117 
 1118   for (i = 0; i < array_size; i++)
 1119     g_assert_cmpuint ((gsize) g_ptr_array_index (ptr_array, i), ==,
 1120                       (gsize) g_ptr_array_index (ptr_array2, i));
 1121 
 1122   g_ptr_array_free (ptr_array2, TRUE);
 1123 
 1124   /* Test copy through GCopyFunc */
 1125   ptr_array2 = g_ptr_array_copy (ptr_array, ptr_array_copy_func, NULL);
 1126   g_ptr_array_set_free_func (ptr_array2, g_free);
 1127 
 1128   g_assert_cmpuint (ptr_array2->len, ==, ptr_array->len);
 1129   for (i = 0; i < array_size; i++)
 1130     g_assert_cmpuint (*((gsize *) g_ptr_array_index (ptr_array2, i)), ==, i);
 1131 
 1132   for (i = 0; i < array_size; i++)
 1133     g_assert_cmpuint ((gsize) g_ptr_array_index (ptr_array, i), !=,
 1134                       (gsize) g_ptr_array_index (ptr_array2, i));
 1135 
 1136   g_ptr_array_free (ptr_array2, TRUE);
 1137 
 1138   /* Final cleanup */
 1139   g_ptr_array_free (ptr_array, TRUE);
 1140   g_free (array_test);
 1141 }
 1142 
 1143 /* Test the g_ptr_array_extend() function */
 1144 static void
 1145 pointer_array_extend (void)
 1146 {
 1147   GPtrArray *ptr_array, *ptr_array2;
 1148   gsize i;
 1149   const gsize array_size = 100;
 1150   gsize *array_test = g_malloc (array_size * sizeof (gsize));
 1151 
 1152   if (g_test_undefined ())
 1153     {
 1154       /* Testing degenerated cases */
 1155       ptr_array = g_ptr_array_sized_new (0);
 1156       g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
 1157                              "*assertion*!= NULL*");
 1158       g_ptr_array_extend (NULL, ptr_array, NULL, NULL);
 1159       g_test_assert_expected_messages ();
 1160 
 1161       g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
 1162                              "*assertion*!= NULL*");
 1163       g_ptr_array_extend (ptr_array, NULL, NULL, NULL);
 1164       g_test_assert_expected_messages ();
 1165 
 1166       g_ptr_array_unref (ptr_array);
 1167     }
 1168 
 1169   /* Initializing array_test */
 1170   for (i = 0; i < array_size; i++)
 1171     array_test[i] = i;
 1172 
 1173   /* Testing extend with array of size zero */
 1174   ptr_array = g_ptr_array_sized_new (0);
 1175   ptr_array2 = g_ptr_array_sized_new (0);
 1176 
 1177   g_ptr_array_extend (ptr_array, ptr_array2, NULL, NULL);
 1178 
 1179   g_assert_cmpuint (ptr_array->len, ==, 0);
 1180   g_assert_cmpuint (ptr_array2->len, ==, 0);
 1181 
 1182   g_ptr_array_unref (ptr_array);
 1183   g_ptr_array_unref (ptr_array2);
 1184 
 1185   /* Testing extend an array of size zero */
 1186   ptr_array = g_ptr_array_sized_new (array_size);
 1187   ptr_array2 = g_ptr_array_sized_new (0);
 1188 
 1189   for (i = 0; i < array_size; i++)
 1190     {
 1191       g_ptr_array_add (ptr_array, &array_test[i]);
 1192     }
 1193 
 1194   g_ptr_array_extend (ptr_array, ptr_array2, NULL, NULL);
 1195 
 1196   for (i = 0; i < array_size; i++)
 1197     g_assert_cmpuint (*((gsize *) g_ptr_array_index (ptr_array, i)), ==, i);
 1198 
 1199   g_ptr_array_unref (ptr_array);
 1200   g_ptr_array_unref (ptr_array2);
 1201 
 1202   /* Testing extend an array of size zero */
 1203   ptr_array = g_ptr_array_sized_new (0);
 1204   ptr_array2 = g_ptr_array_sized_new (array_size);
 1205 
 1206   for (i = 0; i < array_size; i++)
 1207     {
 1208       g_ptr_array_add (ptr_array2, &array_test[i]);
 1209     }
 1210 
 1211   g_ptr_array_extend (ptr_array, ptr_array2, NULL, NULL);
 1212 
 1213   for (i = 0; i < array_size; i++)
 1214     g_assert_cmpuint (*((gsize *) g_ptr_array_index (ptr_array, i)), ==, i);
 1215 
 1216   g_ptr_array_unref (ptr_array);
 1217   g_ptr_array_unref (ptr_array2);
 1218 
 1219   /* Testing simple extend */
 1220   ptr_array = g_ptr_array_sized_new (array_size / 2);
 1221   ptr_array2 = g_ptr_array_sized_new (array_size / 2);
 1222 
 1223   for (i = 0; i < array_size / 2; i++)
 1224     {
 1225       g_ptr_array_add (ptr_array, &array_test[i]);
 1226       g_ptr_array_add (ptr_array2, &array_test[i + (array_size / 2)]);
 1227     }
 1228 
 1229   g_ptr_array_extend (ptr_array, ptr_array2, NULL, NULL);
 1230 
 1231   for (i = 0; i < array_size; i++)
 1232     g_assert_cmpuint (*((gsize *) g_ptr_array_index (ptr_array, i)), ==, i);
 1233 
 1234   g_ptr_array_unref (ptr_array);
 1235   g_ptr_array_unref (ptr_array2);
 1236 
 1237   /* Testing extend with GCopyFunc */
 1238   ptr_array = g_ptr_array_sized_new (array_size / 2);
 1239   ptr_array2 = g_ptr_array_sized_new (array_size / 2);
 1240 
 1241   for (i = 0; i < array_size / 2; i++)
 1242     {
 1243       g_ptr_array_add (ptr_array, &array_test[i]);
 1244       g_ptr_array_add (ptr_array2, &array_test[i + (array_size / 2)]);
 1245     }
 1246 
 1247   g_ptr_array_extend (ptr_array, ptr_array2, ptr_array_copy_func, NULL);
 1248 
 1249   for (i = 0; i < array_size; i++)
 1250     g_assert_cmpuint (*((gsize *) g_ptr_array_index (ptr_array, i)), ==, i);
 1251 
 1252   /* Clean-up memory */
 1253   for (i = array_size / 2; i < array_size; i++)
 1254     g_free (g_ptr_array_index (ptr_array, i));
 1255 
 1256   g_ptr_array_unref (ptr_array);
 1257   g_ptr_array_unref (ptr_array2);
 1258   g_free (array_test);
 1259 }
 1260 
 1261 /* Test the g_ptr_array_extend_and_steal() function */
 1262 static void
 1263 pointer_array_extend_and_steal (void)
 1264 {
 1265   GPtrArray *ptr_array, *ptr_array2, *ptr_array3;
 1266   gsize i;
 1267   const gsize array_size = 100;
 1268   gsize *array_test = g_malloc (array_size * sizeof (gsize));
 1269 
 1270   /* Initializing array_test */
 1271   for (i = 0; i < array_size; i++)
 1272     array_test[i] = i;
 1273 
 1274   /* Testing simple extend_and_steal() */
 1275   ptr_array = g_ptr_array_sized_new (array_size / 2);
 1276   ptr_array2 = g_ptr_array_sized_new (array_size / 2);
 1277 
 1278   for (i = 0; i < array_size / 2; i++)
 1279     {
 1280       g_ptr_array_add (ptr_array, &array_test[i]);
 1281       g_ptr_array_add (ptr_array2, &array_test[i + (array_size / 2)]);
 1282     }
 1283 
 1284   g_ptr_array_extend_and_steal (ptr_array, ptr_array2);
 1285 
 1286   for (i = 0; i < array_size; i++)
 1287     g_assert_cmpuint (*((gsize *) g_ptr_array_index (ptr_array, i)), ==, i);
 1288 
 1289   g_ptr_array_free (ptr_array, TRUE);
 1290 
 1291   /* Testing extend_and_steal() with a pending reference to stolen array */
 1292   ptr_array = g_ptr_array_sized_new (array_size / 2);
 1293   ptr_array2 = g_ptr_array_sized_new (array_size / 2);
 1294 
 1295   for (i = 0; i < array_size / 2; i++)
 1296     {
 1297       g_ptr_array_add (ptr_array, &array_test[i]);
 1298       g_ptr_array_add (ptr_array2, &array_test[i + (array_size / 2)]);
 1299     }
 1300 
 1301   ptr_array3 = g_ptr_array_ref (ptr_array2);
 1302 
 1303   g_ptr_array_extend_and_steal (ptr_array, ptr_array2);
 1304 
 1305   for (i = 0; i < array_size; i++)
 1306     g_assert_cmpuint (*((gsize *) g_ptr_array_index (ptr_array, i)), ==, i);
 1307 
 1308   g_assert_cmpuint (ptr_array3->len, ==, 0);
 1309   g_assert_null (ptr_array3->pdata);
 1310 
 1311   g_ptr_array_add (ptr_array2, NULL);
 1312 
 1313   g_ptr_array_free (ptr_array, TRUE);
 1314   g_ptr_array_free (ptr_array3, TRUE);
 1315 
 1316   /* Final memory clean-up */
 1317   g_free (array_test);
 1318 }
 1319 
 1320 static gint
 1321 ptr_compare (gconstpointer p1, gconstpointer p2)
 1322 {
 1323   gpointer i1 = *(gpointer*)p1;
 1324   gpointer i2 = *(gpointer*)p2;
 1325 
 1326   return GPOINTER_TO_INT (i1) - GPOINTER_TO_INT (i2);
 1327 }
 1328 
 1329 static gint
 1330 ptr_compare_data (gconstpointer p1, gconstpointer p2, gpointer data)
 1331 {
 1332   gpointer i1 = *(gpointer*)p1;
 1333   gpointer i2 = *(gpointer*)p2;
 1334 
 1335   return GPOINTER_TO_INT (i1) - GPOINTER_TO_INT (i2);
 1336 }
 1337 
 1338 static void
 1339 pointer_array_sort (void)
 1340 {
 1341   GPtrArray *gparray;
 1342   gint i;
 1343   gint val;
 1344   gint prev, cur;
 1345 
 1346   gparray = g_ptr_array_new ();
 1347 
 1348   /* Sort empty array */
 1349   g_ptr_array_sort (gparray, ptr_compare);
 1350 
 1351   for (i = 0; i < 10000; i++)
 1352     {
 1353       val = g_random_int_range (0, 10000);
 1354       g_ptr_array_add (gparray, GINT_TO_POINTER (val));
 1355     }
 1356 
 1357   g_ptr_array_sort (gparray, ptr_compare);
 1358 
 1359   prev = -1;
 1360   for (i = 0; i < 10000; i++)
 1361     {
 1362       cur = GPOINTER_TO_INT (g_ptr_array_index (gparray, i));
 1363       g_assert_cmpint (prev, <=, cur);
 1364       prev = cur;
 1365     }
 1366 
 1367   g_ptr_array_free (gparray, TRUE);
 1368 }
 1369 
 1370 /* Please keep pointer_array_sort_example() in sync with the doc-comment
 1371  * of g_ptr_array_sort() */
 1372 
 1373 typedef struct
 1374 {
 1375   gchar *name;
 1376   gint size;
 1377 } FileListEntry;
 1378 
 1379 static void
 1380 file_list_entry_free (gpointer p)
 1381 {
 1382   FileListEntry *entry = p;
 1383 
 1384   g_free (entry->name);
 1385   g_free (entry);
 1386 }
 1387 
 1388 static gint
 1389 sort_filelist (gconstpointer a, gconstpointer b)
 1390 {
 1391    const FileListEntry *entry1 = *((FileListEntry **) a);
 1392    const FileListEntry *entry2 = *((FileListEntry **) b);
 1393 
 1394    return g_ascii_strcasecmp (entry1->name, entry2->name);
 1395 }
 1396 
 1397 static void
 1398 pointer_array_sort_example (void)
 1399 {
 1400   GPtrArray *file_list = NULL;
 1401   FileListEntry *entry;
 1402 
 1403   g_test_summary ("Check that the doc-comment for g_ptr_array_sort() is correct");
 1404 
 1405   file_list = g_ptr_array_new_with_free_func (file_list_entry_free);
 1406 
 1407   entry = g_new0 (FileListEntry, 1);
 1408   entry->name = g_strdup ("README");
 1409   entry->size = 42;
 1410   g_ptr_array_add (file_list, g_steal_pointer (&entry));
 1411 
 1412   entry = g_new0 (FileListEntry, 1);
 1413   entry->name = g_strdup ("empty");
 1414   entry->size = 0;
 1415   g_ptr_array_add (file_list, g_steal_pointer (&entry));
 1416 
 1417   entry = g_new0 (FileListEntry, 1);
 1418   entry->name = g_strdup ("aardvark");
 1419   entry->size = 23;
 1420   g_ptr_array_add (file_list, g_steal_pointer (&entry));
 1421 
 1422   g_ptr_array_sort (file_list, sort_filelist);
 1423 
 1424   g_assert_cmpuint (file_list->len, ==, 3);
 1425   entry = g_ptr_array_index (file_list, 0);
 1426   g_assert_cmpstr (entry->name, ==, "aardvark");
 1427   entry = g_ptr_array_index (file_list, 1);
 1428   g_assert_cmpstr (entry->name, ==, "empty");
 1429   entry = g_ptr_array_index (file_list, 2);
 1430   g_assert_cmpstr (entry->name, ==, "README");
 1431 
 1432   g_ptr_array_unref (file_list);
 1433 }
 1434 
 1435 /* Please keep pointer_array_sort_with_data_example() in sync with the
 1436  * doc-comment of g_ptr_array_sort_with_data() */
 1437 
 1438 typedef enum { SORT_NAME, SORT_SIZE } SortMode;
 1439 
 1440 static gint
 1441 sort_filelist_how (gconstpointer a, gconstpointer b, gpointer user_data)
 1442 {
 1443   gint order;
 1444   const SortMode sort_mode = GPOINTER_TO_INT (user_data);
 1445   const FileListEntry *entry1 = *((FileListEntry **) a);
 1446   const FileListEntry *entry2 = *((FileListEntry **) b);
 1447 
 1448   switch (sort_mode)
 1449     {
 1450     case SORT_NAME:
 1451       order = g_ascii_strcasecmp (entry1->name, entry2->name);
 1452       break;
 1453     case SORT_SIZE:
 1454       order = entry1->size - entry2->size;
 1455       break;
 1456     default:
 1457       order = 0;
 1458       break;
 1459     }
 1460   return order;
 1461 }
 1462 
 1463 static void
 1464 pointer_array_sort_with_data_example (void)
 1465 {
 1466   GPtrArray *file_list = NULL;
 1467   FileListEntry *entry;
 1468   SortMode sort_mode;
 1469 
 1470   g_test_summary ("Check that the doc-comment for g_ptr_array_sort_with_data() is correct");
 1471 
 1472   file_list = g_ptr_array_new_with_free_func (file_list_entry_free);
 1473 
 1474   entry = g_new0 (FileListEntry, 1);
 1475   entry->name = g_strdup ("README");
 1476   entry->size = 42;
 1477   g_ptr_array_add (file_list, g_steal_pointer (&entry));
 1478 
 1479   entry = g_new0 (FileListEntry, 1);
 1480   entry->name = g_strdup ("empty");
 1481   entry->size = 0;
 1482   g_ptr_array_add (file_list, g_steal_pointer (&entry));
 1483 
 1484   entry = g_new0 (FileListEntry, 1);
 1485   entry->name = g_strdup ("aardvark");
 1486   entry->size = 23;
 1487   g_ptr_array_add (file_list, g_steal_pointer (&entry));
 1488 
 1489   sort_mode = SORT_NAME;
 1490   g_ptr_array_sort_with_data (file_list, sort_filelist_how, GINT_TO_POINTER (sort_mode));
 1491 
 1492   g_assert_cmpuint (file_list->len, ==, 3);
 1493   entry = g_ptr_array_index (file_list, 0);
 1494   g_assert_cmpstr (entry->name, ==, "aardvark");
 1495   entry = g_ptr_array_index (file_list, 1);
 1496   g_assert_cmpstr (entry->name, ==, "empty");
 1497   entry = g_ptr_array_index (file_list, 2);
 1498   g_assert_cmpstr (entry->name, ==, "README");
 1499 
 1500   sort_mode = SORT_SIZE;
 1501   g_ptr_array_sort_with_data (file_list, sort_filelist_how, GINT_TO_POINTER (sort_mode));
 1502 
 1503   g_assert_cmpuint (file_list->len, ==, 3);
 1504   entry = g_ptr_array_index (file_list, 0);
 1505   g_assert_cmpstr (entry->name, ==, "empty");
 1506   entry = g_ptr_array_index (file_list, 1);
 1507   g_assert_cmpstr (entry->name, ==, "aardvark");
 1508   entry = g_ptr_array_index (file_list, 2);
 1509   g_assert_cmpstr (entry->name, ==, "README");
 1510 
 1511   g_ptr_array_unref (file_list);
 1512 }
 1513 
 1514 static void
 1515 pointer_array_sort_with_data (void)
 1516 {
 1517   GPtrArray *gparray;
 1518   gint i;
 1519   gint prev, cur;
 1520 
 1521   gparray = g_ptr_array_new ();
 1522 
 1523   /* Sort empty array */
 1524   g_ptr_array_sort_with_data (gparray, ptr_compare_data, NULL);
 1525 
 1526   for (i = 0; i < 10000; i++)
 1527     g_ptr_array_add (gparray, GINT_TO_POINTER (g_random_int_range (0, 10000)));
 1528 
 1529   g_ptr_array_sort_with_data (gparray, ptr_compare_data, NULL);
 1530 
 1531   prev = -1;
 1532   for (i = 0; i < 10000; i++)
 1533     {
 1534       cur = GPOINTER_TO_INT (g_ptr_array_index (gparray, i));
 1535       g_assert_cmpint (prev, <=, cur);
 1536       prev = cur;
 1537     }
 1538 
 1539   g_ptr_array_free (gparray, TRUE);
 1540 }
 1541 
 1542 static void
 1543 pointer_array_find_empty (void)
 1544 {
 1545   GPtrArray *array;
 1546   guint idx;
 1547 
 1548   array = g_ptr_array_new ();
 1549 
 1550   g_assert_false (g_ptr_array_find (array, "some-value", NULL));  /* NULL index */
 1551   g_assert_false (g_ptr_array_find (array, "some-value", &idx));  /* non-NULL index */
 1552   g_assert_false (g_ptr_array_find_with_equal_func (array, "some-value", g_str_equal, NULL));  /* NULL index */
 1553   g_assert_false (g_ptr_array_find_with_equal_func (array, "some-value", g_str_equal, &idx));  /* non-NULL index */
 1554 
 1555   g_ptr_array_free (array, TRUE);
 1556 }
 1557 
 1558 static void
 1559 pointer_array_find_non_empty (void)
 1560 {
 1561   GPtrArray *array;
 1562   guint idx;
 1563   const gchar *str_pointer = "static-string";
 1564 
 1565   array = g_ptr_array_new ();
 1566 
 1567   g_ptr_array_add (array, "some");
 1568   g_ptr_array_add (array, "random");
 1569   g_ptr_array_add (array, "values");
 1570   g_ptr_array_add (array, "some");
 1571   g_ptr_array_add (array, "duplicated");
 1572   g_ptr_array_add (array, (gpointer) str_pointer);
 1573 
 1574   g_assert_true (g_ptr_array_find_with_equal_func (array, "random", g_str_equal, NULL));  /* NULL index */
 1575   g_assert_true (g_ptr_array_find_with_equal_func (array, "random", g_str_equal, &idx));  /* non-NULL index */
 1576   g_assert_cmpuint (idx, ==, 1);
 1577 
 1578   g_assert_true (g_ptr_array_find_with_equal_func (array, "some", g_str_equal, &idx));  /* duplicate element */
 1579   g_assert_cmpuint (idx, ==, 0);
 1580 
 1581   g_assert_false (g_ptr_array_find_with_equal_func (array, "nope", g_str_equal, NULL));
 1582 
 1583   g_assert_true (g_ptr_array_find_with_equal_func (array, str_pointer, g_str_equal, &idx));
 1584   g_assert_cmpuint (idx, ==, 5);
 1585   idx = G_MAXUINT;
 1586   g_assert_true (g_ptr_array_find_with_equal_func (array, str_pointer, NULL, &idx));  /* NULL equal func */
 1587   g_assert_cmpuint (idx, ==, 5);
 1588   idx = G_MAXUINT;
 1589   g_assert_true (g_ptr_array_find (array, str_pointer, &idx));  /* NULL equal func */
 1590   g_assert_cmpuint (idx, ==, 5);
 1591 
 1592   g_ptr_array_free (array, TRUE);
 1593 }
 1594 
 1595 static void
 1596 steal_destroy_notify (gpointer data)
 1597 {
 1598   guint *counter = data;
 1599   *counter = *counter + 1;
 1600 }
 1601 
 1602 /* Test that g_ptr_array_steal_index() and g_ptr_array_steal_index_fast() can
 1603  * remove elements from a pointer array without the #GDestroyNotify being called. */
 1604 static void
 1605 pointer_array_steal_index (void)
 1606 {
 1607   guint i1 = 0, i2 = 0, i3 = 0, i4 = 0;
 1608   gpointer out1, out2;
 1609   GPtrArray *array = g_ptr_array_new_with_free_func (steal_destroy_notify);
 1610 
 1611   g_ptr_array_add (array, &i1);
 1612   g_ptr_array_add (array, &i2);
 1613   g_ptr_array_add (array, &i3);
 1614   g_ptr_array_add (array, &i4);
 1615 
 1616   g_assert_cmpuint (array->len, ==, 4);
 1617 
 1618   /* Remove a single element. */
 1619   out1 = g_ptr_array_steal_index (array, 0);
 1620   g_assert_true (out1 == &i1);
 1621   g_assert_cmpuint (i1, ==, 0);  /* should not have been destroyed */
 1622 
 1623   /* Following elements should have been moved down. */
 1624   g_assert_cmpuint (array->len, ==, 3);
 1625   g_assert_true (g_ptr_array_index (array, 0) == &i2);
 1626   g_assert_true (g_ptr_array_index (array, 1) == &i3);
 1627   g_assert_true (g_ptr_array_index (array, 2) == &i4);
 1628 
 1629   /* Remove another element, quickly. */
 1630   out2 = g_ptr_array_steal_index_fast (array, 0);
 1631   g_assert_true (out2 == &i2);
 1632   g_assert_cmpuint (i2, ==, 0);  /* should not have been destroyed */
 1633 
 1634   /* Last element should have been swapped in place. */
 1635   g_assert_cmpuint (array->len, ==, 2);
 1636   g_assert_true (g_ptr_array_index (array, 0) == &i4);
 1637   g_assert_true (g_ptr_array_index (array, 1) == &i3);
 1638 
 1639   /* Check that destroying the pointer array doesn’t affect the stolen elements. */
 1640   g_ptr_array_unref (array);
 1641 
 1642   g_assert_cmpuint (i1, ==, 0);
 1643   g_assert_cmpuint (i2, ==, 0);
 1644   g_assert_cmpuint (i3, ==, 1);
 1645   g_assert_cmpuint (i4, ==, 1);
 1646 }
 1647 
 1648 static void
 1649 byte_array_steal (void)
 1650 {
 1651   const guint array_size = 10000;
 1652   GByteArray *gbarray;
 1653   guint8 *bdata;
 1654   guint i;
 1655   gsize len, past_len;
 1656 
 1657   gbarray = g_byte_array_new ();
 1658   bdata = g_byte_array_steal (gbarray, NULL);
 1659   g_assert_cmpint ((gsize) bdata, ==, (gsize) gbarray->data);
 1660   g_free (bdata);
 1661 
 1662   for (i = 0; i < array_size; i++)
 1663     g_byte_array_append (gbarray, (guint8 *) "abcd", 4);
 1664 
 1665   past_len = gbarray->len;
 1666   bdata = g_byte_array_steal (gbarray, &len);
 1667 
 1668   g_assert_cmpint (len, ==, past_len);
 1669   g_assert_cmpint (gbarray->len, ==, 0);
 1670 
 1671   g_byte_array_append (gbarray, (guint8 *) "@", 1);
 1672 
 1673   g_assert_cmpint (bdata[0], ==, 'a');
 1674   g_assert_cmpint (gbarray->data[0], ==, '@');
 1675   g_assert_cmpint (gbarray->len, ==, 1);
 1676 
 1677   g_byte_array_remove_index (gbarray, 0);
 1678 
 1679   g_free (bdata);
 1680   g_byte_array_free (gbarray, TRUE);
 1681 }
 1682 
 1683 static void
 1684 byte_array_append (void)
 1685 {
 1686   GByteArray *gbarray;
 1687   gint i;
 1688   guint8 *segment;
 1689 
 1690   gbarray = g_byte_array_sized_new (1000);
 1691   for (i = 0; i < 10000; i++)
 1692     g_byte_array_append (gbarray, (guint8*) "abcd", 4);
 1693 
 1694   for (i = 0; i < 10000; i++)
 1695     {
 1696       g_assert (gbarray->data[4*i] == 'a');
 1697       g_assert (gbarray->data[4*i+1] == 'b');
 1698       g_assert (gbarray->data[4*i+2] == 'c');
 1699       g_assert (gbarray->data[4*i+3] == 'd');
 1700     }
 1701 
 1702   segment = g_byte_array_free (gbarray, FALSE);
 1703 
 1704   for (i = 0; i < 10000; i++)
 1705     {
 1706       g_assert (segment[4*i] == 'a');
 1707       g_assert (segment[4*i+1] == 'b');
 1708       g_assert (segment[4*i+2] == 'c');
 1709       g_assert (segment[4*i+3] == 'd');
 1710     }
 1711 
 1712   g_free (segment);
 1713 }
 1714 
 1715 static void
 1716 byte_array_prepend (void)
 1717 {
 1718   GByteArray *gbarray;
 1719   gint i;
 1720 
 1721   gbarray = g_byte_array_new ();
 1722   g_byte_array_set_size (gbarray, 1000);
 1723 
 1724   for (i = 0; i < 10000; i++)
 1725     g_byte_array_prepend (gbarray, (guint8*) "abcd", 4);
 1726 
 1727   for (i = 0; i < 10000; i++)
 1728     {
 1729       g_assert (gbarray->data[4*i] == 'a');
 1730       g_assert (gbarray->data[4*i+1] == 'b');
 1731       g_assert (gbarray->data[4*i+2] == 'c');
 1732       g_assert (gbarray->data[4*i+3] == 'd');
 1733     }
 1734 
 1735   g_byte_array_free (gbarray, TRUE);
 1736 }
 1737 
 1738 static void
 1739 byte_array_ref_count (void)
 1740 {
 1741   GByteArray *gbarray;
 1742   GByteArray *gbarray2;
 1743   gint i;
 1744 
 1745   gbarray = g_byte_array_new ();
 1746   for (i = 0; i < 10000; i++)
 1747     g_byte_array_append (gbarray, (guint8*) "abcd", 4);
 1748 
 1749   gbarray2 = g_byte_array_ref (gbarray);
 1750   g_assert (gbarray2 == gbarray);
 1751   g_byte_array_unref (gbarray2);
 1752   for (i = 0; i < 10000; i++)
 1753     {
 1754       g_assert (gbarray->data[4*i] == 'a');
 1755       g_assert (gbarray->data[4*i+1] == 'b');
 1756       g_assert (gbarray->data[4*i+2] == 'c');
 1757       g_assert (gbarray->data[4*i+3] == 'd');
 1758     }
 1759 
 1760   gbarray2 = g_byte_array_ref (gbarray);
 1761   g_assert (gbarray2 == gbarray);
 1762   g_byte_array_free (gbarray, TRUE);
 1763   g_assert_cmpint (gbarray2->len, ==, 0);
 1764   g_byte_array_unref (gbarray2);
 1765 }
 1766 
 1767 static void
 1768 byte_array_remove (void)
 1769 {
 1770   GByteArray *gbarray;
 1771   gint i;
 1772 
 1773   gbarray = g_byte_array_new ();
 1774   for (i = 0; i < 100; i++)
 1775     g_byte_array_append (gbarray, (guint8*) "abcd", 4);
 1776 
 1777   g_assert_cmpint (gbarray->len, ==, 400);
 1778 
 1779   g_byte_array_remove_index (gbarray, 4);
 1780   g_byte_array_remove_index (gbarray, 4);
 1781   g_byte_array_remove_index (gbarray, 4);
 1782   g_byte_array_remove_index (gbarray, 4);
 1783 
 1784   g_assert_cmpint (gbarray->len, ==, 396);
 1785 
 1786   for (i = 0; i < 99; i++)
 1787     {
 1788       g_assert (gbarray->data[4*i] == 'a');
 1789       g_assert (gbarray->data[4*i+1] == 'b');
 1790       g_assert (gbarray->data[4*i+2] == 'c');
 1791       g_assert (gbarray->data[4*i+3] == 'd');
 1792     }
 1793 
 1794   g_byte_array_free (gbarray, TRUE);
 1795 }
 1796 
 1797 static void
 1798 byte_array_remove_fast (void)
 1799 {
 1800   GByteArray *gbarray;
 1801   gint i;
 1802 
 1803   gbarray = g_byte_array_new ();
 1804   for (i = 0; i < 100; i++)
 1805     g_byte_array_append (gbarray, (guint8*) "abcd", 4);
 1806 
 1807   g_assert_cmpint (gbarray->len, ==, 400);
 1808 
 1809   g_byte_array_remove_index_fast (gbarray, 4);
 1810   g_byte_array_remove_index_fast (gbarray, 4);
 1811   g_byte_array_remove_index_fast (gbarray, 4);
 1812   g_byte_array_remove_index_fast (gbarray, 4);
 1813 
 1814   g_assert_cmpint (gbarray->len, ==, 396);
 1815 
 1816   for (i = 0; i < 99; i++)
 1817     {
 1818       g_assert (gbarray->data[4*i] == 'a');
 1819       g_assert (gbarray->data[4*i+1] == 'b');
 1820       g_assert (gbarray->data[4*i+2] == 'c');
 1821       g_assert (gbarray->data[4*i+3] == 'd');
 1822     }
 1823 
 1824   g_byte_array_free (gbarray, TRUE);
 1825 }
 1826 
 1827 static void
 1828 byte_array_remove_range (void)
 1829 {
 1830   GByteArray *gbarray;
 1831   gint i;
 1832 
 1833   gbarray = g_byte_array_new ();
 1834   for (i = 0; i < 100; i++)
 1835     g_byte_array_append (gbarray, (guint8*) "abcd", 4);
 1836 
 1837   g_assert_cmpint (gbarray->len, ==, 400);
 1838 
 1839   g_byte_array_remove_range (gbarray, 12, 4);
 1840 
 1841   g_assert_cmpint (gbarray->len, ==, 396);
 1842 
 1843   for (i = 0; i < 99; i++)
 1844     {
 1845       g_assert (gbarray->data[4*i] == 'a');
 1846       g_assert (gbarray->data[4*i+1] == 'b');
 1847       g_assert (gbarray->data[4*i+2] == 'c');
 1848       g_assert (gbarray->data[4*i+3] == 'd');
 1849     }
 1850 
 1851   /* Ensure the entire array can be cleared, even when empty. */
 1852   g_byte_array_remove_range (gbarray, 0, gbarray->len);
 1853   g_byte_array_remove_range (gbarray, 0, gbarray->len);
 1854 
 1855   g_byte_array_free (gbarray, TRUE);
 1856 }
 1857 
 1858 static int
 1859 byte_compare (gconstpointer p1, gconstpointer p2)
 1860 {
 1861   const guint8 *i1 = p1;
 1862   const guint8 *i2 = p2;
 1863 
 1864   return *i1 - *i2;
 1865 }
 1866 
 1867 static int
 1868 byte_compare_data (gconstpointer p1, gconstpointer p2, gpointer data)
 1869 {
 1870   const guint8 *i1 = p1;
 1871   const guint8 *i2 = p2;
 1872 
 1873   return *i1 - *i2;
 1874 }
 1875 
 1876 static void
 1877 byte_array_sort (void)
 1878 {
 1879   GByteArray *gbarray;
 1880   gint i;
 1881   guint8 val;
 1882   guint8 prev, cur;
 1883 
 1884   gbarray = g_byte_array_new ();
 1885   for (i = 0; i < 100; i++)
 1886     {
 1887       val = 'a' + g_random_int_range (0, 26);
 1888       g_byte_array_append (gbarray, (guint8*) &val, 1);
 1889     }
 1890 
 1891   g_byte_array_sort (gbarray, byte_compare);
 1892 
 1893   prev = 'a';
 1894   for (i = 0; i < gbarray->len; i++)
 1895     {
 1896       cur = gbarray->data[i];
 1897       g_assert_cmpint (prev, <=, cur);
 1898       prev = cur;
 1899     }
 1900 
 1901   g_byte_array_free (gbarray, TRUE);
 1902 }
 1903 
 1904 static void
 1905 byte_array_sort_with_data (void)
 1906 {
 1907   GByteArray *gbarray;
 1908   gint i;
 1909   guint8 val;
 1910   guint8 prev, cur;
 1911 
 1912   gbarray = g_byte_array_new ();
 1913   for (i = 0; i < 100; i++)
 1914     {
 1915       val = 'a' + g_random_int_range (0, 26);
 1916       g_byte_array_append (gbarray, (guint8*) &val, 1);
 1917     }
 1918 
 1919   g_byte_array_sort_with_data (gbarray, byte_compare_data, NULL);
 1920 
 1921   prev = 'a';
 1922   for (i = 0; i < gbarray->len; i++)
 1923     {
 1924       cur = gbarray->data[i];
 1925       g_assert_cmpint (prev, <=, cur);
 1926       prev = cur;
 1927     }
 1928 
 1929   g_byte_array_free (gbarray, TRUE);
 1930 }
 1931 
 1932 static void
 1933 byte_array_new_take (void)
 1934 {
 1935   GByteArray *gbarray;
 1936   guint8 *data;
 1937 
 1938   data = g_memdup2 ("woooweeewow", 11);
 1939   gbarray = g_byte_array_new_take (data, 11);
 1940   g_assert (gbarray->data == data);
 1941   g_assert_cmpuint (gbarray->len, ==, 11);
 1942   g_byte_array_free (gbarray, TRUE);
 1943 }
 1944 
 1945 static void
 1946 byte_array_free_to_bytes (void)
 1947 {
 1948   GByteArray *gbarray;
 1949   gpointer memory;
 1950   GBytes *bytes;
 1951   gsize size;
 1952 
 1953   gbarray = g_byte_array_new ();
 1954   g_byte_array_append (gbarray, (guint8 *)"woooweeewow", 11);
 1955   memory = gbarray->data;
 1956 
 1957   bytes = g_byte_array_free_to_bytes (gbarray);
 1958   g_assert (bytes != NULL);
 1959   g_assert_cmpuint (g_bytes_get_size (bytes), ==, 11);
 1960   g_assert (g_bytes_get_data (bytes, &size) == memory);
 1961   g_assert_cmpuint (size, ==, 11);
 1962 
 1963   g_bytes_unref (bytes);
 1964 }
 1965 
 1966 static void
 1967 add_array_test (const gchar         *test_path,
 1968                 const ArrayTestData *config,
 1969                 GTestDataFunc        test_func)
 1970 {
 1971   gchar *test_name = NULL;
 1972 
 1973   test_name = g_strdup_printf ("%s/%s-%s",
 1974                                test_path,
 1975                                config->zero_terminated ? "zero-terminated" : "non-zero-terminated",
 1976                                config->clear_ ? "clear" : "no-clear");
 1977   g_test_add_data_func (test_name, config, test_func);
 1978   g_free (test_name);
 1979 }
 1980 
 1981 int
 1982 main (int argc, char *argv[])
 1983 {
 1984   /* Test all possible combinations of g_array_new() parameters. */
 1985   const ArrayTestData array_configurations[] =
 1986     {
 1987       { FALSE, FALSE },
 1988       { FALSE, TRUE },
 1989       { TRUE, FALSE },
 1990       { TRUE, TRUE },
 1991     };
 1992   gsize i;
 1993 
 1994   g_test_init (&argc, &argv, NULL);
 1995 
 1996   g_test_bug_base ("https://bugzilla.gnome.org/");
 1997 
 1998   /* array tests */
 1999   g_test_add_func ("/array/new/zero-terminated", array_new_zero_terminated);
 2000   g_test_add_func ("/array/ref-count", array_ref_count);
 2001   g_test_add_func ("/array/steal", array_steal);
 2002   g_test_add_func ("/array/clear-func", array_clear_func);
 2003   g_test_add_func ("/array/binary-search", test_array_binary_search);
 2004   g_test_add_func ("/array/copy-sized", test_array_copy_sized);
 2005 
 2006   for (i = 0; i < G_N_ELEMENTS (array_configurations); i++)
 2007     {
 2008       add_array_test ("/array/set-size", &array_configurations[i], array_set_size);
 2009       add_array_test ("/array/set-size/sized", &array_configurations[i], array_set_size_sized);
 2010       add_array_test ("/array/append-val", &array_configurations[i], array_append_val);
 2011       add_array_test ("/array/prepend-val", &array_configurations[i], array_prepend_val);
 2012       add_array_test ("/array/prepend-vals", &array_configurations[i], array_prepend_vals);
 2013       add_array_test ("/array/insert-vals", &array_configurations[i], array_insert_vals);
 2014       add_array_test ("/array/remove-index", &array_configurations[i], array_remove_index);
 2015       add_array_test ("/array/remove-index-fast", &array_configurations[i], array_remove_index_fast);
 2016       add_array_test ("/array/remove-range", &array_configurations[i], array_remove_range);
 2017       add_array_test ("/array/copy", &array_configurations[i], array_copy);
 2018       add_array_test ("/array/sort", &array_configurations[i], array_sort);
 2019       add_array_test ("/array/sort-with-data", &array_configurations[i], array_sort_with_data);
 2020     }
 2021 
 2022   /* pointer arrays */
 2023   g_test_add_func ("/pointerarray/add", pointer_array_add);
 2024   g_test_add_func ("/pointerarray/insert", pointer_array_insert);
 2025   g_test_add_func ("/pointerarray/ref-count", pointer_array_ref_count);
 2026   g_test_add_func ("/pointerarray/free-func", pointer_array_free_func);
 2027   g_test_add_func ("/pointerarray/array_copy", pointer_array_copy);
 2028   g_test_add_func ("/pointerarray/array_extend", pointer_array_extend);
 2029   g_test_add_func ("/pointerarray/array_extend_and_steal", pointer_array_extend_and_steal);
 2030   g_test_add_func ("/pointerarray/sort", pointer_array_sort);
 2031   g_test_add_func ("/pointerarray/sort/example", pointer_array_sort_example);
 2032   g_test_add_func ("/pointerarray/sort-with-data", pointer_array_sort_with_data);
 2033   g_test_add_func ("/pointerarray/sort-with-data/example", pointer_array_sort_with_data_example);
 2034   g_test_add_func ("/pointerarray/find/empty", pointer_array_find_empty);
 2035   g_test_add_func ("/pointerarray/find/non-empty", pointer_array_find_non_empty);
 2036   g_test_add_func ("/pointerarray/steal", pointer_array_steal);
 2037   g_test_add_func ("/pointerarray/steal_index", pointer_array_steal_index);
 2038 
 2039   /* byte arrays */
 2040   g_test_add_func ("/bytearray/steal", byte_array_steal);
 2041   g_test_add_func ("/bytearray/append", byte_array_append);
 2042   g_test_add_func ("/bytearray/prepend", byte_array_prepend);
 2043   g_test_add_func ("/bytearray/remove", byte_array_remove);
 2044   g_test_add_func ("/bytearray/remove-fast", byte_array_remove_fast);
 2045   g_test_add_func ("/bytearray/remove-range", byte_array_remove_range);
 2046   g_test_add_func ("/bytearray/ref-count", byte_array_ref_count);
 2047   g_test_add_func ("/bytearray/sort", byte_array_sort);
 2048   g_test_add_func ("/bytearray/sort-with-data", byte_array_sort_with_data);
 2049   g_test_add_func ("/bytearray/new-take", byte_array_new_take);
 2050   g_test_add_func ("/bytearray/free-to-bytes", byte_array_free_to_bytes);
 2051 
 2052   return g_test_run ();
 2053 }