utils: add/replace string equality macros
[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 #include "test.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: NULL\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     kccgst = xkb_components_from_rules(ctx, &rmlvo);
72     if (!kccgst) {
73         fprintf(stderr, "Received: NULL\n");
74         return data->should_fail;
75     }
76
77     fprintf(stderr, "Received : %s\t%s\t%s\t%s\n",
78             kccgst->keycodes, kccgst->types, kccgst->compat, kccgst->symbols);
79
80     passed = streq(kccgst->keycodes, data->keycodes) &&
81              streq(kccgst->types, data->types) &&
82              streq(kccgst->compat, data->compat) &&
83              streq(kccgst->symbols, data->symbols);
84
85     free(kccgst->keycodes);
86     free(kccgst->types);
87     free(kccgst->compat);
88     free(kccgst->symbols);
89     free(kccgst);
90
91     return passed;
92 }
93
94 int
95 main(void)
96 {
97     struct xkb_context *ctx;
98     const char *srcdir = getenv("srcdir");
99     char *path;
100
101     ctx = xkb_context_new(XKB_CONTEXT_NO_DEFAULT_INCLUDES);
102     assert(ctx);
103
104     assert(asprintf(&path, "%s/test/data", srcdir ? srcdir : ".") > 0);
105     assert(xkb_context_include_path_append(ctx, test_get_path("")));
106     free(path);
107
108     struct test_data test1 = {
109         .rules = "simple",
110
111         .model = "my_model", .layout = "my_layout", .variant = "my_variant",
112         .options = "my_option",
113
114         .keycodes = "my_keycodes", .types = "my_types",
115         .compat = "my_compat+some:compat",
116         .symbols = "my_symbols+extra_variant",
117     };
118     assert(test_rules(ctx, &test1));
119
120     struct test_data test2 = {
121         .rules = "simple",
122
123         .model = "", .layout = "", .variant = "", .options = "",
124
125         .keycodes = "default_keycodes", .types = "default_types",
126         .compat = "default_compat", .symbols = "default_symbols",
127     };
128     assert(test_rules(ctx, &test2));
129
130     struct test_data test3 = {
131         .rules = "groups",
132
133         .model = "pc104", .layout = "foo", .variant = "", .options = "",
134
135         .keycodes = "something(pc104)", .types = "default_types",
136         .compat = "default_compat", .symbols = "default_symbols",
137     };
138     assert(test_rules(ctx, &test3));
139
140     struct test_data test4 = {
141         .rules = "groups",
142
143         .model = "foo", .layout = "ar", .variant = "bar", .options = "",
144
145         .keycodes = "default_keycodes", .types = "default_types",
146         .compat = "default_compat", .symbols = "my_symbols+(bar)",
147     };
148     assert(test_rules(ctx, &test4));
149
150     struct test_data test5 = {
151         .rules = "simple",
152
153         .model = NULL, .layout = "my_layout,second_layout", .variant = "my_variant",
154         .options = "my_option",
155
156         .should_fail = true
157     };
158     assert(test_rules(ctx, &test5));
159
160     struct test_data test6 = {
161         .rules = "index",
162
163         .model = "", .layout = "br,al,cn,az", .variant = "",
164         .options = "some:opt",
165
166         .keycodes = "default_keycodes", .types = "default_types",
167         .compat = "default_compat",
168         .symbols = "default_symbols+extra:1+extra:2+extra:3+extra:4",
169     };
170     assert(test_rules(ctx, &test6));
171
172     struct test_data test7 = {
173         .rules = "multiple-options",
174
175         .model = "my_model", .layout = "my_layout", .variant = "my_variant",
176         .options = "option3,option1,colon:opt,option11",
177
178         .keycodes = "my_keycodes", .types = "my_types",
179         .compat = "my_compat+some:compat+group(bla)",
180         .symbols = "my_symbols+extra_variant+compose(foo)+keypad(bar)+altwin(menu)",
181     };
182     assert(test_rules(ctx, &test7));
183
184     xkb_context_unref(ctx);
185     return 0;
186 }