bug-strtod.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* Test to strtod etc for numbers like x000...0000.000e-nn.
  2. This file is part of the GNU C Library.
  3. Copyright (C) 2001-2019 Free Software Foundation, Inc.
  4. Contributed by Ulrich Drepper <drepper@redhat.com>, 2001.
  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. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include "tst-strtod.h"
  20. #define TEST_STRTOD(FSUF, FTYPE, FTOSTR, LSUF, CSUF) \
  21. static int \
  22. test_strto ## FSUF (void) \
  23. { \
  24. char buf[300]; \
  25. int cnt; \
  26. int result = 0; \
  27. \
  28. for (cnt = 0; cnt < 200; ++cnt) \
  29. { \
  30. ssize_t n; \
  31. FTYPE f; \
  32. \
  33. n = sprintf (buf, "%d", cnt); \
  34. memset (buf + n, '0', cnt); \
  35. sprintf (buf + n + cnt, ".000e-%d", cnt); \
  36. f = strto ## FSUF (buf, NULL); \
  37. \
  38. if (f != (FTYPE) cnt) \
  39. { \
  40. char fstr[FSTRLENMAX]; \
  41. char fcntstr[FSTRLENMAX]; \
  42. FTOSTR (fstr, sizeof (fstr), "%g", f); \
  43. FTOSTR (fcntstr, sizeof (fstr), "%g", (FTYPE) cnt); \
  44. printf ("strto" #FSUF "(\"%s\") " \
  45. "failed for cnt == %d (%s instead of %s)\n", \
  46. buf, cnt, fstr, fcntstr); \
  47. result = 1; \
  48. } \
  49. else \
  50. printf ( "strto" #FSUF "() fine for cnt == %d\n", cnt); \
  51. } \
  52. return result; \
  53. }
  54. GEN_TEST_STRTOD_FOREACH (TEST_STRTOD)
  55. int
  56. main (void)
  57. {
  58. return STRTOD_TEST_FOREACH (test_strto);
  59. }