Interactive tools: add options to hide some fields
[platform/upstream/libxkbcommon.git] / test / stringcomp.c
1 /*
2  * Copyright © 2009 Dan Nicholson
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23
24 #include "config.h"
25
26 #include <assert.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29
30 #include "test.h"
31
32 #define DATA_PATH "keymaps/stringcomp.data"
33
34 int
35 main(int argc, char *argv[])
36 {
37     struct xkb_context *ctx = test_get_context(0);
38     struct xkb_keymap *keymap;
39     char *original, *dump, *dump2;
40
41     assert(ctx);
42
43     /* Load in a prebuilt keymap, make sure we can compile it from a string,
44      * then compare it to make sure we get the same result when dumping it
45      * to a string. */
46     original = test_read_file(DATA_PATH);
47     assert(original);
48
49     keymap = test_compile_string(ctx, original);
50     assert(keymap);
51
52     dump = xkb_keymap_get_as_string(keymap, XKB_KEYMAP_USE_ORIGINAL_FORMAT);
53     assert(dump);
54
55     if (!streq(original, dump)) {
56         fprintf(stderr,
57                 "round-trip test failed: dumped map differs from original\n");
58         fprintf(stderr, "path to original file: %s\n",
59                 test_get_path(DATA_PATH));
60         fprintf(stderr, "length: dumped %lu, original %lu\n",
61                 (unsigned long) strlen(dump),
62                 (unsigned long) strlen(original));
63         fprintf(stderr, "dumped map:\n");
64         fprintf(stderr, "%s\n", dump);
65         fflush(stderr);
66         assert(0);
67     }
68
69     free(original);
70     free(dump);
71     xkb_keymap_unref(keymap);
72
73     /* Make sure we can't (falsely claim to) compile an empty string. */
74     keymap = test_compile_string(ctx, "");
75     assert(!keymap);
76
77     /* Make sure we can recompile our output for a normal keymap from rules. */
78     keymap = test_compile_rules(ctx, NULL, NULL,
79                                 "ru,ca,de,us", ",multix,neo,intl", NULL);
80     assert(keymap);
81     dump = xkb_keymap_get_as_string(keymap, XKB_KEYMAP_USE_ORIGINAL_FORMAT);
82     assert(dump);
83     xkb_keymap_unref(keymap);
84     keymap = test_compile_string(ctx, dump);
85     assert(keymap);
86     /* Now test that the dump of the dump is equal to the dump! */
87     dump2 = xkb_keymap_get_as_string(keymap, XKB_KEYMAP_USE_ORIGINAL_FORMAT);
88     assert(dump2);
89     assert(streq(dump, dump2));
90
91     /* Test response to invalid formats and flags. */
92     assert(!xkb_keymap_new_from_string(ctx, dump, 0, 0));
93     assert(!xkb_keymap_new_from_string(ctx, dump, -1, 0));
94     assert(!xkb_keymap_new_from_string(ctx, dump, XKB_KEYMAP_FORMAT_TEXT_V1+1, 0));
95     assert(!xkb_keymap_new_from_string(ctx, dump, XKB_KEYMAP_FORMAT_TEXT_V1, -1));
96     assert(!xkb_keymap_new_from_string(ctx, dump, XKB_KEYMAP_FORMAT_TEXT_V1, 1414));
97     assert(!xkb_keymap_get_as_string(keymap, 0));
98     assert(!xkb_keymap_get_as_string(keymap, 4893));
99
100     xkb_keymap_unref(keymap);
101     free(dump);
102     free(dump2);
103
104     xkb_context_unref(ctx);
105
106     return 0;
107 }