API: add _context prefix to log-related functions
[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 <time.h>
25
26 #include "test.h"
27 #include "xkbcomp-priv.h"
28 #include "rules.h"
29
30 #define BENCHMARK_ITERATIONS 20000
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 *keycodes;
44     const char *types;
45     const char *compat;
46     const char *symbols;
47
48     /* Or set this if xkb_components_from_rules() should fail. */
49     bool should_fail;
50 };
51
52 static bool
53 test_rules(struct xkb_context *ctx, struct test_data *data)
54 {
55     bool passed;
56     const struct xkb_rule_names rmlvo = {
57         data->rules, data->model, data->layout, data->variant, data->options
58     };
59     struct xkb_component_names kccgst;
60
61     fprintf(stderr, "\n\nChecking : %s\t%s\t%s\t%s\t%s\n", data->rules,
62             data->model, data->layout, data->variant, data->options);
63
64     if (data->should_fail)
65         fprintf(stderr, "Expecting: FAILURE\n");
66     else
67         fprintf(stderr, "Expecting: %s\t%s\t%s\t%s\n",
68                 data->keycodes, data->types, data->compat, data->symbols);
69
70     if (!xkb_components_from_rules(ctx, &rmlvo, &kccgst)) {
71         fprintf(stderr, "Received : FAILURE\n");
72         return data->should_fail;
73     }
74
75     fprintf(stderr, "Received : %s\t%s\t%s\t%s\n",
76             kccgst.keycodes, kccgst.types, kccgst.compat, kccgst.symbols);
77
78     passed = streq(kccgst.keycodes, data->keycodes) &&
79              streq(kccgst.types, data->types) &&
80              streq(kccgst.compat, data->compat) &&
81              streq(kccgst.symbols, data->symbols);
82
83     free(kccgst.keycodes);
84     free(kccgst.types);
85     free(kccgst.compat);
86     free(kccgst.symbols);
87
88     return passed;
89 }
90
91 static void
92 benchmark(struct xkb_context *ctx)
93 {
94     struct timespec start, stop, elapsed;
95     enum xkb_log_level old_level = xkb_context_get_log_level(ctx);
96     int old_verb = xkb_context_get_log_verbosity(ctx);
97     int i;
98     struct xkb_rule_names rmlvo = {
99         "evdev", "pc105", "us,il", ",", "ctrl:nocaps,grp:menu_toggle",
100     };
101     struct xkb_component_names kccgst;
102
103     xkb_context_set_log_level(ctx, XKB_LOG_LEVEL_CRITICAL);
104     xkb_context_set_log_verbosity(ctx, 0);
105
106     clock_gettime(CLOCK_MONOTONIC, &start);
107     for (i = 0; i < BENCHMARK_ITERATIONS; i++) {
108         assert(xkb_components_from_rules(ctx, &rmlvo, &kccgst));
109         free(kccgst.keycodes);
110         free(kccgst.types);
111         free(kccgst.compat);
112         free(kccgst.symbols);
113     }
114     clock_gettime(CLOCK_MONOTONIC, &stop);
115
116     xkb_context_set_log_level(ctx, old_level);
117     xkb_context_set_log_verbosity(ctx, old_verb);
118
119     elapsed.tv_sec = stop.tv_sec - start.tv_sec;
120     elapsed.tv_nsec = stop.tv_nsec - start.tv_nsec;
121     if (elapsed.tv_nsec < 0) {
122         elapsed.tv_nsec += 1000000000;
123         elapsed.tv_sec--;
124     }
125
126     fprintf(stderr, "processed %d times in %ld.%09lds\n",
127             BENCHMARK_ITERATIONS, elapsed.tv_sec, elapsed.tv_nsec);
128 }
129
130 int
131 main(int argc, char *argv[])
132 {
133     struct xkb_context *ctx;
134
135     ctx = test_get_context();
136     assert(ctx);
137
138     if (argc > 1 && streq(argv[1], "bench")) {
139         benchmark(ctx);
140         return 0;
141     }
142
143     struct test_data test1 = {
144         .rules = "simple",
145
146         .model = "my_model", .layout = "my_layout", .variant = "my_variant",
147         .options = "my_option",
148
149         .keycodes = "my_keycodes", .types = "my_types",
150         .compat = "my_compat|some:compat",
151         .symbols = "my_symbols+extra_variant",
152     };
153     assert(test_rules(ctx, &test1));
154
155     struct test_data test2 = {
156         .rules = "simple",
157
158         .model = "", .layout = "", .variant = "", .options = "",
159
160         .keycodes = "default_keycodes", .types = "default_types",
161         .compat = "default_compat", .symbols = "default_symbols",
162     };
163     assert(test_rules(ctx, &test2));
164
165     struct test_data test3 = {
166         .rules = "groups",
167
168         .model = "pc104", .layout = "foo", .variant = "", .options = "",
169
170         .keycodes = "something(pc104)", .types = "default_types",
171         .compat = "default_compat", .symbols = "default_symbols",
172     };
173     assert(test_rules(ctx, &test3));
174
175     struct test_data test4 = {
176         .rules = "groups",
177
178         .model = "foo", .layout = "ar", .variant = "bar", .options = "",
179
180         .keycodes = "default_keycodes", .types = "default_types",
181         .compat = "default_compat", .symbols = "my_symbols+(bar)",
182     };
183     assert(test_rules(ctx, &test4));
184
185     struct test_data test5 = {
186         .rules = "simple",
187
188         .model = NULL, .layout = "my_layout,second_layout", .variant = "my_variant",
189         .options = "my_option",
190
191         .should_fail = true
192     };
193     assert(test_rules(ctx, &test5));
194
195     struct test_data test6 = {
196         .rules = "index",
197
198         .model = "", .layout = "br,al,cn,az", .variant = "",
199         .options = "some:opt",
200
201         .keycodes = "default_keycodes", .types = "default_types",
202         .compat = "default_compat",
203         .symbols = "default_symbols+extra:1+extra:2+extra:3+extra:4",
204     };
205     assert(test_rules(ctx, &test6));
206
207     struct test_data test7 = {
208         .rules = "multiple-options",
209
210         .model = "my_model", .layout = "my_layout", .variant = "my_variant",
211         .options = "option3,option1,colon:opt,option11",
212
213         .keycodes = "my_keycodes", .types = "my_types",
214         .compat = "my_compat+some:compat+group(bla)",
215         .symbols = "my_symbols+extra_variant+compose(foo)+keypad(bar)+altwin(menu)",
216     };
217     assert(test_rules(ctx, &test7));
218
219     xkb_context_unref(ctx);
220     return 0;
221 }