1 /* Test of POSIX compatible vsprintf() and sprintf() functions.
2    Copyright (C) 2007-2018 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 <https://www.gnu.org/licenses/>.  */
16 
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
18 
19 #include "infinity.h"
20 
21 static void
test_function(int (* my_printf)(const char *,...))22 test_function (int (*my_printf) (const char *, ...))
23 {
24   /* Here we don't test output that may be platform dependent.
25      The bulk of the tests is done as part of the 'vasnprintf-posix' module.  */
26 
27   /* Test support of size specifiers as in C99.  */
28 
29   my_printf ("%ju %d\n", (uintmax_t) 12345671, 33, 44, 55);
30 
31   my_printf ("%zu %d\n", (size_t) 12345672, 33, 44, 55);
32 
33   my_printf ("%tu %d\n", (ptrdiff_t) 12345673, 33, 44, 55);
34 
35   /* Test the support of the 'a' and 'A' conversion specifier for hexadecimal
36      output of floating-point numbers.  */
37 
38   /* Positive zero.  */
39   my_printf ("%a %d\n", 0.0, 33, 44, 55);
40 
41   /* Positive infinity.  */
42   my_printf ("%a %d\n", Infinityd (), 33, 44, 55);
43 
44   /* Negative infinity.  */
45   my_printf ("%a %d\n", - Infinityd (), 33, 44, 55);
46 
47   /* FLAG_ZERO with infinite number.  */
48   /* "0000000inf 33" is not a valid result; see
49      <https://lists.gnu.org/r/bug-gnulib/2007-04/msg00107.html> */
50   my_printf ("%010a %d\n", Infinityd (), 33, 44, 55);
51 
52   /* Test the support of the %f format directive.  */
53 
54   /* A positive number.  */
55   my_printf ("%f %d\n", 12.75, 33, 44, 55);
56 
57   /* A larger positive number.  */
58   my_printf ("%f %d\n", 1234567.0, 33, 44, 55);
59 
60   /* A negative number.  */
61   my_printf ("%f %d\n", -0.03125, 33, 44, 55);
62 
63   /* Positive zero.  */
64   my_printf ("%f %d\n", 0.0, 33, 44, 55);
65 
66   /* FLAG_ZERO.  */
67   my_printf ("%015f %d\n", 1234.0, 33, 44, 55);
68 
69   /* Precision.  */
70   my_printf ("%.f %d\n", 1234.0, 33, 44, 55);
71 
72   /* Precision with no rounding.  */
73   my_printf ("%.2f %d\n", 999.95, 33, 44, 55);
74 
75   /* Precision with rounding.  */
76   my_printf ("%.2f %d\n", 999.996, 33, 44, 55);
77 
78   /* A positive number.  */
79   my_printf ("%Lf %d\n", 12.75L, 33, 44, 55);
80 
81   /* A larger positive number.  */
82   my_printf ("%Lf %d\n", 1234567.0L, 33, 44, 55);
83 
84   /* A negative number.  */
85   my_printf ("%Lf %d\n", -0.03125L, 33, 44, 55);
86 
87   /* Positive zero.  */
88   my_printf ("%Lf %d\n", 0.0L, 33, 44, 55);
89 
90   /* FLAG_ZERO.  */
91   my_printf ("%015Lf %d\n", 1234.0L, 33, 44, 55);
92 
93   /* Precision.  */
94   my_printf ("%.Lf %d\n", 1234.0L, 33, 44, 55);
95 
96   /* Precision with no rounding.  */
97   my_printf ("%.2Lf %d\n", 999.95L, 33, 44, 55);
98 
99   /* Precision with rounding.  */
100   my_printf ("%.2Lf %d\n", 999.996L, 33, 44, 55);
101 
102   /* Test the support of the %F format directive.  */
103 
104   /* A positive number.  */
105   my_printf ("%F %d\n", 12.75, 33, 44, 55);
106 
107   /* A larger positive number.  */
108   my_printf ("%F %d\n", 1234567.0, 33, 44, 55);
109 
110   /* A negative number.  */
111   my_printf ("%F %d\n", -0.03125, 33, 44, 55);
112 
113   /* Positive zero.  */
114   my_printf ("%F %d\n", 0.0, 33, 44, 55);
115 
116   /* FLAG_ZERO.  */
117   my_printf ("%015F %d\n", 1234.0, 33, 44, 55);
118 
119   /* Precision.  */
120   my_printf ("%.F %d\n", 1234.0, 33, 44, 55);
121 
122   /* Precision with no rounding.  */
123   my_printf ("%.2F %d\n", 999.95, 33, 44, 55);
124 
125   /* Precision with rounding.  */
126   my_printf ("%.2F %d\n", 999.996, 33, 44, 55);
127 
128   /* A positive number.  */
129   my_printf ("%LF %d\n", 12.75L, 33, 44, 55);
130 
131   /* A larger positive number.  */
132   my_printf ("%LF %d\n", 1234567.0L, 33, 44, 55);
133 
134   /* A negative number.  */
135   my_printf ("%LF %d\n", -0.03125L, 33, 44, 55);
136 
137   /* Positive zero.  */
138   my_printf ("%LF %d\n", 0.0L, 33, 44, 55);
139 
140   /* FLAG_ZERO.  */
141   my_printf ("%015LF %d\n", 1234.0L, 33, 44, 55);
142 
143   /* Precision.  */
144   my_printf ("%.LF %d\n", 1234.0L, 33, 44, 55);
145 
146   /* Precision with no rounding.  */
147   my_printf ("%.2LF %d\n", 999.95L, 33, 44, 55);
148 
149   /* Precision with rounding.  */
150   my_printf ("%.2LF %d\n", 999.996L, 33, 44, 55);
151 
152   /* Test the support of the POSIX/XSI format strings with positions.  */
153 
154   my_printf ("%2$d %1$d\n", 33, 55);
155 }
156