Mercurial > hg > octave-lyh > gnulib-hg
changeset 8865:9e203d8ade65
Stricter check for printf result for NaN arguments.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Tue, 22 May 2007 01:07:46 +0000 |
parents | b99c46035f3e |
children | 54716ad6bb86 |
files | ChangeLog lib/vasnprintf.c m4/printf.m4 tests/test-snprintf-posix.h tests/test-sprintf-posix.h tests/test-vasnprintf-posix.c tests/test-vasprintf-posix.c |
diffstat | 7 files changed, 238 insertions(+), 159 deletions(-) [+] |
line wrap: on
line diff
--- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,19 @@ +2007-05-21 Bruno Haible <bruno@clisp.org> + + * m4/printf.m4 (gl_PRINTF_INFINITE, gl_PRINTF_INFINITE_LONG_DOUBLE): + Check also the output for NaN arguments. When cross-compiling, guess + no on IRIX. + * lib/vasnprintf.c: Update comments. + * tests/test-vasnprintf-posix.c (strisnan): New function. + (test_function): Use it. + * tests/test-vasprintf-posix.c (strisnan): New function. + (test_function): Use it. + * tests/test-snprintf-posix.h (strisnan): New function. + (test_function): Use it. + * tests/test-sprintf-posix.h (strisnan): New function. + (test_function): Use it. + Reported by Eric Blake. + 2007-05-20 Bruno Haible <bruno@clisp.org> * m4/frexpl.m4 (gl_FUNC_FREXPL_WORKS): Add test for large finite
--- a/lib/vasnprintf.c +++ b/lib/vasnprintf.c @@ -1297,17 +1297,16 @@ # if NEED_PRINTF_INFINITE_DOUBLE || (a.arg[dp->arg_index].type == TYPE_DOUBLE /* The systems (mingw) which produce wrong output - for Inf and -Inf also do so for NaN and -0.0. - Therefore we treat these cases here as well. */ + for Inf, -Inf, and NaN also do so for -0.0. + Therefore we treat this case here as well. */ && is_infinite_or_zero (a.arg[dp->arg_index].a.a_double)) # endif # if NEED_PRINTF_LONG_DOUBLE || a.arg[dp->arg_index].type == TYPE_LONGDOUBLE # elif NEED_PRINTF_INFINITE_LONG_DOUBLE || (a.arg[dp->arg_index].type == TYPE_LONGDOUBLE - /* The systems which produce wrong output for Inf - and -Inf also do so for NaN. Therefore treat - this case here as well. */ + /* Some systems produce wrong output for Inf, + -Inf, and NaN. */ && is_infinitel (a.arg[dp->arg_index].a.a_longdouble)) # endif ))
--- a/m4/printf.m4 +++ b/m4/printf.m4 @@ -1,4 +1,4 @@ -# printf.m4 serial 12 +# printf.m4 serial 13 dnl Copyright (C) 2003, 2007 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -124,8 +124,8 @@ ]) ]) -dnl Test whether the *printf family of functions supports infinite 'double' -dnl arguments in the %f, %e, %g directives. (ISO C99, POSIX:2001) +dnl Test whether the *printf family of functions supports infinite and NaN +dnl 'double' arguments in the %f, %e, %g directives. (ISO C99, POSIX:2001) dnl Result is gl_cv_func_printf_infinite. AC_DEFUN([gl_PRINTF_INFINITE], @@ -138,7 +138,26 @@ AC_TRY_RUN([ #include <stdio.h> #include <string.h> +static int +strisnan (const char *string, size_t start_index, size_t end_index) +{ + if (start_index < end_index) + { + if (string[start_index] == '-') + start_index++; + if (start_index + 3 <= end_index + && memcmp (string + start_index, "nan", 3) == 0) + { + start_index += 3; + if (start_index == end_index + || (string[start_index] == '(' && string[end_index - 1] == ')')) + return 1; + } + } + return 0; +} static char buf[100]; +static double zero = 0.0; int main () { if (sprintf (buf, "%f", 1.0 / 0.0) < 0 @@ -147,18 +166,27 @@ if (sprintf (buf, "%f", -1.0 / 0.0) < 0 || (strcmp (buf, "-inf") != 0 && strcmp (buf, "-infinity") != 0)) return 1; + if (sprintf (buf, "%f", zero / zero) < 0 + || !strisnan (buf, 0, strlen (buf))) + return 1; if (sprintf (buf, "%e", 1.0 / 0.0) < 0 || (strcmp (buf, "inf") != 0 && strcmp (buf, "infinity") != 0)) return 1; if (sprintf (buf, "%e", -1.0 / 0.0) < 0 || (strcmp (buf, "-inf") != 0 && strcmp (buf, "-infinity") != 0)) return 1; + if (sprintf (buf, "%e", zero / zero) < 0 + || !strisnan (buf, 0, strlen (buf))) + return 1; if (sprintf (buf, "%g", 1.0 / 0.0) < 0 || (strcmp (buf, "inf") != 0 && strcmp (buf, "infinity") != 0)) return 1; if (sprintf (buf, "%g", -1.0 / 0.0) < 0 || (strcmp (buf, "-inf") != 0 && strcmp (buf, "-infinity") != 0)) return 1; + if (sprintf (buf, "%g", zero / zero) < 0 + || !strisnan (buf, 0, strlen (buf))) + return 1; return 0; }], [gl_cv_func_printf_infinite=yes], [gl_cv_func_printf_infinite=no], [ @@ -175,8 +203,6 @@ # Guess yes on HP-UX >= 11. hpux[7-9]* | hpux10*) gl_cv_func_printf_infinite="guessing no";; hpux*) gl_cv_func_printf_infinite="guessing yes";; - # Guess yes on IRIX >= 6.5. - irix6.5) gl_cv_func_printf_infinite="guessing yes";; # Guess yes on NetBSD >= 3. netbsd[1-2]* | netbsdelf[1-2]* | netbsdaout[1-2]* | netbsdcoff[1-2]*) gl_cv_func_printf_infinite="guessing no";; @@ -191,8 +217,8 @@ ]) ]) -dnl Test whether the *printf family of functions supports infinite 'long double' -dnl arguments in the %f, %e, %g directives. (ISO C99, POSIX:2001) +dnl Test whether the *printf family of functions supports infinite and NaN +dnl 'long double' arguments in the %f, %e, %g directives. (ISO C99, POSIX:2001) dnl Result is gl_cv_func_printf_infinite_long_double. AC_DEFUN([gl_PRINTF_INFINITE_LONG_DOUBLE], @@ -208,7 +234,26 @@ AC_TRY_RUN([ #include <stdio.h> #include <string.h> +static int +strisnan (const char *string, size_t start_index, size_t end_index) +{ + if (start_index < end_index) + { + if (string[start_index] == '-') + start_index++; + if (start_index + 3 <= end_index + && memcmp (string + start_index, "nan", 3) == 0) + { + start_index += 3; + if (start_index == end_index + || (string[start_index] == '(' && string[end_index - 1] == ')')) + return 1; + } + } + return 0; +} static char buf[100]; +static long double zeroL = 0.0L; int main () { if (sprintf (buf, "%Lf", 1.0L / 0.0L) < 0 @@ -217,18 +262,27 @@ if (sprintf (buf, "%Lf", -1.0L / 0.0L) < 0 || (strcmp (buf, "-inf") != 0 && strcmp (buf, "-infinity") != 0)) return 1; + if (sprintf (buf, "%Lf", zeroL / zeroL) < 0 + || !strisnan (buf, 0, strlen (buf))) + return 1; if (sprintf (buf, "%Le", 1.0L / 0.0L) < 0 || (strcmp (buf, "inf") != 0 && strcmp (buf, "infinity") != 0)) return 1; if (sprintf (buf, "%Le", -1.0L / 0.0L) < 0 || (strcmp (buf, "-inf") != 0 && strcmp (buf, "-infinity") != 0)) return 1; + if (sprintf (buf, "%Le", zeroL / zeroL) < 0 + || !strisnan (buf, 0, strlen (buf))) + return 1; if (sprintf (buf, "%Lg", 1.0L / 0.0L) < 0 || (strcmp (buf, "inf") != 0 && strcmp (buf, "infinity") != 0)) return 1; if (sprintf (buf, "%Lg", -1.0L / 0.0L) < 0 || (strcmp (buf, "-inf") != 0 && strcmp (buf, "-infinity") != 0)) return 1; + if (sprintf (buf, "%Lg", zeroL / zeroL) < 0 + || !strisnan (buf, 0, strlen (buf))) + return 1; return 0; }], [gl_cv_func_printf_infinite_long_double=yes], @@ -247,8 +301,6 @@ # Guess yes on HP-UX >= 11. hpux[7-9]* | hpux10*) gl_cv_func_printf_infinite_long_double="guessing no";; hpux*) gl_cv_func_printf_infinite_long_double="guessing yes";; - # Guess yes on IRIX >= 6.5. - irix6.5) gl_cv_func_printf_infinite_long_double="guessing yes";; # Guess yes on NetBSD >= 3. netbsd[1-2]* | netbsdelf[1-2]* | netbsdaout[1-2]* | netbsdcoff[1-2]*) gl_cv_func_printf_infinite_long_double="guessing no";; @@ -895,7 +947,7 @@ dnl AIX 4.3.2, 5.1 # . # # # # . . . # . . . . . dnl HP-UX 11.31 . . . . # . . . . # . . # # . dnl HP-UX 10.20, 11.{00,11,23} # . . . # # . . . # . . # # # -dnl IRIX 6.5 # . . . # # . . . # . . # . . +dnl IRIX 6.5 # . # # # # . . . # . . # . . dnl OSF/1 5.1 # . # # # # . . . # . . # . # dnl OSF/1 4.0d # . # # # # . . . # # # # # # dnl NetBSD 4.0 . ? ? ? ? ? . . ? ? . . . ? ?
--- a/tests/test-snprintf-posix.h +++ b/tests/test-snprintf-posix.h @@ -49,6 +49,27 @@ return 1; } +/* Test whether string[start_index..end_index-1] is a valid textual + representation of NaN. */ +static int +strisnan (const char *string, size_t start_index, size_t end_index, int uppercase) +{ + if (start_index < end_index) + { + if (string[start_index] == '-') + start_index++; + if (start_index + 3 <= end_index + && memcmp (string + start_index, uppercase ? "NAN" : "nan", 3) == 0) + { + start_index += 3; + if (start_index == end_index + || (string[start_index] == '(' && string[end_index - 1] == ')')) + return 1; + } + } + return 0; +} + static void test_function (int (*my_snprintf) (char *, size_t, const char *, ...)) { @@ -176,8 +197,7 @@ int retval = my_snprintf (result, sizeof (result), "%a %d", NaN (), 33, 44, 55); ASSERT (strlen (result) >= 3 + 3 - && (memcmp (result, "nan", 3) == 0 - || memcmp (result, "-nan", 4) == 0) + && strisnan (result, 0, strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (retval == strlen (result)); } @@ -376,8 +396,7 @@ /* "0000000nan 33" is not a valid result; see <http://lists.gnu.org/archive/html/bug-gnulib/2007-04/msg00107.html> */ ASSERT (strlen (result) == 20 + 3 - && (memcmp (result + strspn (result, " "), "nan", 3) == 0 - || memcmp (result + strspn (result, " "), "-nan", 4) == 0) + && strisnan (result, strspn (result, " "), strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (retval == strlen (result)); } @@ -442,8 +461,7 @@ int retval = my_snprintf (result, sizeof (result), "%La %d", 0.0L / 0.0L, 33, 44, 55); ASSERT (strlen (result) >= 3 + 3 - && (memcmp (result, "nan", 3) == 0 - || memcmp (result, "-nan", 4) == 0) + && strisnan (result, 0, strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (retval == strlen (result)); } @@ -643,8 +661,7 @@ /* "0000000nan 33" is not a valid result; see <http://lists.gnu.org/archive/html/bug-gnulib/2007-04/msg00107.html> */ ASSERT (strlen (result) == 20 + 3 - && (memcmp (result + strspn (result, " "), "nan", 3) == 0 - || memcmp (result + strspn (result, " "), "-nan", 4) == 0) + && strisnan (result, strspn (result, " "), strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (retval == strlen (result)); } @@ -804,8 +821,7 @@ int retval = my_snprintf (result, sizeof (result), "%f %d", NaN (), 33, 44, 55); ASSERT (strlen (result) >= 3 + 3 - && (memcmp (result, "nan", 3) == 0 - || memcmp (result, "-nan", 4) == 0) + && strisnan (result, 0, strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (retval == strlen (result)); } @@ -886,8 +902,7 @@ int retval = my_snprintf (result, sizeof (result), "%020f %d", NaN (), 33, 44, 55); ASSERT (strlen (result) == 20 + 3 - && (memcmp (result + strspn (result, " "), "nan", 3) == 0 - || memcmp (result + strspn (result, " "), "-nan", 4) == 0) + && strisnan (result, strspn (result, " "), strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (retval == strlen (result)); } @@ -1054,8 +1069,7 @@ int retval = my_snprintf (result, sizeof (result), "%Lf %d", zero / zero, 33, 44, 55); ASSERT (strlen (result) >= 3 + 3 - && (memcmp (result, "nan", 3) == 0 - || memcmp (result, "-nan", 4) == 0) + && strisnan (result, 0, strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (retval == strlen (result)); } @@ -1137,8 +1151,7 @@ int retval = my_snprintf (result, sizeof (result), "%020Lf %d", zero / zero, 33, 44, 55); ASSERT (strlen (result) == 20 + 3 - && (memcmp (result + strspn (result, " "), "nan", 3) == 0 - || memcmp (result + strspn (result, " "), "-nan", 4) == 0) + && strisnan (result, strspn (result, " "), strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (retval == strlen (result)); } @@ -1217,8 +1230,7 @@ int retval = my_snprintf (result, sizeof (result), "%F %d", NaN (), 33, 44, 55); ASSERT (strlen (result) >= 3 + 3 - && (memcmp (result, "NAN", 3) == 0 - || memcmp (result, "-NAN", 4) == 0) + && strisnan (result, 0, strlen (result) - 3, 1) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (retval == strlen (result)); } @@ -1313,8 +1325,7 @@ int retval = my_snprintf (result, sizeof (result), "%LF %d", zero / zero, 33, 44, 55); ASSERT (strlen (result) >= 3 + 3 - && (memcmp (result, "NAN", 3) == 0 - || memcmp (result, "-NAN", 4) == 0) + && strisnan (result, 0, strlen (result) - 3, 1) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (retval == strlen (result)); } @@ -1513,8 +1524,7 @@ int retval = my_snprintf (result, sizeof (result), "%e %d", NaN (), 33, 44, 55); ASSERT (strlen (result) >= 3 + 3 - && (memcmp (result, "nan", 3) == 0 - || memcmp (result, "-nan", 4) == 0) + && strisnan (result, 0, strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (retval == strlen (result)); } @@ -1605,8 +1615,7 @@ int retval = my_snprintf (result, sizeof (result), "%020e %d", NaN (), 33, 44, 55); ASSERT (strlen (result) == 20 + 3 - && (memcmp (result + strspn (result, " "), "nan", 3) == 0 - || memcmp (result + strspn (result, " "), "-nan", 4) == 0) + && strisnan (result, strspn (result, " "), strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (retval == strlen (result)); } @@ -1774,8 +1783,7 @@ int retval = my_snprintf (result, sizeof (result), "%Le %d", zero / zero, 33, 44, 55); ASSERT (strlen (result) >= 3 + 3 - && (memcmp (result, "nan", 3) == 0 - || memcmp (result, "-nan", 4) == 0) + && strisnan (result, 0, strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (retval == strlen (result)); } @@ -1859,8 +1867,7 @@ int retval = my_snprintf (result, sizeof (result), "%020Le %d", zero / zero, 33, 44, 55); ASSERT (strlen (result) == 20 + 3 - && (memcmp (result + strspn (result, " "), "nan", 3) == 0 - || memcmp (result + strspn (result, " "), "-nan", 4) == 0) + && strisnan (result, strspn (result, " "), strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (retval == strlen (result)); } @@ -2038,8 +2045,7 @@ int retval = my_snprintf (result, sizeof (result), "%g %d", NaN (), 33, 44, 55); ASSERT (strlen (result) >= 3 + 3 - && (memcmp (result, "nan", 3) == 0 - || memcmp (result, "-nan", 4) == 0) + && strisnan (result, 0, strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (retval == strlen (result)); } @@ -2123,8 +2129,7 @@ int retval = my_snprintf (result, sizeof (result), "%020g %d", NaN (), 33, 44, 55); ASSERT (strlen (result) == 20 + 3 - && (memcmp (result + strspn (result, " "), "nan", 3) == 0 - || memcmp (result + strspn (result, " "), "-nan", 4) == 0) + && strisnan (result, strspn (result, " "), strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (retval == strlen (result)); } @@ -2292,8 +2297,7 @@ int retval = my_snprintf (result, sizeof (result), "%Lg %d", zero / zero, 33, 44, 55); ASSERT (strlen (result) >= 3 + 3 - && (memcmp (result, "nan", 3) == 0 - || memcmp (result, "-nan", 4) == 0) + && strisnan (result, 0, strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (retval == strlen (result)); } @@ -2377,8 +2381,7 @@ int retval = my_snprintf (result, sizeof (result), "%020Lg %d", zero / zero, 33, 44, 55); ASSERT (strlen (result) == 20 + 3 - && (memcmp (result + strspn (result, " "), "nan", 3) == 0 - || memcmp (result + strspn (result, " "), "-nan", 4) == 0) + && strisnan (result, strspn (result, " "), strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (retval == strlen (result)); }
--- a/tests/test-sprintf-posix.h +++ b/tests/test-sprintf-posix.h @@ -49,6 +49,27 @@ return 1; } +/* Test whether string[start_index..end_index-1] is a valid textual + representation of NaN. */ +static int +strisnan (const char *string, size_t start_index, size_t end_index, int uppercase) +{ + if (start_index < end_index) + { + if (string[start_index] == '-') + start_index++; + if (start_index + 3 <= end_index + && memcmp (string + start_index, uppercase ? "NAN" : "nan", 3) == 0) + { + start_index += 3; + if (start_index == end_index + || (string[start_index] == '(' && string[end_index - 1] == ')')) + return 1; + } + } + return 0; +} + static void test_function (int (*my_sprintf) (char *, const char *, ...)) { @@ -162,8 +183,7 @@ int retval = my_sprintf (result, "%a %d", NaN (), 33, 44, 55); ASSERT (strlen (result) >= 3 + 3 - && (memcmp (result, "nan", 3) == 0 - || memcmp (result, "-nan", 4) == 0) + && strisnan (result, 0, strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (retval == strlen (result)); } @@ -362,8 +382,7 @@ /* "0000000nan 33" is not a valid result; see <http://lists.gnu.org/archive/html/bug-gnulib/2007-04/msg00107.html> */ ASSERT (strlen (result) == 20 + 3 - && (memcmp (result + strspn (result, " "), "nan", 3) == 0 - || memcmp (result + strspn (result, " "), "-nan", 4) == 0) + && strisnan (result, strspn (result, " "), strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (retval == strlen (result)); } @@ -428,8 +447,7 @@ int retval = my_sprintf (result, "%La %d", 0.0L / 0.0L, 33, 44, 55); ASSERT (strlen (result) >= 3 + 3 - && (memcmp (result, "nan", 3) == 0 - || memcmp (result, "-nan", 4) == 0) + && strisnan (result, 0, strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (retval == strlen (result)); } @@ -629,8 +647,7 @@ /* "0000000nan 33" is not a valid result; see <http://lists.gnu.org/archive/html/bug-gnulib/2007-04/msg00107.html> */ ASSERT (strlen (result) == 20 + 3 - && (memcmp (result + strspn (result, " "), "nan", 3) == 0 - || memcmp (result + strspn (result, " "), "-nan", 4) == 0) + && strisnan (result, strspn (result, " "), strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (retval == strlen (result)); } @@ -790,8 +807,7 @@ int retval = my_sprintf (result, "%f %d", NaN (), 33, 44, 55); ASSERT (strlen (result) >= 3 + 3 - && (memcmp (result, "nan", 3) == 0 - || memcmp (result, "-nan", 4) == 0) + && strisnan (result, 0, strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (retval == strlen (result)); } @@ -866,8 +882,7 @@ int retval = my_sprintf (result, "%020f %d", NaN (), 33, 44, 55); ASSERT (strlen (result) == 20 + 3 - && (memcmp (result + strspn (result, " "), "nan", 3) == 0 - || memcmp (result + strspn (result, " "), "-nan", 4) == 0) + && strisnan (result, strspn (result, " "), strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (retval == strlen (result)); } @@ -1034,8 +1049,7 @@ int retval = my_sprintf (result, "%Lf %d", zero / zero, 33, 44, 55); ASSERT (strlen (result) >= 3 + 3 - && (memcmp (result, "nan", 3) == 0 - || memcmp (result, "-nan", 4) == 0) + && strisnan (result, 0, strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (retval == strlen (result)); } @@ -1111,8 +1125,7 @@ int retval = my_sprintf (result, "%020Lf %d", zero / zero, 33, 44, 55); ASSERT (strlen (result) == 20 + 3 - && (memcmp (result + strspn (result, " "), "nan", 3) == 0 - || memcmp (result + strspn (result, " "), "-nan", 4) == 0) + && strisnan (result, strspn (result, " "), strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (retval == strlen (result)); } @@ -1191,8 +1204,7 @@ int retval = my_sprintf (result, "%F %d", NaN (), 33, 44, 55); ASSERT (strlen (result) >= 3 + 3 - && (memcmp (result, "NAN", 3) == 0 - || memcmp (result, "-NAN", 4) == 0) + && strisnan (result, 0, strlen (result) - 3, 1) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (retval == strlen (result)); } @@ -1287,8 +1299,7 @@ int retval = my_sprintf (result, "%LF %d", zero / zero, 33, 44, 55); ASSERT (strlen (result) >= 3 + 3 - && (memcmp (result, "NAN", 3) == 0 - || memcmp (result, "-NAN", 4) == 0) + && strisnan (result, 0, strlen (result) - 3, 1) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (retval == strlen (result)); } @@ -1487,8 +1498,7 @@ int retval = my_sprintf (result, "%e %d", NaN (), 33, 44, 55); ASSERT (strlen (result) >= 3 + 3 - && (memcmp (result, "nan", 3) == 0 - || memcmp (result, "-nan", 4) == 0) + && strisnan (result, 0, strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (retval == strlen (result)); } @@ -1579,8 +1589,7 @@ int retval = my_sprintf (result, "%020e %d", NaN (), 33, 44, 55); ASSERT (strlen (result) == 20 + 3 - && (memcmp (result + strspn (result, " "), "nan", 3) == 0 - || memcmp (result + strspn (result, " "), "-nan", 4) == 0) + && strisnan (result, strspn (result, " "), strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (retval == strlen (result)); } @@ -1748,8 +1757,7 @@ int retval = my_sprintf (result, "%Le %d", zero / zero, 33, 44, 55); ASSERT (strlen (result) >= 3 + 3 - && (memcmp (result, "nan", 3) == 0 - || memcmp (result, "-nan", 4) == 0) + && strisnan (result, 0, strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (retval == strlen (result)); } @@ -1833,8 +1841,7 @@ int retval = my_sprintf (result, "%020Le %d", zero / zero, 33, 44, 55); ASSERT (strlen (result) == 20 + 3 - && (memcmp (result + strspn (result, " "), "nan", 3) == 0 - || memcmp (result + strspn (result, " "), "-nan", 4) == 0) + && strisnan (result, strspn (result, " "), strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (retval == strlen (result)); } @@ -2012,8 +2019,7 @@ int retval = my_sprintf (result, "%g %d", NaN (), 33, 44, 55); ASSERT (strlen (result) >= 3 + 3 - && (memcmp (result, "nan", 3) == 0 - || memcmp (result, "-nan", 4) == 0) + && strisnan (result, 0, strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (retval == strlen (result)); } @@ -2097,8 +2103,7 @@ int retval = my_sprintf (result, "%020g %d", NaN (), 33, 44, 55); ASSERT (strlen (result) == 20 + 3 - && (memcmp (result + strspn (result, " "), "nan", 3) == 0 - || memcmp (result + strspn (result, " "), "-nan", 4) == 0) + && strisnan (result, strspn (result, " "), strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (retval == strlen (result)); } @@ -2266,8 +2271,7 @@ int retval = my_sprintf (result, "%Lg %d", zero / zero, 33, 44, 55); ASSERT (strlen (result) >= 3 + 3 - && (memcmp (result, "nan", 3) == 0 - || memcmp (result, "-nan", 4) == 0) + && strisnan (result, 0, strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (retval == strlen (result)); } @@ -2351,8 +2355,7 @@ int retval = my_sprintf (result, "%020Lg %d", zero / zero, 33, 44, 55); ASSERT (strlen (result) == 20 + 3 - && (memcmp (result + strspn (result, " "), "nan", 3) == 0 - || memcmp (result + strspn (result, " "), "-nan", 4) == 0) + && strisnan (result, strspn (result, " "), strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (retval == strlen (result)); }
--- a/tests/test-vasnprintf-posix.c +++ b/tests/test-vasnprintf-posix.c @@ -74,6 +74,27 @@ return 1; } +/* Test whether string[start_index..end_index-1] is a valid textual + representation of NaN. */ +static int +strisnan (const char *string, size_t start_index, size_t end_index, int uppercase) +{ + if (start_index < end_index) + { + if (string[start_index] == '-') + start_index++; + if (start_index + 3 <= end_index + && memcmp (string + start_index, uppercase ? "NAN" : "nan", 3) == 0) + { + start_index += 3; + if (start_index == end_index + || (string[start_index] == '(' && string[end_index - 1] == ')')) + return 1; + } + } + return 0; +} + static void test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { @@ -228,8 +249,7 @@ my_asnprintf (NULL, &length, "%a %d", NaN (), 33, 44, 55); ASSERT (result != NULL); ASSERT (strlen (result) >= 3 + 3 - && (memcmp (result, "nan", 3) == 0 - || memcmp (result, "-nan", 4) == 0) + && strisnan (result, 0, strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (length == strlen (result)); free (result); @@ -464,8 +484,7 @@ /* "0000000nan 33" is not a valid result; see <http://lists.gnu.org/archive/html/bug-gnulib/2007-04/msg00107.html> */ ASSERT (strlen (result) == 20 + 3 - && (memcmp (result + strspn (result, " "), "nan", 3) == 0 - || memcmp (result + strspn (result, " "), "-nan", 4) == 0) + && strisnan (result, strspn (result, " "), strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (length == strlen (result)); free (result); @@ -544,8 +563,7 @@ my_asnprintf (NULL, &length, "%La %d", 0.0L / 0.0L, 33, 44, 55); ASSERT (result != NULL); ASSERT (strlen (result) >= 3 + 3 - && (memcmp (result, "nan", 3) == 0 - || memcmp (result, "-nan", 4) == 0) + && strisnan (result, 0, strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (length == strlen (result)); free (result); @@ -781,8 +799,7 @@ /* "0000000nan 33" is not a valid result; see <http://lists.gnu.org/archive/html/bug-gnulib/2007-04/msg00107.html> */ ASSERT (strlen (result) == 20 + 3 - && (memcmp (result + strspn (result, " "), "nan", 3) == 0 - || memcmp (result + strspn (result, " "), "-nan", 4) == 0) + && strisnan (result, strspn (result, " "), strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (length == strlen (result)); free (result); @@ -960,8 +977,7 @@ my_asnprintf (NULL, &length, "%f %d", NaN (), 33, 44, 55); ASSERT (result != NULL); ASSERT (strlen (result) >= 3 + 3 - && (memcmp (result, "nan", 3) == 0 - || memcmp (result, "-nan", 4) == 0) + && strisnan (result, 0, strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (length == strlen (result)); free (result); @@ -1054,8 +1070,7 @@ my_asnprintf (NULL, &length, "%020f %d", NaN (), 33, 44, 55); ASSERT (result != NULL); ASSERT (strlen (result) == 20 + 3 - && (memcmp (result + strspn (result, " "), "nan", 3) == 0 - || memcmp (result + strspn (result, " "), "-nan", 4) == 0) + && strisnan (result, strspn (result, " "), strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (length == strlen (result)); free (result); @@ -1242,8 +1257,7 @@ my_asnprintf (NULL, &length, "%Lf %d", zero / zero, 33, 44, 55); ASSERT (result != NULL); ASSERT (strlen (result) >= 3 + 3 - && (memcmp (result, "nan", 3) == 0 - || memcmp (result, "-nan", 4) == 0) + && strisnan (result, 0, strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (length == strlen (result)); free (result); @@ -1337,8 +1351,7 @@ my_asnprintf (NULL, &length, "%020Lf %d", zero / zero, 33, 44, 55); ASSERT (result != NULL); ASSERT (strlen (result) == 20 + 3 - && (memcmp (result + strspn (result, " "), "nan", 3) == 0 - || memcmp (result + strspn (result, " "), "-nan", 4) == 0) + && strisnan (result, strspn (result, " "), strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (length == strlen (result)); free (result); @@ -1435,8 +1448,7 @@ my_asnprintf (NULL, &length, "%F %d", NaN (), 33, 44, 55); ASSERT (result != NULL); ASSERT (strlen (result) >= 3 + 3 - && (memcmp (result, "NAN", 3) == 0 - || memcmp (result, "-NAN", 4) == 0) + && strisnan (result, 0, strlen (result) - 3, 1) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (length == strlen (result)); free (result); @@ -1553,8 +1565,7 @@ my_asnprintf (NULL, &length, "%LF %d", zero / zero, 33, 44, 55); ASSERT (result != NULL); ASSERT (strlen (result) >= 3 + 3 - && (memcmp (result, "NAN", 3) == 0 - || memcmp (result, "-NAN", 4) == 0) + && strisnan (result, 0, strlen (result) - 3, 1) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (length == strlen (result)); free (result); @@ -1776,8 +1787,7 @@ my_asnprintf (NULL, &length, "%e %d", NaN (), 33, 44, 55); ASSERT (result != NULL); ASSERT (strlen (result) >= 3 + 3 - && (memcmp (result, "nan", 3) == 0 - || memcmp (result, "-nan", 4) == 0) + && strisnan (result, 0, strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (length == strlen (result)); free (result); @@ -1888,8 +1898,7 @@ my_asnprintf (NULL, &length, "%020e %d", NaN (), 33, 44, 55); ASSERT (result != NULL); ASSERT (strlen (result) == 20 + 3 - && (memcmp (result + strspn (result, " "), "nan", 3) == 0 - || memcmp (result + strspn (result, " "), "-nan", 4) == 0) + && strisnan (result, strspn (result, " "), strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (length == strlen (result)); free (result); @@ -2077,8 +2086,7 @@ my_asnprintf (NULL, &length, "%Le %d", zero / zero, 33, 44, 55); ASSERT (result != NULL); ASSERT (strlen (result) >= 3 + 3 - && (memcmp (result, "nan", 3) == 0 - || memcmp (result, "-nan", 4) == 0) + && strisnan (result, 0, strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (length == strlen (result)); free (result); @@ -2182,8 +2190,7 @@ my_asnprintf (NULL, &length, "%020Le %d", zero / zero, 33, 44, 55); ASSERT (result != NULL); ASSERT (strlen (result) == 20 + 3 - && (memcmp (result + strspn (result, " "), "nan", 3) == 0 - || memcmp (result + strspn (result, " "), "-nan", 4) == 0) + && strisnan (result, strspn (result, " "), strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (length == strlen (result)); free (result); @@ -2381,8 +2388,7 @@ my_asnprintf (NULL, &length, "%g %d", NaN (), 33, 44, 55); ASSERT (result != NULL); ASSERT (strlen (result) >= 3 + 3 - && (memcmp (result, "nan", 3) == 0 - || memcmp (result, "-nan", 4) == 0) + && strisnan (result, 0, strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (length == strlen (result)); free (result); @@ -2486,8 +2492,7 @@ my_asnprintf (NULL, &length, "%020g %d", NaN (), 33, 44, 55); ASSERT (result != NULL); ASSERT (strlen (result) == 20 + 3 - && (memcmp (result + strspn (result, " "), "nan", 3) == 0 - || memcmp (result + strspn (result, " "), "-nan", 4) == 0) + && strisnan (result, strspn (result, " "), strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (length == strlen (result)); free (result); @@ -2675,8 +2680,7 @@ my_asnprintf (NULL, &length, "%Lg %d", zero / zero, 33, 44, 55); ASSERT (result != NULL); ASSERT (strlen (result) >= 3 + 3 - && (memcmp (result, "nan", 3) == 0 - || memcmp (result, "-nan", 4) == 0) + && strisnan (result, 0, strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (length == strlen (result)); free (result); @@ -2780,8 +2784,7 @@ my_asnprintf (NULL, &length, "%020Lg %d", zero / zero, 33, 44, 55); ASSERT (result != NULL); ASSERT (strlen (result) == 20 + 3 - && (memcmp (result + strspn (result, " "), "nan", 3) == 0 - || memcmp (result + strspn (result, " "), "-nan", 4) == 0) + && strisnan (result, strspn (result, " "), strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (length == strlen (result)); free (result);
--- a/tests/test-vasprintf-posix.c +++ b/tests/test-vasprintf-posix.c @@ -74,6 +74,27 @@ return 1; } +/* Test whether string[start_index..end_index-1] is a valid textual + representation of NaN. */ +static int +strisnan (const char *string, size_t start_index, size_t end_index, int uppercase) +{ + if (start_index < end_index) + { + if (string[start_index] == '-') + start_index++; + if (start_index + 3 <= end_index + && memcmp (string + start_index, uppercase ? "NAN" : "nan", 3) == 0) + { + start_index += 3; + if (start_index == end_index + || (string[start_index] == '(' && string[end_index - 1] == ')')) + return 1; + } + } + return 0; +} + static void test_function (int (*my_asprintf) (char **, const char *, ...)) { @@ -209,8 +230,7 @@ my_asprintf (&result, "%a %d", NaN (), 33, 44, 55); ASSERT (result != NULL); ASSERT (strlen (result) >= 3 + 3 - && (memcmp (result, "nan", 3) == 0 - || memcmp (result, "-nan", 4) == 0) + && strisnan (result, 0, strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (retval == strlen (result)); free (result); @@ -445,8 +465,7 @@ /* "0000000nan 33" is not a valid result; see <http://lists.gnu.org/archive/html/bug-gnulib/2007-04/msg00107.html> */ ASSERT (strlen (result) == 20 + 3 - && (memcmp (result + strspn (result, " "), "nan", 3) == 0 - || memcmp (result + strspn (result, " "), "-nan", 4) == 0) + && strisnan (result, strspn (result, " "), strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (retval == strlen (result)); free (result); @@ -525,8 +544,7 @@ my_asprintf (&result, "%La %d", 0.0L / 0.0L, 33, 44, 55); ASSERT (result != NULL); ASSERT (strlen (result) >= 3 + 3 - && (memcmp (result, "nan", 3) == 0 - || memcmp (result, "-nan", 4) == 0) + && strisnan (result, 0, strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (retval == strlen (result)); free (result); @@ -762,8 +780,7 @@ /* "0000000nan 33" is not a valid result; see <http://lists.gnu.org/archive/html/bug-gnulib/2007-04/msg00107.html> */ ASSERT (strlen (result) == 20 + 3 - && (memcmp (result + strspn (result, " "), "nan", 3) == 0 - || memcmp (result + strspn (result, " "), "-nan", 4) == 0) + && strisnan (result, strspn (result, " "), strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (retval == strlen (result)); free (result); @@ -941,8 +958,7 @@ my_asprintf (&result, "%f %d", NaN (), 33, 44, 55); ASSERT (result != NULL); ASSERT (strlen (result) >= 3 + 3 - && (memcmp (result, "nan", 3) == 0 - || memcmp (result, "-nan", 4) == 0) + && strisnan (result, 0, strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (retval == strlen (result)); free (result); @@ -1035,8 +1051,7 @@ my_asprintf (&result, "%020f %d", NaN (), 33, 44, 55); ASSERT (result != NULL); ASSERT (strlen (result) == 20 + 3 - && (memcmp (result + strspn (result, " "), "nan", 3) == 0 - || memcmp (result + strspn (result, " "), "-nan", 4) == 0) + && strisnan (result, strspn (result, " "), strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (retval == strlen (result)); free (result); @@ -1223,8 +1238,7 @@ my_asprintf (&result, "%Lf %d", zero / zero, 33, 44, 55); ASSERT (result != NULL); ASSERT (strlen (result) >= 3 + 3 - && (memcmp (result, "nan", 3) == 0 - || memcmp (result, "-nan", 4) == 0) + && strisnan (result, 0, strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (retval == strlen (result)); free (result); @@ -1318,8 +1332,7 @@ my_asprintf (&result, "%020Lf %d", zero / zero, 33, 44, 55); ASSERT (result != NULL); ASSERT (strlen (result) == 20 + 3 - && (memcmp (result + strspn (result, " "), "nan", 3) == 0 - || memcmp (result + strspn (result, " "), "-nan", 4) == 0) + && strisnan (result, strspn (result, " "), strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (retval == strlen (result)); free (result); @@ -1416,8 +1429,7 @@ my_asprintf (&result, "%F %d", NaN (), 33, 44, 55); ASSERT (result != NULL); ASSERT (strlen (result) >= 3 + 3 - && (memcmp (result, "NAN", 3) == 0 - || memcmp (result, "-NAN", 4) == 0) + && strisnan (result, 0, strlen (result) - 3, 1) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (retval == strlen (result)); free (result); @@ -1534,8 +1546,7 @@ my_asprintf (&result, "%LF %d", zero / zero, 33, 44, 55); ASSERT (result != NULL); ASSERT (strlen (result) >= 3 + 3 - && (memcmp (result, "NAN", 3) == 0 - || memcmp (result, "-NAN", 4) == 0) + && strisnan (result, 0, strlen (result) - 3, 1) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (retval == strlen (result)); free (result); @@ -1757,8 +1768,7 @@ my_asprintf (&result, "%e %d", NaN (), 33, 44, 55); ASSERT (result != NULL); ASSERT (strlen (result) >= 3 + 3 - && (memcmp (result, "nan", 3) == 0 - || memcmp (result, "-nan", 4) == 0) + && strisnan (result, 0, strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (retval == strlen (result)); free (result); @@ -1869,8 +1879,7 @@ my_asprintf (&result, "%020e %d", NaN (), 33, 44, 55); ASSERT (result != NULL); ASSERT (strlen (result) == 20 + 3 - && (memcmp (result + strspn (result, " "), "nan", 3) == 0 - || memcmp (result + strspn (result, " "), "-nan", 4) == 0) + && strisnan (result, strspn (result, " "), strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (retval == strlen (result)); free (result); @@ -2058,8 +2067,7 @@ my_asprintf (&result, "%Le %d", zero / zero, 33, 44, 55); ASSERT (result != NULL); ASSERT (strlen (result) >= 3 + 3 - && (memcmp (result, "nan", 3) == 0 - || memcmp (result, "-nan", 4) == 0) + && strisnan (result, 0, strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (retval == strlen (result)); free (result); @@ -2163,8 +2171,7 @@ my_asprintf (&result, "%020Le %d", zero / zero, 33, 44, 55); ASSERT (result != NULL); ASSERT (strlen (result) == 20 + 3 - && (memcmp (result + strspn (result, " "), "nan", 3) == 0 - || memcmp (result + strspn (result, " "), "-nan", 4) == 0) + && strisnan (result, strspn (result, " "), strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (retval == strlen (result)); free (result); @@ -2362,8 +2369,7 @@ my_asprintf (&result, "%g %d", NaN (), 33, 44, 55); ASSERT (result != NULL); ASSERT (strlen (result) >= 3 + 3 - && (memcmp (result, "nan", 3) == 0 - || memcmp (result, "-nan", 4) == 0) + && strisnan (result, 0, strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (retval == strlen (result)); free (result); @@ -2467,8 +2473,7 @@ my_asprintf (&result, "%020g %d", NaN (), 33, 44, 55); ASSERT (result != NULL); ASSERT (strlen (result) == 20 + 3 - && (memcmp (result + strspn (result, " "), "nan", 3) == 0 - || memcmp (result + strspn (result, " "), "-nan", 4) == 0) + && strisnan (result, strspn (result, " "), strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (retval == strlen (result)); free (result); @@ -2656,8 +2661,7 @@ my_asprintf (&result, "%Lg %d", zero / zero, 33, 44, 55); ASSERT (result != NULL); ASSERT (strlen (result) >= 3 + 3 - && (memcmp (result, "nan", 3) == 0 - || memcmp (result, "-nan", 4) == 0) + && strisnan (result, 0, strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (retval == strlen (result)); free (result); @@ -2761,8 +2765,7 @@ my_asprintf (&result, "%020Lg %d", zero / zero, 33, 44, 55); ASSERT (result != NULL); ASSERT (strlen (result) == 20 + 3 - && (memcmp (result + strspn (result, " "), "nan", 3) == 0 - || memcmp (result + strspn (result, " "), "-nan", 4) == 0) + && strisnan (result, strspn (result, " "), strlen (result) - 3, 0) && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (retval == strlen (result)); free (result);