1 /*-
2 * Copyright (c) 2023 Dag-Erling Smørgrav
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7 #include <assert.h>
8 #include <limits.h>
9 #include <locale.h>
10 #include <stdint.h>
11 #include <stdio.h>
12
13 #include <atf-c.h>
14
15 #ifndef nitems
16 #define nitems(a) (sizeof(a) / sizeof(a[0]))
17 #endif
18
19 #define SNPRINTF_TEST(output, format, ...) \
20 do { \
21 char buf[256]; \
22 assert(strlen(output) < nitems(buf)); \
23 int ret = snprintf(buf, nitems(buf), format, \
24 __VA_ARGS__); \
25 ATF_CHECK_EQ(strlen(output), ret); \
26 if (ret > 0) { \
27 ATF_CHECK_EQ(0, strcmp(output, buf)); \
28 } \
29 } while (0)
30
31 ATF_TC_WITHOUT_HEAD(snprintf_b);
ATF_TC_BODY(snprintf_b,tc)32 ATF_TC_BODY(snprintf_b, tc)
33 {
34 SNPRINTF_TEST("0", "%b", 0);
35 SNPRINTF_TEST(" 0", "%12b", 0);
36 SNPRINTF_TEST("000000000000", "%012b", 0);
37 SNPRINTF_TEST("1", "%b", 1);
38 SNPRINTF_TEST(" 1", "%12b", 1);
39 SNPRINTF_TEST("000000000001", "%012b", 1);
40 SNPRINTF_TEST("1111111111111111111111111111111", "%b", INT_MAX);
41 SNPRINTF_TEST("0", "%#b", 0);
42 SNPRINTF_TEST(" 0", "%#12b", 0);
43 SNPRINTF_TEST("000000000000", "%#012b", 0);
44 SNPRINTF_TEST("0b1", "%#b", 1);
45 SNPRINTF_TEST(" 0b1", "%#12b", 1);
46 SNPRINTF_TEST("0b0000000001", "%#012b", 1);
47 SNPRINTF_TEST("0b1111111111111111111111111111111", "%#b", INT_MAX);
48 }
49
50 ATF_TC_WITHOUT_HEAD(snprintf_B);
ATF_TC_BODY(snprintf_B,tc)51 ATF_TC_BODY(snprintf_B, tc)
52 {
53 SNPRINTF_TEST("0", "%B", 0);
54 SNPRINTF_TEST(" 0", "%12B", 0);
55 SNPRINTF_TEST("000000000000", "%012B", 0);
56 SNPRINTF_TEST("1", "%B", 1);
57 SNPRINTF_TEST(" 1", "%12B", 1);
58 SNPRINTF_TEST("000000000001", "%012B", 1);
59 SNPRINTF_TEST("1111111111111111111111111111111", "%B", INT_MAX);
60 SNPRINTF_TEST("0", "%#B", 0);
61 SNPRINTF_TEST(" 0", "%#12B", 0);
62 SNPRINTF_TEST("000000000000", "%#012B", 0);
63 SNPRINTF_TEST("0B1", "%#B", 1);
64 SNPRINTF_TEST(" 0B1", "%#12B", 1);
65 SNPRINTF_TEST("0B0000000001", "%#012B", 1);
66 SNPRINTF_TEST("0B1111111111111111111111111111111", "%#B", INT_MAX);
67 }
68
69 ATF_TC_WITHOUT_HEAD(snprintf_d);
ATF_TC_BODY(snprintf_d,tc)70 ATF_TC_BODY(snprintf_d, tc)
71 {
72 SNPRINTF_TEST("0", "%d", 0);
73 SNPRINTF_TEST(" 0", "%12d", 0);
74 SNPRINTF_TEST("000000000000", "%012d", 0);
75 SNPRINTF_TEST("1", "%d", 1);
76 SNPRINTF_TEST(" 1", "%12d", 1);
77 SNPRINTF_TEST("000000000001", "%012d", 1);
78 SNPRINTF_TEST("2147483647", "%d", INT_MAX);
79 SNPRINTF_TEST(" 2147483647", "%12d", INT_MAX);
80 SNPRINTF_TEST("002147483647", "%012d", INT_MAX);
81 SNPRINTF_TEST("2,147,483,647", "%'d", INT_MAX);
82 }
83
84 ATF_TC_WITHOUT_HEAD(snprintf_x);
ATF_TC_BODY(snprintf_x,tc)85 ATF_TC_BODY(snprintf_x, tc)
86 {
87 SNPRINTF_TEST("0", "%x", 0);
88 SNPRINTF_TEST(" 0", "%12x", 0);
89 SNPRINTF_TEST("000000000000", "%012x", 0);
90 SNPRINTF_TEST("1", "%x", 1);
91 SNPRINTF_TEST(" 1", "%12x", 1);
92 SNPRINTF_TEST("000000000001", "%012x", 1);
93 SNPRINTF_TEST("7fffffff", "%x", INT_MAX);
94 SNPRINTF_TEST(" 7fffffff", "%12x", INT_MAX);
95 SNPRINTF_TEST("00007fffffff", "%012x", INT_MAX);
96 SNPRINTF_TEST("0", "%#x", 0);
97 SNPRINTF_TEST(" 0", "%#12x", 0);
98 SNPRINTF_TEST("000000000000", "%#012x", 0);
99 SNPRINTF_TEST("0x1", "%#x", 1);
100 SNPRINTF_TEST(" 0x1", "%#12x", 1);
101 SNPRINTF_TEST("0x0000000001", "%#012x", 1);
102 SNPRINTF_TEST("0x7fffffff", "%#x", INT_MAX);
103 SNPRINTF_TEST(" 0x7fffffff", "%#12x", INT_MAX);
104 SNPRINTF_TEST("0x007fffffff", "%#012x", INT_MAX);
105 }
106
107 ATF_TC_WITHOUT_HEAD(snprintf_X);
ATF_TC_BODY(snprintf_X,tc)108 ATF_TC_BODY(snprintf_X, tc)
109 {
110 SNPRINTF_TEST("0", "%X", 0);
111 SNPRINTF_TEST(" 0", "%12X", 0);
112 SNPRINTF_TEST("000000000000", "%012X", 0);
113 SNPRINTF_TEST("1", "%X", 1);
114 SNPRINTF_TEST(" 1", "%12X", 1);
115 SNPRINTF_TEST("000000000001", "%012X", 1);
116 SNPRINTF_TEST("7FFFFFFF", "%X", INT_MAX);
117 SNPRINTF_TEST(" 7FFFFFFF", "%12X", INT_MAX);
118 SNPRINTF_TEST("00007FFFFFFF", "%012X", INT_MAX);
119 SNPRINTF_TEST("0", "%#X", 0);
120 SNPRINTF_TEST(" 0", "%#12X", 0);
121 SNPRINTF_TEST("000000000000", "%#012X", 0);
122 SNPRINTF_TEST("0X1", "%#X", 1);
123 SNPRINTF_TEST(" 0X1", "%#12X", 1);
124 SNPRINTF_TEST("0X0000000001", "%#012X", 1);
125 SNPRINTF_TEST("0X7FFFFFFF", "%#X", INT_MAX);
126 SNPRINTF_TEST(" 0X7FFFFFFF", "%#12X", INT_MAX);
127 SNPRINTF_TEST("0X007FFFFFFF", "%#012X", INT_MAX);
128 }
129
130 ATF_TC_WITHOUT_HEAD(snprintf_wN);
ATF_TC_BODY(snprintf_wN,tc)131 ATF_TC_BODY(snprintf_wN, tc)
132 {
133 SNPRINTF_TEST("0", "%w8d", (int8_t)0);
134 SNPRINTF_TEST("-128", "%w8d", (int8_t)SCHAR_MIN);
135 SNPRINTF_TEST("127", "%w8d", (int8_t)SCHAR_MAX);
136 SNPRINTF_TEST("0", "%w8u", (uint8_t)0);
137 SNPRINTF_TEST("255", "%w8u", (uint8_t)UCHAR_MAX);
138
139 SNPRINTF_TEST("0", "%w16d", (int16_t)0);
140 SNPRINTF_TEST("-32768", "%w16d", (int16_t)SHRT_MIN);
141 SNPRINTF_TEST("32767", "%w16d", (int16_t)SHRT_MAX);
142 SNPRINTF_TEST("0", "%w16u", (uint16_t)0);
143 SNPRINTF_TEST("65535", "%w16u", (uint16_t)USHRT_MAX);
144
145 SNPRINTF_TEST("0", "%w32d", (int32_t)0);
146 SNPRINTF_TEST("-2147483648", "%w32d", (int32_t)INT_MIN);
147 SNPRINTF_TEST("2147483647", "%w32d", (int32_t)INT_MAX);
148 SNPRINTF_TEST("0", "%w32u", (uint32_t)0);
149 SNPRINTF_TEST("4294967295", "%w32u", (uint32_t)UINT_MAX);
150
151 SNPRINTF_TEST("0", "%w64d", (int64_t)0);
152 SNPRINTF_TEST("-9223372036854775808", "%w64d", (int64_t)LLONG_MIN);
153 SNPRINTF_TEST("9223372036854775807", "%w64d", (int64_t)LLONG_MAX);
154 SNPRINTF_TEST("0", "%w64u", (uint64_t)0);
155 SNPRINTF_TEST("18446744073709551615", "%w64u", (uint64_t)ULLONG_MAX);
156
157 SNPRINTF_TEST("wd", "%wd", 0);
158 SNPRINTF_TEST("w1d", "%w1d", 0);
159 SNPRINTF_TEST("w128d", "%w128d", 0);
160 }
161
162 ATF_TC_WITHOUT_HEAD(snprintf_wfN);
ATF_TC_BODY(snprintf_wfN,tc)163 ATF_TC_BODY(snprintf_wfN, tc)
164 {
165 SNPRINTF_TEST("0", "%wf8d", (int_fast8_t)0);
166 SNPRINTF_TEST("-2147483648", "%wf8d", (int_fast8_t)INT_MIN);
167 SNPRINTF_TEST("2147483647", "%wf8d", (int_fast8_t)INT_MAX);
168 SNPRINTF_TEST("0", "%wf8u", (uint8_t)0);
169 SNPRINTF_TEST("4294967295", "%wf8u", (uint_fast8_t)UINT_MAX);
170
171 SNPRINTF_TEST("0", "%wf16d", (int_fast16_t)0);
172 SNPRINTF_TEST("-2147483648", "%wf16d", (int_fast16_t)INT_MIN);
173 SNPRINTF_TEST("2147483647", "%wf16d", (int_fast16_t)INT_MAX);
174 SNPRINTF_TEST("0", "%wf16u", (uint16_t)0);
175 SNPRINTF_TEST("4294967295", "%wf16u", (uint_fast16_t)UINT_MAX);
176
177 SNPRINTF_TEST("0", "%wf32d", (int_fast32_t)0);
178 SNPRINTF_TEST("-2147483648", "%wf32d", (int_fast32_t)INT_MIN);
179 SNPRINTF_TEST("2147483647", "%wf32d", (int_fast32_t)INT_MAX);
180 SNPRINTF_TEST("0", "%wf32u", (uint32_t)0);
181 SNPRINTF_TEST("4294967295", "%wf32u", (uint_fast32_t)UINT_MAX);
182
183 SNPRINTF_TEST("0", "%wf64d", (int_fast64_t)0);
184 SNPRINTF_TEST("-9223372036854775808", "%wf64d", (int_fast64_t)LLONG_MIN);
185 SNPRINTF_TEST("9223372036854775807", "%wf64d", (int_fast64_t)LLONG_MAX);
186 SNPRINTF_TEST("0", "%wf64u", (uint64_t)0);
187 SNPRINTF_TEST("18446744073709551615", "%wf64u", (uint_fast64_t)ULLONG_MAX);
188
189 SNPRINTF_TEST("wfd", "%wfd", 0);
190 SNPRINTF_TEST("wf1d", "%wf1d", 0);
191 SNPRINTF_TEST("wf128d", "%wf128d", 0);
192 }
193
ATF_TP_ADD_TCS(tp)194 ATF_TP_ADD_TCS(tp)
195 {
196 setlocale(LC_NUMERIC, "en_US.UTF-8");
197 ATF_TP_ADD_TC(tp, snprintf_b);
198 ATF_TP_ADD_TC(tp, snprintf_B);
199 ATF_TP_ADD_TC(tp, snprintf_d);
200 ATF_TP_ADD_TC(tp, snprintf_x);
201 ATF_TP_ADD_TC(tp, snprintf_X);
202 ATF_TP_ADD_TC(tp, snprintf_wN);
203 ATF_TP_ADD_TC(tp, snprintf_wfN);
204 return (atf_no_error());
205 }
206