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