xref: /freebsd/tests/sys/sys/time_test.c (revision b3e76948)
1 /*-
2  * Copyright (c) 2022 Axcient
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions, and the following disclaimer,
10  *    without modification.
11  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12  *    substantially similar to the "NO WARRANTY" disclaimer below
13  *    ("Disclaimer") and any redistribution must be conditioned upon
14  *    including a substantially similar Disclaimer requirement for further
15  *    binary redistribution.
16  *
17  * NO WARRANTY
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGES.
29  */
30 #include <sys/types.h>
31 #include <sys/time.h>
32 
33 #include <inttypes.h>
34 #include <stdio.h>
35 
36 #include <atf-c.h>
37 
38 
39 static void
atf_check_nstosbt(sbintime_t expected,int64_t ns)40 atf_check_nstosbt(sbintime_t expected, int64_t ns) {
41 	sbintime_t actual = nstosbt(ns);
42 
43 	ATF_CHECK_MSG((expected) - 1 <= (actual) && actual <= (expected) + 1,
44 			"%"PRId64" != nstosbt(%"PRId64") (%"PRId64")",
45 			expected, ns, actual);
46 }
47 
48 ATF_TC_WITHOUT_HEAD(nstosbt);
ATF_TC_BODY(nstosbt,tc)49 ATF_TC_BODY(nstosbt, tc)
50 {
51 	atf_check_nstosbt(0, 0);
52 	atf_check_nstosbt(4, 1);
53 	/* 1 second */
54 	atf_check_nstosbt((1ll << 32) - 4, 999999999);
55 	atf_check_nstosbt(1ll << 32, 1000000000);
56 	/* 2 seconds https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=263073 */
57 	atf_check_nstosbt((1ll << 33) - 4, 1999999999);
58 	atf_check_nstosbt(1ll << 33, 2000000000);
59 	/* 4 seconds */
60 	atf_check_nstosbt((1ll << 34) - 4, 3999999999);
61 	atf_check_nstosbt((1ll << 34), 4000000000);
62 	/* Max value */
63 	atf_check_nstosbt(((1ll << 31) - 1) << 32,
64 			  ((1ll << 31) - 1) * 1000000000);
65 }
66 
67 static void
atf_check_ustosbt(sbintime_t expected,int64_t us)68 atf_check_ustosbt(sbintime_t expected, int64_t us) {
69 	sbintime_t actual = ustosbt(us);
70 
71 	ATF_CHECK_MSG((expected) - 1 <= (actual) && actual <= (expected) + 1,
72 			"%"PRId64" != ustosbt(%"PRId64") (%"PRId64")",
73 			expected, us, actual);
74 }
75 
76 ATF_TC_WITHOUT_HEAD(ustosbt);
ATF_TC_BODY(ustosbt,tc)77 ATF_TC_BODY(ustosbt, tc)
78 {
79 	atf_check_ustosbt(0, 0);
80 	atf_check_ustosbt(4295, 1);
81 	/* 1 second */
82 	atf_check_ustosbt((1ll << 32) - 4295, 999999);
83 	atf_check_ustosbt(1ll << 32, 1000000);
84 	/* 2 seconds https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=263073 */
85 	atf_check_ustosbt((1ll << 33) - 4295, 1999999);
86 	atf_check_ustosbt(1ll << 33, 2000000);
87 	/* 4 seconds */
88 	atf_check_ustosbt((1ll << 34) - 4295, 3999999);
89 	atf_check_ustosbt(1ll << 34, 4000000);
90 	/* Max value */
91 	atf_check_ustosbt(((1ull << 31) - 1) << 32,
92 			  ((1ll << 31) - 1) * 1000000);
93 }
94 
95 static void
atf_check_mstosbt(sbintime_t expected,int64_t ms)96 atf_check_mstosbt(sbintime_t expected, int64_t ms) {
97 	sbintime_t actual = mstosbt(ms);
98 
99 	ATF_CHECK_MSG((expected) - 1 <= (actual) && actual <= (expected) + 1,
100 			"%"PRId64" != mstosbt(%"PRId64") (%"PRId64")",
101 			expected, ms, actual);
102 }
103 
104 ATF_TC_WITHOUT_HEAD(mstosbt);
ATF_TC_BODY(mstosbt,tc)105 ATF_TC_BODY(mstosbt, tc)
106 {
107 	atf_check_mstosbt(0, 0);
108 	atf_check_mstosbt(4294967, 1);
109 	/* 1 second */
110 	atf_check_mstosbt((1ll << 32) - 4294968, 999);
111 	atf_check_mstosbt(1ll << 32, 1000);
112 	/* 2 seconds https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=263073 */
113 	atf_check_mstosbt((1ll << 33) - 4294968, 1999);
114 	atf_check_mstosbt(1ll << 33, 2000);
115 	/* 4 seconds */
116 	atf_check_mstosbt((1ll << 34) - 4294968, 3999);
117 	atf_check_mstosbt(1ll << 34, 4000);
118 	/* Max value */
119 	atf_check_mstosbt(((1ll << 31) - 1) << 32, ((1ll << 31) - 1) * 1000);
120 }
121 
122 static void
atf_check_sbttons(int64_t expected,sbintime_t sbt)123 atf_check_sbttons(int64_t expected, sbintime_t sbt) {
124 	int64_t actual = sbttons(sbt);
125 
126 	ATF_CHECK_MSG((expected) - 1 <= (actual) && actual <= (expected) + 1,
127 			"%"PRId64" != sbttons(%"PRId64") (%"PRId64")",
128 			expected, sbt, actual);
129 }
130 
131 ATF_TC_WITHOUT_HEAD(sbttons);
ATF_TC_BODY(sbttons,tc)132 ATF_TC_BODY(sbttons, tc)
133 {
134 	atf_check_sbttons(0, 0);
135 	atf_check_sbttons(0, 1);
136 	atf_check_sbttons(1, (1ll << 32) / 1000000000);
137 	/* 1 second */
138 	atf_check_sbttons(1000000000, 1ll << 32);
139 	atf_check_sbttons(1999999999, (1ll << 33) - 1);
140 	/* 2 seconds */
141 	atf_check_sbttons(1999999999, (1ll << 33) - 1);
142 	atf_check_sbttons(2000000000, 1ll << 33);
143 	/* 4 seconds */
144 	atf_check_sbttons(3999999999, (1ll << 34) - 1);
145 	atf_check_sbttons(4000000000, 1ll << 34);
146 	/* edge cases */
147 	atf_check_sbttons(999999999, (1ll << 32) - 1);
148 	atf_check_sbttons((1ll << 31) * 1000000000, (1ull << 63) - 1);
149 }
150 
151 static void
atf_check_sbttous(int64_t expected,sbintime_t sbt)152 atf_check_sbttous(int64_t expected, sbintime_t sbt) {
153 	int64_t actual = sbttous(sbt);
154 
155 	ATF_CHECK_MSG((expected) - 1 <= (actual) && actual <= (expected) + 1,
156 			"%"PRId64" != sbttous(%"PRId64") (%"PRId64")",
157 			expected, sbt, actual);
158 }
159 
160 ATF_TC_WITHOUT_HEAD(sbttous);
ATF_TC_BODY(sbttous,tc)161 ATF_TC_BODY(sbttous, tc)
162 {
163 	atf_check_sbttous(0, 0);
164 	atf_check_sbttous(0, 1);
165 	atf_check_sbttous(1, (1ll << 32) / 1000000);
166 	/* 1 second */
167 	atf_check_sbttous(1000000, 1ll << 32);
168 	atf_check_sbttous(1999999, (1ll << 33) - 1);
169 	/* 2 seconds */
170 	atf_check_sbttous(1999999, (1ll << 33) - 1);
171 	atf_check_sbttous(2000000, 1ll << 33);
172 	/* 4 seconds */
173 	atf_check_sbttous(3999999, (1ll << 34) -1);
174 	atf_check_sbttous(4000000, 1ll << 34);
175 	/* Overflows (bug 263073) */
176 	atf_check_sbttous(1ll << 31, (1ull << 63) / 1000000);
177 	atf_check_sbttous(1ll << 31, (1ull << 63) / 1000000 + 1);
178 	atf_check_sbttous((1ll << 31) * 1000000, (1ull << 63) - 1);
179 }
180 
181 static void
atf_check_sbttoms(int64_t expected,sbintime_t sbt)182 atf_check_sbttoms(int64_t expected, sbintime_t sbt) {
183 	int64_t actual = sbttoms(sbt);
184 
185 	ATF_CHECK_MSG((expected) - 1 <= (actual) && actual <= (expected) + 1,
186 			"%"PRId64" != sbttoms(%"PRId64") (%"PRId64")",
187 			expected, sbt, actual);
188 }
189 
190 ATF_TC_WITHOUT_HEAD(sbttoms);
ATF_TC_BODY(sbttoms,tc)191 ATF_TC_BODY(sbttoms, tc)
192 {
193 	atf_check_sbttoms(0, 0);
194 	atf_check_sbttoms(0, 1);
195 	atf_check_sbttoms(1, (1ll << 32) / 1000);
196 	/* 1 second */
197 	atf_check_sbttoms(999, (1ll << 32) - 1);
198 	atf_check_sbttoms(1000, 1ll << 32);
199 	/* 2 seconds */
200 	atf_check_sbttoms(1999, (1ll << 33) - 1);
201 	atf_check_sbttoms(2000, 1ll << 33);
202 	/* 4 seconds */
203 	atf_check_sbttoms(3999, (1ll << 34) - 1);
204 	atf_check_sbttoms(4000, 1ll << 34);
205 	/* Overflows (bug 263073) */
206 	atf_check_sbttoms(1ll << 31, (1ull << 63) / 1000);
207 	atf_check_sbttoms(1ll << 31, (1ull << 63) / 1000 + 1);
208 	atf_check_sbttoms((1ll << 31) * 1000, (1ull << 63) - 1);
209 }
210 
ATF_TP_ADD_TCS(tp)211 ATF_TP_ADD_TCS(tp)
212 {
213 
214 	ATF_TP_ADD_TC(tp, nstosbt);
215 	ATF_TP_ADD_TC(tp, ustosbt);
216 	ATF_TP_ADD_TC(tp, mstosbt);
217 	ATF_TP_ADD_TC(tp, sbttons);
218 	ATF_TP_ADD_TC(tp, sbttous);
219 	ATF_TP_ADD_TC(tp, sbttoms);
220 
221 	return (atf_no_error());
222 }
223