From 8bad5bfa3b5e8d8c347ae65c12a91a98d62b0736 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Fri, 5 Nov 2004 16:09:02 +0000 Subject: [PATCH] Portability fixes and extra sanity checks. (#157453, Morten Welinder) 2004-11-05 Matthias Clasen * tests/strtod-test.c: Portability fixes and extra sanity checks. (#157453, Morten Welinder) --- ChangeLog | 5 ++++ ChangeLog.pre-2-10 | 5 ++++ ChangeLog.pre-2-12 | 5 ++++ ChangeLog.pre-2-6 | 5 ++++ ChangeLog.pre-2-8 | 5 ++++ tests/strtod-test.c | 60 +++++++++++++++++++++++++++++++-------------- 6 files changed, 67 insertions(+), 18 deletions(-) diff --git a/ChangeLog b/ChangeLog index 690d6aeb6..a6e5ea8fc 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2004-11-05 Matthias Clasen + + * tests/strtod-test.c: Portability fixes and extra sanity + checks. (#157453, Morten Welinder) + 2004-11-04 Matthias Clasen * glib/gstrfuncs.c (g_ascii_strtod): Handle numbers like diff --git a/ChangeLog.pre-2-10 b/ChangeLog.pre-2-10 index 690d6aeb6..a6e5ea8fc 100644 --- a/ChangeLog.pre-2-10 +++ b/ChangeLog.pre-2-10 @@ -1,3 +1,8 @@ +2004-11-05 Matthias Clasen + + * tests/strtod-test.c: Portability fixes and extra sanity + checks. (#157453, Morten Welinder) + 2004-11-04 Matthias Clasen * glib/gstrfuncs.c (g_ascii_strtod): Handle numbers like diff --git a/ChangeLog.pre-2-12 b/ChangeLog.pre-2-12 index 690d6aeb6..a6e5ea8fc 100644 --- a/ChangeLog.pre-2-12 +++ b/ChangeLog.pre-2-12 @@ -1,3 +1,8 @@ +2004-11-05 Matthias Clasen + + * tests/strtod-test.c: Portability fixes and extra sanity + checks. (#157453, Morten Welinder) + 2004-11-04 Matthias Clasen * glib/gstrfuncs.c (g_ascii_strtod): Handle numbers like diff --git a/ChangeLog.pre-2-6 b/ChangeLog.pre-2-6 index 690d6aeb6..a6e5ea8fc 100644 --- a/ChangeLog.pre-2-6 +++ b/ChangeLog.pre-2-6 @@ -1,3 +1,8 @@ +2004-11-05 Matthias Clasen + + * tests/strtod-test.c: Portability fixes and extra sanity + checks. (#157453, Morten Welinder) + 2004-11-04 Matthias Clasen * glib/gstrfuncs.c (g_ascii_strtod): Handle numbers like diff --git a/ChangeLog.pre-2-8 b/ChangeLog.pre-2-8 index 690d6aeb6..a6e5ea8fc 100644 --- a/ChangeLog.pre-2-8 +++ b/ChangeLog.pre-2-8 @@ -1,3 +1,8 @@ +2004-11-05 Matthias Clasen + + * tests/strtod-test.c: Portability fixes and extra sanity + checks. (#157453, Morten Welinder) + 2004-11-04 Matthias Clasen * glib/gstrfuncs.c (g_ascii_strtod): Handle numbers like diff --git a/tests/strtod-test.c b/tests/strtod-test.c index 3566ffe35..3eab6ea2b 100644 --- a/tests/strtod-test.c +++ b/tests/strtod-test.c @@ -16,7 +16,6 @@ test_string (char *number, double res, gboolean check_end, int correct_len) double d; char *locales[] = {"sv_SE", "en_US", "fa_IR", "C", "ru_RU"}; int l; - char *end; char *dummy; /* we try a copy of number, with some free space for malloc before that. @@ -28,24 +27,34 @@ test_string (char *number, double res, gboolean check_end, int correct_len) for (l = 0; l < G_N_ELEMENTS (locales); l++) { + gboolean ok; + char *end = "(unset)"; + setlocale (LC_NUMERIC, locales[l]); d = g_ascii_strtod (number, &end); - if ((isfinite(d) && isfinite(res) && d != res) || - (isnan (d) != isnan (res)) || - (isinf (d) != isinf (res))) + ok = isnan (res) ? isnan (d) : (d == res); + if (!ok) { g_print ("g_ascii_strtod on \"%s\" for locale %s failed\n", number, locales[l]); - g_print ("expected %lf (nan %d inf %d) actual %lf (nan %d inf %d)\n", - res, isnan (res), isinf (res), - d, isnan (d), isinf (d)); - + g_print ("expected %f (nan %d) actual %f (nan %d)\n", + res, isnan (res), + d, isnan (d)); } - if (check_end && end - number != correct_len) - g_print ("g_ascii_strtod on \"%s\" for locale %s endptr was wrong, leftover: %s\n", number, locales[l], end); - if (!check_end && end - number != strlen (number)) - g_print ("g_ascii_strtod on \"%s\" for locale %s endptr was wrong, leftover: %s\n", number, locales[l], end); + + ok = (end - number) == (check_end ? correct_len : strlen (number)); + if (!ok) { + if (end == NULL) + g_print ("g_ascii_strtod on \"%s\" for locale %s endptr was NULL\n", + number, locales[l]); + else if (end >= number && end <= number + strlen (number)) + g_print ("g_ascii_strtod on \"%s\" for locale %s endptr was wrong, leftover: \"%s\"\n", + number, locales[l], end); + else + g_print ("g_ascii_strtod on \"%s\" for locale %s endptr was REALLY wrong (number=%p, end=%p)\n", + number, locales[l], number, end); + } } - + g_free (number); } @@ -53,9 +62,24 @@ test_string (char *number, double res, gboolean check_end, int correct_len) int main () { - gdouble d; + gdouble d, our_nan, our_inf; char buffer[G_ASCII_DTOSTR_BUF_SIZE]; +#ifdef NAN + our_nan = NAN; +#else + /* Do this before any call to setlocale. */ + our_nan = atof ("NaN"); +#endif + g_assert (isnan (our_nan)); + +#ifdef INFINITY + our_inf = INFINITY; +#else + our_inf = atof ("Infinity"); +#endif + g_assert (our_inf > 1 && our_inf == our_inf / 2); + test_string ("123.123", 123.123, FALSE, 0); test_string ("123.123e2", 123.123e2, FALSE, 0); test_string ("123.123e-2", 123.123e-2, FALSE, 0); @@ -67,10 +91,10 @@ main () test_string ("5,4", 5.0, TRUE, 1); /* the following are for #156421 */ test_string ("1e1", 1e1, FALSE, 0); - test_string ("NAN", NAN, FALSE, 0); - test_string ("-nan", -NAN, FALSE, 0); - test_string ("INF", INFINITY, FALSE, 0); - test_string ("-infinity", -INFINITY, FALSE, 0); + test_string ("NAN", our_nan, FALSE, 0); + test_string ("-nan", -our_nan, FALSE, 0); + test_string ("INF", our_inf, FALSE, 0); + test_string ("-infinity", -our_inf, FALSE, 0); d = 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0; g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));