xref: /freebsd/tests/sys/sys/time_test.c (revision 38a52bd3)
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  * $FreeBSD$
31  */
32 #include <sys/types.h>
33 #include <sys/time.h>
34 
35 #include <inttypes.h>
36 #include <stdio.h>
37 
38 #include <atf-c.h>
39 
40 
41 static void
42 atf_check_nstosbt(sbintime_t expected, int64_t ns) {
43 	sbintime_t actual = nstosbt(ns);
44 
45 	ATF_CHECK_MSG((expected) - 1 <= (actual) && actual <= (expected) + 1,
46 			"%"PRId64" != nstosbt(%"PRId64") (%"PRId64")",
47 			expected, ns, actual);
48 }
49 
50 ATF_TC_WITHOUT_HEAD(nstosbt);
51 ATF_TC_BODY(nstosbt, tc)
52 {
53 	atf_check_nstosbt(0, 0);
54 	atf_check_nstosbt(4, 1);
55 	/* 1 second */
56 	atf_check_nstosbt((1ll << 32) - 4, 999999999);
57 	atf_check_nstosbt(1ll << 32, 1000000000);
58 	/* 2 seconds https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=263073 */
59 	atf_check_nstosbt((1ll << 33) - 4, 1999999999);
60 	atf_check_nstosbt(1ll << 33, 2000000000);
61 	/* 4 seconds */
62 	atf_check_nstosbt((1ll << 34) - 4, 3999999999);
63 	atf_check_nstosbt((1ll << 34), 4000000000);
64 	/* Max value */
65 	atf_check_nstosbt(((1ll << 31) - 1) << 32,
66 			  ((1ll << 31) - 1) * 1000000000);
67 }
68 
69 static void
70 atf_check_ustosbt(sbintime_t expected, int64_t us) {
71 	sbintime_t actual = ustosbt(us);
72 
73 	ATF_CHECK_MSG((expected) - 1 <= (actual) && actual <= (expected) + 1,
74 			"%"PRId64" != ustosbt(%"PRId64") (%"PRId64")",
75 			expected, us, actual);
76 }
77 
78 ATF_TC_WITHOUT_HEAD(ustosbt);
79 ATF_TC_BODY(ustosbt, tc)
80 {
81 	atf_check_ustosbt(0, 0);
82 	atf_check_ustosbt(4295, 1);
83 	/* 1 second */
84 	atf_check_ustosbt((1ll << 32) - 4295, 999999);
85 	atf_check_ustosbt(1ll << 32, 1000000);
86 	/* 2 seconds https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=263073 */
87 	atf_check_ustosbt((1ll << 33) - 4295, 1999999);
88 	atf_check_ustosbt(1ll << 33, 2000000);
89 	/* 4 seconds */
90 	atf_check_ustosbt((1ll << 34) - 4295, 3999999);
91 	atf_check_ustosbt(1ll << 34, 4000000);
92 	/* Max value */
93 	atf_check_ustosbt(((1ull << 31) - 1) << 32,
94 			  ((1ll << 31) - 1) * 1000000);
95 }
96 
97 static void
98 atf_check_mstosbt(sbintime_t expected, int64_t ms) {
99 	sbintime_t actual = mstosbt(ms);
100 
101 	ATF_CHECK_MSG((expected) - 1 <= (actual) && actual <= (expected) + 1,
102 			"%"PRId64" != mstosbt(%"PRId64") (%"PRId64")",
103 			expected, ms, actual);
104 }
105 
106 ATF_TC_WITHOUT_HEAD(mstosbt);
107 ATF_TC_BODY(mstosbt, tc)
108 {
109 	atf_check_mstosbt(0, 0);
110 	atf_check_mstosbt(4294967, 1);
111 	/* 1 second */
112 	atf_check_mstosbt((1ll << 32) - 4294968, 999);
113 	atf_check_mstosbt(1ll << 32, 1000);
114 	/* 2 seconds https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=263073 */
115 	atf_check_mstosbt((1ll << 33) - 4294968, 1999);
116 	atf_check_mstosbt(1ll << 33, 2000);
117 	/* 4 seconds */
118 	atf_check_mstosbt((1ll << 34) - 4294968, 3999);
119 	atf_check_mstosbt(1ll << 34, 4000);
120 	/* Max value */
121 	atf_check_mstosbt(((1ll << 31) - 1) << 32, ((1ll << 31) - 1) * 1000);
122 }
123 
124 static void
125 atf_check_sbttons(int64_t expected, sbintime_t sbt) {
126 	int64_t actual = sbttons(sbt);
127 
128 	ATF_CHECK_MSG((expected) - 1 <= (actual) && actual <= (expected) + 1,
129 			"%"PRId64" != sbttons(%"PRId64") (%"PRId64")",
130 			expected, sbt, actual);
131 }
132 
133 ATF_TC_WITHOUT_HEAD(sbttons);
134 ATF_TC_BODY(sbttons, tc)
135 {
136 	atf_check_sbttons(0, 0);
137 	atf_check_sbttons(0, 1);
138 	atf_check_sbttons(1, (1ll << 32) / 1000000000);
139 	/* 1 second */
140 	atf_check_sbttons(1000000000, 1ll << 32);
141 	atf_check_sbttons(1999999999, (1ll << 33) - 1);
142 	/* 2 seconds */
143 	atf_check_sbttons(1999999999, (1ll << 33) - 1);
144 	atf_check_sbttons(2000000000, 1ll << 33);
145 	/* 4 seconds */
146 	atf_check_sbttons(3999999999, (1ll << 34) - 1);
147 	atf_check_sbttons(4000000000, 1ll << 34);
148 	/* edge cases */
149 	atf_check_sbttons(999999999, (1ll << 32) - 1);
150 	atf_check_sbttons((1ll << 31) * 1000000000, (1ull << 63) - 1);
151 }
152 
153 static void
154 atf_check_sbttous(int64_t expected, sbintime_t sbt) {
155 	int64_t actual = sbttous(sbt);
156 
157 	ATF_CHECK_MSG((expected) - 1 <= (actual) && actual <= (expected) + 1,
158 			"%"PRId64" != sbttous(%"PRId64") (%"PRId64")",
159 			expected, sbt, actual);
160 }
161 
162 ATF_TC_WITHOUT_HEAD(sbttous);
163 ATF_TC_BODY(sbttous, tc)
164 {
165 	atf_check_sbttous(0, 0);
166 	atf_check_sbttous(0, 1);
167 	atf_check_sbttous(1, (1ll << 32) / 1000000);
168 	/* 1 second */
169 	atf_check_sbttous(1000000, 1ll << 32);
170 	atf_check_sbttous(1999999, (1ll << 33) - 1);
171 	/* 2 seconds */
172 	atf_check_sbttous(1999999, (1ll << 33) - 1);
173 	atf_check_sbttous(2000000, 1ll << 33);
174 	/* 4 seconds */
175 	atf_check_sbttous(3999999, (1ll << 34) -1);
176 	atf_check_sbttous(4000000, 1ll << 34);
177 	/* Overflows (bug 263073) */
178 	atf_check_sbttous(1ll << 31, (1ull << 63) / 1000000);
179 	atf_check_sbttous(1ll << 31, (1ull << 63) / 1000000 + 1);
180 	atf_check_sbttous((1ll << 31) * 1000000, (1ull << 63) - 1);
181 }
182 
183 static void
184 atf_check_sbttoms(int64_t expected, sbintime_t sbt) {
185 	int64_t actual = sbttoms(sbt);
186 
187 	ATF_CHECK_MSG((expected) - 1 <= (actual) && actual <= (expected) + 1,
188 			"%"PRId64" != sbttoms(%"PRId64") (%"PRId64")",
189 			expected, sbt, actual);
190 }
191 
192 ATF_TC_WITHOUT_HEAD(sbttoms);
193 ATF_TC_BODY(sbttoms, tc)
194 {
195 	atf_check_sbttoms(0, 0);
196 	atf_check_sbttoms(0, 1);
197 	atf_check_sbttoms(1, (1ll << 32) / 1000);
198 	/* 1 second */
199 	atf_check_sbttoms(999, (1ll << 32) - 1);
200 	atf_check_sbttoms(1000, 1ll << 32);
201 	/* 2 seconds */
202 	atf_check_sbttoms(1999, (1ll << 33) - 1);
203 	atf_check_sbttoms(2000, 1ll << 33);
204 	/* 4 seconds */
205 	atf_check_sbttoms(3999, (1ll << 34) - 1);
206 	atf_check_sbttoms(4000, 1ll << 34);
207 	/* Overflows (bug 263073) */
208 	atf_check_sbttoms(1ll << 31, (1ull << 63) / 1000);
209 	atf_check_sbttoms(1ll << 31, (1ull << 63) / 1000 + 1);
210 	atf_check_sbttoms((1ll << 31) * 1000, (1ull << 63) - 1);
211 }
212 
213 ATF_TP_ADD_TCS(tp)
214 {
215 
216 	ATF_TP_ADD_TC(tp, nstosbt);
217 	ATF_TP_ADD_TC(tp, ustosbt);
218 	ATF_TP_ADD_TC(tp, mstosbt);
219 	ATF_TP_ADD_TC(tp, sbttons);
220 	ATF_TP_ADD_TC(tp, sbttous);
221 	ATF_TP_ADD_TC(tp, sbttoms);
222 
223 	return (atf_no_error());
224 }
225