tst-memstream1.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #include <mcheck.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #ifndef CHAR_T
  5. # define CHAR_T char
  6. # define W(o) o
  7. # define OPEN_MEMSTREAM open_memstream
  8. #endif
  9. #define S(s) S1 (s)
  10. #define S1(s) #s
  11. static void
  12. mcheck_abort (enum mcheck_status ev)
  13. {
  14. printf ("mecheck failed with status %d\n", (int) ev);
  15. exit (1);
  16. }
  17. static int
  18. do_test (void)
  19. {
  20. mcheck_pedantic (mcheck_abort);
  21. CHAR_T *buf = (CHAR_T *) 1l;
  22. size_t len = 12345;
  23. FILE *fp = OPEN_MEMSTREAM (&buf, &len);
  24. if (fp == NULL)
  25. {
  26. printf ("%s failed\n", S(OPEN_MEMSTREAM));
  27. return 1;
  28. }
  29. if (fflush (fp) != 0)
  30. {
  31. puts ("fflush failed");
  32. return 1;
  33. }
  34. if (len != 0)
  35. {
  36. puts ("string after no write not empty");
  37. return 1;
  38. }
  39. if (buf == (CHAR_T *) 1l)
  40. {
  41. puts ("buf not updated");
  42. return 1;
  43. }
  44. if (buf[0] != W('\0'))
  45. {
  46. puts ("buf[0] != 0");
  47. return 1;
  48. }
  49. buf = (CHAR_T *) 1l;
  50. len = 12345;
  51. if (fclose (fp) != 0)
  52. {
  53. puts ("fclose failed");
  54. return 1;
  55. }
  56. if (len != 0)
  57. {
  58. puts ("string after close with no write not empty");
  59. return 1;
  60. }
  61. if (buf == (CHAR_T *) 1l)
  62. {
  63. puts ("buf not updated");
  64. return 1;
  65. }
  66. if (buf[0] != W('\0'))
  67. {
  68. puts ("buf[0] != 0");
  69. return 1;
  70. }
  71. free (buf);
  72. return 0;
  73. }
  74. #define TEST_FUNCTION do_test ()
  75. #include "../test-skeleton.c"