tst-memstream3.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* Test for open_memstream implementation.
  2. Copyright (C) 2016-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 <mcheck.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <stdarg.h>
  20. #include <errno.h>
  21. #ifndef CHAR_T
  22. # define CHAR_T char
  23. # define W(o) o
  24. # define OPEN_MEMSTREAM open_memstream
  25. # define PRINTF printf
  26. # define FWRITE_FUNC fwrite
  27. # define FPUTC fputc
  28. # define STRCMP strcmp
  29. #endif
  30. #define S(s) S1 (s)
  31. #define S1(s) #s
  32. static void
  33. mcheck_abort (enum mcheck_status ev)
  34. {
  35. printf ("mecheck failed with status %d\n", (int) ev);
  36. exit (1);
  37. }
  38. static void
  39. error_printf (int line, const char *fmt, ...)
  40. {
  41. va_list ap;
  42. printf ("error: %s:%i: ", __FILE__, line);
  43. va_start (ap, fmt);
  44. vprintf (fmt, ap);
  45. va_end (ap);
  46. }
  47. #define ERROR_RET1(...) \
  48. { error_printf(__LINE__, __VA_ARGS__); return 1; }
  49. static int
  50. do_test_bz18241 (void)
  51. {
  52. CHAR_T *buf;
  53. size_t size;
  54. FILE *fp = OPEN_MEMSTREAM (&buf, &size);
  55. if (fp == NULL)
  56. ERROR_RET1 ("%s failed\n", S(OPEN_MEMSTREAM));
  57. if (FPUTC (W('a'), fp) != W('a'))
  58. ERROR_RET1 ("%s failed (errno = %d)\n", S(FPUTC), errno);
  59. if (fflush (fp) != 0)
  60. ERROR_RET1 ("fflush failed (errno = %d)\n", errno);
  61. if (fseek (fp, -2, SEEK_SET) != -1)
  62. ERROR_RET1 ("fseek failed (errno = %d)\n", errno);
  63. if (errno != EINVAL)
  64. ERROR_RET1 ("errno != EINVAL\n");
  65. if (ftell (fp) != 1)
  66. ERROR_RET1 ("ftell failed (errno = %d)\n", errno);
  67. if (ferror (fp) != 0)
  68. ERROR_RET1 ("ferror != 0\n");
  69. if (fseek (fp, -1, SEEK_CUR) == -1)
  70. ERROR_RET1 ("fseek failed (errno = %d)\n", errno);
  71. if (ftell (fp) != 0)
  72. ERROR_RET1 ("ftell failed (errno = %d)\n", errno);
  73. if (ferror (fp) != 0)
  74. ERROR_RET1 ("ferror != 0\n");
  75. if (FPUTC (W('b'), fp) != W('b'))
  76. ERROR_RET1 ("%s failed (errno = %d)\n", S(FPUTC), errno);
  77. if (fflush (fp) != 0)
  78. ERROR_RET1 ("fflush failed (errno = %d)\n", errno);
  79. if (fclose (fp) != 0)
  80. ERROR_RET1 ("fclose failed (errno = %d\n", errno);
  81. if (STRCMP (buf, W("b")) != 0)
  82. ERROR_RET1 ("%s failed\n", S(STRCMP));
  83. free (buf);
  84. return 0;
  85. }
  86. static int
  87. do_test_bz20181 (void)
  88. {
  89. CHAR_T *buf;
  90. size_t size;
  91. size_t ret;
  92. FILE *fp = OPEN_MEMSTREAM (&buf, &size);
  93. if (fp == NULL)
  94. ERROR_RET1 ("%s failed\n", S(OPEN_MEMSTREAM));
  95. if ((ret = FWRITE_FUNC (W("abc"), 1, 3, fp)) != 3)
  96. ERROR_RET1 ("%s failed (errno = %d)\n", S(FWRITE_FUNC), errno);
  97. if (fseek (fp, 0, SEEK_SET) != 0)
  98. ERROR_RET1 ("fseek failed (errno = %d)\n", errno);
  99. if (FWRITE_FUNC (W("z"), 1, 1, fp) != 1)
  100. ERROR_RET1 ("%s failed (errno = %d)\n", S(FWRITE_FUNC), errno);
  101. if (fflush (fp) != 0)
  102. ERROR_RET1 ("fflush failed (errno = %d)\n", errno);
  103. /* Avoid truncating the buffer on close. */
  104. if (fseek (fp, 3, SEEK_SET) != 0)
  105. ERROR_RET1 ("fseek failed (errno = %d)\n", errno);
  106. if (fclose (fp) != 0)
  107. ERROR_RET1 ("fclose failed (errno = %d\n", errno);
  108. if (size != 3)
  109. ERROR_RET1 ("size != 3\n");
  110. if (buf[0] != W('z')
  111. || buf[1] != W('b')
  112. || buf[2] != W('c'))
  113. {
  114. PRINTF (W("error: buf {%c,%c,%c} != {z,b,c}\n"),
  115. buf[0], buf[1], buf[2]);
  116. return 1;
  117. }
  118. free (buf);
  119. return 0;
  120. }
  121. static int
  122. do_test (void)
  123. {
  124. int ret = 0;
  125. mcheck_pedantic (mcheck_abort);
  126. ret += do_test_bz18241 ();
  127. ret += do_test_bz20181 ();
  128. return ret;
  129. }
  130. #define TEST_FUNCTION do_test ()
  131. #include "../test-skeleton.c"