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