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.
32 #include <sys/types.h>
42 atf_check_nstosbt(sbintime_t expected, int64_t ns) {
43 sbintime_t actual = nstosbt(ns);
45 ATF_CHECK_MSG((expected) - 1 <= (actual) && actual <= (expected) + 1,
46 "%"PRId64" != nstosbt(%"PRId64") (%"PRId64")",
47 expected, ns, actual);
50 ATF_TC_WITHOUT_HEAD(nstosbt);
51 ATF_TC_BODY(nstosbt, tc)
53 atf_check_nstosbt(0, 0);
54 atf_check_nstosbt(4, 1);
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);
62 atf_check_nstosbt((1ll << 34) - 4, 3999999999);
63 atf_check_nstosbt((1ll << 34), 4000000000);
65 atf_check_nstosbt(((1ll << 31) - 1) << 32,
66 ((1ll << 31) - 1) * 1000000000);
70 atf_check_ustosbt(sbintime_t expected, int64_t us) {
71 sbintime_t actual = ustosbt(us);
73 ATF_CHECK_MSG((expected) - 1 <= (actual) && actual <= (expected) + 1,
74 "%"PRId64" != ustosbt(%"PRId64") (%"PRId64")",
75 expected, us, actual);
78 ATF_TC_WITHOUT_HEAD(ustosbt);
79 ATF_TC_BODY(ustosbt, tc)
81 atf_check_ustosbt(0, 0);
82 atf_check_ustosbt(4295, 1);
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);
90 atf_check_ustosbt((1ll << 34) - 4295, 3999999);
91 atf_check_ustosbt(1ll << 34, 4000000);
93 atf_check_ustosbt(((1ull << 31) - 1) << 32,
94 ((1ll << 31) - 1) * 1000000);
98 atf_check_mstosbt(sbintime_t expected, int64_t ms) {
99 sbintime_t actual = mstosbt(ms);
101 ATF_CHECK_MSG((expected) - 1 <= (actual) && actual <= (expected) + 1,
102 "%"PRId64" != mstosbt(%"PRId64") (%"PRId64")",
103 expected, ms, actual);
106 ATF_TC_WITHOUT_HEAD(mstosbt);
107 ATF_TC_BODY(mstosbt, tc)
109 atf_check_mstosbt(0, 0);
110 atf_check_mstosbt(4294967, 1);
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);
118 atf_check_mstosbt((1ll << 34) - 4294968, 3999);
119 atf_check_mstosbt(1ll << 34, 4000);
121 atf_check_mstosbt(((1ll << 31) - 1) << 32, ((1ll << 31) - 1) * 1000);
125 atf_check_sbttons(int64_t expected, sbintime_t sbt) {
126 int64_t actual = sbttons(sbt);
128 ATF_CHECK_MSG((expected) - 1 <= (actual) && actual <= (expected) + 1,
129 "%"PRId64" != sbttons(%"PRId64") (%"PRId64")",
130 expected, sbt, actual);
133 ATF_TC_WITHOUT_HEAD(sbttons);
134 ATF_TC_BODY(sbttons, tc)
136 atf_check_sbttons(0, 0);
137 atf_check_sbttons(0, 1);
138 atf_check_sbttons(1, (1ll << 32) / 1000000000);
140 atf_check_sbttons(1000000000, 1ll << 32);
141 atf_check_sbttons(1999999999, (1ll << 33) - 1);
143 atf_check_sbttons(1999999999, (1ll << 33) - 1);
144 atf_check_sbttons(2000000000, 1ll << 33);
146 atf_check_sbttons(3999999999, (1ll << 34) - 1);
147 atf_check_sbttons(4000000000, 1ll << 34);
149 atf_check_sbttons(999999999, (1ll << 32) - 1);
150 atf_check_sbttons((1ll << 31) * 1000000000, (1ull << 63) - 1);
154 atf_check_sbttous(int64_t expected, sbintime_t sbt) {
155 int64_t actual = sbttous(sbt);
157 ATF_CHECK_MSG((expected) - 1 <= (actual) && actual <= (expected) + 1,
158 "%"PRId64" != sbttous(%"PRId64") (%"PRId64")",
159 expected, sbt, actual);
162 ATF_TC_WITHOUT_HEAD(sbttous);
163 ATF_TC_BODY(sbttous, tc)
165 atf_check_sbttous(0, 0);
166 atf_check_sbttous(0, 1);
167 atf_check_sbttous(1, (1ll << 32) / 1000000);
169 atf_check_sbttous(1000000, 1ll << 32);
170 atf_check_sbttous(1999999, (1ll << 33) - 1);
172 atf_check_sbttous(1999999, (1ll << 33) - 1);
173 atf_check_sbttous(2000000, 1ll << 33);
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);
184 atf_check_sbttoms(int64_t expected, sbintime_t sbt) {
185 int64_t actual = sbttoms(sbt);
187 ATF_CHECK_MSG((expected) - 1 <= (actual) && actual <= (expected) + 1,
188 "%"PRId64" != sbttoms(%"PRId64") (%"PRId64")",
189 expected, sbt, actual);
192 ATF_TC_WITHOUT_HEAD(sbttoms);
193 ATF_TC_BODY(sbttoms, tc)
195 atf_check_sbttoms(0, 0);
196 atf_check_sbttoms(0, 1);
197 atf_check_sbttoms(1, (1ll << 32) / 1000);
199 atf_check_sbttoms(999, (1ll << 32) - 1);
200 atf_check_sbttoms(1000, 1ll << 32);
202 atf_check_sbttoms(1999, (1ll << 33) - 1);
203 atf_check_sbttoms(2000, 1ll << 33);
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);
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);
223 return (atf_no_error());