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