xref: /netbsd-src/tests/lib/libc/stdio/t_printf.c (revision ead2c0eee3abe6bcf08c63bfc78eb8a93a579b2b)
1 /* $NetBSD: t_printf.c,v 1.4 2012/02/26 23:14:26 christos 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_dotzero);
40 ATF_TC_HEAD(snprintf_dotzero, tc)
41 {
42 
43 	atf_tc_set_md_var(tc, "descr", \
44 	    "PR lib/32951: %.0f formats (0.0,0.5] to \"0.\"");
45 }
46 
47 ATF_TC_BODY(snprintf_dotzero, tc)
48 {
49 	char s[4];
50 
51 	ATF_CHECK(snprintf(s, sizeof(s), "%.0f", 0.1) == 1);
52 	ATF_REQUIRE_STREQ(s, "0");
53 }
54 
55 ATF_TC(snprintf_posarg);
56 ATF_TC_HEAD(snprintf_posarg, tc)
57 {
58 
59 	atf_tc_set_md_var(tc, "descr", "test for positional arguments");
60 }
61 
62 ATF_TC_BODY(snprintf_posarg, tc)
63 {
64 	char s[16];
65 
66 	ATF_CHECK(snprintf(s, sizeof(s), "%1$d", -23) == 3);
67 	ATF_REQUIRE_STREQ(s, "-23");
68 }
69 
70 ATF_TC(snprintf_posarg_width);
71 ATF_TC_HEAD(snprintf_posarg_width, tc)
72 {
73 
74 	atf_tc_set_md_var(tc, "descr", "test for positional arguments with "
75 	    "field width");
76 }
77 
78 ATF_TC_BODY(snprintf_posarg_width, tc)
79 {
80 	char s[16];
81 
82 	ATF_CHECK(snprintf(s, sizeof(s), "%1$*2$d", -23, 4) == 4);
83 	ATF_REQUIRE_STREQ(s, " -23");
84 }
85 
86 ATF_TC(snprintf_posarg_error);
87 ATF_TC_HEAD(snprintf_posarg_error, tc)
88 {
89 
90 	atf_tc_set_md_var(tc, "descr", "test for positional arguments out "
91 	    "of bounds");
92 }
93 
94 ATF_TC_BODY(snprintf_posarg_error, tc)
95 {
96 	char s[16], fmt[32];
97 
98 	snprintf(fmt, sizeof(fmt), "%%%zu$d", SIZE_MAX / sizeof(size_t));
99 
100 	ATF_CHECK(snprintf(s, sizeof(s), fmt, -23) == -1);
101 }
102 
103 ATF_TC(snprintf_float);
104 ATF_TC_HEAD(snprintf_float, tc)
105 {
106 
107 	atf_tc_set_md_var(tc, "descr", "test that floating conversions don't"
108 	    " leak memory");
109 }
110 
111 ATF_TC_BODY(snprintf_float, tc)
112 {
113 	union {
114 		double d;
115 		uint64_t bits;
116 	} u;
117 	uint32_t ul, uh;
118 	time_t now;
119 	char buf[1000];
120 	struct rlimit rl;
121 
122 	rl.rlim_cur = rl.rlim_max = 1 * 1024 * 1024;
123 	ATF_CHECK(setrlimit(RLIMIT_AS, &rl) != -1);
124 	rl.rlim_cur = rl.rlim_max = 1 * 1024 * 1024;
125 	ATF_CHECK(setrlimit(RLIMIT_DATA, &rl) != -1);
126 
127 	time(&now);
128 	srand(now);
129 	for (size_t i = 0; i < 1000000; i++) {
130 		ul = rand();
131 		uh = rand();
132 		u.bits = (uint64_t)uh << 32 | ul;
133 		ATF_CHECK(snprintf(buf, sizeof buf, " %.2f", u.d) != -1);
134 	}
135 }
136 
137 ATF_TC(sprintf_zeropad);
138 ATF_TC_HEAD(sprintf_zeropad, tc)
139 {
140 
141 	atf_tc_set_md_var(tc, "descr", "output format zero padding");
142 }
143 
144 ATF_TC_BODY(sprintf_zeropad, tc)
145 {
146 	char str[1024];
147 
148 	ATF_CHECK(sprintf(str, "%010f", 0.0) == 10);
149 	ATF_REQUIRE_STREQ(str, "000.000000");
150 
151 	/* ieeefp */
152 #ifndef __vax__
153 	/* PR/44113: printf(3) should ignore zero padding for nan/inf */
154 	ATF_CHECK(sprintf(str, "%010f", NAN) == 10);
155 	ATF_REQUIRE_STREQ(str, "       nan");
156 	ATF_CHECK(sprintf(str, "%010f", INFINITY) == 10);
157 	ATF_REQUIRE_STREQ(str, "       inf");
158 #endif
159 }
160 
161 ATF_TP_ADD_TCS(tp)
162 {
163 
164 	ATF_TP_ADD_TC(tp, snprintf_dotzero);
165 	ATF_TP_ADD_TC(tp, snprintf_posarg);
166 	ATF_TP_ADD_TC(tp, snprintf_posarg_width);
167 	ATF_TP_ADD_TC(tp, snprintf_posarg_error);
168 	ATF_TP_ADD_TC(tp, snprintf_float);
169 	ATF_TP_ADD_TC(tp, sprintf_zeropad);
170 
171 	return atf_no_error();
172 }
173