bug-wmemstream1.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <wchar.h>
  5. static int
  6. do_test (void)
  7. {
  8. size_t size;
  9. wchar_t *buf;
  10. FILE *fp = open_wmemstream (&buf, &size);
  11. if (fp == NULL)
  12. {
  13. puts ("open_wmemstream failed");
  14. return 1;
  15. }
  16. off64_t off = ftello64 (fp);
  17. if (off != 0)
  18. {
  19. puts ("initial position wrong");
  20. return 1;
  21. }
  22. if (fseek (fp, 32768, SEEK_SET) != 0)
  23. {
  24. puts ("fseek failed");
  25. return 1;
  26. }
  27. if (fputws (L"foo", fp) == EOF)
  28. {
  29. puts ("fputws failed");
  30. return 1;
  31. }
  32. if (fclose (fp) == EOF)
  33. {
  34. puts ("fclose failed");
  35. return 1;
  36. }
  37. if (size != 32768 + 3)
  38. {
  39. printf ("expected size %d, got %zu\n", 32768 + 3, size);
  40. return 1;
  41. }
  42. for (int i = 0; i < 32768; ++i)
  43. if (buf[i] != L'\0')
  44. {
  45. printf ("wide character at offset %d is %#x\n",
  46. i, (unsigned int) buf[i]);
  47. return 1;
  48. }
  49. if (wmemcmp (buf + 32768, L"foo", 3) != 0)
  50. {
  51. puts ("written string incorrect");
  52. return 1;
  53. }
  54. /* Mark the buffer. */
  55. wmemset (buf, L'A', size);
  56. free (buf);
  57. /* Try again, this time with write mode enabled before the seek. */
  58. fp = open_wmemstream (&buf, &size);
  59. if (fp == NULL)
  60. {
  61. puts ("2nd open_wmemstream failed");
  62. return 1;
  63. }
  64. off = ftello64 (fp);
  65. if (off != 0)
  66. {
  67. puts ("2nd initial position wrong");
  68. return 1;
  69. }
  70. if (fputws (L"bar", fp) == EOF)
  71. {
  72. puts ("2nd fputws failed");
  73. return 1;
  74. }
  75. if (fseek (fp, 32768, SEEK_SET) != 0)
  76. {
  77. puts ("2nd fseek failed");
  78. return 1;
  79. }
  80. if (fputws (L"foo", fp) == EOF)
  81. {
  82. puts ("3rd fputws failed");
  83. return 1;
  84. }
  85. if (fclose (fp) == EOF)
  86. {
  87. puts ("2nd fclose failed");
  88. return 1;
  89. }
  90. if (size != 32768 + 3)
  91. {
  92. printf ("2nd expected size %d, got %zu\n", 32768 + 3, size);
  93. return 1;
  94. }
  95. if (wmemcmp (buf, L"bar", 3) != 0)
  96. {
  97. puts ("initial string incorrect in 2nd try");
  98. return 1;
  99. }
  100. for (int i = 3; i < 32768; ++i)
  101. if (buf[i] != L'\0')
  102. {
  103. printf ("wide character at offset %d is %#x in 2nd try\n",
  104. i, (unsigned int) buf[i]);
  105. return 1;
  106. }
  107. if (wmemcmp (buf + 32768, L"foo", 3) != 0)
  108. {
  109. puts ("written string incorrect in 2nd try");
  110. return 1;
  111. }
  112. return 0;
  113. }
  114. #define TEST_FUNCTION do_test ()
  115. #include "../test-skeleton.c"