diff --git a/lib/libx52io/Makefile.am b/lib/libx52io/Makefile.am index 748017c..2069f0c 100644 --- a/lib/libx52io/Makefile.am +++ b/lib/libx52io/Makefile.am @@ -28,5 +28,15 @@ x52include_HEADERS = libx52io.h # pkg-config files # pkgconfig_DATA = libx52io.pc +if HAVE_CMOCKA +LOG_DRIVER = env AM_TAP_AWK='$(AWK)' $(SHELL) $(top_srcdir)/tap-driver.sh +TESTS = test-axis +check_PROGRAMS = $(TESTS) + +test_axis_SOURCES = test_axis.c $(libx52io_la_SOURCES) +test_axis_CFLAGS = $(libx52io_la_CFLAGS) +test_axis_LDFLAGS = @CMOCKA_LIBS@ @HIDAPI_LIBS@ $(WARN_LDFLAGS) +endif + # Extra files that need to be in the distribution EXTRA_DIST = libx52io.h io_common.h diff --git a/lib/libx52io/io_core.c b/lib/libx52io/io_core.c index 8d28f37..de9efa9 100644 --- a/lib/libx52io/io_core.c +++ b/lib/libx52io/io_core.c @@ -52,8 +52,8 @@ int libx52io_close(libx52io_context *ctx) if (ctx->handle != NULL) { hid_close(ctx->handle); - memset(ctx, 0, sizeof(*ctx)); } + memset(ctx, 0, sizeof(*ctx)); return LIBX52IO_SUCCESS; } diff --git a/lib/libx52io/test_axis.c b/lib/libx52io/test_axis.c new file mode 100644 index 0000000..3f0b727 --- /dev/null +++ b/lib/libx52io/test_axis.c @@ -0,0 +1,140 @@ +/* + * Saitek X52 IO driver - Axis test suite + * + * Copyright (C) 2012-2020 Nirenjan Krishnan (nirenjan@nirenjan.org) + * + * SPDX-License-Identifier: GPL-2.0-only WITH Classpath-exception-2.0 + */ + +#include +#include +#include +#include + +#include "io_common.h" +#include "usb-ids.h" + +static int group_setup(void **state) +{ + libx52io_context *ctx; + int rc; + + rc = libx52io_init(&ctx); + if (rc != LIBX52IO_SUCCESS) { + return rc; + } + + *state = ctx; + return 0; +} + +static int test_setup(void **state) +{ + libx52io_context *ctx = *state; + + /* Create a dummy handle so that the test cases don't abort early */ + ctx->handle = (void *)(uintptr_t)(-1); + + return 0; +} + +static int test_teardown(void **state) +{ + libx52io_context *ctx = *state; + + ctx->handle = NULL; + libx52io_close(ctx); + return 0; +} + +static int group_teardown(void **state) +{ + libx52io_context *ctx = *state; + + ctx->handle = NULL; + libx52io_exit(ctx); + return 0; +} + +/* List of test cases - list the axis and the corresponding maximum for that axis */ +#define TEST_CASES \ + TEST_X52(X, 2047) \ + TEST_X52(Y, 2047) \ + TEST_X52(RZ, 1023) \ + TEST_X52(Z, 255) \ + TEST_X52(RX, 255) \ + TEST_X52(RY, 255) \ + TEST_X52(SLIDER, 255) \ + TEST_X52(THUMBX, 15) \ + TEST_X52(THUMBY, 15) \ + TEST_PRO(X, 1023) \ + TEST_PRO(Y, 1023) \ + TEST_PRO(RZ, 1023) \ + TEST_PRO(Z, 255) \ + TEST_PRO(RX, 255) \ + TEST_PRO(RY, 255) \ + TEST_PRO(SLIDER, 255) \ + TEST_PRO(THUMBX, 15) \ + TEST_PRO(THUMBY, 15) + +#define TEST_X52(axis, max) TEST(_1, axis, max) TEST(_2, axis, max) +#define TEST_PRO(axis, max) TEST(PRO, axis, max) + +#define TEST(prodid, axis, maxval) \ + static void axis ## _ ## prodid (void **state) \ + { \ + libx52io_context *ctx = *state; \ + int rc; \ + int32_t min, max; \ + ctx->pid = X52_PROD_X52 ## prodid; \ + _x52io_set_axis_range(ctx); \ + rc = libx52io_get_axis_range(ctx, LIBX52IO_AXIS_ ## axis, &min, &max); \ + assert_int_equal(rc, LIBX52IO_SUCCESS); \ + assert_int_equal(min, 0); \ + assert_int_equal(max, maxval); \ + } + +TEST_CASES +#undef TEST + +static void test_error_case(void **state) +{ + libx52io_context *ctx = *state; + int rc; + int32_t min, max; + + rc = libx52io_get_axis_range(NULL, 0, NULL, NULL); + assert_int_equal(rc, LIBX52IO_ERROR_INVALID); + + rc = libx52io_get_axis_range(ctx, 0, NULL, NULL); + assert_int_equal(rc, LIBX52IO_ERROR_INVALID); + + rc = libx52io_get_axis_range(ctx, 0, &min, NULL); + assert_int_equal(rc, LIBX52IO_ERROR_INVALID); + + rc = libx52io_get_axis_range(ctx, -1, &min, &max); + assert_int_equal(rc, LIBX52IO_ERROR_INVALID); + + rc = libx52io_get_axis_range(ctx, LIBX52IO_AXIS_MAX, &min, &max); + assert_int_equal(rc, LIBX52IO_ERROR_INVALID); + + ctx->handle = NULL; + rc = libx52io_get_axis_range(ctx, LIBX52IO_AXIS_X, &min, &max); + assert_int_equal(rc, LIBX52IO_ERROR_NO_DEVICE); +} + +#define TEST(prodid, axis, maxval) cmocka_unit_test_setup_teardown(axis ## _ ## prodid, test_setup, test_teardown), + +const struct CMUnitTest tests[] = { + cmocka_unit_test_setup_teardown(test_error_case, test_setup, test_teardown), + TEST_CASES +}; +#undef TEST + +int main(void) +{ + cmocka_set_message_output(CM_OUTPUT_TAP); + cmocka_run_group_tests(tests, group_setup, group_teardown); + return 0; +} +