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