test/stringcomp: Perform full round-trip test
authorDaniel Stone <daniel@fooishbar.org>
Fri, 3 Aug 2012 02:01:21 +0000 (04:01 +0200)
committerDaniel Stone <daniel@fooishbar.org>
Wed, 8 Aug 2012 14:23:30 +0000 (16:23 +0200)
We now pass! \o/

Signed-off-by: Daniel Stone <daniel@fooishbar.org>
test/stringcomp.c

index e13a312..03a3cb0 100644 (file)
@@ -1,4 +1,5 @@
-/* Copyright 2009 Dan Nicholson
+/*
+ * Copyright 2009 Dan Nicholson
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
 #include "xkbcommon/xkbcommon.h"
 #include "test.h"
 
+#define DATA_PATH "keymaps/stringcomp.data"
+
 int
 main(int argc, char *argv[])
 {
     struct xkb_context *ctx = test_get_context();
     struct xkb_keymap *keymap;
-    char *as_string;
+    char *original, *dump;
 
     assert(ctx);
 
-    as_string = test_read_file("keymaps/stringcomp.data");
-    assert(as_string);
+    /* Load in a prebuilt keymap, make sure we can compile it from a string,
+     * then compare it to make sure we get the same result when dumping it
+     * to a string. */
+    original = test_read_file(DATA_PATH);
+    assert(original);
 
-    keymap = test_compile_string(ctx, as_string);
+    keymap = test_compile_string(ctx, original);
     assert(keymap);
-    free(as_string);
+
+    dump = xkb_map_get_as_string(keymap);
+    assert(dump);
+
+    if (!streq(original, dump)) {
+        fprintf(stderr,
+                "round-trip test failed: dumped map differs from original\n");
+        fprintf(stderr, "path to original file: %s\n",
+                test_get_path(DATA_PATH));
+        fprintf(stderr, "length: dumped %zu, original %zu\n",
+                strlen(dump), strlen(original));
+        fprintf(stderr, "dumped map:\n");
+        fprintf(stderr, "%s\n", dump);
+        fflush(stderr);
+        assert(0);
+    }
+
+    free(original);
+    free(dump);
     xkb_map_unref(keymap);
 
+    /* Make sure we can't (falsely claim to) compile an empty string. */
     keymap = test_compile_string(ctx, "");
     assert(!keymap);