1 /* Test of xvasprintf() and xasprintf() functions.
2 Copyright (C) 2007-2020 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>, 2007. */
19 /* Tell GCC not to warn about the specific edge cases tested here. */
20 #if (__GNUC__ == 4 && 3 <= __GNUC_MINOR__) || 4 < __GNUC__
21 # pragma GCC diagnostic ignored "-Wformat-zero-length"
22 # pragma GCC diagnostic ignored "-Wformat-nonliteral"
23 # pragma GCC diagnostic ignored "-Wformat-security"
28 #include "xvasprintf.h"
37 my_xasprintf (const char *format
, ...)
42 va_start (args
, format
);
43 ret
= xvasprintf (format
, args
);
49 test_xvasprintf (void)
54 for (repeat
= 0; repeat
<= 8; repeat
++)
56 result
= my_xasprintf ("%d", 12345);
57 ASSERT (result
!= NULL
);
58 ASSERT (strcmp (result
, "12345") == 0);
63 /* Silence gcc warning about zero-length format string. */
64 const char *empty
= "";
65 result
= my_xasprintf (empty
);
66 ASSERT (result
!= NULL
);
67 ASSERT (strcmp (result
, "") == 0);
71 result
= my_xasprintf ("%s", "foo");
72 ASSERT (result
!= NULL
);
73 ASSERT (strcmp (result
, "foo") == 0);
76 result
= my_xasprintf ("%s%s", "foo", "bar");
77 ASSERT (result
!= NULL
);
78 ASSERT (strcmp (result
, "foobar") == 0);
81 result
= my_xasprintf ("%s%sbaz", "foo", "bar");
82 ASSERT (result
!= NULL
);
83 ASSERT (strcmp (result
, "foobarbaz") == 0);
93 for (repeat
= 0; repeat
<= 8; repeat
++)
95 result
= xasprintf ("%d", 12345);
96 ASSERT (result
!= NULL
);
97 ASSERT (strcmp (result
, "12345") == 0);
102 /* Silence gcc warning about zero-length format string,
103 and about "format not a string literal and no format"
104 (whatever that means) . */
105 const char *empty
= "";
106 result
= xasprintf (empty
, empty
);
107 ASSERT (result
!= NULL
);
108 ASSERT (strcmp (result
, "") == 0);
112 result
= xasprintf ("%s", "foo");
113 ASSERT (result
!= NULL
);
114 ASSERT (strcmp (result
, "foo") == 0);
117 result
= xasprintf ("%s%s", "foo", "bar");
118 ASSERT (result
!= NULL
);
119 ASSERT (strcmp (result
, "foobar") == 0);
122 result
= my_xasprintf ("%s%sbaz", "foo", "bar");
123 ASSERT (result
!= NULL
);
124 ASSERT (strcmp (result
, "foobarbaz") == 0);
129 main (int argc _GL_UNUSED
, char *argv
[])