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