Add test suite for parser - WIP

pull/26/head
nirenjan 2020-07-10 02:35:53 -07:00
parent 4bd3ae69fe
commit 2c40785c2b
3 changed files with 100 additions and 1 deletions

View File

@ -30,12 +30,16 @@ x52include_HEADERS = libx52io.h
if HAVE_CMOCKA if HAVE_CMOCKA
LOG_DRIVER = env AM_TAP_AWK='$(AWK)' $(SHELL) $(top_srcdir)/tap-driver.sh LOG_DRIVER = env AM_TAP_AWK='$(AWK)' $(SHELL) $(top_srcdir)/tap-driver.sh
TESTS = test-axis TESTS = test-axis test-parser
check_PROGRAMS = $(TESTS) check_PROGRAMS = $(TESTS)
test_axis_SOURCES = test_axis.c $(libx52io_la_SOURCES) test_axis_SOURCES = test_axis.c $(libx52io_la_SOURCES)
test_axis_CFLAGS = $(libx52io_la_CFLAGS) test_axis_CFLAGS = $(libx52io_la_CFLAGS)
test_axis_LDFLAGS = @CMOCKA_LIBS@ @HIDAPI_LIBS@ $(WARN_LDFLAGS) test_axis_LDFLAGS = @CMOCKA_LIBS@ @HIDAPI_LIBS@ $(WARN_LDFLAGS)
test_parser_SOURCES = test_parser.c $(libx52io_la_SOURCES)
test_parser_CFLAGS = $(libx52io_la_CFLAGS)
test_parser_LDFLAGS = @CMOCKA_LIBS@ @HIDAPI_LIBS@ $(WARN_LDFLAGS)
endif endif
# Extra files that need to be in the distribution # Extra files that need to be in the distribution

View File

@ -265,5 +265,6 @@ int libx52io_read_timeout(libx52io_context *ctx, libx52io_report *report, int ti
return LIBX52IO_ERROR_IO; return LIBX52IO_ERROR_IO;
} }
// rc > 0
return _x52io_parse_report(ctx, report, data, rc); return _x52io_parse_report(ctx, report, data, rc);
} }

View File

@ -0,0 +1,94 @@
/*
* Saitek X52 IO driver - Parser test suite
*
* Copyright (C) 2012-2020 Nirenjan Krishnan (nirenjan@nirenjan.org)
*
* SPDX-License-Identifier: GPL-2.0-only WITH Classpath-exception-2.0
*/
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#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;
}
#define TEST_SETUP(prodid) test_setup_ ## prodid
#define TEST_SETUP_FUNCTION(prodid) static int TEST_SETUP(prodid)(void **state) { \
libx52io_context *ctx = *state; \
ctx->pid = X52_PROD_X52 ## prodid; \
_x52io_set_report_parser(ctx); \
return 0; \
}
TEST_SETUP_FUNCTION(_1);
TEST_SETUP_FUNCTION(_2);
TEST_SETUP_FUNCTION(PRO);
#undef TEST_SETUP_FUNCTION
static int test_teardown(void **state)
{
libx52io_context *ctx = *state;
libx52io_close(ctx);
return 0;
}
static int group_teardown(void **state)
{
libx52io_context *ctx = *state;
libx52io_exit(ctx);
return 0;
}
static void test_error_x52(void **state) {
/* Verify that passing a buffer of the wrong size returns IO error */
libx52io_context *ctx = *state;
unsigned char data[15];
int rc;
rc = _x52io_parse_report(ctx, NULL, data, sizeof(data));
assert_int_equal(rc, LIBX52IO_ERROR_IO);
}
static void test_error_pro(void **state) {
/* Verify that passing a buffer of the wrong size returns IO error */
libx52io_context *ctx = *state;
unsigned char data[14];
int rc;
rc = _x52io_parse_report(ctx, NULL, data, sizeof(data));
assert_int_equal(rc, LIBX52IO_ERROR_IO);
}
const struct CMUnitTest tests[] = {
cmocka_unit_test_setup_teardown(test_error_x52, TEST_SETUP(_1), test_teardown),
cmocka_unit_test_setup_teardown(test_error_x52, TEST_SETUP(_2), test_teardown),
cmocka_unit_test_setup_teardown(test_error_pro, TEST_SETUP(PRO), test_teardown),
};
int main(void)
{
cmocka_set_message_output(CM_OUTPUT_TAP);
cmocka_run_group_tests(tests, group_setup, group_teardown);
return 0;
}