tst-warn-wide.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* Test wide output conversion for warn.
  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 <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <support/check.h>
  21. #include <support/xmemstream.h>
  22. #include <wchar.h>
  23. /* Used to trigger the large-string path in __fxprintf. */
  24. #define PADDING \
  25. "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" \
  26. "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" \
  27. "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
  28. #define LPADDING \
  29. L"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" \
  30. "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" \
  31. "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
  32. static void
  33. one_test (const char *message, int error_code, const wchar_t *expected)
  34. {
  35. wchar_t *buffer = NULL;
  36. size_t length = 0;
  37. FILE *fp = open_wmemstream (&buffer, &length);
  38. TEST_VERIFY_EXIT (fp != NULL);
  39. FILE *old_stderr = stderr;
  40. stderr = fp;
  41. errno = error_code;
  42. switch (error_code)
  43. {
  44. case E2BIG:
  45. warn ("%s with padding " PADDING, message);
  46. break;
  47. case EAGAIN:
  48. warn ("%s", message);
  49. break;
  50. case -1:
  51. warnx ("%s", message);
  52. break;
  53. case -2:
  54. warnx ("%s with padding " PADDING, message);
  55. break;
  56. }
  57. stderr = old_stderr;
  58. TEST_VERIFY_EXIT (!ferror (fp));
  59. TEST_COMPARE (fclose (fp), 0);
  60. if (wcscmp (buffer, expected) != 0)
  61. FAIL_EXIT1 ("unexpected output: %ls", buffer);
  62. free (buffer);
  63. }
  64. static int
  65. do_test (void)
  66. {
  67. one_test ("no errno", -1,
  68. L"tst-warn-wide: no errno\n");
  69. one_test ("no errno", -2,
  70. L"tst-warn-wide: no errno with padding " PADDING "\n");
  71. one_test ("with errno", EAGAIN,
  72. L"tst-warn-wide: with errno: Resource temporarily unavailable\n");
  73. one_test ("with errno", E2BIG,
  74. L"tst-warn-wide: with errno with padding " PADDING
  75. ": Argument list too long\n");
  76. return 0;
  77. }
  78. #include <support/test-driver.c>