diff options
author | Bruno Haible <bruno@clisp.org> | 2008-04-19 17:26:36 +0200 |
---|---|---|
committer | Bruno Haible <bruno@clisp.org> | 2008-04-19 17:26:36 +0200 |
commit | f797ae74856a21b4471baec28dbbfd82b48fe2dc (patch) | |
tree | fc1a1a25bd9a89d66437e96c4259e249b3599557 /tests | |
parent | 2e8956774250d3f5636898fea193c5abaf041d08 (diff) | |
download | gnulib-f797ae74856a21b4471baec28dbbfd82b48fe2dc.tar.gz |
Fix rounding when a precision is given.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test-fprintf-posix.h | 26 | ||||
-rw-r--r-- | tests/test-printf-posix.h | 26 | ||||
-rw-r--r-- | tests/test-printf-posix.output | 8 | ||||
-rw-r--r-- | tests/test-snprintf-posix.h | 130 | ||||
-rw-r--r-- | tests/test-sprintf-posix.h | 130 | ||||
-rw-r--r-- | tests/test-vasnprintf-posix.c | 162 | ||||
-rw-r--r-- | tests/test-vasprintf-posix.c | 162 |
7 files changed, 642 insertions, 2 deletions
diff --git a/tests/test-fprintf-posix.h b/tests/test-fprintf-posix.h index fc44351ce9..c0ee71de48 100644 --- a/tests/test-fprintf-posix.h +++ b/tests/test-fprintf-posix.h @@ -1,5 +1,5 @@ /* Test of POSIX compatible vsprintf() and sprintf() 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 @@ -65,6 +65,12 @@ test_function (int (*my_fprintf) (FILE *, const char *, ...)) /* Precision. */ my_fprintf (stdout, "%.f %d\n", 1234.0, 33, 44, 55); + /* Precision with no rounding. */ + my_fprintf (stdout, "%.2f %d\n", 999.95, 33, 44, 55); + + /* Precision with rounding. */ + my_fprintf (stdout, "%.2f %d\n", 999.996, 33, 44, 55); + /* A positive number. */ my_fprintf (stdout, "%Lf %d\n", 12.75L, 33, 44, 55); @@ -83,6 +89,12 @@ test_function (int (*my_fprintf) (FILE *, const char *, ...)) /* Precision. */ my_fprintf (stdout, "%.Lf %d\n", 1234.0L, 33, 44, 55); + /* Precision with no rounding. */ + my_fprintf (stdout, "%.2Lf %d\n", 999.95L, 33, 44, 55); + + /* Precision with rounding. */ + my_fprintf (stdout, "%.2Lf %d\n", 999.996L, 33, 44, 55); + /* Test the support of the %F format directive. */ /* A positive number. */ @@ -103,6 +115,12 @@ test_function (int (*my_fprintf) (FILE *, const char *, ...)) /* Precision. */ my_fprintf (stdout, "%.F %d\n", 1234.0, 33, 44, 55); + /* Precision with no rounding. */ + my_fprintf (stdout, "%.2F %d\n", 999.95, 33, 44, 55); + + /* Precision with rounding. */ + my_fprintf (stdout, "%.2F %d\n", 999.996, 33, 44, 55); + /* A positive number. */ my_fprintf (stdout, "%LF %d\n", 12.75L, 33, 44, 55); @@ -121,6 +139,12 @@ test_function (int (*my_fprintf) (FILE *, const char *, ...)) /* Precision. */ my_fprintf (stdout, "%.LF %d\n", 1234.0L, 33, 44, 55); + /* Precision with no rounding. */ + my_fprintf (stdout, "%.2LF %d\n", 999.95L, 33, 44, 55); + + /* Precision with rounding. */ + my_fprintf (stdout, "%.2LF %d\n", 999.996L, 33, 44, 55); + /* Test the support of the POSIX/XSI format strings with positions. */ my_fprintf (stdout, "%2$d %1$d\n", 33, 55); diff --git a/tests/test-printf-posix.h b/tests/test-printf-posix.h index 37caf86795..c1ba7a5525 100644 --- a/tests/test-printf-posix.h +++ b/tests/test-printf-posix.h @@ -1,5 +1,5 @@ /* Test of POSIX compatible vsprintf() and sprintf() 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 @@ -67,6 +67,12 @@ test_function (int (*my_printf) (const char *, ...)) /* Precision. */ my_printf ("%.f %d\n", 1234.0, 33, 44, 55); + /* Precision with no rounding. */ + my_printf ("%.2f %d\n", 999.95, 33, 44, 55); + + /* Precision with rounding. */ + my_printf ("%.2f %d\n", 999.996, 33, 44, 55); + /* A positive number. */ my_printf ("%Lf %d\n", 12.75L, 33, 44, 55); @@ -85,6 +91,12 @@ test_function (int (*my_printf) (const char *, ...)) /* Precision. */ my_printf ("%.Lf %d\n", 1234.0L, 33, 44, 55); + /* Precision with no rounding. */ + my_printf ("%.2Lf %d\n", 999.95L, 33, 44, 55); + + /* Precision with rounding. */ + my_printf ("%.2Lf %d\n", 999.996L, 33, 44, 55); + /* Test the support of the %F format directive. */ /* A positive number. */ @@ -105,6 +117,12 @@ test_function (int (*my_printf) (const char *, ...)) /* Precision. */ my_printf ("%.F %d\n", 1234.0, 33, 44, 55); + /* Precision with no rounding. */ + my_printf ("%.2F %d\n", 999.95, 33, 44, 55); + + /* Precision with rounding. */ + my_printf ("%.2F %d\n", 999.996, 33, 44, 55); + /* A positive number. */ my_printf ("%LF %d\n", 12.75L, 33, 44, 55); @@ -123,6 +141,12 @@ test_function (int (*my_printf) (const char *, ...)) /* Precision. */ my_printf ("%.LF %d\n", 1234.0L, 33, 44, 55); + /* Precision with no rounding. */ + my_printf ("%.2LF %d\n", 999.95L, 33, 44, 55); + + /* Precision with rounding. */ + my_printf ("%.2LF %d\n", 999.996L, 33, 44, 55); + /* Test the support of the POSIX/XSI format strings with positions. */ my_printf ("%2$d %1$d\n", 33, 55); diff --git a/tests/test-printf-posix.output b/tests/test-printf-posix.output index 4a6a170649..618825bb7b 100644 --- a/tests/test-printf-posix.output +++ b/tests/test-printf-posix.output @@ -11,22 +11,30 @@ inf 33 0.000000 33 00001234.000000 33 1234 33 +999.95 33 +1000.00 33 12.750000 33 1234567.000000 33 -0.031250 33 0.000000 33 00001234.000000 33 1234 33 +999.95 33 +1000.00 33 12.750000 33 1234567.000000 33 -0.031250 33 0.000000 33 00001234.000000 33 1234 33 +999.95 33 +1000.00 33 12.750000 33 1234567.000000 33 -0.031250 33 0.000000 33 00001234.000000 33 1234 33 +999.95 33 +1000.00 33 55 33 diff --git a/tests/test-snprintf-posix.h b/tests/test-snprintf-posix.h index 570f8b77da..47ad86c4be 100644 --- a/tests/test-snprintf-posix.h +++ b/tests/test-snprintf-posix.h @@ -1004,6 +1004,22 @@ test_function (int (*my_snprintf) (char *, size_t, const char *, ...)) ASSERT (retval == strlen (result)); } + { /* Precision with no rounding. */ + char result[100]; + int retval = + my_snprintf (result, sizeof (result), "%.2f %d", 999.951, 33, 44, 55); + ASSERT (strcmp (result, "999.95 33") == 0); + ASSERT (retval == strlen (result)); + } + + { /* Precision with rounding. */ + char result[100]; + int retval = + my_snprintf (result, sizeof (result), "%.2f %d", 999.996, 33, 44, 55); + ASSERT (strcmp (result, "1000.00 33") == 0); + ASSERT (retval == strlen (result)); + } + { /* A positive number. */ char result[100]; int retval = @@ -1338,6 +1354,22 @@ test_function (int (*my_snprintf) (char *, size_t, const char *, ...)) ASSERT (retval == strlen (result)); } + { /* Precision with no rounding. */ + char result[100]; + int retval = + my_snprintf (result, sizeof (result), "%.2Lf %d", 999.951L, 33, 44, 55); + ASSERT (strcmp (result, "999.95 33") == 0); + ASSERT (retval == strlen (result)); + } + + { /* Precision with rounding. */ + char result[100]; + int retval = + my_snprintf (result, sizeof (result), "%.2Lf %d", 999.996L, 33, 44, 55); + ASSERT (strcmp (result, "1000.00 33") == 0); + ASSERT (retval == strlen (result)); + } + /* Test the support of the %F format directive. */ { /* A positive number. */ @@ -1434,6 +1466,22 @@ test_function (int (*my_snprintf) (char *, size_t, const char *, ...)) ASSERT (retval == strlen (result)); } + { /* Precision with no rounding. */ + char result[100]; + int retval = + my_snprintf (result, sizeof (result), "%.2F %d", 999.951, 33, 44, 55); + ASSERT (strcmp (result, "999.95 33") == 0); + ASSERT (retval == strlen (result)); + } + + { /* Precision with rounding. */ + char result[100]; + int retval = + my_snprintf (result, sizeof (result), "%.2F %d", 999.996, 33, 44, 55); + ASSERT (strcmp (result, "1000.00 33") == 0); + ASSERT (retval == strlen (result)); + } + { /* A positive number. */ char result[100]; int retval = @@ -1528,6 +1576,22 @@ test_function (int (*my_snprintf) (char *, size_t, const char *, ...)) ASSERT (retval == strlen (result)); } + { /* Precision with no rounding. */ + char result[100]; + int retval = + my_snprintf (result, sizeof (result), "%.2LF %d", 999.951L, 33, 44, 55); + ASSERT (strcmp (result, "999.95 33") == 0); + ASSERT (retval == strlen (result)); + } + + { /* Precision with rounding. */ + char result[100]; + int retval = + my_snprintf (result, sizeof (result), "%.2LF %d", 999.996L, 33, 44, 55); + ASSERT (strcmp (result, "1000.00 33") == 0); + ASSERT (retval == strlen (result)); + } + /* Test the support of the %e format directive. */ { /* A positive number. */ @@ -1802,6 +1866,24 @@ test_function (int (*my_snprintf) (char *, size_t, const char *, ...)) ASSERT (retval == strlen (result)); } + { /* Precision with no rounding. */ + char result[100]; + int retval = + my_snprintf (result, sizeof (result), "%.4e %d", 999.951, 33, 44, 55); + ASSERT (strcmp (result, "9.9995e+02 33") == 0 + || strcmp (result, "9.9995e+002 33") == 0); + ASSERT (retval == strlen (result)); + } + + { /* Precision with rounding. */ + char result[100]; + int retval = + my_snprintf (result, sizeof (result), "%.4e %d", 999.996, 33, 44, 55); + ASSERT (strcmp (result, "1.0000e+03 33") == 0 + || strcmp (result, "1.0000e+003 33") == 0); + ASSERT (retval == strlen (result)); + } + { /* A positive number. */ char result[100]; int retval = @@ -2138,6 +2220,22 @@ test_function (int (*my_snprintf) (char *, size_t, const char *, ...)) ASSERT (retval == strlen (result)); } + { /* Precision with no rounding. */ + char result[100]; + int retval = + my_snprintf (result, sizeof (result), "%.4Le %d", 999.951L, 33, 44, 55); + ASSERT (strcmp (result, "9.9995e+02 33") == 0); + ASSERT (retval == strlen (result)); + } + + { /* Precision with rounding. */ + char result[100]; + int retval = + my_snprintf (result, sizeof (result), "%.4Le %d", 999.996L, 33, 44, 55); + ASSERT (strcmp (result, "1.0000e+03 33") == 0); + ASSERT (retval == strlen (result)); + } + /* Test the support of the %g format directive. */ { /* A positive number. */ @@ -2401,6 +2499,22 @@ test_function (int (*my_snprintf) (char *, size_t, const char *, ...)) ASSERT (retval == strlen (result)); } + { /* Precision with no rounding. */ + char result[100]; + int retval = + my_snprintf (result, sizeof (result), "%.5g %d", 999.951, 33, 44, 55); + ASSERT (strcmp (result, "999.95 33") == 0); + ASSERT (retval == strlen (result)); + } + + { /* Precision with rounding. */ + char result[100]; + int retval = + my_snprintf (result, sizeof (result), "%.5g %d", 999.996, 33, 44, 55); + ASSERT (strcmp (result, "1000 33") == 0); + ASSERT (retval == strlen (result)); + } + { /* A positive number. */ char result[100]; int retval = @@ -2737,6 +2851,22 @@ test_function (int (*my_snprintf) (char *, size_t, const char *, ...)) ASSERT (retval == strlen (result)); } + { /* Precision with no rounding. */ + char result[100]; + int retval = + my_snprintf (result, sizeof (result), "%.5Lg %d", 999.951L, 33, 44, 55); + ASSERT (strcmp (result, "999.95 33") == 0); + ASSERT (retval == strlen (result)); + } + + { /* Precision with rounding. */ + char result[100]; + int retval = + my_snprintf (result, sizeof (result), "%.5Lg %d", 999.996L, 33, 44, 55); + ASSERT (strcmp (result, "1000 33") == 0); + ASSERT (retval == strlen (result)); + } + /* Test the support of the %n format directive. */ { diff --git a/tests/test-sprintf-posix.h b/tests/test-sprintf-posix.h index afe7ea28e5..8e8539ad5b 100644 --- a/tests/test-sprintf-posix.h +++ b/tests/test-sprintf-posix.h @@ -984,6 +984,22 @@ test_function (int (*my_sprintf) (char *, const char *, ...)) ASSERT (retval == strlen (result)); } + { /* Precision with no rounding. */ + char result[100]; + int retval = + my_sprintf (result, "%.2f %d", 999.951, 33, 44, 55); + ASSERT (strcmp (result, "999.95 33") == 0); + ASSERT (retval == strlen (result)); + } + + { /* Precision with rounding. */ + char result[100]; + int retval = + my_sprintf (result, "%.2f %d", 999.996, 33, 44, 55); + ASSERT (strcmp (result, "1000.00 33") == 0); + ASSERT (retval == strlen (result)); + } + { /* A positive number. */ char result[1000]; int retval = @@ -1312,6 +1328,22 @@ test_function (int (*my_sprintf) (char *, const char *, ...)) ASSERT (retval == strlen (result)); } + { /* Precision with no rounding. */ + char result[100]; + int retval = + my_sprintf (result, "%.2Lf %d", 999.951L, 33, 44, 55); + ASSERT (strcmp (result, "999.95 33") == 0); + ASSERT (retval == strlen (result)); + } + + { /* Precision with rounding. */ + char result[100]; + int retval = + my_sprintf (result, "%.2Lf %d", 999.996L, 33, 44, 55); + ASSERT (strcmp (result, "1000.00 33") == 0); + ASSERT (retval == strlen (result)); + } + /* Test the support of the %F format directive. */ { /* A positive number. */ @@ -1408,6 +1440,22 @@ test_function (int (*my_sprintf) (char *, const char *, ...)) ASSERT (retval == strlen (result)); } + { /* Precision with no rounding. */ + char result[100]; + int retval = + my_sprintf (result, "%.2F %d", 999.951, 33, 44, 55); + ASSERT (strcmp (result, "999.95 33") == 0); + ASSERT (retval == strlen (result)); + } + + { /* Precision with rounding. */ + char result[100]; + int retval = + my_sprintf (result, "%.2F %d", 999.996, 33, 44, 55); + ASSERT (strcmp (result, "1000.00 33") == 0); + ASSERT (retval == strlen (result)); + } + { /* A positive number. */ char result[1000]; int retval = @@ -1502,6 +1550,22 @@ test_function (int (*my_sprintf) (char *, const char *, ...)) ASSERT (retval == strlen (result)); } + { /* Precision with no rounding. */ + char result[100]; + int retval = + my_sprintf (result, "%.2LF %d", 999.951L, 33, 44, 55); + ASSERT (strcmp (result, "999.95 33") == 0); + ASSERT (retval == strlen (result)); + } + + { /* Precision with rounding. */ + char result[100]; + int retval = + my_sprintf (result, "%.2LF %d", 999.996L, 33, 44, 55); + ASSERT (strcmp (result, "1000.00 33") == 0); + ASSERT (retval == strlen (result)); + } + /* Test the support of the %e format directive. */ { /* A positive number. */ @@ -1776,6 +1840,24 @@ test_function (int (*my_sprintf) (char *, const char *, ...)) ASSERT (retval == strlen (result)); } + { /* Precision with no rounding. */ + char result[100]; + int retval = + my_sprintf (result, "%.4e %d", 999.951, 33, 44, 55); + ASSERT (strcmp (result, "9.9995e+02 33") == 0 + || strcmp (result, "9.9995e+002 33") == 0); + ASSERT (retval == strlen (result)); + } + + { /* Precision with rounding. */ + char result[100]; + int retval = + my_sprintf (result, "%.4e %d", 999.996, 33, 44, 55); + ASSERT (strcmp (result, "1.0000e+03 33") == 0 + || strcmp (result, "1.0000e+003 33") == 0); + ASSERT (retval == strlen (result)); + } + { /* A positive number. */ char result[1000]; int retval = @@ -2112,6 +2194,22 @@ test_function (int (*my_sprintf) (char *, const char *, ...)) ASSERT (retval == strlen (result)); } + { /* Precision with no rounding. */ + char result[100]; + int retval = + my_sprintf (result, "%.4Le %d", 999.951L, 33, 44, 55); + ASSERT (strcmp (result, "9.9995e+02 33") == 0); + ASSERT (retval == strlen (result)); + } + + { /* Precision with rounding. */ + char result[100]; + int retval = + my_sprintf (result, "%.4Le %d", 999.996L, 33, 44, 55); + ASSERT (strcmp (result, "1.0000e+03 33") == 0); + ASSERT (retval == strlen (result)); + } + /* Test the support of the %g format directive. */ { /* A positive number. */ @@ -2375,6 +2473,22 @@ test_function (int (*my_sprintf) (char *, const char *, ...)) ASSERT (retval == strlen (result)); } + { /* Precision with no rounding. */ + char result[100]; + int retval = + my_sprintf (result, "%.5g %d", 999.951, 33, 44, 55); + ASSERT (strcmp (result, "999.95 33") == 0); + ASSERT (retval == strlen (result)); + } + + { /* Precision with rounding. */ + char result[100]; + int retval = + my_sprintf (result, "%.5g %d", 999.996, 33, 44, 55); + ASSERT (strcmp (result, "1000 33") == 0); + ASSERT (retval == strlen (result)); + } + { /* A positive number. */ char result[1000]; int retval = @@ -2711,6 +2825,22 @@ test_function (int (*my_sprintf) (char *, const char *, ...)) ASSERT (retval == strlen (result)); } + { /* Precision with no rounding. */ + char result[100]; + int retval = + my_sprintf (result, "%.5Lg %d", 999.951L, 33, 44, 55); + ASSERT (strcmp (result, "999.95 33") == 0); + ASSERT (retval == strlen (result)); + } + + { /* Precision with rounding. */ + char result[100]; + int retval = + my_sprintf (result, "%.5Lg %d", 999.996L, 33, 44, 55); + ASSERT (strcmp (result, "1000 33") == 0); + ASSERT (retval == strlen (result)); + } + /* Test the support of the %n format directive. */ { diff --git a/tests/test-vasnprintf-posix.c b/tests/test-vasnprintf-posix.c index 84da8b2b7c..895c807c7c 100644 --- a/tests/test-vasnprintf-posix.c +++ b/tests/test-vasnprintf-posix.c @@ -1189,6 +1189,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 = @@ -1569,6 +1589,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. */ @@ -1687,6 +1727,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 = @@ -1803,6 +1863,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. */ @@ -2116,6 +2196,28 @@ 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 = @@ -2506,6 +2608,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, "%.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. */ @@ -2809,6 +2931,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 = @@ -3199,6 +3341,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, "%.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. */ { diff --git a/tests/test-vasprintf-posix.c b/tests/test-vasprintf-posix.c index 93d8e87264..bd9039f5a2 100644 --- a/tests/test-vasprintf-posix.c +++ b/tests/test-vasprintf-posix.c @@ -1170,6 +1170,26 @@ test_function (int (*my_asprintf) (char **, const char *, ...)) free (result); } + { /* Precision with no rounding. */ + char *result; + int retval = + my_asprintf (&result, "%.2f %d", 999.951, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "999.95 33") == 0); + ASSERT (retval == strlen (result)); + free (result); + } + + { /* Precision with rounding. */ + char *result; + int retval = + my_asprintf (&result, "%.2f %d", 999.996, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "1000.00 33") == 0); + ASSERT (retval == strlen (result)); + free (result); + } + { /* A positive number. */ char *result; int retval = @@ -1550,6 +1570,26 @@ test_function (int (*my_asprintf) (char **, const char *, ...)) free (result); } + { /* Precision with no rounding. */ + char *result; + int retval = + my_asprintf (&result, "%.2Lf %d", 999.951L, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "999.95 33") == 0); + ASSERT (retval == strlen (result)); + free (result); + } + + { /* Precision with rounding. */ + char *result; + int retval = + my_asprintf (&result, "%.2Lf %d", 999.996L, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "1000.00 33") == 0); + ASSERT (retval == strlen (result)); + free (result); + } + /* Test the support of the %F format directive. */ { /* A positive number. */ @@ -1668,6 +1708,26 @@ test_function (int (*my_asprintf) (char **, const char *, ...)) free (result); } + { /* Precision with no rounding. */ + char *result; + int retval = + my_asprintf (&result, "%.2F %d", 999.951, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "999.95 33") == 0); + ASSERT (retval == strlen (result)); + free (result); + } + + { /* Precision with rounding. */ + char *result; + int retval = + my_asprintf (&result, "%.2F %d", 999.996, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "1000.00 33") == 0); + ASSERT (retval == strlen (result)); + free (result); + } + { /* A positive number. */ char *result; int retval = @@ -1784,6 +1844,26 @@ test_function (int (*my_asprintf) (char **, const char *, ...)) free (result); } + { /* Precision with no rounding. */ + char *result; + int retval = + my_asprintf (&result, "%.2LF %d", 999.951L, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "999.95 33") == 0); + ASSERT (retval == strlen (result)); + free (result); + } + + { /* Precision with rounding. */ + char *result; + int retval = + my_asprintf (&result, "%.2LF %d", 999.996L, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "1000.00 33") == 0); + ASSERT (retval == strlen (result)); + free (result); + } + /* Test the support of the %e format directive. */ { /* A positive number. */ @@ -2097,6 +2177,28 @@ test_function (int (*my_asprintf) (char **, const char *, ...)) free (result); } + { /* Precision with no rounding. */ + char *result; + int retval = + my_asprintf (&result, "%.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 (retval == strlen (result)); + free (result); + } + + { /* Precision with rounding. */ + char *result; + int retval = + my_asprintf (&result, "%.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 (retval == strlen (result)); + free (result); + } + { /* A positive number. */ char *result; int retval = @@ -2487,6 +2589,26 @@ test_function (int (*my_asprintf) (char **, const char *, ...)) free (result); } + { /* Precision with no rounding. */ + char *result; + int retval = + my_asprintf (&result, "%.4Le %d", 999.951L, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "9.9995e+02 33") == 0); + ASSERT (retval == strlen (result)); + free (result); + } + + { /* Precision with rounding. */ + char *result; + int retval = + my_asprintf (&result, "%.4Le %d", 999.996L, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "1.0000e+03 33") == 0); + ASSERT (retval == strlen (result)); + free (result); + } + /* Test the support of the %g format directive. */ { /* A positive number. */ @@ -2790,6 +2912,26 @@ test_function (int (*my_asprintf) (char **, const char *, ...)) free (result); } + { /* Precision with no rounding. */ + char *result; + int retval = + my_asprintf (&result, "%.5g %d", 999.951, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "999.95 33") == 0); + ASSERT (retval == strlen (result)); + free (result); + } + + { /* Precision with rounding. */ + char *result; + int retval = + my_asprintf (&result, "%.5g %d", 999.996, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "1000 33") == 0); + ASSERT (retval == strlen (result)); + free (result); + } + { /* A positive number. */ char *result; int retval = @@ -3180,6 +3322,26 @@ test_function (int (*my_asprintf) (char **, const char *, ...)) free (result); } + { /* Precision with no rounding. */ + char *result; + int retval = + my_asprintf (&result, "%.5Lg %d", 999.951L, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "999.95 33") == 0); + ASSERT (retval == strlen (result)); + free (result); + } + + { /* Precision with rounding. */ + char *result; + int retval = + my_asprintf (&result, "%.5Lg %d", 999.996L, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "1000 33") == 0); + ASSERT (retval == strlen (result)); + free (result); + } + /* Test the support of the %n format directive. */ { |