X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=tests%2Ftest-vasnprintf-posix.c;h=aa537fbd74dd4e6da5bb28064db1126578eaf7be;hb=a9bf022b6d70c5f3a55cf5b2a2b36a4a3f96da6c;hp=4605cd1d9934f6b50e010b9fd5351af65a3be7a4;hpb=0a56509612c0b43a3f05361ecfb73b8b39f8e66c;p=gnulib.git diff --git a/tests/test-vasnprintf-posix.c b/tests/test-vasnprintf-posix.c index 4605cd1d9..aa537fbd7 100644 --- a/tests/test-vasnprintf-posix.c +++ b/tests/test-vasnprintf-posix.c @@ -37,6 +37,7 @@ if (!(expr)) \ { \ fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \ + fflush (stderr); \ abort (); \ } \ } \ @@ -47,10 +48,18 @@ 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 -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. */ +long double minus_zerol = -LDBL_MIN * LDBL_MIN; + /* Representation of an 80-bit 'long double' as an initializer for a sequence of 'unsigned int' words. */ #ifdef WORDS_BIGENDIAN @@ -216,7 +225,7 @@ 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); @@ -530,7 +539,7 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* Negative zero. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%La %d", -0.0L, 33, 44, 55); + my_asnprintf (NULL, &length, "%La %d", minus_zerol, 33, 44, 55); ASSERT (result != NULL); if (have_minus_zero ()) ASSERT (strcmp (result, "-0x0p+0 33") == 0); @@ -1043,7 +1052,7 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* 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); @@ -1188,6 +1197,26 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) 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 = @@ -1322,7 +1351,7 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* Negative zero. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%Lf %d", -0.0L, 33, 44, 55); + my_asnprintf (NULL, &length, "%Lf %d", minus_zerol, 33, 44, 55); ASSERT (result != NULL); if (have_minus_zero ()) ASSERT (strcmp (result, "-0.000000 33") == 0); @@ -1568,6 +1597,26 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) 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. */ @@ -1613,7 +1662,7 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* 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); @@ -1686,6 +1735,26 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) 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 = @@ -1729,7 +1798,7 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* Negative zero. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%LF %d", -0.0L, 33, 44, 55); + my_asnprintf (NULL, &length, "%LF %d", minus_zerol, 33, 44, 55); ASSERT (result != NULL); if (have_minus_zero ()) ASSERT (strcmp (result, "-0.000000 33") == 0); @@ -1802,6 +1871,26 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) 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. */ @@ -1950,7 +2039,7 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* 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 @@ -2115,12 +2204,35 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) 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 = my_asnprintf (NULL, &length, "%Le %d", 12.75L, 33, 44, 55); ASSERT (result != NULL); - ASSERT (strcmp (result, "1.275000e+01 33") == 0); + ASSERT (strcmp (result, "1.275000e+01 33") == 0 + || strcmp (result, "1.275000e+001 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -2130,7 +2242,8 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) char *result = my_asnprintf (NULL, &length, "%Le %d", 1234567.0L, 33, 44, 55); ASSERT (result != NULL); - ASSERT (strcmp (result, "1.234567e+06 33") == 0); + ASSERT (strcmp (result, "1.234567e+06 33") == 0 + || strcmp (result, "1.234567e+006 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -2219,8 +2332,16 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) size_t length; char *result = my_asnprintf (NULL, &length, "%Le", data[k].value); + const char *expected = data[k].string; ASSERT (result != NULL); - ASSERT (strcmp (result, data[k].string) == 0); + ASSERT (strcmp (result, expected) == 0 + /* Some implementations produce exponents with 3 digits. */ + || (strlen (result) == strlen (expected) + 1 + && memcmp (result, expected, strlen (expected) - 2) == 0 + && result[strlen (expected) - 2] == '0' + && strcmp (result + strlen (expected) - 1, + expected + strlen (expected) - 2) + == 0)); ASSERT (length == strlen (result)); free (result); } @@ -2231,7 +2352,8 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) char *result = my_asnprintf (NULL, &length, "%Le %d", -0.03125L, 33, 44, 55); ASSERT (result != NULL); - ASSERT (strcmp (result, "-3.125000e-02 33") == 0); + ASSERT (strcmp (result, "-3.125000e-02 33") == 0 + || strcmp (result, "-3.125000e-002 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -2241,7 +2363,8 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) char *result = my_asnprintf (NULL, &length, "%Le %d", 0.0L, 33, 44, 55); ASSERT (result != NULL); - ASSERT (strcmp (result, "0.000000e+00 33") == 0); + ASSERT (strcmp (result, "0.000000e+00 33") == 0 + || strcmp (result, "0.000000e+000 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -2249,10 +2372,11 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* Negative zero. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%Le %d", -0.0L, 33, 44, 55); + my_asnprintf (NULL, &length, "%Le %d", minus_zerol, 33, 44, 55); ASSERT (result != NULL); if (have_minus_zero ()) - ASSERT (strcmp (result, "-0.000000e+00 33") == 0); + ASSERT (strcmp (result, "-0.000000e+00 33") == 0 + || strcmp (result, "-0.000000e+000 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -2397,7 +2521,8 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) char *result = my_asnprintf (NULL, &length, "%15Le %d", 1.75L, 33, 44, 55); ASSERT (result != NULL); - ASSERT (strcmp (result, " 1.750000e+00 33") == 0); + ASSERT (strcmp (result, " 1.750000e+00 33") == 0 + || strcmp (result, " 1.750000e+000 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -2407,7 +2532,8 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) char *result = my_asnprintf (NULL, &length, "%-15Le %d", 1.75L, 33, 44, 55); ASSERT (result != NULL); - ASSERT (strcmp (result, "1.750000e+00 33") == 0); + ASSERT (strcmp (result, "1.750000e+00 33") == 0 + || strcmp (result, "1.750000e+000 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -2417,7 +2543,8 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) char *result = my_asnprintf (NULL, &length, "%+Le %d", 1.75L, 33, 44, 55); ASSERT (result != NULL); - ASSERT (strcmp (result, "+1.750000e+00 33") == 0); + ASSERT (strcmp (result, "+1.750000e+00 33") == 0 + || strcmp (result, "+1.750000e+000 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -2427,7 +2554,8 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) char *result = my_asnprintf (NULL, &length, "% Le %d", 1.75L, 33, 44, 55); ASSERT (result != NULL); - ASSERT (strcmp (result, " 1.750000e+00 33") == 0); + ASSERT (strcmp (result, " 1.750000e+00 33") == 0 + || strcmp (result, " 1.750000e+000 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -2437,7 +2565,8 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) char *result = my_asnprintf (NULL, &length, "%#Le %d", 1.75L, 33, 44, 55); ASSERT (result != NULL); - ASSERT (strcmp (result, "1.750000e+00 33") == 0); + ASSERT (strcmp (result, "1.750000e+00 33") == 0 + || strcmp (result, "1.750000e+000 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -2447,7 +2576,8 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) char *result = my_asnprintf (NULL, &length, "%#.Le %d", 1.75L, 33, 44, 55); ASSERT (result != NULL); - ASSERT (strcmp (result, "2.e+00 33") == 0); + ASSERT (strcmp (result, "2.e+00 33") == 0 + || strcmp (result, "2.e+000 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -2457,7 +2587,8 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) char *result = my_asnprintf (NULL, &length, "%#.Le %d", 9.75L, 33, 44, 55); ASSERT (result != NULL); - ASSERT (strcmp (result, "1.e+01 33") == 0); + ASSERT (strcmp (result, "1.e+01 33") == 0 + || strcmp (result, "1.e+001 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -2467,7 +2598,8 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) char *result = my_asnprintf (NULL, &length, "%015Le %d", 1234.0L, 33, 44, 55); ASSERT (result != NULL); - ASSERT (strcmp (result, "0001.234000e+03 33") == 0); + ASSERT (strcmp (result, "0001.234000e+03 33") == 0 + || strcmp (result, "001.234000e+003 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -2500,7 +2632,30 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) char *result = my_asnprintf (NULL, &length, "%.Le %d", 1234.0L, 33, 44, 55); ASSERT (result != NULL); - ASSERT (strcmp (result, "1e+03 33") == 0); + ASSERT (strcmp (result, "1e+03 33") == 0 + || strcmp (result, "1e+003 33") == 0); + ASSERT (length == strlen (result)); + 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 + || 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, "%.4Le %d", 999.996L, 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); } @@ -2651,7 +2806,7 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* 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); @@ -2808,6 +2963,26 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) 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 = @@ -2823,7 +2998,8 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) char *result = my_asnprintf (NULL, &length, "%Lg %d", 1234567.0L, 33, 44, 55); ASSERT (result != NULL); - ASSERT (strcmp (result, "1.23457e+06 33") == 0); + ASSERT (strcmp (result, "1.23457e+06 33") == 0 + || strcmp (result, "1.23457e+006 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -2912,8 +3088,17 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) size_t length; char *result = my_asnprintf (NULL, &length, "%Lg", data[k].value); + const char *expected = data[k].string; ASSERT (result != NULL); - ASSERT (strcmp (result, data[k].string) == 0); + ASSERT (strcmp (result, expected) == 0 + /* Some implementations produce exponents with 3 digits. */ + || (expected[strlen (expected) - 4] == 'e' + && strlen (result) == strlen (expected) + 1 + && memcmp (result, expected, strlen (expected) - 2) == 0 + && result[strlen (expected) - 2] == '0' + && strcmp (result + strlen (expected) - 1, + expected + strlen (expected) - 2) + == 0)); ASSERT (length == strlen (result)); free (result); } @@ -2942,7 +3127,7 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* Negative zero. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%Lg %d", -0.0L, 33, 44, 55); + my_asnprintf (NULL, &length, "%Lg %d", minus_zerol, 33, 44, 55); ASSERT (result != NULL); if (have_minus_zero ()) ASSERT (strcmp (result, "-0 33") == 0); @@ -3150,7 +3335,8 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) char *result = my_asnprintf (NULL, &length, "%#.Lg %d", 9.75L, 33, 44, 55); ASSERT (result != NULL); - ASSERT (strcmp (result, "1.e+01 33") == 0); + ASSERT (strcmp (result, "1.e+01 33") == 0 + || strcmp (result, "1.e+001 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -3193,7 +3379,28 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) char *result = my_asnprintf (NULL, &length, "%.Lg %d", 1234.0L, 33, 44, 55); ASSERT (result != NULL); - ASSERT (strcmp (result, "1e+03 33") == 0); + ASSERT (strcmp (result, "1e+03 33") == 0 + || strcmp (result, "1e+003 33") == 0); + ASSERT (length == strlen (result)); + 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); } @@ -3286,6 +3493,19 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { size_t length; char *result = + my_asnprintf (NULL, &length, "%.*d %d", 4000, 1234567, 99); + size_t i; + ASSERT (result != NULL); + for (i = 0; i < 4000 - 7; i++) + ASSERT (result[i] == '0'); + ASSERT (strcmp (result + 4000 - 7, "1234567 99") == 0); + ASSERT (length == strlen (result)); + free (result); + } + + { + size_t length; + char *result = my_asnprintf (NULL, &length, "%.4000d %d", -1234567, 99); size_t i; ASSERT (result != NULL);