1
#undef G_DISABLE_ASSERT
4
/* for NAN and INFINITY */
13
static char *locales[] = {"sv_SE", "en_US", "fa_IR", "C", "ru_RU"};
16
test_string (char *number, double res, gboolean check_end, int correct_len)
22
/* we try a copy of number, with some free space for malloc before that.
23
* This is supposed to smash the some wrong pointer calculations. */
25
dummy = g_malloc (100000);
26
number = g_strdup (number);
29
for (l = 0; l < G_N_ELEMENTS (locales); l++)
32
char *end = "(unset)";
34
setlocale (LC_NUMERIC, locales[l]);
35
d = g_ascii_strtod (number, &end);
36
ok = isnan (res) ? isnan (d) : (d == res);
39
g_print ("g_ascii_strtod on \"%s\" for locale %s failed\n", number, locales[l]);
40
g_print ("expected %f (nan %d) actual %f (nan %d)\n",
45
ok = (end - number) == (check_end ? correct_len : strlen (number));
48
g_print ("g_ascii_strtod on \"%s\" for locale %s endptr was NULL\n",
50
else if (end >= number && end <= number + strlen (number))
51
g_print ("g_ascii_strtod on \"%s\" for locale %s endptr was wrong, leftover: \"%s\"\n",
52
number, locales[l], end);
54
g_print ("g_ascii_strtod on \"%s\" for locale %s endptr was REALLY wrong (number=%p, end=%p)\n",
55
number, locales[l], number, end);
64
test_number (gdouble num, gchar *fmt, gchar *str)
67
gchar buf[G_ASCII_DTOSTR_BUF_SIZE];
69
for (l = 0; l < G_N_ELEMENTS (locales); l++)
71
g_ascii_formatd (buf, G_ASCII_DTOSTR_BUF_SIZE, fmt, num);
72
g_assert (strcmp (buf, str) == 0);
79
gdouble d, our_nan, our_inf;
80
char buffer[G_ASCII_DTOSTR_BUF_SIZE];
85
/* Do this before any call to setlocale. */
86
our_nan = atof ("NaN");
88
g_assert (isnan (our_nan));
93
our_inf = atof ("Infinity");
95
g_assert (our_inf > 1 && our_inf == our_inf / 2);
97
test_string ("123.123", 123.123, FALSE, 0);
98
test_string ("123.123e2", 123.123e2, FALSE, 0);
99
test_string ("123.123e-2", 123.123e-2, FALSE, 0);
100
test_string ("-123.123", -123.123, FALSE, 0);
101
test_string ("-123.123e2", -123.123e2, FALSE, 0);
102
test_string ("-123.123e-2", -123.123e-2, FALSE, 0);
103
test_string ("5.4", 5.4, TRUE, 3);
104
test_string ("5.4,5.5", 5.4, TRUE, 3);
105
test_string ("5,4", 5.0, TRUE, 1);
106
/* the following are for #156421 */
107
test_string ("1e1", 1e1, FALSE, 0);
108
test_string ("NAN", our_nan, FALSE, 0);
109
test_string ("-nan", -our_nan, FALSE, 0);
110
test_string ("INF", our_inf, FALSE, 0);
111
test_string ("-infinity", -our_inf, FALSE, 0);
112
test_string ("-.75,0", -0.75, TRUE, 4);
114
d = 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0;
115
g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
117
d = -179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0;
118
g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
120
d = pow (2.0, -1024.1);
121
g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
123
d = -pow (2.0, -1024.1);
124
g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
127
test_string (" 0.75", 0.75, FALSE, 0);
128
test_string (" +0.75", 0.75, FALSE, 0);
129
test_string (" -0.75", -0.75, FALSE, 0);
130
test_string ("\f0.75", 0.75, FALSE, 0);
131
test_string ("\n0.75", 0.75, FALSE, 0);
132
test_string ("\r0.75", 0.75, FALSE, 0);
133
test_string ("\t0.75", 0.75, FALSE, 0);
135
/* g_ascii_isspace() returns FALSE for vertical tab, see #59388 */
136
test_string ("\v0.75", 0.75, FALSE, 0);
140
test_number (0.75, "%0.2f", "0.75");
141
test_number (0.75, "%5.2f", " 0.75");
142
test_number (-0.75, "%0.2f", "-0.75");
143
test_number (-0.75, "%5.2f", "-0.75");
144
test_number (1e99, "%.0e", "1e+99");