[PATCH libevdev 4/5] Add name-resolver unit tests
David Herrmann
dh.herrmann at gmail.com
Fri Oct 4 11:37:34 PDT 2013
A bunch of tests for the new name resolver.
Signed-off-by: David Herrmann <dh.herrmann at gmail.com>
---
test/Makefile.am | 1 +
test/test-event-codes.c | 314 ++++++++++++++++++++++++++++++++++++++++++++++++
test/test-main.c | 2 +
3 files changed, 317 insertions(+)
create mode 100644 test/test-event-codes.c
diff --git a/test/Makefile.am b/test/Makefile.am
index 258af9e..986fcc9 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -23,6 +23,7 @@ AM_CPPFLAGS = -I$(top_srcdir) -I$(top_builddir)/libevdev $(CHECK_CFLAGS) $(GCOV_
test_libevdev_SOURCES = \
test-main.c \
test-event-names.c \
+ test-event-codes.c \
test-libevdev-init.c \
test-libevdev-has-event.c \
test-int-queue.c \
diff --git a/test/test-event-codes.c b/test/test-event-codes.c
new file mode 100644
index 0000000..2e20165
--- /dev/null
+++ b/test/test-event-codes.c
@@ -0,0 +1,314 @@
+/*
+ * Copyright © 2013 David Herrmann <dh.herrmann at gmail.com>
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and its
+ * documentation for any purpose is hereby granted without fee, provided that
+ * the above copyright notice appear in all copies and that both that copyright
+ * notice and this permission notice appear in supporting documentation, and
+ * that the name of the copyright holders not be used in advertising or
+ * publicity pertaining to distribution of the software without specific,
+ * written prior permission. The copyright holders make no representations
+ * about the suitability of this software for any purpose. It is provided "as
+ * is" without express or implied warranty.
+ *
+ * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
+ * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
+ * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
+ * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
+ * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
+ * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ */
+
+#include <config.h>
+#include <libevdev/libevdev-int.h>
+#include "test-common.h"
+
+struct entry {
+ int code;
+ const char *name;
+ size_t name_len;
+ const char *suffix;
+ size_t suffix_len;
+ int value;
+};
+
+#define IENTRY(_code, _prefix, _suffix, _value) \
+ { \
+ .code = _code, \
+ .name = # _prefix # _suffix, \
+ .name_len = sizeof(# _prefix # _suffix) - 1, \
+ .suffix = # _suffix, \
+ .suffix_len = sizeof(# _suffix) - 1, \
+ .value = _value, \
+ }
+
+#define ENTRY(_code, _prefix, _suffix) IENTRY(_code, _prefix, _suffix, _prefix ## _suffix)
+
+static const struct entry types[] = {
+ ENTRY(-1, EV_, SYN),
+ ENTRY(-1, EV_, KEY),
+ ENTRY(-1, EV_, REL),
+ ENTRY(-1, EV_, ABS),
+ ENTRY(-1, EV_, MSC),
+ ENTRY(-1, EV_, SW),
+ ENTRY(-1, EV_, LED),
+ ENTRY(-1, EV_, SND),
+ ENTRY(-1, EV_, REP),
+ ENTRY(-1, EV_, FF),
+ ENTRY(-1, EV_, PWR),
+ ENTRY(-1, EV_, FF_STATUS),
+ ENTRY(-1, EV_, MAX),
+};
+
+START_TEST(test_type_codes)
+{
+ int i, v, flags;
+
+ flags = LIBEVDEV_RESOLVER_FLAG_DEFAULT;
+
+ ck_assert(libevdev_event_type_from_name("EV_FF", -1, flags) == EV_FF);
+
+ for (i = 0; i < ARRAY_LENGTH(types); ++i) {
+ v = libevdev_event_type_from_name(types[i].name, -1, flags);
+ ck_assert(v == types[i].value);
+
+ v = libevdev_event_type_from_name(types[i].name,
+ types[i].name_len, flags);
+ ck_assert(v == types[i].value);
+
+ v = libevdev_event_type_from_name(types[i].suffix, -1, flags);
+ ck_assert(v == types[i].value);
+
+ v = libevdev_event_type_from_name(types[i].suffix,
+ types[i].suffix_len, flags);
+ ck_assert(v == types[i].value);
+ }
+}
+END_TEST
+
+START_TEST(test_type_codes_case)
+{
+ int i, v, flags;
+ static const struct entry entries[] = {
+ IENTRY(-1, ev_, max, EV_MAX),
+ IENTRY(-1, ev_, SYN, EV_SYN),
+ IENTRY(-1, ev_, REL, EV_REL),
+ IENTRY(-1, EV_, FF_STATUs, EV_FF_STATUS),
+ IENTRY(-1, EV_, pwr, EV_PWR),
+ };
+
+ flags = LIBEVDEV_RESOLVER_FLAG_DEFAULT;
+
+ for (i = 0; i < ARRAY_LENGTH(entries); ++i) {
+ v = libevdev_event_type_from_name(entries[i].name, -1, flags);
+ ck_assert(v == entries[i].value);
+ }
+
+ flags = LIBEVDEV_RESOLVER_FLAG_CASE_SENSITIVE;
+
+ for (i = 0; i < ARRAY_LENGTH(entries); ++i) {
+ v = libevdev_event_type_from_name(entries[i].name, -1, flags);
+ ck_assert(v == -1);
+ }
+
+ ck_assert(libevdev_event_type_from_name("EV_FF", -1, flags) == EV_FF);
+ ck_assert(libevdev_event_type_from_name("EV_Ff", -1, flags) == -1);
+
+ for (i = 0; i < ARRAY_LENGTH(types); ++i) {
+ v = libevdev_event_type_from_name(types[i].name, -1, flags);
+ ck_assert(v == types[i].value);
+
+ v = libevdev_event_type_from_name(types[i].name,
+ types[i].name_len, flags);
+ ck_assert(v == types[i].value);
+
+ v = libevdev_event_type_from_name(types[i].suffix, -1, flags);
+ ck_assert(v == types[i].value);
+
+ v = libevdev_event_type_from_name(types[i].suffix,
+ types[i].suffix_len, flags);
+ ck_assert(v == types[i].value);
+ }
+}
+END_TEST
+
+START_TEST(test_type_codes_prefix)
+{
+ int i, v, flags;
+ static const struct entry entries[] = {
+ IENTRY(-1, ev_, max, EV_MAX),
+ IENTRY(-1, ev_, SYN, EV_SYN),
+ IENTRY(-1, ev_, REL, EV_REL),
+ IENTRY(-1, EV_, FF_STATUs, EV_FF_STATUS),
+ IENTRY(-1, EV_, pwr, EV_PWR),
+ };
+
+ flags = LIBEVDEV_RESOLVER_FLAG_DEFAULT;
+
+ ck_assert(libevdev_event_type_from_name("EV_EV_FF", -1, flags) == -1);
+ ck_assert(libevdev_event_type_from_name("EV_EV_ff", -1, flags) == -1);
+ ck_assert(libevdev_event_type_from_name("EV_FF", -1, flags) == EV_FF);
+ ck_assert(libevdev_event_type_from_name("ev_ff", -1, flags) == EV_FF);
+
+ for (i = 0; i < ARRAY_LENGTH(entries); ++i) {
+ v = libevdev_event_type_from_name(entries[i].suffix, -1, flags);
+ ck_assert(v == entries[i].value);
+ }
+
+ for (i = 0; i < ARRAY_LENGTH(types); ++i) {
+ v = libevdev_event_type_from_name(types[i].name, -1, flags);
+ ck_assert(v == types[i].value);
+
+ v = libevdev_event_type_from_name(types[i].name,
+ types[i].name_len, flags);
+ ck_assert(v == types[i].value);
+
+ v = libevdev_event_type_from_name(types[i].suffix, -1, flags);
+ ck_assert(v == types[i].value);
+
+ v = libevdev_event_type_from_name(types[i].suffix,
+ types[i].suffix_len, flags);
+ ck_assert(v == types[i].value);
+ }
+
+ flags = LIBEVDEV_RESOLVER_FLAG_REQUIRE_PREFIX;
+
+ ck_assert(libevdev_event_type_from_name("EV_EV_FF", -1, flags) == -1);
+ ck_assert(libevdev_event_type_from_name("EV_EV_ff", -1, flags) == -1);
+ ck_assert(libevdev_event_type_from_name("EV_FF", -1, flags) == EV_FF);
+ ck_assert(libevdev_event_type_from_name("ev_ff", -1, flags) == EV_FF);
+ ck_assert(libevdev_event_type_from_name("FF", -1, flags) == -1);
+ ck_assert(libevdev_event_type_from_name("ff", -1, flags) == -1);
+
+ for (i = 0; i < ARRAY_LENGTH(entries); ++i) {
+ v = libevdev_event_type_from_name(entries[i].suffix, -1, flags);
+ ck_assert(v == -1);
+ }
+
+ for (i = 0; i < ARRAY_LENGTH(types); ++i) {
+ v = libevdev_event_type_from_name(types[i].name, -1, flags);
+ ck_assert(v == types[i].value);
+
+ v = libevdev_event_type_from_name(types[i].name,
+ types[i].name_len, flags);
+ ck_assert(v == types[i].value);
+
+ v = libevdev_event_type_from_name(types[i].suffix, -1, flags);
+ ck_assert(v == -1);
+
+ v = libevdev_event_type_from_name(types[i].suffix,
+ types[i].suffix_len, flags);
+ ck_assert(v == -1);
+ }
+}
+END_TEST
+
+START_TEST(test_type_invalid)
+{
+ int i, v, flags;
+ static const struct entry entries[] = {
+ IENTRY(-1, EV_, ASDF, -1),
+ IENTRY(-1, EV_, CNT, -1),
+ IENTRY(-1, EV_, SYNS, -1),
+ IENTRY(-1, EV_, FF_STATU, -1),
+ IENTRY(-1, EV_, A, -1),
+ };
+
+ flags = LIBEVDEV_RESOLVER_FLAG_DEFAULT;
+
+ for (i = 0; i < ARRAY_LENGTH(entries); ++i) {
+ v = libevdev_event_type_from_name(entries[i].name, -1, flags);
+ ck_assert(v == entries[i].value);
+ }
+
+ for (i = 0; i < ARRAY_LENGTH(entries); ++i) {
+ v = libevdev_event_type_from_name(entries[i].suffix, -1, flags);
+ ck_assert(v == entries[i].value);
+ }
+}
+END_TEST
+
+START_TEST(test_type_invalid_case)
+{
+ int i, v, flags;
+ static const struct entry entries[] = {
+ IENTRY(-1, EV_, ASDF, -1),
+ IENTRY(-1, EV_, CNT, -1),
+ IENTRY(-1, EV_, SYNS, -1),
+ IENTRY(-1, EV_, FF_STATU, -1),
+ IENTRY(-1, EV_, A, -1),
+ IENTRY(-1, EV_, a, -1),
+ IENTRY(-1, EV_, asdf, -1),
+ IENTRY(-1, EV_, SYn, -1),
+ IENTRY(-1, EV_, syN, -1),
+ IENTRY(-1, EV_, ff_status, -1),
+ IENTRY(-1, EV_, cnt, -1),
+ IENTRY(-1, EV_, sw, -1),
+ };
+
+ flags = LIBEVDEV_RESOLVER_FLAG_CASE_SENSITIVE;
+
+ for (i = 0; i < ARRAY_LENGTH(entries); ++i) {
+ v = libevdev_event_type_from_name(entries[i].name, -1, flags);
+ ck_assert(v == entries[i].value);
+ }
+
+ for (i = 0; i < ARRAY_LENGTH(entries); ++i) {
+ v = libevdev_event_type_from_name(entries[i].suffix, -1, flags);
+ ck_assert(v == entries[i].value);
+ }
+}
+END_TEST
+
+START_TEST(test_key_codes)
+{
+ int i, v, flags, type;
+ static const struct entry entries[] = {
+ ENTRY(EV_KEY, KEY_, A),
+ ENTRY(EV_KEY, KEY_, ESC),
+ ENTRY(EV_KEY, KEY_, MAX),
+ ENTRY(EV_KEY, BTN_, TRIGGER_HAPPY40),
+ IENTRY(EV_KEY, BTN_, WHEEL, -1),
+ };
+
+ flags = LIBEVDEV_RESOLVER_FLAG_DEFAULT;
+ type = -1;
+
+ for (i = 0; i < ARRAY_LENGTH(entries); ++i) {
+ v = libevdev_event_code_from_name(entries[i].name, -1, type, flags);
+ ck_assert(v == entries[i].value);
+ }
+
+ type = EV_KEY;
+
+ for (i = 0; i < ARRAY_LENGTH(entries); ++i) {
+ v = libevdev_event_code_from_name(entries[i].suffix, -1, type, flags);
+ ck_assert(v == entries[i].value);
+
+ v = libevdev_event_code_from_name(entries[i].name, -1, type, flags);
+ ck_assert(v == entries[i].value);
+ }
+}
+END_TEST
+
+Suite *
+event_code_suite(void)
+{
+ Suite *s = suite_create("Event codes");
+
+ TCase *tc = tcase_create("type tests");
+ tcase_add_test(tc, test_type_codes);
+ tcase_add_test(tc, test_type_codes_case);
+ tcase_add_test(tc, test_type_codes_prefix);
+ tcase_add_test(tc, test_type_invalid);
+ tcase_add_test(tc, test_type_invalid_case);
+ suite_add_tcase(s, tc);
+
+ tc = tcase_create("key tests");
+ tcase_add_test(tc, test_key_codes);
+ suite_add_tcase(s, tc);
+
+ return s;
+}
+
diff --git a/test/test-main.c b/test/test-main.c
index c6efaf6..73d79c9 100644
--- a/test/test-main.c
+++ b/test/test-main.c
@@ -24,6 +24,7 @@
#include <check.h>
extern Suite *event_name_suite(void);
+extern Suite *event_code_suite(void);
extern Suite *libevdev_init_test(void);
extern Suite *queue_suite(void);
extern Suite *libevdev_has_event_test(void);
@@ -39,6 +40,7 @@ int main(int argc, char **argv)
srunner_add_suite(sr, libevdev_init_test());
srunner_add_suite(sr, queue_suite());
srunner_add_suite(sr, event_name_suite());
+ srunner_add_suite(sr, event_code_suite());
srunner_add_suite(sr, uinput_suite());
srunner_run_all(sr, CK_NORMAL);
--
1.8.4
More information about the Input-tools
mailing list