summaryrefslogtreecommitdiff
path: root/tests/test-snprintf-gnu.h
blob: eda2c803343c3029e4baaf4a0dbe372e4c268fa9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* Test of POSIX and GNU compatible vsnprintf() and snprintf() functions.
   Copyright (C) 2007-2023 Free Software Foundation, Inc.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */

/* Written by Bruno Haible <bruno@clisp.org>, 2023.  */

static void
test_function (int (*my_snprintf) (char *, size_t, const char *, ...))
{
  char result[5000];

  /* Test the support of the 'B' conversion specifier for binary output of
     integers.  */

  { /* Zero.  */
    int retval =
      my_snprintf (result, sizeof (result), "%B %d", 0, 33, 44, 55);
    ASSERT (strcmp (result, "0 33") == 0);
    ASSERT (retval == strlen (result));
  }

  { /* A positive number.  */
    int retval =
      my_snprintf (result, sizeof (result), "%B %d", 12345, 33, 44, 55);
    ASSERT (strcmp (result, "11000000111001 33") == 0);
    ASSERT (retval == strlen (result));
  }

  { /* A large positive number.  */
    int retval =
      my_snprintf (result, sizeof (result), "%B %d", 0xFFFFFFFEU, 33, 44, 55);
    ASSERT (strcmp (result, "11111111111111111111111111111110 33") == 0);
    ASSERT (retval == strlen (result));
  }

  { /* Width.  */
    int retval =
      my_snprintf (result, sizeof (result), "%20B %d", 12345, 33, 44, 55);
    ASSERT (strcmp (result, "      11000000111001 33") == 0);
    ASSERT (retval == strlen (result));
  }

  { /* Width given as argument.  */
    int retval =
      my_snprintf (result, sizeof (result), "%*B %d", 20, 12345, 33, 44, 55);
    ASSERT (strcmp (result, "      11000000111001 33") == 0);
    ASSERT (retval == strlen (result));
  }

  { /* Negative width given as argument (cf. FLAG_LEFT below).  */
    int retval =
      my_snprintf (result, sizeof (result), "%*B %d", -20, 12345, 33, 44, 55);
    ASSERT (strcmp (result, "11000000111001       33") == 0);
    ASSERT (retval == strlen (result));
  }

  { /* Precision.  */
    int retval =
      my_snprintf (result, sizeof (result), "%.20B %d", 12345, 33, 44, 55);
    ASSERT (strcmp (result, "00000011000000111001 33") == 0);
    ASSERT (retval == strlen (result));
  }

  { /* Zero precision and a positive number.  */
    int retval =
      my_snprintf (result, sizeof (result), "%.0B %d", 12345, 33, 44, 55);
    ASSERT (strcmp (result, "11000000111001 33") == 0);
    ASSERT (retval == strlen (result));
  }

  { /* Zero precision and a zero number.  */
    int retval =
      my_snprintf (result, sizeof (result), "%.0B %d", 0, 33, 44, 55);
    /* ISO C and POSIX specify that "The result of converting a zero value
       with a precision of zero is no characters."  */
    ASSERT (strcmp (result, " 33") == 0);
    ASSERT (retval == strlen (result));
  }

  { /* Width and precision.  */
    int retval =
      my_snprintf (result, sizeof (result), "%25.20B %d", 12345, 33, 44, 55);
    ASSERT (strcmp (result, "     00000011000000111001 33") == 0);
    ASSERT (retval == strlen (result));
  }

  { /* Padding and precision.  */
    int retval =
      my_snprintf (result, sizeof (result), "%025.20B %d", 12345, 33, 44, 55);
    /* Neither ISO C nor POSIX specify that the '0' flag is ignored when
       a width and a precision are both present.  But implementations do so.  */
    ASSERT (strcmp (result, "     00000011000000111001 33") == 0);
    ASSERT (retval == strlen (result));
  }

  { /* FLAG_LEFT.  */
    int retval =
      my_snprintf (result, sizeof (result), "%-20B %d", 12345, 33, 44, 55);
    ASSERT (strcmp (result, "11000000111001       33") == 0);
    ASSERT (retval == strlen (result));
  }

  { /* FLAG_ALT with zero.  */
    int retval =
      my_snprintf (result, sizeof (result), "%#B %d", 0, 33, 44, 55);
    ASSERT (strcmp (result, "0 33") == 0);
    ASSERT (retval == strlen (result));
  }

  { /* FLAG_ALT with a positive number.  */
    int retval =
      my_snprintf (result, sizeof (result), "%#B %d", 12345, 33, 44, 55);
    ASSERT (strcmp (result, "0B11000000111001 33") == 0);
    ASSERT (retval == strlen (result));
  }

  { /* FLAG_ALT with a positive number and width.  */
    int retval =
      my_snprintf (result, sizeof (result), "%#20B %d", 12345, 33, 44, 55);
    ASSERT (strcmp (result, "    0B11000000111001 33") == 0);
    ASSERT (retval == strlen (result));
  }

  { /* FLAG_ALT with a positive number and padding.  */
    int retval =
      my_snprintf (result, sizeof (result), "%0#20B %d", 12345, 33, 44, 55);
    ASSERT (strcmp (result, "0B000011000000111001 33") == 0);
    ASSERT (retval == strlen (result));
  }

  { /* FLAG_ALT with a positive number and precision.  */
    int retval =
      my_snprintf (result, sizeof (result), "%0#.20B %d", 12345, 33, 44, 55);
    ASSERT (strcmp (result, "0B00000011000000111001 33") == 0);
    ASSERT (retval == strlen (result));
  }

  { /* FLAG_ALT with a positive number and width and precision.  */
    int retval =
      my_snprintf (result, sizeof (result), "%#25.20B %d", 12345, 33, 44, 55);
    ASSERT (strcmp (result, "   0B00000011000000111001 33") == 0);
    ASSERT (retval == strlen (result));
  }

  { /* FLAG_ALT with a positive number and padding and precision.  */
    int retval =
      my_snprintf (result, sizeof (result), "%0#25.20B %d", 12345, 33, 44, 55);
    /* Neither ISO C nor POSIX specify that the '0' flag is ignored when
       a width and a precision are both present.  But implementations do so.  */
    ASSERT (strcmp (result, "   0B00000011000000111001 33") == 0);
    ASSERT (retval == strlen (result));
  }

  { /* FLAG_ALT with a zero precision and a zero number.  */
    int retval =
      my_snprintf (result, sizeof (result), "%#.0B %d", 0, 33, 44, 55);
    /* ISO C and POSIX specify that "The result of converting a zero value
       with a precision of zero is no characters.", and the prefix is added
       only for non-zero values.  */
    ASSERT (strcmp (result, " 33") == 0);
    ASSERT (retval == strlen (result));
  }
}