[MSVCRT_CRT_APITEST]
[reactos.git] / rostests / apitests / crt / wcstombs_s.c
1 /*
2 * PROJECT: ReactOS api tests
3 * LICENSE: GPL - See COPYING in the top level directory
4 * PURPOSE: Test for wcstombs_s
5 */
6
7 #include <apitest.h>
8
9 #define WIN32_NO_STATUS
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <specstrings.h>
13
14 #define ok_errno(x) ok_hex(errno, (x))
15
16 #undef ok_char
17 #define ok_char(x,y) ok_int((unsigned)(unsigned char)(x),(unsigned)(unsigned char)(y))
18 #define ok_wchar(x,y) ok_int((unsigned)(unsigned short)(x),(unsigned)(unsigned short)(y))
19
20 _Check_return_wat_
21 _CRTIMP
22 errno_t
23 __cdecl
24 wcstombs_s(
25 _Out_opt_ size_t * pcchConverted,
26 _Out_writes_bytes_to_opt_(cjDstSize, *pcchConverted)
27 char * pmbsDst,
28 _In_ size_t cjDstSize,
29 _In_z_ const wchar_t * pwszSrc,
30 _In_ size_t cjMaxCount);
31
32
33 START_TEST(wcstombs_s)
34 {
35 errno_t ret;
36 size_t cchConverted;
37 char mbsbuffer[10];
38
39 *_errno() = 0;
40 cchConverted = 0xf00bac;
41 mbsbuffer[5] = 0xFF;
42 ret = wcstombs_s(&cchConverted, mbsbuffer, 6, L"hallo", 5);
43 ok_long(ret, 0);
44 ok_size_t(cchConverted, 6);
45 ok_char(mbsbuffer[5], 0);
46 ok_str(mbsbuffer, "hallo");
47 ok_errno(0);
48
49 *_errno() = 0;
50 cchConverted = 0xf00bac;
51 mbsbuffer[0] = 0xFF;
52 ret = wcstombs_s(&cchConverted, mbsbuffer, 1, L"", 0);
53 ok_long(ret, 0);
54 ok_size_t(cchConverted, 1);
55 ok_wchar(mbsbuffer[0], 0);
56 ok_errno(0);
57
58 *_errno() = 0;
59 cchConverted = 0xf00bac;
60 mbsbuffer[0] = 0xFF;
61 mbsbuffer[1] = 0xFF;
62 mbsbuffer[2] = 0xFF;
63 mbsbuffer[3] = 0xFF;
64 mbsbuffer[4] = 0xFF;
65 mbsbuffer[5] = 0xFF;
66 ret = wcstombs_s(&cchConverted, mbsbuffer, 5, L"hallo", 5);
67 ok_long(ret, ERANGE);
68 ok_size_t(cchConverted, 0);
69 ok_char(mbsbuffer[5], 0xFF);
70 ok_char(mbsbuffer[4], L'o');
71 ok_char(mbsbuffer[3], L'l');
72 ok_char(mbsbuffer[2], L'l');
73 ok_char(mbsbuffer[1], L'a');
74 ok_char(mbsbuffer[0], 0);
75 ok_errno(ERANGE);
76
77 *_errno() = 0;
78 cchConverted = 0xf00bac;
79 mbsbuffer[0] = 0xFF;
80 mbsbuffer[1] = 0xFF;
81 mbsbuffer[2] = 0xFF;
82 mbsbuffer[3] = 0xFF;
83 mbsbuffer[4] = 0xFF;
84 mbsbuffer[5] = 0xFF;
85 ret = wcstombs_s(&cchConverted, mbsbuffer, 3, L"hallo", 5);
86 ok_long(ret, ERANGE);
87 ok_size_t(cchConverted, 0);
88 ok_char(mbsbuffer[5], 0xFF);
89 ok_char(mbsbuffer[4], 0xFF);
90 ok_char(mbsbuffer[3], 0xFF);
91 ok_char(mbsbuffer[2], L'l');
92 ok_char(mbsbuffer[1], L'a');
93 ok_char(mbsbuffer[0], 0);
94 ok_errno(ERANGE);
95
96 *_errno() = 0;
97 ret = wcstombs_s(0, 0, 0, 0, 0);
98 ok_long(ret, EINVAL);
99 ok_errno(EINVAL);
100
101 *_errno() = 0;
102 cchConverted = 0xf00bac;
103 ret = wcstombs_s(&cchConverted, 0, 0, 0, 0);
104 ok_long(ret, EINVAL);
105 ok_size_t(cchConverted, 0);
106 ok_errno(EINVAL);
107
108 *_errno() = 0;
109 mbsbuffer[0] = L'x';
110 ret = wcstombs_s(0, mbsbuffer, 0, 0, 0);
111 ok_long(ret, EINVAL);
112 ok_char(mbsbuffer[0], L'x');
113 ok_errno(EINVAL);
114
115 *_errno() = 0;
116 ret = wcstombs_s(0, mbsbuffer, 10, L"hallo", 5);
117 ok_long(ret, 0);
118 ok_errno(0);
119
120 *_errno() = 0;
121 ret = wcstombs_s(0, mbsbuffer, 0, L"hallo", 5);
122 ok_long(ret, EINVAL);
123 ok_errno(EINVAL);
124
125 *_errno() = 0;
126 cchConverted = 0xf00bac;
127 ret = wcstombs_s(&cchConverted, 0, 10, L"hallo", 5);
128 ok_long(ret, EINVAL);
129 ok_size_t(cchConverted, 0xf00bac);
130 ok_errno(EINVAL);
131
132 *_errno() = 0;
133 cchConverted = 0xf00bac;
134 ret = wcstombs_s(&cchConverted, 0, 0, L"hallo", 5);
135 ok_long(ret, 0);
136 ok_size_t(cchConverted, 6);
137 ok_errno(0);
138
139 *_errno() = 0;
140 cchConverted = 0xf00bac;
141 ret = wcstombs_s(&cchConverted, mbsbuffer, 10, 0, 5);
142 ok_long(ret, EINVAL);
143 ok_size_t(cchConverted, 0);
144 ok_errno(EINVAL);
145
146 *_errno() = 0;
147 cchConverted = 0xf00bac;
148 ret = wcstombs_s(&cchConverted, mbsbuffer, 10, L"hallo", 0);
149 ok_long(ret, 0);
150 ok_size_t(cchConverted, 1);
151 ok_errno(0);
152
153 *_errno() = 0;
154 cchConverted = 0xf00bac;
155 mbsbuffer[0] = 0xAB;
156 mbsbuffer[1] = 0xCD;
157 mbsbuffer[2] = 0xAB;
158 mbsbuffer[3] = 0xCD;
159 mbsbuffer[4] = 0xAB;
160 mbsbuffer[5] = 0xCD;
161 ret = wcstombs_s(&cchConverted, mbsbuffer, 10, L"hallo", 2);
162 ok_long(ret, 0);
163 ok_size_t(cchConverted, 3);
164 ok_char(mbsbuffer[5], 0xCD);
165 ok_char(mbsbuffer[4], 0xAB);
166 ok_char(mbsbuffer[3], 0xCD);
167 ok_char(mbsbuffer[2], 0);
168 ok_char(mbsbuffer[1], L'a');
169 ok_char(mbsbuffer[0], L'h');
170 ok_errno(0);
171
172 *_errno() = 0;
173 cchConverted = 0xf00bac;
174 ret = wcstombs_s(&cchConverted, mbsbuffer, 10, 0, 0);
175 ok_long(ret, 0);
176 ok_size_t(cchConverted, 1);
177 ok_errno(0);
178
179 *_errno() = 0;
180 cchConverted = 0xf00bac;
181 ret = wcstombs_s(&cchConverted, mbsbuffer, 10, L"hallo", 7);
182 ok_long(ret, 0);
183 ok_size_t(cchConverted, 6);
184 ok_errno(0);
185
186 *_errno() = 0;
187 cchConverted = 0xf00bac;
188 ret = wcstombs_s(&cchConverted, 0, 0, L"hallo", 7);
189 ok_long(ret, 0);
190 ok_size_t(cchConverted, 6);
191 ok_errno(0);
192
193 *_errno() = 0;
194 cchConverted = 0xf00bac;
195 mbsbuffer[0] = 0xAB;
196 mbsbuffer[1] = 0xCD;
197 mbsbuffer[2] = 0xAB;
198 mbsbuffer[3] = 0xCD;
199 mbsbuffer[4] = 0xAB;
200 mbsbuffer[5] = 0xCD;
201 ret = wcstombs_s(&cchConverted, mbsbuffer, 5, L"hallo", _TRUNCATE);
202 ok_long(ret, STRUNCATE);
203 ok_size_t(cchConverted, 5);
204 ok_char(mbsbuffer[5], 0xCD);
205 ok_char(mbsbuffer[4], 0);
206 ok_char(mbsbuffer[3], L'l');
207 ok_char(mbsbuffer[2], L'l');
208 ok_char(mbsbuffer[1], L'a');
209 ok_char(mbsbuffer[0], L'h');
210 ok_errno(0);
211
212 *_errno() = 0;
213 cchConverted = 0xf00bac;
214 ret = wcstombs_s(&cchConverted, mbsbuffer, 10, L"hallo", -1);
215 ok_long(ret, 0);
216 ok_size_t(cchConverted, 6);
217 ok_errno(0);
218
219 }