tst-strtod5.c 2.1 KB

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