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