Add a library of common test functions Including creating a context (will come in useful soon), opening and reading files, and compiling keymaps. Signed-off-by: Daniel Stone <daniel@fooishbar.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
diff --git a/Makefile.am b/Makefile.am
index a8ec13d..a2fe5ea 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -108,9 +108,11 @@ $(top_builddir)/makekeys/makekeys$(EXEEXT): $(top_srcdir)/makekeys/makekeys.c
# Some tests need to use unexported symbols, so we link them against
# a private copy of libxkbcommon with all symbols exposed.
-noinst_LTLIBRARIES = libxkbcommon-priv.la
-libxkbcommon_priv_la_LDFLAGS = $(libxkbcommon_la_LDFLAGS)
-libxkbcommon_priv_la_SOURCES = $(libxkbcommon_la_SOURCES)
+noinst_LTLIBRARIES = libtest.la
+libtest_la_LDFLAGS = $(libxkbcommon_la_LDFLAGS)
+libtest_la_SOURCES = \
+ $(libxkbcommon_la_SOURCES) \
+ test/common.c
TESTS_ENVIRONMENT =
@@ -126,7 +128,7 @@ TESTS = \
test/dump \
test/stringcomp \
test/keyseq
-TESTS_LDADD = libxkbcommon-priv.la
+TESTS_LDADD = libtest.la
test_xkey_LDADD = $(TESTS_LDADD)
test_filecomp_LDADD = $(TESTS_LDADD)
@@ -143,7 +145,9 @@ test_keyseq_LDADD = $(TESTS_LDADD)
check_PROGRAMS = $(TESTS)
-EXTRA_DIST = test/data
+EXTRA_DIST = \
+ test/data \
+ test/test.h
# This sed script strips out lines that start with '#define _' which
# removes #define _OSF_Keysyms and such. The XK_Ydiaeresis case is to
diff --git a/test/common.c b/test/common.c
new file mode 100644
index 0000000..3938dcd
--- /dev/null
+++ b/test/common.c
@@ -0,0 +1,165 @@
+/*
+Copyright 2009 Dan Nicholson
+Copyright © 2012 Daniel Stone
+Copyright © 2012 Ran Benita
+
+Permission is hereby granted, free of charge, to any person obtaining a
+copy of this software and associated documentation files (the "Software"),
+to deal in the Software without restriction, including without limitation
+the rights to use, copy, modify, merge, publish, distribute, sublicense,
+and/or sell copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+Except as contained in this notice, the names of the authors or their
+institutions shall not be used in advertising or otherwise to promote the
+sale, use or other dealings in this Software without prior written
+authorization from the authors.
+*/
+
+#include <assert.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <limits.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#include "xkbcommon/xkbcommon.h"
+#include "test.h"
+
+const char *
+test_get_path(const char *path_rel)
+{
+ static char path[PATH_MAX];
+ const char *srcdir = getenv("srcdir");
+
+ snprintf(path, PATH_MAX - 1,
+ "%s/test/data/%s", srcdir ? srcdir : ".", path_rel);
+
+ return path;
+}
+
+char *
+test_read_file(const char *path_rel)
+{
+ struct stat info;
+ char *ret, *tmp;
+ int fd, count, remaining;
+
+ fd = open(test_get_path(path_rel), O_RDONLY);
+ if (fd < 0)
+ return NULL;
+
+ if (fstat(fd, &info) != 0) {
+ close(fd);
+ return NULL;
+ }
+
+ ret = malloc(info.st_size + 1);
+ if (!ret) {
+ close(fd);
+ return NULL;
+ }
+
+ remaining = info.st_size;
+ tmp = ret;
+ while ((count = read(fd, tmp, remaining))) {
+ remaining -= count;
+ tmp += count;
+ }
+ ret[info.st_size] = '\0';
+ close(fd);
+
+ if (remaining != 0) {
+ free(ret);
+ return NULL;
+ }
+
+ return ret;
+}
+
+struct xkb_context *
+test_get_context(void)
+{
+ return xkb_context_new(0);
+}
+
+struct xkb_keymap *
+test_compile_file(struct xkb_context *context, const char *path_rel)
+{
+ struct xkb_keymap *keymap;
+ FILE *file;
+ const char *path = test_get_path(path_rel);
+
+ file = fopen(path, "r");
+ if (!file) {
+ fprintf(stderr, "Failed to open path: %s\n", path);
+ return NULL;
+ }
+ assert(file != NULL);
+
+ keymap = xkb_map_new_from_file(context, file,
+ XKB_KEYMAP_FORMAT_TEXT_V1, 0);
+ fclose(file);
+
+ if (!keymap) {
+ fprintf(stderr, "Failed to compile path: %s\n", path);
+ return NULL;
+ }
+
+ fprintf(stderr, "Successfully compiled path: %s\n", path);
+
+ return keymap;
+}
+
+struct xkb_keymap *
+test_compile_string(struct xkb_context *context, const char *string)
+{
+ struct xkb_keymap *keymap;
+
+ keymap = xkb_map_new_from_string(context, string,
+ XKB_KEYMAP_FORMAT_TEXT_V1, 0);
+ if (!keymap) {
+ fprintf(stderr, "Failed to compile string\n");
+ return NULL;
+ }
+
+ return keymap;
+}
+
+struct xkb_keymap *
+test_compile_rules(struct xkb_context *context, const char *rules,
+ const char *model, const char *layout,
+ const char *variant, const char *options)
+{
+ struct xkb_keymap *keymap;
+ struct xkb_rule_names rmlvo = {
+ .rules = rules,
+ .model = model,
+ .layout = layout,
+ .variant = variant,
+ .options = options
+ };
+
+ keymap = xkb_map_new_from_names(context, &rmlvo, 0);
+ if (!keymap) {
+ fprintf(stderr,
+ "Failed to compile RMLVO: '%s', '%s', '%s', '%s', '%s'\n",
+ rules, model, layout, variant, options);
+ return NULL;
+ }
+
+ return keymap;
+}
diff --git a/test/dump.c b/test/dump.c
index 3fe3b42..8cb60a9 100644
--- a/test/dump.c
+++ b/test/dump.c
@@ -34,53 +34,40 @@ authorization from the authors.
#include <sys/types.h>
#include "xkbcommon/xkbcommon.h"
+#include "test.h"
int main(int argc, char *argv[])
{
- struct xkb_context *ctx = xkb_context_new(0);
+ struct xkb_context *ctx = test_get_context();
struct xkb_keymap *keymap;
- struct xkb_rule_names names = {
- .rules = "evdev",
- .model = "pc105",
- .layout = "us,ru,ca,de",
- .variant = ",,multix,neo",
- .options = NULL,
- };
- struct stat stat_buf;
char *as_string, *expected;
- const char *srcdir = getenv("srcdir");
- char *path;
- int fd;
-
- srcdir = srcdir ? srcdir : ".";
- assert(asprintf(&path, "%s/test/data/keymaps/dump.data", srcdir) != -1);
- fd = open(path, O_RDONLY);
- assert(fd >= 0);
- assert(stat(path, &stat_buf) == 0);
- assert(stat_buf.st_size > 0);
- free(path);
-
- expected = mmap(NULL, stat_buf.st_size, PROT_READ, MAP_SHARED, fd, 0);
- assert(expected != MAP_FAILED);
assert(ctx);
- keymap = xkb_map_new_from_names(ctx, &names, 0);
+
+ keymap = test_compile_rules(ctx, "evdev", "pc105", "us,ru,ca,de",
+ ",,multix,neo", NULL);
assert(keymap);
as_string = xkb_map_get_as_string(keymap);
+ xkb_map_unref(keymap);
assert(as_string);
+ expected = test_read_file("keymaps/dump.data");
+ assert(expected);
+
if (strcmp(as_string, expected) != 0) {
fprintf(stderr, "dumped map differs from expected!\n\n");
fprintf(stderr, "length: got %zu, expected %zu\n",
strlen(as_string), strlen(expected));
fprintf(stderr, "result:\n");
- printf("%s\n", as_string);
+ fprintf(stderr, "%s\n", as_string);
+ fflush(stderr);
assert(0);
}
free(as_string);
- xkb_map_unref(keymap);
+ free(expected);
+
xkb_context_unref(ctx);
return 0;
diff --git a/test/filecomp.c b/test/filecomp.c
index a91d24a..b9b7ccd 100644
--- a/test/filecomp.c
+++ b/test/filecomp.c
@@ -31,82 +31,33 @@ authorization from the authors.
#include <limits.h>
#include "xkbcommon/xkbcommon.h"
+#include "test.h"
static int
-test_file(const char *path)
+test_file(struct xkb_context *ctx, const char *path_rel)
{
- FILE *file;
- struct xkb_context *context;
- struct xkb_keymap *keymap;
+ struct xkb_keymap *keymap = test_compile_file(ctx, path_rel);
- file = fopen(path, "r");
- assert(file != NULL);
-
- context = xkb_context_new(0);
- assert(context);
-
- fprintf(stderr, "\nCompiling path: %s\n", path);
-
- keymap = xkb_map_new_from_file(context, file,
- XKB_KEYMAP_FORMAT_TEXT_V1, 0);
- fclose(file);
-
- if (!keymap) {
- fprintf(stderr, "Failed to compile keymap\n");
- xkb_context_unref(context);
- return 0;
- }
-
- xkb_map_unref(keymap);
- xkb_context_unref(context);
- return 1;
-}
-
-static int
-test_file_name(const char *file_name)
-{
- static char path[PATH_MAX];
- const char *srcdir = getenv("srcdir");
-
- snprintf(path, PATH_MAX - 1,
- "%s/test/data/keymaps/%s", srcdir ? srcdir : ".", file_name);
- return test_file(path);
-}
-
-static int
-test_string(const char *string)
-{
- struct xkb_context *context;
- struct xkb_keymap *keymap;
-
- context = xkb_context_new(0);
- assert(context);
-
- fprintf(stderr, "\nCompiling string\n");
-
- keymap = xkb_map_new_from_string(context, string, XKB_KEYMAP_FORMAT_TEXT_V1, 0);
- if (!keymap) {
- xkb_context_unref(context);
+ if (!keymap)
return 0;
- }
xkb_map_unref(keymap);
- xkb_context_unref(context);
return 1;
}
int
main(void)
{
+ struct xkb_context *ctx = test_get_context();
- assert(test_file_name("keymaps/basic.xkb"));
+ assert(test_file(ctx, "keymaps/basic.xkb"));
/* XXX check we actually get qwertz here ... */
- assert(test_file_name("keymaps/default.xkb"));
- assert(test_file_name("keymaps/comprehensive-plus-geom.xkb"));
+ assert(test_file(ctx, "keymaps/default.xkb"));
+ assert(test_file(ctx, "keymaps/comprehensive-plus-geom.xkb"));
- assert(!test_file_name("bad.xkb"));
+ assert(!test_file(ctx, "bad.xkb"));
- assert(!test_string(""));
+ xkb_context_unref(ctx);
return 0;
}
diff --git a/test/keyseq.c b/test/keyseq.c
index 4bf9d4f..eab6e86 100644
--- a/test/keyseq.c
+++ b/test/keyseq.c
@@ -29,6 +29,7 @@
#include <X11/keysym.h>
#include "xkbcommon/xkbcommon.h"
+#include "test.h"
enum {
DOWN,
@@ -133,19 +134,12 @@ fail:
int
main(void)
{
- struct xkb_context *ctx;
+ struct xkb_context *ctx = test_get_context();
struct xkb_keymap *keymap;
- const struct xkb_rule_names names = {
- .rules = "evdev",
- .model = "evdev",
- .layout = "us,il",
- .variant = "",
- .options = "grp:alt_shift_toggle,grp:menu_toggle",
- };
-
- ctx = xkb_context_new(0);
+
assert(ctx);
- keymap = xkb_map_new_from_names(ctx, &names, 0);
+ keymap = test_compile_rules(ctx, "evdev", "evdev", "us,il", NULL,
+ "grp:alt_shift_toggle,grp:menu_toggle");
assert(keymap);
assert(test_key_seq(keymap,
diff --git a/test/namescomp.c b/test/namescomp.c
index 0668e40..66d86cb 100644
--- a/test/namescomp.c
+++ b/test/namescomp.c
@@ -31,13 +31,13 @@ authorization from the authors.
#include "xkb-priv.h"
#include "xkbcommon/xkbcommon.h"
+#include "test.h"
static int
-test_names(const char *keycodes, const char *types,
- const char *compat, const char *symbols)
+test_names(struct xkb_context *context, const char *keycodes,
+ const char *types, const char *compat, const char *symbols)
{
int ret = 1;
- struct xkb_context *context;
struct xkb_keymap *keymap;
struct xkb_component_names kccgst = {
.keycodes = strdup(keycodes),
@@ -46,9 +46,6 @@ test_names(const char *keycodes, const char *types,
.symbols = strdup(symbols),
};
- context = xkb_context_new(0);
- assert(context);
-
fprintf(stderr, "\nCompiling %s %s %s %s\n", kccgst.keycodes, kccgst.types,
kccgst.compat, kccgst.symbols);
@@ -60,7 +57,6 @@ test_names(const char *keycodes, const char *types,
xkb_map_unref(keymap);
err_context:
- xkb_context_unref(context);
free(kccgst.keycodes);
free(kccgst.types);
free(kccgst.compat);
@@ -71,18 +67,24 @@ err_context:
int
main(void)
{
- assert(test_names("xfree86+aliases(qwertz)", "complete", "complete", "pc+de"));
- assert(test_names("xfree86+aliases(qwerty)", "complete", "complete", "pc+us"));
- assert(test_names("xfree86+aliases(qwertz)", "complete", "complete",
+ struct xkb_context *ctx = test_get_context();
+
+ assert(ctx);
+
+ assert(test_names(ctx, "evdev+aliases(qwertz)", "complete", "complete", "pc+de"));
+ assert(test_names(ctx, "evdev+aliases(qwerty)", "complete", "complete", "pc+us"));
+ assert(test_names(ctx, "evdev+aliases(qwertz)", "complete", "complete",
"pc+de+level3(ralt_switch_for_alts_toggle)+group(alts_toggle)"));
- assert(!test_names("", "", "", ""));
- assert(!test_names("xfree86+aliases(qwerty)", "", "", ""));
- assert(!test_names("xfree86+aliases(qwertz)", "", "", "pc+de"));
- assert(!test_names("xfree86+aliases(qwertz)", "complete", "", "pc+de"));
- assert(!test_names("xfree86+aliases(qwertz)", "", "complete", "pc+de"));
- assert(!test_names("xfree86+aliases(qwertz)", "complete", "complete", ""));
- assert(!test_names("badnames", "complete", "pc+us", "pc(pc101)"));
+ assert(!test_names(ctx, "", "", "", ""));
+ assert(!test_names(ctx, "evdev+aliases(qwerty)", "", "", ""));
+ assert(!test_names(ctx, "evdev+aliases(qwertz)", "", "", "pc+de"));
+ assert(!test_names(ctx, "evdev+aliases(qwertz)", "complete", "", "pc+de"));
+ assert(!test_names(ctx, "evdev+aliases(qwertz)", "", "complete", "pc+de"));
+ assert(!test_names(ctx, "evdev+aliases(qwertz)", "complete", "complete", ""));
+ assert(!test_names(ctx, "badnames", "complete", "pc+us", "pc(pc101)"));
+
+ xkb_context_unref(ctx);
return 0;
}
diff --git a/test/rules-file.c b/test/rules-file.c
index 6d4c508..c52ff23 100644
--- a/test/rules-file.c
+++ b/test/rules-file.c
@@ -28,6 +28,7 @@
#include "xkbcommon/xkbcommon.h"
#include "rules.h"
+#include "test.h"
struct test_data {
/* Rules file */
@@ -109,7 +110,7 @@ main(void)
assert(ctx);
assert(asprintf(&path, "%s/test/data", srcdir ? srcdir : ".") > 0);
- assert(xkb_context_include_path_append(ctx, path));
+ assert(xkb_context_include_path_append(ctx, test_get_path("")));
free(path);
struct test_data test1 = {
diff --git a/test/rulescomp.c b/test/rulescomp.c
index 26873a6..c726ff2 100644
--- a/test/rulescomp.c
+++ b/test/rulescomp.c
@@ -30,64 +30,52 @@ authorization from the authors.
#include <time.h>
#include "xkbcommon/xkbcommon.h"
+#include "test.h"
#define BENCHMARK_ITERATIONS 1000
static int
-do_test_rmlvo(const char *rules, const char *model, const char *layout,
- const char *variant, const char *options, int silent)
+test_rmlvo(struct xkb_context *context, const char *rules,
+ const char *model, const char *layout,
+ const char *variant, const char *options)
{
- struct xkb_context *context;
struct xkb_keymap *keymap;
- struct xkb_rule_names rmlvo = {
- .rules = rules,
- .model = model,
- .layout = layout,
- .variant = variant,
- .options = options
- };
-
- context = xkb_context_new(0);
- assert(context);
-
- if (!silent)
- fprintf(stderr, "Compiling %s %s %s %s %s\n", rules, model, layout,
- variant, options);
-
- keymap = xkb_map_new_from_names(context, &rmlvo, 0);
- if (!keymap) {
- xkb_context_unref(context);
- return 0;
- }
- xkb_map_unref(keymap);
- xkb_context_unref(context);
- return 1;
-}
+ keymap = test_compile_rules(context, rules, model, layout, variant,
+ options);
+ if (keymap)
+ xkb_map_unref(keymap);
-static int
-test_rmlvo(const char *rules, const char *model, const char *layout,
- const char *variant, const char *options)
-{
- return do_test_rmlvo(rules, model, layout, variant, options, 0);
+ return keymap != NULL;
}
static int
-test_rmlvo_silent(const char *rules, const char *model, const char *layout,
+test_rmlvo_silent(struct xkb_context *context, const char *rules,
+ const char *model, const char *layout,
const char *variant, const char *options)
{
- return do_test_rmlvo(rules, model, layout, variant, options, 1);
+ struct xkb_keymap *keymap;
+
+ keymap = test_compile_rules(context, rules, model, layout, variant,
+ options);
+ if (keymap) {
+ fprintf(stderr, "Compiled '%s' '%s' '%s' '%s' '%s'\n",
+ rules, model, layout, variant, options);
+ xkb_map_unref(keymap);
+ }
+
+ return keymap != NULL;
}
static void
-benchmark(void)
+benchmark(struct xkb_context *context)
{
struct timespec start, stop, elapsed;
int i;
clock_gettime(CLOCK_MONOTONIC, &start);
for (i = 0; i < BENCHMARK_ITERATIONS; i++)
- assert(test_rmlvo_silent("base", "", "us", "", ""));
+ assert(test_rmlvo_silent(context, "base", "", "us", "", ""));
clock_gettime(CLOCK_MONOTONIC, &stop);
elapsed.tv_sec = stop.tv_sec - start.tv_sec;
@@ -103,16 +91,22 @@ benchmark(void)
int main(int argc, char *argv[])
{
- assert(test_rmlvo("base", "pc105", "us,il,ru,ca", ",,,multix", "grp:alts_toggle,ctrl:nocaps,compose:rwin"));
- assert(test_rmlvo("base", "", "us", "", ""));
- assert(test_rmlvo("evdev", "pc105", "us", "intl", ""));
- assert(test_rmlvo("evdev", "pc105", "us", "intl", "grp:alts_toggle"));
+ struct xkb_context *ctx = test_get_context();
- assert(!test_rmlvo("", "", "", "", ""));
- assert(!test_rmlvo("base", "", "", "", ""));
- assert(!test_rmlvo("base", "pc105", "", "", ""));
- assert(!test_rmlvo("badrules", "", "us", "", ""));
+ assert(ctx);
+
+ assert(test_rmlvo(ctx, "base", "pc105", "us,il,ru,ca", ",,,multix", "grp:alts_toggle,ctrl:nocaps,compose:rwin"));
+ assert(test_rmlvo(ctx, "base", "", "us", "", ""));
+ assert(test_rmlvo(ctx, "evdev", "pc105", "us", "intl", ""));
+ assert(test_rmlvo(ctx, "evdev", "pc105", "us", "intl", "grp:alts_toggle"));
+
+ assert(!test_rmlvo(ctx, "", "", "", "", ""));
+ assert(!test_rmlvo(ctx, "base", "", "", "", ""));
+ assert(!test_rmlvo(ctx, "base", "pc105", "", "", ""));
+ assert(!test_rmlvo(ctx, "badrules", "", "us", "", ""));
if (argc > 1 && strcmp(argv[1], "bench") == 0)
- benchmark();
+ benchmark(ctx);
+
+ xkb_context_unref(ctx);
}
diff --git a/test/state.c b/test/state.c
index b0974ef..fe57844 100644
--- a/test/state.c
+++ b/test/state.c
@@ -31,6 +31,7 @@
#include "xkbcommon/xkbcommon.h"
#include "xkbcommon/xkbcommon-names.h"
#include "xkb-priv.h"
+#include "test.h"
/* Offset between evdev keycodes (where KEY_ESCAPE is 1), and the evdev XKB
* keycode set (where ESC is 9). */
@@ -240,20 +241,12 @@ test_repeat(struct xkb_keymap *keymap)
int
main(void)
{
- struct xkb_context *context;
+ struct xkb_context *context = test_get_context();
struct xkb_keymap *keymap;
- struct xkb_rule_names rmlvo = {
- .rules = "evdev",
- .model = "pc104",
- .layout = "us",
- .variant = NULL,
- .options = NULL,
- };
-
- context = xkb_context_new(0);
+
assert(context);
- keymap = xkb_map_new_from_names(context, &rmlvo, 0);
+ keymap = test_compile_rules(context, "evdev", "pc104", "us", NULL, NULL);
assert(keymap);
test_update_key(keymap);
diff --git a/test/stringcomp.c b/test/stringcomp.c
index 8db7836..c965162 100644
--- a/test/stringcomp.c
+++ b/test/stringcomp.c
@@ -1,5 +1,4 @@
-/*
-Copyright 2009 Dan Nicholson
+/* Copyright 2009 Dan Nicholson
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
@@ -34,48 +33,28 @@ authorization from the authors.
#include <sys/types.h>
#include "xkbcommon/xkbcommon.h"
+#include "test.h"
-int main(int argc, char *argv[])
+int
+main(int argc, char *argv[])
{
- struct xkb_context *ctx = xkb_context_new(0);
+ struct xkb_context *ctx = test_get_context();
struct xkb_keymap *keymap;
- struct stat info;
- char *as_string, *buf;
- char *path;
- const char *srcdir;
- int fd, count, remaining;
+ char *as_string;
assert(ctx);
- srcdir = getenv("srcdir");
- assert(srcdir);
- assert(asprintf(&path, "%s/test/data/keymaps/stringcomp.data", srcdir) > 0);
- assert(path);
-
- fd = open(path, O_RDONLY);
- assert(fd >= 0);
- assert(fstat(fd, &info) == 0);
- as_string = malloc(info.st_size + 1);
+ as_string = test_read_file("keymaps/stringcomp.data");
assert(as_string);
- remaining = info.st_size;
- buf = as_string;
- while ((count = read(fd, buf, remaining))) {
- remaining -= count;
- buf += count;
- }
-
- assert(remaining == 0);
- as_string[info.st_size] = '\0';
-
- keymap = xkb_map_new_from_string(ctx, as_string,
- XKB_KEYMAP_FORMAT_TEXT_V1, 0);
+ keymap = test_compile_string(ctx, as_string);
assert(keymap);
-
- free(path);
- close(fd);
free(as_string);
xkb_map_unref(keymap);
+
+ keymap = test_compile_string(ctx, "");
+ assert(!keymap);
+
xkb_context_unref(ctx);
return 0;
diff --git a/test/test.h b/test/test.h
new file mode 100644
index 0000000..bbc41e3
--- /dev/null
+++ b/test/test.h
@@ -0,0 +1,46 @@
+/*
+ * Copyright © 2012 Daniel Stone
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * Author: Daniel Stone <daniel@fooishbar.org>
+ */
+
+#include "xkbcommon/xkbcommon.h"
+
+const char *
+test_get_path(const char *path_rel);
+
+char *
+test_read_file(const char *path_rel);
+
+struct xkb_context *
+test_get_context(void);
+
+struct xkb_keymap *
+test_compile_file(struct xkb_context *context, const char *path_rel);
+
+struct xkb_keymap *
+test_compile_string(struct xkb_context *context, const char *string);
+
+struct xkb_keymap *
+test_compile_rules(struct xkb_context *context, const char *rules,
+ const char *model, const char *layout,
+ const char *variant, const char *options);