[glib/wip/serializable: 6/6] Add tests for GEncoder and GSerializable
- From: Emmanuele Bassi <ebassi src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glib/wip/serializable: 6/6] Add tests for GEncoder and GSerializable
- Date: Fri, 24 May 2013 14:03:23 +0000 (UTC)
commit a2ac1d0fd100532b2d646c24c409dde00816b414
Author: Emmanuele Bassi <ebassi gnome org>
Date: Fri May 24 15:01:44 2013 +0100
Add tests for GEncoder and GSerializable
gio/tests/.gitignore | 2 +
gio/tests/Makefile.am | 2 +
gio/tests/encoder.c | 93 ++++++++++++++++++++++++
gio/tests/serializable.c | 177 ++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 274 insertions(+), 0 deletions(-)
---
diff --git a/gio/tests/.gitignore b/gio/tests/.gitignore
index 0652ff7..d6908fc 100644
--- a/gio/tests/.gitignore
+++ b/gio/tests/.gitignore
@@ -16,6 +16,7 @@ data-output-stream
de/
desktop-app-info
echo-server
+encoder
file
fileattributematcher
filter-cat
@@ -94,6 +95,7 @@ readwrite
resolver
resources
send-data
+serializable
services/org.gtk.GDBus.Examples.ObjectManager.service
simple-async-result
simple-proxy
diff --git a/gio/tests/Makefile.am b/gio/tests/Makefile.am
index 7697ccb..edbe491 100644
--- a/gio/tests/Makefile.am
+++ b/gio/tests/Makefile.am
@@ -70,6 +70,8 @@ test_progs = \
permission \
task \
credentials \
+ encoder \
+ serializable \
$(NULL)
if HAVE_DBUS_DAEMON
diff --git a/gio/tests/encoder.c b/gio/tests/encoder.c
new file mode 100644
index 0000000..698f23b
--- /dev/null
+++ b/gio/tests/encoder.c
@@ -0,0 +1,93 @@
+#include <gio/gio.h>
+
+static void
+encoder_binary (void)
+{
+ GEncoder *encoder = g_binary_encoder_new ();
+ GError *error = NULL;
+ GBytes *buffer;
+ gboolean bool_value;
+ char *str_value;
+
+ g_object_add_weak_pointer (G_OBJECT (encoder), (gpointer *) &encoder);
+
+ g_encoder_add_key_bool (encoder, "BoolValue", TRUE);
+ g_encoder_add_key_string (encoder, "StringValue", "Hello");
+
+ buffer = g_encoder_write_to_bytes (encoder, &error);
+ g_assert_no_error (error);
+ g_assert (buffer != NULL);
+
+ g_object_unref (encoder);
+ g_assert (encoder == NULL);
+
+ if (g_test_verbose ())
+ g_print ("*** buffer (len: %d) = ***\n%s\n",
+ (int) g_bytes_get_size (buffer),
+ (const char *) g_bytes_get_data (buffer, NULL));
+
+ encoder = g_binary_encoder_new ();
+
+ g_object_add_weak_pointer (G_OBJECT (encoder), (gpointer *) &encoder);
+
+ g_encoder_read_from_bytes (encoder, buffer, &error);
+ g_assert_no_error (error);
+
+ g_encoder_get_key_bool (encoder, "BoolValue", &bool_value);
+ g_assert (bool_value);
+
+ g_encoder_get_key_string (encoder, "StringValue", &str_value);
+ g_assert_cmpstr (str_value, ==, "Hello");
+ g_free (str_value);
+
+ g_bytes_unref (buffer);
+
+ g_object_unref (encoder);
+ g_assert (encoder == NULL);
+}
+
+static void
+encoder_keyfile (void)
+{
+ GEncoder *encoder = g_keyfile_encoder_new ();
+ GError *error = NULL;
+ GBytes *buffer;
+ gboolean res;
+
+ g_keyfile_encoder_set_section_name (G_KEYFILE_ENCODER (encoder), "Test");
+ g_encoder_add_key_bool (encoder, "BoolValue", TRUE);
+
+ buffer = g_encoder_write_to_bytes (encoder, &error);
+ g_assert_no_error (error);
+ g_assert (buffer != NULL);
+
+ g_object_unref (encoder);
+
+ if (g_test_verbose ())
+ g_print ("*** buffer (len: %d) = ***\n%s",
+ (int) g_bytes_get_size (buffer),
+ (const char *) g_bytes_get_data (buffer, NULL));
+
+ encoder = g_keyfile_encoder_new ();
+ g_keyfile_encoder_set_section_name (G_KEYFILE_ENCODER (encoder), "Test");
+
+ g_encoder_read_from_bytes (encoder, buffer, &error);
+ g_assert_no_error (error);
+
+ g_encoder_get_key_bool (encoder, "BoolValue", &res);
+ g_assert (res);
+
+ g_bytes_unref (buffer);
+ g_object_unref (encoder);
+}
+
+int
+main (int argc, char *argv[])
+{
+ g_test_init (&argc, &argv, NULL);
+
+ g_test_add_func ("/encoder/binary", encoder_binary);
+ g_test_add_func ("/encoder/key-file", encoder_keyfile);
+
+ return g_test_run ();
+}
diff --git a/gio/tests/serializable.c b/gio/tests/serializable.c
new file mode 100644
index 0000000..fd6cf3a
--- /dev/null
+++ b/gio/tests/serializable.c
@@ -0,0 +1,177 @@
+#include <gio/gio.h>
+
+typedef struct {
+ GObject parent_instance;
+
+ gboolean bool_value;
+ char *str_value;
+ gdouble double_value;
+} TestObject;
+
+typedef struct {
+ GObjectClass parent_class;
+} TestObjectClass;
+
+GType test_object_get_type (void);
+
+static void test_object_serializable_init (GSerializableInterface *iface);
+
+G_DEFINE_TYPE_WITH_CODE (TestObject, test_object, G_TYPE_OBJECT,
+ G_IMPLEMENT_INTERFACE (G_TYPE_SERIALIZABLE, test_object_serializable_init))
+
+static void
+test_object_serialize (GSerializable *serializable,
+ GEncoder *encoder)
+{
+ TestObject *self = (TestObject *) serializable;
+
+ g_encoder_add_key_bool (encoder, "bool-value", self->bool_value);
+ g_encoder_add_key_string (encoder, "str-value", self->str_value);
+ g_encoder_add_key_double (encoder, "double-value", self->double_value);
+}
+
+static gboolean
+test_object_deserialize (GSerializable *serializable,
+ GEncoder *encoder,
+ GError **error)
+{
+ TestObject *self = (TestObject *) serializable;
+
+ g_encoder_get_key_bool (encoder, "bool-value", &self->bool_value);
+ g_encoder_get_key_string (encoder, "str-value", &self->str_value);
+ g_encoder_get_key_double (encoder, "double-value", &self->double_value);
+
+ return TRUE;
+}
+
+static void
+test_object_serializable_init (GSerializableInterface *iface)
+{
+ iface->serialize = test_object_serialize;
+ iface->deserialize = test_object_deserialize;
+}
+
+static void
+test_object_finalize (GObject *gobject)
+{
+ TestObject *self = (TestObject *) gobject;
+
+ g_free (self->str_value);
+
+ G_OBJECT_CLASS (test_object_parent_class)->finalize (gobject);
+}
+
+static void
+test_object_class_init (TestObjectClass *klass)
+{
+ G_OBJECT_CLASS (klass)->finalize = test_object_finalize;
+}
+
+static void
+test_object_init (TestObject *self)
+{
+}
+
+static TestObject *
+test_object_new (void)
+{
+ return g_object_new (test_object_get_type (), NULL);
+}
+
+static void
+test_object_set_bool_value (TestObject *obj,
+ gboolean value)
+{
+ value = !!value;
+
+ obj->bool_value = value;
+}
+
+static gboolean
+test_object_get_bool_value (TestObject *obj)
+{
+ return obj->bool_value;
+}
+
+static void
+test_object_set_str_value (TestObject *obj,
+ const char *value)
+{
+ char *str_value = g_strdup (value);
+
+ g_free (obj->str_value);
+ obj->str_value = str_value;
+}
+
+static const char *
+test_object_get_str_value (TestObject *obj)
+{
+ return obj->str_value;
+}
+
+static void
+test_object_set_double_value (TestObject *obj,
+ double value)
+{
+ obj->double_value = value;
+}
+
+static gdouble
+test_object_get_double_value (TestObject *obj)
+{
+ return obj->double_value;
+}
+
+/* tests */
+static void
+serializable_roundtrip (void)
+{
+ GEncoder *encoder;
+ TestObject *obj;
+ GError *error;
+ GBytes *buffer;
+
+ obj = test_object_new ();
+ test_object_set_bool_value (obj, TRUE);
+ test_object_set_str_value (obj, "Hello, World");
+ test_object_set_double_value (obj, 3.14159);
+
+ encoder = g_binary_encoder_new ();
+ g_serializable_serialize (G_SERIALIZABLE (obj), encoder);
+
+ error = NULL;
+ buffer = g_encoder_write_to_bytes (encoder, &error);
+ g_assert_no_error (error);
+ g_object_unref (encoder);
+ g_object_unref (obj);
+
+ if (g_test_verbose ())
+ g_print ("*** buffer: '%s' ***\n",
+ (const char *) g_bytes_get_data (buffer, NULL));
+
+ encoder = g_binary_encoder_new ();
+ g_encoder_read_from_bytes (encoder, buffer, &error);
+ g_assert_no_error (error);
+
+ obj = test_object_new ();
+ g_serializable_deserialize (G_SERIALIZABLE (obj), encoder, &error);
+ g_assert_no_error (error);
+
+ g_assert (test_object_get_bool_value (obj));
+ g_assert_cmpstr (test_object_get_str_value (obj), ==, "Hello, World");
+ g_assert_cmpfloat ((float) test_object_get_double_value (obj), ==, 3.14159f);
+
+ g_bytes_unref (buffer);
+ g_object_unref (encoder);
+ g_object_unref (obj);
+}
+
+int
+main (int argc, char *argv[])
+{
+ g_test_init (&argc, &argv, NULL);
+
+ g_test_add_func ("/serializable/roundtrip", serializable_roundtrip);
+
+ return g_test_run ();
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]