t_timespec_get.c revision 1.1.4.2 1 /*-
2 * Copyright (c) 2025 The NetBSD Foundation, Inc.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to The NetBSD Foundation
6 * by Nia Alarie.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #include <atf-c.h>
31
32 #include <limits.h>
33 #include <time.h>
34
35 ATF_TC(timespec_getres);
36 ATF_TC_HEAD(timespec_getres, tc)
37 {
38 atf_tc_set_md_var(tc, "descr", "Resolution tests for timespec_getres");
39 }
40
41 ATF_TC_BODY(timespec_getres, tc)
42 {
43 struct timespec ts, ts2;
44
45 ATF_REQUIRE_EQ(timespec_getres(&ts, TIME_MONOTONIC), TIME_MONOTONIC);
46 ATF_REQUIRE_EQ(clock_getres(CLOCK_MONOTONIC, &ts2), 0);
47
48 ATF_REQUIRE_EQ(ts.tv_sec, ts2.tv_sec);
49 ATF_REQUIRE_EQ(ts.tv_nsec, ts2.tv_nsec);
50
51 ATF_REQUIRE_EQ(timespec_getres(&ts, TIME_UTC), TIME_UTC);
52 ATF_REQUIRE_EQ(clock_getres(CLOCK_REALTIME, &ts2), 0);
53
54 ATF_REQUIRE_EQ(ts.tv_sec, ts2.tv_sec);
55 ATF_REQUIRE_EQ(ts.tv_nsec, ts2.tv_nsec);
56
57 /* now an invalid value */
58 ATF_REQUIRE_EQ(timespec_getres(&ts, INT_MAX), 0);
59 }
60
61 ATF_TC(timespec_get);
62 ATF_TC_HEAD(timespec_get, tc)
63 {
64 atf_tc_set_md_var(tc, "descr", "Basic tests for timespec_get");
65 }
66
67 ATF_TC_BODY(timespec_get, tc)
68 {
69 struct timespec ts, ts2;
70
71 ATF_REQUIRE_EQ(timespec_get(&ts, TIME_UTC), TIME_UTC);
72 ATF_REQUIRE_EQ(clock_gettime(CLOCK_REALTIME, &ts2), 0);
73
74 /*
75 * basically test that these clocks (which should be the same source)
76 * aren't too wildly apart...
77 */
78
79 if (ts2.tv_sec >= ts.tv_sec) {
80 ATF_REQUIRE((ts2.tv_sec - ts.tv_sec) < 86400);
81 } else {
82 ATF_REQUIRE((ts.tv_sec - ts2.tv_sec) < 86400);
83 }
84
85 /* now an invalid value */
86 ATF_REQUIRE_EQ(timespec_get(&ts, INT_MAX), 0);
87 }
88
89 ATF_TC(timespec_get_monotonic);
90 ATF_TC_HEAD(timespec_get_monotonic, tc)
91 {
92 atf_tc_set_md_var(tc, "descr", "Monotonic tests for timespec_getres");
93 }
94
95 ATF_TC_BODY(timespec_get_monotonic, tc)
96 {
97 struct timespec ts, ts2;
98
99 ATF_REQUIRE_EQ(timespec_get(&ts, TIME_MONOTONIC), TIME_MONOTONIC);
100 ATF_REQUIRE_EQ(clock_gettime(CLOCK_MONOTONIC, &ts2), 0);
101
102 /*
103 * basically test that these clocks (which should be the same source)
104 * aren't too wildly apart...
105 */
106
107 ATF_REQUIRE(ts2.tv_sec >= ts.tv_sec);
108 ATF_REQUIRE((ts2.tv_sec - ts.tv_sec) < 86400);
109
110 /* test that it's actually monotonic */
111 ATF_REQUIRE_EQ(timespec_get(&ts2, TIME_MONOTONIC), TIME_MONOTONIC);
112 ATF_REQUIRE(ts2.tv_sec >= ts.tv_sec);
113 }
114
115 ATF_TP_ADD_TCS(tp)
116 {
117 ATF_TP_ADD_TC(tp, timespec_getres);
118 ATF_TP_ADD_TC(tp, timespec_get);
119 ATF_TP_ADD_TC(tp, timespec_get_monotonic);
120
121 return atf_no_error();
122 }
123
124