Improve wording. (#161484, Christian Biere)
[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 void
14 test_string (char *number, double res, gboolean check_end, int correct_len)
15 {
16   double d;
17   char *locales[] = {"sv_SE", "en_US", "fa_IR", "C", "ru_RU"};
18   int l;
19   char *dummy;
20   
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. */
23
24   dummy = g_malloc (100000);
25   number = g_strdup (number);
26   g_free (dummy);
27
28   for (l = 0; l < G_N_ELEMENTS (locales); l++)
29     {
30       gboolean ok;
31       char *end = "(unset)";
32
33       setlocale (LC_NUMERIC, locales[l]);
34       d = g_ascii_strtod (number, &end);
35       ok = isnan (res) ? isnan (d) : (d == res);
36       if (!ok)
37         {
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", 
40                    res, isnan (res),
41                    d, isnan (d));
42         }
43
44       ok = (end - number) == (check_end ? correct_len : strlen (number));
45       if (!ok) {
46         if (end == NULL)
47           g_print ("g_ascii_strtod on \"%s\" for locale %s endptr was NULL\n",
48                    number, locales[l]);
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);
52         else
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);
55       }
56     }
57
58   g_free (number);
59 }
60
61
62 int 
63 main ()
64 {
65   gdouble d, our_nan, our_inf;
66   char buffer[G_ASCII_DTOSTR_BUF_SIZE];
67
68 #ifdef NAN
69   our_nan = NAN;
70 #else
71   /* Do this before any call to setlocale.  */
72   our_nan = atof ("NaN");
73 #endif
74   g_assert (isnan (our_nan));
75
76 #ifdef INFINITY
77   our_inf = INFINITY;
78 #else
79   our_inf = atof ("Infinity");
80 #endif
81   g_assert (our_inf > 1 && our_inf == our_inf / 2);
82
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);
99   
100   d = 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0;
101   g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
102
103   d = -179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0;
104   g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
105   
106   d = pow (2.0, -1024.1);
107   g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
108   
109   d = -pow (2.0, -1024.1);
110   g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
111   
112
113   return 0;
114 }