Update contrib.
1 /* Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.*/
2 #undef G_DISABLE_ASSERT
5 /* for NAN and INFINITY */
16 #include "mrt2_glib2_test.h"
21 test_string (char *number, double res, gboolean check_end, int correct_len)
24 char *locales[] = {"sv_SE", "en_US", "fa_IR", "C", "ru_RU"};
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. */
31 dummy = g_malloc (100000);
32 number = g_strdup (number);
35 for (l = 0; l < G_N_ELEMENTS (locales); l++)
38 char *end = "(unset)";
40 setlocale (LC_NUMERIC, locales[l]);
41 d = g_ascii_strtod (number, &end);
42 ok = isnan (res) ? isnan (d) : (d == res);
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",
52 ok = (end - number) == (check_end ? correct_len : strlen (number));
56 g_print ("g_ascii_strtod on \"%s\" for locale %s endptr was NULL\n",
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);
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);
74 gdouble d, our_nan, our_inf;
75 char buffer[G_ASCII_DTOSTR_BUF_SIZE];
81 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);
82 g_set_print_handler(mrtPrintHandler);
89 /* Do this before any call to setlocale. */
90 our_nan = atof ("NaN");
92 g_assert (isnan (our_nan));
97 our_inf = atof ("Infinity");
99 g_assert (our_inf > 1 && our_inf == our_inf / 2);
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 ("-123.123", -123.123, FALSE, 0);
105 test_string ("-123.123e2", -123.123e2, FALSE, 0);
106 test_string ("-123.123e-2", -123.123e-2, FALSE, 0);
107 test_string ("5.4", 5.4, TRUE, 3);
108 test_string ("5.4,5.5", 5.4, TRUE, 3);
109 test_string ("5,4", 5.0, TRUE, 1);
110 /* the following are for #156421 */
111 test_string ("1e1", 1e1, FALSE, 0);
112 test_string ("NAN", our_nan, FALSE, 0);
113 test_string ("-nan", -our_nan, FALSE, 0);
114 test_string ("INF", our_inf, FALSE, 0);
115 test_string ("-infinity", -our_inf, FALSE, 0);
116 test_string ("-.75,0", -0.75, TRUE, 4);
119 d = 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0;
120 g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
122 d = -179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0;
123 g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
127 d = pow (2.0, -1021.1);
128 g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
130 d = -pow (2.0, -1021.1);
131 g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
133 testResultXml("strtod-test");
134 #endif /* EMULATOR */