|
Packit |
33f14e |
/* Test of xvasprintf() and xasprintf() functions.
|
|
Packit |
33f14e |
Copyright (C) 2007-2017 Free Software Foundation, Inc.
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
This program is free software: you can redistribute it and/or modify
|
|
Packit |
33f14e |
it under the terms of the GNU General Public License as published by
|
|
Packit |
33f14e |
the Free Software Foundation; either version 3 of the License, or
|
|
Packit |
33f14e |
(at your option) any later version.
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
This program is distributed in the hope that it will be useful,
|
|
Packit |
33f14e |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
33f14e |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit |
33f14e |
GNU General Public License for more details.
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
You should have received a copy of the GNU General Public License
|
|
Packit |
33f14e |
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
/* Written by Bruno Haible <bruno@clisp.org>, 2007. */
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
/* Tell GCC not to warn about the specific edge cases tested here. */
|
|
Packit |
33f14e |
#if (__GNUC__ == 4 && 3 <= __GNUC_MINOR__) || 4 < __GNUC__
|
|
Packit |
33f14e |
# pragma GCC diagnostic ignored "-Wformat-zero-length"
|
|
Packit |
33f14e |
# pragma GCC diagnostic ignored "-Wformat-nonliteral"
|
|
Packit |
33f14e |
# pragma GCC diagnostic ignored "-Wformat-security"
|
|
Packit |
33f14e |
#endif
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
#include <config.h>
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
#include "xvasprintf.h"
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
#include <stdarg.h>
|
|
Packit |
33f14e |
#include <stdlib.h>
|
|
Packit |
33f14e |
#include <string.h>
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
#include "macros.h"
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
static char *
|
|
Packit |
33f14e |
my_xasprintf (const char *format, ...)
|
|
Packit |
33f14e |
{
|
|
Packit |
33f14e |
va_list args;
|
|
Packit |
33f14e |
char *ret;
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
va_start (args, format);
|
|
Packit |
33f14e |
ret = xvasprintf (format, args);
|
|
Packit |
33f14e |
va_end (args);
|
|
Packit |
33f14e |
return ret;
|
|
Packit |
33f14e |
}
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
static void
|
|
Packit |
33f14e |
test_xvasprintf (void)
|
|
Packit |
33f14e |
{
|
|
Packit |
33f14e |
int repeat;
|
|
Packit |
33f14e |
char *result;
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
for (repeat = 0; repeat <= 8; repeat++)
|
|
Packit |
33f14e |
{
|
|
Packit |
33f14e |
result = my_xasprintf ("%d", 12345);
|
|
Packit |
33f14e |
ASSERT (result != NULL);
|
|
Packit |
33f14e |
ASSERT (strcmp (result, "12345") == 0);
|
|
Packit |
33f14e |
free (result);
|
|
Packit |
33f14e |
}
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
{
|
|
Packit |
33f14e |
/* Silence gcc warning about zero-length format string. */
|
|
Packit |
33f14e |
const char *empty = "";
|
|
Packit |
33f14e |
result = my_xasprintf (empty);
|
|
Packit |
33f14e |
ASSERT (result != NULL);
|
|
Packit |
33f14e |
ASSERT (strcmp (result, "") == 0);
|
|
Packit |
33f14e |
free (result);
|
|
Packit |
33f14e |
}
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
result = my_xasprintf ("%s", "foo");
|
|
Packit |
33f14e |
ASSERT (result != NULL);
|
|
Packit |
33f14e |
ASSERT (strcmp (result, "foo") == 0);
|
|
Packit |
33f14e |
free (result);
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
result = my_xasprintf ("%s%s", "foo", "bar");
|
|
Packit |
33f14e |
ASSERT (result != NULL);
|
|
Packit |
33f14e |
ASSERT (strcmp (result, "foobar") == 0);
|
|
Packit |
33f14e |
free (result);
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
result = my_xasprintf ("%s%sbaz", "foo", "bar");
|
|
Packit |
33f14e |
ASSERT (result != NULL);
|
|
Packit |
33f14e |
ASSERT (strcmp (result, "foobarbaz") == 0);
|
|
Packit |
33f14e |
free (result);
|
|
Packit |
33f14e |
}
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
static void
|
|
Packit |
33f14e |
test_xasprintf (void)
|
|
Packit |
33f14e |
{
|
|
Packit |
33f14e |
int repeat;
|
|
Packit |
33f14e |
char *result;
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
for (repeat = 0; repeat <= 8; repeat++)
|
|
Packit |
33f14e |
{
|
|
Packit |
33f14e |
result = xasprintf ("%d", 12345);
|
|
Packit |
33f14e |
ASSERT (result != NULL);
|
|
Packit |
33f14e |
ASSERT (strcmp (result, "12345") == 0);
|
|
Packit |
33f14e |
free (result);
|
|
Packit |
33f14e |
}
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
{
|
|
Packit |
33f14e |
/* Silence gcc warning about zero-length format string,
|
|
Packit |
33f14e |
and about "format not a string literal and no format"
|
|
Packit |
33f14e |
(whatever that means) . */
|
|
Packit |
33f14e |
const char *empty = "";
|
|
Packit |
33f14e |
result = xasprintf (empty, empty);
|
|
Packit |
33f14e |
ASSERT (result != NULL);
|
|
Packit |
33f14e |
ASSERT (strcmp (result, "") == 0);
|
|
Packit |
33f14e |
free (result);
|
|
Packit |
33f14e |
}
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
result = xasprintf ("%s", "foo");
|
|
Packit |
33f14e |
ASSERT (result != NULL);
|
|
Packit |
33f14e |
ASSERT (strcmp (result, "foo") == 0);
|
|
Packit |
33f14e |
free (result);
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
result = xasprintf ("%s%s", "foo", "bar");
|
|
Packit |
33f14e |
ASSERT (result != NULL);
|
|
Packit |
33f14e |
ASSERT (strcmp (result, "foobar") == 0);
|
|
Packit |
33f14e |
free (result);
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
result = my_xasprintf ("%s%sbaz", "foo", "bar");
|
|
Packit |
33f14e |
ASSERT (result != NULL);
|
|
Packit |
33f14e |
ASSERT (strcmp (result, "foobarbaz") == 0);
|
|
Packit |
33f14e |
free (result);
|
|
Packit |
33f14e |
}
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
int
|
|
Packit |
33f14e |
main (int argc _GL_UNUSED, char *argv[])
|
|
Packit |
33f14e |
{
|
|
Packit |
33f14e |
test_xvasprintf ();
|
|
Packit |
33f14e |
test_xasprintf ();
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
return 0;
|
|
Packit |
33f14e |
}
|