2 * Copyright (c) 2022 Axcient
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
30 #include <sys/types.h>
40 atf_check_nstosbt(sbintime_t expected, int64_t ns) {
41 sbintime_t actual = nstosbt(ns);
43 ATF_CHECK_MSG((expected) - 1 <= (actual) && actual <= (expected) + 1,
44 "%"PRId64" != nstosbt(%"PRId64") (%"PRId64")",
45 expected, ns, actual);
48 ATF_TC_WITHOUT_HEAD(nstosbt);
49 ATF_TC_BODY(nstosbt, tc)
51 atf_check_nstosbt(0, 0);
52 atf_check_nstosbt(4, 1);
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);
60 atf_check_nstosbt((1ll << 34) - 4, 3999999999);
61 atf_check_nstosbt((1ll << 34), 4000000000);
63 atf_check_nstosbt(((1ll << 31) - 1) << 32,
64 ((1ll << 31) - 1) * 1000000000);
68 atf_check_ustosbt(sbintime_t expected, int64_t us) {
69 sbintime_t actual = ustosbt(us);
71 ATF_CHECK_MSG((expected) - 1 <= (actual) && actual <= (expected) + 1,
72 "%"PRId64" != ustosbt(%"PRId64") (%"PRId64")",
73 expected, us, actual);
76 ATF_TC_WITHOUT_HEAD(ustosbt);
77 ATF_TC_BODY(ustosbt, tc)
79 atf_check_ustosbt(0, 0);
80 atf_check_ustosbt(4295, 1);
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);
88 atf_check_ustosbt((1ll << 34) - 4295, 3999999);
89 atf_check_ustosbt(1ll << 34, 4000000);
91 atf_check_ustosbt(((1ull << 31) - 1) << 32,
92 ((1ll << 31) - 1) * 1000000);
96 atf_check_mstosbt(sbintime_t expected, int64_t ms) {
97 sbintime_t actual = mstosbt(ms);
99 ATF_CHECK_MSG((expected) - 1 <= (actual) && actual <= (expected) + 1,
100 "%"PRId64" != mstosbt(%"PRId64") (%"PRId64")",
101 expected, ms, actual);
104 ATF_TC_WITHOUT_HEAD(mstosbt);
105 ATF_TC_BODY(mstosbt, tc)
107 atf_check_mstosbt(0, 0);
108 atf_check_mstosbt(4294967, 1);
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);
116 atf_check_mstosbt((1ll << 34) - 4294968, 3999);
117 atf_check_mstosbt(1ll << 34, 4000);
119 atf_check_mstosbt(((1ll << 31) - 1) << 32, ((1ll << 31) - 1) * 1000);
123 atf_check_sbttons(int64_t expected, sbintime_t sbt) {
124 int64_t actual = sbttons(sbt);
126 ATF_CHECK_MSG((expected) - 1 <= (actual) && actual <= (expected) + 1,
127 "%"PRId64" != sbttons(%"PRId64") (%"PRId64")",
128 expected, sbt, actual);
131 ATF_TC_WITHOUT_HEAD(sbttons);
132 ATF_TC_BODY(sbttons, tc)
134 atf_check_sbttons(0, 0);
135 atf_check_sbttons(0, 1);
136 atf_check_sbttons(1, (1ll << 32) / 1000000000);
138 atf_check_sbttons(1000000000, 1ll << 32);
139 atf_check_sbttons(1999999999, (1ll << 33) - 1);
141 atf_check_sbttons(1999999999, (1ll << 33) - 1);
142 atf_check_sbttons(2000000000, 1ll << 33);
144 atf_check_sbttons(3999999999, (1ll << 34) - 1);
145 atf_check_sbttons(4000000000, 1ll << 34);
147 atf_check_sbttons(999999999, (1ll << 32) - 1);
148 atf_check_sbttons((1ll << 31) * 1000000000, (1ull << 63) - 1);
152 atf_check_sbttous(int64_t expected, sbintime_t sbt) {
153 int64_t actual = sbttous(sbt);
155 ATF_CHECK_MSG((expected) - 1 <= (actual) && actual <= (expected) + 1,
156 "%"PRId64" != sbttous(%"PRId64") (%"PRId64")",
157 expected, sbt, actual);
160 ATF_TC_WITHOUT_HEAD(sbttous);
161 ATF_TC_BODY(sbttous, tc)
163 atf_check_sbttous(0, 0);
164 atf_check_sbttous(0, 1);
165 atf_check_sbttous(1, (1ll << 32) / 1000000);
167 atf_check_sbttous(1000000, 1ll << 32);
168 atf_check_sbttous(1999999, (1ll << 33) - 1);
170 atf_check_sbttous(1999999, (1ll << 33) - 1);
171 atf_check_sbttous(2000000, 1ll << 33);
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);
182 atf_check_sbttoms(int64_t expected, sbintime_t sbt) {
183 int64_t actual = sbttoms(sbt);
185 ATF_CHECK_MSG((expected) - 1 <= (actual) && actual <= (expected) + 1,
186 "%"PRId64" != sbttoms(%"PRId64") (%"PRId64")",
187 expected, sbt, actual);
190 ATF_TC_WITHOUT_HEAD(sbttoms);
191 ATF_TC_BODY(sbttoms, tc)
193 atf_check_sbttoms(0, 0);
194 atf_check_sbttoms(0, 1);
195 atf_check_sbttoms(1, (1ll << 32) / 1000);
197 atf_check_sbttoms(999, (1ll << 32) - 1);
198 atf_check_sbttoms(1000, 1ll << 32);
200 atf_check_sbttoms(1999, (1ll << 33) - 1);
201 atf_check_sbttoms(2000, 1ll << 33);
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);
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);
221 return (atf_no_error());