1  /* Tests of strtod in a locale using decimal comma.
       2     Copyright (C) 2007-2023 Free Software Foundation, Inc.
       3     This file is part of the GNU C Library.
       4  
       5     The GNU C Library is free software; you can redistribute it and/or
       6     modify it under the terms of the GNU Lesser General Public
       7     License as published by the Free Software Foundation; either
       8     version 2.1 of the License, or (at your option) any later version.
       9  
      10     The GNU C Library is distributed in the hope that it will be useful,
      11     but WITHOUT ANY WARRANTY; without even the implied warranty of
      12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      13     Lesser General Public License for more details.
      14  
      15     You should have received a copy of the GNU Lesser General Public
      16     License along with the GNU C Library; if not, see
      17     <https://www.gnu.org/licenses/>.  */
      18  
      19  #include <locale.h>
      20  #include <stdio.h>
      21  #include <stdlib.h>
      22  #include <string.h>
      23  #include <math.h>
      24  
      25  #define NBSP "\xc2\xa0"
      26  
      27  static const struct
      28  {
      29    const char *in;
      30    double expected;
      31  } tests[] =
      32    {
      33      { "0", 0.0 },
      34      { "000", 0.0 },
      35      { "-0", -0.0 },
      36      { "-000", -0.0 },
      37      { "0,", 0.0 },
      38      { "-0,", -0.0 },
      39      { "0,0", 0.0 },
      40      { "-0,0", -0.0 },
      41      { "0e-10", 0.0 },
      42      { "-0e-10", -0.0 },
      43      { "0,e-10", 0.0 },
      44      { "-0,e-10", -0.0 },
      45      { "0,0e-10", 0.0 },
      46      { "-0,0e-10", -0.0 },
      47      { "0e-1000000", 0.0 },
      48      { "-0e-1000000", -0.0 },
      49      { "0,0e-1000000", 0.0 },
      50      { "-0,0e-1000000", -0.0 },
      51    };
      52  #define NTESTS (sizeof (tests) / sizeof (tests[0]))
      53  
      54  
      55  static int
      56  do_test (void)
      57  {
      58    if (setlocale (LC_ALL, "cs_CZ.UTF-8") == NULL)
      59      {
      60        puts ("could not set locale");
      61        return 1;
      62      }
      63  
      64    int status = 0;
      65  
      66    for (int i = 0; i < NTESTS; ++i)
      67      {
      68        char *ep;
      69        double r = strtod (tests[i].in, &ep);
      70  
      71        if (*ep != '\0')
      72  	{
      73  	  printf ("%d: got rest string \"%s\", expected \"\"\n", i, ep);
      74  	  status = 1;
      75  	}
      76  
      77        if (r != tests[i].expected
      78  	  || copysign (10.0, r) != copysign (10.0, tests[i].expected))
      79  	{
      80  	  printf ("%d: got wrong results %g, expected %g\n",
      81  		  i, r, tests[i].expected);
      82  	  status = 1;
      83  	}
      84      }
      85  
      86    return status;
      87  }
      88  
      89  #include <support/test-driver.c>