Dummy dynamic type module for testing type plugin code and dynamic types.
[platform/upstream/glib.git] / tests / strtod-test.c
1 #undef G_DISABLE_ASSERT
2 #undef G_LOG_DOMAIN
3
4 #include <glib.h>
5 #include <locale.h>
6 #include <string.h>
7 #include <math.h>
8
9 void
10 test_string (char *number, double res)
11 {
12   gdouble d;
13   char *locales[] = {"sv_SE", "en_US", "fa_IR", "C"};
14   int l;
15   char *end;
16
17   for (l = 0; l < G_N_ELEMENTS (locales); l++)
18     {
19       setlocale (LC_NUMERIC, locales[l]);
20       d = g_ascii_strtod (number, &end);
21       if (d != res)
22         g_print ("g_ascii_strtod for locale %s failed\n", locales[l]);
23       if (*end != 0)
24         g_print ("g_ascii_strtod for locale %s endptr was wrong\n", locales[l]);
25     }
26 }
27
28
29 int 
30 main ()
31 {
32   gdouble d;
33   char buffer[G_ASCII_DTOSTR_BUF_SIZE];
34
35   test_string ("123.123", 123.123);
36   test_string ("123.123e2", 123.123e2);
37   test_string ("123.123e-2", 123.123e-2);
38   test_string ("-123.123", -123.123);
39   test_string ("-123.123e2", -123.123e2);
40   test_string ("-123.123e-2", -123.123e-2);
41   
42   d = 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0;
43   g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
44
45   d = -179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0;
46   g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
47   
48   d = pow (2.0, -1024.1);
49   g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
50   
51   d = -pow (2.0, -1024.1);
52   g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
53   
54
55   return 0;
56 }