xref: /freebsd/contrib/netbsd-tests/lib/libc/stdio/t_printf.c (revision 531c2d7af3cd2e64eec94aa1b19c4b2f16fce515)
1 /* $NetBSD: t_printf.c,v 1.8 2012/04/11 16:21:42 jruoho 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 
39 ATF_TC(snprintf_c99);
ATF_TC_HEAD(snprintf_c99,tc)40 ATF_TC_HEAD(snprintf_c99, tc)
41 {
42 
43 	atf_tc_set_md_var(tc, "descr",
44 	    "Test printf(3) C99 conformance (PR lib/22019)");
45 }
46 
ATF_TC_BODY(snprintf_c99,tc)47 ATF_TC_BODY(snprintf_c99, tc)
48 {
49 	char s[4];
50 
51 	(void)memset(s, '\0', sizeof(s));
52 	(void)snprintf(s, sizeof(s), "%#.o", 0);
53 	(void)printf("printf = %#.o\n", 0);
54 	(void)fprintf(stderr, "snprintf = %s", s);
55 
56 	ATF_REQUIRE(strlen(s) == 1);
57 	ATF_REQUIRE(s[0] == '0');
58 }
59 
60 ATF_TC(snprintf_dotzero);
ATF_TC_HEAD(snprintf_dotzero,tc)61 ATF_TC_HEAD(snprintf_dotzero, tc)
62 {
63 
64 	atf_tc_set_md_var(tc, "descr",
65 	    "PR lib/32951: %%.0f formats (0.0,0.5] to \"0.\"");
66 }
67 
ATF_TC_BODY(snprintf_dotzero,tc)68 ATF_TC_BODY(snprintf_dotzero, tc)
69 {
70 	char s[4];
71 
72 	ATF_CHECK(snprintf(s, sizeof(s), "%.0f", 0.1) == 1);
73 	ATF_REQUIRE_STREQ(s, "0");
74 }
75 
76 ATF_TC(snprintf_posarg);
ATF_TC_HEAD(snprintf_posarg,tc)77 ATF_TC_HEAD(snprintf_posarg, tc)
78 {
79 
80 	atf_tc_set_md_var(tc, "descr", "test for positional arguments");
81 }
82 
ATF_TC_BODY(snprintf_posarg,tc)83 ATF_TC_BODY(snprintf_posarg, tc)
84 {
85 	char s[16];
86 
87 	ATF_CHECK(snprintf(s, sizeof(s), "%1$d", -23) == 3);
88 	ATF_REQUIRE_STREQ(s, "-23");
89 }
90 
91 ATF_TC(snprintf_posarg_width);
ATF_TC_HEAD(snprintf_posarg_width,tc)92 ATF_TC_HEAD(snprintf_posarg_width, tc)
93 {
94 
95 	atf_tc_set_md_var(tc, "descr", "test for positional arguments with "
96 	    "field width");
97 }
98 
ATF_TC_BODY(snprintf_posarg_width,tc)99 ATF_TC_BODY(snprintf_posarg_width, tc)
100 {
101 	char s[16];
102 
103 	ATF_CHECK(snprintf(s, sizeof(s), "%1$*2$d", -23, 4) == 4);
104 	ATF_REQUIRE_STREQ(s, " -23");
105 }
106 
107 ATF_TC(snprintf_posarg_error);
ATF_TC_HEAD(snprintf_posarg_error,tc)108 ATF_TC_HEAD(snprintf_posarg_error, tc)
109 {
110 
111 	atf_tc_set_md_var(tc, "descr", "test for positional arguments out "
112 	    "of bounds");
113 }
114 
ATF_TC_BODY(snprintf_posarg_error,tc)115 ATF_TC_BODY(snprintf_posarg_error, tc)
116 {
117 	char s[16], fmt[32];
118 
119 	snprintf(fmt, sizeof(fmt), "%%%zu$d", SIZE_MAX / sizeof(size_t));
120 
121 	ATF_CHECK(snprintf(s, sizeof(s), fmt, -23) == -1);
122 }
123 
124 ATF_TC(snprintf_float);
ATF_TC_HEAD(snprintf_float,tc)125 ATF_TC_HEAD(snprintf_float, tc)
126 {
127 
128 	atf_tc_set_md_var(tc, "descr", "test that floating conversions don't"
129 	    " leak memory");
130 #ifdef	__FreeBSD__
131 	atf_tc_set_md_var(tc, "require.memory", "64m");
132 	atf_tc_set_md_var(tc, "require.user", "root");
133 #endif
134 }
135 
ATF_TC_BODY(snprintf_float,tc)136 ATF_TC_BODY(snprintf_float, tc)
137 {
138 	union {
139 		double d;
140 		uint64_t bits;
141 	} u;
142 	uint32_t ul, uh;
143 	time_t now;
144 	char buf[1000];
145 	struct rlimit rl;
146 
147 #ifdef	__FreeBSD__
148 	rl.rlim_cur = rl.rlim_max = 32 * 1024 * 1024;
149 	ATF_CHECK(setrlimit(RLIMIT_AS, &rl) != -1);
150 	rl.rlim_cur = rl.rlim_max = 32 * 1024 * 1024;
151 	ATF_CHECK(setrlimit(RLIMIT_DATA, &rl) != -1);
152 #else
153 	rl.rlim_cur = rl.rlim_max = 1 * 1024 * 1024;
154 	ATF_CHECK(setrlimit(RLIMIT_AS, &rl) != -1);
155 	rl.rlim_cur = rl.rlim_max = 1 * 1024 * 1024;
156 	ATF_CHECK(setrlimit(RLIMIT_DATA, &rl) != -1);
157 #endif
158 
159 	time(&now);
160 	srand(now);
161 	for (size_t i = 0; i < 10000; i++) {
162 		ul = rand();
163 		uh = rand();
164 		u.bits = (uint64_t)uh << 32 | ul;
165 		ATF_CHECK(snprintf(buf, sizeof buf, " %.2f", u.d) != -1);
166 	}
167 }
168 
169 ATF_TC(sprintf_zeropad);
ATF_TC_HEAD(sprintf_zeropad,tc)170 ATF_TC_HEAD(sprintf_zeropad, tc)
171 {
172 	atf_tc_set_md_var(tc, "descr",
173 	    "Test output format zero padding (PR lib/44113)");
174 }
175 
ATF_TC_BODY(sprintf_zeropad,tc)176 ATF_TC_BODY(sprintf_zeropad, tc)
177 {
178 	char str[1024];
179 
180 	ATF_CHECK(sprintf(str, "%010f", 0.0) == 10);
181 	ATF_REQUIRE_STREQ(str, "000.000000");
182 
183 	/* ieeefp */
184 #ifndef __vax__
185 	/* printf(3) should ignore zero padding for nan/inf */
186 	ATF_CHECK(sprintf(str, "%010f", NAN) == 10);
187 	ATF_REQUIRE_STREQ(str, "       nan");
188 	ATF_CHECK(sprintf(str, "%010f", INFINITY) == 10);
189 	ATF_REQUIRE_STREQ(str, "       inf");
190 #endif
191 }
192 
ATF_TP_ADD_TCS(tp)193 ATF_TP_ADD_TCS(tp)
194 {
195 
196 	ATF_TP_ADD_TC(tp, snprintf_c99);
197 	ATF_TP_ADD_TC(tp, snprintf_dotzero);
198 	ATF_TP_ADD_TC(tp, snprintf_posarg);
199 	ATF_TP_ADD_TC(tp, snprintf_posarg_width);
200 	ATF_TP_ADD_TC(tp, snprintf_posarg_error);
201 	ATF_TP_ADD_TC(tp, snprintf_float);
202 	ATF_TP_ADD_TC(tp, sprintf_zeropad);
203 
204 	return atf_no_error();
205 }
206