Debian Codegraph @

test/rules-file.c

raw
1 /*
2 * Copyright © 2012 Ran Benita <[email protected]>
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "config.h"
7
8 #include <stdlib.h>
9 #include <string.h>
10
11 #include "xkbcommon/xkbcommon.h"
12
13 #include "context.h"
14 #include "keymap.h"
15 #include "utils.h"
16 #include "xkbcomp/rules.h"
17 #include "test.h"
18
19 struct test_data {
20 /* Rules file */
21 const char *rules;
22
23 /* Input */
24 const char *model;
25 const char *layout;
26 const char *variant;
27 const char *options;
28
29 /* Expected output */
30 const char *keycodes;
31 const char *types;
32 const char *compat;
33 const char *symbols;
34 const char *geometry;
35 xkb_layout_index_t explicit_layouts;
36
37 /* Or set this if xkb_components_from_rules() should fail. */
38 bool should_fail;
39 };
40
41 static bool
42 test_rules(struct xkb_context *ctx, const struct test_data *data)
43 {
44 fprintf(stderr, "\n\nChecking : %s\t%s\t%s\t%s\t%s\n", data->rules,
45 data->model, data->layout, data->variant, data->options);
46
47 if (data->should_fail)
48 fprintf(stderr, "Expecting: FAILURE\n");
49 else
50 fprintf(stderr, "Expecting: %s\t%s\t%s\t%s\t%s\t%u\n",
51 data->keycodes, data->types, data->compat, data->symbols,
52 data->geometry, data->explicit_layouts);
53
54 bool passed = true;
55 xkb_layout_index_t explicit_layouts = 0;
56 for (int k = 0; k < 2; k++) {
57 bool ok;
58 const struct xkb_rule_names rmlvo = {
59 data->rules, data->model, data->layout, data->variant, data->options
60 };
61 struct xkb_component_names kccgst;
62
63 if (k == 0) {
64 /* Private API */
65 ok = xkb_components_from_rules_names(ctx, &rmlvo, &kccgst,
66 &explicit_layouts);
67 } else {
68 /* Public API */
69 ok = xkb_components_names_from_rules(ctx, &rmlvo, NULL, &kccgst);
70 }
71 if (ok) {
72 fprintf(stderr, "Received : %s\t%s\t%s\t%s\t%s\t%u\n",
73 kccgst.keycodes, kccgst.types, kccgst.compatibility,
74 kccgst.symbols, kccgst.geometry, explicit_layouts);
75 } else {
76 fprintf(stderr, "Received : FAILURE\n");
77 return data->should_fail;
78 }
79
80 passed &= streq_not_null(kccgst.keycodes, data->keycodes) &&
81 streq_not_null(kccgst.types, data->types) &&
82 streq_not_null(kccgst.compatibility, data->compat) &&
83 streq_not_null(kccgst.symbols, data->symbols) &&
84 streq_null(kccgst.geometry, data->geometry) &&
85 explicit_layouts == data->explicit_layouts;
86
87 free(kccgst.keycodes);
88 free(kccgst.types);
89 free(kccgst.compatibility);
90 free(kccgst.symbols);
91 free(kccgst.geometry);
92 }
93
94 return passed;
95 }
96
97 static void
98 test_encodings(struct xkb_context *ctx)
99 {
100 static const struct test_data tests[] = {
101 {
102 .rules = "utf-8_with_bom",
103
104 .model = "my_model", .layout = "my_layout", .variant = "my_variant",
105 .options = "my_option",
106
107 .keycodes = "my_keycodes", .types = "my_types",
108 .compat = "my_compat|some:compat",
109 .symbols = "my_symbols+extra_variant",
110 .explicit_layouts = 1,
111 },
112 {
113 .rules = "utf-16le_with_bom",
114
115 .model = "my_model", .layout = "my_layout", .variant = "my_variant",
116 .options = "my_option",
117
118 .keycodes = "my_keycodes", .types = "my_types",
119 .compat = "my_compat|some:compat",
120 .symbols = "my_symbols+extra_variant",
121 .explicit_layouts = 1,
122 .should_fail = true,
123 },
124 {
125 .rules = "utf-16be_with_bom",
126
127 .model = "my_model", .layout = "my_layout", .variant = "my_variant",
128 .options = "my_option",
129
130 .keycodes = "my_keycodes", .types = "my_types",
131 .compat = "my_compat|some:compat",
132 .symbols = "my_symbols+extra_variant",
133 .explicit_layouts = 1,
134 .should_fail = true,
135 },
136 {
137 .rules = "utf-32be",
138
139 .model = "my_model", .layout = "my_layout", .variant = "my_variant",
140 .options = "my_option",
141
142 .keycodes = "my_keycodes", .types = "my_types",
143 .compat = "my_compat|some:compat",
144 .symbols = "my_symbols+extra_variant",
145 .explicit_layouts = 1,
146 .should_fail = true,
147 }
148 };
149
150 for (unsigned int k = 0; k < ARRAY_SIZE(tests); k++) {
151 fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, k);
152 assert(test_rules(ctx, &tests[k]));
153 }
154 }
155
156 /* Only parse strict decimal groups */
157 static void
158 test_strict_decimal_groups(struct xkb_context *ctx)
159 {
160 static const struct test_data tests[] = {
161 {
162 .rules = "invalid-group-index",
163
164 .model = "my_model", .layout = "1,2", .variant = NULL,
165 .options = NULL,
166
167 .keycodes = "default_keycodes", .types = "default_types",
168 .compat = "default_compat",
169 .symbols = "default_symbols+default_symbols:2",
170 .explicit_layouts = 2,
171 .should_fail = true,
172 },
173 {
174 .rules = "invalid-group-qualifier",
175
176 .model = "my_model", .layout = "1,2", .variant = NULL,
177 .options = NULL,
178
179 .keycodes = "default_keycodes", .types = "default_types",
180 .compat = "default_compat",
181 .symbols = "default_symbols+default_symbols:+2",
182 .explicit_layouts = 1,
183 }
184 };
185
186 for (unsigned int k = 0; k < ARRAY_SIZE(tests); k++) {
187 fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, k);
188 assert(test_rules(ctx, &tests[k]));
189 }
190 }
191
192 static void
193 test_simple(struct xkb_context *ctx)
194 {
195 static const struct test_data tests[] = {
196 {
197 .rules = "simple",
198
199 .model = "my_model", .layout = "my_layout", .variant = "my_variant",
200 .options = "my_option",
201
202 .keycodes = "my_keycodes", .types = "my_types",
203 .compat = "my_compat|some:compat",
204 .symbols = "my_symbols+extra_variant",
205 .explicit_layouts = 1,
206 },
207 {
208 .rules = "simple",
209
210 .model = "", .layout = "foo", .variant = "", .options = "",
211
212 .keycodes = "default_keycodes", .types = "default_types",
213 .compat = "default_compat", .symbols = "default_symbols",
214 .explicit_layouts = 1,
215 },
216 {
217 .rules = "groups",
218
219 .model = "pc104", .layout = "foo", .variant = "", .options = "",
220
221 .keycodes = "something(pc104)", .types = "default_types",
222 .compat = "default_compat", .symbols = "default_symbols",
223 .explicit_layouts = 1,
224 },
225 {
226 .rules = "groups",
227
228 .model = "foo", .layout = "ar", .variant = "bar", .options = "",
229
230 .keycodes = "default_keycodes", .types = "default_types",
231 .compat = "default_compat", .symbols = "my_symbols+(bar)",
232 .explicit_layouts = 1,
233 },
234 {
235 .rules = "simple",
236
237 .model = NULL, .layout = "my_layout,second_layout", .variant = "my_variant",
238 .options = "my_option",
239
240 .should_fail = true
241 },
242 {
243 .rules = "index",
244
245 .model = "", .layout = "br,al,cn,az", .variant = "",
246 .options = "some:opt",
247
248 .keycodes = "default_keycodes", .types = "default_types",
249 .compat = "default_compat",
250 .symbols = "default_symbols+extra:1+extra:2+extra:3+extra:4",
251 .explicit_layouts = 4,
252 },
253 {
254 .rules = "multiple-options",
255
256 .model = "my_model", .layout = "my_layout", .variant = "my_variant",
257 .options = "option3,option1,colon:opt,option11",
258
259 .keycodes = "my_keycodes", .types = "my_types",
260 .compat = "my_compat+some:compat+group(bla)",
261 .symbols = "my_symbols+extra_variant+compose(foo)+keypad(bar)+altwin(menu)",
262 .explicit_layouts = 1,
263 },
264 /* “replace” merge mode */
265 {
266 .rules = "merge-mode-replace",
267
268 .model = "my_model", .layout = "us,de", .variant = "",
269 .options = "replace:first",
270
271 .keycodes = "evdev", .types = "complete",
272 .compat = "complete",
273 .symbols = "pc+us+de:2^level3(ralt_alt)|empty",
274 .explicit_layouts = 2,
275 }
276 };
277
278 for (unsigned int k = 0; k < ARRAY_SIZE(tests); k++) {
279 fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, k);
280 assert(test_rules(ctx, &tests[k]));
281 }
282 }
283
284 static void
285 test_wild_card(struct xkb_context *ctx)
286 {
287 /* Wild card does not match empty entries for layouts and variants */
288 #define ENTRY(_model, _layout, _variant, _options, _symbols, _layouts, _fail) \
289 { .rules = "wildcard", .model = (_model), \
290 .layout = (_layout), .variant = (_variant), .options = (_options), \
291 .keycodes = "evdev", .types = "complete", .compat = "complete", \
292 .symbols = (_symbols) , .geometry = "pc(pc104)", \
293 .explicit_layouts = (_layouts), .should_fail = (_fail) }
294 static const struct test_data tests[] = {
295 /* OK: empty model and options and at least one layout+variant combo */
296 ENTRY(NULL, "a" , "1" , NULL, "pc+a(1)", 1, false),
297 ENTRY("" , "a" , "1" , "" , "pc+a(1)", 1, false),
298 ENTRY("" , "a," , "1," , "" , "pc+a(1)", 1, false),
299 ENTRY("" , ",b" , ",2" , "" , "+b(2):2", 2, false),
300 ENTRY("" , "a,b", "1," , "" , "pc+a(1)", 1, false),
301 ENTRY("" , "a,b", ",2" , "" , "+b(2):2", 2, false),
302 /* Fails: empty layout or variant */
303 ENTRY(NULL, NULL , NULL , NULL, "", 1, true),
304 ENTRY(NULL, "" , "" , NULL, "", 1, true),
305 ENTRY(NULL, NULL , "1" , NULL, "", 1, true),
306 ENTRY(NULL, "" , "1" , NULL, "", 1, true),
307 ENTRY(NULL, "," , "1,2", NULL, "", 2, true),
308 ENTRY(NULL, "a" , NULL , NULL, "", 1, true),
309 ENTRY(NULL, "a" , "" , NULL, "", 1, true),
310 ENTRY(NULL, "a,b", NULL , NULL, "", 2, true),
311 ENTRY(NULL, "a,b", "" , NULL, "", 2, true),
312 ENTRY(NULL, "a,b", "," , NULL, "", 2, true)
313 };
314 #undef ENTRY
315
316 for (unsigned int k = 0; k < ARRAY_SIZE(tests); k++) {
317 fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, k);
318 assert(test_rules(ctx, &tests[k]));
319 }
320
321 }
322
323 /* Test extended wild cards: <none>, <some> and <any> */
324 static void
325 test_extended_wilcards(struct xkb_context *ctx)
326 {
327 #define ENTRY(_rules, _layout, _variant, _symbols, _layouts, _fail) \
328 { .rules = (_rules), .model = NULL, \
329 .layout = (_layout), .variant = (_variant), .options = NULL, \
330 .keycodes = "evdev", .types = "complete", .compat = "complete", \
331 .symbols = (_symbols) , .explicit_layouts = (_layouts), \
332 .should_fail = (_fail) }
333
334 static const struct test_data tests[] = {
335 ENTRY("extended-wild-cards", "l1", NULL, "pc+l10:1", 1, false),
336 ENTRY("extended-wild-cards", "l1", "v1", "pc+l20:1", 1, false),
337 ENTRY("extended-wild-cards", "l1", "v2", "pc+l30(v2):1", 1, false),
338 /* legacy wild card * does not catch empty variant */
339 ENTRY("extended-wild-cards", "l2", NULL, "pc+l2:1", 1, false),
340 ENTRY("extended-wild-cards", "l2", "v1", "pc+l40(v1):1", 1, false),
341 ENTRY("extended-wild-cards", "l2", "v2", "pc+l40(v2):1", 1, false),
342 ENTRY("extended-wild-cards", "l3", NULL, "pc+l50:1", 1, false),
343 ENTRY("extended-wild-cards", "l3", "v1", "pc+l50(v1):1", 1, false),
344 ENTRY("extended-wild-cards", "l3", "v2", "pc+l50(v2):1", 1, false),
345 /* ? wild card does catch empty variant */
346 ENTRY("extended-wild-cards", "l4", NULL, "pc+l4:1", 1, false),
347 ENTRY("extended-wild-cards", "l4", "v1", "pc+l4(v1):1", 1, false),
348 ENTRY("extended-wild-cards", "l4", "v2", "pc+l4(v20):1", 1, false),
349 ENTRY("extended-wild-cards", "l1,l1,l1,l2", ",v1,v2,",
350 "pc+l10:1+l20:2+l30(v2):3+l2:4", 4, false),
351 ENTRY("extended-wild-cards", "l2,l2,l3,l3", "v1,v2,,v1",
352 "pc+l40(v1):1+l40(v2):2+l50:3+l50(v1):4", 4, false),
353 ENTRY("extended-wild-cards", "l3,l4,l4,l4", "v2,,v1,v2",
354 "pc+l50(v2):1+l4:2+l4(v1):3+l4(v20):4", 4, false),
355 };
356 #undef ENTRY
357
358 for (unsigned int k = 0; k < ARRAY_SIZE(tests); k++) {
359 fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, k);
360 assert(test_rules(ctx, &tests[k]));
361 }
362 }
363
364 static void
365 test_layout_index_ranges(struct xkb_context *ctx, const char *too_much_layouts,
366 const char *too_much_symbols)
367 {
368 #define ENTRY2(_rules, _model, _layout, _variant, _options, \
369 _keycodes, _types, _compat, _symbols, _count, _should_fail) \
370 { .rules = (_rules), .model = (_model), \
371 .layout = (_layout), .variant = (_variant), .options = (_options), \
372 .keycodes = (_keycodes), .types = (_types), .compat = (_compat), \
373 .symbols = (_symbols), \
374 .geometry = (strncmp((_rules), "evdev", 5) == 0) ? "pc(pc104)" : NULL,\
375 .explicit_layouts = (_count), .should_fail = (_should_fail) }
376 #define ENTRY(layout, variant, options, symbols, count, should_fail) \
377 ENTRY2("special_indices", NULL, layout, variant, options, \
378 "default_keycodes", "default_types", "default_compat", symbols, \
379 count, should_fail)
380
381 const struct test_data tests[] = {
382 /* Test index ranges: layout vs layout[first] */
383 ENTRY("layout_a", NULL, NULL, "A", 1, false),
384 ENTRY("layout_e", NULL, NULL, "E+layout_e", 1, false),
385 ENTRY("a", NULL, NULL, "a", 1, false),
386 ENTRY("a", "1", NULL, "a(1)", 1, false),
387 /* Test index ranges: invalid layout qualifier */
388 ENTRY("layout_c", NULL, NULL, "C:1+z:1", 1, false),
389 /* Test index ranges: invalid layout[first] qualifier */
390 ENTRY("layout_d", NULL, NULL, "D", 1, false),
391 /* Test index ranges: multiple layouts */
392 ENTRY("a,b", NULL, NULL, "a+b:2", 2, false),
393 ENTRY("a,b", ",c", NULL, "a+b(c):2", 2, false),
394 ENTRY("layout_e,layout_a", NULL, NULL, "e:1+x:2", 2, false),
395 ENTRY("layout_a,layout_b,layout_c,layout_d", NULL, NULL,
396 "a:1+y:2+layout_c:3+layout_d:4+z:3", 4, false),
397 ENTRY("layout_a,layout_b,layout_c,layout_d",
398 "extra,,,extra", NULL,
399 "a:1+y:2+layout_c:3+layout_d(extra):4+z:3"
400 "+foo:1|bar:1+foo:4|bar:4", 4, false),
401 ENTRY("layout_a,layout_b,layout_c,layout_d,layout_e", NULL, NULL,
402 "a:1+y:2+layout_c:3+layout_d:4+layout_e:5+z:3", 5, false),
403 /* Check that special indices merge the KcCGST values in the expected order */
404 ENTRY("layout_a,layout_b,layout_c", NULL, "option_3,option_2,option_1",
405 "a:1+y:2+layout_c:3+z:3+III:2+JJJ:2+HHH:3+KKK:3+LLL+OOO:2+MMM:3+NNN:3",
406 3, false),
407 #undef ENTRY
408 /* Test index ranges: too much layouts */
409 ENTRY2("special_indices-limit", NULL, too_much_layouts, NULL, NULL,
410 "default_keycodes", "default_types", "default_compat", too_much_symbols,
411 XKB_MAX_GROUPS, false),
412 #define ENTRY(model, layout, variant, options, compat, symbols, count, should_fail) \
413 ENTRY2("evdev-modern", model, layout, variant, options, \
414 "evdev+aliases(qwerty)", "complete", compat, symbols, count, should_fail)
415 /* evdev-modern: 1 layout */
416 ENTRY("whatever", "ar", NULL, NULL, "complete", "pc+ara+inet(evdev)", 1, false),
417 ENTRY("whatever", "ben", "probhat", NULL, "complete", "pc+in(ben_probhat)+inet(evdev)", 1, false),
418 ENTRY("ataritt", "es", NULL, NULL, "complete", "xfree68_vndr/ataritt(us)+es+inet(evdev)", 1, false),
419 ENTRY("ataritt", "jp", NULL, NULL, "complete+japan", "xfree68_vndr/ataritt(us)+jp+inet(evdev)", 1, false),
420 ENTRY2("evdev-modern", "olpc", "us", NULL, NULL,
421 "evdev+olpc(olpc)+aliases(qwerty)", "complete", "olpc", "olpc+us(olpc)+inet(evdev)", 1, false),
422 ENTRY2("evdev-modern", "olpc", "jp", NULL, NULL,
423 "evdev+olpc(olpc)+aliases(qwerty)", "complete", "complete+japan", "olpc+jp+inet(evdev)", 1, false),
424 ENTRY("pc104", "jp", NULL, NULL, "complete+japan", "pc+jp+inet(evdev)", 1, false),
425 ENTRY("pc104", "jp", "xxx", NULL, "complete+japan", "pc+jp(xxx)+inet(evdev)", 1, false),
426 ENTRY("pc104", "es", NULL, NULL, "complete", "pc+es+inet(evdev)", 1, false),
427 ENTRY("pc104", "es", "xxx", NULL, "complete", "pc+es(xxx)+inet(evdev)", 1, false),
428 ENTRY2("evdev-modern", "pc104", "de", "neo", NULL,
429 "evdev+aliases(qwertz)", "complete",
430 "complete+caps(caps_lock):1+misc(assign_shift_left_action):1+level5(level5_lock):1",
431 "pc+de(neo)+inet(evdev)", 1, false),
432 ENTRY("pc104", "br", NULL, "misc:typo,misc:apl", "complete",
433 "pc+br+inet(evdev)+apl(level3):1+typo(base):1", 1, false),
434 /* evdev-modern: 2 layouts */
435 ENTRY("whatever", "ar,pt", NULL, NULL, "complete", "pc+ara+pt:2+inet(evdev)", 2, false),
436 ENTRY("whatever", "pt,ar", NULL, NULL, "complete", "pc+pt+ara:2+inet(evdev)", 2, false),
437 ENTRY("whatever", "ben,gb", "probhat,", NULL, "complete",
438 "pc+in(ben_probhat)+gb:2+inet(evdev)", 2, false),
439 ENTRY("whatever", "gb,ben", ",probhat", NULL, "complete",
440 "pc+gb+in(ben):2+in(ben_probhat):2+inet(evdev)", 2, false),
441 ENTRY("whatever", "ben,ar", "probhat,", NULL, "complete",
442 "pc+in(ben_probhat)+ara:2+inet(evdev)", 2, false),
443 ENTRY("ataritt", "jp,es", NULL, NULL, "complete", "pc+jp+es:2+inet(evdev)", 2, false),
444 ENTRY("ataritt", "es,jp", NULL, NULL, "complete", "pc+es+jp:2+inet(evdev)", 2, false),
445 ENTRY2("evdev-modern", "olpc", "jp,es", NULL, NULL,
446 "evdev+olpc(olpc)+aliases(qwerty)", "complete", "complete", "pc+jp+es:2+inet(evdev)", 2, false),
447 ENTRY2("evdev-modern", "olpc", "es,jp", NULL, NULL,
448 "evdev+olpc(olpc)+aliases(qwerty)", "complete", "complete", "pc+es+jp:2+inet(evdev)", 2, false),
449 ENTRY("pc104", "jp,es", NULL, NULL, "complete", "pc+jp+es:2+inet(evdev)", 2, false),
450 ENTRY("pc104", "jp,es", "xxx,yyy", NULL, "complete", "pc+jp(xxx)+es(yyy):2+inet(evdev)", 2, false),
451 ENTRY("pc104", "latin,jp", NULL, NULL, "complete", "pc+latin+jp:2+inet(evdev)", 2, false),
452 ENTRY("pc104", "latin,jp", "xxx,yyy", NULL, "complete", "pc+latin(xxx)+jp(yyy):2+inet(evdev)", 2, false),
453 ENTRY2("evdev-modern", "pc104", "gb,de", ",neo", NULL,
454 "evdev+aliases(qwerty)", "complete",
455 "complete+caps(caps_lock):2+misc(assign_shift_left_action):2+level5(level5_lock):2",
456 "pc+gb+de(neo):2+inet(evdev)", 2, false),
457 ENTRY("pc104", "ca,br", NULL, "misc:typo,misc:apl", "complete",
458 "pc+ca+br:2+inet(evdev)+apl(level3):1+apl(level3):2+typo(base):1+typo(base):2", 2, false),
459 };
460 #undef ENTRY
461
462 for (unsigned int k = 0; k < ARRAY_SIZE(tests); k++) {
463 fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, k);
464 assert(test_rules(ctx, &tests[k]));
465 }
466 }
467
468 static void
469 test_extended_layout_indices(struct xkb_context *ctx)
470 {
471 char layouts[(2 + MAX_LAYOUT_INDEX_STR_LENGTH) * XKB_MAX_GROUPS] = { 0 };
472 size_t count = 0;
473 for (xkb_layout_index_t l = 0; l < XKB_MAX_GROUPS; l++) {
474 count += snprintf(&layouts[count], sizeof(layouts) - count,
475 "x%"PRIu32",", l + 1);
476 }
477 layouts[--count] = '\0';
478
479 static const char symbols_prefix[] = "pc+x1+";
480 static const char symbols_suffix[] = "+inet(evdev)";
481 char symbols[
482 (3 + 2 * MAX_LAYOUT_INDEX_STR_LENGTH) * XKB_MAX_GROUPS +
483 sizeof(symbols_prefix) + sizeof(symbols_suffix)
484 ] = { 0 };
485 memcpy(symbols, symbols_prefix, sizeof(symbols_prefix));
486 count = sizeof(symbols_prefix) - 1;
487 for (xkb_layout_index_t l = 1; l < XKB_MAX_GROUPS; l++) {
488 count += snprintf(&symbols[count], sizeof(symbols) - count,
489 "x%"PRIu32":%"PRIu32"+", l + 1, l + 1);
490 }
491 memcpy(symbols + count - 1, symbols_suffix, sizeof(symbols_suffix));
492
493 const struct test_data test = {
494 .rules = "evdev-modern",
495
496 .model = "pc104",
497 .layout = layouts, .variant = "", .options = "",
498
499 .keycodes = "evdev+aliases(qwerty)",
500 .compat = "complete",
501 .types = "complete",
502 .symbols = symbols,
503 .geometry = "pc(pc104)",
504
505 .explicit_layouts = XKB_MAX_GROUPS
506 };
507 assert(test_rules(ctx, &test));
508 }
509
510 static void
511 test_all_qualifier(struct xkb_context *ctx,
512 const char *too_much_layouts, const char *too_much_symbols)
513 {
514 const struct test_data tests[] = {
515 /* Test :all qualifier without special indices, with option */
516 {
517 .rules = "all_qualifier",
518
519 .model = "my_model",
520 .layout = "layout_a,layout_b,layout_a,layout_b,layout_c",
521 .variant = "",
522 .options = "my_option",
523
524 .keycodes = "my_keycodes", .types = "my_types",
525 .compat = "my_compat",
526 .symbols = "symbols_a:1+symbols_b:2+symbols_a:3+symbols_b:4+symbols_c:5"
527 "+extra_option:1+extra_option:2+extra_option:3+extra_option:4"
528 "+extra_option:5",
529 .explicit_layouts = 5,
530 },
531 /* Test :all qualifier without special indices, base for all layout */
532 {
533 .rules = "all_qualifier",
534
535 .model = "my_model",
536 .layout = "layout_x,layout_a,layout_b,layout_c,layout_d",
537 .variant = "",
538 .options = "",
539
540 .keycodes = "my_keycodes", .types = "my_types",
541 .compat = "my_compat",
542 .symbols = "base:1+base:2+base:3+base:4+base:5"
543 "+symbols_a:2+symbols_b:3+default_symbols:4+default_symbols:5",
544 .explicit_layouts = 5,
545 },
546 /* Test :all qualifier without special indices, with option, too much layouts */
547 {
548 .rules = "all_qualifier-limit",
549
550 .model = "my_model",
551 .layout = too_much_layouts,
552 .variant = "",
553 .options = "",
554
555 .keycodes = "default_keycodes", .types = "default_types",
556 .compat = "default_compat",
557 .symbols = too_much_symbols,
558 .explicit_layouts = XKB_MAX_GROUPS,
559 },
560 /* Test :all qualifier with special indices */
561 {
562 .rules = "all_qualifier",
563
564 .model = "my_model",
565 .layout = "layout_a,layout_b,layout_a,layout_b,layout_c",
566 .variant = "extra1,,,,",
567 .options = "my_option",
568
569 .keycodes = "my_keycodes", .types = "my_types",
570 .compat = "my_compat",
571 .symbols = "symbols_a:1+symbols_b:2+symbols_a:3+symbols_b:4+symbols_c:5"
572 "+extra_symbols:1+extra_symbols:2+extra_symbols:3+extra_symbols:4+extra_symbols:5"
573 "+extra_option:1+extra_option:2+extra_option:3+extra_option:4+extra_option:5",
574 .explicit_layouts = 5,
575 },
576 /* Test :all qualifier with special indices
577 * It uses :all combined with layout[any], which is valid but
578 * :%i was probably the intended qualified, so raises warning */
579 {
580 .rules = "all_qualifier",
581
582 .model = "my_model",
583 .layout = "layout_a,layout_b,layout_a,layout_b,layout_c",
584 .variant = "extra2,,extra3,,",
585 .options = "my_option",
586
587 .keycodes = "my_keycodes", .types = "my_types",
588 .compat = "my_compat",
589 .symbols = "symbols_a:1+symbols_b:2+symbols_a:3+symbols_b:4+symbols_c:5"
590 "+extra_symbols1:1+extra_symbols2:1+extra_symbols2:2+extra_symbols2:3+extra_symbols2:4+extra_symbols2:5"
591 "+extra_symbols2:1+extra_symbols2:2+extra_symbols2:3+extra_symbols2:4+extra_symbols2:5"
592 "+extra_symbols1:3"
593 "+extra_option:1"
594 "+extra_option:2+extra_option:3+extra_option:4+extra_option:5",
595 .explicit_layouts = 5,
596 }
597 };
598
599 for (unsigned int k = 0; k < ARRAY_SIZE(tests); k++) {
600 fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, k);
601 assert(test_rules(ctx, &tests[k]));
602 }
603 }
604
605 static void
606 test_layout_specific_options(struct xkb_context *ctx)
607 {
608 const struct test_data tests[] = {
609 /*
610 * 1 layout, no layout index
611 */
612 {
613 .rules = "layout-specific-options",
614 .model = "pc104", .layout = "l1", .variant = NULL,
615 .options = "opt1,opt2,opt3,opt4,opt5,opt6,opt7",
616 .keycodes = "evdev", .compat = "complete", .types = "complete",
617 .symbols = "pc+l1:1+s1:1+s3:1+s7",
618 .explicit_layouts = 1
619 },
620 {
621 .rules = "layout-specific-options",
622 .model = "pc104", .layout = "l2", .variant = NULL,
623 .options = "opt1,opt2,opt3,opt4,opt5,opt6,opt7",
624 .keycodes = "evdev", .compat = "complete", .types = "complete",
625 .symbols = "pc+l2:1+s1:1+s2:1+s3:1+s4:1+s7",
626 .explicit_layouts = 1
627 },
628 /*
629 * 1 layout, invalid layout index
630 */
631 {
632 .rules = "layout-specific-options",
633 .model = "pc104", .layout = "l1", .variant = NULL,
634 .options = "opt1!,opt2!1x,opt3!x,opt4!x1,opt5!!,opt6!+,opt7!|",
635 .keycodes = "evdev", .compat = "complete", .types = "complete",
636 .symbols = "pc+l1:1+s1:1+s3:1+s7",
637 .explicit_layouts = 1
638 },
639 /*
640 * 1 layout, matching layout index
641 */
642 {
643 .rules = "layout-specific-options",
644 .model = "pc104", .layout = "l1", .variant = NULL,
645 .options = "opt1!1,opt2!1,opt3!1,opt4!1,opt5!1,opt6!1,opt7!1",
646 .keycodes = "evdev", .compat = "complete", .types = "complete",
647 .symbols = "pc+l1:1+s1:1+s3:1",
648 .explicit_layouts = 1
649 },
650 {
651 .rules = "layout-specific-options",
652 .model = "pc104", .layout = "l2", .variant = NULL,
653 .options = "opt1!1,opt2!1,opt3!1,opt4!1,opt5!1,opt6!1,opt7!1",
654 .keycodes = "evdev", .compat = "complete", .types = "complete",
655 .symbols = "pc+l2:1+s1:1+s2:1+s3:1+s4:1",
656 .explicit_layouts = 1
657 },
658 /*
659 * 1 layout, non-matching layout index
660 */
661 {
662 .rules = "layout-specific-options",
663 .model = "pc104", .layout = "l1", .variant = NULL,
664 .options = "opt1!2,opt2!2,opt3!2,opt4!2,opt5!2,opt6!2,opt7!2",
665 .keycodes = "evdev", .compat = "complete", .types = "complete",
666 .symbols = "pc+l1:1",
667 .explicit_layouts = 1
668 },
669 {
670 .rules = "layout-specific-options",
671 .model = "pc104", .layout = "l2", .variant = NULL,
672 .options = "opt1!2,opt2!2,opt3!2,opt4!2,opt5!2,opt6!2,opt7!2",
673 .keycodes = "evdev", .compat = "complete", .types = "complete",
674 .symbols = "pc+l2:1",
675 .explicit_layouts = 1
676 },
677 /*
678 * 2 layouts: no specifier
679 */
680 {
681 .rules = "layout-specific-options",
682 .model = "pc104", .layout = "l1,l2", .variant = NULL,
683 .options = "opt1,opt2,opt3,opt4,opt5,opt6,opt7",
684 .keycodes = "evdev", .compat = "complete", .types = "complete",
685 .symbols = "pc+l1:1+l2:2+s1:1+s3:1+s3:2+s4:2+s5:1+s6:2+s7",
686 .explicit_layouts = 2
687 },
688 /* 2 layout: non-matching index */
689 {
690 .rules = "layout-specific-options",
691 .model = "pc104", .layout = "l1,l2", .variant = NULL,
692 .options = "opt1!3,opt2!3,opt3!3,opt4!3,opt5!3,opt6!3,opt7!3",
693 .keycodes = "evdev", .compat = "complete", .types = "complete",
694 .symbols = "pc+l1:1+l2:2",
695 .explicit_layouts = 2
696 },
697 /*
698 * 2 layouts: specify only 1 layout for each option
699 */
700 {
701 .rules = "layout-specific-options",
702 .model = "pc104", .layout = "l1,l2", .variant = NULL,
703 .options = "opt1!1,opt2!1,opt3!1,opt4!1,opt5!1,opt6!1,opt7!1",
704 .keycodes = "evdev", .compat = "complete", .types = "complete",
705 .symbols = "pc+l1:1+l2:2+s1:1+s3:1+s5:1",
706 .explicit_layouts = 2
707 },
708 {
709 .rules = "layout-specific-options",
710 .model = "pc104", .layout = "l1,l2", .variant = NULL,
711 .options = "opt1!2,opt2!2,opt3!2,opt4!2,opt5!2,opt6!2,opt7!2",
712 .keycodes = "evdev", .compat = "complete", .types = "complete",
713 .symbols = "pc+l1:1+l2:2+s3:2+s4:2+s6:2",
714 .explicit_layouts = 2
715 },
716 /*
717 * 2 layouts: specify 2 layouts for each option
718 */
719 {
720 .rules = "layout-specific-options",
721 .model = "pc104", .layout = "l1,l2", .variant = NULL,
722 .options = "opt1!1,opt1!2,opt2!1,opt2!2,opt3!1,opt3!2,"
723 "opt4!1,opt4!2,opt5!1,opt5!2,opt6!1,opt6!2,opt7!1,opt7!2",
724 .keycodes = "evdev", .compat = "complete", .types = "complete",
725 .symbols = "pc+l1:1+l2:2+s1:1+s3:1+s3:2+s4:2+s5:1+s6:2",
726 .explicit_layouts = 2
727 },
728 };
729 for (unsigned int k = 0; k < ARRAY_SIZE(tests); k++) {
730 fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, k);
731 assert(test_rules(ctx, &tests[k]));
732 }
733 }
734
735 static void
736 test_partial_rules(struct xkb_context *ctx)
737 {
738 /*
739 * 1 include path
740 */
741
742 const struct test_data tests_1[] = {
743 /* Does not trigger partial rules */
744 {
745 .rules = "partial",
746 .model = "pc104", .layout = "l1", .variant = NULL,
747 .options = NULL,
748 .keycodes = "evdev", .compat = "complete", .types = "complete",
749 .symbols = "pc+l1:1",
750 .explicit_layouts = 1
751 },
752 /* Does trigger pre-partial rules */
753 {
754 .rules = "partial",
755 .model = "m1", .layout = "l1", .variant = NULL,
756 .options = NULL,
757 .keycodes = "m1", .compat = "complete", .types = "complete",
758 .symbols = "m1+l1:1",
759 .explicit_layouts = 1
760 },
761 /* Does trigger post-partial rules */
762 {
763 .rules = "partial",
764 .model = "pc104", .layout = "l1,l2", .variant = NULL,
765 .options = "opt:a!1",
766 .keycodes = "evdev", .compat = "complete", .types = "complete",
767 .symbols = "pc+l1:1+l2:2+extra:2+a:1",
768 .explicit_layouts = 2
769 },
770 /* Does trigger both pre- and post-partial rules */
771 {
772 .rules = "partial",
773 .model = "m1", .layout = "l1,l2", .variant = NULL,
774 .options = "opt:a!1",
775 .keycodes = "m1", .compat = "complete", .types = "complete",
776 .symbols = "m1+l1:1+l2:2+extra:2+a:1",
777 .explicit_layouts = 2
778 },
779 };
780 for (unsigned int k = 0; k < ARRAY_SIZE(tests_1); k++) {
781 fprintf(stderr, "------\n*** %s: #%u (1 include path) ***\n", __func__, k);
782 assert(test_rules(ctx, &tests_1[k]));
783 }
784
785
786 /*
787 * 2 include paths
788 */
789
790 ctx = test_get_context(CONTEXT_NO_FLAG);
791 assert(ctx);
792 char *extra = test_get_path("extra");
793 assert(extra);
794 xkb_context_include_path_append(ctx, extra);
795 free(extra);
796
797 const struct test_data tests_2[] = {
798 /* Does not trigger partial rules */
799 {
800 .rules = "partial",
801 .model = "pc104", .layout = "l1", .variant = NULL,
802 .options = NULL,
803 .keycodes = "evdev", .compat = "complete", .types = "complete",
804 .symbols = "pc+l1:1",
805 .explicit_layouts = 1
806 },
807 /* Does trigger pre-partial rules */
808 {
809 .rules = "partial",
810 .model = "m1", .layout = "l1", .variant = NULL,
811 .options = NULL,
812 .keycodes = "m1+m1-ter", .compat = "complete", .types = "complete",
813 .symbols = "m1+l1:1",
814 .explicit_layouts = 1
815 },
816 /* Does trigger post-partial rules */
817 {
818 .rules = "partial",
819 .model = "pc104", .layout = "l1,l2", .variant = NULL,
820 .options = "opt:a!1",
821 .keycodes = "evdev", .compat = "complete", .types = "complete",
822 .symbols = "pc+l1:1+l2:2+extra:2+a:1+extra-bis:2+a-bis:1",
823 .explicit_layouts = 2
824 },
825 /* Does trigger both pre- and post-partial rules */
826 {
827 .rules = "partial",
828 .model = "m1", .layout = "l1,l2", .variant = NULL,
829 .options = "opt:a!1",
830 .keycodes = "m1+m1-ter", .compat = "complete", .types = "complete",
831 .symbols = "m1+l1:1+l2:2+extra:2+a:1+extra-bis:2+a-bis:1",
832 .explicit_layouts = 2
833 },
834 };
835 for (unsigned int k = 0; k < ARRAY_SIZE(tests_2); k++) {
836 fprintf(stderr, "------\n*** %s: #%u (2 include paths) ***\n", __func__, k);
837 assert(test_rules(ctx, &tests_2[k]));
838 }
839 xkb_context_unref(ctx);
840 }
841
842 int
843 main(int argc, char *argv[])
844 {
845 struct xkb_context *ctx;
846
847 test_init();
848
849 ctx = test_get_context(CONTEXT_NO_FLAG);
850 assert(ctx);
851
852 test_encodings(ctx);
853 test_strict_decimal_groups(ctx);
854 test_simple(ctx);
855 test_wild_card(ctx);
856 test_extended_wilcards(ctx);
857
858 /* Prepare data with too much layouts */
859 char too_much_layouts[(2 + MAX_LAYOUT_INDEX_STR_LENGTH) * (XKB_MAX_GROUPS + 1)] = { 0 };
860 char too_much_symbols[(3 + MAX_LAYOUT_INDEX_STR_LENGTH) * XKB_MAX_GROUPS] = { 0 };
861 size_t i = 0;
862 for (xkb_layout_index_t l = 0; l <= XKB_MAX_GROUPS; l++) {
863 i += snprintf(&too_much_layouts[i], sizeof(too_much_layouts) - i,
864 "x%"PRIu32",", l + 1);
865 }
866 too_much_layouts[--i] = '\0';
867 i = 0;
868 for (xkb_layout_index_t l = 0; l < XKB_MAX_GROUPS; l++) {
869 i += snprintf(&too_much_symbols[i], sizeof(too_much_symbols) - i,
870 "x:%"PRIu32"+", l + 1);
871 }
872 too_much_symbols[--i] = '\0';
873
874 test_layout_index_ranges(ctx, too_much_layouts, too_much_symbols);
875 test_extended_layout_indices(ctx);
876 test_all_qualifier(ctx, too_much_layouts, too_much_symbols);
877 test_layout_specific_options(ctx);
878 test_partial_rules(ctx);
879
880 xkb_context_unref(ctx);
881 return EXIT_SUCCESS;
882 }