Add tests for %f and %F directives.
[gnulib.git] / tests / test-fprintf-posix.h
1 /* Test of POSIX compatible vsprintf() and sprintf() functions.
2    Copyright (C) 2007 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 /* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
19
20 /* The Compaq (ex-DEC) C 6.4 compiler chokes on the expression 0.0 / 0.0.  */
21 #ifdef __DECC
22 static double
23 NaN ()
24 {
25   static double zero = 0.0;
26   return zero / zero;
27 }
28 #else
29 # define NaN() (0.0 / 0.0)
30 #endif
31
32 static void
33 test_function (int (*my_fprintf) (FILE *, const char *, ...))
34 {
35   /* Here we don't test output that may be platform dependent.
36      The bulk of the tests is done as part of the 'vasnprintf-posix' module.  */
37
38   /* Test support of size specifiers as in C99.  */
39
40   my_fprintf (stdout, "%ju %d\n", (uintmax_t) 12345671, 33, 44, 55);
41
42   my_fprintf (stdout, "%zu %d\n", (size_t) 12345672, 33, 44, 55);
43
44   my_fprintf (stdout, "%tu %d\n", (ptrdiff_t) 12345673, 33, 44, 55);
45
46   /* Test the support of the 'a' and 'A' conversion specifier for hexadecimal
47      output of floating-point numbers.  */
48
49   /* Positive zero.  */
50   my_fprintf (stdout, "%a %d\n", 0.0, 33, 44, 55);
51
52   /* Negative zero.  */
53   my_fprintf (stdout, "%a %d\n", -0.0, 33, 44, 55);
54
55   /* Positive infinity.  */
56   my_fprintf (stdout, "%a %d\n", 1.0 / 0.0, 33, 44, 55);
57
58   /* Negative infinity.  */
59   my_fprintf (stdout, "%a %d\n", -1.0 / 0.0, 33, 44, 55);
60
61   /* NaN.  */
62   my_fprintf (stdout, "%a %d\n", NaN (), 33, 44, 55);
63
64   /* FLAG_ZERO with infinite number.  */
65   my_fprintf (stdout, "%010a %d\n", 1.0 / 0.0, 33, 44, 55);
66
67   /* FLAG_ZERO with NaN.  */
68   my_fprintf (stdout, "%010a %d\n", NaN (), 33, 44, 55);
69
70   /* Test the support of the %f format directive.  */
71
72   /* A positive number.  */
73   my_fprintf (stdout, "%f %d\n", 12.75, 33, 44, 55);
74
75   /* A larger positive number.  */
76   my_fprintf (stdout, "%f %d\n", 1234567.0, 33, 44, 55);
77
78   /* A negative number.  */
79   my_fprintf (stdout, "%f %d\n", -0.03125, 33, 44, 55);
80
81   /* Positive zero.  */
82   my_fprintf (stdout, "%f %d\n", 0.0, 33, 44, 55);
83
84   /* Negative zero.  */
85   my_fprintf (stdout, "%f %d\n", -0.0, 33, 44, 55);
86
87   /* NaN.  */
88   my_fprintf (stdout, "%f %d\n", NaN (), 33, 44, 55);
89
90   /* FLAG_ZERO.  */
91   my_fprintf (stdout, "%015f %d\n", 1234.0, 33, 44, 55);
92
93   /* Precision.  */
94   my_fprintf (stdout, "%.f %d\n", 1234.0, 33, 44, 55);
95
96   /* A positive number.  */
97   my_fprintf (stdout, "%Lf %d\n", 12.75L, 33, 44, 55);
98
99   /* A larger positive number.  */
100   my_fprintf (stdout, "%Lf %d\n", 1234567.0L, 33, 44, 55);
101
102   /* A negative number.  */
103   my_fprintf (stdout, "%Lf %d\n", -0.03125L, 33, 44, 55);
104
105   /* Positive zero.  */
106   my_fprintf (stdout, "%Lf %d\n", 0.0L, 33, 44, 55);
107
108   /* Negative zero.  */
109   my_fprintf (stdout, "%Lf %d\n", -0.0L, 33, 44, 55);
110
111   { /* NaN.  */
112     static long double zero = 0.0L;
113     my_fprintf (stdout, "%Lf %d\n", zero / zero, 33, 44, 55);
114   }
115
116   /* FLAG_ZERO.  */
117   my_fprintf (stdout, "%015Lf %d\n", 1234.0L, 33, 44, 55);
118
119   /* Precision.  */
120   my_fprintf (stdout, "%.Lf %d\n", 1234.0L, 33, 44, 55);
121
122   /* Test the support of the %F format directive.  */
123
124   /* A positive number.  */
125   my_fprintf (stdout, "%F %d\n", 12.75, 33, 44, 55);
126
127   /* A larger positive number.  */
128   my_fprintf (stdout, "%F %d\n", 1234567.0, 33, 44, 55);
129
130   /* A negative number.  */
131   my_fprintf (stdout, "%F %d\n", -0.03125, 33, 44, 55);
132
133   /* Positive zero.  */
134   my_fprintf (stdout, "%F %d\n", 0.0, 33, 44, 55);
135
136   /* Negative zero.  */
137   my_fprintf (stdout, "%F %d\n", -0.0, 33, 44, 55);
138
139   /* NaN.  */
140   my_fprintf (stdout, "%F %d\n", NaN (), 33, 44, 55);
141
142   /* FLAG_ZERO.  */
143   my_fprintf (stdout, "%015F %d\n", 1234.0, 33, 44, 55);
144
145   /* Precision.  */
146   my_fprintf (stdout, "%.F %d\n", 1234.0, 33, 44, 55);
147
148   /* A positive number.  */
149   my_fprintf (stdout, "%LF %d\n", 12.75L, 33, 44, 55);
150
151   /* A larger positive number.  */
152   my_fprintf (stdout, "%LF %d\n", 1234567.0L, 33, 44, 55);
153
154   /* A negative number.  */
155   my_fprintf (stdout, "%LF %d\n", -0.03125L, 33, 44, 55);
156
157   /* Positive zero.  */
158   my_fprintf (stdout, "%LF %d\n", 0.0L, 33, 44, 55);
159
160   /* Negative zero.  */
161   my_fprintf (stdout, "%LF %d\n", -0.0L, 33, 44, 55);
162
163   { /* NaN.  */
164     static long double zero = 0.0L;
165     my_fprintf (stdout, "%LF %d\n", zero / zero, 33, 44, 55);
166   }
167
168   /* FLAG_ZERO.  */
169   my_fprintf (stdout, "%015LF %d\n", 1234.0L, 33, 44, 55);
170
171   /* Precision.  */
172   my_fprintf (stdout, "%.LF %d\n", 1234.0L, 33, 44, 55);
173
174   /* Test the support of the POSIX/XSI format strings with positions.  */
175
176   my_fprintf (stdout, "%2$d %1$d\n", 33, 55);
177 }