OSDN Git Service

Avoid built-in snprintf() prototypes; fix MinGW-Bug #39224
[mingw/mingw-org-wsl.git] / mingwrt / tests / ansiprintf.at
1 # ansiprintf.at
2 #
3 # Autotest module to verify correct operation of MinGW.org's suite of
4 # ANSI compliant replacements for the printf() family of functions.
5 #
6 # $Id$
7 #
8 # Written by Keith Marshall <keithmarshall@users.sourceforge.net>
9 # Copyright (C) 2016, MinGW.org Project
10 #
11 #
12 # Permission is hereby granted, free of charge, to any person obtaining a
13 # copy of this software and associated documentation files (the "Software"),
14 # to deal in the Software without restriction, including without limitation
15 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 # and/or sell copies of the Software, and to permit persons to whom the
17 # Software is furnished to do so, subject to the following conditions:
18 #
19 # The above copyright notice and this permission notice (including the next
20 # paragraph) shall be included in all copies or substantial portions of the
21 # Software.
22 #
23 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 # AUTHORS OR THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29 # DEALINGS IN THE SOFTWARE.
30 #
31 #
32 # All tests specified herein are written in the C language.
33 #
34 MINGW_AT_LANG([C])
35
36 # MINGW_AT_CHECK_PRINTF( FORMAT, ARGS, EXPOUT )
37 # ---------------------------------------------
38 # Set up the test case to evaluate the behaviour of printf() when
39 # invoked with the specified (unquoted) FORMAT, and the given comma
40 # separated list of ARGS; confirm that the resultant output matches
41 # EXPOUT, (which is interpreted as CRLF delimited text).
42 #
43 m4_define([MINGW_AT_CHECK_PRINTF],[dnl
44 AT_SETUP([printf ("$1", $2)])_MINGW_AT_CHECK_PRINTF([$1],[$2],[$3])dnl
45 ])m4_define([_MINGW_AT_CHECK_PRINTF],[dnl
46 AT_KEYWORDS([C printf])
47 MINGW_AT_DATA_CRLF([expout],[[$3
48 ]])MINGW_AT_CHECK_RUN([[[
49 #define _XOPEN_SOURCE 700
50 #include <stdio.h>
51 #include <tgmath.h>
52 int main()
53 { printf ("::]$1[::\n", ]$2[); return 0;
54 }]]],,[expout])dnl
55 AT_CLEANUP
56 ])# MINGW_AT_CHECK_PRINTF
57
58 # MINGW_AT_XFAIL_PRINTF( FORMAT, ARGS, EXPOUT )
59 # ---------------------------------------------
60 # A variation on MINGW_AT_CHECK_PRINTF, used to introduce any test
61 # from which the output is likely to differ from that which might be
62 # expected for the given format and arguments, (e.g. the display of
63 # numeric digits beyond the limit of precision for the data type).
64 #
65 m4_define([MINGW_AT_XFAIL_PRINTF],[dnl
66 AT_SETUP([printf ("$1", $2)])AT_XFAIL_IF([true])dnl
67 _MINGW_AT_CHECK_PRINTF([$1],[$2],[$3])dnl
68 ])# MINGW_AT_XFAIL_PRINTF
69
70
71 # Test behaviour of "%d" format conversion, for decimal
72 # display of integer values.
73 #
74 AT_BANNER([[ISO-C99 printf() integer value formatting.]])
75 MINGW_AT_CHECK_PRINTF([[%20d]],    [+0],    [::                   0::])
76 MINGW_AT_CHECK_PRINTF([[%+20d]],   [-0],    [::                  +0::])
77 MINGW_AT_CHECK_PRINTF([[%020d]],   [+0],    [::00000000000000000000::])
78 MINGW_AT_CHECK_PRINTF([[%-20d]],   [-0],    [::0                   ::])
79 MINGW_AT_CHECK_PRINTF([[%20d]],    [+55],   [::                  55::])
80 MINGW_AT_CHECK_PRINTF([[%20d]],    [-55],   [::                 -55::])
81 MINGW_AT_CHECK_PRINTF([[%+20d]],   [+55],   [::                 +55::])
82 MINGW_AT_CHECK_PRINTF([[%+20d]],   [-55],   [::                 -55::])
83 MINGW_AT_CHECK_PRINTF([[%020d]],   [+55],   [::00000000000000000055::])
84 MINGW_AT_CHECK_PRINTF([[%-20d]],   [+55],   [::55                  ::])
85
86
87 # Test behaviour of "%#x" format conversion, for hexadecimal
88 # display of integer values.
89 #
90 AT_BANNER([[ISO-C99 printf() hexadecimal integer formatting.]])
91 MINGW_AT_CHECK_PRINTF([[%#20x]],   [-0],    [::                   0::])
92 MINGW_AT_CHECK_PRINTF([[%#+20x]],  [+0],    [::                   0::])
93 MINGW_AT_CHECK_PRINTF([[%#020x]],  [-0],    [::00000000000000000000::])
94 MINGW_AT_CHECK_PRINTF([[%#-20x]],  [+0],    [::0                   ::])
95 MINGW_AT_CHECK_PRINTF([[%#20x]],   [+77],   [::                0x4d::])
96 MINGW_AT_CHECK_PRINTF([[%#20x]],   [-77],   [::          0xffffffb3::])
97 MINGW_AT_CHECK_PRINTF([[%#+20x]],  [+77],   [::                0x4d::])
98 MINGW_AT_CHECK_PRINTF([[%#+20x]],  [-77],   [::          0xffffffb3::])
99 MINGW_AT_CHECK_PRINTF([[%#020x]],  [+77],   [::0x00000000000000004d::])
100 MINGW_AT_CHECK_PRINTF([[%#-20x]],  [+77],   [::0x4d                ::])
101
102
103 # Test effect of alignment and sign flags on "%f" format conversion.
104 #
105 AT_BANNER([[ISO-C99 printf() fixed point real number formatting.]])
106 MINGW_AT_CHECK_PRINTF([[%20f]],    [M_E],   [::            2.718282::])
107 MINGW_AT_CHECK_PRINTF([[% 20f]],   [M_E],   [::            2.718282::])
108 MINGW_AT_CHECK_PRINTF([[%+20f]],   [M_E],   [::           +2.718282::])
109 MINGW_AT_CHECK_PRINTF([[% 020f]],  [M_E],   [:: 000000000002.718282::])
110 MINGW_AT_CHECK_PRINTF([[%020f]],   [M_E],   [::0000000000002.718282::])
111 MINGW_AT_CHECK_PRINTF([[%+020f]],  [M_E],   [::+000000000002.718282::])
112 MINGW_AT_CHECK_PRINTF([[%+ 020f]], [M_E],   [::+000000000002.718282::])
113 MINGW_AT_CHECK_PRINTF([[%-20f]],   [M_E],   [::2.718282            ::])
114 MINGW_AT_CHECK_PRINTF([[%- 20f]],  [M_E],   [:: 2.718282           ::])
115 MINGW_AT_CHECK_PRINTF([[%-+20f]],  [M_E],   [::+2.718282           ::])
116 MINGW_AT_CHECK_PRINTF([[%- +20f]], [M_E],   [::+2.718282           ::])
117 MINGW_AT_CHECK_PRINTF([[%-020f]],  [M_E],   [::2.718282            ::])
118
119
120 # Test the effect of "%f" format conversion, with a value which
121 # requires in excess the representable 15-digit maximum precision
122 # of the double data type; ideally, a value with an exact binary
123 # representation would be displayed exactly, but we expect this
124 # to fail, with garbage beyond the 15-digit boundary.
125 #
126 AT_BANNER([[ISO-C99 printf() fixed point formatting imprecision.]])
127 MINGW_AT_XFAIL_PRINTF([[%30.1f]], [1e+26],  [:: 100000000000000000000000000.0::])
128
129
130 # Test default behaviour of "%g" format conversion; (this should
131 # have no non-significant trailing zeros, nor should it end with
132 # a radix point without following non-zero fractional digits).
133 #
134 AT_BANNER([[ISO-C99 printf() floating point real number formatting.]])
135 MINGW_AT_CHECK_PRINTF([[%20g]],  [1.75e+02], [::                 175::])
136 MINGW_AT_CHECK_PRINTF([[%20g]],  [1.75e+05], [::              175000::])
137 MINGW_AT_CHECK_PRINTF([[%20g]],  [1.75e+15], [::           1.75e+015::])
138 MINGW_AT_CHECK_PRINTF([[%20g]],  [1.75e+00], [::                1.75::])
139 MINGW_AT_CHECK_PRINTF([[%20g]],  [1.75e-03], [::             0.00175::])
140 MINGW_AT_CHECK_PRINTF([[%20g]],  [1.75e-15], [::           1.75e-015::])
141
142
143 # Test effect of alignment and sign flags on "%g" format conversion.
144 #
145 AT_BANNER([[ISO-C99 printf() floating point formatting with flags.]])
146 MINGW_AT_CHECK_PRINTF([[%+20g]],   [M_E],    [::            +2.71828::])
147 MINGW_AT_CHECK_PRINTF([[%020g]],   [M_E],    [::00000000000002.71828::])
148 MINGW_AT_CHECK_PRINTF([[% 020g]],  [M_E],    [:: 0000000000002.71828::])
149 MINGW_AT_CHECK_PRINTF([[%+020g]],  [M_E],    [::+0000000000002.71828::])
150 MINGW_AT_CHECK_PRINTF([[%+ 020g]], [M_E],    [::+0000000000002.71828::])
151 MINGW_AT_CHECK_PRINTF([[%-20g]],   [M_E],    [::2.71828             ::])
152 MINGW_AT_CHECK_PRINTF([[%- 20g]],  [M_E],    [:: 2.71828            ::])
153 MINGW_AT_CHECK_PRINTF([[%-+20g]],  [M_E],    [::+2.71828            ::])
154 MINGW_AT_CHECK_PRINTF([[%- +20g]], [M_E],    [::+2.71828            ::])
155 MINGW_AT_CHECK_PRINTF([[%-020g]],  [M_E],    [::2.71828             ::])
156
157
158 # Test effect of "#" flag on "%g" format conversion; (this should
159 # override suppression of non-significant trailing zeros).
160 #
161 AT_BANNER([[ISO-C99 printf() alternative floating point formatting.]])
162 MINGW_AT_CHECK_PRINTF([[%#20g]], [1.75e+02], [::             175.000::])
163 MINGW_AT_CHECK_PRINTF([[%#20g]], [1.75e+05], [::             175000.::])
164 MINGW_AT_CHECK_PRINTF([[%#20g]], [1.75e+15], [::        1.75000e+015::])
165 MINGW_AT_CHECK_PRINTF([[%#20g]], [1.75e+00], [::             1.75000::])
166 MINGW_AT_CHECK_PRINTF([[%#20g]], [1.75e-03], [::          0.00175000::])
167 MINGW_AT_CHECK_PRINTF([[%#20g]], [1.75e-15], [::        1.75000e-015::])
168
169
170 # Test transition between "%e" and "%f" style representations, for
171 # default precision "%g" format conversion.
172 #
173 AT_BANNER([[ISO-C99 printf() floating point format style transitions.]])
174 MINGW_AT_CHECK_PRINTF([[%20g]], [M_E * 1e-6], [::        2.71828e-006::])
175 MINGW_AT_CHECK_PRINTF([[%20g]], [M_E * 1e-5], [::        2.71828e-005::])
176 MINGW_AT_CHECK_PRINTF([[%20g]], [M_E * 1e-4], [::         0.000271828::])
177 MINGW_AT_CHECK_PRINTF([[%20g]], [M_E * 1e-3], [::          0.00271828::])
178 MINGW_AT_CHECK_PRINTF([[%20g]], [M_E * 1e-2], [::           0.0271828::])
179 MINGW_AT_CHECK_PRINTF([[%20g]], [M_E * 1e-1], [::            0.271828::])
180 MINGW_AT_CHECK_PRINTF([[%20g]], [M_E * 1e+0], [::             2.71828::])
181 MINGW_AT_CHECK_PRINTF([[%20g]], [M_E * 1e+1], [::             27.1828::])
182 MINGW_AT_CHECK_PRINTF([[%20g]], [M_E * 1e+2], [::             271.828::])
183 MINGW_AT_CHECK_PRINTF([[%20g]], [M_E * 1e+3], [::             2718.28::])
184 MINGW_AT_CHECK_PRINTF([[%20g]], [M_E * 1e+4], [::             27182.8::])
185 MINGW_AT_CHECK_PRINTF([[%20g]], [M_E * 1e+5], [::              271828::])
186 MINGW_AT_CHECK_PRINTF([[%20g]], [M_E * 1e+6], [::        2.71828e+006::])
187 MINGW_AT_CHECK_PRINTF([[%20g]], [M_E * 1e+7], [::        2.71828e+007::])
188
189
190 # Test the effect of varying precision, as specified by the "*" format
191 # modifier, on "%g" format conversion; (-ve precision implies default;
192 # zero or +ve specifies actual significant digits).
193 #
194 AT_BANNER([[ISO-C99 printf() variable precision floating point format.]])
195 MINGW_AT_CHECK_PRINTF([[%20.*g]], [-2, M_E], [::             2.71828::])
196 MINGW_AT_CHECK_PRINTF([[%20.*g]], [-1, M_E], [::             2.71828::])
197 MINGW_AT_CHECK_PRINTF([[%20.*g]], [+0, M_E], [::                   3::])
198 MINGW_AT_CHECK_PRINTF([[%20.*g]], [+1, M_E], [::                   3::])
199 MINGW_AT_CHECK_PRINTF([[%20.*g]], [+2, M_E], [::                 2.7::])
200 MINGW_AT_CHECK_PRINTF([[%20.*g]], [+3, M_E], [::                2.72::])
201 MINGW_AT_CHECK_PRINTF([[%20.*g]], [+4, M_E], [::               2.718::])
202 MINGW_AT_CHECK_PRINTF([[%20.*g]], [+5, M_E], [::              2.7183::])
203 MINGW_AT_CHECK_PRINTF([[%20.*g]], [+6, M_E], [::             2.71828::])
204 MINGW_AT_CHECK_PRINTF([[%20.*g]], [+7, M_E], [::            2.718282::])
205 MINGW_AT_CHECK_PRINTF([[%20.*g]], [+8, M_E], [::           2.7182818::])
206
207
208 # Test variations of "%e" format conversion.
209 #
210 AT_BANNER([[ISO-C99 printf() scientific style floating point format.]])
211 MINGW_AT_CHECK_PRINTF([[%20.7e]],  [+M_E * 0.0], [::      0.0000000e+000::])
212 MINGW_AT_CHECK_PRINTF([[%20.7e]],  [-M_E * 0.0], [::     -0.0000000e+000::])
213 MINGW_AT_CHECK_PRINTF([[%20.7e]],  [+M_E * 1.0], [::      2.7182818e+000::])
214 MINGW_AT_CHECK_PRINTF([[%20.7e]],  [-M_E * 1.0], [::     -2.7182818e+000::])
215 MINGW_AT_CHECK_PRINTF([[%20.7e]],  [+M_E / 0.0], [::                 inf::])
216 MINGW_AT_CHECK_PRINTF([[%20.7e]],  [-M_E / 0.0], [::                -inf::])
217 MINGW_AT_CHECK_PRINTF([[%20.7e]],  [-0.0 / 0.0], [::                 nan::])
218 MINGW_AT_CHECK_PRINTF([[%-20.7e]], [+M_E * 0.0], [::0.0000000e+000      ::])
219 MINGW_AT_CHECK_PRINTF([[%-20.7e]], [-M_E * 0.0], [::-0.0000000e+000     ::])
220 MINGW_AT_CHECK_PRINTF([[%-20.7e]], [+M_E * 1.0], [::2.7182818e+000      ::])
221 MINGW_AT_CHECK_PRINTF([[%-20.7e]], [-M_E * 1.0], [::-2.7182818e+000     ::])
222 MINGW_AT_CHECK_PRINTF([[%-20.7e]], [+M_E / 0.0], [::inf                 ::])
223 MINGW_AT_CHECK_PRINTF([[%-20.7e]], [-M_E / 0.0], [::-inf                ::])
224 MINGW_AT_CHECK_PRINTF([[%-20.7e]], [-0.0 / 0.0], [::nan                 ::])
225 MINGW_AT_CHECK_PRINTF([[%+20.7e]], [+M_E * 0.0], [::     +0.0000000e+000::])
226 MINGW_AT_CHECK_PRINTF([[%+20.7e]], [-M_E * 0.0], [::     -0.0000000e+000::])
227 MINGW_AT_CHECK_PRINTF([[%+20.7e]], [+M_E * 1.0], [::     +2.7182818e+000::])
228 MINGW_AT_CHECK_PRINTF([[%+20.7e]], [-M_E * 1.0], [::     -2.7182818e+000::])
229 MINGW_AT_CHECK_PRINTF([[%+20.7e]], [+M_E / 0.0], [::                +inf::])
230 MINGW_AT_CHECK_PRINTF([[%+20.7e]], [-M_E / 0.0], [::                -inf::])
231 MINGW_AT_CHECK_PRINTF([[%+20.7e]], [-0.0 / 0.0], [::                +nan::])
232
233
234 # Test variations of "%a" format conversion; (the following integer
235 # value confirms correct size of the floating point entity passed on
236 # the printf() argument stack).
237 #
238 AT_BANNER([[ISO-C99 printf() hexadecimal floating point formatting.]])
239 MINGW_AT_CHECK_PRINTF([[%16a%4d]],    [0.000, 77],  [::          0x0p+0  77::])
240 MINGW_AT_CHECK_PRINTF([[%16.3a%4d]],  [0.000, 99],  [::      0x0.000p+0  99::])
241 MINGW_AT_CHECK_PRINTF([[%16a%4d]],    [1.750, 77],  [::          0xep-3  77::])
242 MINGW_AT_CHECK_PRINTF([[%016a%4d]],   [1.750, 77],  [::0x0000000000ep-3  77::])
243 MINGW_AT_CHECK_PRINTF([[%-016a%4d]],  [1.750, 77],  [::0xep-3            77::])
244 MINGW_AT_CHECK_PRINTF([[%16.0a%4d]],  [1.450, 44],  [::          0xcp-3  44::])
245 MINGW_AT_CHECK_PRINTF([[%16.1a%4d]],  [1.999, 99],  [::        0x8.0p-2  99::])
246 MINGW_AT_CHECK_PRINTF([[%16La%4d]],   [1.750L, 77], [::          0xep-3  77::])
247 MINGW_AT_CHECK_PRINTF([[%016La%4d]],  [1.750L, 77], [::0x0000000000ep-3  77::])
248 MINGW_AT_CHECK_PRINTF([[%16.0La%4d]], [1.450L, 44], [::          0xcp-3  44::])
249 MINGW_AT_CHECK_PRINTF([[%16.1La%4d]], [1.999L, 99], [::        0x8.0p-2  99::])
250
251
252 # MINGW_AT_CHECK_SNPRINTF( FORMAT, COUNT, MAXCOUNT, INITCOUNT )
253 # -------------------------------------------------------------
254 # Test the behaviour of the snprintf() function, with respect to
255 # output truncation at specified MAXCOUNT (no more than 32), when
256 # writing string "Sample text; sufficient buffer" subject to the
257 # specified FORMAT and COUNT (concatenated to define the format
258 # string).  Also supports testing the effect of "%n" counting on
259 # an internal integer variable, initialized to INITCOUNT, when
260 # COUNT is specified as "n", (with optional "hh" length modifier
261 # prefix; if this option is not to be exercised, FORMAT should
262 # be passed as "%", with COUNT as "s").
263 #
264 m4_define([MINGW_AT_CHECK_SNPRINTF],[dnl
265 AT_SETUP([snprintf (output, $3, "$1$2", ...)])
266 AT_KEYWORDS([C printf])MINGW_AT_DATA_CRLF([expout],[[$5
267 ]])MINGW_AT_CHECK_RUN([[[
268 #define _XOPEN_SOURCE 700
269 #include <stdio.h>
270 int main()
271 { char output[32]; union { int n; char hhn; } capture = {]$4[};
272   const char *sample = "Sample text; sufficient buffer";
273   int total = snprintf (output, ]$3[, "]$1$2[", sample]m4_if([$2],[s],]dnl
274     [,[, &capture.$2])[);
275   snprintf (NULL, 0, "]$1$2[", output]m4_if([$2],[s],,[, &capture.$2])[);
276   printf ("%s: %d required; %d captured\n", output, total, capture.n);
277   return 0;
278 }]]],,[expout])dnl
279 AT_CLEANUP
280 ])# MINGW_AT_CHECK_SNPRINTF
281
282
283 # Tests for snprintf() with limited length internal buffer.
284 #
285 AT_BANNER([ISO-C99 snprintf() buffer length control.])
286 MINGW_AT_CHECK_SNPRINTF([[%]],[s],[32],[1024],dnl
287 [Sample text; sufficient buffer: 30 required; 1024 captured])
288 MINGW_AT_CHECK_SNPRINTF([[%]],[s],[12],[1024],dnl
289 [Sample text: 30 required; 1024 captured])
290
291
292 # Tests for effect of "%n" output length counting.
293 #
294 AT_BANNER([ISO-C99 snprintf() intermediate output counting.])
295 MINGW_AT_CHECK_SNPRINTF([[%s%]],[n],[32],[1024],dnl
296 [Sample text; sufficient buffer: 30 required; 30 captured])
297 MINGW_AT_CHECK_SNPRINTF([[%s%]],[n],[12],[1024],dnl
298 [Sample text: 30 required; 11 captured])
299 MINGW_AT_CHECK_SNPRINTF([[%s%]],[hhn],[12],[1024],dnl
300 [Sample text: 30 required; 1035 captured])
301
302 # vim: filetype=config formatoptions=croql
303 # $RCSfile$: end of file