tst-strtod.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /* Basic tests for strtod.
  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. struct ltest
  24. {
  25. const char *str; /* Convert this. */
  26. double expect; /* To get this. */
  27. char left; /* With this left over. */
  28. int err; /* And this in errno. */
  29. };
  30. static const struct ltest tests[] =
  31. {
  32. { "12.345", 12.345, '\0', 0 },
  33. { "12.345e19", 12.345e19, '\0', 0 },
  34. { "-.1e+9", -.1e+9, '\0', 0 },
  35. { ".125", .125, '\0', 0 },
  36. { "1e20", 1e20, '\0', 0 },
  37. { "0e-19", 0, '\0', 0 },
  38. { "4\00012", 4.0, '\0', 0 },
  39. { "5.9e-76", 5.9e-76, '\0', 0 },
  40. { "0x1.4p+3", 10.0, '\0', 0 },
  41. { "0xAp0", 10.0, '\0', 0 },
  42. { "0x0Ap0", 10.0, '\0', 0 },
  43. { "0x0A", 10.0, '\0', 0 },
  44. { "0xA0", 160.0, '\0', 0 },
  45. { "0x0.A0p8", 160.0, '\0', 0 },
  46. { "0x0.50p9", 160.0, '\0', 0 },
  47. { "0x0.28p10", 160.0, '\0', 0 },
  48. { "0x0.14p11", 160.0, '\0', 0 },
  49. { "0x0.0A0p12", 160.0, '\0', 0 },
  50. { "0x0.050p13", 160.0, '\0', 0 },
  51. { "0x0.028p14", 160.0, '\0', 0 },
  52. { "0x0.014p15", 160.0, '\0', 0 },
  53. { "0x00.00A0p16", 160.0, '\0', 0 },
  54. { "0x00.0050p17", 160.0, '\0', 0 },
  55. { "0x00.0028p18", 160.0, '\0', 0 },
  56. { "0x00.0014p19", 160.0, '\0', 0 },
  57. { "0x1p-1023",
  58. 1.11253692925360069154511635866620203210960799023116591527666e-308,
  59. '\0', 0 },
  60. { "0x0.8p-1022",
  61. 1.11253692925360069154511635866620203210960799023116591527666e-308,
  62. '\0', 0 },
  63. { "Inf", HUGE_VAL, '\0', 0 },
  64. { "-Inf", -HUGE_VAL, '\0', 0 },
  65. { "+InFiNiTy", HUGE_VAL, '\0', 0 },
  66. { "0x80000Ap-23", 0x80000Ap-23, '\0', 0 },
  67. { "1e-324", 0, '\0', ERANGE },
  68. { "0x100000000000008p0", 0x1p56, '\0', 0 },
  69. { "0x100000000000008.p0", 0x1p56, '\0', 0 },
  70. { "0x100000000000008.00p0", 0x1p56, '\0', 0 },
  71. { "0x10000000000000800p0", 0x1p64, '\0', 0 },
  72. { "0x10000000000000801p0", 0x1.0000000000001p64, '\0', 0 },
  73. { NULL, 0, '\0', 0 }
  74. };
  75. static void expand (char *dst, int c);
  76. static int long_dbl (void);
  77. static int
  78. do_test (void)
  79. {
  80. char buf[100];
  81. const struct ltest *lt;
  82. char *ep;
  83. int status = 0;
  84. int save_errno;
  85. for (lt = tests; lt->str != NULL; ++lt)
  86. {
  87. double d;
  88. errno = 0;
  89. d = strtod(lt->str, &ep);
  90. save_errno = errno;
  91. printf ("strtod (\"%s\") test %u",
  92. lt->str, (unsigned int) (lt - tests));
  93. if (d == lt->expect && *ep == lt->left && save_errno == lt->err)
  94. puts ("\tOK");
  95. else
  96. {
  97. puts ("\tBAD");
  98. if (d != lt->expect)
  99. printf (" returns %.60g, expected %.60g\n", d, lt->expect);
  100. if (lt->left != *ep)
  101. {
  102. char exp1[5], exp2[5];
  103. expand (exp1, *ep);
  104. expand (exp2, lt->left);
  105. printf (" leaves '%s', expected '%s'\n", exp1, exp2);
  106. }
  107. if (save_errno != lt->err)
  108. printf (" errno %d (%s) instead of %d (%s)\n",
  109. save_errno, strerror (save_errno),
  110. lt->err, strerror (lt->err));
  111. status = 1;
  112. }
  113. }
  114. sprintf (buf, "%f", strtod ("-0.0", NULL));
  115. if (strcmp (buf, "-0.000000") != 0)
  116. {
  117. printf (" strtod (\"-0.0\", NULL) returns \"%s\"\n", buf);
  118. status = 1;
  119. }
  120. const char input[] = "3752432815e-39";
  121. float f1 = strtold (input, NULL);
  122. float f2;
  123. float f3 = strtof (input, NULL);
  124. sscanf (input, "%g", &f2);
  125. if (f1 != f2)
  126. {
  127. printf ("f1 = %a != f2 = %a\n", f1, f2);
  128. status = 1;
  129. }
  130. if (f1 != f3)
  131. {
  132. printf ("f1 = %a != f3 = %a\n", f1, f3);
  133. status = 1;
  134. }
  135. if (f2 != f3)
  136. {
  137. printf ("f2 = %a != f3 = %a\n", f2, f3);
  138. status = 1;
  139. }
  140. const char input2[] = "+1.000000000116415321826934814453125";
  141. if (strtold (input2, NULL) != +1.000000000116415321826934814453125L)
  142. {
  143. printf ("input2: %La != %La\n", strtold (input2, NULL),
  144. +1.000000000116415321826934814453125L);
  145. status = 1;
  146. }
  147. static struct { const char *str; long double l; } ltests[] =
  148. {
  149. { "42.0000000000000000001", 42.0000000000000000001L },
  150. { "42.00000000000000000001", 42.00000000000000000001L },
  151. { "42.000000000000000000001", 42.000000000000000000001L }
  152. };
  153. int n;
  154. for (n = 0; n < sizeof (ltests) / sizeof (ltests[0]); ++n)
  155. if (strtold (ltests[n].str, NULL) != ltests[n].l)
  156. {
  157. printf ("ltests[%d]: %La != %La\n", n,
  158. strtold (ltests[n].str, NULL), ltests[n].l);
  159. status = 1;
  160. }
  161. status |= long_dbl ();
  162. return status ? EXIT_FAILURE : EXIT_SUCCESS;
  163. }
  164. static void
  165. expand (char *dst, int c)
  166. {
  167. if (isprint (c))
  168. {
  169. dst[0] = c;
  170. dst[1] = '\0';
  171. }
  172. else
  173. (void) sprintf (dst, "%#.3o", (unsigned int) c);
  174. }
  175. static int
  176. long_dbl (void)
  177. {
  178. /* Regenerate this string using
  179. echo '(2^53-1)*2^(1024-53)' | bc | sed 's/\([^\]*\)\\*$/ "\1"/'
  180. */
  181. static const char longestdbl[] =
  182. "17976931348623157081452742373170435679807056752584499659891747680315"
  183. "72607800285387605895586327668781715404589535143824642343213268894641"
  184. "82768467546703537516986049910576551282076245490090389328944075868508"
  185. "45513394230458323690322294816580855933212334827479782620414472316873"
  186. "8177180919299881250404026184124858368";
  187. double d = strtod (longestdbl, NULL);
  188. printf ("strtod (\"%s\", NULL) = %g\n", longestdbl, d);
  189. if (d != 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000)
  190. return 1;
  191. return 0;
  192. }
  193. #include <support/test-driver.c>