Debian Codegraph @

test/stringcomp.c

raw
1 /*
2 * Copyright © 2009 Dan Nicholson
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "config.h"
7
8 #include <assert.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11
12 #include "xkbcommon/xkbcommon.h"
13 #include "test.h"
14 #include "test/utils-text.h"
15 #include "utils.h"
16 #include "keymap-compare.h"
17
18 static void
19 test_keymap_comparison(struct xkb_context *ctx)
20 {
21 static const struct {
22 const char* keymap1;
23 const char* keymap2;
24 enum xkb_keymap_compare_property properties;
25 bool same;
26 } tests[] = {
27 {
28 .keymap1 = "xkb_keymap {};",
29 .keymap2 = "xkb_keymap {};",
30 .properties = XKB_KEYMAP_CMP_ALL,
31 .same = true
32 },
33 {
34 .keymap1 =
35 "xkb_keymap {\n"
36 " xkb_keycodes { <> = 1; };\n"
37 "};",
38 .keymap2 = "xkb_keymap {};",
39 .properties = XKB_KEYMAP_CMP_ALL,
40 .same = false
41 },
42 {
43 .keymap1 =
44 "xkb_keymap {\n"
45 " xkb_keycodes {\n"
46 " <a> = 1;\n"
47 " <b> = 2;\n"
48 " };\n"
49 " xkb_compat { include \"caps\" };\n"
50 " xkb_types { include \"basic+iso9995\" };\n"
51 " xkb_symbols {\n"
52 " key <a> { [a, A] };\n"
53 " key <b> { [Caps_Lock] };\n" /* implicit action */
54 " };\n"
55 "};",
56 .keymap2 =
57 "xkb_keymap {\n"
58 " xkb_keycodes {\n"
59 " <a> = 1;\n"
60 " <b> = 2;\n"
61 " };\n"
62 " xkb_compat {};\n" /* empty */
63 " xkb_types { include \"basic\" };\n" /* less types */
64 " xkb_symbols {\n"
65 /* Change key order + explicit actions and types */
66 " key <b> {\n"
67 " [Caps_Lock],\n"
68 " [LockMods(modifiers = Lock)],\n"
69 " type=\"ONE_LEVEL\"\n"
70 " };\n"
71 " key <a> { [a, A], type=\"ALPHABETIC\" };\n"
72 /* Definition in different section */
73 " virtual_modifiers LevelThree;\n"
74 " };\n"
75 "};",
76 .properties = (XKB_KEYMAP_CMP_ALL & ~(XKB_KEYMAP_CMP_POSSIBLY_DROPPED)),
77 .same = true
78 },
79 };
80 for (size_t t = 0; t < ARRAY_SIZE(tests); t++) {
81 fprintf(stderr, "------\n%s: #%zu\n", __func__, t);
82 struct xkb_keymap * const keymap1 = test_compile_string(
83 ctx, XKB_KEYMAP_FORMAT_TEXT_V1, tests[t].keymap1
84 );
85 assert(keymap1);
86 struct xkb_keymap * const keymap2 = test_compile_string(
87 ctx, XKB_KEYMAP_FORMAT_TEXT_V1, tests[t].keymap2
88 );
89 assert(keymap2);
90 assert(xkb_keymap_compare(ctx, keymap1, keymap2, tests[t].properties) ==
91 tests[t].same);
92 xkb_keymap_unref(keymap1);
93 xkb_keymap_unref(keymap2);
94 }
95 }
96
97 static void
98 test_explicit_actions(struct xkb_context *ctx)
99 {
100 struct xkb_keymap *keymap;
101
102 char *original = test_read_file("keymaps/explicit-actions.xkb");
103 assert(original);
104 char *tmp = uncomment(original, strlen(original), "//");
105 assert(tmp);
106 char *expected = strip_lines(tmp, strlen(tmp), "//");
107 free(tmp);
108 assert(expected);
109 char *got = NULL;
110
111 /* Try original */
112 keymap = test_compile_string(ctx, XKB_KEYMAP_FORMAT_TEXT_V1, original);
113 free(original);
114 assert(keymap);
115 got = xkb_keymap_get_as_string2(keymap, XKB_KEYMAP_USE_ORIGINAL_FORMAT,
116 TEST_KEYMAP_SERIALIZE_FLAGS);
117 xkb_keymap_unref(keymap);
118 assert_streq_not_null("Check output from original", expected, got);
119 free(got);
120
121 /* Try round-trip */
122 keymap = test_compile_string(ctx, XKB_KEYMAP_FORMAT_TEXT_V1, expected);
123 assert(keymap);
124 got = xkb_keymap_get_as_string2(keymap, XKB_KEYMAP_USE_ORIGINAL_FORMAT,
125 TEST_KEYMAP_SERIALIZE_FLAGS);
126 xkb_keymap_unref(keymap);
127 assert_streq_not_null("Check roundtrip", expected, got);
128 free(got);
129
130 free(expected);
131 }
132
133 static struct xkb_keymap*
134 compile_string(struct xkb_context *context, enum xkb_keymap_format format,
135 const char *buf, size_t len, void *private)
136 {
137 return test_compile_string(context, format, buf);
138 }
139
140 int
141 main(int argc, char *argv[])
142 {
143 test_init();
144
145 bool update_output_files = false;
146 if (argc > 1) {
147 if (streq(argv[1], "update")) {
148 /* Update files with *obtained* results */
149 update_output_files = true;
150 } else {
151 fprintf(stderr, "ERROR: unsupported argument: \"%s\".\n", argv[1]);
152 exit(EXIT_FAILURE);
153 }
154 }
155
156 struct xkb_context *ctx = test_get_context(CONTEXT_NO_FLAG);
157 struct xkb_keymap *keymap;
158 char *dump, *dump2;
159
160 assert(ctx);
161
162 /* Make sure we can't (falsely claim to) compile an empty string. */
163 keymap = test_compile_string(ctx, XKB_KEYMAP_FORMAT_TEXT_V1, "");
164 assert(!keymap);
165
166 /* Load in a prebuilt keymap, make sure we can compile it from a string,
167 * then compare it to make sure we get the same result when dumping it
168 * to a string. */
169 static const struct {
170 const char* path;
171 enum xkb_keymap_format format;
172 enum xkb_keymap_serialize_flags serialize_flags;
173 } data[] = {
174 {
175 .path = "keymaps/stringcomp-v1.xkb",
176 .format = XKB_KEYMAP_FORMAT_TEXT_V1,
177 .serialize_flags = TEST_KEYMAP_SERIALIZE_FLAGS
178 },
179 {
180 .path = "keymaps/stringcomp-v1-no-prettyfied.xkb",
181 .format = XKB_KEYMAP_FORMAT_TEXT_V1,
182 .serialize_flags = TEST_KEYMAP_SERIALIZE_FLAGS
183 & ~XKB_KEYMAP_SERIALIZE_PRETTY
184 },
185 {
186 .path = "keymaps/stringcomp-v1-no-flags.xkb",
187 .format = XKB_KEYMAP_FORMAT_TEXT_V1,
188 .serialize_flags = XKB_KEYMAP_SERIALIZE_NO_FLAGS
189 },
190 {
191 .path = "keymaps/stringcomp-v2.xkb",
192 .format = XKB_KEYMAP_FORMAT_TEXT_V2,
193 .serialize_flags = TEST_KEYMAP_SERIALIZE_FLAGS
194 },
195 };
196 for (unsigned int k = 0; k < ARRAY_SIZE(data); k++) {
197 fprintf(stderr, "------\nTest roundtrip of: %s\n", data[k].path);
198 char *original = test_read_file(data[k].path);
199 assert(original);
200
201 /* Check round-trip with same serialize flags */
202 assert(test_compile_output2(ctx, data[k].format,
203 XKB_KEYMAP_USE_ORIGINAL_FORMAT,
204 data[k].serialize_flags,
205 compile_string, NULL,
206 "Round-trip with same serialize flags",
207 original, 0 /* unused */, data[k].path,
208 update_output_files));
209
210 /*
211 * Check round-trip by dropping/keeping unused keymap items
212 * We obviously cannot compare the serializations, so only keymap
213 * objects are compared.
214 */
215
216 keymap = xkb_keymap_new_from_string(ctx, original, data[k].format,
217 XKB_KEYMAP_COMPILE_NO_FLAGS);
218 assert(keymap);
219 free(original);
220 const enum xkb_keymap_serialize_flags test_serialize_flags[] = {
221 (data[k].serialize_flags & ~XKB_KEYMAP_SERIALIZE_KEEP_UNUSED),
222 (data[k].serialize_flags | XKB_KEYMAP_SERIALIZE_KEEP_UNUSED)
223 };
224 for (size_t f = 0; f < ARRAY_SIZE(test_serialize_flags); f++) {
225 original = xkb_keymap_get_as_string2(keymap, data[k].format,
226 test_serialize_flags[f]);
227 assert(original);
228 struct xkb_keymap *keymap2 = xkb_keymap_new_from_string(
229 ctx, original, data[k].format, XKB_KEYMAP_COMPILE_NO_FLAGS
230 );
231 assert(keymap2);
232 free(original);
233 assert(xkb_keymap_compare(ctx, keymap, keymap2,
234 (XKB_KEYMAP_CMP_ALL &
235 ~(XKB_KEYMAP_CMP_POSSIBLY_DROPPED))));
236 xkb_keymap_unref(keymap2);
237 }
238
239 /*
240 * Check pretty/no-pretty round-trip, i.e. check that changing the
241 * pretty-print flags results in the same keymap, for both keymap
242 * objects and serializations.
243 */
244
245 const enum xkb_keymap_serialize_flags serialize_flags =
246 (data[k].serialize_flags & XKB_KEYMAP_SERIALIZE_PRETTY)
247 ? (data[k].serialize_flags & ~XKB_KEYMAP_SERIALIZE_PRETTY)
248 : (data[k].serialize_flags | XKB_KEYMAP_SERIALIZE_PRETTY);
249 original = xkb_keymap_get_as_string2(keymap, data[k].format,
250 serialize_flags);
251 assert(original);
252
253 struct xkb_keymap *keymap2 = xkb_keymap_new_from_string(
254 ctx, original, data[k].format, XKB_KEYMAP_COMPILE_NO_FLAGS
255 );
256 assert(keymap2);
257 assert(xkb_keymap_compare(ctx, keymap, keymap2, XKB_KEYMAP_CMP_ALL));
258 xkb_keymap_unref(keymap2);
259
260 assert(test_compile_output2(ctx, data[k].format,
261 XKB_KEYMAP_USE_ORIGINAL_FORMAT,
262 data[k].serialize_flags,
263 compile_string, NULL,
264 "Round-trip with different serialize flags",
265 original, 0 /* unused */, data[k].path,
266 update_output_files));
267 free(original);
268 xkb_keymap_unref(keymap);
269 }
270
271 /* Make sure we can recompile our output for a normal keymap from rules. */
272 keymap = test_compile_rules(ctx, XKB_KEYMAP_FORMAT_TEXT_V1, NULL,
273 NULL, "ru,ca,de,us", ",multix,neo,intl", NULL);
274 assert(keymap);
275 dump = xkb_keymap_get_as_string2(keymap, XKB_KEYMAP_USE_ORIGINAL_FORMAT,
276 TEST_KEYMAP_SERIALIZE_FLAGS);
277 assert(dump);
278 xkb_keymap_unref(keymap);
279 keymap = test_compile_string(ctx, XKB_KEYMAP_FORMAT_TEXT_V1, dump);
280 assert(keymap);
281 /* Now test that the dump of the dump is equal to the dump! */
282 dump2 = xkb_keymap_get_as_string2(keymap, XKB_KEYMAP_USE_ORIGINAL_FORMAT,
283 TEST_KEYMAP_SERIALIZE_FLAGS);
284 assert(dump2);
285 assert(streq(dump, dump2));
286
287 /* Test response to invalid formats and flags. */
288 assert(!xkb_keymap_new_from_string(ctx, dump, 0, 0));
289 assert(!xkb_keymap_new_from_string(ctx, dump, -1, 0));
290 assert(!xkb_keymap_new_from_string(ctx, dump, XKB_KEYMAP_USE_ORIGINAL_FORMAT, 0));
291 assert(!xkb_keymap_new_from_string(ctx, dump, XKB_KEYMAP_FORMAT_TEXT_V2+1, 0));
292 assert(!xkb_keymap_new_from_string(ctx, dump, XKB_KEYMAP_FORMAT_TEXT_V1, -1));
293 assert(!xkb_keymap_new_from_string(ctx, dump, XKB_KEYMAP_FORMAT_TEXT_V1, 1414));
294 assert(!xkb_keymap_get_as_string2(keymap, XKB_KEYMAP_USE_ORIGINAL_FORMAT, -1));
295 assert(!xkb_keymap_get_as_string2(keymap, XKB_KEYMAP_USE_ORIGINAL_FORMAT, 3333));
296 assert(!xkb_keymap_get_as_string2(keymap, 0, TEST_KEYMAP_SERIALIZE_FLAGS));
297 assert(!xkb_keymap_get_as_string2(keymap, 4893, TEST_KEYMAP_SERIALIZE_FLAGS));
298
299 xkb_keymap_unref(keymap);
300 free(dump);
301 free(dump2);
302
303 test_keymap_comparison(ctx);
304 test_explicit_actions(ctx);
305
306 xkb_context_unref(ctx);
307
308 return EXIT_SUCCESS;
309 }