|
1 /* Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.*/ |
|
2 #undef G_DISABLE_ASSERT |
|
3 #undef G_LOG_DOMAIN |
|
4 |
|
5 /* for NAN and INFINITY */ |
|
6 #define _ISOC99_SOURCE |
|
7 |
|
8 #include <glib.h> |
|
9 #include <locale.h> |
|
10 #include <string.h> |
|
11 #include <stdlib.h> |
|
12 #include <math.h> |
|
13 #include <stdio.h> |
|
14 |
|
15 #ifdef __SYMBIAN32__ |
|
16 #include "mrt2_glib2_test.h" |
|
17 #endif /*__SYMBIAN32__*/ |
|
18 |
|
19 |
|
20 void |
|
21 test_string (char *number, double res, gboolean check_end, int correct_len) |
|
22 { |
|
23 double d; |
|
24 char *locales[] = {"sv_SE", "en_US", "fa_IR", "C", "ru_RU"}; |
|
25 int l; |
|
26 char *dummy; |
|
27 |
|
28 /* we try a copy of number, with some free space for malloc before that. |
|
29 * This is supposed to smash the some wrong pointer calculations. */ |
|
30 |
|
31 dummy = g_malloc (100000); |
|
32 number = g_strdup (number); |
|
33 g_free (dummy); |
|
34 |
|
35 for (l = 0; l < G_N_ELEMENTS (locales); l++) |
|
36 { |
|
37 gboolean ok; |
|
38 char *end = "(unset)"; |
|
39 |
|
40 setlocale (LC_NUMERIC, locales[l]); |
|
41 d = g_ascii_strtod (number, &end); |
|
42 ok = isnan (res) ? isnan (d) : (d == res); |
|
43 if (!ok) |
|
44 { |
|
45 g_print ("g_ascii_strtod on \"%s\" for locale %s failed\n", number, locales[l]); |
|
46 g_print ("expected %f (nan %d) actual %f (nan %d)\n", |
|
47 res, isnan (res), |
|
48 d, isnan (d)); |
|
49 assert_failed = TRUE; |
|
50 } |
|
51 |
|
52 ok = (end - number) == (check_end ? correct_len : strlen (number)); |
|
53 if (!ok) { |
|
54 assert_failed = TRUE; |
|
55 if (end == NULL) |
|
56 g_print ("g_ascii_strtod on \"%s\" for locale %s endptr was NULL\n", |
|
57 number, locales[l]); |
|
58 else if (end >= number && end <= number + strlen (number)) |
|
59 g_print ("g_ascii_strtod on \"%s\" for locale %s endptr was wrong, leftover: \"%s\"\n", |
|
60 number, locales[l], end); |
|
61 else |
|
62 g_print ("g_ascii_strtod on \"%s\" for locale %s endptr was REALLY wrong (number=%p, end=%p)\n", |
|
63 number, locales[l], number, end); |
|
64 } |
|
65 } |
|
66 |
|
67 g_free (number); |
|
68 } |
|
69 |
|
70 |
|
71 int |
|
72 main () |
|
73 { |
|
74 gdouble d, our_nan, our_inf; |
|
75 char buffer[G_ASCII_DTOSTR_BUF_SIZE]; |
|
76 |
|
77 #ifdef __SYMBIAN32__ |
|
78 g_log_set_handler (NULL, G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL); |
|
79 g_set_print_handler(mrtPrintHandler); |
|
80 #endif /*__SYMBIAN32__*/ |
|
81 |
|
82 |
|
83 #ifdef NAN |
|
84 our_nan = NAN; |
|
85 #else |
|
86 /* Do this before any call to setlocale. */ |
|
87 our_nan = atof ("NaN"); |
|
88 #endif |
|
89 g_assert (isnan (our_nan)); |
|
90 |
|
91 #ifdef INFINITY |
|
92 our_inf = INFINITY; |
|
93 #else |
|
94 our_inf = atof ("Infinity"); |
|
95 #endif |
|
96 g_assert (our_inf > 1 && our_inf == our_inf / 2); |
|
97 |
|
98 test_string ("123.123", 123.123, FALSE, 0); |
|
99 test_string ("123.123e2", 123.123e2, FALSE, 0); |
|
100 test_string ("123.123e-2", 123.123e-2, FALSE, 0); |
|
101 test_string ("-123.123", -123.123, FALSE, 0); |
|
102 test_string ("-123.123e2", -123.123e2, FALSE, 0); |
|
103 test_string ("-123.123e-2", -123.123e-2, FALSE, 0); |
|
104 test_string ("5.4", 5.4, TRUE, 3); |
|
105 test_string ("5.4,5.5", 5.4, TRUE, 3); |
|
106 test_string ("5,4", 5.0, TRUE, 1); |
|
107 /* the following are for #156421 */ |
|
108 test_string ("1e1", 1e1, FALSE, 0); |
|
109 test_string ("NAN", our_nan, FALSE, 0); |
|
110 test_string ("-nan", -our_nan, FALSE, 0); |
|
111 test_string ("INF", our_inf, FALSE, 0); |
|
112 test_string ("-infinity", -our_inf, FALSE, 0); |
|
113 test_string ("-.75,0", -0.75, TRUE, 4); |
|
114 |
|
115 d = 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0; |
|
116 g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL)); |
|
117 |
|
118 d = -179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0; |
|
119 g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL)); |
|
120 |
|
121 d = pow (2.0, -1021.1); |
|
122 g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL)); |
|
123 |
|
124 d = -pow (2.0, -1021.1); |
|
125 g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL)); |
|
126 |
|
127 #ifdef __SYMBIAN32__ |
|
128 testResultXml("strtod-test"); |
|
129 #endif /* EMULATOR */ |
|
130 |
|
131 return 0; |
|
132 } |