2012-02-20 17:41:30 +01:00
|
|
|
#define GLIB_VERSION_MIN_REQUIRED GLIB_VERSION_2_30
|
2011-06-21 06:54:00 +02:00
|
|
|
#include <glib-object.h>
|
|
|
|
|
2015-10-02 16:06:22 +02:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2011-06-21 06:54:00 +02:00
|
|
|
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
|
2015-10-02 16:06:22 +02:00
|
|
|
test_valuearray_basic (void)
|
2011-06-21 06:54:00 +02:00
|
|
|
{
|
|
|
|
GValueArray *a;
|
|
|
|
GValueArray *a2;
|
2011-09-30 18:19:50 +02:00
|
|
|
GValue v = G_VALUE_INIT;
|
2011-06-21 06:54:00 +02:00
|
|
|
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);
|
|
|
|
|
2015-10-02 16:06:22 +02:00
|
|
|
g_test_add_func ("/value/basic", test_value_basic);
|
|
|
|
g_test_add_func ("/value/array/basic", test_valuearray_basic);
|
2011-06-21 06:54:00 +02:00
|
|
|
|
|
|
|
return g_test_run ();
|
|
|
|
}
|