1 #ifndef _XOPEN_SOURCE
2 #define _XOPEN_SOURCE 700
3 #endif
4 #include <stdio.h>
5 #include <string.h>
6 #include <errno.h>
7 #include <limits.h>
8 #include <math.h>
9 #include "test.h"
10 
11 #define DISABLE_SLOW_TESTS
12 
13 #define TEST(r, f, x, m) ( \
14 	((r) = (f)) == (x) || \
15 	(t_error("%s failed (" m ")\n", #f, r, x), 0) )
16 
17 #define TEST_S(s, x, m) ( \
18 	!strcmp((s),(x)) || \
19 	(t_error("[%s] != [%s] (%s)\n", s, x, m), 0) )
20 
21 static const struct {
22 	const char *fmt;
23 	int i;
24 	const char *expect;
25 } int_tests[] = {
26 	/* width, precision, alignment */
27 	{ "%04d", 12, "0012" },
28 	{ "%.3d", 12, "012" },
29 	{ "%3d", 12, " 12" },
30 	{ "%-3d", 12, "12 " },
31 	{ "%+3d", 12, "+12" },
32 	{ "%+-5d", 12, "+12  " },
33 	{ "%+- 5d", 12, "+12  " },
34 	{ "%- 5d", 12, " 12  " },
35 	{ "% d", 12, " 12" },
36 	{ "%0-5d", 12, "12   " },
37 	{ "%-05d", 12, "12   " },
38 
39 	/* ...explicit precision of 0 shall be no characters except for alt-octal. */
40 	{ "%.0d", 0, "" },
41 	{ "%.0o", 0, "" },
42 	{ "%#.0d", 0, "" },
43 	{ "%#.0o", 0, "0" },
44 	{ "%#.0x", 0, "" },
45 
46 	/* ...but it still has to honor width and flags. */
47 	{ "%2.0u", 0, "  " },
48 	{ "%02.0u", 0, "  " },
49 	{ "%2.0d", 0, "  " },
50 	{ "%02.0d", 0, "  " },
51 	{ "% .0d", 0, " " },
52 	{ "%+.0d", 0, "+" },
53 
54 	/* hex: test alt form and case */
55 	{ "%x", 63, "3f" },
56 	{ "%#x", 63, "0x3f" },
57 	{ "%X", 63, "3F" },
58 
59 	/* octal: test alt form */
60 	{ "%o", 15, "17" },
61 	{ "%#o", 15, "017" },
62 
63 	/* octal: corner cases */
64 	{ "%#o", 0, "0" },
65 	{ "%#.0o", 0, "0" },
66 	{ "%#.1o", 0, "0" },
67 	{ "%#o", 1, "01" },
68 	{ "%#.0o", 1, "01" },
69 	{ "%#.1o", 1, "01" },
70 	{ "%#04o", 1, "0001" },
71 	{ "%#04.0o", 1, "  01" },
72 	{ "%#04.1o", 1, "  01" },
73 	{ "%04o", 1, "0001" },
74 	{ "%04.0o", 1, "   1" },
75 	{ "%04.1o", 1, "   1" },
76 
77 	{ NULL, 0.0, NULL }
78 };
79 
80 static const struct {
81 	const char *fmt;
82 	double f;
83 	const char *expect;
84 } fp_tests[] = {
85 	/* basic form, handling of exponent/precision for 0 */
86 	{ "%a", 0.0, "0x0p+0" },
87 	{ "%e", 0.0, "0.000000e+00" },
88 	{ "%f", 0.0, "0.000000" },
89 	{ "%g", 0.0, "0" },
90 	{ "%#g", 0.0, "0.00000" },
91 	{ "%la", 0.0, "0x0p+0" },
92 	{ "%le", 0.0, "0.000000e+00" },
93 	{ "%lf", 0.0, "0.000000" },
94 	{ "%lg", 0.0, "0" },
95 	{ "%#lg", 0.0, "0.00000" },
96 
97 	/* rounding */
98 	{ "%f", 1.1, "1.100000" },
99 	{ "%f", 1.2, "1.200000" },
100 	{ "%f", 1.3, "1.300000" },
101 	{ "%f", 1.4, "1.400000" },
102 	{ "%f", 1.5, "1.500000" },
103 	{ "%.4f", 1.06125, "1.0613" }, /* input is not representible exactly as double */
104 	{ "%.4f", 1.03125, "1.0312" }, /* 0x1.08p0 */
105 	{ "%.2f", 1.375, "1.38" },
106 	{ "%.1f", 1.375, "1.4" },
107 	{ "%.1lf", 1.375, "1.4" },
108 	{ "%.15f", 1.1, "1.100000000000000" },
109 	{ "%.16f", 1.1, "1.1000000000000001" },
110 	{ "%.17f", 1.1, "1.10000000000000009" },
111 	{ "%.2e", 1500001.0, "1.50e+06" },
112 	{ "%.2e", 1505000.0, "1.50e+06" },
113 	{ "%.2e", 1505000.00000095367431640625, "1.51e+06" },
114 	{ "%.2e", 1505001.0, "1.51e+06" },
115 	{ "%.2e", 1506000.0, "1.51e+06" },
116 
117 	/* correctness in DBL_DIG places */
118 	{ "%.15g", 1.23456789012345, "1.23456789012345" },
119 
120 	/* correct choice of notation for %g */
121 	{ "%g", 0.0001, "0.0001" },
122 	{ "%g", 0.00001, "1e-05" },
123 	{ "%g", 123456, "123456" },
124 	{ "%g", 1234567, "1.23457e+06" },
125 	{ "%.7g", 1234567, "1234567" },
126 	{ "%.7g", 12345678, "1.234568e+07" },
127 	{ "%.8g", 0.1, "0.1" },
128 	{ "%.9g", 0.1, "0.1" },
129 	{ "%.10g", 0.1, "0.1" },
130 	{ "%.11g", 0.1, "0.1" },
131 
132 	/* pi in double precision, printed to a few extra places */
133 	{ "%.15f", M_PI, "3.141592653589793" },
134 	{ "%.18f", M_PI, "3.141592653589793116" },
135 
136 	/* exact conversion of large integers */
137 	{ "%.0f", 340282366920938463463374607431768211456.0,
138 	         "340282366920938463463374607431768211456" },
139 
140 	{ NULL, 0.0, NULL }
141 };
142 
main(void)143 int main(void)
144 {
145 	int i, j, k;
146 	char b[2000];
147 
148 	TEST(i, snprintf(0, 0, "%d", 123456), 6, "length returned %d != %d");
149 	TEST(i, snprintf(0, 0, "%.4s", "hello"), 4, "length returned %d != %d");
150 	TEST(i, snprintf(b, 0, "%.0s", "goodbye"), 0, "length returned %d != %d");
151 
152 	strcpy(b, "xxxxxxxx");
153 	TEST(i, snprintf(b, 4, "%d", 123456), 6, "length returned %d != %d");
154 	TEST_S(b, "123", "incorrect output");
155 	TEST(i, b[5], 'x', "buffer overrun");
156 
157 	/* Perform ascii arithmetic to test printing tiny doubles */
158 	TEST(i, snprintf(b, sizeof b, "%.1022f", 0x1p-1021), 1024, "%d != %d");
159 	b[1] = '0';
160 	for (i=0; i<1021; i++) {
161 		for (k=0, j=1023; j>0; j--) {
162 			if (b[j]<'5') b[j]+=b[j]-'0'+k, k=0;
163 			else b[j]+=b[j]-'0'-10+k, k=1;
164 		}
165 	}
166 	TEST(i, b[1], '1', "'%c' != '%c'");
167 	for (j=2; b[j]=='0'; j++);
168 	TEST(i, j, 1024, "%d != %d");
169 
170 
171 #ifndef DISABLE_SLOW_TESTS
172 	errno = 0;
173 	TEST(i, snprintf(NULL, 0, "%.*u", 2147483647, 0), 2147483647, "cannot print max length %d");
174 	TEST(i, snprintf(NULL, 0, "%.*u ", 2147483647, 0), -1, "integer overflow %d");
175 	TEST(i, errno, EOVERFLOW, "after overflow: %d != %d");
176 #endif
177 	for (j=0; int_tests[j].fmt; j++) {
178 		i = snprintf(b, sizeof b, int_tests[j].fmt, int_tests[j].i);
179 		if (i != strlen(int_tests[j].expect)) {
180 			t_error("snprintf(b, sizeof b, \"%s\", %d) returned %d wanted %d\n",
181 				int_tests[j].fmt, int_tests[j].i, i, strlen(int_tests[j].expect));
182 		}
183 		if (strcmp(b, int_tests[j].expect) != 0)
184 			t_error("bad integer conversion: got \"%s\", want \"%s\"\n", b, int_tests[j].expect);
185 	}
186 
187 	for (j=0; fp_tests[j].fmt; j++) {
188 		i = snprintf(b, sizeof b, fp_tests[j].fmt, fp_tests[j].f);
189 		if (i != strlen(fp_tests[j].expect)) {
190 			t_error("snprintf(b, sizeof b, \"%s\", %f) returned %d wanted %d\n",
191 				fp_tests[j].fmt, fp_tests[j].f, i, strlen(fp_tests[j].expect));
192 		}
193 		if (strcmp(b, fp_tests[j].expect) != 0)
194 			t_error("bad floating-point conversion: got \"%s\", want \"%s\"\n", b, fp_tests[j].expect);
195 	}
196 
197 	TEST(i, snprintf(0, 0, "%.4a", 1.0), 11, "%d != %d");
198 	return t_status;
199 }
200