glib/tsrc/BC/tests/strtod-test.c
changeset 31 ce057bb09d0b
child 45 4b03adbd26ca
equal deleted inserted replaced
30:e20de85af2ee 31:ce057bb09d0b
       
     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 SYMBIAN
       
    16 #include "mrt2_glib2_test.h"
       
    17 #endif /*SYMBIAN*/
       
    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 SYMBIAN
       
    78   gdouble gd;
       
    79   gchar* gstr1; 
       
    80   gchar* gstr2; 	
       
    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);
       
    83 #endif /*SYMBIAN*/
       
    84 	  
       
    85 
       
    86 #ifdef NAN
       
    87   our_nan = NAN;
       
    88 #else
       
    89   /* Do this before any call to setlocale.  */
       
    90   our_nan = atof ("NaN");
       
    91 #endif
       
    92   g_assert (isnan (our_nan));
       
    93 
       
    94 #ifdef INFINITY
       
    95   our_inf = INFINITY;
       
    96 #else
       
    97   our_inf = atof ("Infinity");
       
    98 #endif
       
    99   g_assert (our_inf > 1 && our_inf == our_inf / 2);
       
   100 
       
   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);
       
   117   
       
   118   
       
   119   d = 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0;
       
   120   g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
       
   121 
       
   122   d = -179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0;
       
   123   g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
       
   124   
       
   125   
       
   126   
       
   127   d = pow (2.0, -1021.1);
       
   128   g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
       
   129   
       
   130   d = -pow (2.0, -1021.1);
       
   131   g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
       
   132 #ifdef SYMBIAN
       
   133   testResultXml("strtod-test");
       
   134 #endif /* EMULATOR */
       
   135 
       
   136   return 0;
       
   137 }