xref: /netbsd-src/tests/lib/libc/stdio/t_printf.c (revision f4748aaa01faf324805f9747191535eb6600f82c)
1 /* $NetBSD: t_printf.c,v 1.10 2023/04/04 19:39:38 he Exp $ */
2 
3 /*-
4  * Copyright (c) 2010 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/types.h>
30 #include <sys/resource.h>
31 #include <atf-c.h>
32 #include <math.h>
33 #include <stdio.h>
34 #include <stdint.h>
35 #include <string.h>
36 #include <time.h>
37 #include <stdlib.h>
38 #include <errno.h>
39 
40 ATF_TC(snprintf_c99);
41 ATF_TC_HEAD(snprintf_c99, tc)
42 {
43 
44 	atf_tc_set_md_var(tc, "descr",
45 	    "Test printf(3) C99 conformance (PR lib/22019)");
46 }
47 
48 ATF_TC_BODY(snprintf_c99, tc)
49 {
50 	char s[4];
51 
52 	(void)memset(s, '\0', sizeof(s));
53 	(void)snprintf(s, sizeof(s), "%#.o", 0);
54 	(void)printf("printf = %#.o\n", 0);
55 	(void)fprintf(stderr, "snprintf = %s", s);
56 
57 	ATF_REQUIRE(strlen(s) == 1);
58 	ATF_REQUIRE(s[0] == '0');
59 }
60 
61 ATF_TC(snprintf_dotzero);
62 ATF_TC_HEAD(snprintf_dotzero, tc)
63 {
64 
65 	atf_tc_set_md_var(tc, "descr",
66 	    "PR lib/32951: %%.0f formats (0.0,0.5] to \"0.\"");
67 }
68 
69 ATF_TC_BODY(snprintf_dotzero, tc)
70 {
71 	char s[4];
72 
73 	ATF_CHECK(snprintf(s, sizeof(s), "%.0f", 0.1) == 1);
74 	ATF_REQUIRE_STREQ(s, "0");
75 }
76 
77 ATF_TC(snprintf_posarg);
78 ATF_TC_HEAD(snprintf_posarg, tc)
79 {
80 
81 	atf_tc_set_md_var(tc, "descr", "test for positional arguments");
82 }
83 
84 ATF_TC_BODY(snprintf_posarg, tc)
85 {
86 	char s[16];
87 
88 	ATF_CHECK(snprintf(s, sizeof(s), "%1$d", -23) == 3);
89 	ATF_REQUIRE_STREQ(s, "-23");
90 }
91 
92 ATF_TC(snprintf_posarg_width);
93 ATF_TC_HEAD(snprintf_posarg_width, tc)
94 {
95 
96 	atf_tc_set_md_var(tc, "descr", "test for positional arguments with "
97 	    "field width");
98 }
99 
100 ATF_TC_BODY(snprintf_posarg_width, tc)
101 {
102 	char s[16];
103 
104 	ATF_CHECK(snprintf(s, sizeof(s), "%1$*2$d", -23, 4) == 4);
105 	ATF_REQUIRE_STREQ(s, " -23");
106 }
107 
108 ATF_TC(snprintf_posarg_error);
109 ATF_TC_HEAD(snprintf_posarg_error, tc)
110 {
111 
112 	atf_tc_set_md_var(tc, "descr", "test for positional arguments out "
113 	    "of bounds");
114 }
115 
116 ATF_TC_BODY(snprintf_posarg_error, tc)
117 {
118 	char s[16], fmt[32];
119 
120 	snprintf(fmt, sizeof(fmt), "%%%zu$d", SIZE_MAX / sizeof(size_t));
121 
122 	ATF_CHECK(snprintf(s, sizeof(s), fmt, -23) == -1);
123 }
124 
125 ATF_TC(snprintf_float);
126 ATF_TC_HEAD(snprintf_float, tc)
127 {
128 
129 	atf_tc_set_md_var(tc, "descr", "test that floating conversions don't"
130 	    " leak memory");
131 }
132 
133 ATF_TC_BODY(snprintf_float, tc)
134 {
135 	union {
136 		double d;
137 		uint64_t bits;
138 	} u;
139 	uint32_t ul, uh;
140 	time_t now;
141 	char buf[1000];
142 	struct rlimit rl;
143 
144 	rl.rlim_cur = rl.rlim_max = 1 * 1024 * 1024;
145 	ATF_CHECK(setrlimit(RLIMIT_AS, &rl) != -1);
146 	rl.rlim_cur = rl.rlim_max = 1 * 1024 * 1024;
147 	ATF_CHECK(setrlimit(RLIMIT_DATA, &rl) != -1);
148 
149 	time(&now);
150 	srand(now);
151 	for (size_t i = 0; i < 10000; i++) {
152 		ul = rand();
153 		uh = rand();
154 		u.bits = (uint64_t)uh << 32 | ul;
155 		ATF_CHECK(snprintf(buf, sizeof buf, " %.2f", u.d) != -1);
156 	}
157 }
158 
159 ATF_TC(sprintf_zeropad);
160 ATF_TC_HEAD(sprintf_zeropad, tc)
161 {
162 	atf_tc_set_md_var(tc, "descr",
163 	    "Test output format zero padding (PR lib/44113)");
164 }
165 
166 ATF_TC_BODY(sprintf_zeropad, tc)
167 {
168 	char str[1024];
169 
170 	ATF_CHECK(sprintf(str, "%010f", 0.0) == 10);
171 	ATF_REQUIRE_STREQ(str, "000.000000");
172 
173 	/* ieeefp */
174 #ifndef __vax__
175 	/* printf(3) should ignore zero padding for nan/inf */
176 	ATF_CHECK(sprintf(str, "%010f", NAN) == 10);
177 	ATF_REQUIRE_STREQ(str, "       nan");
178 	ATF_CHECK(sprintf(str, "%010f", INFINITY) == 10);
179 	ATF_REQUIRE_STREQ(str, "       inf");
180 #endif
181 }
182 
183 ATF_TC(snprintf_double_a);
184 ATF_TC_HEAD(snprintf_double_a, tc)
185 {
186 	atf_tc_set_md_var(tc, "descr", "Test printf a format");
187 }
188 
189 ATF_TC_BODY(snprintf_double_a, tc)
190 {
191 	char buf[1000];
192 
193 	snprintf(buf, sizeof buf, "%.3a", (double)10.6);
194 	ATF_REQUIRE_STREQ("0x1.533p+3", buf);
195 }
196 
197 /* is "long double" and "double" different? */
198 #if (__LDBL_MANT_DIG__ != __DBL_MANT_DIG__) || \
199     (__LDBL_MAX_EXP__ != __DBL_MAX_EXP__)
200 #define WIDE_DOUBLE
201 #endif
202 
203 #ifndef WIDE_DOUBLE
204 ATF_TC(pr57250_fix);
205 ATF_TC_HEAD(pr57250_fix, tc)
206 {
207 	atf_tc_set_md_var(tc, "descr", "Test for PR57250");
208 }
209 
210 ATF_TC_BODY(pr57250_fix, tc)
211 {
212 	char *eptr;
213 	char buf[1000];
214 	long double ld;
215 
216 	errno = 0;
217 	ld = strtold("1e309", &eptr);
218 	ATF_CHECK(errno != 0);
219 	ld = (double)ld;
220 	ATF_CHECK(isfinite(ld) == 0);
221 	snprintf(buf, sizeof buf, "%Lf\n", ld);
222 	ATF_REQUIRE_STREQ(buf, "inf\n");
223 }
224 #endif
225 
226 
227 ATF_TP_ADD_TCS(tp)
228 {
229 
230 	ATF_TP_ADD_TC(tp, snprintf_c99);
231 	ATF_TP_ADD_TC(tp, snprintf_dotzero);
232 	ATF_TP_ADD_TC(tp, snprintf_posarg);
233 	ATF_TP_ADD_TC(tp, snprintf_posarg_width);
234 	ATF_TP_ADD_TC(tp, snprintf_posarg_error);
235 	ATF_TP_ADD_TC(tp, snprintf_float);
236 	ATF_TP_ADD_TC(tp, sprintf_zeropad);
237 	ATF_TP_ADD_TC(tp, snprintf_double_a);
238 #ifndef WIDE_DOUBLE
239 	ATF_TP_ADD_TC(tp, pr57250_fix);
240 #endif
241 
242 	return atf_no_error();
243 }
244