tst-strtod1i.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* Basic tests for __strtod_internal.
  2. Copyright (C) 1991-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 <ctype.h>
  16. #include <locale.h>
  17. #include <stddef.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <errno.h>
  21. #include <string.h>
  22. #include <math.h>
  23. /* Perform a few tests in a locale with thousands separators. */
  24. static int
  25. do_test (void)
  26. {
  27. static const struct
  28. {
  29. const char *loc;
  30. const char *str;
  31. double exp;
  32. ptrdiff_t nread;
  33. } tests[] =
  34. {
  35. { "de_DE.UTF-8", "1,5", 1.5, 3 },
  36. { "de_DE.UTF-8", "1.5", 1.0, 1 },
  37. { "de_DE.UTF-8", "1.500", 1500.0, 5 },
  38. { "de_DE.UTF-8", "36.893.488.147.419.103.232", 0x1.0p65, 26 }
  39. };
  40. #define ntests (sizeof (tests) / sizeof (tests[0]))
  41. size_t n;
  42. int result = 0;
  43. puts ("\nLocale tests");
  44. for (n = 0; n < ntests; ++n)
  45. {
  46. double d;
  47. char *endp;
  48. if (setlocale (LC_ALL, tests[n].loc) == NULL)
  49. {
  50. printf ("cannot set locale %s\n", tests[n].loc);
  51. result = 1;
  52. continue;
  53. }
  54. d = __strtod_internal (tests[n].str, &endp, 1);
  55. if (d != tests[n].exp)
  56. {
  57. printf ("strtod(\"%s\") returns %g and not %g\n",
  58. tests[n].str, d, tests[n].exp);
  59. result = 1;
  60. }
  61. else if (endp - tests[n].str != tests[n].nread)
  62. {
  63. printf ("strtod(\"%s\") read %td bytes and not %td\n",
  64. tests[n].str, endp - tests[n].str, tests[n].nread);
  65. result = 1;
  66. }
  67. }
  68. if (result == 0)
  69. puts ("all OK");
  70. return result ? EXIT_FAILURE : EXIT_SUCCESS;
  71. }
  72. #include <support/test-driver.c>