Index: tests/keyfile-idempotent-test.c
===================================================================
--- tests/keyfile-idempotent-test.c	(revision 0)
+++ tests/keyfile-idempotent-test.c	(revision 0)
@@ -0,0 +1,76 @@
+#undef G_DISABLE_ASSERT
+#undef G_LOG_DOMAIN
+
+#ifdef GLIB_COMPILATION
+#undef GLIB_COMPILATION
+#endif
+
+#include "glib.h"
+#include <string.h>
+
+static const gchar *original_keyfile=""
+"[first]\n"
+"# Single line comment\n"
+"[second]\n"
+"# Double line comment\n"
+"# Double line comment\n"
+"[third]\n"
+"\n"
+"[fourth]\n"
+"[fifth]\n";
+
+int
+main(int argc, char **argv)
+{
+  GKeyFile *keyfile;
+  GError *error = NULL;
+  gchar *data0, *data1;
+  gsize len0, len1;
+
+
+  /* check that we only insert a single new line between groups */
+  keyfile = g_key_file_new ();
+  if (!g_key_file_load_from_data (keyfile,
+	original_keyfile, strlen(original_keyfile),
+	G_KEY_FILE_KEEP_COMMENTS,
+	&error)) {
+    g_error ("Failed to parse keyfile[1]: %s",
+	error->message);
+  }
+
+  data0 = g_key_file_to_data (keyfile, &len0, &error);
+  if (data0 == NULL) {
+    g_error ("Failed to extract keyfile[1]: %s",
+	error->message);
+  }
+  g_key_file_free (keyfile);
+
+  g_print ("%s\n", data0);
+
+  keyfile = g_key_file_new ();
+  if (!g_key_file_load_from_data (keyfile,
+	data0, len0,
+	G_KEY_FILE_KEEP_COMMENTS,
+	&error)) {
+    g_error ("Failed to parse keyfile[2]: %s",
+	error->message);
+  }
+
+  data1 = g_key_file_to_data (keyfile, &len1, &error);
+  if (data1 == NULL) {
+    g_error ("Failed to extract keyfile[2]: %s",
+	error->message);
+  }
+  g_key_file_free (keyfile);
+
+  g_print ("%s\n", data1);
+
+  if (strcmp(data0, data1) != 0) {
+    g_error ("Reloading GKeyFile is not idempotent.");
+  }
+
+  g_free (data0);
+  g_free (data1);
+
+  return 0;
+}
Index: tests/Makefile.am
===================================================================
--- tests/Makefile.am	(revision 5430)
+++ tests/Makefile.am	(working copy)
@@ -85,6 +85,7 @@ test_programs =					\
 	hash-test				\
 	iochannel-test				\
 	keyfile-test				\
+	keyfile-idempotent-test			\
 	list-test				\
 	mainloop-test				\
 	mapping-test				\
@@ -153,6 +154,7 @@ gio_test_LDADD = $(progs_ldadd)
 hash_test_LDADD = $(progs_ldadd)
 iochannel_test_LDADD = $(progs_ldadd)
 keyfile_test_LDADD = $(progs_ldadd)
+keyfile_idempotent_test_LDADD = $(progs_ldadd)
 list_test_LDADD = $(progs_ldadd)
 mainloop_test_LDADD = $(thread_ldadd)
 markup_test_LDADD = $(progs_ldadd)
