1/* Useful functions for tests that use struct timespec.
2 Copyright (C) 2019-2020 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19#ifndef SUPPORT_TIMESPEC_H
20#define SUPPORT_TIMESPEC_H
21
22#include <stdio.h>
23#include <time.h>
24#include <support/check.h>
25#include <support/xtime.h>
26
27struct timespec timespec_add (struct timespec, struct timespec)
28 __attribute__((const));
29struct timespec timespec_sub (struct timespec, struct timespec)
30 __attribute__((const));
31
32static inline struct timespec
33make_timespec (time_t s, long int ns)
34{
35 struct timespec r;
36 r.tv_sec = s;
37 r.tv_nsec = ns;
38 return r;
39}
40
41enum { TIMESPEC_HZ = 1000000000 };
42
43void test_timespec_before_impl (const char *file, int line,
44 const struct timespec left,
45 const struct timespec right);
46
47void test_timespec_equal_or_after_impl (const char *file, int line,
48 const struct timespec left,
49 const struct timespec right);
50
51/* Check that the timespec on the left represents a time before the
52 time on the right. */
53#define TEST_TIMESPEC_BEFORE(left, right) \
54 test_timespec_before_impl (__FILE__, __LINE__, (left), (right))
55
56#define TEST_TIMESPEC_BEFORE_NOW(left, clockid) \
57 ({ \
58 struct timespec now; \
59 const int saved_errno = errno; \
60 xclock_gettime ((clockid), &now); \
61 TEST_TIMESPEC_BEFORE ((left), now); \
62 errno = saved_errno; \
63 })
64
65/* Check that the timespec on the left represents a time equal to or
66 after the time on the right. */
67#define TEST_TIMESPEC_EQUAL_OR_AFTER(left, right) \
68 test_timespec_equal_or_after_impl (__FILE__, __LINE__, left, right)
69
70#define TEST_TIMESPEC_NOW_OR_AFTER(clockid, right) \
71 ({ \
72 struct timespec now; \
73 const int saved_errno = errno; \
74 xclock_gettime ((clockid), &now); \
75 TEST_TIMESPEC_EQUAL_OR_AFTER (now, (right)); \
76 errno = saved_errno; \
77 })
78
79#endif /* SUPPORT_TIMESPEC_H */
80