os/ossrv/glib/tsrc/BC/tests/strtod-test.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/* Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.*/
sl@0
     2
#undef G_DISABLE_ASSERT
sl@0
     3
#undef G_LOG_DOMAIN
sl@0
     4
sl@0
     5
/* for NAN and INFINITY */
sl@0
     6
#define _ISOC99_SOURCE
sl@0
     7
sl@0
     8
#include <glib.h>
sl@0
     9
#include <locale.h>
sl@0
    10
#include <string.h>
sl@0
    11
#include <stdlib.h>
sl@0
    12
#include <math.h>
sl@0
    13
#include <stdio.h>
sl@0
    14
sl@0
    15
#ifdef SYMBIAN
sl@0
    16
#include "mrt2_glib2_test.h"
sl@0
    17
#endif /*SYMBIAN*/
sl@0
    18
sl@0
    19
sl@0
    20
void
sl@0
    21
test_string (char *number, double res, gboolean check_end, int correct_len)
sl@0
    22
{
sl@0
    23
  double d;
sl@0
    24
  char *locales[] = {"sv_SE", "en_US", "fa_IR", "C", "ru_RU"};
sl@0
    25
  int l;
sl@0
    26
  char *dummy;
sl@0
    27
  
sl@0
    28
  /* we try a copy of number, with some free space for malloc before that. 
sl@0
    29
   * This is supposed to smash the some wrong pointer calculations. */
sl@0
    30
sl@0
    31
  dummy = g_malloc (100000);
sl@0
    32
  number = g_strdup (number);
sl@0
    33
  g_free (dummy);
sl@0
    34
sl@0
    35
  for (l = 0; l < G_N_ELEMENTS (locales); l++)
sl@0
    36
    {
sl@0
    37
      gboolean ok;
sl@0
    38
      char *end = "(unset)";
sl@0
    39
sl@0
    40
      setlocale (LC_NUMERIC, locales[l]);
sl@0
    41
      d = g_ascii_strtod (number, &end);
sl@0
    42
      ok = isnan (res) ? isnan (d) : (d == res);
sl@0
    43
      if (!ok)
sl@0
    44
	{
sl@0
    45
	  g_print ("g_ascii_strtod on \"%s\" for locale %s failed\n", number, locales[l]);
sl@0
    46
	  g_print ("expected %f (nan %d) actual %f (nan %d)\n", 
sl@0
    47
		   res, isnan (res),
sl@0
    48
		   d, isnan (d));
sl@0
    49
	  assert_failed = TRUE;
sl@0
    50
	}
sl@0
    51
sl@0
    52
      ok = (end - number) == (check_end ? correct_len : strlen (number));
sl@0
    53
      if (!ok) {
sl@0
    54
      assert_failed = TRUE;
sl@0
    55
	if (end == NULL)
sl@0
    56
	  g_print ("g_ascii_strtod on \"%s\" for locale %s endptr was NULL\n",
sl@0
    57
		   number, locales[l]);
sl@0
    58
	else if (end >= number && end <= number + strlen (number))
sl@0
    59
	  g_print ("g_ascii_strtod on \"%s\" for locale %s endptr was wrong, leftover: \"%s\"\n",
sl@0
    60
		   number, locales[l], end);
sl@0
    61
	else
sl@0
    62
	  g_print ("g_ascii_strtod on \"%s\" for locale %s endptr was REALLY wrong (number=%p, end=%p)\n",
sl@0
    63
		   number, locales[l], number, end);
sl@0
    64
      }
sl@0
    65
    }
sl@0
    66
sl@0
    67
  g_free (number);
sl@0
    68
}
sl@0
    69
sl@0
    70
sl@0
    71
int 
sl@0
    72
main ()
sl@0
    73
{
sl@0
    74
  gdouble d, our_nan, our_inf;
sl@0
    75
  char buffer[G_ASCII_DTOSTR_BUF_SIZE];
sl@0
    76
sl@0
    77
#ifdef SYMBIAN
sl@0
    78
  gdouble gd;
sl@0
    79
  gchar* gstr1; 
sl@0
    80
  gchar* gstr2; 	
sl@0
    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);
sl@0
    82
  g_set_print_handler(mrtPrintHandler);
sl@0
    83
#endif /*SYMBIAN*/
sl@0
    84
	  
sl@0
    85
sl@0
    86
#ifdef NAN
sl@0
    87
  our_nan = NAN;
sl@0
    88
#else
sl@0
    89
  /* Do this before any call to setlocale.  */
sl@0
    90
  our_nan = atof ("NaN");
sl@0
    91
#endif
sl@0
    92
  g_assert (isnan (our_nan));
sl@0
    93
sl@0
    94
#ifdef INFINITY
sl@0
    95
  our_inf = INFINITY;
sl@0
    96
#else
sl@0
    97
  our_inf = atof ("Infinity");
sl@0
    98
#endif
sl@0
    99
  g_assert (our_inf > 1 && our_inf == our_inf / 2);
sl@0
   100
sl@0
   101
  test_string ("123.123", 123.123, FALSE, 0);
sl@0
   102
  test_string ("123.123e2", 123.123e2, FALSE, 0);
sl@0
   103
  test_string ("123.123e-2", 123.123e-2, FALSE, 0);
sl@0
   104
  test_string ("-123.123", -123.123, FALSE, 0);
sl@0
   105
  test_string ("-123.123e2", -123.123e2, FALSE, 0);
sl@0
   106
  test_string ("-123.123e-2", -123.123e-2, FALSE, 0);
sl@0
   107
  test_string ("5.4", 5.4, TRUE, 3);
sl@0
   108
  test_string ("5.4,5.5", 5.4, TRUE, 3);
sl@0
   109
  test_string ("5,4", 5.0, TRUE, 1);
sl@0
   110
  /* the following are for #156421 */
sl@0
   111
  test_string ("1e1", 1e1, FALSE, 0); 
sl@0
   112
  test_string ("NAN", our_nan, FALSE, 0);
sl@0
   113
  test_string ("-nan", -our_nan, FALSE, 0);
sl@0
   114
  test_string ("INF", our_inf, FALSE, 0);
sl@0
   115
  test_string ("-infinity", -our_inf, FALSE, 0);
sl@0
   116
  test_string ("-.75,0", -0.75, TRUE, 4);
sl@0
   117
  
sl@0
   118
  
sl@0
   119
  d = 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0;
sl@0
   120
  g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
sl@0
   121
sl@0
   122
  d = -179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0;
sl@0
   123
  g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
sl@0
   124
  
sl@0
   125
  
sl@0
   126
  
sl@0
   127
  d = pow (2.0, -1021.1);
sl@0
   128
  g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
sl@0
   129
  
sl@0
   130
  d = -pow (2.0, -1021.1);
sl@0
   131
  g_assert (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
sl@0
   132
#ifdef SYMBIAN
sl@0
   133
  testResultXml("strtod-test");
sl@0
   134
#endif /* EMULATOR */
sl@0
   135
sl@0
   136
  return 0;
sl@0
   137
}