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.
34 #define BENCHMARK_ITERATIONS 20000
52 /* Or set this if xkb_components_from_rules() should fail. */
57 test_rules(struct xkb_context *ctx, struct test_data *data)
60 const struct xkb_rule_names rmlvo = {
61 data->rules, data->model, data->layout, data->variant, data->options
63 struct xkb_component_names kccgst;
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);
68 if (data->should_fail)
69 fprintf(stderr, "Expecting: FAILURE\n");
71 fprintf(stderr, "Expecting: %s\t%s\t%s\t%s\n",
72 data->keycodes, data->types, data->compat, data->symbols);
74 if (!xkb_components_from_rules(ctx, &rmlvo, &kccgst)) {
75 fprintf(stderr, "Received : FAILURE\n");
76 return data->should_fail;
79 fprintf(stderr, "Received : %s\t%s\t%s\t%s\n",
80 kccgst.keycodes, kccgst.types, kccgst.compat, kccgst.symbols);
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);
87 free(kccgst.keycodes);
96 benchmark(struct xkb_context *ctx)
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);
102 struct xkb_rule_names rmlvo = {
103 "evdev", "pc105", "us,il", ",", "ctrl:nocaps,grp:menu_toggle",
105 struct xkb_component_names kccgst;
107 xkb_set_log_level(ctx, XKB_LOG_LEVEL_CRITICAL);
108 xkb_set_log_verbosity(ctx, 0);
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);
116 free(kccgst.symbols);
118 clock_gettime(CLOCK_MONOTONIC, &stop);
120 xkb_set_log_level(ctx, old_level);
121 xkb_set_log_verbosity(ctx, old_verb);
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;
130 fprintf(stderr, "processed %d times in %ld.%09lds\n",
131 BENCHMARK_ITERATIONS, elapsed.tv_sec, elapsed.tv_nsec);
135 main(int argc, char *argv[])
137 struct xkb_context *ctx;
139 ctx = test_get_context();
142 if (argc > 1 && streq(argv[1], "bench")) {
147 struct test_data test1 = {
150 .model = "my_model", .layout = "my_layout", .variant = "my_variant",
151 .options = "my_option",
153 .keycodes = "my_keycodes", .types = "my_types",
154 .compat = "my_compat|some:compat",
155 .symbols = "my_symbols+extra_variant",
157 assert(test_rules(ctx, &test1));
159 struct test_data test2 = {
162 .model = "", .layout = "", .variant = "", .options = "",
164 .keycodes = "default_keycodes", .types = "default_types",
165 .compat = "default_compat", .symbols = "default_symbols",
167 assert(test_rules(ctx, &test2));
169 struct test_data test3 = {
172 .model = "pc104", .layout = "foo", .variant = "", .options = "",
174 .keycodes = "something(pc104)", .types = "default_types",
175 .compat = "default_compat", .symbols = "default_symbols",
177 assert(test_rules(ctx, &test3));
179 struct test_data test4 = {
182 .model = "foo", .layout = "ar", .variant = "bar", .options = "",
184 .keycodes = "default_keycodes", .types = "default_types",
185 .compat = "default_compat", .symbols = "my_symbols+(bar)",
187 assert(test_rules(ctx, &test4));
189 struct test_data test5 = {
192 .model = NULL, .layout = "my_layout,second_layout", .variant = "my_variant",
193 .options = "my_option",
197 assert(test_rules(ctx, &test5));
199 struct test_data test6 = {
202 .model = "", .layout = "br,al,cn,az", .variant = "",
203 .options = "some:opt",
205 .keycodes = "default_keycodes", .types = "default_types",
206 .compat = "default_compat",
207 .symbols = "default_symbols+extra:1+extra:2+extra:3+extra:4",
209 assert(test_rules(ctx, &test6));
211 struct test_data test7 = {
212 .rules = "multiple-options",
214 .model = "my_model", .layout = "my_layout", .variant = "my_variant",
215 .options = "option3,option1,colon:opt,option11",
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)",
221 assert(test_rules(ctx, &test7));
223 xkb_context_unref(ctx);