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