tst-strfrom.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* Tests for strfromf, strfromd, strfroml functions.
  2. Copyright (C) 2016-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 <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <float.h>
  19. #include <math.h>
  20. #include <locale.h>
  21. #include "tst-strtod.h"
  22. #define _CONCAT(a, b) a ## b
  23. #define CONCAT(a, b) _CONCAT (a, b)
  24. /* Generator to create an FTYPE member variabled named FSUF
  25. * used to populate struct member variables. */
  26. #define FTYPE_MEMBER(FSUF, FTYPE, FTOSTR, LSUF, CSUF) \
  27. FTYPE FSUF;
  28. #define STRUCT_FOREACH_FLOAT_FTYPE GEN_TEST_STRTOD_FOREACH (FTYPE_MEMBER)
  29. #define ENTRY(FSUF, FTYPE, FTOSTR, LSUF, CSUF, ...) \
  30. CONCAT (__VA_ARGS__, LSUF),
  31. /* This is hacky way around the seemingly unavoidable macro
  32. * expansion of the INFINITY or HUGE_VAL like macros in the
  33. * above. It is assumed the compiler will implicitly convert
  34. * the infinity correctly. */
  35. #define INF INFINITY + 0.0
  36. #define NAN_ NAN + 0.0
  37. struct test_input
  38. {
  39. STRUCT_FOREACH_FLOAT_FTYPE
  40. };
  41. struct test {
  42. const char *s;
  43. const char *fmt;
  44. int size;
  45. int rc;
  46. struct test_input t;
  47. };
  48. #define TEST(s, fmt, size, rc, val) \
  49. { \
  50. s, fmt, size, rc, { GEN_TEST_STRTOD_FOREACH (ENTRY, val) } \
  51. }
  52. /* Hexadecimal tests. */
  53. struct htests
  54. {
  55. const char *fmt;
  56. const char *exp[4];
  57. struct test_input t;
  58. };
  59. #define HTEST(fmt, exp1, exp2, exp3, exp4, val) \
  60. { \
  61. fmt, exp1, exp2, exp3, exp4, { GEN_TEST_STRTOD_FOREACH (ENTRY, val) } \
  62. }
  63. #define TEST_STRFROM(FSUF, FTYPE, FTOSTR, LSUF, CSUF) \
  64. static int \
  65. test_ ## FSUF (void) \
  66. { \
  67. char buf[50], sbuf[5]; \
  68. int status = 0; \
  69. int i, rc = 0, rc1 = 0; \
  70. for (i = 0; i < sizeof (stest) / sizeof (stest[0]); i++) \
  71. { \
  72. rc = FTOSTR (sbuf, stest[i].size, stest[i].fmt, stest[i].t.FSUF); \
  73. rc1 = (strcmp (sbuf, stest[i].s) != 0) || (rc != stest[i].rc); \
  74. if (rc1) \
  75. { \
  76. printf (#FTOSTR ": got %s (%d), expected %s (%d)\n", \
  77. sbuf, rc, stest[i].s, stest[i].rc); \
  78. status++; \
  79. } \
  80. } \
  81. for (i = 0; i < sizeof (tests) / sizeof (tests[0]); i++) \
  82. { \
  83. rc = FTOSTR (buf, tests[i].size, tests[i].fmt, tests[i].t.FSUF); \
  84. rc1 = (strcmp (buf, tests[i].s) != 0) || (rc != tests[i].rc); \
  85. if (rc1) \
  86. { \
  87. printf (#FTOSTR ": got %s (%d), expected %s (%d)\n", \
  88. buf, rc, tests[i].s, tests[i].rc); \
  89. status++; \
  90. } \
  91. } \
  92. for (i = 0; i < sizeof (htest) / sizeof (htest[0]); i++) \
  93. { \
  94. rc = FTOSTR (buf, 50, htest[i].fmt, htest[i].t.FSUF); \
  95. if (strcmp (buf, htest[i].exp[0]) == 0 || \
  96. strcmp (buf, htest[i].exp[1]) == 0 || \
  97. strcmp (buf, htest[i].exp[2]) == 0 || \
  98. strcmp (buf, htest[i].exp[3]) == 0) \
  99. continue; \
  100. else \
  101. { \
  102. printf (#FTOSTR ": got %s (%d), expected %s or %s or %s " \
  103. "or %s\n", buf, rc, htest[i].exp[0], htest[i].exp[1], \
  104. htest[i].exp[2], htest[i].exp[3]); \
  105. status++; \
  106. } \
  107. } \
  108. return status; \
  109. }