introduce an ENUMPREFIX substitution.
[platform/upstream/glib.git] / tests / strtod-test.c
1 #undef G_DISABLE_ASSERT
2 #undef G_LOG_DOMAIN
3
4 /* for NAN and INFINITY */
5 #define _ISOC99_SOURCE
6
7 #include <glib.h>
8 #include <locale.h>
9 #include <string.h>
10 #include <stdlib.h>
11 #include <math.h>
12
13 static char *locales[] = {"sv_SE", "en_US", "fa_IR", "C", "ru_RU"};
14
15 void
16 test_string (char *number, double res, gboolean check_end, int correct_len)
17 {
18   double d;
19   int l;
20   char *dummy;
21   
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. */
24
25   dummy = g_malloc (100000);
26   number = g_strdup (number);
27   g_free (dummy);
28
29   for (l = 0; l < G_N_ELEMENTS (locales); l++)
30     {
31       gboolean ok;
32       char *end = "(unset)";
33
34       setlocale (LC_NUMERIC, locales[l]);
35       d = g_ascii_strtod (number, &end);
36       ok = isnan (res) ? isnan (d) : (d == res);
37       if (!ok)
38         {
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", 
41                    res, isnan (res),
42                    d, isnan (d));
43         }
44
45       ok = (end - number) == (check_end ? correct_len : strlen (number));
46       if (!ok) {
47         if (end == NULL)
48           g_print ("g_ascii_strtod on \"%s\" for locale %s endptr was NULL\n",
49                    number, locales[l]);
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);
53         else
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);
56       }
57     }
58
59   g_free (number);
60 }
61
62
63 static void
64 test_number (gdouble num, gchar *fmt, gchar *str)
65 {
66   int l;
67   gchar buf[G_ASCII_DTOSTR_BUF_SIZE];
68
69   for (l = 0; l < G_N_ELEMENTS (locales); l++)
70     {
71       g_ascii_formatd (buf, G_ASCII_DTOSTR_BUF_SIZE, fmt, num);
72       g_assert (strcmp (buf, str) == 0);
73     }
74 }
75
76 int 
77 main ()
78 {
79   gdouble d, our_nan, our_inf;
80   char buffer[G_ASCII_DTOSTR_BUF_SIZE];
81
82 #ifdef NAN
83   our_nan = NAN;
84 #else
85   /* Do this before any call to setlocale.  */
86   our_nan = atof ("NaN");
87 #endif
88   g_assert (isnan (our_nan));
89
90 #ifdef INFINITY
91   our_inf = INFINITY;
92 #else
93   our_inf = atof ("Infinity");
94 #endif
95   g_assert (our_inf > 1 && our_inf == our_inf / 2);
96
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);
113   
114   d = 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0;
115   g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
116
117   d = -179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0;
118   g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
119   
120   d = pow (2.0, -1024.1);
121   g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
122   
123   d = -pow (2.0, -1024.1);
124   g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
125
126   /* for #343899 */
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);
134 #if 0
135   /* g_ascii_isspace() returns FALSE for vertical tab, see #59388 */
136   test_string ("\v0.75", 0.75, FALSE, 0);
137 #endif
138
139   /* for #343899 */
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");
145   return 0;
146 }