Merge branch 'move_gobject_tests' into 'main'

Moving gobject tests from tests/gobjects to gobjects/tests

See merge request GNOME/glib!2550
This commit is contained in:
Philip Withnall 2022-05-26 18:37:19 +00:00
commit 81fd75b4bb
14 changed files with 406 additions and 425 deletions

View File

@ -15,18 +15,9 @@
* Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "TestAccumulator"
#include <glib-object.h>
#undef G_DISABLE_ASSERT
#undef G_DISABLE_CHECKS
#undef G_DISABLE_CAST_CHECKS
#include <string.h>
#include <glib-object.h>
#include "testmarshal.h"
#include "marshalers.h"
#include "testcommon.h"
/* What this test tests is the behavior of signal accumulators
@ -37,9 +28,7 @@
* emission on TRUE returns.
*/
/*
* TestObject, a parent class for TestObject
*/
/* TestObject, a parent class for TestObject */
#define TEST_TYPE_OBJECT (test_object_get_type ())
typedef struct _TestObject TestObject;
typedef struct _TestObjectClass TestObjectClass;
@ -53,9 +42,9 @@ struct _TestObjectClass
GObjectClass parent_class;
gchar* (*test_signal1) (TestObject *tobject,
gint param);
gint param);
gboolean (*test_signal2) (TestObject *tobject,
gint param);
gint param);
GVariant* (*test_signal3) (TestObject *tobject,
gboolean *weak_ptr);
};
@ -64,9 +53,9 @@ static GType test_object_get_type (void);
static gboolean
test_signal1_accumulator (GSignalInvocationHint *ihint,
GValue *return_accu,
const GValue *handler_return,
gpointer data)
GValue *return_accu,
const GValue *handler_return,
gpointer data)
{
const gchar *accu_string = g_value_get_string (return_accu);
const gchar *new_string = g_value_get_string (handler_return);
@ -79,37 +68,37 @@ test_signal1_accumulator (GSignalInvocationHint *ihint,
else
result_string = NULL;
g_value_set_string_take_ownership (return_accu, result_string);
g_value_take_string (return_accu, result_string);
return TRUE;
}
static gchar *
test_object_signal1_callback_before (TestObject *tobject,
gint param,
gpointer data)
gint param,
gpointer data)
{
return g_strdup ("<before>");
}
static gchar *
test_object_real_signal1 (TestObject *tobject,
gint param)
gint param)
{
return g_strdup ("<default>");
}
static gchar *
test_object_signal1_callback_after (TestObject *tobject,
gint param,
gpointer data)
gint param,
gpointer data)
{
return g_strdup ("<after>");
}
static gboolean
test_object_signal2_callback_before (TestObject *tobject,
gint param)
gint param)
{
switch (param)
{
@ -125,7 +114,7 @@ test_object_signal2_callback_before (TestObject *tobject,
static gboolean
test_object_real_signal2 (TestObject *tobject,
gint param)
gint param)
{
switch (param)
{
@ -134,14 +123,14 @@ test_object_real_signal2 (TestObject *tobject,
case 3: return FALSE;
case 4: return FALSE;
}
g_assert_not_reached ();
return FALSE;
}
static gboolean
test_object_signal2_callback_after (TestObject *tobject,
gint param)
gint param)
{
switch (param)
{
@ -150,21 +139,21 @@ test_object_signal2_callback_after (TestObject *tobject,
case 3: return TRUE;
case 4: return FALSE;
}
g_assert_not_reached ();
return FALSE;
}
static gboolean
test_signal3_accumulator (GSignalInvocationHint *ihint,
GValue *return_accu,
const GValue *handler_return,
gpointer data)
GValue *return_accu,
const GValue *handler_return,
gpointer data)
{
GVariant *variant;
variant = g_value_get_variant (handler_return);
g_assert (!g_variant_is_floating (variant));
g_assert_false (g_variant_is_floating (variant));
g_value_set_variant (return_accu, variant);
@ -219,89 +208,97 @@ test_object_class_init (TestObjectClass *class)
class->test_signal1 = test_object_real_signal1;
class->test_signal2 = test_object_real_signal2;
class->test_signal3 = test_object_real_signal3;
g_signal_new ("test-signal1",
G_OBJECT_CLASS_TYPE (class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (TestObjectClass, test_signal1),
test_signal1_accumulator, NULL,
test_marshal_STRING__INT,
G_TYPE_STRING, 1, G_TYPE_INT);
G_OBJECT_CLASS_TYPE (class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (TestObjectClass, test_signal1),
test_signal1_accumulator, NULL,
test_STRING__INT,
G_TYPE_STRING, 1, G_TYPE_INT);
g_signal_new ("test-signal2",
G_OBJECT_CLASS_TYPE (class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (TestObjectClass, test_signal2),
g_signal_accumulator_true_handled, NULL,
test_marshal_BOOLEAN__INT,
G_TYPE_BOOLEAN, 1, G_TYPE_INT);
G_OBJECT_CLASS_TYPE (class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (TestObjectClass, test_signal2),
g_signal_accumulator_true_handled, NULL,
test_BOOLEAN__INT,
G_TYPE_BOOLEAN, 1, G_TYPE_INT);
g_signal_new ("test-signal3",
G_OBJECT_CLASS_TYPE (class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (TestObjectClass, test_signal3),
test_signal3_accumulator, NULL,
test_marshal_VARIANT__POINTER,
G_TYPE_VARIANT, 1, G_TYPE_POINTER);
G_OBJECT_CLASS_TYPE (class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (TestObjectClass, test_signal3),
test_signal3_accumulator, NULL,
test_VARIANT__POINTER,
G_TYPE_VARIANT, 1, G_TYPE_POINTER);
}
static DEFINE_TYPE(TestObject, test_object,
test_object_class_init, NULL, NULL,
G_TYPE_OBJECT)
test_object_class_init, NULL, NULL,
G_TYPE_OBJECT);
int
main (int argc,
char *argv[])
static void
test_accumulator (void)
{
TestObject *object;
gchar *string_result;
gboolean bool_result;
gboolean variant_finalised;
GVariant *variant_result;
g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
G_LOG_LEVEL_WARNING |
G_LOG_LEVEL_CRITICAL);
object = g_object_new (TEST_TYPE_OBJECT, NULL);
g_signal_connect (object, "test-signal1",
G_CALLBACK (test_object_signal1_callback_before), NULL);
G_CALLBACK (test_object_signal1_callback_before), NULL);
g_signal_connect_after (object, "test-signal1",
G_CALLBACK (test_object_signal1_callback_after), NULL);
G_CALLBACK (test_object_signal1_callback_after), NULL);
g_signal_emit_by_name (object, "test-signal1", 0, &string_result);
g_assert (strcmp (string_result, "<before><default><after>") == 0);
g_assert_cmpstr (string_result, ==, "<before><default><after>");
g_free (string_result);
g_signal_connect (object, "test-signal2",
G_CALLBACK (test_object_signal2_callback_before), NULL);
G_CALLBACK (test_object_signal2_callback_before), NULL);
g_signal_connect_after (object, "test-signal2",
G_CALLBACK (test_object_signal2_callback_after), NULL);
G_CALLBACK (test_object_signal2_callback_after), NULL);
bool_result = FALSE;
g_signal_emit_by_name (object, "test-signal2", 1, &bool_result);
g_assert (bool_result == TRUE);
g_assert_true (bool_result);
bool_result = FALSE;
g_signal_emit_by_name (object, "test-signal2", 2, &bool_result);
g_assert (bool_result == TRUE);
g_assert_true (bool_result);
bool_result = FALSE;
g_signal_emit_by_name (object, "test-signal2", 3, &bool_result);
g_assert (bool_result == TRUE);
g_assert_true (bool_result);
bool_result = TRUE;
g_signal_emit_by_name (object, "test-signal2", 4, &bool_result);
g_assert (bool_result == FALSE);
g_assert_false (bool_result);
variant_finalised = FALSE;
variant_result = NULL;
g_signal_emit_by_name (object, "test-signal3", &variant_finalised, &variant_result);
g_assert (variant_result != NULL);
g_assert (!g_variant_is_floating (variant_result));
g_assert_nonnull (variant_result);
g_assert_false (g_variant_is_floating (variant_result));
/* Test that variant_result had refcount 1 */
g_assert (!variant_finalised);
g_assert_false (variant_finalised);
g_variant_unref (variant_result);
g_assert (variant_finalised);
g_assert_true (variant_finalised);
g_object_unref (object);
return 0;
}
int
main (int argc,
char *argv[])
{
g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
G_LOG_LEVEL_WARNING |
G_LOG_LEVEL_CRITICAL);
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/gobject/accumulator", test_accumulator);
return g_test_run ();
}

View File

@ -16,13 +16,6 @@
* Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "TestSignals"
#undef G_DISABLE_ASSERT
#undef G_DISABLE_CHECKS
#undef G_DISABLE_CAST_CHECKS
#include <glib-object.h>
#include "testcommon.h"
@ -37,18 +30,19 @@
* from an earlier handler in the same emission will not be run.
*/
/*
* TestObject, a parent class for TestObject
*/
/* TestObject, a parent class for TestObject */
#define TEST_TYPE_OBJECT (test_object_get_type ())
typedef struct _TestObject TestObject;
typedef struct _TestObjectClass TestObjectClass;
static gboolean callback1_ran = FALSE, callback2_ran = FALSE, callback3_ran = FALSE, default_handler_ran = FALSE;
static gboolean callback1_ran = FALSE, callback2_ran = FALSE,
callback3_ran = FALSE, default_handler_ran = FALSE;
struct _TestObject
{
GObject parent_instance;
};
struct _TestObjectClass
{
GObjectClass parent_class;
@ -106,16 +100,11 @@ static DEFINE_TYPE(TestObject, test_object,
test_object_class_init, NULL, NULL,
G_TYPE_OBJECT)
int
main (int argc,
char *argv[])
static void
test_basic_signals (void)
{
TestObject *object;
g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
G_LOG_LEVEL_WARNING |
G_LOG_LEVEL_CRITICAL);
object = g_object_new (TEST_TYPE_OBJECT, NULL);
g_signal_connect (object, "test-signal",
@ -124,11 +113,25 @@ main (int argc,
G_CALLBACK (test_object_signal_callback2), NULL);
g_signal_emit_by_name (object, "test-signal");
g_assert (callback1_ran);
g_assert (!callback2_ran);
g_assert (!callback3_ran);
g_assert (default_handler_ran);
g_assert_true (callback1_ran);
g_assert_false (callback2_ran);
g_assert_false (callback3_ran);
g_assert_true (default_handler_ran);
g_object_unref (object);
return 0;
}
int
main (int argc,
char *argv[])
{
g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
G_LOG_LEVEL_WARNING |
G_LOG_LEVEL_CRITICAL);
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/gobject/basic-signals", test_basic_signals);
return g_test_run ();
}

View File

@ -15,13 +15,6 @@
* Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "TestDefaultIface"
#undef G_DISABLE_ASSERT
#undef G_DISABLE_CHECKS
#undef G_DISABLE_CAST_CHECKS
#include <glib-object.h>
#include "testcommon.h"
@ -56,7 +49,7 @@ test_static_iface_default_init (TestStaticIfaceClass *iface)
}
DEFINE_IFACE (TestStaticIface, test_static_iface,
NULL, test_static_iface_default_init)
NULL, test_static_iface_default_init)
static void
test_static_iface (void)
@ -65,22 +58,25 @@ test_static_iface (void)
/* Not loaded until we call ref for the first time */
static_iface = g_type_default_interface_peek (TEST_TYPE_STATIC_IFACE);
g_assert (static_iface == NULL);
g_assert_null (static_iface);
/* Ref loads */
static_iface = g_type_default_interface_ref (TEST_TYPE_STATIC_IFACE);
g_assert (static_iface && static_iface->val == 42);
g_assert_nonnull (static_iface);
g_assert_cmpint (static_iface->val, ==, 42);
/* Peek then works */
static_iface = g_type_default_interface_peek (TEST_TYPE_STATIC_IFACE);
g_assert (static_iface && static_iface->val == 42);
g_assert_nonnull (static_iface);
g_assert_cmpint (static_iface->val, ==, 42);
/* Unref does nothing */
g_type_default_interface_unref (static_iface);
/* And peek still works */
static_iface = g_type_default_interface_peek (TEST_TYPE_STATIC_IFACE);
g_assert (static_iface && static_iface->val == 42);
g_assert_nonnull (static_iface);
g_assert_cmpint (static_iface->val, ==, 42);
}
/**********************************************************************
@ -116,11 +112,11 @@ test_dynamic_iface_default_finalize (TestStaticIfaceClass *iface)
static void
test_dynamic_iface_register (GTypeModule *module)
{
const GTypeInfo iface_info =
{
const GTypeInfo iface_info =
{
sizeof (TestDynamicIfaceClass),
(GBaseInitFunc) NULL,
(GBaseFinalizeFunc) NULL,
(GBaseInitFunc) NULL,
(GBaseFinalizeFunc) NULL,
(GClassInitFunc) test_dynamic_iface_default_init,
(GClassFinalizeFunc) test_dynamic_iface_default_finalize,
NULL,
@ -128,10 +124,11 @@ test_dynamic_iface_register (GTypeModule *module)
0,
NULL,
NULL
};
};
test_dynamic_iface_type = g_type_module_register_type (module, G_TYPE_INTERFACE,
"TestDynamicIface", &iface_info, 0);
test_dynamic_iface_type =
g_type_module_register_type (module, G_TYPE_INTERFACE,
"TestDynamicIface", &iface_info, 0);
}
static void
@ -149,38 +146,47 @@ test_dynamic_iface (void)
/* Not loaded until we call ref for the first time */
dynamic_iface = g_type_default_interface_peek (TEST_TYPE_DYNAMIC_IFACE);
g_assert (dynamic_iface == NULL);
g_assert_null (dynamic_iface);
/* Ref loads */
dynamic_iface = g_type_default_interface_ref (TEST_TYPE_DYNAMIC_IFACE);
g_assert (dynamic_iface_init);
g_assert (dynamic_iface && dynamic_iface->val == 42);
g_assert_true (dynamic_iface_init);
g_assert_nonnull (dynamic_iface);
g_assert_cmpint (dynamic_iface->val, ==, 42);
/* Peek then works */
dynamic_iface = g_type_default_interface_peek (TEST_TYPE_DYNAMIC_IFACE);
g_assert (dynamic_iface && dynamic_iface->val == 42);
g_assert_nonnull (dynamic_iface);
g_assert_cmpint (dynamic_iface->val, ==, 42);
/* Unref causes finalize */
g_type_default_interface_unref (dynamic_iface);
#if 0
g_assert (!dynamic_iface_init);
/* Disabled as unloading dynamic types is disabled.
* See https://gitlab.gnome.org/GNOME/glib/-/issues/667 */
g_assert_false (dynamic_iface_init);
#endif
/* Peek returns NULL */
dynamic_iface = g_type_default_interface_peek (TEST_TYPE_DYNAMIC_IFACE);
#if 0
g_assert (dynamic_iface == NULL);
/* Disabled as unloading dynamic types is disabled.
* See https://gitlab.gnome.org/GNOME/glib/-/issues/667 */
g_assert_null (dynamic_iface);
#endif
/* Ref reloads */
dynamic_iface = g_type_default_interface_ref (TEST_TYPE_DYNAMIC_IFACE);
g_assert (dynamic_iface_init);
g_assert (dynamic_iface && dynamic_iface->val == 42);
g_assert_true (dynamic_iface_init);
g_assert_nonnull (dynamic_iface);
g_assert_cmpint (dynamic_iface->val, ==, 42);
/* And Unref causes finalize once more*/
g_type_default_interface_unref (dynamic_iface);
#if 0
g_assert (!dynamic_iface_init);
/* Disabled as unloading dynamic types is disabled.
* See https://gitlab.gnome.org/GNOME/glib/-/issues/667 */
g_assert_false (dynamic_iface_init);
#endif
}
@ -189,11 +195,13 @@ main (int argc,
char *argv[])
{
g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
G_LOG_LEVEL_WARNING |
G_LOG_LEVEL_CRITICAL);
G_LOG_LEVEL_WARNING |
G_LOG_LEVEL_CRITICAL);
test_static_iface ();
test_dynamic_iface ();
return 0;
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/gobject/static-iface", test_static_iface);
g_test_add_func ("/gobject/dynamic-iface", test_dynamic_iface);
return g_test_run ();
}

View File

@ -15,20 +15,12 @@
* Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "TestDynamicType"
#undef G_DISABLE_ASSERT
#undef G_DISABLE_CHECKS
#undef G_DISABLE_CAST_CHECKS
#include <glib-object.h>
#include "testcommon.h"
#include "testmodule.h"
/* This test tests the macros for defining dynamic types.
*/
/* This test tests the macros for defining dynamic types */
static gboolean loaded = FALSE;
@ -39,8 +31,11 @@ struct _TestIfaceClass
};
static GType test_iface_get_type (void);
#define TEST_TYPE_IFACE (test_iface_get_type ())
#define TEST_IFACE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE, TestIfaceClass))
#define TEST_IFACE_GET_CLASS(obj) \
(G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE, TestIfaceClass))
typedef struct _TestIface TestIface;
typedef struct _TestIfaceClass TestIfaceClass;
@ -51,7 +46,7 @@ static DEFINE_IFACE(TestIface, test_iface, test_iface_base_init, test_iface_defa
static void
test_iface_default_init (TestIfaceClass *iface,
gpointer class_data)
gpointer class_data)
{
}
@ -75,10 +70,10 @@ struct _DynamicObjectClass
static void dynamic_object_iface_init (TestIface *iface);
G_DEFINE_DYNAMIC_TYPE_EXTENDED(DynamicObject, dynamic_object, G_TYPE_OBJECT, 0,
G_IMPLEMENT_INTERFACE_DYNAMIC (TEST_TYPE_IFACE,
dynamic_object_iface_init));
G_IMPLEMENT_INTERFACE_DYNAMIC (TEST_TYPE_IFACE,
dynamic_object_iface_init));
static void
static void
dynamic_object_class_init (DynamicObjectClass *class)
{
class->val = 42;
@ -116,26 +111,28 @@ test_dynamic_type (void)
/* Not loaded until we call ref for the first time */
class = g_type_class_peek (DYNAMIC_OBJECT_TYPE);
g_assert (class == NULL);
g_assert (!loaded);
g_assert_null (class);
g_assert_false (loaded);
/* Make sure interfaces work */
g_assert (g_type_is_a (DYNAMIC_OBJECT_TYPE,
TEST_TYPE_IFACE));
g_assert_true (g_type_is_a (DYNAMIC_OBJECT_TYPE,
TEST_TYPE_IFACE));
/* Ref loads */
class = g_type_class_ref (DYNAMIC_OBJECT_TYPE);
g_assert (class && class->val == 42);
g_assert (loaded);
g_assert_nonnull (class);
g_assert_cmpint (class->val, ==, 42);
g_assert_true (loaded);
/* Peek then works */
class = g_type_class_peek (DYNAMIC_OBJECT_TYPE);
g_assert (class && class->val == 42);
g_assert (loaded);
g_assert_nonnull (class);
g_assert_cmpint (class->val, ==, 42);
g_assert_true (loaded);
/* Make sure interfaces still work */
g_assert (g_type_is_a (DYNAMIC_OBJECT_TYPE,
TEST_TYPE_IFACE));
g_assert_true (g_type_is_a (DYNAMIC_OBJECT_TYPE,
TEST_TYPE_IFACE));
/* Unref causes finalize */
g_type_class_unref (class);
@ -143,21 +140,26 @@ test_dynamic_type (void)
/* Peek returns NULL */
class = g_type_class_peek (DYNAMIC_OBJECT_TYPE);
#if 0
g_assert (!class);
g_assert (!loaded);
/* Disabled as unloading dynamic types is disabled.
* See https://gitlab.gnome.org/GNOME/glib/-/issues/667 */
g_assert_false (class);
g_assert_false (loaded);
#endif
/* Ref reloads */
class = g_type_class_ref (DYNAMIC_OBJECT_TYPE);
g_assert (class && class->val == 42);
g_assert (loaded);
g_assert_nonnull (class);
g_assert_cmpint (class->val, ==, 42);
g_assert_true (loaded);
/* And Unref causes finalize once more*/
g_type_class_unref (class);
class = g_type_class_peek (DYNAMIC_OBJECT_TYPE);
#if 0
g_assert (!class);
g_assert (!loaded);
/* Disabled as unloading dynamic types is disabled.
* See https://gitlab.gnome.org/GNOME/glib/-/issues/667 */
g_assert_null (class);
g_assert_false (loaded);
#endif
}
@ -166,10 +168,12 @@ main (int argc,
char *argv[])
{
g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
G_LOG_LEVEL_WARNING |
G_LOG_LEVEL_CRITICAL);
G_LOG_LEVEL_WARNING |
G_LOG_LEVEL_CRITICAL);
test_dynamic_type ();
return 0;
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/gobject/dynamic-type", test_dynamic_type);
return g_test_run ();
}

View File

@ -1,3 +1,6 @@
VOID:INT,BOOLEAN,CHAR,UCHAR,UINT,LONG,ULONG,ENUM,FLAGS,FLOAT,DOUBLE,STRING,PARAM,BOXED,POINTER,OBJECT,VARIANT,INT64,UINT64
BOOLEAN:INT
INT:VOID
STRING:INT
UINT:VOID
VARIANT:POINTER

View File

@ -28,12 +28,26 @@ marshalers_c = custom_target('marshalers_c',
gobject_tests = {
'qdata' : {},
'accumulator' : {
'source' : ['accumulator.c', marshalers_h, marshalers_c],
},
'boxed' : {},
'cxx' : {
'source' : ['cxx.cpp'],
},
'defaultiface' : {
'source' : ['defaultiface.c', 'testmodule.c'],
},
'deftype' : {},
'dynamictype' : {
'source' : ['dynamictype.c', 'testmodule.c'],
},
'enums' : {},
'override' : {},
'param' : {},
'references' : {},
'basic-signals' : {},
'singleton' : {},
'threadtests' : {},
'dynamictests' : {},
'binding' : {},

View File

@ -17,17 +17,6 @@
* Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "TestOverride"
#undef G_DISABLE_ASSERT
#undef G_DISABLE_CHECKS
#undef G_DISABLE_CAST_CHECKS
#undef VERBOSE
#include <string.h>
#include <glib.h>
#include <glib-object.h>
@ -64,13 +53,13 @@ static void
test_i_default_init (gpointer g_class)
{
foo_signal_id = g_signal_newv ("foo",
TEST_TYPE_I,
G_SIGNAL_RUN_LAST,
g_cclosure_new(G_CALLBACK(test_i_foo),
NULL, NULL),
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0, NULL);
TEST_TYPE_I,
G_SIGNAL_RUN_LAST,
g_cclosure_new(G_CALLBACK(test_i_foo),
NULL, NULL),
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0, NULL);
}
static DEFINE_IFACE (TestI, test_i, NULL, test_i_default_init)
@ -99,7 +88,9 @@ test_a_foo (TestI *self)
g_value_init (&args[0], TEST_TYPE_A);
g_value_set_object (&args[0], self);
g_assert (g_signal_get_invocation_hint (self)->signal_id == foo_signal_id);
g_assert_cmpint (g_signal_get_invocation_hint (self)->signal_id,
==,
foo_signal_id);
g_signal_chain_from_overridden (args, NULL);
g_value_unset (&args[0]);
@ -118,8 +109,8 @@ test_a_baz (TestA *self,
{
record ("TestA::baz");
g_assert (object == G_OBJECT (self));
g_assert (GPOINTER_TO_INT (pointer) == 23);
g_assert_true (object == G_OBJECT (self));
g_assert_cmpint (GPOINTER_TO_INT (pointer), ==, 23);
return g_strdup ("TestA::baz");
}
@ -130,38 +121,39 @@ test_a_class_init (TestAClass *class)
class->bar = test_a_bar;
bar_signal_id = g_signal_new ("bar",
TEST_TYPE_A,
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (TestAClass, bar),
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0, NULL);
TEST_TYPE_A,
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (TestAClass, bar),
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0, NULL);
baz_signal_id = g_signal_new_class_handler ("baz",
TEST_TYPE_A,
G_SIGNAL_RUN_LAST,
G_CALLBACK (test_a_baz),
NULL, NULL,
g_cclosure_marshal_STRING__OBJECT_POINTER,
G_TYPE_STRING, 2,
G_TYPE_OBJECT,
G_TYPE_POINTER);
baz_signal_id =
g_signal_new_class_handler ("baz",
TEST_TYPE_A,
G_SIGNAL_RUN_LAST,
G_CALLBACK (test_a_baz),
NULL, NULL,
g_cclosure_marshal_STRING__OBJECT_POINTER,
G_TYPE_STRING, 2,
G_TYPE_OBJECT,
G_TYPE_POINTER);
}
static void
test_a_interface_init (TestIClass *iface)
{
g_signal_override_class_closure (foo_signal_id,
TEST_TYPE_A,
g_cclosure_new (G_CALLBACK (test_a_foo),
NULL, NULL));
TEST_TYPE_A,
g_cclosure_new (G_CALLBACK (test_a_foo),
NULL, NULL));
}
static DEFINE_TYPE_FULL (TestA, test_a,
test_a_class_init, NULL, NULL,
G_TYPE_OBJECT,
INTERFACE (test_a_interface_init, TEST_TYPE_I))
test_a_class_init, NULL, NULL,
G_TYPE_OBJECT,
INTERFACE (test_a_interface_init, TEST_TYPE_I))
#define TEST_TYPE_B (test_b_get_type())
typedef struct _TestB TestB;
@ -184,7 +176,9 @@ test_b_foo (TestI *self)
g_value_init (&args[0], TEST_TYPE_A);
g_value_set_object (&args[0], self);
g_assert (g_signal_get_invocation_hint (self)->signal_id == foo_signal_id);
g_assert_cmpint (g_signal_get_invocation_hint (self)->signal_id,
==,
foo_signal_id);
g_signal_chain_from_overridden (args, NULL);
g_value_unset (&args[0]);
@ -200,7 +194,9 @@ test_b_bar (TestA *self)
g_value_init (&args[0], TEST_TYPE_A);
g_value_set_object (&args[0], self);
g_assert (g_signal_get_invocation_hint (self)->signal_id == bar_signal_id);
g_assert_cmpint (g_signal_get_invocation_hint (self)->signal_id,
==,
bar_signal_id);
g_signal_chain_from_overridden (args, NULL);
g_value_unset (&args[0]);
@ -215,8 +211,8 @@ test_b_baz (TestA *self,
record ("TestB::baz");
g_assert (object == G_OBJECT (self));
g_assert (GPOINTER_TO_INT (pointer) == 23);
g_assert_true (object == G_OBJECT (self));
g_assert_cmpint (GPOINTER_TO_INT (pointer), ==, 23);
g_signal_chain_from_overridden_handler (self, object, pointer, &retval);
@ -234,21 +230,21 @@ static void
test_b_class_init (TestBClass *class)
{
g_signal_override_class_closure (foo_signal_id,
TEST_TYPE_B,
g_cclosure_new (G_CALLBACK (test_b_foo),
NULL, NULL));
TEST_TYPE_B,
g_cclosure_new (G_CALLBACK (test_b_foo),
NULL, NULL));
g_signal_override_class_closure (bar_signal_id,
TEST_TYPE_B,
g_cclosure_new (G_CALLBACK (test_b_bar),
NULL, NULL));
TEST_TYPE_B,
g_cclosure_new (G_CALLBACK (test_b_bar),
NULL, NULL));
g_signal_override_class_handler ("baz",
TEST_TYPE_B,
G_CALLBACK (test_b_baz));
TEST_TYPE_B,
G_CALLBACK (test_b_baz));
}
static DEFINE_TYPE (TestB, test_b,
test_b_class_init, NULL, NULL,
TEST_TYPE_A)
test_b_class_init, NULL, NULL,
TEST_TYPE_A)
#define TEST_TYPE_C (test_c_get_type())
@ -272,7 +268,9 @@ test_c_foo (TestI *self)
g_value_init (&args[0], TEST_TYPE_A);
g_value_set_object (&args[0], self);
g_assert (g_signal_get_invocation_hint (self)->signal_id == foo_signal_id);
g_assert_cmpint (g_signal_get_invocation_hint (self)->signal_id,
==,
foo_signal_id);
g_signal_chain_from_overridden (args, NULL);
g_value_unset (&args[0]);
@ -288,7 +286,9 @@ test_c_bar (TestA *self)
g_value_init (&args[0], TEST_TYPE_A);
g_value_set_object (&args[0], self);
g_assert (g_signal_get_invocation_hint (self)->signal_id == bar_signal_id);
g_assert_cmpint (g_signal_get_invocation_hint (self)->signal_id,
==,
bar_signal_id);
g_signal_chain_from_overridden (args, NULL);
g_value_unset (&args[0]);
@ -303,8 +303,8 @@ test_c_baz (TestA *self,
record ("TestC::baz");
g_assert (object == G_OBJECT (self));
g_assert (GPOINTER_TO_INT (pointer) == 23);
g_assert_true (object == G_OBJECT (self));
g_assert_cmpint (GPOINTER_TO_INT (pointer), ==, 23);
g_signal_chain_from_overridden_handler (self, object, pointer, &retval);
@ -322,26 +322,25 @@ static void
test_c_class_init (TestBClass *class)
{
g_signal_override_class_closure (foo_signal_id,
TEST_TYPE_C,
g_cclosure_new (G_CALLBACK (test_c_foo),
NULL, NULL));
TEST_TYPE_C,
g_cclosure_new (G_CALLBACK (test_c_foo),
NULL, NULL));
g_signal_override_class_closure (bar_signal_id,
TEST_TYPE_C,
g_cclosure_new (G_CALLBACK (test_c_bar),
NULL, NULL));
TEST_TYPE_C,
g_cclosure_new (G_CALLBACK (test_c_bar),
NULL, NULL));
g_signal_override_class_handler ("baz",
TEST_TYPE_C,
G_CALLBACK (test_c_baz));
TEST_TYPE_C,
G_CALLBACK (test_c_baz));
}
static DEFINE_TYPE (TestC, test_c,
test_c_class_init, NULL, NULL,
TEST_TYPE_B)
test_c_class_init, NULL, NULL,
TEST_TYPE_B)
static GString *test_string = NULL;
gboolean failed = FALSE;
static void
record (const gchar *str)
{
@ -349,7 +348,7 @@ record (const gchar *str)
g_string_append_c (test_string, ',');
g_string_append (test_string, str);
}
static void
test (GType type,
const gchar *signal,
@ -369,39 +368,27 @@ test (GType type,
gchar *ret;
g_signal_emit_by_name (self, signal, self, GINT_TO_POINTER (23), &ret);
if (strcmp (ret, expected_retval) != 0)
failed = TRUE;
g_assert_cmpstr (ret, ==, expected_retval);
g_free (ret);
}
#ifndef VERBOSE
if (strcmp (test_string->str, expected) != 0)
#endif
{
g_printerr ("*** emitting %s on a %s instance\n"
" Expecting: %s\n"
" Got: %s\n",
signal, g_type_name (type),
expected,
test_string->str);
if (strcmp (test_string->str, expected) != 0)
failed = TRUE;
}
g_test_message ("*** emitting %s on a %s instance\n"
" Expecting: %s\n"
" Got: %s",
signal, g_type_name (type),
expected,
test_string->str);
g_assert_cmpstr (test_string->str, ==, expected);
g_string_free (test_string, TRUE);
g_object_unref (self);
}
int
main (int argc, char **argv)
{
g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
G_LOG_LEVEL_WARNING |
G_LOG_LEVEL_CRITICAL);
static void
test_override (void)
{
test (TEST_TYPE_A, "foo", "TestA::foo,TestI::foo", NULL);
test (TEST_TYPE_A, "bar", "TestA::bar", NULL);
test (TEST_TYPE_A, "baz", "TestA::baz", "TestA::baz");
@ -413,6 +400,19 @@ main (int argc, char **argv)
test (TEST_TYPE_C, "foo", "TestC::foo,TestB::foo,TestA::foo,TestI::foo", NULL);
test (TEST_TYPE_C, "bar", "TestC::bar,TestB::bar,TestA::bar", NULL);
test (TEST_TYPE_C, "baz", "TestC::baz,TestB::baz,TestA::baz", "TestA::baz,TestB::baz,TestC::baz");
return failed ? 1 : 0;
}
int
main (int argc,
char **argv)
{
g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
G_LOG_LEVEL_WARNING |
G_LOG_LEVEL_CRITICAL);
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/gobject/override", test_override);
return g_test_run ();
}

View File

@ -15,17 +15,9 @@
* Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "TestReferences"
#include <glib-object.h>
#undef G_DISABLE_ASSERT
#undef G_DISABLE_CHECKS
#undef G_DISABLE_CAST_CHECKS
#include <glib-object.h>
/* This test tests weak and toggle references
*/
/* This test tests weak and toggle references */
static GObject *global_object;
@ -39,9 +31,7 @@ static gboolean toggle_ref2_strengthened;
static gboolean toggle_ref3_weakened;
static gboolean toggle_ref3_strengthened;
/*
* TestObject, a parent class for TestObject
*/
/* TestObject, a parent class for TestObject */
static GType test_object_get_type (void);
#define TEST_TYPE_OBJECT (test_object_get_type ())
typedef struct _TestObject TestObject;
@ -62,7 +52,7 @@ static void
test_object_finalize (GObject *object)
{
object_destroyed = TRUE;
G_OBJECT_CLASS (test_object_parent_class)->finalize (object);
}
@ -95,31 +85,31 @@ clear_flags (void)
static void
weak_ref1 (gpointer data,
GObject *object)
GObject *object)
{
g_assert (object == global_object);
g_assert (data == GUINT_TO_POINTER (42));
g_assert_true (object == global_object);
g_assert_cmpint (GPOINTER_TO_INT (data), ==, 42);
weak_ref1_notified = TRUE;
}
static void
weak_ref2 (gpointer data,
GObject *object)
GObject *object)
{
g_assert (object == global_object);
g_assert (data == GUINT_TO_POINTER (24));
g_assert_true (object == global_object);
g_assert_cmpint (GPOINTER_TO_INT (data), ==, 24);
weak_ref2_notified = TRUE;
}
static void
toggle_ref1 (gpointer data,
GObject *object,
gboolean is_last_ref)
GObject *object,
gboolean is_last_ref)
{
g_assert (object == global_object);
g_assert (data == GUINT_TO_POINTER (42));
g_assert_true (object == global_object);
g_assert_cmpint (GPOINTER_TO_INT (data), ==, 42);
if (is_last_ref)
toggle_ref1_weakened = TRUE;
@ -129,11 +119,11 @@ toggle_ref1 (gpointer data,
static void
toggle_ref2 (gpointer data,
GObject *object,
gboolean is_last_ref)
GObject *object,
gboolean is_last_ref)
{
g_assert (object == global_object);
g_assert (data == GUINT_TO_POINTER (24));
g_assert_true (object == global_object);
g_assert_cmpint (GPOINTER_TO_INT (data), ==, 24);
if (is_last_ref)
toggle_ref2_weakened = TRUE;
@ -143,11 +133,11 @@ toggle_ref2 (gpointer data,
static void
toggle_ref3 (gpointer data,
GObject *object,
gboolean is_last_ref)
GObject *object,
gboolean is_last_ref)
{
g_assert (object == global_object);
g_assert (data == GUINT_TO_POINTER (34));
g_assert_true (object == global_object);
g_assert_cmpint (GPOINTER_TO_INT (data), ==, 34);
if (is_last_ref)
{
@ -158,123 +148,126 @@ toggle_ref3 (gpointer data,
toggle_ref3_strengthened = TRUE;
}
int
main (int argc,
char *argv[])
static void
test_references (void)
{
GObject *object;
g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
G_LOG_LEVEL_WARNING |
G_LOG_LEVEL_CRITICAL);
/* Test basic weak reference operation
*/
/* Test basic weak reference operation */
global_object = object = g_object_new (TEST_TYPE_OBJECT, NULL);
g_object_weak_ref (object, weak_ref1, GUINT_TO_POINTER (42));
clear_flags ();
g_object_unref (object);
g_assert (weak_ref1_notified == TRUE);
g_assert (object_destroyed == TRUE);
g_assert_true (weak_ref1_notified);
g_assert_true (object_destroyed);
/* Test two weak references at once
*/
global_object = object = g_object_new (TEST_TYPE_OBJECT, NULL);
g_object_weak_ref (object, weak_ref1, GUINT_TO_POINTER (42));
g_object_weak_ref (object, weak_ref2, GUINT_TO_POINTER (24));
clear_flags ();
g_object_unref (object);
g_assert (weak_ref1_notified == TRUE);
g_assert (weak_ref2_notified == TRUE);
g_assert (object_destroyed == TRUE);
g_assert_true (weak_ref1_notified);
g_assert_true (weak_ref2_notified);
g_assert_true (object_destroyed);
/* Test remove weak references
*/
/* Test remove weak references */
global_object = object = g_object_new (TEST_TYPE_OBJECT, NULL);
g_object_weak_ref (object, weak_ref1, GUINT_TO_POINTER (42));
g_object_weak_ref (object, weak_ref2, GUINT_TO_POINTER (24));
g_object_weak_unref (object, weak_ref1, GUINT_TO_POINTER (42));
clear_flags ();
g_object_unref (object);
g_assert (weak_ref1_notified == FALSE);
g_assert (weak_ref2_notified == TRUE);
g_assert (object_destroyed == TRUE);
g_assert_false (weak_ref1_notified);
g_assert_true (weak_ref2_notified);
g_assert_true (object_destroyed);
/* Test basic toggle reference operation
*/
/* Test basic toggle reference operation */
global_object = object = g_object_new (TEST_TYPE_OBJECT, NULL);
g_object_add_toggle_ref (object, toggle_ref1, GUINT_TO_POINTER (42));
clear_flags ();
g_object_unref (object);
g_assert (toggle_ref1_weakened == TRUE);
g_assert (toggle_ref1_strengthened == FALSE);
g_assert (object_destroyed == FALSE);
g_assert_true (toggle_ref1_weakened);
g_assert_false (toggle_ref1_strengthened);
g_assert_false (object_destroyed);
clear_flags ();
g_object_ref (object);
g_assert (toggle_ref1_weakened == FALSE);
g_assert (toggle_ref1_strengthened == TRUE);
g_assert (object_destroyed == FALSE);
g_assert_false (toggle_ref1_weakened);
g_assert_true (toggle_ref1_strengthened);
g_assert_false (object_destroyed);
g_object_unref (object);
clear_flags ();
g_object_remove_toggle_ref (object, toggle_ref1, GUINT_TO_POINTER (42));
g_assert (toggle_ref1_weakened == FALSE);
g_assert (toggle_ref1_strengthened == FALSE);
g_assert (object_destroyed == TRUE);
g_assert_false (toggle_ref1_weakened);
g_assert_false (toggle_ref1_strengthened);
g_assert_true (object_destroyed);
global_object = object = g_object_new (TEST_TYPE_OBJECT, NULL);
/* Test two toggle references at once
*/
/* Test two toggle references at once */
g_object_add_toggle_ref (object, toggle_ref1, GUINT_TO_POINTER (42));
g_object_add_toggle_ref (object, toggle_ref2, GUINT_TO_POINTER (24));
clear_flags ();
g_object_unref (object);
g_assert (toggle_ref1_weakened == FALSE);
g_assert (toggle_ref1_strengthened == FALSE);
g_assert (toggle_ref2_weakened == FALSE);
g_assert (toggle_ref2_strengthened == FALSE);
g_assert (object_destroyed == FALSE);
g_assert_false (toggle_ref1_weakened);
g_assert_false (toggle_ref1_strengthened);
g_assert_false (toggle_ref2_weakened);
g_assert_false (toggle_ref2_strengthened);
g_assert_false (object_destroyed);
clear_flags ();
g_object_remove_toggle_ref (object, toggle_ref1, GUINT_TO_POINTER (42));
g_assert (toggle_ref1_weakened == FALSE);
g_assert (toggle_ref1_strengthened == FALSE);
g_assert (toggle_ref2_weakened == TRUE);
g_assert (toggle_ref2_strengthened == FALSE);
g_assert (object_destroyed == FALSE);
g_assert_false (toggle_ref1_weakened);
g_assert_false (toggle_ref1_strengthened);
g_assert_true (toggle_ref2_weakened);
g_assert_false (toggle_ref2_strengthened);
g_assert_false (object_destroyed);
clear_flags ();
/* Check that removing a toggle ref with %NULL data works fine. */
g_object_remove_toggle_ref (object, toggle_ref2, NULL);
g_assert (toggle_ref1_weakened == FALSE);
g_assert (toggle_ref1_strengthened == FALSE);
g_assert (toggle_ref2_weakened == FALSE);
g_assert (toggle_ref2_strengthened == FALSE);
g_assert (object_destroyed == TRUE);
/* Test a toggle reference that removes itself
*/
g_assert_false (toggle_ref1_weakened);
g_assert_false (toggle_ref1_strengthened);
g_assert_false (toggle_ref2_weakened);
g_assert_false (toggle_ref2_strengthened);
g_assert_true (object_destroyed);
/* Test a toggle reference that removes itself */
global_object = object = g_object_new (TEST_TYPE_OBJECT, NULL);
g_object_add_toggle_ref (object, toggle_ref3, GUINT_TO_POINTER (34));
clear_flags ();
g_object_unref (object);
g_assert (toggle_ref3_weakened == TRUE);
g_assert (toggle_ref3_strengthened == FALSE);
g_assert (object_destroyed == TRUE);
return 0;
g_assert_true (toggle_ref3_weakened);
g_assert_false (toggle_ref3_strengthened);
g_assert_true (object_destroyed);
}
int
main (int argc,
char *argv[])
{
g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
G_LOG_LEVEL_WARNING |
G_LOG_LEVEL_CRITICAL);
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/gobject/references", test_references);
return g_test_run ();
}

View File

@ -14,10 +14,8 @@
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "TestSingleton"
#include <glib-object.h>
#include <string.h>
/* --- MySingleton class --- */
typedef struct {
@ -29,11 +27,16 @@ typedef struct {
static GType my_singleton_get_type (void);
#define MY_TYPE_SINGLETON (my_singleton_get_type ())
#define MY_SINGLETON(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), MY_TYPE_SINGLETON, MySingleton))
#define MY_IS_SINGLETON(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), MY_TYPE_SINGLETON))
#define MY_SINGLETON_CLASS(c) (G_TYPE_CHECK_CLASS_CAST ((c), MY_TYPE_SINGLETON, MySingletonClass))
#define MY_IS_SINGLETON_CLASS(c) (G_TYPE_CHECK_CLASS_TYPE ((c), MY_TYPE_SINGLETON))
#define MY_SINGLETON_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), MY_TYPE_SINGLETON, MySingletonClass))
#define MY_SINGLETON(o) \
(G_TYPE_CHECK_INSTANCE_CAST ((o), MY_TYPE_SINGLETON, MySingleton))
#define MY_IS_SINGLETON(o) \
(G_TYPE_CHECK_INSTANCE_TYPE ((o), MY_TYPE_SINGLETON))
#define MY_SINGLETON_CLASS(c) \
(G_TYPE_CHECK_CLASS_CAST ((c), MY_TYPE_SINGLETON, MySingletonClass))
#define MY_IS_SINGLETON_CLASS(c) \
(G_TYPE_CHECK_CLASS_TYPE ((c), MY_TYPE_SINGLETON))
#define MY_SINGLETON_GET_CLASS(o) \
(G_TYPE_INSTANCE_GET_CLASS ((o), MY_TYPE_SINGLETON, MySingletonClass))
G_DEFINE_TYPE (MySingleton, my_singleton, G_TYPE_OBJECT)
@ -54,7 +57,7 @@ my_singleton_constructor (GType type,
static void
my_singleton_init (MySingleton *self)
{
g_assert (the_one_and_only == NULL);
g_assert_null (the_one_and_only);
the_one_and_only = self;
}
@ -73,11 +76,13 @@ main (int argc,
/* create the singleton */
singleton = g_object_new (MY_TYPE_SINGLETON, NULL);
g_assert (singleton != NULL);
g_assert_nonnull (singleton);
/* assert _singleton_ creation */
obj = g_object_new (MY_TYPE_SINGLETON, NULL);
g_assert (singleton == obj);
g_assert_true (singleton == obj);
g_object_unref (obj);
/* shutdown */
g_object_unref (singleton);
return 0;

View File

@ -1,47 +1,5 @@
# We cannot use gnome.genmarshal() here
testmarshal_h = custom_target('testmarshal_h',
output : 'testmarshal.h',
input : 'testmarshal.list',
command : [
python, glib_genmarshal,
'--prefix=test_marshal',
'--output=@OUTPUT@',
'--quiet',
'--header',
'@INPUT@',
],
)
testmarshal_c = custom_target('testmarshal_c',
output : 'testmarshal.c',
input : 'testmarshal.list',
command : [
python, glib_genmarshal,
'--prefix=test_marshal',
'--include-header=testmarshal.h',
'--output=@OUTPUT@',
'--quiet',
'--body',
'@INPUT@',
],
)
gobject_tests = {
'deftype' : {},
'defaultiface' : {
'extra_sources' : ['testmodule.c'],
},
'dynamictype' : {
'extra_sources' : ['testmodule.c'],
},
'override' : {},
'signals' : {},
'singleton' : {},
'references' : {},
'testgobject' : {},
'accumulator' : {
'extra_sources' : [testmarshal_c, testmarshal_h],
},
}
if host_system != 'windows'

View File

@ -1,4 +0,0 @@
# Marshallers used in tests
BOOLEAN:INT
STRING:INT
VARIANT:POINTER