tst-ldbl-warn.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* Test for the long double conversions in *warn* 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 <stdarg.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <support/check.h>
  22. #include <support/xmemstream.h>
  23. enum {WARN, WARNX, VWARN, VWARNX};
  24. static void
  25. do_one_test (int select, const char *format, va_list args,
  26. long double arg1, double arg2, long double arg3,
  27. double arg4, const char *expected)
  28. {
  29. /* Prepare in-memory buffer to hold the output. */
  30. struct xmemstream stream;
  31. xopen_memstream (&stream);
  32. FILE *old_stderr = stderr;
  33. stderr = stream.out;
  34. /* Write to the buffer using one of the *warn* functions. */
  35. errno = 0;
  36. switch (select)
  37. {
  38. case WARN:
  39. warn (format, arg1, arg2, arg3, arg4);
  40. break;
  41. case WARNX:
  42. warnx (format, arg1, arg2, arg3, arg4);
  43. break;
  44. case VWARN:
  45. vwarn (format, args);
  46. break;
  47. case VWARNX:
  48. vwarnx (format, args);
  49. break;
  50. }
  51. stderr = old_stderr;
  52. /* Close the in-memory stream and check the output buffer. */
  53. xfclose_memstream (&stream);
  54. TEST_COMPARE_STRING (stream.buffer, expected);
  55. if (stream.buffer != NULL)
  56. free (stream.buffer);
  57. }
  58. static void
  59. do_test_call_varg (const char *format, ...)
  60. {
  61. va_list args;
  62. va_start (args, format);
  63. do_one_test (VWARN, format, args, 0, 0, 0, 0,
  64. "tst-ldbl-warn: "
  65. "-1.000000 - -2.000000 - -3.000000 - -4.000000: Success\n");
  66. va_end (args);
  67. va_start (args, format);
  68. do_one_test (VWARNX, format, args, 0, 0, 0, 0,
  69. "tst-ldbl-warn: "
  70. "-1.000000 - -2.000000 - -3.000000 - -4.000000\n");
  71. va_end (args);
  72. }
  73. static void
  74. do_test_call_rarg (const char *format, long double arg1, double arg2,
  75. long double arg3, double arg4)
  76. {
  77. va_list args;
  78. memset (&args, 0, sizeof (args));
  79. do_one_test (WARN, format, args, arg1, arg2, arg3, arg4,
  80. "tst-ldbl-warn: "
  81. "-1.000000 - -2.000000 - -3.000000 - -4.000000: Success\n");
  82. do_one_test (WARNX, format, args, arg1, arg2, arg3, arg4,
  83. "tst-ldbl-warn: "
  84. "-1.000000 - -2.000000 - -3.000000 - -4.000000\n");
  85. }
  86. static int
  87. do_test (void)
  88. {
  89. long double arg1 = -1;
  90. long double arg3 = -3;
  91. double arg2 = -2;
  92. double arg4 = -4;
  93. do_test_call_rarg ("%Lf - %f - %Lf - %f", arg1, arg2, arg3, arg4);
  94. do_test_call_varg ("%Lf - %f - %Lf - %f", arg1, arg2, arg3, arg4);
  95. return 0;
  96. }
  97. #include <support/test-driver.c>