test: Add test suite for libx52io/device.c

pull/87/head
nirenjan 2026-11-05 09:02:10 -08:00
parent 57ce34a681
commit 43ac4e8f27
2 changed files with 80 additions and 0 deletions

View File

@ -51,3 +51,9 @@ test_compare = executable('test-io-compare', 'test_compare.c', libx52io_files,
)
test('test-io-compare', test_compare, protocol: 'tap')
test_device = executable('test-device', 'test_device.c', libx52io_files,
build_by_default: false,
dependencies: [dep_cmocka, dep_hidapi, dep_intl],
include_directories: [includes],
)
test('test-io-device', test_device, protocol: 'tap')

View File

@ -0,0 +1,74 @@
/*
* Saitek X52 IO driver - device info test suite
*
* Copyright (C) 2012-2026 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 <wchar.h>
#include "common.h"
#include "usb-ids.h"
static void test_device_info(void **state)
{
struct hid_device_info dev_info = {0};
libx52io_context ctx = {0};
wchar_t invalid_wcs[] = { (wchar_t)-1, L'\0' };
(void)state;
dev_info.vendor_id = VENDOR_SAITEK;
dev_info.product_id = X52_PROD_X52PRO;
dev_info.release_number = 0x1337;
dev_info.manufacturer_string = L"Saitek";
dev_info.product_string = invalid_wcs;
dev_info.serial_number = L"";
_x52io_save_device_info(&ctx, &dev_info);
assert_int_equal(ctx.vid, VENDOR_SAITEK);
assert_int_equal(ctx.pid, X52_PROD_X52PRO);
assert_int_equal(ctx.version, 0x1337);
assert_string_equal(ctx.manufacturer, "Saitek");
assert_null(ctx.product);
assert_null(ctx.serial_number);
assert_int_equal(libx52io_get_vendor_id(&ctx), VENDOR_SAITEK);
assert_int_equal(libx52io_get_product_id(&ctx), X52_PROD_X52PRO);
assert_int_equal(libx52io_get_device_version(&ctx), 0x1337);
assert_string_equal(libx52io_get_manufacturer_string(&ctx), "Saitek");
assert_null(libx52io_get_product_string(&ctx));
assert_null(libx52io_get_serial_number_string(&ctx));
_x52io_release_device_info(&ctx);
}
static void test_null_handling(void **state)
{
(void)state;
assert_int_equal(libx52io_get_vendor_id(NULL), 0);
assert_int_equal(libx52io_get_product_id(NULL), 0);
assert_int_equal(libx52io_get_device_version(NULL), 0);
assert_null(libx52io_get_manufacturer_string(NULL));
assert_null(libx52io_get_product_string(NULL));
assert_null(libx52io_get_serial_number_string(NULL));
}
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_device_info),
cmocka_unit_test(test_null_handling),
};
int main(void)
{
cmocka_set_message_output(CM_OUTPUT_TAP);
cmocka_run_group_tests(tests, NULL, NULL);
return 0;
}