tests: posix: add a test for clock_getres()

Add a test for clock_getres().

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
This commit is contained in:
Christopher Friedt 2024-03-20 18:06:01 -04:00 committed by Chris Friedt
parent d36dd8097e
commit 0084092fae

View file

@ -227,4 +227,48 @@ ZTEST(clock, test_clock_getcpuclockid)
zassert_equal(ret, EPERM, "POSIX clock_getcpuclock id failed");
}
ZTEST(clock, test_clock_getres)
{
int ret;
struct timespec res;
const struct timespec one_ns = {
.tv_sec = 0,
.tv_nsec = 1,
};
struct arg {
clockid_t clock_id;
struct timespec *res;
int expect;
};
const struct arg args[] = {
/* permuting over "invalid" inputs */
{CLOCK_INVALID, NULL, -1},
{CLOCK_INVALID, &res, -1},
{CLOCK_REALTIME, NULL, 0},
{CLOCK_MONOTONIC, NULL, 0},
{CLOCK_PROCESS_CPUTIME_ID, NULL, 0},
/* all valid inputs */
{CLOCK_REALTIME, &res, 0},
{CLOCK_MONOTONIC, &res, 0},
{CLOCK_PROCESS_CPUTIME_ID, &res, 0},
};
ARRAY_FOR_EACH_PTR(args, arg) {
errno = 0;
res = (struct timespec){0};
ret = clock_getres(arg->clock_id, arg->res);
zassert_equal(ret, arg->expect);
if (ret != 0) {
zassert_equal(errno, EINVAL);
continue;
}
if (arg->res != NULL) {
zassert_true(tp_ge(arg->res, &one_ns));
}
}
}
ZTEST_SUITE(clock, NULL, NULL, NULL, NULL, NULL);