tst-ldbl-error.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* Test for the long double conversions in *err* functions.
  2. Copyright (C) 2018-2026 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. <https://www.gnu.org/licenses/>. */
  15. #include <err.h>
  16. #include <errno.h>
  17. #include <error.h>
  18. #include <stdarg.h>
  19. #include <string.h>
  20. #include <support/capture_subprocess.h>
  21. #include <support/check.h>
  22. struct tests
  23. {
  24. void *callback;
  25. const char *expected;
  26. };
  27. va_list args;
  28. static void
  29. callback_err (void *closure)
  30. {
  31. errno = 0;
  32. err (0, "%Lf", (long double) -1);
  33. }
  34. static void
  35. callback_errx (void *closure)
  36. {
  37. errno = 0;
  38. errx (0, "%Lf", (long double) -1);
  39. }
  40. static void
  41. callback_verr (void *closure)
  42. {
  43. errno = 0;
  44. verr (0, "%Lf", args);
  45. }
  46. static void
  47. callback_verrx (void *closure)
  48. {
  49. errno = 0;
  50. verrx (0, "%Lf", args);
  51. }
  52. static void
  53. callback_error (void *closure)
  54. {
  55. errno = 0;
  56. error (0, 0, "%Lf", (long double) -1);
  57. }
  58. static void
  59. callback_error_at_line (void *closure)
  60. {
  61. errno = 0;
  62. error_at_line (0, 0, "", 0, "%Lf", (long double) -1);
  63. }
  64. static void
  65. do_one_test (void *callback, const char *expected, ...)
  66. {
  67. struct support_capture_subprocess result;
  68. va_start (args, expected);
  69. /* Call 'callback', which fills in the output and error buffers. */
  70. result = support_capture_subprocess (callback, NULL);
  71. /* Filter out the name of the program (which should always end with
  72. -error), so that the test case can be reused by ldbl-opt and
  73. ldbl-128ibm-compat. */
  74. const char *needle = "-error:";
  75. char *message;
  76. message = strstr (result.err.buffer, needle);
  77. if (message == NULL)
  78. FAIL_EXIT1 ("test case error");
  79. message += strlen (needle);
  80. /* Verify that the output message is as expected. */
  81. TEST_COMPARE_STRING (message, expected);
  82. va_end (args);
  83. }
  84. static int
  85. do_test (void)
  86. {
  87. struct tests tests[] = {
  88. { &callback_err, " -1.000000: Success\n" },
  89. { &callback_errx, " -1.000000\n" },
  90. { &callback_verr, " -1.000000: Success\n" },
  91. { &callback_verrx, " -1.000000\n" },
  92. { &callback_error, " -1.000000\n" },
  93. { &callback_error_at_line, ":0: -1.000000\n" }
  94. };
  95. for (int i = 0; i < sizeof (tests) / sizeof (tests[0]); i++)
  96. {
  97. do_one_test (tests[i].callback, tests[i].expected, (long double) -1);
  98. }
  99. return 0;
  100. }
  101. #include <support/test-driver.c>