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