Improve test coverage in gobject/

Lines:          6631    8862    74.8 %
Functions:      747     893     83.7 %
This commit is contained in:
Matthias Clasen 2013-05-29 00:15:55 -04:00
parent 3382ac99be
commit a9abbb3192
4 changed files with 300 additions and 1 deletions

View File

@ -1,4 +1,5 @@
include $(top_srcdir)/Makefile.decl
NULL =
AM_CPPFLAGS = \
-g \
@ -29,7 +30,9 @@ TEST_PROGS += \
properties \
reference \
ifaceproperties \
valuearray
valuearray \
type \
$(NULL)
signals_SOURCES = signals.c marshalers.c

View File

@ -504,6 +504,117 @@ test_boxed_variantbuilder (void)
g_value_unset (&value);
}
static void
test_boxed_timezone (void)
{
GTimeZone *z, *z2;
GValue value = G_VALUE_INIT;
g_value_init (&value, G_TYPE_TIME_ZONE);
g_assert (G_VALUE_HOLDS_BOXED (&value));
z = g_time_zone_new_utc ();
g_value_take_boxed (&value, z);
z2 = g_value_get_boxed (&value);
g_assert (z == z2);
z2 = g_value_dup_boxed (&value);
g_assert (z == z2); /* timezone uses ref/unref for copy/free */
g_time_zone_unref (z2);
g_value_unset (&value);
}
static void
test_boxed_pollfd (void)
{
GPollFD *p, *p2;
GValue value = G_VALUE_INIT;
g_value_init (&value, G_TYPE_POLLFD);
g_assert (G_VALUE_HOLDS_BOXED (&value));
p = g_new (GPollFD, 1);
g_value_take_boxed (&value, p);
p2 = g_value_get_boxed (&value);
g_assert (p == p2);
p2 = g_value_dup_boxed (&value);
g_assert (p != p2);
g_free (p2);
g_value_unset (&value);
}
static void
test_boxed_markup (void)
{
GMarkupParseContext *c, *c2;
const GMarkupParser parser = { 0 };
GValue value = G_VALUE_INIT;
g_value_init (&value, G_TYPE_MARKUP_PARSE_CONTEXT);
g_assert (G_VALUE_HOLDS_BOXED (&value));
c = g_markup_parse_context_new (&parser, 0, NULL, NULL);
g_value_take_boxed (&value, c);
c2 = g_value_get_boxed (&value);
g_assert (c == c2);
c2 = g_value_dup_boxed (&value);
g_assert (c == c2);
g_markup_parse_context_unref (c2);
g_value_unset (&value);
}
static void
test_boxed_thread (void)
{
GThread *t, *t2;
GValue value = G_VALUE_INIT;
g_value_init (&value, G_TYPE_THREAD);
g_assert (G_VALUE_HOLDS_BOXED (&value));
t = g_thread_self ();
g_value_take_boxed (&value, t);
t2 = g_value_get_boxed (&value);
g_assert (t == t2);
t2 = g_value_dup_boxed (&value);
g_assert (t == t2);
g_thread_unref (t2);
g_value_unset (&value);
}
static void
test_boxed_checksum (void)
{
GChecksum *c, *c2;
GValue value = G_VALUE_INIT;
g_value_init (&value, G_TYPE_CHECKSUM);
g_assert (G_VALUE_HOLDS_BOXED (&value));
c = g_checksum_new (G_CHECKSUM_SHA512);
g_value_take_boxed (&value, c);
c2 = g_value_get_boxed (&value);
g_assert (c == c2);
c2 = g_value_dup_boxed (&value);
g_assert (c != c2);
g_checksum_free (c2);
g_value_unset (&value);
}
int
main (int argc, char *argv[])
{
@ -528,6 +639,11 @@ main (int argc, char *argv[])
g_test_add_func ("/boxed/maincontext", test_boxed_maincontext);
g_test_add_func ("/boxed/source", test_boxed_source);
g_test_add_func ("/boxed/variantbuilder", test_boxed_variantbuilder);
g_test_add_func ("/boxed/timezone", test_boxed_timezone);
g_test_add_func ("/boxed/pollfd", test_boxed_pollfd);
g_test_add_func ("/boxed/markup", test_boxed_markup);
g_test_add_func ("/boxed/thread", test_boxed_thread);
g_test_add_func ("/boxed/checksum", test_boxed_checksum);
return g_test_run ();
}

View File

@ -78,6 +78,33 @@ test_qdata_threaded (void)
result = GPOINTER_TO_INT (g_object_get_data (object, "test"));
g_assert_cmpint (sum, ==, result);
g_object_unref (object);
}
static void
test_qdata_dup (void)
{
gchar *s, *s2;
GQuark quark;
gboolean b;
quark = g_quark_from_static_string ("test");
object = g_object_new (G_TYPE_OBJECT, NULL);
s = g_strdup ("s");
g_object_set_qdata_full (object, quark, s, g_free);
s2 = g_object_dup_qdata (object, quark, (GDuplicateFunc)g_strdup, NULL);
g_assert_cmpstr (s, ==, s2);
g_assert (s != s2);
g_free (s2);
b = g_object_replace_qdata (object, quark, s, "s2", NULL, NULL);
g_assert (b);
g_object_unref (object);
}
int
@ -88,6 +115,7 @@ main (int argc, char **argv)
fail = !!g_getenv ("FAIL");
g_test_add_func ("/qdata/threaded", test_qdata_threaded);
g_test_add_func ("/qdata/dup", test_qdata_dup);
return g_test_run ();
}

152
gobject/tests/type.c Normal file
View File

@ -0,0 +1,152 @@
#include <glib-object.h>
static void
test_registration_serial (void)
{
gint serial1, serial2, serial3;
serial1 = g_type_get_type_registration_serial ();
g_pointer_type_register_static ("my+pointer");
serial2 = g_type_get_type_registration_serial ();
g_assert (serial1 != serial2);
serial3 = g_type_get_type_registration_serial ();
g_assert (serial2 == serial3);
}
typedef struct {
GTypeInterface g_iface;
} BarInterface;
GType bar_get_type (void);
G_DEFINE_INTERFACE (Bar, bar, G_TYPE_OBJECT)
static void
bar_default_init (BarInterface *iface)
{
}
typedef struct {
GTypeInterface g_iface;
} FooInterface;
GType foo_get_type (void);
G_DEFINE_INTERFACE_WITH_CODE (Foo, foo, G_TYPE_OBJECT,
g_type_interface_add_prerequisite (g_define_type_id, bar_get_type ());)
static void
foo_default_init (FooInterface *iface)
{
}
static void
test_interface_prerequisite (void)
{
GType *prereqs;
guint n_prereqs;
gpointer iface;
gpointer parent;
prereqs = g_type_interface_prerequisites (foo_get_type (), &n_prereqs);
g_assert_cmpint (n_prereqs, ==, 2);
g_assert (prereqs[0] == bar_get_type ());
g_assert (prereqs[1] == G_TYPE_OBJECT);
iface = g_type_default_interface_ref (foo_get_type ());
parent = g_type_interface_peek_parent (iface);
g_assert (parent == NULL);
g_type_default_interface_unref (iface);
g_free (prereqs);
}
typedef struct {
GTypeInterface g_iface;
} BazInterface;
GType baz_get_type (void);
G_DEFINE_INTERFACE (Baz, baz, G_TYPE_OBJECT)
static void
baz_default_init (BazInterface *iface)
{
}
typedef struct {
GObject parent;
} Bazo;
typedef struct {
GObjectClass parent_class;
} BazoClass;
GType bazo_get_type (void);
static void bazo_iface_init (BazInterface *i);
G_DEFINE_TYPE_WITH_CODE (Bazo, bazo, G_TYPE_INITIALLY_UNOWNED,
G_IMPLEMENT_INTERFACE (baz_get_type (),
bazo_iface_init);)
static void
bazo_init (Bazo *b)
{
}
static void
bazo_class_init (BazoClass *c)
{
}
static void
bazo_iface_init (BazInterface *i)
{
}
static gint check_called;
static void
check_func (gpointer check_data,
gpointer g_iface)
{
g_assert (check_data == &check_called);
check_called++;
}
static void
test_interface_check (void)
{
GObject *o;
check_called = 0;
g_type_add_interface_check (&check_called, check_func);
o = g_object_new (bazo_get_type (), NULL);
g_object_unref (o);
g_assert_cmpint (check_called, ==, 1);
g_type_remove_interface_check (&check_called, check_func);
}
static void
test_next_base (void)
{
GType type;
type = g_type_next_base (bazo_get_type (), G_TYPE_OBJECT);
g_assert (type == G_TYPE_INITIALLY_UNOWNED);
}
int
main (int argc, char *argv[])
{
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/type/registration-serial", test_registration_serial);
g_test_add_func ("/type/interface-prerequisite", test_interface_prerequisite);
g_test_add_func ("/type/interface-check", test_interface_check);
g_test_add_func ("/type/next-base", test_next_base);
return g_test_run ();
}