test-fmemopen.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* Test for fmemopen implementation.
  2. Copyright (C) 2000-2026 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. <https://www.gnu.org/licenses/>. */
  15. static char buffer[] = "foobar";
  16. #include <errno.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <errno.h>
  20. #include <mcheck.h>
  21. static int
  22. do_bz18820 (void)
  23. {
  24. char ch;
  25. FILE *stream;
  26. errno = 0;
  27. stream = fmemopen (&ch, 1, "?");
  28. if (stream)
  29. {
  30. printf ("fmemopen: expected NULL, got %p\n", stream);
  31. fclose (stream);
  32. return 1;
  33. }
  34. if (errno != EINVAL)
  35. {
  36. printf ("fmemopen: got %i, expected EINVAL (%i)\n", errno, EINVAL);
  37. return 10;
  38. }
  39. stream = fmemopen (NULL, 42, "?");
  40. if (stream)
  41. {
  42. printf ("fmemopen: expected NULL, got %p\n", stream);
  43. fclose (stream);
  44. return 2;
  45. }
  46. errno = 0;
  47. stream = fmemopen (NULL, ~0, "w");
  48. if (stream)
  49. {
  50. printf ("fmemopen: expected NULL, got %p\n", stream);
  51. fclose (stream);
  52. return 3;
  53. }
  54. if (errno != ENOMEM)
  55. {
  56. printf ("fmemopen: got %i, expected ENOMEM (%i)\n", errno, ENOMEM);
  57. return 20;
  58. }
  59. return 0;
  60. }
  61. static int
  62. do_test (void)
  63. {
  64. int ch;
  65. FILE *stream;
  66. int ret = 0;
  67. mtrace ();
  68. stream = fmemopen (buffer, strlen (buffer), "r+");
  69. while ((ch = fgetc (stream)) != EOF)
  70. printf ("Got %c\n", ch);
  71. fputc ('1', stream);
  72. if (fflush (stream) != EOF || errno != ENOSPC)
  73. {
  74. printf ("fflush didn't fail with ENOSPC\n");
  75. ret = 1;
  76. }
  77. fclose (stream);
  78. return ret + do_bz18820 ();
  79. }
  80. #define TEST_FUNCTION do_test ()
  81. #include "../test-skeleton.c"