dbus/test/glib test-dbus-glib.c, 1.33, 1.34 test-service-glib.c, 1.27, 1.28 test-service-glib.xml, 1.15, 1.16

Robert McQueen robot101 at freedesktop.org
Fri Jan 27 07:40:38 PST 2006


Update of /cvs/dbus/dbus/test/glib
In directory gabe:/tmp/cvs-serv29370/test/glib

Modified Files:
	test-dbus-glib.c test-service-glib.c test-service-glib.xml 
Log Message:
2006-01-27  Robert McQueen  <robot101 at debian.org>

	* glib/dbus-gtype-specialized.[ch], glib/dbus-gvalue-utils.c: Patch
	by me and Rob Taylor to add a simple_free function to D-Bus map
	and collection types, which allows those types which can be freed
	with a GDestroyNotify (such as GHashTables and GArrays, but not
	GPtrArrays) to be stored as the values in hashtables.

	* test/glib/test-dbus-glib.c, test/glib/test-service-glib.{c,xml}:
	Patch by Rob Taylor to add nested dicts to the glib tests to check
	the above code works, and appears not to leak when called repeatedly.

Index: test-dbus-glib.c
===================================================================
RCS file: /cvs/dbus/dbus/test/glib/test-dbus-glib.c,v
retrieving revision 1.33
retrieving revision 1.34
diff -u -d -r1.33 -r1.34
--- test-dbus-glib.c	30 Nov 2005 19:32:26 -0000	1.33
+++ test-dbus-glib.c	27 Jan 2006 15:40:36 -0000	1.34
@@ -1164,6 +1164,95 @@
 
     g_value_unset (variant);
   }
+
+  for (i=0; i<3; i++)
+  {
+    gchar *val;
+    GHashTable *table;
+    GHashTable *subtable;
+    GHashTable *ret_table;
+
+    table = g_hash_table_new_full (g_str_hash, g_str_equal,
+				   (GDestroyNotify) (g_free),
+                                   (GDestroyNotify) (g_hash_table_destroy));
+
+    subtable = g_hash_table_new_full (g_str_hash, g_str_equal,
+                                      (GDestroyNotify) (g_free),
+                                      (GDestroyNotify) (g_free));
+    g_hash_table_insert (subtable, g_strdup ("foo"), g_strdup("1"));
+    g_hash_table_insert (subtable, g_strdup ("bar"), g_strdup("2"));
+    g_hash_table_insert (subtable, g_strdup ("baz"), g_strdup("3"));
+
+    g_hash_table_insert (table, g_strdup("dict1"), subtable);
+
+    subtable = g_hash_table_new_full (g_str_hash, g_str_equal,
+                                      (GDestroyNotify) (g_free),
+                                      (GDestroyNotify) (g_free));
+    g_hash_table_insert (subtable, g_strdup ("foo"), g_strdup("4"));
+    g_hash_table_insert (subtable, g_strdup ("bar"), g_strdup("5"));
+    g_hash_table_insert (subtable, g_strdup ("baz"), g_strdup("6"));
+
+    g_hash_table_insert (table, g_strdup("dict2"), subtable);
+
+    subtable = NULL;
+
+    ret_table = NULL;
+
+    g_print ("Calling DictOfDicts\n");
+    if (!dbus_g_proxy_call (proxy, "DictOfDicts", &error,
+			    dbus_g_type_get_map ("GHashTable", G_TYPE_STRING,
+                              dbus_g_type_get_map ("GHashTable", G_TYPE_STRING,
+                                G_TYPE_STRING)), table,
+			    G_TYPE_INVALID,
+			    dbus_g_type_get_map ("GHashTable", G_TYPE_STRING,
+                              dbus_g_type_get_map ("GHashTable", G_TYPE_STRING,
+                                G_TYPE_STRING)), &ret_table,
+			    G_TYPE_INVALID))
+      lose_gerror ("Failed to complete DictOfDicts call", error);
+
+    g_assert (ret_table != NULL);
+    g_assert (g_hash_table_size (ret_table) == 2);
+
+    subtable = g_hash_table_lookup (ret_table, "dict1");
+    g_assert(subtable);
+    g_assert (g_hash_table_size (subtable) == 3);
+
+    val = g_hash_table_lookup (subtable, "foo");
+    g_assert (val != NULL);
+    g_assert (!strcmp ("dict1 1", val));
+
+    val = g_hash_table_lookup (subtable, "bar");
+    g_assert (val != NULL);
+    g_assert (!strcmp ("dict1 2", val));
+
+    val = g_hash_table_lookup (subtable, "baz");
+    g_assert (val != NULL);
+    g_assert (!strcmp ("dict1 3", val));
+
+    subtable = g_hash_table_lookup (ret_table, "dict2");
+    g_assert(subtable);
+    g_assert (g_hash_table_size (subtable) == 3);
+
+    val = g_hash_table_lookup (subtable, "foo");
+    g_assert (val != NULL);
+    g_assert (!strcmp ("dict2 4", val));
+
+    val = g_hash_table_lookup (subtable, "bar");
+    g_assert (val != NULL);
+    g_assert (!strcmp ("dict2 5", val));
+
+    val = g_hash_table_lookup (subtable, "baz");
+    g_assert (val != NULL);
+    g_assert (!strcmp ("dict2 6", val));
+
+    g_hash_table_destroy (table);
+    g_hash_table_destroy (ret_table);
+
+    g_mem_profile ();
+  }
+
+
+
   /* Signal handling tests */
   
   g_print ("Testing signal handling\n");

Index: test-service-glib.c
===================================================================
RCS file: /cvs/dbus/dbus/test/glib/test-service-glib.c,v
retrieving revision 1.27
retrieving revision 1.28
diff -u -d -r1.27 -r1.28
--- test-service-glib.c	18 Oct 2005 04:38:05 -0000	1.27
+++ test-service-glib.c	27 Jan 2006 15:40:36 -0000	1.28
@@ -101,6 +101,8 @@
 
 gboolean my_object_process_variant_of_array_of_ints123 (MyObject *obj, GValue *variant, GError **error);
 
+gboolean my_object_dict_of_dicts (MyObject *obj, GHashTable *dict, GHashTable **ret, GError **error);
+
 gboolean my_object_terminate (MyObject *obj, GError **error);
 
 void my_object_async_increment (MyObject *obj, gint32 x, DBusGMethodInvocation *context);
@@ -706,6 +708,51 @@
   return FALSE;
 }
 
+
+typedef struct _HashAndString HashAndString;
+
+struct _HashAndString
+{
+  GHashTable *hash;
+  gchar* string;
+};
+
+static void
+hash_foreach_prepend_string (gpointer key, gpointer val, gpointer user_data)
+{
+  HashAndString *data = (HashAndString*) user_data;
+  gchar *in = (gchar*) val;
+  g_hash_table_insert (data->hash, g_strdup ((gchar*) key),
+                       g_strjoin (" ", data->string, in, NULL));
+}
+
+
+static void
+hash_foreach_mangle_dict_of_strings (gpointer key, gpointer val, gpointer user_data)
+{
+  GHashTable *out = (GHashTable*) user_data;
+  GHashTable *in_dict = (GHashTable *) val;
+  HashAndString *data = g_new0 (HashAndString, 1);
+
+  data->string = (gchar*) key;
+  data->hash = g_hash_table_new_full (g_str_hash, g_str_equal,
+                                            g_free, g_free);
+  g_hash_table_foreach (in_dict, hash_foreach_prepend_string, data);
+
+  g_hash_table_insert(out, g_strdup ((gchar*) key), data->hash);
+}
+
+gboolean
+my_object_dict_of_dicts (MyObject *obj, GHashTable *in,
+                                GHashTable **out, GError **error)
+{
+  *out = g_hash_table_new_full (g_str_hash, g_str_equal,
+				(GDestroyNotify) g_free,
+                                (GDestroyNotify) g_hash_table_destroy);
+  g_hash_table_foreach (in, hash_foreach_mangle_dict_of_strings, *out);
+  return TRUE;
+}
+
 gboolean
 my_object_emit_frobnicate (MyObject *obj, GError **error)
 {

Index: test-service-glib.xml
===================================================================
RCS file: /cvs/dbus/dbus/test/glib/test-service-glib.xml,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -d -r1.15 -r1.16
--- test-service-glib.xml	5 Oct 2005 20:43:46 -0000	1.15
+++ test-service-glib.xml	27 Jan 2006 15:40:36 -0000	1.16
@@ -137,6 +137,11 @@
       <arg type="v" direction="in" />
     </method>
 
+    <method name="DictOfDicts">
+      <arg type="a{sa{ss}}" direction="in"/>
+      <arg type="a{sa{ss}}" direction="out"/>
+    </method>
+
     <method name="EmitFrobnicate">
     </method>
 



More information about the dbus-commit mailing list