4 * The canonical version of this file is maintained in the rra-c-util package,
5 * which can be found at <https://www.eyrie.org/~eagle/software/rra-c-util/>.
7 * Written by Russ Allbery <eagle@eyrie.org>
8 * Copyright 2000-2006, 2018-2020 Russ Allbery <eagle@eyrie.org>
10 * The Board of Trustees of the Leland Stanford Junior University
11 * Copyright 1995 Patrick Powell
12 * Copyright 2001 Hrvoje Niksic
14 * This code is based on code written by Patrick Powell (papowell@astart.com)
15 * It may be used for any purpose as long as this notice remains intact
16 * on all source code distributions
18 * There is no SPDX-License-Identifier registered for this license.
22 #include <portable/system.h>
24 #include <tests/tap/basic.h>
27 * Disable the requirement that format strings be literals. We need variable
28 * formats for easy testing.
30 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 2) || defined(__clang__)
31 # pragma GCC diagnostic ignored "-Wformat-nonliteral"
35 * Intentionally don't add the printf attribute here since we pass a
36 * zero-length printf format during testing and don't want warnings.
38 int test_snprintf(char *str, size_t count, const char *fmt, ...);
39 int test_vsnprintf(char *str, size_t count, const char *fmt, va_list args);
41 static const char string[] = "abcdefghijklmnopqrstuvwxyz0123456789";
43 /* clang-format off */
45 static const char *const fp_formats[] = {
46 "%-1.5f", "%1.5f", "%31.6f", "%10.5f", "% 10.5f", "%+22.6f",
47 "%+4.6f", "%01.3f", "%3.1f", "%3.2f", "%.0f", "%.1f",
51 static const char *const int_formats[] = {
52 "%-1.5d", "%1.5d", "%31.9d", "%5.5d", "%10.5d", "% 10.5d",
53 "%+22.30d", "%01.3d", "%4d", "%d", "%ld", NULL
55 static const char *const uint_formats[] = {
56 "%-1.5lu", "%1.5lu", "%31.9lu", "%5.5lu", "%10.5lu", "% 10.5lu",
57 "%+6.30lu", "%01.3lu", "%4lu", "%lu", "%4lx", "%4lX",
58 "%01.3lx", "%1lo", NULL
60 static const char *const llong_formats[] = {
61 "%lld", "%-1.5lld", "%1.5lld", "%123.9lld", "%5.5lld",
62 "%10.5lld", "% 10.5lld", "%+22.33lld", "%01.3lld", "%4lld",
65 static const char *const ullong_formats[] = {
66 "%llu", "%-1.5llu", "%1.5llu", "%123.9llu", "%5.5llu",
67 "%10.5llu", "% 10.5llu", "%+22.33llu", "%01.3llu", "%4llu",
71 static const double fp_nums[] = {
72 -1.5, 134.21, 91340.2, 341.1234, 0203.9, 0.96, 0.996, 0.9996, 1.996,
73 4.136, 0.1, 0.01, 0.001, 10.1, 0
75 static long int_nums[] = {
76 -1, 134, 91340, 341, 0203, 0
78 static unsigned long uint_nums[] = {
79 (unsigned long) -1, 134, 91340, 341, 0203, 0
81 static long long llong_nums[] = {
82 ~(long long) 0, /* All-1 bit pattern. */
83 (~(unsigned long long) 0) >> 1, /* Largest signed long long. */
84 -150, 134, 91340, 341,
87 static unsigned long long ullong_nums[] = {
88 ~(unsigned long long) 0, /* All-1 bit pattern. */
89 (~(unsigned long long) 0) >> 1, /* Largest signed long long. */
98 test_format(bool trunc, const char *expected, int count, const char *format,
105 va_start(args, format);
106 result = test_vsnprintf(buf, trunc ? 32 : sizeof(buf), format, args);
108 is_string(expected, buf, "format %s, wanted %s", format, expected);
109 is_int(count, result, "...and output length correct");
122 + (18 + (ARRAY_SIZE(fp_formats) - 1) * ARRAY_SIZE(fp_nums)
123 + (ARRAY_SIZE(int_formats) - 1) * ARRAY_SIZE(int_nums)
124 + (ARRAY_SIZE(uint_formats) - 1) * ARRAY_SIZE(uint_nums)
125 + (ARRAY_SIZE(llong_formats) - 1) * ARRAY_SIZE(llong_nums)
126 + (ARRAY_SIZE(ullong_formats) - 1) * ARRAY_SIZE(ullong_nums))
129 is_int(4, test_snprintf(NULL, 0, "%s", "abcd"), "simple string length");
130 is_int(2, test_snprintf(NULL, 0, "%d", 20), "number length");
131 is_int(7, test_snprintf(NULL, 0, "Test %.2s", "abcd"), "limited string");
132 is_int(1, test_snprintf(NULL, 0, "%c", 'a'), "character length");
133 is_int(0, test_snprintf(NULL, 0, ""), "empty format length");
135 test_format(true, "abcd", 4, "%s", "abcd");
136 test_format(true, "20", 2, "%d", 20);
137 test_format(true, "Test ab", 7, "Test %.2s", "abcd");
138 test_format(true, "a", 1, "%c", 'a');
139 test_format(true, "", 0, "");
140 test_format(true, "abcdefghijklmnopqrstuvwxyz01234", 36, "%s", string);
141 test_format(true, "abcdefghij", 10, "%.10s", string);
142 test_format(true, " abcdefghij", 12, "%12.10s", string);
143 test_format(true, " abcdefghijklmnopqrstuvwxyz0", 40, "%40s", string);
144 test_format(true, "abcdefghij ", 14, "%-14.10s", string);
145 test_format(true, " abcdefghijklmnopq", 50, "%50s", string);
146 test_format(true, "%abcd%", 6, "%%%0s%%", "abcd");
147 test_format(true, "", 0, "%.0s", string);
148 test_format(true, "abcdefghijklmnopqrstuvwxyz 444", 32, "%.26s %d",
150 test_format(true, "abcdefghijklmnopqrstuvwxyz -2.", 32, "%.26s %.1f",
152 test_format(true, "abcdefghij4444", 14, "%.10s%n%d", string, &count, 4444);
153 is_int(10, count, "correct output from %%n");
154 test_format(true, "abcdefghijklmnopqrstuvwxyz01234", 36, "%n%s%ln", &count,
156 is_int(0, count, "correct output from two %%n");
157 is_int(31, lcount, "correct output from long %%ln");
158 test_format(true, "(null)", 6, "%s", (char *) NULL);
160 for (i = 0; fp_formats[i] != NULL; i++)
161 for (j = 0; j < ARRAY_SIZE(fp_nums); j++) {
162 count = sprintf(lgbuf, fp_formats[i], fp_nums[j]);
163 test_format(false, lgbuf, count, fp_formats[i], fp_nums[j]);
165 for (i = 0; int_formats[i] != NULL; i++)
166 for (j = 0; j < ARRAY_SIZE(int_nums); j++) {
167 count = sprintf(lgbuf, int_formats[i], int_nums[j]);
168 test_format(false, lgbuf, count, int_formats[i], int_nums[j]);
170 for (i = 0; uint_formats[i] != NULL; i++)
171 for (j = 0; j < ARRAY_SIZE(uint_nums); j++) {
172 count = sprintf(lgbuf, uint_formats[i], uint_nums[j]);
173 test_format(false, lgbuf, count, uint_formats[i], uint_nums[j]);
175 for (i = 0; llong_formats[i] != NULL; i++)
176 for (j = 0; j < ARRAY_SIZE(llong_nums); j++) {
177 count = sprintf(lgbuf, llong_formats[i], llong_nums[j]);
178 test_format(false, lgbuf, count, llong_formats[i], llong_nums[j]);
180 for (i = 0; ullong_formats[i] != NULL; i++)
181 for (j = 0; j < ARRAY_SIZE(ullong_nums); j++) {
182 count = sprintf(lgbuf, ullong_formats[i], ullong_nums[j]);
183 test_format(false, lgbuf, count, ullong_formats[i],