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