tst-ldbl-error.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* Test for the long double conversions in *err* functions.
  2. Copyright (C) 2018-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 <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. /* The functions err, errx, verr, and verrx print just the program
  72. name followed by a colon, whereas error and error_at_line print the
  73. whole path to the program. Since the whole path depends on the
  74. working directory used to build and test glibc, remove it from the
  75. comparison against the expected result. */
  76. const char *needle = "tst-ldbl-error:";
  77. char *message;
  78. message = strstr (result.err.buffer, needle);
  79. /* Verify that the output message is as expected. */
  80. TEST_COMPARE_STRING (message, expected);
  81. va_end (args);
  82. }
  83. static int
  84. do_test (void)
  85. {
  86. struct tests tests[] = {
  87. { &callback_err, "tst-ldbl-error: -1.000000: Success\n" },
  88. { &callback_errx, "tst-ldbl-error: -1.000000\n" },
  89. { &callback_verr, "tst-ldbl-error: -1.000000: Success\n" },
  90. { &callback_verrx, "tst-ldbl-error: -1.000000\n" },
  91. { &callback_error, "tst-ldbl-error: -1.000000\n" },
  92. { &callback_error_at_line, "tst-ldbl-error::0: -1.000000\n" }
  93. };
  94. for (int i = 0; i < sizeof (tests) / sizeof (tests[0]); i++)
  95. {
  96. do_one_test (tests[i].callback, tests[i].expected, (long double) -1);
  97. }
  98. return 0;
  99. }
  100. #include <support/test-driver.c>