X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;ds=sidebyside;f=tests%2Ftest-vasnprintf-posix.c;h=dea0d3c1b29b8cd142155c680104bc68cee3f9ce;hb=88418d5fd24c99aac393d3e2f564f6939e4e2fe3;hp=0c20a604430fb7d40fea52a2732904f9fc94f3de;hpb=441aa3044f43e5572f58c354f01e6bc070acd5c7;p=gnulib.git diff --git a/tests/test-vasnprintf-posix.c b/tests/test-vasnprintf-posix.c index 0c20a6044..dea0d3c1b 100644 --- a/tests/test-vasnprintf-posix.c +++ b/tests/test-vasnprintf-posix.c @@ -1,5 +1,5 @@ /* Test of POSIX compatible vasnprintf() and asnprintf() functions. - Copyright (C) 2007-2009 Free Software Foundation, Inc. + Copyright (C) 2007-2011 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 @@ -20,58 +20,27 @@ #include "vasnprintf.h" +#include #include #include #include -#include #include #include #include +#include "macros.h" +#include "minus-zero.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 SGI MIPS floating-point format does not distinguish 0.0 and -0.0. */ static int have_minus_zero () { static double plus_zero = 0.0; - double minus_zero = - plus_zero; + double minus_zero = minus_zerod; 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 -zerod instead. */ -double zerod = 0.0; - -/* On HP-UX 10.20, negating 0.0L does not yield -0.0L. - So we use minus_zerol instead. - IRIX cc can't put -0.0L into .data, but can compute at runtime. - Note that the expression -LDBL_MIN * LDBL_MIN does not work on other - platforms, such as when cross-compiling to PowerPC on MacOS X 10.5. */ -#if defined __hpux || defined __sgi -static long double -compute_minus_zerol (void) -{ - return -LDBL_MIN * LDBL_MIN; -} -# define minus_zerol compute_minus_zerol () -#else -long double minus_zerol = -0.0L; -#endif - /* Representation of an 80-bit 'long double' as an initializer for a sequence of 'unsigned int' words. */ #ifdef WORDS_BIGENDIAN @@ -237,7 +206,7 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* Negative zero. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%a %d", -zerod, 33, 44, 55); + my_asnprintf (NULL, &length, "%a %d", minus_zerod, 33, 44, 55); ASSERT (result != NULL); if (have_minus_zero ()) ASSERT (strcmp (result, "-0x0p+0 33") == 0); @@ -1064,7 +1033,7 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* Negative zero. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%f %d", -zerod, 33, 44, 55); + my_asnprintf (NULL, &length, "%f %d", minus_zerod, 33, 44, 55); ASSERT (result != NULL); if (have_minus_zero ()) ASSERT (strcmp (result, "-0.000000 33") == 0); @@ -1674,7 +1643,7 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* Negative zero. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%F %d", -zerod, 33, 44, 55); + my_asnprintf (NULL, &length, "%F %d", minus_zerod, 33, 44, 55); ASSERT (result != NULL); if (have_minus_zero ()) ASSERT (strcmp (result, "-0.000000 33") == 0); @@ -2051,7 +2020,7 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* Negative zero. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%e %d", -zerod, 33, 44, 55); + my_asnprintf (NULL, &length, "%e %d", minus_zerod, 33, 44, 55); ASSERT (result != NULL); if (have_minus_zero ()) ASSERT (strcmp (result, "-0.000000e+00 33") == 0 @@ -2818,7 +2787,7 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* Negative zero. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%g %d", -zerod, 33, 44, 55); + my_asnprintf (NULL, &length, "%g %d", minus_zerod, 33, 44, 55); ASSERT (result != NULL); if (have_minus_zero ()) ASSERT (strcmp (result, "-0 33") == 0); @@ -3584,6 +3553,36 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) } { + size_t length; + char *result = + my_asnprintf (NULL, &length, "%.4000f %d", 1.0, 99); + size_t i; + ASSERT (result != NULL); + ASSERT (result[0] == '1'); + ASSERT (result[1] == '.'); + for (i = 0; i < 4000; i++) + ASSERT (result[2 + i] == '0'); + ASSERT (strcmp (result + 2 + 4000, " 99") == 0); + ASSERT (length == strlen (result)); + free (result); + } + + { + size_t length; + char *result = + my_asnprintf (NULL, &length, "%.511f %d", 1.0, 99); + size_t i; + ASSERT (result != NULL); + ASSERT (result[0] == '1'); + ASSERT (result[1] == '.'); + for (i = 0; i < 511; i++) + ASSERT (result[2 + i] == '0'); + ASSERT (strcmp (result + 2 + 511, " 99") == 0); + ASSERT (length == strlen (result)); + free (result); + } + + { char input[5000]; size_t length; char *result; @@ -3649,6 +3648,59 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) } } #endif + +#if HAVE_WCHAR_T + /* Test that converting an invalid wchar_t[] to char[] fails with EILSEQ. */ + { + static const wchar_t input[] = { (wchar_t) 1702057263, 114, 0 }; + size_t length; + char *result = my_asnprintf (NULL, &length, "%ls %d", input, 99); + if (result == NULL) + ASSERT (errno == EILSEQ); + else + free (result); + } + { + static const wchar_t input[] = { (wchar_t) 1702057263, 114, 0 }; + size_t length; + char *result = my_asnprintf (NULL, &length, "%3ls %d", input, 99); + if (result == NULL) + ASSERT (errno == EILSEQ); + else + free (result); + } + { + static const wchar_t input[] = { (wchar_t) 1702057263, 114, 0 }; + size_t length; + char *result = my_asnprintf (NULL, &length, "%.1ls %d", input, 99); + if (result == NULL) + ASSERT (errno == EILSEQ); + else + free (result); + } + { + static const wchar_t input[] = { (wchar_t) 1702057263, 114, 0 }; + size_t length; + char *result = my_asnprintf (NULL, &length, "%3.1ls %d", input, 99); + if (result == NULL) + ASSERT (errno == EILSEQ); + else + free (result); + } +#endif + +#if (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2)) && !defined __UCLIBC__ + /* Test that the 'I' flag is supported. */ + { + size_t length; + char *result = + my_asnprintf (NULL, &length, "%Id %d", 1234567, 99); + ASSERT (result != NULL); + ASSERT (strcmp (result, "1234567 99") == 0); + ASSERT (length == strlen (result)); + free (result); + } +#endif } static char *