Update offset tests to use common test infrastructure.

Prior to the introduction of common test infrastructure, the offset test
was using an undocumented function in libx52 to validate the timezone
offset calculations.

This change allows us to hide that undocumented function and not have to
expose internal functionality in order to perform testing.
pull/22/head
nirenjan 2020-06-12 17:18:04 -07:00
parent 02165a8712
commit 16a7801e59
4 changed files with 54 additions and 32 deletions

View File

@ -33,7 +33,7 @@ pkgconfig_DATA = libx52.pc
check_PROGRAMS = test_offset test_led test_blink_shift
test_offset_SOURCES = test_offset.c
test_offset_SOURCES = test_offset.c test_common.c
test_offset_CFLAGS = @LIBUSB_CFLAGS@
test_offset_LDADD = libx52.la

View File

@ -11,51 +11,74 @@
#include <stdint.h>
#include <string.h>
#include "x52_common.h"
#include "test_common.h"
struct test_id {
struct test_case {
const char *test_case_id;
int offset_primary;
int offset_secondary;
libx52_clock_id clock_id;
uint16_t x52_clock;
uint16_t x52_offset;
};
const struct test_id test_cases[] = {
{"Etc/UTC+24|Etc/UTC-24", -1440, +1440, 0},
{"Etc/UTC-24|Etc/UTC+24", +1440, -1440, 0x0400}, // Negative 0
{"Honolulu|Auckland", -600, +720, 0x0478}, // -2 hours
{"Auckland|Honolulu", +720, -600, 0x0078}, // +2 hours
{"Etc/UTC+12|Etc/UTC-14", -720, +840, 0x078}, // +2 hours
{"Etc/UTC-14|Etc/UTC+12", +840, -720, 0x478}, // -2 hours
{"PDT|UTC", -420, 0, 0x1a4}, // +7 hours
{"UTC|PDT", 0, -420, 0x5a4}, // -7 hours
{"PST|UTC", -480, 0, 0x1e0}, // +8 hours
{"UTC|PST", 0, -480, 0x5e0}, // -8 hours
{"Etc/UTC+12|Etc/UTC-12", -720, +720, 0},
{"Etc/UTC-12|Etc/UTC+12", +720, -720, 0x0400},
#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("Etc/UTC+24|Etc/UTC-24", -1440, +1440, 0),
TEST("Etc/UTC-24|Etc/UTC+24", +1440, -1440, 0x0400), // Negative 0
TEST("Honolulu|Auckland", -600, +720, 0x0478), // -2 hours
TEST("Auckland|Honolulu", +720, -600, 0x0078), // +2 hours
TEST("Etc/UTC+12|Etc/UTC-14", -720, +840, 0x078), // +2 hours
TEST("Etc/UTC-14|Etc/UTC+12", +840, -720, 0x478), // -2 hours
TEST("PDT|UTC", -420, 0, 0x1a4), // +7 hours
TEST("UTC|PDT", 0, -420, 0x5a4), // -7 hours
TEST("PST|UTC", -480, 0, 0x1e0), // +8 hours
TEST("UTC|PST", 0, -480, 0x5e0), // -8 hours
TEST("Etc/UTC+12|Etc/UTC-12", -720, +720, 0),
TEST("Etc/UTC-12|Etc/UTC+12", +720, -720, 0x0400),
};
#define TC_COUNT (sizeof(test_cases) / sizeof(test_cases[0]))
void run_test(int tc_id)
{
struct libx52_device dev;
memset(&dev, 0, sizeof(dev));
struct libx52_device *dev = x52_test_init();
struct test_case test = test_cases[tc_id];
int rc;
struct ivpair data[2] = { 0 };
struct test_id test = test_cases[tc_id];
#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_2] = test.offset_secondary;
uint16_t obtained = libx52_calculate_clock_offset(&dev, LIBX52_CLOCK_2, 0);
if (obtained != test.x52_offset) {
printf("not ");
dev->timezone[LIBX52_CLOCK_1] = test.offset_primary;
rc = libx52_set_clock_timezone(dev, test.clock_id, test.offset_secondary);
if (rc != LIBX52_SUCCESS) {
PRINT_FAIL();
printf("# set_clock_timezone failed, rc = %d\n", rc);
return;
}
printf("ok %d %s\n", tc_id + 1, test.test_case_id);
if (obtained != test.x52_offset) {
// Diagnostics
printf("# Expected: %04x\n", test.x52_offset);
printf("# Observed: %04x\n", obtained);
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].value = test.x52_offset;
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);
}
int main()

View File

@ -109,6 +109,5 @@ static inline uint32_t tst_bit(uint32_t *value, uint32_t bit)
int _x52_translate_libusb_error(enum libusb_error errcode);
int _x52_vendor_command(libx52_device *x52, uint16_t index, uint16_t value);
uint16_t libx52_calculate_clock_offset(libx52_device *x52, libx52_clock_id clock, uint16_t h24);
#endif /* !defined X52JOY_COMMON_H */

View File

@ -218,7 +218,7 @@ static int _x52_write_date(libx52_device *x52, uint32_t bit)
return rc;
}
uint16_t libx52_calculate_clock_offset(libx52_device *x52, libx52_clock_id clock, uint16_t h24)
static uint16_t _x52_calculate_clock_offset(libx52_device *x52, libx52_clock_id clock, uint16_t h24)
{
int offset;
int negative;
@ -285,7 +285,7 @@ static int _x52_write_time(libx52_device *x52, uint32_t bit)
h24 = !!(x52->time_format[clock]);
if (clock != LIBX52_CLOCK_1) {
value = libx52_calculate_clock_offset(x52, clock, h24);
value = _x52_calculate_clock_offset(x52, clock, h24);
} else {
value = h24 << 15 |
(x52->time_hour & 0x7F) << 8 |