/* Test of POSIX compatible vasnprintf() and asnprintf() functions.
- Copyright (C) 2007 Free Software Foundation, Inc.
+ Copyright (C) 2007-2008 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
#include <stdlib.h>
#include <string.h>
+#include "nan.h"
+
#define SIZEOF(array) (sizeof (array) / sizeof (array[0]))
#define ASSERT(expr) \
do \
if (!(expr)) \
{ \
fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
+ fflush (stderr); \
abort (); \
} \
} \
while (0)
-/* The Compaq (ex-DEC) C 6.4 compiler chokes on the expression 0.0 / 0.0. */
-#ifdef __DECC
-static double
-NaN ()
-{
- static double zero = 0.0;
- return zero / zero;
-}
-#else
-# define NaN() (0.0 / 0.0)
-#endif
-
/* The SGI MIPS floating-point format does not distinguish 0.0 and -0.0. */
static int
have_minus_zero ()
{
static double plus_zero = 0.0;
- static double minus_zero = -0.0;
+ double minus_zero = - plus_zero;
return memcmp (&plus_zero, &minus_zero, sizeof (double)) != 0;
}
+/* HP cc on HP-UX 10.20 has a bug with the constant expression -0.0.
+ So we use -zero instead. */
+double zerod = 0.0;
+
+/* HP cc on HP-UX 10.20 has a bug with the constant expression -0.0L.
+ So we use -zero instead. */
+long double zerol = 0.0L;
+
/* Representation of an 80-bit 'long double' as an initializer for a sequence
of 'unsigned int' words. */
#ifdef WORDS_BIGENDIAN
}
return 0;
}
-
+
static void
test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
{
{ /* Negative zero. */
size_t length;
char *result =
- my_asnprintf (NULL, &length, "%a %d", -0.0, 33, 44, 55);
+ my_asnprintf (NULL, &length, "%a %d", -zerod, 33, 44, 55);
ASSERT (result != NULL);
if (have_minus_zero ())
ASSERT (strcmp (result, "-0x0p+0 33") == 0);
{ /* NaN. */
size_t length;
char *result =
- my_asnprintf (NULL, &length, "%a %d", NaN (), 33, 44, 55);
+ my_asnprintf (NULL, &length, "%a %d", NaNd (), 33, 44, 55);
ASSERT (result != NULL);
ASSERT (strlen (result) >= 3 + 3
&& strisnan (result, 0, strlen (result) - 3, 0)
{ /* FLAG_ZERO with NaN. */
size_t length;
char *result =
- my_asnprintf (NULL, &length, "%050a %d", NaN (), 33, 44, 55);
+ my_asnprintf (NULL, &length, "%050a %d", NaNd (), 33, 44, 55);
ASSERT (result != NULL);
/* "0000000nan 33" is not a valid result; see
<http://lists.gnu.org/archive/html/bug-gnulib/2007-04/msg00107.html> */
{ /* Negative zero. */
size_t length;
char *result =
- my_asnprintf (NULL, &length, "%La %d", -0.0L, 33, 44, 55);
+ my_asnprintf (NULL, &length, "%La %d", -zerol, 33, 44, 55);
ASSERT (result != NULL);
if (have_minus_zero ())
ASSERT (strcmp (result, "-0x0p+0 33") == 0);
{ /* NaN. */
size_t length;
char *result =
- my_asnprintf (NULL, &length, "%La %d", 0.0L / 0.0L, 33, 44, 55);
+ my_asnprintf (NULL, &length, "%La %d", NaNl (), 33, 44, 55);
ASSERT (result != NULL);
ASSERT (strlen (result) >= 3 + 3
&& strisnan (result, 0, strlen (result) - 3, 0)
{ /* FLAG_ZERO with NaN. */
size_t length;
char *result =
- my_asnprintf (NULL, &length, "%050La %d", 0.0L / 0.0L, 33, 44, 55);
+ my_asnprintf (NULL, &length, "%050La %d", NaNl (), 33, 44, 55);
ASSERT (result != NULL);
/* "0000000nan 33" is not a valid result; see
<http://lists.gnu.org/archive/html/bug-gnulib/2007-04/msg00107.html> */
{ /* Negative zero. */
size_t length;
char *result =
- my_asnprintf (NULL, &length, "%f %d", -0.0, 33, 44, 55);
+ my_asnprintf (NULL, &length, "%f %d", -zerod, 33, 44, 55);
ASSERT (result != NULL);
if (have_minus_zero ())
ASSERT (strcmp (result, "-0.000000 33") == 0);
{ /* NaN. */
size_t length;
char *result =
- my_asnprintf (NULL, &length, "%f %d", NaN (), 33, 44, 55);
+ my_asnprintf (NULL, &length, "%f %d", NaNd (), 33, 44, 55);
ASSERT (result != NULL);
ASSERT (strlen (result) >= 3 + 3
&& strisnan (result, 0, strlen (result) - 3, 0)
{ /* FLAG_ZERO with NaN. */
size_t length;
char *result =
- my_asnprintf (NULL, &length, "%050f %d", NaN (), 33, 44, 55);
+ my_asnprintf (NULL, &length, "%050f %d", NaNd (), 33, 44, 55);
ASSERT (result != NULL);
ASSERT (strlen (result) == 50 + 3
&& strisnan (result, strspn (result, " "), strlen (result) - 3, 0)
free (result);
}
+ { /* Precision with no rounding. */
+ size_t length;
+ char *result =
+ my_asnprintf (NULL, &length, "%.2f %d", 999.951, 33, 44, 55);
+ ASSERT (result != NULL);
+ ASSERT (strcmp (result, "999.95 33") == 0);
+ ASSERT (length == strlen (result));
+ free (result);
+ }
+
+ { /* Precision with rounding. */
+ size_t length;
+ char *result =
+ my_asnprintf (NULL, &length, "%.2f %d", 999.996, 33, 44, 55);
+ ASSERT (result != NULL);
+ ASSERT (strcmp (result, "1000.00 33") == 0);
+ ASSERT (length == strlen (result));
+ free (result);
+ }
+
{ /* A positive number. */
size_t length;
char *result =
{ /* Negative zero. */
size_t length;
char *result =
- my_asnprintf (NULL, &length, "%Lf %d", -0.0L, 33, 44, 55);
+ my_asnprintf (NULL, &length, "%Lf %d", -zerol, 33, 44, 55);
ASSERT (result != NULL);
if (have_minus_zero ())
ASSERT (strcmp (result, "-0.000000 33") == 0);
}
{ /* NaN. */
- static long double zero = 0.0L;
size_t length;
char *result =
- my_asnprintf (NULL, &length, "%Lf %d", zero / zero, 33, 44, 55);
+ my_asnprintf (NULL, &length, "%Lf %d", NaNl (), 33, 44, 55);
ASSERT (result != NULL);
ASSERT (strlen (result) >= 3 + 3
&& strisnan (result, 0, strlen (result) - 3, 0)
}
{ /* FLAG_ZERO with NaN. */
- static long double zero = 0.0L;
size_t length;
char *result =
- my_asnprintf (NULL, &length, "%050Lf %d", zero / zero, 33, 44, 55);
+ my_asnprintf (NULL, &length, "%050Lf %d", NaNl (), 33, 44, 55);
ASSERT (result != NULL);
ASSERT (strlen (result) == 50 + 3
&& strisnan (result, strspn (result, " "), strlen (result) - 3, 0)
free (result);
}
+ { /* Precision with no rounding. */
+ size_t length;
+ char *result =
+ my_asnprintf (NULL, &length, "%.2Lf %d", 999.951L, 33, 44, 55);
+ ASSERT (result != NULL);
+ ASSERT (strcmp (result, "999.95 33") == 0);
+ ASSERT (length == strlen (result));
+ free (result);
+ }
+
+ { /* Precision with rounding. */
+ size_t length;
+ char *result =
+ my_asnprintf (NULL, &length, "%.2Lf %d", 999.996L, 33, 44, 55);
+ ASSERT (result != NULL);
+ ASSERT (strcmp (result, "1000.00 33") == 0);
+ ASSERT (length == strlen (result));
+ free (result);
+ }
+
/* Test the support of the %F format directive. */
{ /* A positive number. */
{ /* Negative zero. */
size_t length;
char *result =
- my_asnprintf (NULL, &length, "%F %d", -0.0, 33, 44, 55);
+ my_asnprintf (NULL, &length, "%F %d", -zerod, 33, 44, 55);
ASSERT (result != NULL);
if (have_minus_zero ())
ASSERT (strcmp (result, "-0.000000 33") == 0);
{ /* NaN. */
size_t length;
char *result =
- my_asnprintf (NULL, &length, "%F %d", NaN (), 33, 44, 55);
+ my_asnprintf (NULL, &length, "%F %d", NaNd (), 33, 44, 55);
ASSERT (result != NULL);
ASSERT (strlen (result) >= 3 + 3
&& strisnan (result, 0, strlen (result) - 3, 1)
free (result);
}
+ { /* Precision with no rounding. */
+ size_t length;
+ char *result =
+ my_asnprintf (NULL, &length, "%.2F %d", 999.951, 33, 44, 55);
+ ASSERT (result != NULL);
+ ASSERT (strcmp (result, "999.95 33") == 0);
+ ASSERT (length == strlen (result));
+ free (result);
+ }
+
+ { /* Precision with rounding. */
+ size_t length;
+ char *result =
+ my_asnprintf (NULL, &length, "%.2F %d", 999.996, 33, 44, 55);
+ ASSERT (result != NULL);
+ ASSERT (strcmp (result, "1000.00 33") == 0);
+ ASSERT (length == strlen (result));
+ free (result);
+ }
+
{ /* A positive number. */
size_t length;
char *result =
{ /* Negative zero. */
size_t length;
char *result =
- my_asnprintf (NULL, &length, "%LF %d", -0.0L, 33, 44, 55);
+ my_asnprintf (NULL, &length, "%LF %d", -zerol, 33, 44, 55);
ASSERT (result != NULL);
if (have_minus_zero ())
ASSERT (strcmp (result, "-0.000000 33") == 0);
}
{ /* NaN. */
- static long double zero = 0.0L;
size_t length;
char *result =
- my_asnprintf (NULL, &length, "%LF %d", zero / zero, 33, 44, 55);
+ my_asnprintf (NULL, &length, "%LF %d", NaNl (), 33, 44, 55);
ASSERT (result != NULL);
ASSERT (strlen (result) >= 3 + 3
&& strisnan (result, 0, strlen (result) - 3, 1)
free (result);
}
+ { /* Precision with no rounding. */
+ size_t length;
+ char *result =
+ my_asnprintf (NULL, &length, "%.2LF %d", 999.951L, 33, 44, 55);
+ ASSERT (result != NULL);
+ ASSERT (strcmp (result, "999.95 33") == 0);
+ ASSERT (length == strlen (result));
+ free (result);
+ }
+
+ { /* Precision with rounding. */
+ size_t length;
+ char *result =
+ my_asnprintf (NULL, &length, "%.2LF %d", 999.996L, 33, 44, 55);
+ ASSERT (result != NULL);
+ ASSERT (strcmp (result, "1000.00 33") == 0);
+ ASSERT (length == strlen (result));
+ free (result);
+ }
+
/* Test the support of the %e format directive. */
{ /* A positive number. */
{ /* Negative zero. */
size_t length;
char *result =
- my_asnprintf (NULL, &length, "%e %d", -0.0, 33, 44, 55);
+ my_asnprintf (NULL, &length, "%e %d", -zerod, 33, 44, 55);
ASSERT (result != NULL);
if (have_minus_zero ())
ASSERT (strcmp (result, "-0.000000e+00 33") == 0
{ /* NaN. */
size_t length;
char *result =
- my_asnprintf (NULL, &length, "%e %d", NaN (), 33, 44, 55);
+ my_asnprintf (NULL, &length, "%e %d", NaNd (), 33, 44, 55);
ASSERT (result != NULL);
ASSERT (strlen (result) >= 3 + 3
&& strisnan (result, 0, strlen (result) - 3, 0)
{ /* FLAG_ZERO with NaN. */
size_t length;
char *result =
- my_asnprintf (NULL, &length, "%050e %d", NaN (), 33, 44, 55);
+ my_asnprintf (NULL, &length, "%050e %d", NaNd (), 33, 44, 55);
ASSERT (result != NULL);
ASSERT (strlen (result) == 50 + 3
&& strisnan (result, strspn (result, " "), strlen (result) - 3, 0)
free (result);
}
+ { /* Precision with no rounding. */
+ size_t length;
+ char *result =
+ my_asnprintf (NULL, &length, "%.4e %d", 999.951, 33, 44, 55);
+ ASSERT (result != NULL);
+ ASSERT (strcmp (result, "9.9995e+02 33") == 0
+ || strcmp (result, "9.9995e+002 33") == 0);
+ ASSERT (length == strlen (result));
+ free (result);
+ }
+
+ { /* Precision with rounding. */
+ size_t length;
+ char *result =
+ my_asnprintf (NULL, &length, "%.4e %d", 999.996, 33, 44, 55);
+ ASSERT (result != NULL);
+ ASSERT (strcmp (result, "1.0000e+03 33") == 0
+ || strcmp (result, "1.0000e+003 33") == 0);
+ ASSERT (length == strlen (result));
+ free (result);
+ }
+
{ /* A positive number. */
size_t length;
char *result =
{ /* Negative zero. */
size_t length;
char *result =
- my_asnprintf (NULL, &length, "%Le %d", -0.0L, 33, 44, 55);
+ my_asnprintf (NULL, &length, "%Le %d", -zerol, 33, 44, 55);
ASSERT (result != NULL);
if (have_minus_zero ())
ASSERT (strcmp (result, "-0.000000e+00 33") == 0);
}
{ /* NaN. */
- static long double zero = 0.0L;
size_t length;
char *result =
- my_asnprintf (NULL, &length, "%Le %d", zero / zero, 33, 44, 55);
+ my_asnprintf (NULL, &length, "%Le %d", NaNl (), 33, 44, 55);
ASSERT (result != NULL);
ASSERT (strlen (result) >= 3 + 3
&& strisnan (result, 0, strlen (result) - 3, 0)
}
{ /* FLAG_ZERO with NaN. */
- static long double zero = 0.0L;
size_t length;
char *result =
- my_asnprintf (NULL, &length, "%050Le %d", zero / zero, 33, 44, 55);
+ my_asnprintf (NULL, &length, "%050Le %d", NaNl (), 33, 44, 55);
ASSERT (result != NULL);
ASSERT (strlen (result) == 50 + 3
&& strisnan (result, strspn (result, " "), strlen (result) - 3, 0)
free (result);
}
+ { /* Precision with no rounding. */
+ size_t length;
+ char *result =
+ my_asnprintf (NULL, &length, "%.4Le %d", 999.951L, 33, 44, 55);
+ ASSERT (result != NULL);
+ ASSERT (strcmp (result, "9.9995e+02 33") == 0);
+ ASSERT (length == strlen (result));
+ free (result);
+ }
+
+ { /* Precision with rounding. */
+ size_t length;
+ char *result =
+ my_asnprintf (NULL, &length, "%.4Le %d", 999.996L, 33, 44, 55);
+ ASSERT (result != NULL);
+ ASSERT (strcmp (result, "1.0000e+03 33") == 0);
+ ASSERT (length == strlen (result));
+ free (result);
+ }
+
/* Test the support of the %g format directive. */
{ /* A positive number. */
{ /* Negative zero. */
size_t length;
char *result =
- my_asnprintf (NULL, &length, "%g %d", -0.0, 33, 44, 55);
+ my_asnprintf (NULL, &length, "%g %d", -zerod, 33, 44, 55);
ASSERT (result != NULL);
if (have_minus_zero ())
ASSERT (strcmp (result, "-0 33") == 0);
{ /* NaN. */
size_t length;
char *result =
- my_asnprintf (NULL, &length, "%g %d", NaN (), 33, 44, 55);
+ my_asnprintf (NULL, &length, "%g %d", NaNd (), 33, 44, 55);
ASSERT (result != NULL);
ASSERT (strlen (result) >= 3 + 3
&& strisnan (result, 0, strlen (result) - 3, 0)
{ /* FLAG_ZERO with NaN. */
size_t length;
char *result =
- my_asnprintf (NULL, &length, "%050g %d", NaN (), 33, 44, 55);
+ my_asnprintf (NULL, &length, "%050g %d", NaNd (), 33, 44, 55);
ASSERT (result != NULL);
ASSERT (strlen (result) == 50 + 3
&& strisnan (result, strspn (result, " "), strlen (result) - 3, 0)
free (result);
}
+ { /* Precision with no rounding. */
+ size_t length;
+ char *result =
+ my_asnprintf (NULL, &length, "%.5g %d", 999.951, 33, 44, 55);
+ ASSERT (result != NULL);
+ ASSERT (strcmp (result, "999.95 33") == 0);
+ ASSERT (length == strlen (result));
+ free (result);
+ }
+
+ { /* Precision with rounding. */
+ size_t length;
+ char *result =
+ my_asnprintf (NULL, &length, "%.5g %d", 999.996, 33, 44, 55);
+ ASSERT (result != NULL);
+ ASSERT (strcmp (result, "1000 33") == 0);
+ ASSERT (length == strlen (result));
+ free (result);
+ }
+
{ /* A positive number. */
size_t length;
char *result =
{ /* Negative zero. */
size_t length;
char *result =
- my_asnprintf (NULL, &length, "%Lg %d", -0.0L, 33, 44, 55);
+ my_asnprintf (NULL, &length, "%Lg %d", -zerol, 33, 44, 55);
ASSERT (result != NULL);
if (have_minus_zero ())
ASSERT (strcmp (result, "-0 33") == 0);
}
{ /* NaN. */
- static long double zero = 0.0L;
size_t length;
char *result =
- my_asnprintf (NULL, &length, "%Lg %d", zero / zero, 33, 44, 55);
+ my_asnprintf (NULL, &length, "%Lg %d", NaNl (), 33, 44, 55);
ASSERT (result != NULL);
ASSERT (strlen (result) >= 3 + 3
&& strisnan (result, 0, strlen (result) - 3, 0)
}
{ /* FLAG_ZERO with NaN. */
- static long double zero = 0.0L;
size_t length;
char *result =
- my_asnprintf (NULL, &length, "%050Lg %d", zero / zero, 33, 44, 55);
+ my_asnprintf (NULL, &length, "%050Lg %d", NaNl (), 33, 44, 55);
ASSERT (result != NULL);
ASSERT (strlen (result) == 50 + 3
&& strisnan (result, strspn (result, " "), strlen (result) - 3, 0)
free (result);
}
+ { /* Precision with no rounding. */
+ size_t length;
+ char *result =
+ my_asnprintf (NULL, &length, "%.5Lg %d", 999.951L, 33, 44, 55);
+ ASSERT (result != NULL);
+ ASSERT (strcmp (result, "999.95 33") == 0);
+ ASSERT (length == strlen (result));
+ free (result);
+ }
+
+ { /* Precision with rounding. */
+ size_t length;
+ char *result =
+ my_asnprintf (NULL, &length, "%.5Lg %d", 999.996L, 33, 44, 55);
+ ASSERT (result != NULL);
+ ASSERT (strcmp (result, "1000 33") == 0);
+ ASSERT (length == strlen (result));
+ free (result);
+ }
+
/* Test the support of the %n format directive. */
{
free (result);
}
+ /* Test the support of the left-adjust flag. */
+
+ {
+ size_t length;
+ char *result =
+ my_asnprintf (NULL, &length, "a%*sc", -3, "b");
+ ASSERT (result != NULL);
+ ASSERT (strcmp (result, "ab c") == 0);
+ ASSERT (length == strlen (result));
+ free (result);
+ }
+
+ {
+ size_t length;
+ char *result =
+ my_asnprintf (NULL, &length, "a%-*sc", 3, "b");
+ ASSERT (result != NULL);
+ ASSERT (strcmp (result, "ab c") == 0);
+ ASSERT (length == strlen (result));
+ free (result);
+ }
+
+ {
+ size_t length;
+ char *result =
+ my_asnprintf (NULL, &length, "a%-*sc", -3, "b");
+ ASSERT (result != NULL);
+ ASSERT (strcmp (result, "ab c") == 0);
+ ASSERT (length == strlen (result));
+ free (result);
+ }
+
/* Test the support of large precision. */
{