Tests for module 'dprintf-posix'.
[gnulib.git] / tests / test-dprintf-posix.c
1 /* Test of POSIX compatible dprintf() function.
2    Copyright (C) 2007-2009 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 3 of the License, or
7    (at your option) 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, see <http://www.gnu.org/licenses/>.  */
16
17 /* Written by Bruno Haible <bruno@clisp.org>, 2009.  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22
23 #include <stddef.h>
24 #include <stdint.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #define ASSERT(expr) \
29   do                                                                         \
30     {                                                                        \
31       if (!(expr))                                                           \
32         {                                                                    \
33           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
34           fflush (stderr);                                                   \
35           abort ();                                                          \
36         }                                                                    \
37     }                                                                        \
38   while (0)
39
40 static void
41 test_function (int (*my_dprintf) (int, const char *, ...))
42 {
43   /* Here we don't test output that may be platform dependent.
44      The bulk of the tests is done as part of the 'vasnprintf-posix' module.  */
45
46   /* Test support of size specifiers as in C99.  */
47
48   my_dprintf (fileno (stdout), "%ju %d\n", (uintmax_t) 12345671, 33, 44, 55);
49
50   my_dprintf (fileno (stdout), "%zu %d\n", (size_t) 12345672, 33, 44, 55);
51
52   my_dprintf (fileno (stdout), "%tu %d\n", (ptrdiff_t) 12345673, 33, 44, 55);
53
54   /* Test the support of the 'a' and 'A' conversion specifier for hexadecimal
55      output of floating-point numbers.  */
56
57   /* Positive zero.  */
58   my_dprintf (fileno (stdout), "%a %d\n", 0.0, 33, 44, 55);
59
60   /* Positive infinity.  */
61   my_dprintf (fileno (stdout), "%a %d\n", 1.0 / 0.0, 33, 44, 55);
62
63   /* Negative infinity.  */
64   my_dprintf (fileno (stdout), "%a %d\n", -1.0 / 0.0, 33, 44, 55);
65
66   /* FLAG_ZERO with infinite number.  */
67   my_dprintf (fileno (stdout), "%010a %d\n", 1.0 / 0.0, 33, 44, 55);
68
69   /* Test the support of the %f format directive.  */
70
71   /* A positive number.  */
72   my_dprintf (fileno (stdout), "%f %d\n", 12.75, 33, 44, 55);
73
74   /* A larger positive number.  */
75   my_dprintf (fileno (stdout), "%f %d\n", 1234567.0, 33, 44, 55);
76
77   /* A negative number.  */
78   my_dprintf (fileno (stdout), "%f %d\n", -0.03125, 33, 44, 55);
79
80   /* Positive zero.  */
81   my_dprintf (fileno (stdout), "%f %d\n", 0.0, 33, 44, 55);
82
83   /* FLAG_ZERO.  */
84   my_dprintf (fileno (stdout), "%015f %d\n", 1234.0, 33, 44, 55);
85
86   /* Precision.  */
87   my_dprintf (fileno (stdout), "%.f %d\n", 1234.0, 33, 44, 55);
88
89   /* Precision with no rounding.  */
90   my_dprintf (fileno (stdout), "%.2f %d\n", 999.95, 33, 44, 55);
91
92   /* Precision with rounding.  */
93   my_dprintf (fileno (stdout), "%.2f %d\n", 999.996, 33, 44, 55);
94
95   /* A positive number.  */
96   my_dprintf (fileno (stdout), "%Lf %d\n", 12.75L, 33, 44, 55);
97
98   /* A larger positive number.  */
99   my_dprintf (fileno (stdout), "%Lf %d\n", 1234567.0L, 33, 44, 55);
100
101   /* A negative number.  */
102   my_dprintf (fileno (stdout), "%Lf %d\n", -0.03125L, 33, 44, 55);
103
104   /* Positive zero.  */
105   my_dprintf (fileno (stdout), "%Lf %d\n", 0.0L, 33, 44, 55);
106
107   /* FLAG_ZERO.  */
108   my_dprintf (fileno (stdout), "%015Lf %d\n", 1234.0L, 33, 44, 55);
109
110   /* Precision.  */
111   my_dprintf (fileno (stdout), "%.Lf %d\n", 1234.0L, 33, 44, 55);
112
113   /* Precision with no rounding.  */
114   my_dprintf (fileno (stdout), "%.2Lf %d\n", 999.95L, 33, 44, 55);
115
116   /* Precision with rounding.  */
117   my_dprintf (fileno (stdout), "%.2Lf %d\n", 999.996L, 33, 44, 55);
118
119   /* Test the support of the %F format directive.  */
120
121   /* A positive number.  */
122   my_dprintf (fileno (stdout), "%F %d\n", 12.75, 33, 44, 55);
123
124   /* A larger positive number.  */
125   my_dprintf (fileno (stdout), "%F %d\n", 1234567.0, 33, 44, 55);
126
127   /* A negative number.  */
128   my_dprintf (fileno (stdout), "%F %d\n", -0.03125, 33, 44, 55);
129
130   /* Positive zero.  */
131   my_dprintf (fileno (stdout), "%F %d\n", 0.0, 33, 44, 55);
132
133   /* FLAG_ZERO.  */
134   my_dprintf (fileno (stdout), "%015F %d\n", 1234.0, 33, 44, 55);
135
136   /* Precision.  */
137   my_dprintf (fileno (stdout), "%.F %d\n", 1234.0, 33, 44, 55);
138
139   /* Precision with no rounding.  */
140   my_dprintf (fileno (stdout), "%.2F %d\n", 999.95, 33, 44, 55);
141
142   /* Precision with rounding.  */
143   my_dprintf (fileno (stdout), "%.2F %d\n", 999.996, 33, 44, 55);
144
145   /* A positive number.  */
146   my_dprintf (fileno (stdout), "%LF %d\n", 12.75L, 33, 44, 55);
147
148   /* A larger positive number.  */
149   my_dprintf (fileno (stdout), "%LF %d\n", 1234567.0L, 33, 44, 55);
150
151   /* A negative number.  */
152   my_dprintf (fileno (stdout), "%LF %d\n", -0.03125L, 33, 44, 55);
153
154   /* Positive zero.  */
155   my_dprintf (fileno (stdout), "%LF %d\n", 0.0L, 33, 44, 55);
156
157   /* FLAG_ZERO.  */
158   my_dprintf (fileno (stdout), "%015LF %d\n", 1234.0L, 33, 44, 55);
159
160   /* Precision.  */
161   my_dprintf (fileno (stdout), "%.LF %d\n", 1234.0L, 33, 44, 55);
162
163   /* Precision with no rounding.  */
164   my_dprintf (fileno (stdout), "%.2LF %d\n", 999.95L, 33, 44, 55);
165
166   /* Precision with rounding.  */
167   my_dprintf (fileno (stdout), "%.2LF %d\n", 999.996L, 33, 44, 55);
168
169   /* Test the support of the POSIX/XSI format strings with positions.  */
170
171   my_dprintf (fileno (stdout), "%2$d %1$d\n", 33, 55);
172 }
173
174 int
175 main (int argc, char *argv[])
176 {
177   test_function (dprintf);
178   return 0;
179 }