Imported Upstream version 2.72.3
[platform/upstream/glib.git] / glib / tests / array-test.c
index 64b996f..284ac08 100644 (file)
  */
 
 #undef G_DISABLE_ASSERT
-#undef G_LOG_DOMAIN
 
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include "glib.h"
 
+/* Test data to be passed to any function which calls g_array_new(), providing
+ * the parameters for that call. Most #GArray tests should be repeated for all
+ * possible values of #ArrayTestData. */
+typedef struct
+{
+  gboolean zero_terminated;
+  gboolean clear_;
+} ArrayTestData;
+
+/* Assert that @garray contains @n_expected_elements as given in @expected_data.
+ * @garray must contain #gint elements. */
+static void
+assert_int_array_equal (GArray     *garray,
+                        const gint *expected_data,
+                        gsize       n_expected_elements)
+{
+  gsize i;
+
+  g_assert_cmpuint (garray->len, ==, n_expected_elements);
+  for (i = 0; i < garray->len; i++)
+    g_assert_cmpint (g_array_index (garray, gint, i), ==, expected_data[i]);
+}
+
+/* Iff config->zero_terminated is %TRUE, assert that the final element of
+ * @garray is zero. @garray must contain #gint elements. */
+static void
+assert_int_array_zero_terminated (const ArrayTestData *config,
+                                  GArray              *garray)
+{
+  if (config->zero_terminated)
+    {
+      gint *data = (gint *) garray->data;
+      g_assert_cmpint (data[garray->len], ==, 0);
+    }
+}
+
 static void
 sum_up (gpointer data,
        gpointer user_data)
@@ -42,38 +77,46 @@ sum_up (gpointer data,
 /* Check that expanding an array with g_array_set_size() clears the new elements
  * if @clear_ was specified during construction. */
 static void
-array_new_cleared (void)
+array_set_size (gconstpointer test_data)
 {
+  const ArrayTestData *config = test_data;
   GArray *garray;
   gsize i;
 
-  garray = g_array_new (FALSE, TRUE, sizeof (gint));
+  garray = g_array_new (config->zero_terminated, config->clear_, sizeof (gint));
   g_assert_cmpuint (garray->len, ==, 0);
+  assert_int_array_zero_terminated (config, garray);
 
   g_array_set_size (garray, 5);
   g_assert_cmpuint (garray->len, ==, 5);
+  assert_int_array_zero_terminated (config, garray);
 
-  for (i = 0; i < 5; i++)
-    g_assert_cmpint (g_array_index (garray, gint, i), ==, 0);
+  if (config->clear_)
+    for (i = 0; i < 5; i++)
+      g_assert_cmpint (g_array_index (garray, gint, i), ==, 0);
 
   g_array_unref (garray);
 }
 
-/* As with array_new_cleared(), but with a sized array. */
+/* As with array_set_size(), but with a sized array. */
 static void
-array_new_sized_cleared (void)
+array_set_size_sized (gconstpointer test_data)
 {
+  const ArrayTestData *config = test_data;
   GArray *garray;
   gsize i;
 
-  garray = g_array_sized_new (FALSE, TRUE, sizeof (gint), 10);
+  garray = g_array_sized_new (config->zero_terminated, config->clear_, sizeof (gint), 10);
   g_assert_cmpuint (garray->len, ==, 0);
+  assert_int_array_zero_terminated (config, garray);
 
   g_array_set_size (garray, 5);
   g_assert_cmpuint (garray->len, ==, 5);
+  assert_int_array_zero_terminated (config, garray);
 
-  for (i = 0; i < 5; i++)
-    g_assert_cmpint (g_array_index (garray, gint, i), ==, 0);
+  if (config->clear_)
+    for (i = 0; i < 5; i++)
+      g_assert_cmpint (g_array_index (garray, gint, i), ==, 0);
 
   g_array_unref (garray);
 }
@@ -97,16 +140,71 @@ array_new_zero_terminated (void)
   g_free (out_str);
 }
 
+/* Check g_array_steal() function */
+static void
+array_steal (void)
+{
+  const guint array_size = 10000;
+  GArray *garray;
+  gint *adata;
+  guint i;
+  gsize len, past_len;
+
+  garray = g_array_new (FALSE, FALSE, sizeof (gint));
+  adata = (gint *) g_array_steal (garray, NULL);
+  g_assert_null (adata);
+
+  adata = (gint *) g_array_steal (garray, &len);
+  g_assert_null (adata);
+  g_assert_cmpint (len, ==, 0);
+
+  for (i = 0; i < array_size; i++)
+    g_array_append_val (garray, i);
+
+  for (i = 0; i < array_size; i++)
+    g_assert_cmpint (g_array_index (garray, gint, i), ==, i);
+
+
+  past_len = garray->len;
+  adata = (gint *) g_array_steal (garray, &len);
+  for (i = 0; i < array_size; i++)
+    g_assert_cmpint (adata[i], ==, i);
+
+  g_assert_cmpint (past_len, ==, len);
+  g_assert_cmpint (garray->len, ==, 0);
+
+  g_array_append_val (garray, i);
+
+  g_assert_cmpint (adata[0], ==, 0);
+  g_assert_cmpint (g_array_index (garray, gint, 0), ==, array_size);
+  g_assert_cmpint (garray->len, ==, 1);
+
+  g_array_remove_index (garray, 0);
+
+  for (i = 0; i < array_size; i++)
+    g_array_append_val (garray, i);
+
+  g_assert_cmpint (garray->len, ==, array_size);
+  g_assert_cmpmem (adata, array_size * sizeof (gint),
+                   garray->data, array_size * sizeof (gint));
+  g_free (adata);
+  g_array_free (garray, TRUE);
+}
+
+/* Check that g_array_append_val() works correctly for various #GArray
+ * configurations. */
 static void
-array_append (void)
+array_append_val (gconstpointer test_data)
 {
+  const ArrayTestData *config = test_data;
   GArray *garray;
   gint i;
   gint *segment;
 
-  garray = g_array_new (FALSE, FALSE, sizeof (gint));
+  garray = g_array_new (config->zero_terminated, config->clear_, sizeof (gint));
   for (i = 0; i < 10000; i++)
     g_array_append_val (garray, i);
+  assert_int_array_zero_terminated (config, garray);
 
   for (i = 0; i < 10000; i++)
     g_assert_cmpint (g_array_index (garray, gint, i), ==, i);
@@ -114,18 +212,25 @@ array_append (void)
   segment = (gint*)g_array_free (garray, FALSE);
   for (i = 0; i < 10000; i++)
     g_assert_cmpint (segment[i], ==, i);
+  if (config->zero_terminated)
+    g_assert_cmpint (segment[10000], ==, 0);
+
   g_free (segment);
 }
 
+/* Check that g_array_prepend_val() works correctly for various #GArray
+ * configurations. */
 static void
-array_prepend (void)
+array_prepend_val (gconstpointer test_data)
 {
+  const ArrayTestData *config = test_data;
   GArray *garray;
   gint i;
 
-  garray = g_array_new (FALSE, FALSE, sizeof (gint));
+  garray = g_array_new (config->zero_terminated, config->clear_, sizeof (gint));
   for (i = 0; i < 100; i++)
     g_array_prepend_val (garray, i);
+  assert_int_array_zero_terminated (config, garray);
 
   for (i = 0; i < 100; i++)
     g_assert_cmpint (g_array_index (garray, gint, i), ==, (100 - i - 1));
@@ -133,16 +238,141 @@ array_prepend (void)
   g_array_free (garray, TRUE);
 }
 
+/* Test that g_array_prepend_vals() works correctly with various array
+ * configurations. */
+static void
+array_prepend_vals (gconstpointer test_data)
+{
+  const ArrayTestData *config = test_data;
+  GArray *garray, *garray_out;
+  const gint vals[] = { 0, 1, 2, 3, 4 };
+  const gint expected_vals1[] = { 0, 1 };
+  const gint expected_vals2[] = { 2, 0, 1 };
+  const gint expected_vals3[] = { 3, 4, 2, 0, 1 };
+
+  /* Set up an array. */
+  garray = g_array_new (config->zero_terminated, config->clear_, sizeof (gint));
+  assert_int_array_zero_terminated (config, garray);
+
+  /* Prepend several values to an empty array. */
+  garray_out = g_array_prepend_vals (garray, vals, 2);
+  g_assert_true (garray == garray_out);
+  assert_int_array_equal (garray, expected_vals1, G_N_ELEMENTS (expected_vals1));
+  assert_int_array_zero_terminated (config, garray);
+
+  /* Prepend a single value. */
+  garray_out = g_array_prepend_vals (garray, vals + 2, 1);
+  g_assert_true (garray == garray_out);
+  assert_int_array_equal (garray, expected_vals2, G_N_ELEMENTS (expected_vals2));
+  assert_int_array_zero_terminated (config, garray);
+
+  /* Prepend several values to a non-empty array. */
+  garray_out = g_array_prepend_vals (garray, vals + 3, 2);
+  g_assert_true (garray == garray_out);
+  assert_int_array_equal (garray, expected_vals3, G_N_ELEMENTS (expected_vals3));
+  assert_int_array_zero_terminated (config, garray);
+
+  /* Prepend no values. */
+  garray_out = g_array_prepend_vals (garray, vals, 0);
+  g_assert_true (garray == garray_out);
+  assert_int_array_equal (garray, expected_vals3, G_N_ELEMENTS (expected_vals3));
+  assert_int_array_zero_terminated (config, garray);
+
+  /* Prepend no values with %NULL data. */
+  garray_out = g_array_prepend_vals (garray, NULL, 0);
+  g_assert_true (garray == garray_out);
+  assert_int_array_equal (garray, expected_vals3, G_N_ELEMENTS (expected_vals3));
+  assert_int_array_zero_terminated (config, garray);
+
+  g_array_free (garray, TRUE);
+}
+
+/* Test that g_array_insert_vals() works correctly with various array
+ * configurations. */
 static void
-array_remove (void)
+array_insert_vals (gconstpointer test_data)
 {
+  const ArrayTestData *config = test_data;
+  GArray *garray, *garray_out;
+  gsize i;
+  const gint vals[] = { 0, 1, 2, 3, 4, 5, 6, 7 };
+  const gint expected_vals1[] = { 0, 1 };
+  const gint expected_vals2[] = { 0, 2, 3, 1 };
+  const gint expected_vals3[] = { 0, 2, 3, 1, 4 };
+  const gint expected_vals4[] = { 5, 0, 2, 3, 1, 4 };
+  const gint expected_vals5[] = { 5, 0, 2, 3, 1, 4, 0, 0, 0, 0, 6, 7 };
+
+  /* Set up an array. */
+  garray = g_array_new (config->zero_terminated, config->clear_, sizeof (gint));
+  assert_int_array_zero_terminated (config, garray);
+
+  /* Insert several values at the beginning. */
+  garray_out = g_array_insert_vals (garray, 0, vals, 2);
+  g_assert_true (garray == garray_out);
+  assert_int_array_equal (garray, expected_vals1, G_N_ELEMENTS (expected_vals1));
+  assert_int_array_zero_terminated (config, garray);
+
+  /* Insert some more part-way through. */
+  garray_out = g_array_insert_vals (garray, 1, vals + 2, 2);
+  g_assert_true (garray == garray_out);
+  assert_int_array_equal (garray, expected_vals2, G_N_ELEMENTS (expected_vals2));
+  assert_int_array_zero_terminated (config, garray);
+
+  /* And at the end. */
+  garray_out = g_array_insert_vals (garray, garray->len, vals + 4, 1);
+  g_assert_true (garray == garray_out);
+  assert_int_array_equal (garray, expected_vals3, G_N_ELEMENTS (expected_vals3));
+  assert_int_array_zero_terminated (config, garray);
+
+  /* Then back at the beginning again. */
+  garray_out = g_array_insert_vals (garray, 0, vals + 5, 1);
+  g_assert_true (garray == garray_out);
+  assert_int_array_equal (garray, expected_vals4, G_N_ELEMENTS (expected_vals4));
+  assert_int_array_zero_terminated (config, garray);
+
+  /* Insert zero elements. */
+  garray_out = g_array_insert_vals (garray, 0, vals, 0);
+  g_assert_true (garray == garray_out);
+  assert_int_array_equal (garray, expected_vals4, G_N_ELEMENTS (expected_vals4));
+  assert_int_array_zero_terminated (config, garray);
+
+  /* Insert zero elements with a %NULL pointer. */
+  garray_out = g_array_insert_vals (garray, 0, NULL, 0);
+  g_assert_true (garray == garray_out);
+  assert_int_array_equal (garray, expected_vals4, G_N_ELEMENTS (expected_vals4));
+  assert_int_array_zero_terminated (config, garray);
+
+  /* Insert some elements off the end of the array. The behaviour here depends
+   * on whether the array clears entries. */
+  garray_out = g_array_insert_vals (garray, garray->len + 4, vals + 6, 2);
+  g_assert_true (garray == garray_out);
+
+  g_assert_cmpuint (garray->len, ==, G_N_ELEMENTS (expected_vals5));
+  for (i = 0; i < G_N_ELEMENTS (expected_vals5); i++)
+    {
+      if (config->clear_ || i < 6 || i > 9)
+        g_assert_cmpint (g_array_index (garray, gint, i), ==, expected_vals5[i]);
+    }
+
+  assert_int_array_zero_terminated (config, garray);
+
+  g_array_free (garray, TRUE);
+}
+
+/* Check that g_array_remove_index() works correctly for various #GArray
+ * configurations. */
+static void
+array_remove_index (gconstpointer test_data)
+{
+  const ArrayTestData *config = test_data;
   GArray *garray;
-  gint i;
+  guint i;
   gint prev, cur;
 
-  garray = g_array_new (FALSE, FALSE, sizeof (gint));
+  garray = g_array_new (config->zero_terminated, config->clear_, sizeof (gint));
   for (i = 0; i < 100; i++)
     g_array_append_val (garray, i);
+  assert_int_array_zero_terminated (config, garray);
 
   g_assert_cmpint (garray->len, ==, 100);
 
@@ -152,6 +382,7 @@ array_remove (void)
   g_array_remove_index (garray, 57);
 
   g_assert_cmpint (garray->len, ==, 96);
+  assert_int_array_zero_terminated (config, garray);
 
   prev = -1;
   for (i = 0; i < garray->len; i++)
@@ -165,18 +396,22 @@ array_remove (void)
   g_array_free (garray, TRUE);
 }
 
+/* Check that g_array_remove_index_fast() works correctly for various #GArray
+ * configurations. */
 static void
-array_remove_fast (void)
+array_remove_index_fast (gconstpointer test_data)
 {
+  const ArrayTestData *config = test_data;
   GArray *garray;
-  gint i;
+  guint i;
   gint prev, cur;
 
-  garray = g_array_new (FALSE, FALSE, sizeof (gint));
+  garray = g_array_new (config->zero_terminated, config->clear_, sizeof (gint));
   for (i = 0; i < 100; i++)
     g_array_append_val (garray, i);
 
   g_assert_cmpint (garray->len, ==, 100);
+  assert_int_array_zero_terminated (config, garray);
 
   g_array_remove_index_fast (garray, 1);
   g_array_remove_index_fast (garray, 3);
@@ -184,6 +419,7 @@ array_remove_fast (void)
   g_array_remove_index_fast (garray, 57);
 
   g_assert_cmpint (garray->len, ==, 96);
+  assert_int_array_zero_terminated (config, garray);
 
   prev = -1;
   for (i = 0; i < garray->len; i++)
@@ -200,22 +436,27 @@ array_remove_fast (void)
   g_array_free (garray, TRUE);
 }
 
+/* Check that g_array_remove_range() works correctly for various #GArray
+ * configurations. */
 static void
-array_remove_range (void)
+array_remove_range (gconstpointer test_data)
 {
+  const ArrayTestData *config = test_data;
   GArray *garray;
-  gint i;
+  guint i;
   gint prev, cur;
 
-  garray = g_array_new (FALSE, FALSE, sizeof (gint));
+  garray = g_array_new (config->zero_terminated, config->clear_, sizeof (gint));
   for (i = 0; i < 100; i++)
     g_array_append_val (garray, i);
 
   g_assert_cmpint (garray->len, ==, 100);
+  assert_int_array_zero_terminated (config, garray);
 
   g_array_remove_range (garray, 31, 4);
 
   g_assert_cmpint (garray->len, ==, 96);
+  assert_int_array_zero_terminated (config, garray);
 
   prev = -1;
   for (i = 0; i < garray->len; i++)
@@ -228,8 +469,15 @@ array_remove_range (void)
 
   /* Ensure the entire array can be cleared, even when empty. */
   g_array_remove_range (garray, 0, garray->len);
+
+  g_assert_cmpint (garray->len, ==, 0);
+  assert_int_array_zero_terminated (config, garray);
+
   g_array_remove_range (garray, 0, garray->len);
 
+  g_assert_cmpint (garray->len, ==, 0);
+  assert_int_array_zero_terminated (config, garray);
+
   g_array_free (garray, TRUE);
 }
 
@@ -269,6 +517,59 @@ int_compare (gconstpointer p1, gconstpointer p2)
   return *i1 - *i2;
 }
 
+static void
+array_copy (gconstpointer test_data)
+{
+  GArray *array, *array_copy;
+  gsize i;
+  const ArrayTestData *config = test_data;
+  const gsize array_size = 100;
+
+  /* Testing degenerated cases */
+  if (g_test_undefined ())
+    {
+      g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
+                             "*assertion*!= NULL*");
+      array = g_array_copy (NULL);
+      g_test_assert_expected_messages ();
+
+      g_assert_null (array);
+    }
+
+  /* Testing simple copy */
+  array = g_array_new (config->zero_terminated, config->clear_, sizeof (gint));
+
+  for (i = 0; i < array_size; i++)
+    g_array_append_val (array, i);
+
+  array_copy = g_array_copy (array);
+
+  /* Check internal data */
+  for (i = 0; i < array_size; i++)
+    g_assert_cmpuint (g_array_index (array, gint, i), ==,
+                      g_array_index (array_copy, gint, i));
+
+  /* Check internal parameters ('zero_terminated' flag) */
+  if (config->zero_terminated)
+    {
+      const gint *data = (const gint *) array_copy->data;
+      g_assert_cmpint (data[array_copy->len], ==, 0);
+    }
+
+  /* Check internal parameters ('clear' flag) */
+  if (config->clear_)
+    {
+      guint old_length = array_copy->len;
+      g_array_set_size (array_copy, old_length + 5);
+      for (i = old_length; i < old_length + 5; i++)
+        g_assert_cmpint (g_array_index (array_copy, gint, i), ==, 0);
+    }
+
+  /* Clean-up */
+  g_array_unref (array);
+  g_array_unref (array_copy);
+}
+
 static int
 int_compare_data (gconstpointer p1, gconstpointer p2, gpointer data)
 {
@@ -277,20 +578,31 @@ int_compare_data (gconstpointer p1, gconstpointer p2, gpointer data)
 
   return *i1 - *i2;
 }
+
+/* Check that g_array_sort() works correctly for various #GArray
+ * configurations. */
 static void
-array_sort (void)
+array_sort (gconstpointer test_data)
 {
+  const ArrayTestData *config = test_data;
   GArray *garray;
-  gint i;
+  guint i;
   gint prev, cur;
 
-  garray = g_array_new (FALSE, FALSE, sizeof (gint));
+  garray = g_array_new (config->zero_terminated, config->clear_, sizeof (gint));
+
+  /* Sort empty array */
+  g_array_sort (garray, int_compare);
+
   for (i = 0; i < 10000; i++)
     {
       cur = g_random_int_range (0, 10000);
       g_array_append_val (garray, cur);
     }
-  g_array_sort (garray,  int_compare);
+  assert_int_array_zero_terminated (config, garray);
+
+  g_array_sort (garray, int_compare);
+  assert_int_array_zero_terminated (config, garray);
 
   prev = -1;
   for (i = 0; i < garray->len; i++)
@@ -303,20 +615,30 @@ array_sort (void)
   g_array_free (garray, TRUE);
 }
 
+/* Check that g_array_sort_with_data() works correctly for various #GArray
+ * configurations. */
 static void
-array_sort_with_data (void)
+array_sort_with_data (gconstpointer test_data)
 {
+  const ArrayTestData *config = test_data;
   GArray *garray;
-  gint i;
+  guint i;
   gint prev, cur;
 
-  garray = g_array_new (FALSE, FALSE, sizeof (gint));
+  garray = g_array_new (config->zero_terminated, config->clear_, sizeof (gint));
+
+  /* Sort empty array */
+  g_array_sort_with_data (garray, int_compare_data, NULL);
+
   for (i = 0; i < 10000; i++)
     {
       cur = g_random_int_range (0, 10000);
       g_array_append_val (garray, cur);
     }
+  assert_int_array_zero_terminated (config, garray);
+
   g_array_sort_with_data (garray, int_compare_data, NULL);
+  assert_int_array_zero_terminated (config, garray);
 
   prev = -1;
   for (i = 0; i < garray->len; i++)
@@ -366,6 +688,245 @@ array_clear_func (void)
   g_assert_cmpint (num_clear_func_invocations, ==, 10);
 }
 
+/* Defining a comparison function for testing g_array_binary_search() */
+static gint
+cmpint (gconstpointer a, gconstpointer b)
+{
+  const gint *_a = a;
+  const gint *_b = b;
+
+  return *_a - *_b;
+}
+
+/* Testing g_array_binary_search() function */
+static void
+test_array_binary_search (void)
+{
+  GArray *garray;
+  guint i, matched_index;
+
+  if (g_test_undefined ())
+    {
+      /* Testing degenerated cases */
+      garray = g_array_sized_new (FALSE, FALSE, sizeof (guint), 0);
+      g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
+                             "*assertion*!= NULL*");
+      g_assert_false (g_array_binary_search (NULL, &i, cmpint, NULL));
+      g_test_assert_expected_messages ();
+
+      g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
+                             "*assertion*!= NULL*");
+      g_assert_false (g_array_binary_search (garray, &i, NULL, NULL));
+      g_test_assert_expected_messages ();
+      g_array_free (garray, TRUE);
+    }
+
+  /* Testing array of size 0 */
+  garray = g_array_sized_new (FALSE, FALSE, sizeof (guint), 0);
+
+  i = 1;
+  g_assert_false (g_array_binary_search (garray, &i, cmpint, NULL));
+
+  g_array_free (garray, TRUE);
+
+  /* Testing array of size 1 */
+  garray = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
+  i = 1;
+  g_array_append_val (garray, i);
+
+  g_assert_true (g_array_binary_search (garray, &i, cmpint, NULL));
+
+  i = 0;
+  g_assert_false (g_array_binary_search (garray, &i, cmpint, NULL));
+
+  i = 2;
+  g_assert_false (g_array_binary_search (garray, &i, cmpint, NULL));
+
+  g_array_free (garray, TRUE);
+
+  /* Testing array of size 2 */
+  garray = g_array_sized_new (FALSE, FALSE, sizeof (guint), 2);
+  for (i = 1; i < 3; i++)
+    g_array_append_val (garray, i);
+
+  for (i = 1; i < 3; i++)
+    g_assert_true (g_array_binary_search (garray, &i, cmpint, NULL));
+
+  i = 0;
+  g_assert_false (g_array_binary_search (garray, &i, cmpint, NULL));
+
+  i = 4;
+  g_assert_false (g_array_binary_search (garray, &i, cmpint, NULL));
+
+  g_array_free (garray, TRUE);
+
+  /* Testing array of size 3 */
+  garray = g_array_sized_new (FALSE, FALSE, sizeof (guint), 3);
+  for (i = 1; i < 4; i++)
+    g_array_append_val (garray, i);
+
+  for (i = 1; i < 4; i++)
+    g_assert_true (g_array_binary_search (garray, &i, cmpint, NULL));
+
+  i = 0;
+  g_assert_false (g_array_binary_search (garray, &i, cmpint, NULL));
+
+  i = 5;
+  g_assert_false (g_array_binary_search (garray, &i, cmpint, NULL));
+
+  g_array_free (garray, TRUE);
+
+  /* Testing array of size 10000 */
+  garray = g_array_sized_new (FALSE, FALSE, sizeof (guint), 10000);
+
+  for (i = 1; i < 10001; i++)
+    g_array_append_val (garray, i);
+
+  for (i = 1; i < 10001; i++)
+    g_assert_true (g_array_binary_search (garray, &i, cmpint, NULL));
+
+  for (i = 1; i < 10001; i++)
+    {
+      g_assert_true (g_array_binary_search (garray, &i, cmpint, &matched_index));
+      g_assert_cmpint (i, ==, matched_index + 1);
+    }
+
+  /* Testing negative result */
+  i = 0;
+  g_assert_false (g_array_binary_search (garray, &i, cmpint, NULL));
+  g_assert_false (g_array_binary_search (garray, &i, cmpint, &matched_index));
+
+  i = 10002;
+  g_assert_false (g_array_binary_search (garray, &i, cmpint, NULL));
+  g_assert_false (g_array_binary_search (garray, &i, cmpint, &matched_index));
+
+  g_array_free (garray, TRUE);
+
+  /* Test for a not-found element in the middle of the array. */
+  garray = g_array_sized_new (FALSE, FALSE, sizeof (guint), 3);
+  for (i = 1; i < 10; i += 2)
+    g_array_append_val (garray, i);
+
+  i = 0;
+  g_assert_false (g_array_binary_search (garray, &i, cmpint, NULL));
+
+  i = 2;
+  g_assert_false (g_array_binary_search (garray, &i, cmpint, NULL));
+
+  i = 10;
+  g_assert_false (g_array_binary_search (garray, &i, cmpint, NULL));
+
+  g_array_free (garray, TRUE);
+}
+
+static void
+test_array_copy_sized (void)
+{
+  GArray *array1 = NULL, *array2 = NULL, *array3 = NULL;
+  int val = 5;
+
+  g_test_summary ("Test that copying a newly-allocated sized array works.");
+
+  array1 = g_array_sized_new (FALSE, FALSE, sizeof (int), 1);
+  array2 = g_array_copy (array1);
+
+  g_assert_cmpuint (array2->len, ==, array1->len);
+
+  g_array_append_val (array1, val);
+  array3 = g_array_copy (array1);
+
+  g_assert_cmpuint (array3->len, ==, array1->len);
+  g_assert_cmpuint (g_array_index (array3, int, 0), ==, g_array_index (array1, int, 0));
+  g_assert_cmpuint (array3->len, ==, 1);
+  g_assert_cmpuint (g_array_index (array3, int, 0), ==, val);
+
+  g_array_unref (array3);
+  g_array_unref (array2);
+  g_array_unref (array1);
+}
+
+static void
+array_overflow_append_vals (void)
+{
+  if (!g_test_undefined ())
+      return;
+
+  if (g_test_subprocess ())
+    {
+      GArray *array = g_array_new (TRUE, FALSE, 1);
+      /* Check for overflow should happen before data is accessed. */
+      g_array_append_vals (array, NULL, G_MAXUINT);
+    }
+  else
+    {
+      g_test_trap_subprocess (NULL, 0, 0);
+      g_test_trap_assert_failed ();
+      g_test_trap_assert_stderr ("*adding 4294967295 to array would overflow*");
+    }
+}
+
+static void
+array_overflow_set_size (void)
+{
+  if (!g_test_undefined ())
+      return;
+
+  if (g_test_subprocess ())
+    {
+      GArray *array = g_array_new (TRUE, FALSE, 1);
+      g_array_set_size (array, G_MAXUINT);
+    }
+  else
+    {
+      g_test_trap_subprocess (NULL, 0, 0);
+      g_test_trap_assert_failed ();
+      g_test_trap_assert_stderr ("*adding 4294967295 to array would overflow*");
+    }
+}
+
+/* Check g_ptr_array_steal() function */
+static void
+pointer_array_steal (void)
+{
+  const guint array_size = 10000;
+  GPtrArray *gparray;
+  gpointer *pdata;
+  guint i;
+  gsize len, past_len;
+
+  gparray = g_ptr_array_new ();
+  pdata = g_ptr_array_steal (gparray, NULL);
+  g_assert_null (pdata);
+
+  pdata = g_ptr_array_steal (gparray, &len);
+  g_assert_null (pdata);
+  g_assert_cmpint (len, ==, 0);
+
+  for (i = 0; i < array_size; i++)
+    g_ptr_array_add (gparray, GINT_TO_POINTER (i));
+
+  past_len = gparray->len;
+  pdata = g_ptr_array_steal (gparray, &len);
+  g_assert_cmpint (gparray->len, ==, 0);
+  g_assert_cmpint (past_len, ==, len);
+  g_ptr_array_add (gparray, GINT_TO_POINTER (10));
+
+  g_assert_cmpint ((gsize) pdata[0], ==, (gsize) GINT_TO_POINTER (0));
+  g_assert_cmpint ((gsize) g_ptr_array_index (gparray, 0), ==,
+                   (gsize) GINT_TO_POINTER (10));
+  g_assert_cmpint (gparray->len, ==, 1);
+
+  g_ptr_array_remove_index (gparray, 0);
+
+  for (i = 0; i < array_size; i++)
+    g_ptr_array_add (gparray, GINT_TO_POINTER (i));
+  g_assert_cmpmem (pdata, array_size * sizeof (gpointer),
+                   gparray->pdata, array_size * sizeof (gpointer));
+  g_free (pdata);
+
+  g_ptr_array_free (gparray, TRUE);
+}
+
 static void
 pointer_array_add (void)
 {
@@ -535,6 +1096,264 @@ pointer_array_free_func (void)
   g_assert_cmpint (num_free_func_invocations, ==, 0);
 }
 
+static gpointer
+ptr_array_copy_func (gconstpointer src, gpointer userdata)
+{
+  gsize *dst = g_malloc (sizeof (gsize));
+  *dst = *((gsize *) src);
+  return dst;
+}
+
+/* Test the g_ptr_array_copy() function */
+static void
+pointer_array_copy (void)
+{
+  GPtrArray *ptr_array, *ptr_array2;
+  gsize i;
+  const gsize array_size = 100;
+  gsize *array_test = g_malloc (array_size * sizeof (gsize));
+
+  g_test_summary ("Check all normal behaviour of stealing elements from one "
+                  "array to append to another, covering different array sizes "
+                  "and element copy functions");
+
+  if (g_test_undefined ())
+    {
+      /* Testing degenerated cases */
+      g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
+                             "*assertion*!= NULL*");
+      ptr_array = g_ptr_array_copy (NULL, NULL, NULL);
+      g_test_assert_expected_messages ();
+      g_assert_cmpuint ((gsize) ptr_array, ==, (gsize) NULL);
+    }
+
+  /* Initializing array_test */
+  for (i = 0; i < array_size; i++)
+    array_test[i] = i;
+
+  /* Test copy an empty array */
+  ptr_array = g_ptr_array_sized_new (0);
+  ptr_array2 = g_ptr_array_copy (ptr_array, NULL, NULL);
+
+  g_assert_cmpuint (ptr_array2->len, ==, ptr_array->len);
+
+  g_ptr_array_unref (ptr_array);
+  g_ptr_array_unref (ptr_array2);
+
+  /* Test simple copy */
+  ptr_array = g_ptr_array_sized_new (array_size);
+
+  for (i = 0; i < array_size; i++)
+    g_ptr_array_add (ptr_array, &array_test[i]);
+
+  ptr_array2 = g_ptr_array_copy (ptr_array, NULL, NULL);
+
+  g_assert_cmpuint (ptr_array2->len, ==, ptr_array->len);
+  for (i = 0; i < array_size; i++)
+    g_assert_cmpuint (*((gsize *) g_ptr_array_index (ptr_array2, i)), ==, i);
+
+  for (i = 0; i < array_size; i++)
+    g_assert_cmpuint ((gsize) g_ptr_array_index (ptr_array, i), ==,
+                      (gsize) g_ptr_array_index (ptr_array2, i));
+
+  g_ptr_array_free (ptr_array2, TRUE);
+
+  /* Test copy through GCopyFunc */
+  ptr_array2 = g_ptr_array_copy (ptr_array, ptr_array_copy_func, NULL);
+  g_ptr_array_set_free_func (ptr_array2, g_free);
+
+  g_assert_cmpuint (ptr_array2->len, ==, ptr_array->len);
+  for (i = 0; i < array_size; i++)
+    g_assert_cmpuint (*((gsize *) g_ptr_array_index (ptr_array2, i)), ==, i);
+
+  for (i = 0; i < array_size; i++)
+    g_assert_cmpuint ((gsize) g_ptr_array_index (ptr_array, i), !=,
+                      (gsize) g_ptr_array_index (ptr_array2, i));
+
+  g_ptr_array_free (ptr_array2, TRUE);
+
+  /* Final cleanup */
+  g_ptr_array_free (ptr_array, TRUE);
+  g_free (array_test);
+}
+
+/* Test the g_ptr_array_extend() function */
+static void
+pointer_array_extend (void)
+{
+  GPtrArray *ptr_array, *ptr_array2;
+  gsize i;
+  const gsize array_size = 100;
+  gsize *array_test = g_malloc (array_size * sizeof (gsize));
+
+  if (g_test_undefined ())
+    {
+      /* Testing degenerated cases */
+      ptr_array = g_ptr_array_sized_new (0);
+      g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
+                             "*assertion*!= NULL*");
+      g_ptr_array_extend (NULL, ptr_array, NULL, NULL);
+      g_test_assert_expected_messages ();
+
+      g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
+                             "*assertion*!= NULL*");
+      g_ptr_array_extend (ptr_array, NULL, NULL, NULL);
+      g_test_assert_expected_messages ();
+
+      g_ptr_array_unref (ptr_array);
+    }
+
+  /* Initializing array_test */
+  for (i = 0; i < array_size; i++)
+    array_test[i] = i;
+
+  /* Testing extend with array of size zero */
+  ptr_array = g_ptr_array_sized_new (0);
+  ptr_array2 = g_ptr_array_sized_new (0);
+
+  g_ptr_array_extend (ptr_array, ptr_array2, NULL, NULL);
+
+  g_assert_cmpuint (ptr_array->len, ==, 0);
+  g_assert_cmpuint (ptr_array2->len, ==, 0);
+
+  g_ptr_array_unref (ptr_array);
+  g_ptr_array_unref (ptr_array2);
+
+  /* Testing extend an array of size zero */
+  ptr_array = g_ptr_array_sized_new (array_size);
+  ptr_array2 = g_ptr_array_sized_new (0);
+
+  for (i = 0; i < array_size; i++)
+    {
+      g_ptr_array_add (ptr_array, &array_test[i]);
+    }
+
+  g_ptr_array_extend (ptr_array, ptr_array2, NULL, NULL);
+
+  for (i = 0; i < array_size; i++)
+    g_assert_cmpuint (*((gsize *) g_ptr_array_index (ptr_array, i)), ==, i);
+
+  g_ptr_array_unref (ptr_array);
+  g_ptr_array_unref (ptr_array2);
+
+  /* Testing extend an array of size zero */
+  ptr_array = g_ptr_array_sized_new (0);
+  ptr_array2 = g_ptr_array_sized_new (array_size);
+
+  for (i = 0; i < array_size; i++)
+    {
+      g_ptr_array_add (ptr_array2, &array_test[i]);
+    }
+
+  g_ptr_array_extend (ptr_array, ptr_array2, NULL, NULL);
+
+  for (i = 0; i < array_size; i++)
+    g_assert_cmpuint (*((gsize *) g_ptr_array_index (ptr_array, i)), ==, i);
+
+  g_ptr_array_unref (ptr_array);
+  g_ptr_array_unref (ptr_array2);
+
+  /* Testing simple extend */
+  ptr_array = g_ptr_array_sized_new (array_size / 2);
+  ptr_array2 = g_ptr_array_sized_new (array_size / 2);
+
+  for (i = 0; i < array_size / 2; i++)
+    {
+      g_ptr_array_add (ptr_array, &array_test[i]);
+      g_ptr_array_add (ptr_array2, &array_test[i + (array_size / 2)]);
+    }
+
+  g_ptr_array_extend (ptr_array, ptr_array2, NULL, NULL);
+
+  for (i = 0; i < array_size; i++)
+    g_assert_cmpuint (*((gsize *) g_ptr_array_index (ptr_array, i)), ==, i);
+
+  g_ptr_array_unref (ptr_array);
+  g_ptr_array_unref (ptr_array2);
+
+  /* Testing extend with GCopyFunc */
+  ptr_array = g_ptr_array_sized_new (array_size / 2);
+  ptr_array2 = g_ptr_array_sized_new (array_size / 2);
+
+  for (i = 0; i < array_size / 2; i++)
+    {
+      g_ptr_array_add (ptr_array, &array_test[i]);
+      g_ptr_array_add (ptr_array2, &array_test[i + (array_size / 2)]);
+    }
+
+  g_ptr_array_extend (ptr_array, ptr_array2, ptr_array_copy_func, NULL);
+
+  for (i = 0; i < array_size; i++)
+    g_assert_cmpuint (*((gsize *) g_ptr_array_index (ptr_array, i)), ==, i);
+
+  /* Clean-up memory */
+  for (i = array_size / 2; i < array_size; i++)
+    g_free (g_ptr_array_index (ptr_array, i));
+
+  g_ptr_array_unref (ptr_array);
+  g_ptr_array_unref (ptr_array2);
+  g_free (array_test);
+}
+
+/* Test the g_ptr_array_extend_and_steal() function */
+static void
+pointer_array_extend_and_steal (void)
+{
+  GPtrArray *ptr_array, *ptr_array2, *ptr_array3;
+  gsize i;
+  const gsize array_size = 100;
+  gsize *array_test = g_malloc (array_size * sizeof (gsize));
+
+  /* Initializing array_test */
+  for (i = 0; i < array_size; i++)
+    array_test[i] = i;
+
+  /* Testing simple extend_and_steal() */
+  ptr_array = g_ptr_array_sized_new (array_size / 2);
+  ptr_array2 = g_ptr_array_sized_new (array_size / 2);
+
+  for (i = 0; i < array_size / 2; i++)
+    {
+      g_ptr_array_add (ptr_array, &array_test[i]);
+      g_ptr_array_add (ptr_array2, &array_test[i + (array_size / 2)]);
+    }
+
+  g_ptr_array_extend_and_steal (ptr_array, ptr_array2);
+
+  for (i = 0; i < array_size; i++)
+    g_assert_cmpuint (*((gsize *) g_ptr_array_index (ptr_array, i)), ==, i);
+
+  g_ptr_array_free (ptr_array, TRUE);
+
+  /* Testing extend_and_steal() with a pending reference to stolen array */
+  ptr_array = g_ptr_array_sized_new (array_size / 2);
+  ptr_array2 = g_ptr_array_sized_new (array_size / 2);
+
+  for (i = 0; i < array_size / 2; i++)
+    {
+      g_ptr_array_add (ptr_array, &array_test[i]);
+      g_ptr_array_add (ptr_array2, &array_test[i + (array_size / 2)]);
+    }
+
+  ptr_array3 = g_ptr_array_ref (ptr_array2);
+
+  g_ptr_array_extend_and_steal (ptr_array, ptr_array2);
+
+  for (i = 0; i < array_size; i++)
+    g_assert_cmpuint (*((gsize *) g_ptr_array_index (ptr_array, i)), ==, i);
+
+  g_assert_cmpuint (ptr_array3->len, ==, 0);
+  g_assert_null (ptr_array3->pdata);
+
+  g_ptr_array_add (ptr_array2, NULL);
+
+  g_ptr_array_free (ptr_array, TRUE);
+  g_ptr_array_free (ptr_array3, TRUE);
+
+  /* Final memory clean-up */
+  g_free (array_test);
+}
+
 static gint
 ptr_compare (gconstpointer p1, gconstpointer p2)
 {
@@ -562,6 +1381,10 @@ pointer_array_sort (void)
   gint prev, cur;
 
   gparray = g_ptr_array_new ();
+
+  /* Sort empty array */
+  g_ptr_array_sort (gparray, ptr_compare);
+
   for (i = 0; i < 10000; i++)
     {
       val = g_random_int_range (0, 10000);
@@ -581,6 +1404,150 @@ pointer_array_sort (void)
   g_ptr_array_free (gparray, TRUE);
 }
 
+/* Please keep pointer_array_sort_example() in sync with the doc-comment
+ * of g_ptr_array_sort() */
+
+typedef struct
+{
+  gchar *name;
+  gint size;
+} FileListEntry;
+
+static void
+file_list_entry_free (gpointer p)
+{
+  FileListEntry *entry = p;
+
+  g_free (entry->name);
+  g_free (entry);
+}
+
+static gint
+sort_filelist (gconstpointer a, gconstpointer b)
+{
+   const FileListEntry *entry1 = *((FileListEntry **) a);
+   const FileListEntry *entry2 = *((FileListEntry **) b);
+
+   return g_ascii_strcasecmp (entry1->name, entry2->name);
+}
+
+static void
+pointer_array_sort_example (void)
+{
+  GPtrArray *file_list = NULL;
+  FileListEntry *entry;
+
+  g_test_summary ("Check that the doc-comment for g_ptr_array_sort() is correct");
+
+  file_list = g_ptr_array_new_with_free_func (file_list_entry_free);
+
+  entry = g_new0 (FileListEntry, 1);
+  entry->name = g_strdup ("README");
+  entry->size = 42;
+  g_ptr_array_add (file_list, g_steal_pointer (&entry));
+
+  entry = g_new0 (FileListEntry, 1);
+  entry->name = g_strdup ("empty");
+  entry->size = 0;
+  g_ptr_array_add (file_list, g_steal_pointer (&entry));
+
+  entry = g_new0 (FileListEntry, 1);
+  entry->name = g_strdup ("aardvark");
+  entry->size = 23;
+  g_ptr_array_add (file_list, g_steal_pointer (&entry));
+
+  g_ptr_array_sort (file_list, sort_filelist);
+
+  g_assert_cmpuint (file_list->len, ==, 3);
+  entry = g_ptr_array_index (file_list, 0);
+  g_assert_cmpstr (entry->name, ==, "aardvark");
+  entry = g_ptr_array_index (file_list, 1);
+  g_assert_cmpstr (entry->name, ==, "empty");
+  entry = g_ptr_array_index (file_list, 2);
+  g_assert_cmpstr (entry->name, ==, "README");
+
+  g_ptr_array_unref (file_list);
+}
+
+/* Please keep pointer_array_sort_with_data_example() in sync with the
+ * doc-comment of g_ptr_array_sort_with_data() */
+
+typedef enum { SORT_NAME, SORT_SIZE } SortMode;
+
+static gint
+sort_filelist_how (gconstpointer a, gconstpointer b, gpointer user_data)
+{
+  gint order;
+  const SortMode sort_mode = GPOINTER_TO_INT (user_data);
+  const FileListEntry *entry1 = *((FileListEntry **) a);
+  const FileListEntry *entry2 = *((FileListEntry **) b);
+
+  switch (sort_mode)
+    {
+    case SORT_NAME:
+      order = g_ascii_strcasecmp (entry1->name, entry2->name);
+      break;
+    case SORT_SIZE:
+      order = entry1->size - entry2->size;
+      break;
+    default:
+      order = 0;
+      break;
+    }
+  return order;
+}
+
+static void
+pointer_array_sort_with_data_example (void)
+{
+  GPtrArray *file_list = NULL;
+  FileListEntry *entry;
+  SortMode sort_mode;
+
+  g_test_summary ("Check that the doc-comment for g_ptr_array_sort_with_data() is correct");
+
+  file_list = g_ptr_array_new_with_free_func (file_list_entry_free);
+
+  entry = g_new0 (FileListEntry, 1);
+  entry->name = g_strdup ("README");
+  entry->size = 42;
+  g_ptr_array_add (file_list, g_steal_pointer (&entry));
+
+  entry = g_new0 (FileListEntry, 1);
+  entry->name = g_strdup ("empty");
+  entry->size = 0;
+  g_ptr_array_add (file_list, g_steal_pointer (&entry));
+
+  entry = g_new0 (FileListEntry, 1);
+  entry->name = g_strdup ("aardvark");
+  entry->size = 23;
+  g_ptr_array_add (file_list, g_steal_pointer (&entry));
+
+  sort_mode = SORT_NAME;
+  g_ptr_array_sort_with_data (file_list, sort_filelist_how, GINT_TO_POINTER (sort_mode));
+
+  g_assert_cmpuint (file_list->len, ==, 3);
+  entry = g_ptr_array_index (file_list, 0);
+  g_assert_cmpstr (entry->name, ==, "aardvark");
+  entry = g_ptr_array_index (file_list, 1);
+  g_assert_cmpstr (entry->name, ==, "empty");
+  entry = g_ptr_array_index (file_list, 2);
+  g_assert_cmpstr (entry->name, ==, "README");
+
+  sort_mode = SORT_SIZE;
+  g_ptr_array_sort_with_data (file_list, sort_filelist_how, GINT_TO_POINTER (sort_mode));
+
+  g_assert_cmpuint (file_list->len, ==, 3);
+  entry = g_ptr_array_index (file_list, 0);
+  g_assert_cmpstr (entry->name, ==, "empty");
+  entry = g_ptr_array_index (file_list, 1);
+  g_assert_cmpstr (entry->name, ==, "aardvark");
+  entry = g_ptr_array_index (file_list, 2);
+  g_assert_cmpstr (entry->name, ==, "README");
+
+  g_ptr_array_unref (file_list);
+}
+
 static void
 pointer_array_sort_with_data (void)
 {
@@ -589,6 +1556,10 @@ pointer_array_sort_with_data (void)
   gint prev, cur;
 
   gparray = g_ptr_array_new ();
+
+  /* Sort empty array */
+  g_ptr_array_sort_with_data (gparray, ptr_compare_data, NULL);
+
   for (i = 0; i < 10000; i++)
     g_ptr_array_add (gparray, GINT_TO_POINTER (g_random_int_range (0, 10000)));
 
@@ -659,6 +1630,114 @@ pointer_array_find_non_empty (void)
 }
 
 static void
+steal_destroy_notify (gpointer data)
+{
+  guint *counter = data;
+  *counter = *counter + 1;
+}
+
+/* Test that g_ptr_array_steal_index() and g_ptr_array_steal_index_fast() can
+ * remove elements from a pointer array without the #GDestroyNotify being called. */
+static void
+pointer_array_steal_index (void)
+{
+  guint i1 = 0, i2 = 0, i3 = 0, i4 = 0;
+  gpointer out1, out2;
+  GPtrArray *array = g_ptr_array_new_with_free_func (steal_destroy_notify);
+
+  g_ptr_array_add (array, &i1);
+  g_ptr_array_add (array, &i2);
+  g_ptr_array_add (array, &i3);
+  g_ptr_array_add (array, &i4);
+
+  g_assert_cmpuint (array->len, ==, 4);
+
+  /* Remove a single element. */
+  out1 = g_ptr_array_steal_index (array, 0);
+  g_assert_true (out1 == &i1);
+  g_assert_cmpuint (i1, ==, 0);  /* should not have been destroyed */
+
+  /* Following elements should have been moved down. */
+  g_assert_cmpuint (array->len, ==, 3);
+  g_assert_true (g_ptr_array_index (array, 0) == &i2);
+  g_assert_true (g_ptr_array_index (array, 1) == &i3);
+  g_assert_true (g_ptr_array_index (array, 2) == &i4);
+
+  /* Remove another element, quickly. */
+  out2 = g_ptr_array_steal_index_fast (array, 0);
+  g_assert_true (out2 == &i2);
+  g_assert_cmpuint (i2, ==, 0);  /* should not have been destroyed */
+
+  /* Last element should have been swapped in place. */
+  g_assert_cmpuint (array->len, ==, 2);
+  g_assert_true (g_ptr_array_index (array, 0) == &i4);
+  g_assert_true (g_ptr_array_index (array, 1) == &i3);
+
+  /* Check that destroying the pointer array doesn’t affect the stolen elements. */
+  g_ptr_array_unref (array);
+
+  g_assert_cmpuint (i1, ==, 0);
+  g_assert_cmpuint (i2, ==, 0);
+  g_assert_cmpuint (i3, ==, 1);
+  g_assert_cmpuint (i4, ==, 1);
+}
+
+static void
+byte_array_new_take_overflow (void)
+{
+#if SIZE_WIDTH <= UINT_WIDTH
+  g_test_skip ("Overflow test requires G_MAXSIZE > G_MAXUINT.");
+#else
+  GByteArray* arr;
+
+  if (!g_test_undefined ())
+      return;
+
+  /* Check for overflow should happen before data is accessed. */
+  g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
+                          "*assertion 'len <= G_MAXUINT' failed");
+  arr = g_byte_array_new_take (NULL, (gsize)G_MAXUINT + 1);
+  g_assert_null (arr);
+  g_test_assert_expected_messages ();
+#endif
+}
+
+static void
+byte_array_steal (void)
+{
+  const guint array_size = 10000;
+  GByteArray *gbarray;
+  guint8 *bdata;
+  guint i;
+  gsize len, past_len;
+
+  gbarray = g_byte_array_new ();
+  bdata = g_byte_array_steal (gbarray, NULL);
+  g_assert_cmpint ((gsize) bdata, ==, (gsize) gbarray->data);
+  g_free (bdata);
+
+  for (i = 0; i < array_size; i++)
+    g_byte_array_append (gbarray, (guint8 *) "abcd", 4);
+
+  past_len = gbarray->len;
+  bdata = g_byte_array_steal (gbarray, &len);
+
+  g_assert_cmpint (len, ==, past_len);
+  g_assert_cmpint (gbarray->len, ==, 0);
+
+  g_byte_array_append (gbarray, (guint8 *) "@", 1);
+
+  g_assert_cmpint (bdata[0], ==, 'a');
+  g_assert_cmpint (gbarray->data[0], ==, '@');
+  g_assert_cmpint (gbarray->len, ==, 1);
+
+  g_byte_array_remove_index (gbarray, 0);
+
+  g_free (bdata);
+  g_byte_array_free (gbarray, TRUE);
+}
+
+static void
 byte_array_append (void)
 {
   GByteArray *gbarray;
@@ -855,7 +1934,7 @@ static void
 byte_array_sort (void)
 {
   GByteArray *gbarray;
-  gint i;
+  guint i;
   guint8 val;
   guint8 prev, cur;
 
@@ -883,7 +1962,7 @@ static void
 byte_array_sort_with_data (void)
 {
   GByteArray *gbarray;
-  gint i;
+  guint i;
   guint8 val;
   guint8 prev, cur;
 
@@ -913,7 +1992,7 @@ byte_array_new_take (void)
   GByteArray *gbarray;
   guint8 *data;
 
-  data = g_memdup ("woooweeewow", 11);
+  data = g_memdup2 ("woooweeewow", 11);
   gbarray = g_byte_array_new_take (data, 11);
   g_assert (gbarray->data == data);
   g_assert_cmpuint (gbarray->len, ==, 11);
@@ -940,38 +2019,82 @@ byte_array_free_to_bytes (void)
 
   g_bytes_unref (bytes);
 }
+
+static void
+add_array_test (const gchar         *test_path,
+                const ArrayTestData *config,
+                GTestDataFunc        test_func)
+{
+  gchar *test_name = NULL;
+
+  test_name = g_strdup_printf ("%s/%s-%s",
+                               test_path,
+                               config->zero_terminated ? "zero-terminated" : "non-zero-terminated",
+                               config->clear_ ? "clear" : "no-clear");
+  g_test_add_data_func (test_name, config, test_func);
+  g_free (test_name);
+}
+
 int
 main (int argc, char *argv[])
 {
-  g_test_init (&argc, &argv, NULL);
+  /* Test all possible combinations of g_array_new() parameters. */
+  const ArrayTestData array_configurations[] =
+    {
+      { FALSE, FALSE },
+      { FALSE, TRUE },
+      { TRUE, FALSE },
+      { TRUE, TRUE },
+    };
+  gsize i;
 
-  g_test_bug_base ("https://bugzilla.gnome.org/");
+  g_test_init (&argc, &argv, NULL);
 
   /* array tests */
-  g_test_add_func ("/array/new/cleared", array_new_cleared);
-  g_test_add_func ("/array/new/sized-cleared", array_new_sized_cleared);
   g_test_add_func ("/array/new/zero-terminated", array_new_zero_terminated);
-  g_test_add_func ("/array/append", array_append);
-  g_test_add_func ("/array/prepend", array_prepend);
-  g_test_add_func ("/array/remove", array_remove);
-  g_test_add_func ("/array/remove-fast", array_remove_fast);
-  g_test_add_func ("/array/remove-range", array_remove_range);
   g_test_add_func ("/array/ref-count", array_ref_count);
-  g_test_add_func ("/array/sort", array_sort);
-  g_test_add_func ("/array/sort-with-data", array_sort_with_data);
+  g_test_add_func ("/array/steal", array_steal);
   g_test_add_func ("/array/clear-func", array_clear_func);
+  g_test_add_func ("/array/binary-search", test_array_binary_search);
+  g_test_add_func ("/array/copy-sized", test_array_copy_sized);
+  g_test_add_func ("/array/overflow-append-vals", array_overflow_append_vals);
+  g_test_add_func ("/array/overflow-set-size", array_overflow_set_size);
+
+  for (i = 0; i < G_N_ELEMENTS (array_configurations); i++)
+    {
+      add_array_test ("/array/set-size", &array_configurations[i], array_set_size);
+      add_array_test ("/array/set-size/sized", &array_configurations[i], array_set_size_sized);
+      add_array_test ("/array/append-val", &array_configurations[i], array_append_val);
+      add_array_test ("/array/prepend-val", &array_configurations[i], array_prepend_val);
+      add_array_test ("/array/prepend-vals", &array_configurations[i], array_prepend_vals);
+      add_array_test ("/array/insert-vals", &array_configurations[i], array_insert_vals);
+      add_array_test ("/array/remove-index", &array_configurations[i], array_remove_index);
+      add_array_test ("/array/remove-index-fast", &array_configurations[i], array_remove_index_fast);
+      add_array_test ("/array/remove-range", &array_configurations[i], array_remove_range);
+      add_array_test ("/array/copy", &array_configurations[i], array_copy);
+      add_array_test ("/array/sort", &array_configurations[i], array_sort);
+      add_array_test ("/array/sort-with-data", &array_configurations[i], array_sort_with_data);
+    }
 
   /* pointer arrays */
   g_test_add_func ("/pointerarray/add", pointer_array_add);
   g_test_add_func ("/pointerarray/insert", pointer_array_insert);
   g_test_add_func ("/pointerarray/ref-count", pointer_array_ref_count);
   g_test_add_func ("/pointerarray/free-func", pointer_array_free_func);
+  g_test_add_func ("/pointerarray/array_copy", pointer_array_copy);
+  g_test_add_func ("/pointerarray/array_extend", pointer_array_extend);
+  g_test_add_func ("/pointerarray/array_extend_and_steal", pointer_array_extend_and_steal);
   g_test_add_func ("/pointerarray/sort", pointer_array_sort);
+  g_test_add_func ("/pointerarray/sort/example", pointer_array_sort_example);
   g_test_add_func ("/pointerarray/sort-with-data", pointer_array_sort_with_data);
+  g_test_add_func ("/pointerarray/sort-with-data/example", pointer_array_sort_with_data_example);
   g_test_add_func ("/pointerarray/find/empty", pointer_array_find_empty);
   g_test_add_func ("/pointerarray/find/non-empty", pointer_array_find_non_empty);
+  g_test_add_func ("/pointerarray/steal", pointer_array_steal);
+  g_test_add_func ("/pointerarray/steal_index", pointer_array_steal_index);
 
   /* byte arrays */
+  g_test_add_func ("/bytearray/steal", byte_array_steal);
   g_test_add_func ("/bytearray/append", byte_array_append);
   g_test_add_func ("/bytearray/prepend", byte_array_prepend);
   g_test_add_func ("/bytearray/remove", byte_array_remove);
@@ -981,8 +2104,8 @@ main (int argc, char *argv[])
   g_test_add_func ("/bytearray/sort", byte_array_sort);
   g_test_add_func ("/bytearray/sort-with-data", byte_array_sort_with_data);
   g_test_add_func ("/bytearray/new-take", byte_array_new_take);
+  g_test_add_func ("/bytearray/new-take-overflow", byte_array_new_take_overflow);
   g_test_add_func ("/bytearray/free-to-bytes", byte_array_free_to_bytes);
 
   return g_test_run ();
 }
-