|
| 1 | +/* |
| 2 | + * Copyright (c) 2018 Intel Corporation |
| 3 | + * Copyright (c) 2023, Meta |
| 4 | + * |
| 5 | + * SPDX-License-Identifier: Apache-2.0 |
| 6 | + */ |
| 7 | +#include <sys/time.h> |
| 8 | +#include <time.h> |
| 9 | +#include <unistd.h> |
| 10 | + |
| 11 | +#include <zephyr/ztest.h> |
| 12 | + |
| 13 | +static inline int64_t ts_to_ns(const struct timespec *ts) |
| 14 | +{ |
| 15 | + return ts->tv_sec * NSEC_PER_SEC + ts->tv_nsec; |
| 16 | +} |
| 17 | + |
| 18 | +static inline void tv_to_ts(const struct timeval *tv, struct timespec *ts) |
| 19 | +{ |
| 20 | + ts->tv_sec = tv->tv_sec; |
| 21 | + ts->tv_nsec = tv->tv_usec * NSEC_PER_USEC; |
| 22 | +} |
| 23 | + |
| 24 | +#define _tp_op(_a, _b, _op) (ts_to_ns(_a) _op ts_to_ns(_b)) |
| 25 | + |
| 26 | +#define _decl_op(_type, _name, _op) \ |
| 27 | + static inline _type _name(const struct timespec *_a, const struct timespec *_b) \ |
| 28 | + { \ |
| 29 | + return _tp_op(_a, _b, _op); \ |
| 30 | + } |
| 31 | + |
| 32 | +_decl_op(bool, tp_ge, >=); /* a >= b */ |
| 33 | + |
| 34 | +ZTEST(xsi_single_process, test_gettimeofday) |
| 35 | +{ |
| 36 | + struct timeval tv; |
| 37 | + struct timespec ts; |
| 38 | + struct timespec rts; |
| 39 | + |
| 40 | + if (false) { |
| 41 | + /* undefined behaviour */ |
| 42 | + errno = 0; |
| 43 | + zassert_equal(gettimeofday(NULL, NULL), -1); |
| 44 | + zassert_equal(errno, EINVAL); |
| 45 | + } |
| 46 | + |
| 47 | + /* Validate gettimeofday API */ |
| 48 | + zassert_ok(gettimeofday(&tv, NULL)); |
| 49 | + zassert_ok(clock_gettime(CLOCK_REALTIME, &rts)); |
| 50 | + |
| 51 | + /* TESTPOINT: Check if time obtained from |
| 52 | + * gettimeofday is same or more than obtained |
| 53 | + * from clock_gettime |
| 54 | + */ |
| 55 | + tv_to_ts(&tv, &ts); |
| 56 | + zassert_true(tp_ge(&rts, &ts)); |
| 57 | +} |
0 commit comments