mirror of https://github.com/nirenjan/libx52.git
Reduce duplicate code in libx52 test code
parent
e1915bc734
commit
3eaee7b8f4
|
@ -16,12 +16,11 @@
|
||||||
|
|
||||||
typedef int (*blink_shift_fn)(libx52_device *, uint8_t);
|
typedef int (*blink_shift_fn)(libx52_device *, uint8_t);
|
||||||
|
|
||||||
struct test_case {
|
TEST_STRUCT (
|
||||||
const char *test_case_id;
|
|
||||||
blink_shift_fn func;
|
blink_shift_fn func;
|
||||||
uint8_t state;
|
uint8_t state;
|
||||||
struct ivpair data[2];
|
struct ivpair data[2];
|
||||||
};
|
)
|
||||||
|
|
||||||
#define on 1
|
#define on 1
|
||||||
#define off 0
|
#define off 0
|
||||||
|
@ -36,23 +35,16 @@ struct test_case {
|
||||||
|
|
||||||
#define TEST(func, state) { #func "/" #state, libx52_set_ ## func, state, {{ x52_ ## func ## _indicator, x52_ ## func ## _ ## state}, {0, 0}}}
|
#define TEST(func, state) { #func "/" #state, libx52_set_ ## func, state, {{ x52_ ## func ## _indicator, x52_ ## func ## _ ## state}, {0, 0}}}
|
||||||
|
|
||||||
const struct test_case test_cases[] = {
|
TEST_CASES = {
|
||||||
TEST(blink, on),
|
TEST(blink, on),
|
||||||
TEST(blink, off),
|
TEST(blink, off),
|
||||||
TEST(shift, on),
|
TEST(shift, on),
|
||||||
TEST(shift, off),
|
TEST(shift, off),
|
||||||
};
|
};
|
||||||
|
|
||||||
#define TC_COUNT (sizeof(test_cases) / sizeof(test_cases[0]))
|
TEST_FUNC()
|
||||||
|
|
||||||
void run_test(int tc_id)
|
|
||||||
{
|
{
|
||||||
struct libx52_device *dev = x52_test_init();
|
TEST_INIT();
|
||||||
|
|
||||||
struct test_case test = test_cases[tc_id];
|
|
||||||
|
|
||||||
#define PRINT_FAIL() printf("not ok %d %s\n", tc_id+1, test.test_case_id)
|
|
||||||
#define PRINT_PASS() printf("ok %d %s\n", tc_id+1, test.test_case_id)
|
|
||||||
int rc = (*test.func)(dev, test.state);
|
int rc = (*test.func)(dev, test.state);
|
||||||
|
|
||||||
if (rc != LIBX52_SUCCESS) {
|
if (rc != LIBX52_SUCCESS) {
|
||||||
|
@ -61,29 +53,7 @@ void run_test(int tc_id)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
rc = libx52_update(dev);
|
TEST_VERIFY(test.data);
|
||||||
if (rc != LIBX52_SUCCESS) {
|
|
||||||
PRINT_FAIL();
|
|
||||||
printf("# libx52_update failed, rc = %d\n", rc);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!x52_test_assert_expected(dev, test.data)) {
|
TEST_MAIN()
|
||||||
PRINT_FAIL();
|
|
||||||
x52_test_print_diagnostics();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
PRINT_PASS();
|
|
||||||
x52_test_cleanup(dev);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
|
|
||||||
printf("1..%ld\n", TC_COUNT);
|
|
||||||
for (i = 0; i < TC_COUNT; i++) {
|
|
||||||
run_test(i);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -48,4 +48,47 @@ void x52_test_cleanup(libx52_device *dev);
|
||||||
void x52_test_print_observed_data(libx52_device *dev);
|
void x52_test_print_observed_data(libx52_device *dev);
|
||||||
void x52_test_print_expected_data(struct ivpair *data);
|
void x52_test_print_expected_data(struct ivpair *data);
|
||||||
|
|
||||||
|
#define PRINT_FAIL() printf("not ok %d %s\n", tc_id+1, test.test_case_id)
|
||||||
|
#define PRINT_PASS() printf("ok %d %s\n", tc_id+1, test.test_case_id)
|
||||||
|
#define TEST_STRUCT(...) struct test_case { \
|
||||||
|
const char *test_case_id; \
|
||||||
|
__VA_ARGS__ \
|
||||||
|
};
|
||||||
|
|
||||||
|
#define TEST_CASES const struct test_case test_cases[]
|
||||||
|
#define TEST_FUNC() void run_test(int tc_id)
|
||||||
|
#define TEST_INIT() \
|
||||||
|
struct libx52_device *dev = x52_test_init(); \
|
||||||
|
struct test_case test = test_cases[tc_id];
|
||||||
|
|
||||||
|
#define TEST_VERIFY(data) do { \
|
||||||
|
int test_rc; \
|
||||||
|
test_rc = libx52_update(dev); \
|
||||||
|
if (test_rc != LIBX52_SUCCESS) { \
|
||||||
|
PRINT_FAIL(); \
|
||||||
|
printf("# libx52_update failed, rc = %d\n", test_rc); \
|
||||||
|
return; \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
if (!x52_test_assert_expected(dev, data)) { \
|
||||||
|
PRINT_FAIL(); \
|
||||||
|
x52_test_print_diagnostics(); \
|
||||||
|
x52_test_print_expected_data(data); \
|
||||||
|
x52_test_print_observed_data(dev); \
|
||||||
|
return; \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
PRINT_PASS(); \
|
||||||
|
x52_test_cleanup(dev); \
|
||||||
|
} while(0)
|
||||||
|
|
||||||
|
#define TEST_MAIN() int main() { \
|
||||||
|
int i; \
|
||||||
|
size_t tc_count = sizeof(test_cases) / sizeof(test_cases[0]); \
|
||||||
|
printf("1..%lu\n", tc_count); \
|
||||||
|
for (i = 0; i < tc_count; i++) { \
|
||||||
|
run_test(i); \
|
||||||
|
} \
|
||||||
|
}
|
||||||
|
|
||||||
#endif // !defined _TEST_COMMON_H
|
#endif // !defined _TEST_COMMON_H
|
||||||
|
|
|
@ -14,13 +14,12 @@
|
||||||
#include "test_common.h"
|
#include "test_common.h"
|
||||||
#include "x52_commands.h"
|
#include "x52_commands.h"
|
||||||
|
|
||||||
struct test_case {
|
TEST_STRUCT (
|
||||||
const char *test_case_id;
|
|
||||||
libx52_led_id led_id;
|
libx52_led_id led_id;
|
||||||
libx52_led_state state;
|
libx52_led_state state;
|
||||||
int retval;
|
int retval;
|
||||||
struct ivpair data[3];
|
struct ivpair data[3];
|
||||||
};
|
)
|
||||||
|
|
||||||
#define X52_LED_CMD 0xb8
|
#define X52_LED_CMD 0xb8
|
||||||
#define UNSUPPORTED(led, state) { #led "/" #state " unsupported", LIBX52_LED_ ## led, LIBX52_LED_STATE_ ## state, LIBX52_ERROR_NOT_SUPPORTED}
|
#define UNSUPPORTED(led, state) { #led "/" #state " unsupported", LIBX52_LED_ ## led, LIBX52_LED_STATE_ ## state, LIBX52_ERROR_NOT_SUPPORTED}
|
||||||
|
@ -31,7 +30,7 @@ struct test_case {
|
||||||
#define AMBER(led) { #led "/Amber", LIBX52_LED_## led, LIBX52_LED_STATE_AMBER, LIBX52_SUCCESS, {{X52_LED_CMD, ((LIBX52_LED_ ## led + 0) << 8) | 1}, {X52_LED_CMD, ((LIBX52_LED_ ## led + 1) << 8) | 1}, {0, 0}}}
|
#define AMBER(led) { #led "/Amber", LIBX52_LED_## led, LIBX52_LED_STATE_AMBER, LIBX52_SUCCESS, {{X52_LED_CMD, ((LIBX52_LED_ ## led + 0) << 8) | 1}, {X52_LED_CMD, ((LIBX52_LED_ ## led + 1) << 8) | 1}, {0, 0}}}
|
||||||
#define GREEN(led) { #led "/Green", LIBX52_LED_## led, LIBX52_LED_STATE_GREEN, LIBX52_SUCCESS, {{X52_LED_CMD, ((LIBX52_LED_ ## led + 0) << 8) | 0}, {X52_LED_CMD, ((LIBX52_LED_ ## led + 1) << 8) | 1}, {0, 0}}}
|
#define GREEN(led) { #led "/Green", LIBX52_LED_## led, LIBX52_LED_STATE_GREEN, LIBX52_SUCCESS, {{X52_LED_CMD, ((LIBX52_LED_ ## led + 0) << 8) | 0}, {X52_LED_CMD, ((LIBX52_LED_ ## led + 1) << 8) | 1}, {0, 0}}}
|
||||||
|
|
||||||
const struct test_case test_cases[] = {
|
TEST_CASES = {
|
||||||
OFF_MONO(FIRE),
|
OFF_MONO(FIRE),
|
||||||
ON(FIRE),
|
ON(FIRE),
|
||||||
UNSUPPORTED(FIRE, RED),
|
UNSUPPORTED(FIRE, RED),
|
||||||
|
@ -99,21 +98,16 @@ const struct test_case test_cases[] = {
|
||||||
UNSUPPORTED(THROTTLE, GREEN),
|
UNSUPPORTED(THROTTLE, GREEN),
|
||||||
};
|
};
|
||||||
|
|
||||||
#define TC_COUNT (sizeof(test_cases) / sizeof(test_cases[0]))
|
|
||||||
|
|
||||||
void run_test(int tc_id)
|
TEST_FUNC()
|
||||||
{
|
{
|
||||||
struct libx52_device *dev = x52_test_init();
|
TEST_INIT();
|
||||||
|
|
||||||
struct test_case test = test_cases[tc_id];
|
|
||||||
|
|
||||||
/* Set the X52Pro flag in dev->flags, otherwise libx52_set_led_state will
|
/* Set the X52Pro flag in dev->flags, otherwise libx52_set_led_state will
|
||||||
* always return not supported
|
* always return not supported
|
||||||
*/
|
*/
|
||||||
dev->flags = 1;
|
dev->flags = 1;
|
||||||
|
|
||||||
#define PRINT_FAIL() printf("not ok %d %s\n", tc_id+1, test.test_case_id)
|
|
||||||
#define PRINT_PASS() printf("ok %d %s\n", tc_id+1, test.test_case_id)
|
|
||||||
int rc = libx52_set_led_state(dev, test.led_id, test.state);
|
int rc = libx52_set_led_state(dev, test.led_id, test.state);
|
||||||
|
|
||||||
if (rc != test.retval) {
|
if (rc != test.retval) {
|
||||||
|
@ -122,29 +116,7 @@ void run_test(int tc_id)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
rc = libx52_update(dev);
|
TEST_VERIFY(test.data);
|
||||||
if (rc != LIBX52_SUCCESS) {
|
|
||||||
PRINT_FAIL();
|
|
||||||
printf("# libx52_update failed, rc = %d\n", rc);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!x52_test_assert_expected(dev, test.data)) {
|
TEST_MAIN()
|
||||||
PRINT_FAIL();
|
|
||||||
x52_test_print_diagnostics();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
PRINT_PASS();
|
|
||||||
x52_test_cleanup(dev);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
|
|
||||||
printf("1..%ld\n", TC_COUNT);
|
|
||||||
for (i = 0; i < TC_COUNT; i++) {
|
|
||||||
run_test(i);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -13,18 +13,17 @@
|
||||||
#include "x52_common.h"
|
#include "x52_common.h"
|
||||||
#include "test_common.h"
|
#include "test_common.h"
|
||||||
|
|
||||||
struct test_case {
|
TEST_STRUCT (
|
||||||
const char *test_case_id;
|
|
||||||
int offset_primary;
|
int offset_primary;
|
||||||
int offset_secondary;
|
int offset_secondary;
|
||||||
libx52_clock_id clock_id;
|
libx52_clock_id clock_id;
|
||||||
uint16_t x52_clock;
|
uint16_t x52_clock;
|
||||||
uint16_t x52_offset;
|
uint16_t x52_offset;
|
||||||
};
|
)
|
||||||
|
|
||||||
#define TEST(id, o1, o23, offs) {id, o1, o23, LIBX52_CLOCK_2, 0xc1, offs}, {id, o1, o23, LIBX52_CLOCK_3, 0xc2, offs}
|
#define TEST(id, o1, o23, offs) {id, o1, o23, LIBX52_CLOCK_2, 0xc1, offs}, {id, o1, o23, LIBX52_CLOCK_3, 0xc2, offs}
|
||||||
|
|
||||||
const struct test_case test_cases[] = {
|
TEST_CASES = {
|
||||||
TEST("Etc/UTC+24|Etc/UTC-24", -1440, +1440, 0),
|
TEST("Etc/UTC+24|Etc/UTC-24", -1440, +1440, 0),
|
||||||
TEST("Etc/UTC-24|Etc/UTC+24", +1440, -1440, 0x0400), // Negative 0
|
TEST("Etc/UTC-24|Etc/UTC+24", +1440, -1440, 0x0400), // Negative 0
|
||||||
TEST("Honolulu|Auckland", -600, +720, 0x0478), // -2 hours
|
TEST("Honolulu|Auckland", -600, +720, 0x0478), // -2 hours
|
||||||
|
@ -39,18 +38,12 @@ const struct test_case test_cases[] = {
|
||||||
TEST("Etc/UTC-12|Etc/UTC+12", +720, -720, 0x0400),
|
TEST("Etc/UTC-12|Etc/UTC+12", +720, -720, 0x0400),
|
||||||
};
|
};
|
||||||
|
|
||||||
#define TC_COUNT (sizeof(test_cases) / sizeof(test_cases[0]))
|
TEST_FUNC()
|
||||||
|
|
||||||
void run_test(int tc_id)
|
|
||||||
{
|
{
|
||||||
struct libx52_device *dev = x52_test_init();
|
TEST_INIT();
|
||||||
struct test_case test = test_cases[tc_id];
|
|
||||||
int rc;
|
int rc;
|
||||||
struct ivpair data[2] = { 0 };
|
struct ivpair data[2] = { 0 };
|
||||||
|
|
||||||
#define PRINT_FAIL() printf("not ok %d %s/%d\n", tc_id+1, test.test_case_id, test.clock_id + 1)
|
|
||||||
#define PRINT_PASS() printf("ok %d %s/%d\n", tc_id+1, test.test_case_id, test.clock_id + 1)
|
|
||||||
|
|
||||||
dev->timezone[LIBX52_CLOCK_1] = test.offset_primary;
|
dev->timezone[LIBX52_CLOCK_1] = test.offset_primary;
|
||||||
rc = libx52_set_clock_timezone(dev, test.clock_id, test.offset_secondary);
|
rc = libx52_set_clock_timezone(dev, test.clock_id, test.offset_secondary);
|
||||||
if (rc != LIBX52_SUCCESS) {
|
if (rc != LIBX52_SUCCESS) {
|
||||||
|
@ -59,34 +52,9 @@ void run_test(int tc_id)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
rc = libx52_update(dev);
|
|
||||||
if (rc != LIBX52_SUCCESS) {
|
|
||||||
PRINT_FAIL();
|
|
||||||
printf("# libx52_update failed, rc = %d\n", rc);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
data[0].index = test.x52_clock;
|
data[0].index = test.x52_clock;
|
||||||
data[0].value = test.x52_offset;
|
data[0].value = test.x52_offset;
|
||||||
|
TEST_VERIFY(data);
|
||||||
if (!x52_test_assert_expected(dev, data)) {
|
|
||||||
PRINT_FAIL();
|
|
||||||
x52_test_print_diagnostics();
|
|
||||||
x52_test_print_expected_data(data);
|
|
||||||
x52_test_print_observed_data(dev);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
PRINT_PASS();
|
TEST_MAIN();
|
||||||
x52_test_cleanup(dev);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
|
|
||||||
printf("1..%ld\n", TC_COUNT);
|
|
||||||
for (i = 0; i < TC_COUNT; i++) {
|
|
||||||
run_test(i);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue