os/ossrv/glib/tsrc/BC/tests/strtod-test.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/glib/tsrc/BC/tests/strtod-test.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,137 @@
     1.4 +/* Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.*/
     1.5 +#undef G_DISABLE_ASSERT
     1.6 +#undef G_LOG_DOMAIN
     1.7 +
     1.8 +/* for NAN and INFINITY */
     1.9 +#define _ISOC99_SOURCE
    1.10 +
    1.11 +#include <glib.h>
    1.12 +#include <locale.h>
    1.13 +#include <string.h>
    1.14 +#include <stdlib.h>
    1.15 +#include <math.h>
    1.16 +#include <stdio.h>
    1.17 +
    1.18 +#ifdef SYMBIAN
    1.19 +#include "mrt2_glib2_test.h"
    1.20 +#endif /*SYMBIAN*/
    1.21 +
    1.22 +
    1.23 +void
    1.24 +test_string (char *number, double res, gboolean check_end, int correct_len)
    1.25 +{
    1.26 +  double d;
    1.27 +  char *locales[] = {"sv_SE", "en_US", "fa_IR", "C", "ru_RU"};
    1.28 +  int l;
    1.29 +  char *dummy;
    1.30 +  
    1.31 +  /* we try a copy of number, with some free space for malloc before that. 
    1.32 +   * This is supposed to smash the some wrong pointer calculations. */
    1.33 +
    1.34 +  dummy = g_malloc (100000);
    1.35 +  number = g_strdup (number);
    1.36 +  g_free (dummy);
    1.37 +
    1.38 +  for (l = 0; l < G_N_ELEMENTS (locales); l++)
    1.39 +    {
    1.40 +      gboolean ok;
    1.41 +      char *end = "(unset)";
    1.42 +
    1.43 +      setlocale (LC_NUMERIC, locales[l]);
    1.44 +      d = g_ascii_strtod (number, &end);
    1.45 +      ok = isnan (res) ? isnan (d) : (d == res);
    1.46 +      if (!ok)
    1.47 +	{
    1.48 +	  g_print ("g_ascii_strtod on \"%s\" for locale %s failed\n", number, locales[l]);
    1.49 +	  g_print ("expected %f (nan %d) actual %f (nan %d)\n", 
    1.50 +		   res, isnan (res),
    1.51 +		   d, isnan (d));
    1.52 +	  assert_failed = TRUE;
    1.53 +	}
    1.54 +
    1.55 +      ok = (end - number) == (check_end ? correct_len : strlen (number));
    1.56 +      if (!ok) {
    1.57 +      assert_failed = TRUE;
    1.58 +	if (end == NULL)
    1.59 +	  g_print ("g_ascii_strtod on \"%s\" for locale %s endptr was NULL\n",
    1.60 +		   number, locales[l]);
    1.61 +	else if (end >= number && end <= number + strlen (number))
    1.62 +	  g_print ("g_ascii_strtod on \"%s\" for locale %s endptr was wrong, leftover: \"%s\"\n",
    1.63 +		   number, locales[l], end);
    1.64 +	else
    1.65 +	  g_print ("g_ascii_strtod on \"%s\" for locale %s endptr was REALLY wrong (number=%p, end=%p)\n",
    1.66 +		   number, locales[l], number, end);
    1.67 +      }
    1.68 +    }
    1.69 +
    1.70 +  g_free (number);
    1.71 +}
    1.72 +
    1.73 +
    1.74 +int 
    1.75 +main ()
    1.76 +{
    1.77 +  gdouble d, our_nan, our_inf;
    1.78 +  char buffer[G_ASCII_DTOSTR_BUF_SIZE];
    1.79 +
    1.80 +#ifdef SYMBIAN
    1.81 +  gdouble gd;
    1.82 +  gchar* gstr1; 
    1.83 +  gchar* gstr2; 	
    1.84 +  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);
    1.85 +  g_set_print_handler(mrtPrintHandler);
    1.86 +#endif /*SYMBIAN*/
    1.87 +	  
    1.88 +
    1.89 +#ifdef NAN
    1.90 +  our_nan = NAN;
    1.91 +#else
    1.92 +  /* Do this before any call to setlocale.  */
    1.93 +  our_nan = atof ("NaN");
    1.94 +#endif
    1.95 +  g_assert (isnan (our_nan));
    1.96 +
    1.97 +#ifdef INFINITY
    1.98 +  our_inf = INFINITY;
    1.99 +#else
   1.100 +  our_inf = atof ("Infinity");
   1.101 +#endif
   1.102 +  g_assert (our_inf > 1 && our_inf == our_inf / 2);
   1.103 +
   1.104 +  test_string ("123.123", 123.123, FALSE, 0);
   1.105 +  test_string ("123.123e2", 123.123e2, FALSE, 0);
   1.106 +  test_string ("123.123e-2", 123.123e-2, FALSE, 0);
   1.107 +  test_string ("-123.123", -123.123, FALSE, 0);
   1.108 +  test_string ("-123.123e2", -123.123e2, FALSE, 0);
   1.109 +  test_string ("-123.123e-2", -123.123e-2, FALSE, 0);
   1.110 +  test_string ("5.4", 5.4, TRUE, 3);
   1.111 +  test_string ("5.4,5.5", 5.4, TRUE, 3);
   1.112 +  test_string ("5,4", 5.0, TRUE, 1);
   1.113 +  /* the following are for #156421 */
   1.114 +  test_string ("1e1", 1e1, FALSE, 0); 
   1.115 +  test_string ("NAN", our_nan, FALSE, 0);
   1.116 +  test_string ("-nan", -our_nan, FALSE, 0);
   1.117 +  test_string ("INF", our_inf, FALSE, 0);
   1.118 +  test_string ("-infinity", -our_inf, FALSE, 0);
   1.119 +  test_string ("-.75,0", -0.75, TRUE, 4);
   1.120 +  
   1.121 +  
   1.122 +  d = 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0;
   1.123 +  g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
   1.124 +
   1.125 +  d = -179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0;
   1.126 +  g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
   1.127 +  
   1.128 +  
   1.129 +  
   1.130 +  d = pow (2.0, -1021.1);
   1.131 +  g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
   1.132 +  
   1.133 +  d = -pow (2.0, -1021.1);
   1.134 +  g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
   1.135 +#ifdef SYMBIAN
   1.136 +  testResultXml("strtod-test");
   1.137 +#endif /* EMULATOR */
   1.138 +
   1.139 +  return 0;
   1.140 +}