tst-memstream4.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /* Test for open_memstream BZ #21037.
  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 "tst-memstream.h"
  16. static void
  17. mcheck_abort (enum mcheck_status ev)
  18. {
  19. printf ("mecheck failed with status %d\n", (int) ev);
  20. exit (1);
  21. }
  22. static int
  23. do_test (void)
  24. {
  25. mcheck_pedantic (mcheck_abort);
  26. /* Check if freopen proper fflush the stream. */
  27. {
  28. CHAR_T old[] = W("old");
  29. CHAR_T *buf = old;
  30. size_t size;
  31. FILE *fp = OPEN_MEMSTREAM (&buf, &size);
  32. TEST_VERIFY_EXIT (fp != NULL);
  33. FPUTS (W("new"), fp);
  34. /* The stream buffer pointer should be updated with only a fflush or
  35. fclose. */
  36. TEST_COMPARE (STRCMP (buf, old), 0);
  37. /* The old stream should be fflush the stream, even for an invalid
  38. streams. */
  39. FILE *nfp = freopen ("invalid-file", "r", fp);
  40. TEST_VERIFY_EXIT (nfp == NULL);
  41. TEST_VERIFY (STRCMP (buf, W("new")) == 0);
  42. TEST_COMPARE_BLOB (buf, STRLEN (buf) * sizeof (CHAR_T),
  43. W("new"), STRLEN (W("new")) * sizeof (CHAR_T));
  44. TEST_COMPARE (fclose (fp), 0);
  45. free (buf);
  46. }
  47. return 0;
  48. }
  49. #include <support/test-driver.c>