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>
master
Daniel Stone 2012-07-12 14:15:08 +01:00
parent 059c1842ef
commit 3e86ebca06
11 changed files with 324 additions and 208 deletions

View File

@ -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

165
test/common.c Normal file
View File

@ -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;
}

View File

@ -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;

View File

@ -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);
if (!keymap)
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);
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;
}

View File

@ -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,

View File

@ -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;
}

View File

@ -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 = {

View File

@ -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)
{
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;
}
static int
test_rmlvo(const char *rules, const char *model, const char *layout,
test_rmlvo(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, 0);
struct xkb_keymap *keymap;
keymap = test_compile_rules(context, rules, model, layout, variant,
options);
if (keymap)
xkb_map_unref(keymap);
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);
}

View File

@ -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);

View File

@ -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;

46
test/test.h Normal file
View File

@ -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);