2 * Copyright © 2012 Ran Benita <ran234@gmail.com>
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:
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
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.
27 #include "xkbcomp-priv.h"
30 #define BENCHMARK_ITERATIONS 20000
48 /* Or set this if xkb_components_from_rules() should fail. */
53 test_rules(struct xkb_context *ctx, struct test_data *data)
56 const struct xkb_rule_names rmlvo = {
57 data->rules, data->model, data->layout, data->variant, data->options
59 struct xkb_component_names kccgst;
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);
64 if (data->should_fail)
65 fprintf(stderr, "Expecting: FAILURE\n");
67 fprintf(stderr, "Expecting: %s\t%s\t%s\t%s\n",
68 data->keycodes, data->types, data->compat, data->symbols);
70 if (!xkb_components_from_rules(ctx, &rmlvo, &kccgst)) {
71 fprintf(stderr, "Received : FAILURE\n");
72 return data->should_fail;
75 fprintf(stderr, "Received : %s\t%s\t%s\t%s\n",
76 kccgst.keycodes, kccgst.types, kccgst.compat, kccgst.symbols);
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);
83 free(kccgst.keycodes);
92 benchmark(struct xkb_context *ctx)
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);
98 struct xkb_rule_names rmlvo = {
99 "evdev", "pc105", "us,il", ",", "ctrl:nocaps,grp:menu_toggle",
101 struct xkb_component_names kccgst;
103 xkb_context_set_log_level(ctx, XKB_LOG_LEVEL_CRITICAL);
104 xkb_context_set_log_verbosity(ctx, 0);
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);
112 free(kccgst.symbols);
114 clock_gettime(CLOCK_MONOTONIC, &stop);
116 xkb_context_set_log_level(ctx, old_level);
117 xkb_context_set_log_verbosity(ctx, old_verb);
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;
126 fprintf(stderr, "processed %d times in %ld.%09lds\n",
127 BENCHMARK_ITERATIONS, elapsed.tv_sec, elapsed.tv_nsec);
131 main(int argc, char *argv[])
133 struct xkb_context *ctx;
135 ctx = test_get_context();
138 if (argc > 1 && streq(argv[1], "bench")) {
143 struct test_data test1 = {
146 .model = "my_model", .layout = "my_layout", .variant = "my_variant",
147 .options = "my_option",
149 .keycodes = "my_keycodes", .types = "my_types",
150 .compat = "my_compat|some:compat",
151 .symbols = "my_symbols+extra_variant",
153 assert(test_rules(ctx, &test1));
155 struct test_data test2 = {
158 .model = "", .layout = "", .variant = "", .options = "",
160 .keycodes = "default_keycodes", .types = "default_types",
161 .compat = "default_compat", .symbols = "default_symbols",
163 assert(test_rules(ctx, &test2));
165 struct test_data test3 = {
168 .model = "pc104", .layout = "foo", .variant = "", .options = "",
170 .keycodes = "something(pc104)", .types = "default_types",
171 .compat = "default_compat", .symbols = "default_symbols",
173 assert(test_rules(ctx, &test3));
175 struct test_data test4 = {
178 .model = "foo", .layout = "ar", .variant = "bar", .options = "",
180 .keycodes = "default_keycodes", .types = "default_types",
181 .compat = "default_compat", .symbols = "my_symbols+(bar)",
183 assert(test_rules(ctx, &test4));
185 struct test_data test5 = {
188 .model = NULL, .layout = "my_layout,second_layout", .variant = "my_variant",
189 .options = "my_option",
193 assert(test_rules(ctx, &test5));
195 struct test_data test6 = {
198 .model = "", .layout = "br,al,cn,az", .variant = "",
199 .options = "some:opt",
201 .keycodes = "default_keycodes", .types = "default_types",
202 .compat = "default_compat",
203 .symbols = "default_symbols+extra:1+extra:2+extra:3+extra:4",
205 assert(test_rules(ctx, &test6));
207 struct test_data test7 = {
208 .rules = "multiple-options",
210 .model = "my_model", .layout = "my_layout", .variant = "my_variant",
211 .options = "option3,option1,colon:opt,option11",
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)",
217 assert(test_rules(ctx, &test7));
219 xkb_context_unref(ctx);