glib/gobject/tests/value.c
Dan Winship 4b2d92a864 Allow passing unset GValues to g_value_unset()
This makes it more useful as an autocleanup func.

Also, add a minimal test of g_value_init/g_value_reset/g_value_unset.

https://bugzilla.gnome.org/show_bug.cgi?id=755766
2015-10-02 10:07:53 -04:00

96 lines
2.3 KiB
C

#define GLIB_VERSION_MIN_REQUIRED GLIB_VERSION_2_30
#include <glib-object.h>
static void
test_value_basic (void)
{
GValue value = G_VALUE_INIT;
g_assert_false (G_IS_VALUE (&value));
g_assert_false (G_VALUE_HOLDS_INT (&value));
g_value_unset (&value);
g_assert_false (G_IS_VALUE (&value));
g_assert_false (G_VALUE_HOLDS_INT (&value));
g_value_init (&value, G_TYPE_INT);
g_assert_true (G_IS_VALUE (&value));
g_assert_true (G_VALUE_HOLDS_INT (&value));
g_assert_false (G_VALUE_HOLDS_UINT (&value));
g_assert_cmpint (g_value_get_int (&value), ==, 0);
g_value_set_int (&value, 10);
g_assert_cmpint (g_value_get_int (&value), ==, 10);
g_value_reset (&value);
g_assert_true (G_IS_VALUE (&value));
g_assert_true (G_VALUE_HOLDS_INT (&value));
g_assert_cmpint (g_value_get_int (&value), ==, 0);
g_value_unset (&value);
g_assert_false (G_IS_VALUE (&value));
g_assert_false (G_VALUE_HOLDS_INT (&value));
}
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_valuearray_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_test_init (&argc, &argv, NULL);
g_test_add_func ("/value/basic", test_value_basic);
g_test_add_func ("/value/array/basic", test_valuearray_basic);
return g_test_run ();
}