diff options
author | Matthias Clasen <mclasen@redhat.com> | 2004-11-05 03:21:24 +0000 |
---|---|---|
committer | Matthias Clasen <matthiasc@src.gnome.org> | 2004-11-05 03:21:24 +0000 |
commit | c92fb33b42bced81bbd8748dd5dd73916f33d1c4 (patch) | |
tree | 9b32a82ac13ff62e88b43797b0fc16cc786cf066 /tests | |
parent | b7d67f62ecd72ae225e056819394a179043d62a4 (diff) |
Handle numbers like 1e1, nan, -infinity. Also try harder to preserve
2004-11-04 Matthias Clasen <mclasen@redhat.com>
* glib/gstrfuncs.c (g_ascii_strtod): Handle numbers like
1e1, nan, -infinity. Also try harder to preserve errno.
(#156421, Morten Welinder)
* tests/strtod-test.c: Add testcases.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/strtod-test.c | 25 |
1 files changed, 21 insertions, 4 deletions
diff --git a/tests/strtod-test.c b/tests/strtod-test.c index 7e45570ce..3566ffe35 100644 --- a/tests/strtod-test.c +++ b/tests/strtod-test.c @@ -1,15 +1,19 @@ #undef G_DISABLE_ASSERT #undef G_LOG_DOMAIN +/* for NAN and INFINITY */ +#define _ISOC99_SOURCE + #include <glib.h> #include <locale.h> #include <string.h> +#include <stdlib.h> #include <math.h> void test_string (char *number, double res, gboolean check_end, int correct_len) { - gdouble d; + double d; char *locales[] = {"sv_SE", "en_US", "fa_IR", "C", "ru_RU"}; int l; char *end; @@ -26,8 +30,16 @@ test_string (char *number, double res, gboolean check_end, int correct_len) { setlocale (LC_NUMERIC, locales[l]); d = g_ascii_strtod (number, &end); - if (d != res) - g_print ("g_ascii_strtod on \"%s\" for locale %s failed\n", number, locales[l]); + if ((isfinite(d) && isfinite(res) && d != res) || + (isnan (d) != isnan (res)) || + (isinf (d) != isinf (res))) + { + 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)); + + } 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)) @@ -50,10 +62,15 @@ main () 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); - test_string ("1e1", 1e1, FALSE, 0); test_string ("5.4", 5.4, TRUE, 3); test_string ("5.4,5.5", 5.4, TRUE, 3); 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); d = 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0; g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL)); |