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