test-fmemopen.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* Test for fmemopen implementation.
  2. Copyright (C) 2000-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Hanno Mueller, kontakt@hanno.de, 2000.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. static char buffer[] = "foobar";
  17. #include <errno.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <errno.h>
  21. #include <mcheck.h>
  22. static int
  23. do_bz18820 (void)
  24. {
  25. char ch;
  26. FILE *stream;
  27. errno = 0;
  28. stream = fmemopen (&ch, 1, "?");
  29. if (stream)
  30. {
  31. printf ("fmemopen: expected NULL, got %p\n", stream);
  32. fclose (stream);
  33. return 1;
  34. }
  35. if (errno != EINVAL)
  36. {
  37. printf ("fmemopen: got %i, expected EINVAL (%i)\n", errno, EINVAL);
  38. return 10;
  39. }
  40. stream = fmemopen (NULL, 42, "?");
  41. if (stream)
  42. {
  43. printf ("fmemopen: expected NULL, got %p\n", stream);
  44. fclose (stream);
  45. return 2;
  46. }
  47. errno = 0;
  48. stream = fmemopen (NULL, ~0, "w");
  49. if (stream)
  50. {
  51. printf ("fmemopen: expected NULL, got %p\n", stream);
  52. fclose (stream);
  53. return 3;
  54. }
  55. if (errno != ENOMEM)
  56. {
  57. printf ("fmemopen: got %i, expected ENOMEM (%i)\n", errno, ENOMEM);
  58. return 20;
  59. }
  60. return 0;
  61. }
  62. static int
  63. do_test (void)
  64. {
  65. int ch;
  66. FILE *stream;
  67. int ret = 0;
  68. mtrace ();
  69. stream = fmemopen (buffer, strlen (buffer), "r+");
  70. while ((ch = fgetc (stream)) != EOF)
  71. printf ("Got %c\n", ch);
  72. fputc ('1', stream);
  73. if (fflush (stream) != EOF || errno != ENOSPC)
  74. {
  75. printf ("fflush didn't fail with ENOSPC\n");
  76. ret = 1;
  77. }
  78. fclose (stream);
  79. return ret + do_bz18820 ();
  80. }
  81. #define TEST_FUNCTION do_test ()
  82. #include "../test-skeleton.c"