usleep tests: Avoid failure due to known Cygwin 3.5.3 bug.
[gnulib.git] / tests / test-xvasprintf.c
blob038b0c242bad4d67e6a243fc3539b6beac3e1d8a
1 /* Test of xvasprintf() and xasprintf() 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>, 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"
24 #endif
26 #include <config.h>
28 #include "xvasprintf.h"
30 #include <stdarg.h>
31 #include <stdlib.h>
32 #include <string.h>
34 #include "macros.h"
36 static char *
37 my_xasprintf (const char *format, ...)
39 va_list args;
40 char *ret;
42 va_start (args, format);
43 ret = xvasprintf (format, args);
44 va_end (args);
45 return ret;
48 static void
49 test_xvasprintf (void)
51 int repeat;
52 char *result;
54 for (repeat = 0; repeat <= 8; repeat++)
56 result = my_xasprintf ("%d", 12345);
57 ASSERT (result != NULL);
58 ASSERT (strcmp (result, "12345") == 0);
59 free (result);
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);
68 free (result);
71 result = my_xasprintf ("%s", "foo");
72 ASSERT (result != NULL);
73 ASSERT (strcmp (result, "foo") == 0);
74 free (result);
76 result = my_xasprintf ("%s%s", "foo", "bar");
77 ASSERT (result != NULL);
78 ASSERT (strcmp (result, "foobar") == 0);
79 free (result);
81 result = my_xasprintf ("%s%sbaz", "foo", "bar");
82 ASSERT (result != NULL);
83 ASSERT (strcmp (result, "foobarbaz") == 0);
84 free (result);
87 static void
88 test_xasprintf (void)
90 int repeat;
91 char *result;
93 for (repeat = 0; repeat <= 8; repeat++)
95 result = xasprintf ("%d", 12345);
96 ASSERT (result != NULL);
97 ASSERT (strcmp (result, "12345") == 0);
98 free (result);
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);
109 free (result);
112 result = xasprintf ("%s", "foo");
113 ASSERT (result != NULL);
114 ASSERT (strcmp (result, "foo") == 0);
115 free (result);
117 result = xasprintf ("%s%s", "foo", "bar");
118 ASSERT (result != NULL);
119 ASSERT (strcmp (result, "foobar") == 0);
120 free (result);
122 result = my_xasprintf ("%s%sbaz", "foo", "bar");
123 ASSERT (result != NULL);
124 ASSERT (strcmp (result, "foobarbaz") == 0);
125 free (result);
129 main (_GL_UNUSED int argc, char *argv[])
131 test_xvasprintf ();
132 test_xasprintf ();
134 return test_exit_status;