tst-strtod5i.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* Tests of __strtod_internal 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 NNBSP "\xe2\x80\xaf"
  21. static const struct
  22. {
  23. const char *in;
  24. int group;
  25. double expected;
  26. } tests[] =
  27. {
  28. { "0", 0, 0.0 },
  29. { "000", 0, 0.0 },
  30. { "-0", 0, -0.0 },
  31. { "-000", 0, -0.0 },
  32. { "0,", 0, 0.0 },
  33. { "-0,", 0, -0.0 },
  34. { "0,0", 0, 0.0 },
  35. { "-0,0", 0, -0.0 },
  36. { "0e-10", 0, 0.0 },
  37. { "-0e-10", 0, -0.0 },
  38. { "0,e-10", 0, 0.0 },
  39. { "-0,e-10", 0, -0.0 },
  40. { "0,0e-10", 0, 0.0 },
  41. { "-0,0e-10", 0, -0.0 },
  42. { "0e-1000000", 0, 0.0 },
  43. { "-0e-1000000", 0, -0.0 },
  44. { "0,0e-1000000", 0, 0.0 },
  45. { "-0,0e-1000000", 0, -0.0 },
  46. { "0", 1, 0.0 },
  47. { "000", 1, 0.0 },
  48. { "-0", 1, -0.0 },
  49. { "-000", 1, -0.0 },
  50. { "0e-10", 1, 0.0 },
  51. { "-0e-10", 1, -0.0 },
  52. { "0e-1000000", 1, 0.0 },
  53. { "-0e-1000000", 1, -0.0 },
  54. { "000"NNBSP"000"NNBSP"000", 1, 0.0 },
  55. { "-000"NNBSP"000"NNBSP"000", 1, -0.0 }
  56. };
  57. #define NTESTS (sizeof (tests) / sizeof (tests[0]))
  58. static int
  59. do_test (void)
  60. {
  61. if (setlocale (LC_ALL, "cs_CZ.UTF-8") == NULL)
  62. {
  63. puts ("could not set locale");
  64. return 1;
  65. }
  66. int status = 0;
  67. for (int i = 0; i < NTESTS; ++i)
  68. {
  69. char *ep;
  70. double r = __strtod_internal (tests[i].in, &ep, tests[i].group);
  71. if (*ep != '\0')
  72. {
  73. printf ("%d: got rest string \"%s\", expected \"\"\n", i, ep);
  74. status = 1;
  75. }
  76. if (r != tests[i].expected
  77. || copysign (10.0, r) != copysign (10.0, tests[i].expected))
  78. {
  79. printf ("%d: got wrong results %g, expected %g\n",
  80. i, r, tests[i].expected);
  81. status = 1;
  82. }
  83. }
  84. return status;
  85. }
  86. #include <support/test-driver.c>