glib/gobject/tests/valuearray.c
Emmanuele Bassi 0ac9ab4e27 Deprecate GValueArray
The GValueArray type was added in a time, during the Jurassic era or so,
when GArray did not have a representable GType. The GValueArray API has
various issues as well:

  - it doesn't match the other GLib array types;
  - it is not reference counted;
  - the structure is fully exposed on the stack, so it cannot be
    extended to add reference counting;
  - it cannot be forcibly resized.

The nice thing is that now we have a GArray type that can replace in
full GValueArray, so we can deprecate the latter, and reduce the
complexity in GLib, application code, and bindings.

https://bugzilla.gnome.org/show_bug.cgi?id=667228
2012-01-24 23:37:24 -05:00

67 lines
1.4 KiB
C

#define GLIB_DISABLE_DEPRECATION_WARNINGS
#include <glib-object.h>
static gint
cmpint (gconstpointer a, gconstpointer b)
{
const GValue *aa = a;
const GValue *bb = b;
return g_value_get_int (aa) - g_value_get_int (bb);
}
static void
test_basic (void)
{
GValueArray *a;
GValueArray *a2;
GValue v = G_VALUE_INIT;
GValue *p;
gint i;
a = g_value_array_new (20);
g_value_init (&v, G_TYPE_INT);
for (i = 0; i < 100; i++)
{
g_value_set_int (&v, i);
g_value_array_append (a, &v);
}
g_assert_cmpint (a->n_values, ==, 100);
p = g_value_array_get_nth (a, 5);
g_assert_cmpint (g_value_get_int (p), ==, 5);
for (i = 20; i < 100; i+= 5)
g_value_array_remove (a, 100 - i);
for (i = 100; i < 150; i++)
{
g_value_set_int (&v, i);
g_value_array_prepend (a, &v);
}
g_value_array_sort (a, cmpint);
for (i = 0; i < a->n_values - 1; i++)
g_assert_cmpint (g_value_get_int (&a->values[i]), <=, g_value_get_int (&a->values[i+1]));
a2 = g_value_array_copy (a);
for (i = 0; i < a->n_values; i++)
g_assert_cmpint (g_value_get_int (&a->values[i]), ==, g_value_get_int (&a2->values[i]));
g_value_array_free (a);
g_value_array_free (a2);
}
int
main (int argc, char *argv[])
{
g_type_init ();
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/valuearray/basic", test_basic);
return g_test_run ();
}