1 #undef G_DISABLE_ASSERT
4 /* for NAN and INFINITY */
14 test_string (char *number, double res, gboolean check_end, int correct_len)
17 char *locales[] = {"sv_SE", "en_US", "fa_IR", "C", "ru_RU"};
21 /* we try a copy of number, with some free space for malloc before that.
22 * This is supposed to smash the some wrong pointer calculations. */
24 dummy = g_malloc (100000);
25 number = g_strdup (number);
28 for (l = 0; l < G_N_ELEMENTS (locales); l++)
31 char *end = "(unset)";
33 setlocale (LC_NUMERIC, locales[l]);
34 d = g_ascii_strtod (number, &end);
35 ok = isnan (res) ? isnan (d) : (d == res);
38 g_print ("g_ascii_strtod on \"%s\" for locale %s failed\n", number, locales[l]);
39 g_print ("expected %f (nan %d) actual %f (nan %d)\n",
44 ok = (end - number) == (check_end ? correct_len : strlen (number));
47 g_print ("g_ascii_strtod on \"%s\" for locale %s endptr was NULL\n",
49 else if (end >= number && end <= number + strlen (number))
50 g_print ("g_ascii_strtod on \"%s\" for locale %s endptr was wrong, leftover: \"%s\"\n",
51 number, locales[l], end);
53 g_print ("g_ascii_strtod on \"%s\" for locale %s endptr was REALLY wrong (number=%p, end=%p)\n",
54 number, locales[l], number, end);
65 gdouble d, our_nan, our_inf;
66 char buffer[G_ASCII_DTOSTR_BUF_SIZE];
71 /* Do this before any call to setlocale. */
72 our_nan = atof ("NaN");
74 g_assert (isnan (our_nan));
79 our_inf = atof ("Infinity");
81 g_assert (our_inf > 1 && our_inf == our_inf / 2);
83 test_string ("123.123", 123.123, FALSE, 0);
84 test_string ("123.123e2", 123.123e2, FALSE, 0);
85 test_string ("123.123e-2", 123.123e-2, FALSE, 0);
86 test_string ("-123.123", -123.123, FALSE, 0);
87 test_string ("-123.123e2", -123.123e2, FALSE, 0);
88 test_string ("-123.123e-2", -123.123e-2, FALSE, 0);
89 test_string ("5.4", 5.4, TRUE, 3);
90 test_string ("5.4,5.5", 5.4, TRUE, 3);
91 test_string ("5,4", 5.0, TRUE, 1);
92 /* the following are for #156421 */
93 test_string ("1e1", 1e1, FALSE, 0);
94 test_string ("NAN", our_nan, FALSE, 0);
95 test_string ("-nan", -our_nan, FALSE, 0);
96 test_string ("INF", our_inf, FALSE, 0);
97 test_string ("-infinity", -our_inf, FALSE, 0);
98 test_string ("-.75,0", -0.75, TRUE, 4);
100 d = 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0;
101 g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
103 d = -179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0;
104 g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
106 d = pow (2.0, -1024.1);
107 g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
109 d = -pow (2.0, -1024.1);
110 g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));