xref: /aosp_15_r20/external/libxkbcommon/test/rules-file.c (revision 2b949d0487e80d67f1fda82db69e101e761f8064)
1*2b949d04SAndroid Build Coastguard Worker /*
2*2b949d04SAndroid Build Coastguard Worker  * Copyright © 2012 Ran Benita <[email protected]>
3*2b949d04SAndroid Build Coastguard Worker  *
4*2b949d04SAndroid Build Coastguard Worker  * Permission is hereby granted, free of charge, to any person obtaining a
5*2b949d04SAndroid Build Coastguard Worker  * copy of this software and associated documentation files (the "Software"),
6*2b949d04SAndroid Build Coastguard Worker  * to deal in the Software without restriction, including without limitation
7*2b949d04SAndroid Build Coastguard Worker  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*2b949d04SAndroid Build Coastguard Worker  * and/or sell copies of the Software, and to permit persons to whom the
9*2b949d04SAndroid Build Coastguard Worker  * Software is furnished to do so, subject to the following conditions:
10*2b949d04SAndroid Build Coastguard Worker  *
11*2b949d04SAndroid Build Coastguard Worker  * The above copyright notice and this permission notice (including the next
12*2b949d04SAndroid Build Coastguard Worker  * paragraph) shall be included in all copies or substantial portions of the
13*2b949d04SAndroid Build Coastguard Worker  * Software.
14*2b949d04SAndroid Build Coastguard Worker  *
15*2b949d04SAndroid Build Coastguard Worker  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16*2b949d04SAndroid Build Coastguard Worker  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17*2b949d04SAndroid Build Coastguard Worker  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18*2b949d04SAndroid Build Coastguard Worker  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19*2b949d04SAndroid Build Coastguard Worker  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20*2b949d04SAndroid Build Coastguard Worker  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21*2b949d04SAndroid Build Coastguard Worker  * DEALINGS IN THE SOFTWARE.
22*2b949d04SAndroid Build Coastguard Worker  */
23*2b949d04SAndroid Build Coastguard Worker 
24*2b949d04SAndroid Build Coastguard Worker #include "config.h"
25*2b949d04SAndroid Build Coastguard Worker 
26*2b949d04SAndroid Build Coastguard Worker #include "test.h"
27*2b949d04SAndroid Build Coastguard Worker #include "xkbcomp/xkbcomp-priv.h"
28*2b949d04SAndroid Build Coastguard Worker #include "xkbcomp/rules.h"
29*2b949d04SAndroid Build Coastguard Worker 
30*2b949d04SAndroid Build Coastguard Worker struct test_data {
31*2b949d04SAndroid Build Coastguard Worker     /* Rules file */
32*2b949d04SAndroid Build Coastguard Worker     const char *rules;
33*2b949d04SAndroid Build Coastguard Worker 
34*2b949d04SAndroid Build Coastguard Worker     /* Input */
35*2b949d04SAndroid Build Coastguard Worker     const char *model;
36*2b949d04SAndroid Build Coastguard Worker     const char *layout;
37*2b949d04SAndroid Build Coastguard Worker     const char *variant;
38*2b949d04SAndroid Build Coastguard Worker     const char *options;
39*2b949d04SAndroid Build Coastguard Worker 
40*2b949d04SAndroid Build Coastguard Worker     /* Expected output */
41*2b949d04SAndroid Build Coastguard Worker     const char *keycodes;
42*2b949d04SAndroid Build Coastguard Worker     const char *types;
43*2b949d04SAndroid Build Coastguard Worker     const char *compat;
44*2b949d04SAndroid Build Coastguard Worker     const char *symbols;
45*2b949d04SAndroid Build Coastguard Worker 
46*2b949d04SAndroid Build Coastguard Worker     /* Or set this if xkb_components_from_rules() should fail. */
47*2b949d04SAndroid Build Coastguard Worker     bool should_fail;
48*2b949d04SAndroid Build Coastguard Worker };
49*2b949d04SAndroid Build Coastguard Worker 
50*2b949d04SAndroid Build Coastguard Worker static bool
test_rules(struct xkb_context * ctx,struct test_data * data)51*2b949d04SAndroid Build Coastguard Worker test_rules(struct xkb_context *ctx, struct test_data *data)
52*2b949d04SAndroid Build Coastguard Worker {
53*2b949d04SAndroid Build Coastguard Worker     bool passed;
54*2b949d04SAndroid Build Coastguard Worker     const struct xkb_rule_names rmlvo = {
55*2b949d04SAndroid Build Coastguard Worker         data->rules, data->model, data->layout, data->variant, data->options
56*2b949d04SAndroid Build Coastguard Worker     };
57*2b949d04SAndroid Build Coastguard Worker     struct xkb_component_names kccgst;
58*2b949d04SAndroid Build Coastguard Worker 
59*2b949d04SAndroid Build Coastguard Worker     fprintf(stderr, "\n\nChecking : %s\t%s\t%s\t%s\t%s\n", data->rules,
60*2b949d04SAndroid Build Coastguard Worker             data->model, data->layout, data->variant, data->options);
61*2b949d04SAndroid Build Coastguard Worker 
62*2b949d04SAndroid Build Coastguard Worker     if (data->should_fail)
63*2b949d04SAndroid Build Coastguard Worker         fprintf(stderr, "Expecting: FAILURE\n");
64*2b949d04SAndroid Build Coastguard Worker     else
65*2b949d04SAndroid Build Coastguard Worker         fprintf(stderr, "Expecting: %s\t%s\t%s\t%s\n",
66*2b949d04SAndroid Build Coastguard Worker                 data->keycodes, data->types, data->compat, data->symbols);
67*2b949d04SAndroid Build Coastguard Worker 
68*2b949d04SAndroid Build Coastguard Worker     if (!xkb_components_from_rules(ctx, &rmlvo, &kccgst)) {
69*2b949d04SAndroid Build Coastguard Worker         fprintf(stderr, "Received : FAILURE\n");
70*2b949d04SAndroid Build Coastguard Worker         return data->should_fail;
71*2b949d04SAndroid Build Coastguard Worker     }
72*2b949d04SAndroid Build Coastguard Worker 
73*2b949d04SAndroid Build Coastguard Worker     fprintf(stderr, "Received : %s\t%s\t%s\t%s\n",
74*2b949d04SAndroid Build Coastguard Worker             kccgst.keycodes, kccgst.types, kccgst.compat, kccgst.symbols);
75*2b949d04SAndroid Build Coastguard Worker 
76*2b949d04SAndroid Build Coastguard Worker     passed = streq(kccgst.keycodes, data->keycodes) &&
77*2b949d04SAndroid Build Coastguard Worker              streq(kccgst.types, data->types) &&
78*2b949d04SAndroid Build Coastguard Worker              streq(kccgst.compat, data->compat) &&
79*2b949d04SAndroid Build Coastguard Worker              streq(kccgst.symbols, data->symbols);
80*2b949d04SAndroid Build Coastguard Worker 
81*2b949d04SAndroid Build Coastguard Worker     free(kccgst.keycodes);
82*2b949d04SAndroid Build Coastguard Worker     free(kccgst.types);
83*2b949d04SAndroid Build Coastguard Worker     free(kccgst.compat);
84*2b949d04SAndroid Build Coastguard Worker     free(kccgst.symbols);
85*2b949d04SAndroid Build Coastguard Worker 
86*2b949d04SAndroid Build Coastguard Worker     return passed;
87*2b949d04SAndroid Build Coastguard Worker }
88*2b949d04SAndroid Build Coastguard Worker 
89*2b949d04SAndroid Build Coastguard Worker int
main(int argc,char * argv[])90*2b949d04SAndroid Build Coastguard Worker main(int argc, char *argv[])
91*2b949d04SAndroid Build Coastguard Worker {
92*2b949d04SAndroid Build Coastguard Worker     struct xkb_context *ctx;
93*2b949d04SAndroid Build Coastguard Worker 
94*2b949d04SAndroid Build Coastguard Worker     ctx = test_get_context(0);
95*2b949d04SAndroid Build Coastguard Worker     assert(ctx);
96*2b949d04SAndroid Build Coastguard Worker 
97*2b949d04SAndroid Build Coastguard Worker     struct test_data test1 = {
98*2b949d04SAndroid Build Coastguard Worker         .rules = "simple",
99*2b949d04SAndroid Build Coastguard Worker 
100*2b949d04SAndroid Build Coastguard Worker         .model = "my_model", .layout = "my_layout", .variant = "my_variant",
101*2b949d04SAndroid Build Coastguard Worker         .options = "my_option",
102*2b949d04SAndroid Build Coastguard Worker 
103*2b949d04SAndroid Build Coastguard Worker         .keycodes = "my_keycodes", .types = "my_types",
104*2b949d04SAndroid Build Coastguard Worker         .compat = "my_compat|some:compat",
105*2b949d04SAndroid Build Coastguard Worker         .symbols = "my_symbols+extra_variant",
106*2b949d04SAndroid Build Coastguard Worker     };
107*2b949d04SAndroid Build Coastguard Worker     assert(test_rules(ctx, &test1));
108*2b949d04SAndroid Build Coastguard Worker 
109*2b949d04SAndroid Build Coastguard Worker     struct test_data test2 = {
110*2b949d04SAndroid Build Coastguard Worker         .rules = "simple",
111*2b949d04SAndroid Build Coastguard Worker 
112*2b949d04SAndroid Build Coastguard Worker         .model = "", .layout = "", .variant = "", .options = "",
113*2b949d04SAndroid Build Coastguard Worker 
114*2b949d04SAndroid Build Coastguard Worker         .keycodes = "default_keycodes", .types = "default_types",
115*2b949d04SAndroid Build Coastguard Worker         .compat = "default_compat", .symbols = "default_symbols",
116*2b949d04SAndroid Build Coastguard Worker     };
117*2b949d04SAndroid Build Coastguard Worker     assert(test_rules(ctx, &test2));
118*2b949d04SAndroid Build Coastguard Worker 
119*2b949d04SAndroid Build Coastguard Worker     struct test_data test3 = {
120*2b949d04SAndroid Build Coastguard Worker         .rules = "groups",
121*2b949d04SAndroid Build Coastguard Worker 
122*2b949d04SAndroid Build Coastguard Worker         .model = "pc104", .layout = "foo", .variant = "", .options = "",
123*2b949d04SAndroid Build Coastguard Worker 
124*2b949d04SAndroid Build Coastguard Worker         .keycodes = "something(pc104)", .types = "default_types",
125*2b949d04SAndroid Build Coastguard Worker         .compat = "default_compat", .symbols = "default_symbols",
126*2b949d04SAndroid Build Coastguard Worker     };
127*2b949d04SAndroid Build Coastguard Worker     assert(test_rules(ctx, &test3));
128*2b949d04SAndroid Build Coastguard Worker 
129*2b949d04SAndroid Build Coastguard Worker     struct test_data test4 = {
130*2b949d04SAndroid Build Coastguard Worker         .rules = "groups",
131*2b949d04SAndroid Build Coastguard Worker 
132*2b949d04SAndroid Build Coastguard Worker         .model = "foo", .layout = "ar", .variant = "bar", .options = "",
133*2b949d04SAndroid Build Coastguard Worker 
134*2b949d04SAndroid Build Coastguard Worker         .keycodes = "default_keycodes", .types = "default_types",
135*2b949d04SAndroid Build Coastguard Worker         .compat = "default_compat", .symbols = "my_symbols+(bar)",
136*2b949d04SAndroid Build Coastguard Worker     };
137*2b949d04SAndroid Build Coastguard Worker     assert(test_rules(ctx, &test4));
138*2b949d04SAndroid Build Coastguard Worker 
139*2b949d04SAndroid Build Coastguard Worker     struct test_data test5 = {
140*2b949d04SAndroid Build Coastguard Worker         .rules = "simple",
141*2b949d04SAndroid Build Coastguard Worker 
142*2b949d04SAndroid Build Coastguard Worker         .model = NULL, .layout = "my_layout,second_layout", .variant = "my_variant",
143*2b949d04SAndroid Build Coastguard Worker         .options = "my_option",
144*2b949d04SAndroid Build Coastguard Worker 
145*2b949d04SAndroid Build Coastguard Worker         .should_fail = true
146*2b949d04SAndroid Build Coastguard Worker     };
147*2b949d04SAndroid Build Coastguard Worker     assert(test_rules(ctx, &test5));
148*2b949d04SAndroid Build Coastguard Worker 
149*2b949d04SAndroid Build Coastguard Worker     struct test_data test6 = {
150*2b949d04SAndroid Build Coastguard Worker         .rules = "index",
151*2b949d04SAndroid Build Coastguard Worker 
152*2b949d04SAndroid Build Coastguard Worker         .model = "", .layout = "br,al,cn,az", .variant = "",
153*2b949d04SAndroid Build Coastguard Worker         .options = "some:opt",
154*2b949d04SAndroid Build Coastguard Worker 
155*2b949d04SAndroid Build Coastguard Worker         .keycodes = "default_keycodes", .types = "default_types",
156*2b949d04SAndroid Build Coastguard Worker         .compat = "default_compat",
157*2b949d04SAndroid Build Coastguard Worker         .symbols = "default_symbols+extra:1+extra:2+extra:3+extra:4",
158*2b949d04SAndroid Build Coastguard Worker     };
159*2b949d04SAndroid Build Coastguard Worker     assert(test_rules(ctx, &test6));
160*2b949d04SAndroid Build Coastguard Worker 
161*2b949d04SAndroid Build Coastguard Worker     struct test_data test7 = {
162*2b949d04SAndroid Build Coastguard Worker         .rules = "multiple-options",
163*2b949d04SAndroid Build Coastguard Worker 
164*2b949d04SAndroid Build Coastguard Worker         .model = "my_model", .layout = "my_layout", .variant = "my_variant",
165*2b949d04SAndroid Build Coastguard Worker         .options = "option3,option1,colon:opt,option11",
166*2b949d04SAndroid Build Coastguard Worker 
167*2b949d04SAndroid Build Coastguard Worker         .keycodes = "my_keycodes", .types = "my_types",
168*2b949d04SAndroid Build Coastguard Worker         .compat = "my_compat+some:compat+group(bla)",
169*2b949d04SAndroid Build Coastguard Worker         .symbols = "my_symbols+extra_variant+compose(foo)+keypad(bar)+altwin(menu)",
170*2b949d04SAndroid Build Coastguard Worker     };
171*2b949d04SAndroid Build Coastguard Worker     assert(test_rules(ctx, &test7));
172*2b949d04SAndroid Build Coastguard Worker 
173*2b949d04SAndroid Build Coastguard Worker     xkb_context_unref(ctx);
174*2b949d04SAndroid Build Coastguard Worker     return 0;
175*2b949d04SAndroid Build Coastguard Worker }
176