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