openat: don’t close (-1)
[gnulib.git] / tests / test-sprintf-gnu.h
blob80a2833ac92cf3536884db0608e1d72146f8fae3
1 /* Test of POSIX and GNU compatible vsprintf() and sprintf() functions.
2 Copyright (C) 2007-2024 Free Software Foundation, Inc.
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.
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.
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/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2023. */
19 static void
20 test_function (int (*my_sprintf) (char *, const char *, ...))
22 char result[5000];
24 /* Test the support of the 'B' conversion specifier for binary output of
25 integers. */
27 { /* Zero. */
28 int retval =
29 my_sprintf (result, "%B %d", 0, 33, 44, 55);
30 ASSERT (strcmp (result, "0 33") == 0);
31 ASSERT (retval == strlen (result));
34 { /* A positive number. */
35 int retval =
36 my_sprintf (result, "%B %d", 12345, 33, 44, 55);
37 ASSERT (strcmp (result, "11000000111001 33") == 0);
38 ASSERT (retval == strlen (result));
41 { /* A large positive number. */
42 int retval =
43 my_sprintf (result, "%B %d", 0xFFFFFFFEU, 33, 44, 55);
44 ASSERT (strcmp (result, "11111111111111111111111111111110 33") == 0);
45 ASSERT (retval == strlen (result));
48 { /* Width. */
49 int retval =
50 my_sprintf (result, "%20B %d", 12345, 33, 44, 55);
51 ASSERT (strcmp (result, " 11000000111001 33") == 0);
52 ASSERT (retval == strlen (result));
55 { /* Width given as argument. */
56 int retval =
57 my_sprintf (result, "%*B %d", 20, 12345, 33, 44, 55);
58 ASSERT (strcmp (result, " 11000000111001 33") == 0);
59 ASSERT (retval == strlen (result));
62 { /* Negative width given as argument (cf. FLAG_LEFT below). */
63 int retval =
64 my_sprintf (result, "%*B %d", -20, 12345, 33, 44, 55);
65 ASSERT (strcmp (result, "11000000111001 33") == 0);
66 ASSERT (retval == strlen (result));
69 { /* Precision. */
70 int retval =
71 my_sprintf (result, "%.20B %d", 12345, 33, 44, 55);
72 ASSERT (strcmp (result, "00000011000000111001 33") == 0);
73 ASSERT (retval == strlen (result));
76 { /* Zero precision and a positive number. */
77 int retval =
78 my_sprintf (result, "%.0B %d", 12345, 33, 44, 55);
79 ASSERT (strcmp (result, "11000000111001 33") == 0);
80 ASSERT (retval == strlen (result));
83 { /* Zero precision and a zero number. */
84 int retval =
85 my_sprintf (result, "%.0B %d", 0, 33, 44, 55);
86 /* ISO C and POSIX specify that "The result of converting a zero value
87 with a precision of zero is no characters." */
88 ASSERT (strcmp (result, " 33") == 0);
89 ASSERT (retval == strlen (result));
92 { /* Width and precision. */
93 int retval =
94 my_sprintf (result, "%25.20B %d", 12345, 33, 44, 55);
95 ASSERT (strcmp (result, " 00000011000000111001 33") == 0);
96 ASSERT (retval == strlen (result));
99 { /* Padding and precision. */
100 int retval =
101 my_sprintf (result, "%025.20B %d", 12345, 33, 44, 55);
102 /* Neither ISO C nor POSIX specify that the '0' flag is ignored when
103 a width and a precision are both present. But implementations do so. */
104 ASSERT (strcmp (result, " 00000011000000111001 33") == 0);
105 ASSERT (retval == strlen (result));
108 { /* FLAG_LEFT. */
109 int retval =
110 my_sprintf (result, "%-20B %d", 12345, 33, 44, 55);
111 ASSERT (strcmp (result, "11000000111001 33") == 0);
112 ASSERT (retval == strlen (result));
115 { /* FLAG_ALT with zero. */
116 int retval =
117 my_sprintf (result, "%#B %d", 0, 33, 44, 55);
118 ASSERT (strcmp (result, "0 33") == 0);
119 ASSERT (retval == strlen (result));
122 { /* FLAG_ALT with a positive number. */
123 int retval =
124 my_sprintf (result, "%#B %d", 12345, 33, 44, 55);
125 ASSERT (strcmp (result, "0B11000000111001 33") == 0);
126 ASSERT (retval == strlen (result));
129 { /* FLAG_ALT with a positive number and width. */
130 int retval =
131 my_sprintf (result, "%#20B %d", 12345, 33, 44, 55);
132 ASSERT (strcmp (result, " 0B11000000111001 33") == 0);
133 ASSERT (retval == strlen (result));
136 { /* FLAG_ALT with a positive number and padding. */
137 int retval =
138 my_sprintf (result, "%0#20B %d", 12345, 33, 44, 55);
139 ASSERT (strcmp (result, "0B000011000000111001 33") == 0);
140 ASSERT (retval == strlen (result));
143 { /* FLAG_ALT with a positive number and precision. */
144 int retval =
145 my_sprintf (result, "%0#.20B %d", 12345, 33, 44, 55);
146 ASSERT (strcmp (result, "0B00000011000000111001 33") == 0);
147 ASSERT (retval == strlen (result));
150 { /* FLAG_ALT with a positive number and width and precision. */
151 int retval =
152 my_sprintf (result, "%#25.20B %d", 12345, 33, 44, 55);
153 ASSERT (strcmp (result, " 0B00000011000000111001 33") == 0);
154 ASSERT (retval == strlen (result));
157 { /* FLAG_ALT with a positive number and padding and precision. */
158 int retval =
159 my_sprintf (result, "%0#25.20B %d", 12345, 33, 44, 55);
160 /* Neither ISO C nor POSIX specify that the '0' flag is ignored when
161 a width and a precision are both present. But implementations do so. */
162 ASSERT (strcmp (result, " 0B00000011000000111001 33") == 0);
163 ASSERT (retval == strlen (result));
166 { /* FLAG_ALT with a zero precision and a zero number. */
167 int retval =
168 my_sprintf (result, "%#.0B %d", 0, 33, 44, 55);
169 /* ISO C and POSIX specify that "The result of converting a zero value
170 with a precision of zero is no characters.", and the prefix is added
171 only for non-zero values. */
172 ASSERT (strcmp (result, " 33") == 0);
173 ASSERT (retval == strlen (result));