Organize xkbcomp/ header files
[platform/upstream/libxkbcommon.git] / test / rules-file.c
1 /*
2  * Copyright © 2012 Ran Benita <ran234@gmail.com>
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 <assert.h>
25 #include <stdbool.h>
26 #include <stdio.h>
27 #include <string.h>
28
29 #include "test.h"
30 #include "xkb-priv.h"
31 #include "rules.h"
32
33 struct test_data {
34     /* Rules file */
35     const char *rules;
36
37     /* Input */
38     const char *model;
39     const char *layout;
40     const char *variant;
41     const char *options;
42
43     /* Expected output */
44     const char *keycodes;
45     const char *types;
46     const char *compat;
47     const char *symbols;
48
49     /* Or set this if xkb_components_from_rules() should fail. */
50     bool should_fail;
51 };
52
53 static bool
54 test_rules(struct xkb_context *ctx, struct test_data *data)
55 {
56     bool passed;
57     const struct xkb_rule_names rmlvo = {
58         data->rules, data->model, data->layout, data->variant, data->options
59     };
60     struct xkb_component_names kccgst;
61
62     fprintf(stderr, "\n\nChecking : %s\t%s\t%s\t%s\t%s\n", data->rules,
63             data->model, data->layout, data->variant, data->options);
64
65     if (data->should_fail)
66         fprintf(stderr, "Expecting: FAILURE\n");
67     else
68         fprintf(stderr, "Expecting: %s\t%s\t%s\t%s\n",
69                 data->keycodes, data->types, data->compat, data->symbols);
70
71     if (!xkb_components_from_rules(ctx, &rmlvo, &kccgst)) {
72         fprintf(stderr, "Received : FAILURE\n");
73         return data->should_fail;
74     }
75
76     fprintf(stderr, "Received : %s\t%s\t%s\t%s\n",
77             kccgst.keycodes, kccgst.types, kccgst.compat, kccgst.symbols);
78
79     passed = streq(kccgst.keycodes, data->keycodes) &&
80              streq(kccgst.types, data->types) &&
81              streq(kccgst.compat, data->compat) &&
82              streq(kccgst.symbols, data->symbols);
83
84     free(kccgst.keycodes);
85     free(kccgst.types);
86     free(kccgst.compat);
87     free(kccgst.symbols);
88
89     return passed;
90 }
91
92 int
93 main(void)
94 {
95     struct xkb_context *ctx;
96     const char *srcdir = getenv("srcdir");
97     char *path;
98
99     ctx = xkb_context_new(XKB_CONTEXT_NO_DEFAULT_INCLUDES);
100     assert(ctx);
101
102     assert(asprintf(&path, "%s/test/data", srcdir ? srcdir : ".") > 0);
103     assert(xkb_context_include_path_append(ctx, test_get_path("")));
104     free(path);
105
106     struct test_data test1 = {
107         .rules = "simple",
108
109         .model = "my_model", .layout = "my_layout", .variant = "my_variant",
110         .options = "my_option",
111
112         .keycodes = "my_keycodes", .types = "my_types",
113         .compat = "my_compat+some:compat",
114         .symbols = "my_symbols+extra_variant",
115     };
116     assert(test_rules(ctx, &test1));
117
118     struct test_data test2 = {
119         .rules = "simple",
120
121         .model = "", .layout = "", .variant = "", .options = "",
122
123         .keycodes = "default_keycodes", .types = "default_types",
124         .compat = "default_compat", .symbols = "default_symbols",
125     };
126     assert(test_rules(ctx, &test2));
127
128     struct test_data test3 = {
129         .rules = "groups",
130
131         .model = "pc104", .layout = "foo", .variant = "", .options = "",
132
133         .keycodes = "something(pc104)", .types = "default_types",
134         .compat = "default_compat", .symbols = "default_symbols",
135     };
136     assert(test_rules(ctx, &test3));
137
138     struct test_data test4 = {
139         .rules = "groups",
140
141         .model = "foo", .layout = "ar", .variant = "bar", .options = "",
142
143         .keycodes = "default_keycodes", .types = "default_types",
144         .compat = "default_compat", .symbols = "my_symbols+(bar)",
145     };
146     assert(test_rules(ctx, &test4));
147
148     struct test_data test5 = {
149         .rules = "simple",
150
151         .model = NULL, .layout = "my_layout,second_layout", .variant = "my_variant",
152         .options = "my_option",
153
154         .should_fail = true
155     };
156     assert(test_rules(ctx, &test5));
157
158     struct test_data test6 = {
159         .rules = "index",
160
161         .model = "", .layout = "br,al,cn,az", .variant = "",
162         .options = "some:opt",
163
164         .keycodes = "default_keycodes", .types = "default_types",
165         .compat = "default_compat",
166         .symbols = "default_symbols+extra:1+extra:2+extra:3+extra:4",
167     };
168     assert(test_rules(ctx, &test6));
169
170     struct test_data test7 = {
171         .rules = "multiple-options",
172
173         .model = "my_model", .layout = "my_layout", .variant = "my_variant",
174         .options = "option3,option1,colon:opt,option11",
175
176         .keycodes = "my_keycodes", .types = "my_types",
177         .compat = "my_compat+some:compat+group(bla)",
178         .symbols = "my_symbols+extra_variant+compose(foo)+keypad(bar)+altwin(menu)",
179     };
180     assert(test_rules(ctx, &test7));
181
182     xkb_context_unref(ctx);
183     return 0;
184 }