diff options
Diffstat (limited to 'accel-cmd')
| -rw-r--r-- | accel-cmd/CMakeLists.txt | 5 | ||||
| -rw-r--r-- | accel-cmd/accel_cmd.c | 2 | ||||
| -rw-r--r-- | accel-cmd/accel_cmd_test.c | 46 |
3 files changed, 52 insertions, 1 deletions
diff --git a/accel-cmd/CMakeLists.txt b/accel-cmd/CMakeLists.txt index 4455c8df..115e67fc 100644 --- a/accel-cmd/CMakeLists.txt +++ b/accel-cmd/CMakeLists.txt @@ -6,6 +6,11 @@ ADD_EXECUTABLE(accel-cmd accel_cmd.c ) +if (BUILD_TESTING) + add_executable(accel-cmd-timeout-test accel_cmd_test.c) + add_test(NAME accel-cmd-timeout COMMAND accel-cmd-timeout-test) +endif () + INSTALL(TARGETS accel-cmd RUNTIME DESTINATION bin ) diff --git a/accel-cmd/accel_cmd.c b/accel-cmd/accel_cmd.c index 3de8624f..6a6dc678 100644 --- a/accel-cmd/accel_cmd.c +++ b/accel-cmd/accel_cmd.c @@ -536,7 +536,7 @@ static int set_timespec(struct timespec *timeout, const char *str) timeout->tv_nsec = 0; } else if (strcmp(end, "ms") == 0) { timeout->tv_sec = ulong / 1000; - timeout->tv_nsec = (ulong % 1000) * 1000; + timeout->tv_nsec = (ulong % 1000) * 1000000; } else if (strcmp(end, "us") == 0) { timeout->tv_sec = ulong / 1000000; timeout->tv_nsec = (ulong % 1000000) * 1000; diff --git a/accel-cmd/accel_cmd_test.c b/accel-cmd/accel_cmd_test.c new file mode 100644 index 00000000..a127caca --- /dev/null +++ b/accel-cmd/accel_cmd_test.c @@ -0,0 +1,46 @@ +/* Exercise the production timeout parser without opening a connection. */ +#define main accel_cmd_main +#include "accel_cmd.c" +#undef main + +int main(void) +{ + static const struct { + const char *value; + time_t seconds; + long nanoseconds; + } cases[] = { + { "0", 0, 0 }, + { "2s", 2, 0 }, + { "0ms", 0, 0 }, + { "1ms", 0, 1000000 }, + { "500ms", 0, 500000000 }, + { "999ms", 0, 999000000 }, + { "1000ms", 1, 0 }, + { "1001ms", 1, 1000000 }, + { "1500ms", 1, 500000000 }, + { "1us", 0, 1000 }, + { "1500000us", 1, 500000000 }, + }; + static const char *invalid[] = { "", "-1ms", "1.5ms", "1m" }; + struct timespec timeout; + size_t i; + + for (i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) { + if (set_timespec(&timeout, cases[i].value) || + timeout.tv_sec != cases[i].seconds || + timeout.tv_nsec != cases[i].nanoseconds) { + fprintf(stderr, "incorrect timeout for %s\n", cases[i].value); + return 1; + } + } + for (i = 0; i < sizeof(invalid) / sizeof(invalid[0]); i++) { + if (!set_timespec(&timeout, invalid[i])) { + fprintf(stderr, "accepted invalid timeout %s\n", invalid[i]); + return 1; + } + } + + puts("accel-cmd timeout parsing: PASS"); + return 0; +} |
