[USB]
[reactos.git] / rostests / winetests / msvcrt / string.c
1 /*
2 * Unit test suite for string functions.
3 *
4 * Copyright 2004 Uwe Bonnes
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #include "wine/test.h"
22 #include "winbase.h"
23 #include "winnls.h"
24 #include <string.h>
25 #include <mbstring.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <mbctype.h>
29 #include <locale.h>
30 #include <errno.h>
31 #include <limits.h>
32
33 /* ReactOS */
34 typedef unsigned int __msvcrt_ulong;
35
36 static char *buf_to_string(const unsigned char *bin, int len, int nr)
37 {
38 static char buf[2][1024];
39 char *w = buf[nr];
40 int i;
41
42 for (i = 0; i < len; i++)
43 {
44 sprintf(w, "%02x ", (unsigned char)bin[i]);
45 w += strlen(w);
46 }
47 return buf[nr];
48 }
49
50 static void __cdecl test_invalid_parameter_handler(const wchar_t *expression,
51 const wchar_t *function, const wchar_t *file,
52 unsigned line, uintptr_t arg)
53 {
54 /* we just ignore handler calls */
55 }
56
57 #define expect_eq(expr, value, type, format) { type ret = (expr); ok((value) == ret, #expr " expected " format " got " format "\n", value, ret); }
58 #define expect_bin(buf, value, len) { ok(memcmp((buf), value, len) == 0, "Binary buffer mismatch - expected %s, got %s\n", buf_to_string((unsigned char *)value, len, 1), buf_to_string((buf), len, 0)); }
59
60 static void* (__cdecl *pmemcpy)(void *, const void *, size_t n);
61 static int* (__cdecl *pmemcmp)(void *, const void *, size_t n);
62 static int (__cdecl *pstrcpy_s)(char *dst, size_t len, const char *src);
63 static int (__cdecl *pstrcat_s)(char *dst, size_t len, const char *src);
64 static int (__cdecl *p_mbsnbcat_s)(unsigned char *dst, size_t size, const unsigned char *src, size_t count);
65 static int (__cdecl *p_mbsnbcpy_s)(unsigned char * dst, size_t size, const unsigned char * src, size_t count);
66 static int (__cdecl *p_wcscpy_s)(wchar_t *wcDest, size_t size, const wchar_t *wcSrc);
67 static int (__cdecl *p_wcsncpy_s)(wchar_t *wcDest, size_t size, const wchar_t *wcSrc, size_t count);
68 static int (__cdecl *p_wcsncat_s)(wchar_t *dst, size_t elem, const wchar_t *src, size_t count);
69 static int (__cdecl *p_wcsupr_s)(wchar_t *str, size_t size);
70 static size_t (__cdecl *p_strnlen)(const char *, size_t);
71 static __int64 (__cdecl *p_strtoi64)(const char *, char **, int);
72 static unsigned __int64 (__cdecl *p_strtoui64)(const char *, char **, int);
73 static int (__cdecl *pwcstombs_s)(size_t*,char*,size_t,const wchar_t*,size_t);
74 static int (__cdecl *pmbstowcs_s)(size_t*,wchar_t*,size_t,const char*,size_t);
75 static size_t (__cdecl *pwcsrtombs)(char*, const wchar_t**, size_t, int*);
76 static errno_t (__cdecl *p_gcvt_s)(char*,size_t,double,int);
77 static errno_t (__cdecl *p_itoa_s)(int,char*,size_t,int);
78 static errno_t (__cdecl *p_strlwr_s)(char*,size_t);
79 static errno_t (__cdecl *p_ultoa_s)(__msvcrt_ulong,char*,size_t,int);
80 static int *p__mb_cur_max;
81 static unsigned char *p_mbctype;
82 static _invalid_parameter_handler (__cdecl *p_set_invalid_parameter_handler)(_invalid_parameter_handler);
83 static int (__cdecl *p_wcslwr_s)(wchar_t*,size_t);
84 static errno_t (__cdecl *p_mbsupr_s)(unsigned char *str, size_t numberOfElements);
85 static errno_t (__cdecl *p_mbslwr_s)(unsigned char *str, size_t numberOfElements);
86 static int (__cdecl *p_wctob)(wint_t);
87 static int (__cdecl *p_tolower)(int);
88
89 #define SETNOFAIL(x,y) x = (void*)GetProcAddress(hMsvcrt,y)
90 #define SET(x,y) SETNOFAIL(x,y); ok(x != NULL, "Export '%s' not found\n", y)
91
92 static HMODULE hMsvcrt;
93
94 static void test_swab( void ) {
95 char original[] = "BADCFEHGJILKNMPORQTSVUXWZY@#";
96 char expected1[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ@#";
97 char expected2[] = "ABCDEFGHIJKLMNOPQRSTUVWX$";
98 char expected3[] = "$";
99
100 char from[30];
101 char to[30];
102
103 int testsize;
104
105 /* Test 1 - normal even case */
106 memset(to,'$', sizeof(to));
107 memset(from,'@', sizeof(from));
108 testsize = 26;
109 memcpy(from, original, testsize);
110 _swab( from, to, testsize );
111 ok(memcmp(to,expected1,testsize) == 0, "Testing even size %d returned '%*.*s'\n", testsize, testsize, testsize, to);
112
113 /* Test 2 - uneven case */
114 memset(to,'$', sizeof(to));
115 memset(from,'@', sizeof(from));
116 testsize = 25;
117 memcpy(from, original, testsize);
118 _swab( from, to, testsize );
119 ok(memcmp(to,expected2,testsize) == 0, "Testing odd size %d returned '%*.*s'\n", testsize, testsize, testsize, to);
120
121 /* Test 3 - from = to */
122 memset(to,'$', sizeof(to));
123 memset(from,'@', sizeof(from));
124 testsize = 26;
125 memcpy(to, original, testsize);
126 _swab( to, to, testsize );
127 ok(memcmp(to,expected1,testsize) == 0, "Testing overlapped size %d returned '%*.*s'\n", testsize, testsize, testsize, to);
128
129 /* Test 4 - 1 bytes */
130 memset(to,'$', sizeof(to));
131 memset(from,'@', sizeof(from));
132 testsize = 1;
133 memcpy(from, original, testsize);
134 _swab( from, to, testsize );
135 ok(memcmp(to,expected3,testsize) == 0, "Testing small size %d returned '%*.*s'\n", testsize, testsize, testsize, to);
136 }
137
138 #if 0 /* use this to generate more tests */
139
140 static void test_codepage(int cp)
141 {
142 int i;
143 int prev;
144 int count = 1;
145
146 ok(_setmbcp(cp) == 0, "Couldn't set mbcp\n");
147
148 prev = p_mbctype[0];
149 printf("static int result_cp_%d_mbctype[] = { ", cp);
150 for (i = 1; i < 257; i++)
151 {
152 if (p_mbctype[i] != prev)
153 {
154 printf("0x%x,%d, ", prev, count);
155 prev = p_mbctype[i];
156 count = 1;
157 }
158 else
159 count++;
160 }
161 printf("0x%x,%d };\n", prev, count);
162 }
163
164 #else
165
166 /* RLE-encoded mbctype tables for given codepages */
167 static int result_cp_932_mbctype[] = { 0x0,65, 0x8,1, 0x18,26, 0x8,6, 0x28,26, 0x8,4,
168 0x0,1, 0x8,1, 0xc,31, 0x8,1, 0xa,5, 0x9,58, 0xc,29, 0,3 };
169 static int result_cp_936_mbctype[] = { 0x0,65, 0x8,1, 0x18,26, 0x8,6, 0x28,26, 0x8,6,
170 0xc,126, 0,1 };
171 static int result_cp_949_mbctype[] = { 0x0,66, 0x18,26, 0x8,6, 0x28,26, 0x8,6, 0xc,126,
172 0,1 };
173 static int result_cp_950_mbctype[] = { 0x0,65, 0x8,1, 0x18,26, 0x8,6, 0x28,26, 0x8,4,
174 0x0,2, 0x4,32, 0xc,94, 0,1 };
175
176 static void test_cp_table(int cp, int *result)
177 {
178 int i;
179 int count = 0;
180 int curr = 0;
181 _setmbcp(cp);
182 for (i = 0; i < 256; i++)
183 {
184 if (count == 0)
185 {
186 curr = result[0];
187 count = result[1];
188 result += 2;
189 }
190 ok(p_mbctype[i] == curr, "CP%d: Mismatch in ctype for character %d - %d instead of %d\n", cp, i-1, p_mbctype[i], curr);
191 count--;
192 }
193 }
194
195 #define test_codepage(num) test_cp_table(num, result_cp_##num##_mbctype);
196
197 #endif
198
199 static void test_mbcp(void)
200 {
201 int mb_orig_max = *p__mb_cur_max;
202 int curr_mbcp = _getmbcp();
203 unsigned char *mbstring = (unsigned char *)"\xb0\xb1\xb2 \xb3\xb4 \xb5"; /* incorrect string */
204 unsigned char *mbstring2 = (unsigned char *)"\xb0\xb1\xb2\xb3Q\xb4\xb5"; /* correct string */
205 unsigned char *mbsonlylead = (unsigned char *)"\xb0\0\xb1\xb2 \xb3";
206 unsigned char buf[16];
207 int step;
208 CPINFO cp_info;
209
210 /* _mbtype tests */
211
212 /* An SBCS codepage test. The ctype of characters on e.g. CP1252 or CP1250 differs slightly
213 * between versions of Windows. Also Windows 9x seems to ignore the codepage and always uses
214 * CP1252 (or the ACP?) so we test only a few ASCII characters */
215 _setmbcp(1252);
216 expect_eq(p_mbctype[10], 0, char, "%x");
217 expect_eq(p_mbctype[50], 0, char, "%x");
218 expect_eq(p_mbctype[66], _SBUP, char, "%x");
219 expect_eq(p_mbctype[100], _SBLOW, char, "%x");
220 expect_eq(p_mbctype[128], 0, char, "%x");
221 _setmbcp(1250);
222 expect_eq(p_mbctype[10], 0, char, "%x");
223 expect_eq(p_mbctype[50], 0, char, "%x");
224 expect_eq(p_mbctype[66], _SBUP, char, "%x");
225 expect_eq(p_mbctype[100], _SBLOW, char, "%x");
226 expect_eq(p_mbctype[128], 0, char, "%x");
227
228 /* double byte code pages */
229 test_codepage(932);
230 test_codepage(936);
231 test_codepage(949);
232 test_codepage(950);
233
234 _setmbcp(936);
235 ok(*p__mb_cur_max == mb_orig_max, "__mb_cur_max shouldn't be updated (is %d != %d)\n", *p__mb_cur_max, mb_orig_max);
236 ok(_ismbblead('\354'), "\354 should be a lead byte\n");
237 ok(_ismbblead(' ') == FALSE, "' ' should not be a lead byte\n");
238 ok(_ismbblead(0x1234b0), "0x1234b0 should not be a lead byte\n");
239 ok(_ismbblead(0x123420) == FALSE, "0x123420 should not be a lead byte\n");
240 ok(_ismbbtrail('\xb0'), "\xa0 should be a trail byte\n");
241 ok(_ismbbtrail(' ') == FALSE, "' ' should not be a trail byte\n");
242
243 /* _ismbslead */
244 expect_eq(_ismbslead(mbstring, &mbstring[0]), -1, int, "%d");
245 expect_eq(_ismbslead(mbstring, &mbstring[1]), FALSE, int, "%d");
246 expect_eq(_ismbslead(mbstring, &mbstring[2]), -1, int, "%d");
247 expect_eq(_ismbslead(mbstring, &mbstring[3]), FALSE, int, "%d");
248 expect_eq(_ismbslead(mbstring, &mbstring[4]), -1, int, "%d");
249 expect_eq(_ismbslead(mbstring, &mbstring[5]), FALSE, int, "%d");
250 expect_eq(_ismbslead(mbstring, &mbstring[6]), FALSE, int, "%d");
251 expect_eq(_ismbslead(mbstring, &mbstring[7]), -1, int, "%d");
252 expect_eq(_ismbslead(mbstring, &mbstring[8]), FALSE, int, "%d");
253
254 expect_eq(_ismbslead(mbsonlylead, &mbsonlylead[0]), -1, int, "%d");
255 expect_eq(_ismbslead(mbsonlylead, &mbsonlylead[1]), FALSE, int, "%d");
256 expect_eq(_ismbslead(mbsonlylead, &mbsonlylead[2]), FALSE, int, "%d");
257 expect_eq(_ismbslead(mbsonlylead, &mbsonlylead[5]), FALSE, int, "%d");
258
259 /* _ismbstrail */
260 expect_eq(_ismbstrail(mbstring, &mbstring[0]), FALSE, int, "%d");
261 expect_eq(_ismbstrail(mbstring, &mbstring[1]), -1, int, "%d");
262 expect_eq(_ismbstrail(mbstring, &mbstring[2]), FALSE, int, "%d");
263 expect_eq(_ismbstrail(mbstring, &mbstring[3]), -1, int, "%d");
264 expect_eq(_ismbstrail(mbstring, &mbstring[4]), FALSE, int, "%d");
265 expect_eq(_ismbstrail(mbstring, &mbstring[5]), -1, int, "%d");
266 expect_eq(_ismbstrail(mbstring, &mbstring[6]), FALSE, int, "%d");
267 expect_eq(_ismbstrail(mbstring, &mbstring[7]), FALSE, int, "%d");
268 expect_eq(_ismbstrail(mbstring, &mbstring[8]), -1, int, "%d");
269
270 expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[0]), FALSE, int, "%d");
271 expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[1]), -1, int, "%d");
272 expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[2]), FALSE, int, "%d");
273 expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[3]), FALSE, int, "%d");
274 expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[4]), FALSE, int, "%d");
275 expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[5]), FALSE, int, "%d");
276
277 /* _mbsbtype */
278 expect_eq(_mbsbtype(mbstring, 0), _MBC_LEAD, int, "%d");
279 expect_eq(_mbsbtype(mbstring, 1), _MBC_TRAIL, int, "%d");
280 expect_eq(_mbsbtype(mbstring, 2), _MBC_LEAD, int, "%d");
281 expect_eq(_mbsbtype(mbstring, 3), _MBC_ILLEGAL, int, "%d");
282 expect_eq(_mbsbtype(mbstring, 4), _MBC_LEAD, int, "%d");
283 expect_eq(_mbsbtype(mbstring, 5), _MBC_TRAIL, int, "%d");
284 expect_eq(_mbsbtype(mbstring, 6), _MBC_SINGLE, int, "%d");
285 expect_eq(_mbsbtype(mbstring, 7), _MBC_LEAD, int, "%d");
286 expect_eq(_mbsbtype(mbstring, 8), _MBC_ILLEGAL, int, "%d");
287
288 expect_eq(_mbsbtype(mbsonlylead, 0), _MBC_LEAD, int, "%d");
289 expect_eq(_mbsbtype(mbsonlylead, 1), _MBC_ILLEGAL, int, "%d");
290 expect_eq(_mbsbtype(mbsonlylead, 2), _MBC_ILLEGAL, int, "%d");
291 expect_eq(_mbsbtype(mbsonlylead, 3), _MBC_ILLEGAL, int, "%d");
292 expect_eq(_mbsbtype(mbsonlylead, 4), _MBC_ILLEGAL, int, "%d");
293 expect_eq(_mbsbtype(mbsonlylead, 5), _MBC_ILLEGAL, int, "%d");
294
295 /* _mbsnextc */
296 expect_eq(_mbsnextc(mbstring), 0xb0b1, int, "%x");
297 expect_eq(_mbsnextc(&mbstring[2]), 0xb220, int, "%x"); /* lead + invalid tail */
298 expect_eq(_mbsnextc(&mbstring[3]), 0x20, int, "%x"); /* single char */
299
300 /* _mbclen/_mbslen */
301 expect_eq(_mbclen(mbstring), 2, int, "%d");
302 expect_eq(_mbclen(&mbstring[2]), 2, int, "%d");
303 expect_eq(_mbclen(&mbstring[3]), 1, int, "%d");
304 expect_eq(_mbslen(mbstring2), 4, int, "%d");
305 expect_eq(_mbslen(mbsonlylead), 0, int, "%d"); /* lead + NUL not counted as character */
306 expect_eq(_mbslen(mbstring), 4, int, "%d"); /* lead + invalid trail counted */
307
308 /* _mbccpy/_mbsncpy */
309 memset(buf, 0xff, sizeof(buf));
310 _mbccpy(buf, mbstring);
311 expect_bin(buf, "\xb0\xb1\xff", 3);
312
313 memset(buf, 0xff, sizeof(buf));
314 _mbsncpy(buf, mbstring, 1);
315 expect_bin(buf, "\xb0\xb1\xff", 3);
316 memset(buf, 0xff, sizeof(buf));
317 _mbsncpy(buf, mbstring, 2);
318 expect_bin(buf, "\xb0\xb1\xb2 \xff", 5);
319 memset(buf, 0xff, sizeof(buf));
320 _mbsncpy(buf, mbstring, 3);
321 expect_bin(buf, "\xb0\xb1\xb2 \xb3\xb4\xff", 7);
322 memset(buf, 0xff, sizeof(buf));
323 _mbsncpy(buf, mbstring, 4);
324 expect_bin(buf, "\xb0\xb1\xb2 \xb3\xb4 \xff", 8);
325 memset(buf, 0xff, sizeof(buf));
326 _mbsncpy(buf, mbstring, 5);
327 expect_bin(buf, "\xb0\xb1\xb2 \xb3\xb4 \0\0\xff", 10);
328 memset(buf, 0xff, sizeof(buf));
329 _mbsncpy(buf, mbsonlylead, 6);
330 expect_bin(buf, "\0\0\0\0\0\0\0\xff", 8);
331
332 memset(buf, 0xff, sizeof(buf));
333 _mbsnbcpy(buf, mbstring2, 2);
334 expect_bin(buf, "\xb0\xb1\xff", 3);
335 _mbsnbcpy(buf, mbstring2, 3);
336 expect_bin(buf, "\xb0\xb1\0\xff", 4);
337 _mbsnbcpy(buf, mbstring2, 4);
338 expect_bin(buf, "\xb0\xb1\xb2\xb3\xff", 5);
339 memset(buf, 0xff, sizeof(buf));
340 _mbsnbcpy(buf, mbsonlylead, 5);
341 expect_bin(buf, "\0\0\0\0\0\xff", 6);
342
343 /* _mbsinc/mbsdec */
344 step = _mbsinc(mbstring) - mbstring;
345 ok(step == 2, "_mbsinc adds %d (exp. 2)\n", step);
346 step = _mbsinc(&mbstring[2]) - &mbstring[2]; /* lead + invalid tail */
347 ok(step == 2, "_mbsinc adds %d (exp. 2)\n", step);
348
349 step = _mbsninc(mbsonlylead, 1) - mbsonlylead;
350 ok(step == 0, "_mbsninc adds %d (exp. 0)\n", step);
351 step = _mbsninc(mbsonlylead, 2) - mbsonlylead; /* lead + NUL byte + lead + char */
352 ok(step == 0, "_mbsninc adds %d (exp. 0)\n", step);
353 step = _mbsninc(mbstring2, 0) - mbstring2;
354 ok(step == 0, "_mbsninc adds %d (exp. 2)\n", step);
355 step = _mbsninc(mbstring2, 1) - mbstring2;
356 ok(step == 2, "_mbsninc adds %d (exp. 2)\n", step);
357 step = _mbsninc(mbstring2, 2) - mbstring2;
358 ok(step == 4, "_mbsninc adds %d (exp. 4)\n", step);
359 step = _mbsninc(mbstring2, 3) - mbstring2;
360 ok(step == 5, "_mbsninc adds %d (exp. 5)\n", step);
361 step = _mbsninc(mbstring2, 4) - mbstring2;
362 ok(step == 7, "_mbsninc adds %d (exp. 7)\n", step);
363 step = _mbsninc(mbstring2, 5) - mbstring2;
364 ok(step == 7, "_mbsninc adds %d (exp. 7)\n", step);
365 step = _mbsninc(mbstring2, 17) - mbstring2;
366 ok(step == 7, "_mbsninc adds %d (exp. 7)\n", step);
367
368 /* functions that depend on locale codepage, not mbcp.
369 * we hope the current locale to be SBCS because setlocale(LC_ALL, ".1252") seems not to work yet
370 * (as of Wine 0.9.43)
371 */
372 GetCPInfo(GetACP(), &cp_info);
373 if (cp_info.MaxCharSize == 1)
374 {
375 expect_eq(mblen((char *)mbstring, 3), 1, int, "%x");
376 expect_eq(_mbstrlen((char *)mbstring2), 7, int, "%d");
377 }
378 else
379 skip("Current locale has double-byte charset - could lead to false positives\n");
380
381 _setmbcp(1361);
382 expect_eq(_ismbblead(0x80), 0, int, "%d");
383 todo_wine {
384 expect_eq(_ismbblead(0x81), 1, int, "%d");
385 expect_eq(_ismbblead(0x83), 1, int, "%d");
386 }
387 expect_eq(_ismbblead(0x84), 1, int, "%d");
388 expect_eq(_ismbblead(0xd3), 1, int, "%d");
389 expect_eq(_ismbblead(0xd7), 0, int, "%d");
390 todo_wine {
391 expect_eq(_ismbblead(0xd8), 1, int, "%d");
392 }
393 expect_eq(_ismbblead(0xd9), 1, int, "%d");
394
395 expect_eq(_ismbbtrail(0x30), 0, int, "%d");
396 expect_eq(_ismbbtrail(0x31), 1, int, "%d");
397 expect_eq(_ismbbtrail(0x7e), 1, int, "%d");
398 expect_eq(_ismbbtrail(0x7f), 0, int, "%d");
399 expect_eq(_ismbbtrail(0x80), 0, int, "%d");
400 expect_eq(_ismbbtrail(0x81), 1, int, "%d");
401 expect_eq(_ismbbtrail(0xfe), 1, int, "%d");
402 expect_eq(_ismbbtrail(0xff), 0, int, "%d");
403
404 _setmbcp(curr_mbcp);
405 }
406
407 static void test_mbsspn( void)
408 {
409 unsigned char str1[]="cabernet";
410 unsigned char str2[]="shiraz";
411 unsigned char set[]="abc";
412 unsigned char empty[]="";
413 int ret;
414 ret=_mbsspn( str1, set);
415 ok( ret==3, "_mbsspn returns %d should be 3\n", ret);
416 ret=_mbsspn( str2, set);
417 ok( ret==0, "_mbsspn returns %d should be 0\n", ret);
418 ret=_mbsspn( str1, empty);
419 ok( ret==0, "_mbsspn returns %d should be 0\n", ret);
420 }
421
422 static void test_mbsspnp( void)
423 {
424 unsigned char str1[]="cabernet";
425 unsigned char str2[]="shiraz";
426 unsigned char set[]="abc";
427 unsigned char empty[]="";
428 unsigned char full[]="abcenrt";
429 unsigned char* ret;
430 ret=_mbsspnp( str1, set);
431 ok( ret[0]=='e', "_mbsspnp returns %c should be e\n", ret[0]);
432 ret=_mbsspnp( str2, set);
433 ok( ret[0]=='s', "_mbsspnp returns %c should be s\n", ret[0]);
434 ret=_mbsspnp( str1, empty);
435 ok( ret[0]=='c', "_mbsspnp returns %c should be c\n", ret[0]);
436 ret=_mbsspnp( str1, full);
437 ok( ret==NULL, "_mbsspnp returns %p should be NULL\n", ret);
438 }
439
440 static void test_strdup(void)
441 {
442 char *str;
443 str = _strdup( 0 );
444 ok( str == 0, "strdup returns %s should be 0\n", str);
445 free( str );
446 }
447
448 static void test_strcpy_s(void)
449 {
450 char dest[8];
451 const char *small = "small";
452 const char *big = "atoolongstringforthislittledestination";
453 int ret;
454
455 if(!pstrcpy_s)
456 {
457 skip("strcpy_s not found\n");
458 return;
459 }
460
461 memset(dest, 'X', sizeof(dest));
462 ret = pstrcpy_s(dest, sizeof(dest), small);
463 ok(ret == 0, "Copying a string into a big enough destination returned %d, expected 0\n", ret);
464 ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == 'l' &&
465 dest[4] == 'l' && dest[5] == '\0'&& dest[6] == 'X' && dest[7] == 'X',
466 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
467 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
468
469 memset(dest, 'X', sizeof(dest));
470 ret = pstrcpy_s(dest, 0, big);
471 ok(ret == EINVAL, "Copying into a destination of size 0 returned %d, expected EINVAL\n", ret);
472 ok(dest[0] == 'X' && dest[1] == 'X' && dest[2] == 'X' && dest[3] == 'X' &&
473 dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
474 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
475 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
476 ret = pstrcpy_s(dest, 0, NULL);
477 ok(ret == EINVAL, "Copying into a destination of size 0 returned %d, expected EINVAL\n", ret);
478 ok(dest[0] == 'X' && dest[1] == 'X' && dest[2] == 'X' && dest[3] == 'X' &&
479 dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
480 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
481 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
482
483 memset(dest, 'X', sizeof(dest));
484 ret = pstrcpy_s(dest, sizeof(dest), big);
485 ok(ret == ERANGE, "Copying a big string in a small location returned %d, expected ERANGE\n", ret);
486 ok(dest[0] == '\0'&& dest[1] == 't' && dest[2] == 'o' && dest[3] == 'o' &&
487 dest[4] == 'l' && dest[5] == 'o' && dest[6] == 'n' && dest[7] == 'g',
488 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
489 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
490
491 memset(dest, 'X', sizeof(dest));
492 ret = pstrcpy_s(dest, sizeof(dest), NULL);
493 ok(ret == EINVAL, "Copying from a NULL source string returned %d, expected EINVAL\n", ret);
494 ok(dest[0] == '\0'&& dest[1] == 'X' && dest[2] == 'X' && dest[3] == 'X' &&
495 dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
496 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
497 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
498
499 ret = pstrcpy_s(NULL, sizeof(dest), small);
500 ok(ret == EINVAL, "Copying a big string a NULL dest returned %d, expected EINVAL\n", ret);
501 }
502
503 static void test_strcat_s(void)
504 {
505 char dest[8];
506 const char *small = "sma";
507 int ret;
508
509 if(!pstrcat_s)
510 {
511 skip("strcat_s not found\n");
512 return;
513 }
514
515 memset(dest, 'X', sizeof(dest));
516 dest[0] = '\0';
517 ret = pstrcat_s(dest, sizeof(dest), small);
518 ok(ret == 0, "strcat_s: Copying a string into a big enough destination returned %d, expected 0\n", ret);
519 ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == '\0'&&
520 dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
521 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
522 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
523 ret = pstrcat_s(dest, sizeof(dest), small);
524 ok(ret == 0, "strcat_s: Attaching a string to a big enough destination returned %d, expected 0\n", ret);
525 ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == 's' &&
526 dest[4] == 'm' && dest[5] == 'a' && dest[6] == '\0'&& dest[7] == 'X',
527 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
528 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
529
530 ret = pstrcat_s(dest, sizeof(dest), small);
531 ok(ret == ERANGE, "strcat_s: Attaching a string to a filled up destination returned %d, expected ERANGE\n", ret);
532 ok(dest[0] == '\0'&& dest[1] == 'm' && dest[2] == 'a' && dest[3] == 's' &&
533 dest[4] == 'm' && dest[5] == 'a' && dest[6] == 's' && dest[7] == 'm',
534 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
535 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
536
537 memset(dest, 'X', sizeof(dest));
538 dest[0] = 'a';
539 dest[1] = '\0';
540
541 ret = pstrcat_s(dest, 0, small);
542 ok(ret == EINVAL, "strcat_s: Source len = 0 returned %d, expected EINVAL\n", ret);
543 ok(dest[0] == 'a' && dest[1] == '\0'&& dest[2] == 'X' && dest[3] == 'X' &&
544 dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
545 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
546 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
547
548 ret = pstrcat_s(dest, 0, NULL);
549 ok(ret == EINVAL, "strcat_s: len = 0 and src = NULL returned %d, expected EINVAL\n", ret);
550 ok(dest[0] == 'a' && dest[1] == '\0'&& dest[2] == 'X' && dest[3] == 'X' &&
551 dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
552 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
553 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
554
555 ret = pstrcat_s(dest, sizeof(dest), NULL);
556 ok(ret == EINVAL, "strcat_s: Sourcing from NULL returned %d, expected EINVAL\n", ret);
557 ok(dest[0] == '\0'&& dest[1] == '\0'&& dest[2] == 'X' && dest[3] == 'X' &&
558 dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
559 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
560 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
561
562 ret = pstrcat_s(NULL, sizeof(dest), small);
563 ok(ret == EINVAL, "strcat_s: Writing to a NULL string returned %d, expected EINVAL\n", ret);
564 }
565
566 static void test__mbsnbcpy_s(void)
567 {
568 unsigned char dest[8];
569 const unsigned char big[] = "atoolongstringforthislittledestination";
570 const unsigned char small[] = "small";
571 int ret;
572
573 if(!p_mbsnbcpy_s)
574 {
575 skip("_mbsnbcpy_s not found\n");
576 return;
577 }
578
579 memset(dest, 'X', sizeof(dest));
580 ret = p_mbsnbcpy_s(dest, sizeof(dest), small, sizeof(small));
581 ok(ret == 0, "_mbsnbcpy_s: Copying a string into a big enough destination returned %d, expected 0\n", ret);
582 ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == 'l' &&
583 dest[4] == 'l' && dest[5] == '\0'&& dest[6] == 'X' && dest[7] == 'X',
584 "Unexpected return data from _mbsnbcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
585 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
586
587 /* WTF? */
588 memset(dest, 'X', sizeof(dest));
589 ret = p_mbsnbcpy_s(dest, sizeof(dest) - 2, big, sizeof(small));
590 ok(ret == ERANGE, "_mbsnbcpy_s: Copying a too long string returned %d, expected ERANGE\n", ret);
591 ok(dest[0] == '\0'&& dest[1] == 't' && dest[2] == 'o' && dest[3] == 'o' &&
592 dest[4] == 'l' && dest[5] == 'o' && dest[6] == 'X' && dest[7] == 'X',
593 "Unexpected return data from _mbsnbcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
594 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
595
596 memset(dest, 'X', sizeof(dest));
597 ret = p_mbsnbcpy_s(dest, sizeof(dest) - 2, big, 4);
598 ok(ret == 0, "_mbsnbcpy_s: Copying a too long string with a count cap returned %d, expected 0\n", ret);
599 ok(dest[0] == 'a' && dest[1] == 't' && dest[2] == 'o' && dest[3] == 'o' &&
600 dest[4] == '\0'&& dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
601 "Unexpected return data from _mbsnbcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
602 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
603
604 memset(dest, 'X', sizeof(dest));
605 ret = p_mbsnbcpy_s(dest, sizeof(dest) - 2, small, sizeof(small) + 10);
606 ok(ret == 0, "_mbsnbcpy_s: Copying more data than the source string len returned %d, expected 0\n", ret);
607 ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == 'l' &&
608 dest[4] == 'l' && dest[5] == '\0'&& dest[6] == 'X' && dest[7] == 'X',
609 "Unexpected return data from _mbsnbcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
610 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
611 }
612
613 static void test_wcscpy_s(void)
614 {
615 static const WCHAR szLongText[] = { 'T','h','i','s','A','L','o','n','g','s','t','r','i','n','g',0 };
616 static WCHAR szDest[18];
617 static WCHAR szDestShort[8];
618 int ret;
619
620 if(!p_wcscpy_s)
621 {
622 win_skip("wcscpy_s not found\n");
623 return;
624 }
625
626 /* Test NULL Dest */
627 ret = p_wcscpy_s(NULL, 18, szLongText);
628 ok(ret == EINVAL, "p_wcscpy_s expect EINVAL got %d\n", ret);
629
630 /* Test NULL Source */
631 szDest[0] = 'A';
632 ret = p_wcscpy_s(szDest, 18, NULL);
633 ok(ret == EINVAL, "expected EINVAL got %d\n", ret);
634 ok(szDest[0] == 0, "szDest[0] not 0\n");
635
636 /* Test invalid size */
637 szDest[0] = 'A';
638 ret = p_wcscpy_s(szDest, 0, szLongText);
639 /* Later versions changed the return value for this case to EINVAL,
640 * and don't modify the result if the dest size is 0.
641 */
642 ok(ret == ERANGE || ret == EINVAL, "expected ERANGE/EINVAL got %d\n", ret);
643 ok(szDest[0] == 0 || ret == EINVAL, "szDest[0] not 0\n");
644
645 /* Copy same buffer size */
646 ret = p_wcscpy_s(szDest, 18, szLongText);
647 ok(ret == 0, "expected 0 got %d\n", ret);
648 ok(lstrcmpW(szDest, szLongText) == 0, "szDest != szLongText\n");
649
650 /* Copy smaller buffer size */
651 szDest[0] = 'A';
652 ret = p_wcscpy_s(szDestShort, 8, szLongText);
653 ok(ret == ERANGE || ret == EINVAL, "expected ERANGE/EINVAL got %d\n", ret);
654 ok(szDestShort[0] == 0, "szDestShort[0] not 0\n");
655
656 if(!p_wcsncpy_s)
657 {
658 win_skip("wcsncpy_s not found\n");
659 return;
660 }
661
662 ret = p_wcsncpy_s(NULL, 18, szLongText, sizeof(szLongText)/sizeof(WCHAR));
663 ok(ret == EINVAL, "p_wcsncpy_s expect EINVAL got %d\n", ret);
664
665 szDest[0] = 'A';
666 ret = p_wcsncpy_s(szDest, 18, NULL, 1);
667 ok(ret == EINVAL, "expected EINVAL got %d\n", ret);
668 ok(szDest[0] == 0, "szDest[0] not 0\n");
669
670 szDest[0] = 'A';
671 ret = p_wcsncpy_s(szDest, 18, NULL, 0);
672 ok(ret == 0, "expected ERROR_SUCCESS got %d\n", ret);
673 ok(szDest[0] == 0, "szDest[0] not 0\n");
674
675 szDest[0] = 'A';
676 ret = p_wcsncpy_s(szDest, 0, szLongText, sizeof(szLongText)/sizeof(WCHAR));
677 ok(ret == ERANGE || ret == EINVAL, "expected ERANGE/EINVAL got %d\n", ret);
678 ok(szDest[0] == 0 || ret == EINVAL, "szDest[0] not 0\n");
679
680 ret = p_wcsncpy_s(szDest, 18, szLongText, sizeof(szLongText)/sizeof(WCHAR));
681 ok(ret == 0, "expected 0 got %d\n", ret);
682 ok(lstrcmpW(szDest, szLongText) == 0, "szDest != szLongText\n");
683
684 szDest[0] = 'A';
685 ret = p_wcsncpy_s(szDestShort, 8, szLongText, sizeof(szLongText)/sizeof(WCHAR));
686 ok(ret == ERANGE || ret == EINVAL, "expected ERANGE/EINVAL got %d\n", ret);
687 ok(szDestShort[0] == 0, "szDestShort[0] not 0\n");
688 }
689
690 static void test__wcsupr_s(void)
691 {
692 static const WCHAR mixedString[] = {'M', 'i', 'X', 'e', 'D', 'l', 'o', 'w',
693 'e', 'r', 'U', 'P', 'P', 'E', 'R', 0};
694 static const WCHAR expectedString[] = {'M', 'I', 'X', 'E', 'D', 'L', 'O',
695 'W', 'E', 'R', 'U', 'P', 'P', 'E',
696 'R', 0};
697 WCHAR testBuffer[2*sizeof(mixedString)/sizeof(WCHAR)];
698 int ret;
699
700 if (!p_wcsupr_s)
701 {
702 win_skip("_wcsupr_s not found\n");
703 return;
704 }
705
706 /* Test NULL input string and invalid size. */
707 errno = EBADF;
708 ret = p_wcsupr_s(NULL, 0);
709 ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
710 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
711
712 /* Test NULL input string and valid size. */
713 errno = EBADF;
714 ret = p_wcsupr_s(NULL, sizeof(testBuffer)/sizeof(WCHAR));
715 ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
716 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
717
718 /* Test empty string with zero size. */
719 errno = EBADF;
720 testBuffer[0] = '\0';
721 ret = p_wcsupr_s(testBuffer, 0);
722 ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
723 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
724 ok(testBuffer[0] == '\0', "Expected the buffer to be unchanged\n");
725
726 /* Test empty string with size of one. */
727 testBuffer[0] = '\0';
728 ret = p_wcsupr_s(testBuffer, 1);
729 ok(ret == 0, "Expected _wcsupr_s to succeed, got %d\n", ret);
730 ok(testBuffer[0] == '\0', "Expected the buffer to be unchanged\n");
731
732 /* Test one-byte buffer with zero size. */
733 errno = EBADF;
734 testBuffer[0] = 'x';
735 ret = p_wcsupr_s(testBuffer, 0);
736 ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
737 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
738 ok(testBuffer[0] == '\0', "Expected the first buffer character to be null\n");
739
740 /* Test one-byte buffer with size of one. */
741 errno = EBADF;
742 testBuffer[0] = 'x';
743 ret = p_wcsupr_s(testBuffer, 1);
744 ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
745 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
746 ok(testBuffer[0] == '\0', "Expected the first buffer character to be null\n");
747
748 /* Test invalid size. */
749 wcscpy(testBuffer, mixedString);
750 errno = EBADF;
751 ret = p_wcsupr_s(testBuffer, 0);
752 ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
753 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
754 ok(testBuffer[0] == '\0', "Expected the first buffer character to be null\n");
755
756 /* Test normal string uppercasing. */
757 wcscpy(testBuffer, mixedString);
758 ret = p_wcsupr_s(testBuffer, sizeof(mixedString)/sizeof(WCHAR));
759 ok(ret == 0, "Expected _wcsupr_s to succeed, got %d\n", ret);
760 ok(!wcscmp(testBuffer, expectedString), "Expected the string to be fully upper-case\n");
761
762 /* Test uppercasing with a shorter buffer size count. */
763 wcscpy(testBuffer, mixedString);
764 errno = EBADF;
765 ret = p_wcsupr_s(testBuffer, sizeof(mixedString)/sizeof(WCHAR) - 1);
766 ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
767 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
768 ok(testBuffer[0] == '\0', "Expected the first buffer character to be null\n");
769
770 /* Test uppercasing with a longer buffer size count. */
771 wcscpy(testBuffer, mixedString);
772 ret = p_wcsupr_s(testBuffer, sizeof(testBuffer)/sizeof(WCHAR));
773 ok(ret == 0, "Expected _wcsupr_s to succeed, got %d\n", ret);
774 ok(!wcscmp(testBuffer, expectedString), "Expected the string to be fully upper-case\n");
775 }
776
777 static void test__wcslwr_s(void)
778 {
779 static const WCHAR mixedString[] = {'M', 'i', 'X', 'e', 'D', 'l', 'o', 'w',
780 'e', 'r', 'U', 'P', 'P', 'E', 'R', 0};
781 static const WCHAR expectedString[] = {'m', 'i', 'x', 'e', 'd', 'l', 'o',
782 'w', 'e', 'r', 'u', 'p', 'p', 'e',
783 'r', 0};
784 WCHAR buffer[2*sizeof(mixedString)/sizeof(WCHAR)];
785 int ret;
786
787 if (!p_wcslwr_s)
788 {
789 win_skip("_wcslwr_s not found\n");
790 return;
791 }
792
793 /* Test NULL input string and invalid size. */
794 errno = EBADF;
795 ret = p_wcslwr_s(NULL, 0);
796 ok(ret == EINVAL, "expected EINVAL, got %d\n", ret);
797 ok(errno == EINVAL, "expected errno EINVAL, got %d\n", errno);
798
799 /* Test NULL input string and valid size. */
800 errno = EBADF;
801 ret = p_wcslwr_s(NULL, sizeof(buffer)/sizeof(wchar_t));
802 ok(ret == EINVAL, "expected EINVAL, got %d\n", ret);
803 ok(errno == EINVAL, "expected errno EINVAL, got %d\n", errno);
804
805 /* Test empty string with zero size. */
806 errno = EBADF;
807 buffer[0] = 'a';
808 ret = p_wcslwr_s(buffer, 0);
809 ok(ret == EINVAL, "expected EINVAL, got %d\n", ret);
810 ok(errno == EINVAL, "expected errno EINVAL, got %d\n", errno);
811 ok(buffer[0] == 0, "expected empty string\n");
812
813 /* Test empty string with size of one. */
814 buffer[0] = 0;
815 ret = p_wcslwr_s(buffer, 1);
816 ok(ret == 0, "got %d\n", ret);
817 ok(buffer[0] == 0, "expected buffer to be unchanged\n");
818
819 /* Test one-byte buffer with zero size. */
820 errno = EBADF;
821 buffer[0] = 'x';
822 ret = p_wcslwr_s(buffer, 0);
823 ok(ret == EINVAL, "expected EINVAL, got %d\n", ret);
824 ok(errno == EINVAL, "expected errno to be EINVAL, got %d\n", errno);
825 ok(buffer[0] == '\0', "expected empty string\n");
826
827 /* Test one-byte buffer with size of one. */
828 errno = EBADF;
829 buffer[0] = 'x';
830 ret = p_wcslwr_s(buffer, 1);
831 ok(ret == EINVAL, "expected EINVAL, got %d\n", ret);
832 ok(errno == EINVAL, "expected errno to be EINVAL, got %d\n", errno);
833 ok(buffer[0] == '\0', "expected empty string\n");
834
835 /* Test invalid size. */
836 wcscpy(buffer, mixedString);
837 errno = EBADF;
838 ret = p_wcslwr_s(buffer, 0);
839 ok(ret == EINVAL, "Expected EINVAL, got %d\n", ret);
840 ok(errno == EINVAL, "expected errno to be EINVAL, got %d\n", errno);
841 ok(buffer[0] == '\0', "expected empty string\n");
842
843 /* Test normal string uppercasing. */
844 wcscpy(buffer, mixedString);
845 ret = p_wcslwr_s(buffer, sizeof(mixedString)/sizeof(WCHAR));
846 ok(ret == 0, "expected 0, got %d\n", ret);
847 ok(!wcscmp(buffer, expectedString), "expected lowercase\n");
848
849 /* Test uppercasing with a shorter buffer size count. */
850 wcscpy(buffer, mixedString);
851 errno = EBADF;
852 ret = p_wcslwr_s(buffer, sizeof(mixedString)/sizeof(WCHAR) - 1);
853 ok(ret == EINVAL, "expected EINVAL, got %d\n", ret);
854 ok(errno == EINVAL, "expected errno to be EINVAL, got %d\n", errno);
855 ok(buffer[0] == '\0', "expected empty string\n");
856
857 /* Test uppercasing with a longer buffer size count. */
858 wcscpy(buffer, mixedString);
859 ret = p_wcslwr_s(buffer, sizeof(buffer)/sizeof(WCHAR));
860 ok(ret == 0, "expected 0, got %d\n", ret);
861 ok(!wcscmp(buffer, expectedString), "expected lowercase\n");
862 }
863
864 static void test_mbcjisjms(void)
865 {
866 /* List of value-pairs to test. The test assumes the last pair to be {0, ..} */
867 unsigned int jisjms[][2] = { {0x2020, 0}, {0x2021, 0}, {0x2120, 0}, {0x2121, 0x8140},
868 {0x7f7f, 0}, {0x7f7e, 0}, {0x7e7f, 0}, {0x7e7e, 0xeffc},
869 {0x2121FFFF, 0}, {0x2223, 0x81a1}, {0x237e, 0x829e}, {0, 0}};
870 unsigned int ret, exp, i;
871
872 i = 0;
873 do
874 {
875 ret = _mbcjistojms(jisjms[i][0]);
876
877 if(_getmbcp() == 932) /* Japanese codepage? */
878 exp = jisjms[i][1];
879 else
880 exp = jisjms[i][0]; /* If not, no conversion */
881
882 ok(ret == exp, "Expected 0x%x, got 0x%x\n", exp, ret);
883 } while(jisjms[i++][0] != 0);
884 }
885
886 static void test_mbctombb(void)
887 {
888 static const unsigned int mbcmbb_932[][2] = {
889 {0x829e, 0x829e}, {0x829f, 0xa7}, {0x82f1, 0xdd}, {0x82f2, 0x82f2},
890 {0x833f, 0x833f}, {0x8340, 0xa7}, {0x837e, 0xd0}, {0x837f, 0x837f},
891 {0x8380, 0xd1}, {0x8396, 0xb9}, {0x8397, 0x8397}, {0x813f, 0x813f},
892 {0x8140, 0x20}, {0x814c, 0x814c}, {0x814f, 0x5e}, {0x8197, 0x40},
893 {0x8198, 0x8198}, {0x8258, 0x39}, {0x8259, 0x8259}, {0x825f, 0x825f},
894 {0x8260, 0x41}, {0x82f1, 0xdd}, {0x82f2, 0x82f2}, {0,0}};
895 unsigned int exp, ret, i;
896 unsigned int prev_cp = _getmbcp();
897
898 _setmbcp(932);
899 for (i = 0; mbcmbb_932[i][0] != 0; i++)
900 {
901 ret = _mbctombb(mbcmbb_932[i][0]);
902 exp = mbcmbb_932[i][1];
903 ok(ret == exp, "Expected 0x%x, got 0x%x\n", exp, ret);
904 }
905 _setmbcp(prev_cp);
906 }
907
908 static void test_ismbclegal(void) {
909 unsigned int prev_cp = _getmbcp();
910 int ret, exp, err;
911 unsigned int i;
912
913 _setmbcp(932); /* Japanese */
914 err = 0;
915 for(i = 0; i < 0x10000; i++) {
916 ret = _ismbclegal(i);
917 exp = ((HIBYTE(i) >= 0x81 && HIBYTE(i) <= 0x9F) ||
918 (HIBYTE(i) >= 0xE0 && HIBYTE(i) <= 0xFC)) &&
919 ((LOBYTE(i) >= 0x40 && LOBYTE(i) <= 0x7E) ||
920 (LOBYTE(i) >= 0x80 && LOBYTE(i) <= 0xFC));
921 if(ret != exp) {
922 err = 1;
923 break;
924 }
925 }
926 ok(!err, "_ismbclegal (932) : Expected 0x%x, got 0x%x (0x%x)\n", exp, ret, i);
927 _setmbcp(936); /* Chinese (GBK) */
928 err = 0;
929 for(i = 0; i < 0x10000; i++) {
930 ret = _ismbclegal(i);
931 exp = HIBYTE(i) >= 0x81 && HIBYTE(i) <= 0xFE &&
932 LOBYTE(i) >= 0x40 && LOBYTE(i) <= 0xFE;
933 if(ret != exp) {
934 err = 1;
935 break;
936 }
937 }
938 ok(!err, "_ismbclegal (936) : Expected 0x%x, got 0x%x (0x%x)\n", exp, ret, i);
939 _setmbcp(949); /* Korean */
940 err = 0;
941 for(i = 0; i < 0x10000; i++) {
942 ret = _ismbclegal(i);
943 exp = HIBYTE(i) >= 0x81 && HIBYTE(i) <= 0xFE &&
944 LOBYTE(i) >= 0x41 && LOBYTE(i) <= 0xFE;
945 if(ret != exp) {
946 err = 1;
947 break;
948 }
949 }
950 ok(!err, "_ismbclegal (949) : Expected 0x%x, got 0x%x (0x%x)\n", exp, ret, i);
951 _setmbcp(950); /* Chinese (Big5) */
952 err = 0;
953 for(i = 0; i < 0x10000; i++) {
954 ret = _ismbclegal(i);
955 exp = HIBYTE(i) >= 0x81 && HIBYTE(i) <= 0xFE &&
956 ((LOBYTE(i) >= 0x40 && LOBYTE(i) <= 0x7E) ||
957 (LOBYTE(i) >= 0xA1 && LOBYTE(i) <= 0xFE));
958 if(ret != exp) {
959 err = 1;
960 break;
961 }
962 }
963 ok(!err, "_ismbclegal (950) : Expected 0x%x, got 0x%x (0x%x)\n", exp, ret, i);
964 _setmbcp(1361); /* Korean (Johab) */
965 err = 0;
966 for(i = 0; i < 0x10000; i++) {
967 ret = _ismbclegal(i);
968 exp = ((HIBYTE(i) >= 0x81 && HIBYTE(i) <= 0xD3) ||
969 (HIBYTE(i) >= 0xD8 && HIBYTE(i) <= 0xF9)) &&
970 ((LOBYTE(i) >= 0x31 && LOBYTE(i) <= 0x7E) ||
971 (LOBYTE(i) >= 0x81 && LOBYTE(i) <= 0xFE)) &&
972 HIBYTE(i) != 0xDF;
973 if(ret != exp) {
974 err = 1;
975 break;
976 }
977 }
978 todo_wine ok(!err, "_ismbclegal (1361) : Expected 0x%x, got 0x%x (0x%x)\n", exp, ret, i);
979
980 _setmbcp(prev_cp);
981 }
982
983 static const struct {
984 const char* string;
985 const char* delimiter;
986 int exp_offsetret1; /* returned offset from string after first call to strtok()
987 -1 means NULL */
988 int exp_offsetret2; /* returned offset from string after second call to strtok()
989 -1 means NULL */
990 int exp_offsetret3; /* returned offset from string after third call to strtok()
991 -1 means NULL */
992 } testcases_strtok[] = {
993 { "red cabernet", " ", 0, 4, -1 },
994 { "sparkling white riesling", " ", 0, 10, 16 },
995 { " pale cream sherry", "e ", 1, 6, 9 },
996 /* end mark */
997 { 0}
998 };
999
1000 static void test_strtok(void)
1001 {
1002 int i;
1003 char *strret;
1004 char teststr[100];
1005 for( i = 0; testcases_strtok[i].string; i++){
1006 strcpy( teststr, testcases_strtok[i].string);
1007 strret = strtok( teststr, testcases_strtok[i].delimiter);
1008 ok( (int)(strret - teststr) == testcases_strtok[i].exp_offsetret1 ||
1009 (!strret && testcases_strtok[i].exp_offsetret1 == -1),
1010 "string (%p) \'%s\' return %p\n",
1011 teststr, testcases_strtok[i].string, strret);
1012 if( !strret) continue;
1013 strret = strtok( NULL, testcases_strtok[i].delimiter);
1014 ok( (int)(strret - teststr) == testcases_strtok[i].exp_offsetret2 ||
1015 (!strret && testcases_strtok[i].exp_offsetret2 == -1),
1016 "second call string (%p) \'%s\' return %p\n",
1017 teststr, testcases_strtok[i].string, strret);
1018 if( !strret) continue;
1019 strret = strtok( NULL, testcases_strtok[i].delimiter);
1020 ok( (int)(strret - teststr) == testcases_strtok[i].exp_offsetret3 ||
1021 (!strret && testcases_strtok[i].exp_offsetret3 == -1),
1022 "third call string (%p) \'%s\' return %p\n",
1023 teststr, testcases_strtok[i].string, strret);
1024 }
1025 }
1026
1027 static void test_strtol(void)
1028 {
1029 char* e;
1030 LONG l;
1031 ULONG ul;
1032
1033 /* errno is only set in case of error, so reset errno to EBADF to check for errno modification */
1034 /* errno is modified on W2K8+ */
1035 errno = EBADF;
1036 l = strtol("-1234", &e, 0);
1037 ok(l==-1234, "wrong value %d\n", l);
1038 ok(errno == EBADF || broken(errno == 0), "wrong errno %d\n", errno);
1039 errno = EBADF;
1040 ul = strtoul("1234", &e, 0);
1041 ok(ul==1234, "wrong value %u\n", ul);
1042 ok(errno == EBADF || broken(errno == 0), "wrong errno %d\n", errno);
1043
1044 errno = EBADF;
1045 l = strtol("2147483647L", &e, 0);
1046 ok(l==2147483647, "wrong value %d\n", l);
1047 ok(errno == EBADF || broken(errno == 0), "wrong errno %d\n", errno);
1048 errno = EBADF;
1049 l = strtol("-2147483648L", &e, 0);
1050 ok(l==-2147483647L - 1, "wrong value %d\n", l);
1051 ok(errno == EBADF || broken(errno == 0), "wrong errno %d\n", errno);
1052 errno = EBADF;
1053 ul = strtoul("4294967295UL", &e, 0);
1054 ok(ul==4294967295ul, "wrong value %u\n", ul);
1055 ok(errno == EBADF || broken(errno == 0), "wrong errno %d\n", errno);
1056
1057 errno = 0;
1058 l = strtol("9223372036854775807L", &e, 0);
1059 ok(l==2147483647, "wrong value %d\n", l);
1060 ok(errno == ERANGE, "wrong errno %d\n", errno);
1061 errno = 0;
1062 ul = strtoul("9223372036854775807L", &e, 0);
1063 ok(ul==4294967295ul, "wrong value %u\n", ul);
1064 ok(errno == ERANGE, "wrong errno %d\n", errno);
1065 }
1066
1067 static void test_strnlen(void)
1068 {
1069 static const char str[] = "string";
1070 size_t res;
1071
1072 if(!p_strnlen) {
1073 win_skip("strnlen not found\n");
1074 return;
1075 }
1076
1077 res = p_strnlen(str, 20);
1078 ok(res == 6, "Returned length = %d\n", (int)res);
1079
1080 res = p_strnlen(str, 3);
1081 ok(res == 3, "Returned length = %d\n", (int)res);
1082
1083 res = p_strnlen(NULL, 0);
1084 ok(res == 0, "Returned length = %d\n", (int)res);
1085 }
1086
1087 static void test__strtoi64(void)
1088 {
1089 static const char no1[] = "31923";
1090 static const char no2[] = "-213312";
1091 static const char no3[] = "12aa";
1092 static const char no4[] = "abc12";
1093 static const char overflow[] = "99999999999999999999";
1094 static const char neg_overflow[] = "-99999999999999999999";
1095 static const char hex[] = "0x123";
1096 static const char oct[] = "000123";
1097 static const char blanks[] = " 12 212.31";
1098
1099 __int64 res;
1100 unsigned __int64 ures;
1101 char *endpos;
1102
1103 if(!p_strtoi64 || !p_strtoui64) {
1104 win_skip("_strtoi64 or _strtoui64 not found\n");
1105 return;
1106 }
1107
1108 errno = 0xdeadbeef;
1109 res = p_strtoi64(no1, NULL, 10);
1110 ok(res == 31923, "res != 31923\n");
1111 res = p_strtoi64(no2, NULL, 10);
1112 ok(res == -213312, "res != -213312\n");
1113 res = p_strtoi64(no3, NULL, 10);
1114 ok(res == 12, "res != 12\n");
1115 res = p_strtoi64(no4, &endpos, 10);
1116 ok(res == 0, "res != 0\n");
1117 ok(endpos == no4, "Scanning was not stopped on first character\n");
1118 res = p_strtoi64(hex, &endpos, 10);
1119 ok(res == 0, "res != 0\n");
1120 ok(endpos == hex+1, "Incorrect endpos (%p-%p)\n", hex, endpos);
1121 res = p_strtoi64(oct, &endpos, 10);
1122 ok(res == 123, "res != 123\n");
1123 ok(endpos == oct+strlen(oct), "Incorrect endpos (%p-%p)\n", oct, endpos);
1124 res = p_strtoi64(blanks, &endpos, 10);
1125 ok(res == 12, "res != 12\n");
1126 ok(endpos == blanks+10, "Incorrect endpos (%p-%p)\n", blanks, endpos);
1127 ok(errno == 0xdeadbeef, "errno = %x\n", errno);
1128
1129 errno = 0xdeadbeef;
1130 res = p_strtoi64(overflow, &endpos, 10);
1131 ok(res == _I64_MAX, "res != _I64_MAX\n");
1132 ok(endpos == overflow+strlen(overflow), "Incorrect endpos (%p-%p)\n", overflow, endpos);
1133 ok(errno == ERANGE, "errno = %x\n", errno);
1134
1135 errno = 0xdeadbeef;
1136 res = p_strtoi64(neg_overflow, &endpos, 10);
1137 ok(res == _I64_MIN, "res != _I64_MIN\n");
1138 ok(endpos == neg_overflow+strlen(neg_overflow), "Incorrect endpos (%p-%p)\n", neg_overflow, endpos);
1139 ok(errno == ERANGE, "errno = %x\n", errno);
1140
1141 errno = 0xdeadbeef;
1142 res = p_strtoi64(no1, &endpos, 16);
1143 ok(res == 203043, "res != 203043\n");
1144 ok(endpos == no1+strlen(no1), "Incorrect endpos (%p-%p)\n", no1, endpos);
1145 res = p_strtoi64(no2, &endpos, 16);
1146 ok(res == -2175762, "res != -2175762\n");
1147 ok(endpos == no2+strlen(no2), "Incorrect endpos (%p-%p)\n", no2, endpos);
1148 res = p_strtoi64(no3, &endpos, 16);
1149 ok(res == 4778, "res != 4778\n");
1150 ok(endpos == no3+strlen(no3), "Incorrect endpos (%p-%p)\n", no3, endpos);
1151 res = p_strtoi64(no4, &endpos, 16);
1152 ok(res == 703506, "res != 703506\n");
1153 ok(endpos == no4+strlen(no4), "Incorrect endpos (%p-%p)\n", no4, endpos);
1154 res = p_strtoi64(hex, &endpos, 16);
1155 ok(res == 291, "res != 291\n");
1156 ok(endpos == hex+strlen(hex), "Incorrect endpos (%p-%p)\n", hex, endpos);
1157 res = p_strtoi64(oct, &endpos, 16);
1158 ok(res == 291, "res != 291\n");
1159 ok(endpos == oct+strlen(oct), "Incorrect endpos (%p-%p)\n", oct, endpos);
1160 res = p_strtoi64(blanks, &endpos, 16);
1161 ok(res == 18, "res != 18\n");
1162 ok(endpos == blanks+10, "Incorrect endpos (%p-%p)\n", blanks, endpos);
1163 ok(errno == 0xdeadbeef, "errno = %x\n", errno);
1164
1165 errno = 0xdeadbeef;
1166 res = p_strtoi64(hex, &endpos, 36);
1167 ok(res == 1541019, "res != 1541019\n");
1168 ok(endpos == hex+strlen(hex), "Incorrect endpos (%p-%p)\n", hex, endpos);
1169 ok(errno == 0xdeadbeef, "errno = %x\n", errno);
1170
1171 errno = 0xdeadbeef;
1172 res = p_strtoi64(no1, &endpos, 0);
1173 ok(res == 31923, "res != 31923\n");
1174 ok(endpos == no1+strlen(no1), "Incorrect endpos (%p-%p)\n", no1, endpos);
1175 res = p_strtoi64(no2, &endpos, 0);
1176 ok(res == -213312, "res != -213312\n");
1177 ok(endpos == no2+strlen(no2), "Incorrect endpos (%p-%p)\n", no2, endpos);
1178 res = p_strtoi64(no3, &endpos, 10);
1179 ok(res == 12, "res != 12\n");
1180 ok(endpos == no3+2, "Incorrect endpos (%p-%p)\n", no3, endpos);
1181 res = p_strtoi64(no4, &endpos, 10);
1182 ok(res == 0, "res != 0\n");
1183 ok(endpos == no4, "Incorrect endpos (%p-%p)\n", no4, endpos);
1184 res = p_strtoi64(hex, &endpos, 10);
1185 ok(res == 0, "res != 0\n");
1186 ok(endpos == hex+1, "Incorrect endpos (%p-%p)\n", hex, endpos);
1187 res = p_strtoi64(oct, &endpos, 10);
1188 ok(res == 123, "res != 123\n");
1189 ok(endpos == oct+strlen(oct), "Incorrect endpos (%p-%p)\n", oct, endpos);
1190 res = p_strtoi64(blanks, &endpos, 10);
1191 ok(res == 12, "res != 12\n");
1192 ok(endpos == blanks+10, "Incorrect endpos (%p-%p)\n", blanks, endpos);
1193 ok(errno == 0xdeadbeef, "errno = %x\n", errno);
1194
1195 errno = 0xdeadbeef;
1196 ures = p_strtoui64(no1, &endpos, 0);
1197 ok(ures == 31923, "ures != 31923\n");
1198 ok(endpos == no1+strlen(no1), "Incorrect endpos (%p-%p)\n", no1, endpos);
1199 ures = p_strtoui64(no2, &endpos, 0);
1200 ok(ures == -213312, "ures != -213312\n");
1201 ok(endpos == no2+strlen(no2), "Incorrect endpos (%p-%p)\n", no2, endpos);
1202 ures = p_strtoui64(no3, &endpos, 10);
1203 ok(ures == 12, "ures != 12\n");
1204 ok(endpos == no3+2, "Incorrect endpos (%p-%p)\n", no3, endpos);
1205 ures = p_strtoui64(no4, &endpos, 10);
1206 ok(ures == 0, "ures != 0\n");
1207 ok(endpos == no4, "Incorrect endpos (%p-%p)\n", no4, endpos);
1208 ures = p_strtoui64(hex, &endpos, 10);
1209 ok(ures == 0, "ures != 0\n");
1210 ok(endpos == hex+1, "Incorrect endpos (%p-%p)\n", hex, endpos);
1211 ures = p_strtoui64(oct, &endpos, 10);
1212 ok(ures == 123, "ures != 123\n");
1213 ok(endpos == oct+strlen(oct), "Incorrect endpos (%p-%p)\n", oct, endpos);
1214 ures = p_strtoui64(blanks, &endpos, 10);
1215 ok(ures == 12, "ures != 12\n");
1216 ok(endpos == blanks+10, "Incorrect endpos (%p-%p)\n", blanks, endpos);
1217 ok(errno == 0xdeadbeef, "errno = %x\n", errno);
1218
1219 errno = 0xdeadbeef;
1220 ures = p_strtoui64(overflow, &endpos, 10);
1221 ok(ures == _UI64_MAX, "ures != _UI64_MAX\n");
1222 ok(endpos == overflow+strlen(overflow), "Incorrect endpos (%p-%p)\n", overflow, endpos);
1223 ok(errno == ERANGE, "errno = %x\n", errno);
1224
1225 errno = 0xdeadbeef;
1226 ures = p_strtoui64(neg_overflow, &endpos, 10);
1227 ok(ures == 1, "ures != 1\n");
1228 ok(endpos == neg_overflow+strlen(neg_overflow), "Incorrect endpos (%p-%p)\n", neg_overflow, endpos);
1229 ok(errno == ERANGE, "errno = %x\n", errno);
1230 }
1231
1232 static inline BOOL almost_equal(double d1, double d2) {
1233 if(d1-d2>-1e-30 && d1-d2<1e-30)
1234 return TRUE;
1235 return FALSE;
1236 }
1237
1238 static void test__strtod(void)
1239 {
1240 const char double1[] = "12.1";
1241 const char double2[] = "-13.721";
1242 const char double3[] = "INF";
1243 const char double4[] = ".21e12";
1244 const char double5[] = "214353e-3";
1245 const char overflow[] = "1d9999999999999999999";
1246 const char white_chars[] = " d10";
1247
1248 char *end;
1249 double d;
1250
1251 d = strtod(double1, &end);
1252 ok(almost_equal(d, 12.1), "d = %lf\n", d);
1253 ok(end == double1+4, "incorrect end (%d)\n", (int)(end-double1));
1254
1255 d = strtod(double2, &end);
1256 ok(almost_equal(d, -13.721), "d = %lf\n", d);
1257 ok(end == double2+7, "incorrect end (%d)\n", (int)(end-double2));
1258
1259 d = strtod(double3, &end);
1260 ok(almost_equal(d, 0), "d = %lf\n", d);
1261 ok(end == double3, "incorrect end (%d)\n", (int)(end-double3));
1262
1263 d = strtod(double4, &end);
1264 ok(almost_equal(d, 210000000000.0), "d = %lf\n", d);
1265 ok(end == double4+6, "incorrect end (%d)\n", (int)(end-double4));
1266
1267 d = strtod(double5, &end);
1268 ok(almost_equal(d, 214.353), "d = %lf\n", d);
1269 ok(end == double5+9, "incorrect end (%d)\n", (int)(end-double5));
1270
1271 d = strtod("12.1d2", NULL);
1272 ok(almost_equal(d, 12.1e2), "d = %lf\n", d);
1273
1274 d = strtod(white_chars, &end);
1275 ok(almost_equal(d, 0), "d = %lf\n", d);
1276 ok(end == white_chars, "incorrect end (%d)\n", (int)(end-white_chars));
1277
1278 /* Set locale with non '.' decimal point (',') */
1279 if(!setlocale(LC_ALL, "Polish")) {
1280 win_skip("system with limited locales\n");
1281 return;
1282 }
1283
1284 d = strtod("12.1", NULL);
1285 ok(almost_equal(d, 12.0), "d = %lf\n", d);
1286
1287 d = strtod("12,1", NULL);
1288 ok(almost_equal(d, 12.1), "d = %lf\n", d);
1289
1290 setlocale(LC_ALL, "C");
1291
1292 /* Precision tests */
1293 d = strtod("0.1", NULL);
1294 ok(almost_equal(d, 0.1), "d = %lf\n", d);
1295 d = strtod("-0.1", NULL);
1296 ok(almost_equal(d, -0.1), "d = %lf\n", d);
1297 d = strtod("0.1281832188491894198128921", NULL);
1298 ok(almost_equal(d, 0.1281832188491894198128921), "d = %lf\n", d);
1299 d = strtod("0.82181281288121", NULL);
1300 ok(almost_equal(d, 0.82181281288121), "d = %lf\n", d);
1301 d = strtod("21921922352523587651128218821", NULL);
1302 ok(almost_equal(d, 21921922352523587651128218821.0), "d = %lf\n", d);
1303 d = strtod("0.1d238", NULL);
1304 ok(almost_equal(d, 0.1e238L), "d = %lf\n", d);
1305 d = strtod("0.1D-4736", NULL);
1306 ok(almost_equal(d, 0.1e-4736L), "d = %lf\n", d);
1307
1308 errno = 0xdeadbeef;
1309 strtod(overflow, &end);
1310 ok(errno == ERANGE, "errno = %x\n", errno);
1311 ok(end == overflow+21, "incorrect end (%d)\n", (int)(end-overflow));
1312
1313 errno = 0xdeadbeef;
1314 strtod("-1d309", NULL);
1315 ok(errno == ERANGE, "errno = %x\n", errno);
1316 }
1317
1318 static void test_mbstowcs(void)
1319 {
1320 static const wchar_t wSimple[] = { 't','e','x','t',0 };
1321 static const wchar_t wHiragana[] = { 0x3042,0x3043,0 };
1322 static const char mSimple[] = "text";
1323 static const char mHiragana[] = { 0x82,0xa0,0x82,0xa1,0 };
1324
1325 const wchar_t *pwstr;
1326 wchar_t wOut[6];
1327 char mOut[6];
1328 size_t ret;
1329 int err;
1330
1331 wOut[4] = '!'; wOut[5] = '\0';
1332 mOut[4] = '!'; mOut[5] = '\0';
1333
1334 ret = mbstowcs(NULL, mSimple, 0);
1335 ok(ret == 4, "mbstowcs did not return 4\n");
1336
1337 ret = mbstowcs(wOut, mSimple, 4);
1338 ok(ret == 4, "mbstowcs did not return 4\n");
1339 ok(!memcmp(wOut, wSimple, 4*sizeof(wchar_t)), "wOut = %s\n", wine_dbgstr_w(wOut));
1340 ok(wOut[4] == '!', "wOut[4] != \'!\'\n");
1341
1342 ret = wcstombs(NULL, wSimple, 0);
1343 ok(ret == 4, "wcstombs did not return 4\n");
1344
1345 ret = wcstombs(mOut, wSimple, 6);
1346 ok(ret == 4, "wcstombs did not return 4\n");
1347 ok(!memcmp(mOut, mSimple, 5*sizeof(char)), "mOut = %s\n", mOut);
1348
1349 ret = wcstombs(mOut, wSimple, 2);
1350 ok(ret == 2, "wcstombs did not return 2\n");
1351 ok(!memcmp(mOut, mSimple, 5*sizeof(char)), "mOut = %s\n", mOut);
1352
1353 if(!setlocale(LC_ALL, "Japanese_Japan.932")) {
1354 win_skip("Japanese_Japan.932 locale not available\n");
1355 return;
1356 }
1357
1358 ret = mbstowcs(wOut, mHiragana, 6);
1359 ok(ret == 2, "mbstowcs did not return 2\n");
1360 ok(!memcmp(wOut, wHiragana, sizeof(wHiragana)), "wOut = %s\n", wine_dbgstr_w(wOut));
1361
1362 ret = wcstombs(mOut, wHiragana, 6);
1363 ok(ret == 4, "wcstombs did not return 4\n");
1364 ok(!memcmp(mOut, mHiragana, sizeof(mHiragana)), "mOut = %s\n", mOut);
1365
1366 if(!pmbstowcs_s || !pwcstombs_s) {
1367 setlocale(LC_ALL, "C");
1368 win_skip("mbstowcs_s or wcstombs_s not available\n");
1369 return;
1370 }
1371
1372 err = pmbstowcs_s(&ret, wOut, 6, mSimple, _TRUNCATE);
1373 ok(err == 0, "err = %d\n", err);
1374 ok(ret == 5, "mbstowcs_s did not return 5\n");
1375 ok(!memcmp(wOut, wSimple, sizeof(wSimple)), "wOut = %s\n", wine_dbgstr_w(wOut));
1376
1377 err = pmbstowcs_s(&ret, wOut, 6, mHiragana, _TRUNCATE);
1378 ok(err == 0, "err = %d\n", err);
1379 ok(ret == 3, "mbstowcs_s did not return 3\n");
1380 ok(!memcmp(wOut, wHiragana, sizeof(wHiragana)), "wOut = %s\n", wine_dbgstr_w(wOut));
1381
1382 err = pmbstowcs_s(&ret, NULL, 0, mHiragana, 1);
1383 ok(err == 0, "err = %d\n", err);
1384 ok(ret == 3, "mbstowcs_s did not return 3\n");
1385
1386 err = pwcstombs_s(&ret, mOut, 6, wSimple, _TRUNCATE);
1387 ok(err == 0, "err = %d\n", err);
1388 ok(ret == 5, "wcstombs_s did not return 5\n");
1389 ok(!memcmp(mOut, mSimple, sizeof(mSimple)), "mOut = %s\n", mOut);
1390
1391 err = pwcstombs_s(&ret, mOut, 6, wHiragana, _TRUNCATE);
1392 ok(err == 0, "err = %d\n", err);
1393 ok(ret == 5, "wcstombs_s did not return 5\n");
1394 ok(!memcmp(mOut, mHiragana, sizeof(mHiragana)), "mOut = %s\n", mOut);
1395
1396 err = pwcstombs_s(&ret, NULL, 0, wHiragana, 1);
1397 ok(err == 0, "err = %d\n", err);
1398 ok(ret == 5, "wcstombs_s did not return 5\n");
1399
1400 if(!pwcsrtombs) {
1401 setlocale(LC_ALL, "C");
1402 win_skip("wcsrtombs not available\n");
1403 return;
1404 }
1405
1406 pwstr = wSimple;
1407 err = -3;
1408 ret = pwcsrtombs(mOut, &pwstr, 4, &err);
1409 ok(ret == 4, "wcsrtombs did not return 4\n");
1410 ok(err == 0, "err = %d\n", err);
1411 ok(pwstr == wSimple+4, "pwstr = %p (wszSimple = %p)\n", pwstr, wSimple);
1412 ok(!memcmp(mOut, mSimple, ret), "mOut = %s\n", mOut);
1413
1414 pwstr = wSimple;
1415 ret = pwcsrtombs(mOut, &pwstr, 5, NULL);
1416 ok(ret == 4, "wcsrtombs did not return 4\n");
1417 ok(pwstr == NULL, "pwstr != NULL\n");
1418 ok(!memcmp(mOut, mSimple, sizeof(mSimple)), "mOut = %s\n", mOut);
1419
1420 setlocale(LC_ALL, "C");
1421 }
1422
1423 static void test_gcvt(void)
1424 {
1425 char buf[1024], *res;
1426 errno_t err;
1427
1428 if(!p_gcvt_s) {
1429 win_skip("Skipping _gcvt tests\n");
1430 return;
1431 }
1432
1433 errno = 0;
1434 res = _gcvt(1.2, -1, buf);
1435 ok(res == NULL, "res != NULL\n");
1436 ok(errno == ERANGE, "errno = %d\n", errno);
1437
1438 errno = 0;
1439 res = _gcvt(1.2, 5, NULL);
1440 ok(res == NULL, "res != NULL\n");
1441 ok(errno == EINVAL, "errno = %d\n", errno);
1442
1443 res = gcvt(1.2, 5, buf);
1444 ok(res == buf, "res != buf\n");
1445 ok(!strcmp(buf, "1.2"), "buf = %s\n", buf);
1446
1447 buf[0] = 'x';
1448 err = p_gcvt_s(buf, 5, 1.2, 10);
1449 ok(err == ERANGE, "err = %d\n", err);
1450 ok(buf[0] == '\0', "buf[0] = %c\n", buf[0]);
1451
1452 buf[0] = 'x';
1453 err = p_gcvt_s(buf, 4, 123456, 2);
1454 ok(err == ERANGE, "err = %d\n", err);
1455 ok(buf[0] == '\0', "buf[0] = %c\n", buf[0]);
1456 }
1457
1458 static void test__itoa_s(void)
1459 {
1460 errno_t ret;
1461 char buffer[33];
1462
1463 if (!p_itoa_s)
1464 {
1465 win_skip("Skipping _itoa_s tests\n");
1466 return;
1467 }
1468
1469 if (p_set_invalid_parameter_handler)
1470 ok(p_set_invalid_parameter_handler(test_invalid_parameter_handler) == NULL,
1471 "Invalid parameter handler was already set\n");
1472
1473 errno = EBADF;
1474 ret = p_itoa_s(0, NULL, 0, 0);
1475 ok(ret == EINVAL, "Expected _itoa_s to return EINVAL, got %d\n", ret);
1476 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1477
1478 memset(buffer, 'X', sizeof(buffer));
1479 errno = EBADF;
1480 ret = p_itoa_s(0, buffer, 0, 0);
1481 ok(ret == EINVAL, "Expected _itoa_s to return EINVAL, got %d\n", ret);
1482 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1483 ok(buffer[0] == 'X', "Expected the output buffer to be untouched\n");
1484
1485 memset(buffer, 'X', sizeof(buffer));
1486 errno = EBADF;
1487 ret = p_itoa_s(0, buffer, sizeof(buffer), 0);
1488 ok(ret == EINVAL, "Expected _itoa_s to return EINVAL, got %d\n", ret);
1489 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1490 ok(buffer[0] == '\0', "Expected the output buffer to be null terminated\n");
1491
1492 memset(buffer, 'X', sizeof(buffer));
1493 errno = EBADF;
1494 ret = p_itoa_s(0, buffer, sizeof(buffer), 64);
1495 ok(ret == EINVAL, "Expected _itoa_s to return EINVAL, got %d\n", ret);
1496 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1497 ok(buffer[0] == '\0', "Expected the output buffer to be null terminated\n");
1498
1499 memset(buffer, 'X', sizeof(buffer));
1500 errno = EBADF;
1501 ret = p_itoa_s(12345678, buffer, 4, 10);
1502 ok(ret == ERANGE, "Expected _itoa_s to return ERANGE, got %d\n", ret);
1503 ok(errno == ERANGE, "Expected errno to be ERANGE, got %d\n", errno);
1504 ok(!memcmp(buffer, "\000765", 4),
1505 "Expected the output buffer to be null terminated with truncated output\n");
1506
1507 memset(buffer, 'X', sizeof(buffer));
1508 errno = EBADF;
1509 ret = p_itoa_s(12345678, buffer, 8, 10);
1510 ok(ret == ERANGE, "Expected _itoa_s to return ERANGE, got %d\n", ret);
1511 ok(errno == ERANGE, "Expected errno to be ERANGE, got %d\n", errno);
1512 ok(!memcmp(buffer, "\0007654321", 8),
1513 "Expected the output buffer to be null terminated with truncated output\n");
1514
1515 memset(buffer, 'X', sizeof(buffer));
1516 errno = EBADF;
1517 ret = p_itoa_s(-12345678, buffer, 9, 10);
1518 ok(ret == ERANGE, "Expected _itoa_s to return ERANGE, got %d\n", ret);
1519 ok(errno == ERANGE, "Expected errno to be ERANGE, got %d\n", errno);
1520 ok(!memcmp(buffer, "\00087654321", 9),
1521 "Expected the output buffer to be null terminated with truncated output\n");
1522
1523 ret = p_itoa_s(12345678, buffer, 9, 10);
1524 ok(ret == 0, "Expected _itoa_s to return 0, got %d\n", ret);
1525 ok(!strcmp(buffer, "12345678"),
1526 "Expected output buffer string to be \"12345678\", got \"%s\"\n",
1527 buffer);
1528
1529 ret = p_itoa_s(43690, buffer, sizeof(buffer), 2);
1530 ok(ret == 0, "Expected _itoa_s to return 0, got %d\n", ret);
1531 ok(!strcmp(buffer, "1010101010101010"),
1532 "Expected output buffer string to be \"1010101010101010\", got \"%s\"\n",
1533 buffer);
1534
1535 ret = p_itoa_s(1092009, buffer, sizeof(buffer), 36);
1536 ok(ret == 0, "Expected _itoa_s to return 0, got %d\n", ret);
1537 ok(!strcmp(buffer, "nell"),
1538 "Expected output buffer string to be \"nell\", got \"%s\"\n",
1539 buffer);
1540
1541 ret = p_itoa_s(5704, buffer, sizeof(buffer), 18);
1542 ok(ret == 0, "Expected _itoa_s to return 0, got %d\n", ret);
1543 ok(!strcmp(buffer, "hag"),
1544 "Expected output buffer string to be \"hag\", got \"%s\"\n",
1545 buffer);
1546
1547 ret = p_itoa_s(-12345678, buffer, sizeof(buffer), 10);
1548 ok(ret == 0, "Expected _itoa_s to return 0, got %d\n", ret);
1549 ok(!strcmp(buffer, "-12345678"),
1550 "Expected output buffer string to be \"-12345678\", got \"%s\"\n",
1551 buffer);
1552 if (p_set_invalid_parameter_handler)
1553 ok(p_set_invalid_parameter_handler(NULL) == test_invalid_parameter_handler,
1554 "Cannot reset invalid parameter handler\n");
1555 }
1556
1557 static void test__strlwr_s(void)
1558 {
1559 errno_t ret;
1560 char buffer[20];
1561
1562 if (!p_strlwr_s)
1563 {
1564 win_skip("Skipping _strlwr_s tests\n");
1565 return;
1566 }
1567
1568 errno = EBADF;
1569 ret = p_strlwr_s(NULL, 0);
1570 ok(ret == EINVAL, "Expected _strlwr_s to return EINVAL, got %d\n", ret);
1571 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1572
1573 errno = EBADF;
1574 ret = p_strlwr_s(NULL, sizeof(buffer));
1575 ok(ret == EINVAL, "Expected _strlwr_s to return EINVAL, got %d\n", ret);
1576 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1577
1578 errno = EBADF;
1579 ret = p_strlwr_s(buffer, 0);
1580 ok(ret == EINVAL, "Expected _strlwr_s to return EINVAL, got %d\n", ret);
1581 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1582
1583 strcpy(buffer, "GoRrIsTeR");
1584 errno = EBADF;
1585 ret = p_strlwr_s(buffer, 5);
1586 ok(ret == EINVAL, "Expected _strlwr_s to return EINVAL, got %d\n", ret);
1587 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1588 ok(!memcmp(buffer, "\0oRrIsTeR", sizeof("\0oRrIsTeR")),
1589 "Expected the output buffer to be \"gorrIsTeR\"\n");
1590
1591 strcpy(buffer, "GoRrIsTeR");
1592 errno = EBADF;
1593 ret = p_strlwr_s(buffer, sizeof("GoRrIsTeR") - 1);
1594 ok(ret == EINVAL, "Expected _strlwr_s to return EINVAL, got %d\n", ret);
1595 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1596 ok(!memcmp(buffer, "\0oRrIsTeR", sizeof("\0oRrIsTeR")),
1597 "Expected the output buffer to be \"gorrIsTeR\"\n");
1598
1599 strcpy(buffer, "GoRrIsTeR");
1600 ret = p_strlwr_s(buffer, sizeof("GoRrIsTeR"));
1601 ok(ret == 0, "Expected _strlwr_s to return 0, got %d\n", ret);
1602 ok(!strcmp(buffer, "gorrister"),
1603 "Expected the output buffer to be \"gorrister\", got \"%s\"\n",
1604 buffer);
1605
1606 memcpy(buffer, "GoRrIsTeR\0ELLEN", sizeof("GoRrIsTeR\0ELLEN"));
1607 ret = p_strlwr_s(buffer, sizeof(buffer));
1608 ok(ret == 0, "Expected _strlwr_s to return 0, got %d\n", ret);
1609 ok(!memcmp(buffer, "gorrister\0ELLEN", sizeof("gorrister\0ELLEN")),
1610 "Expected the output buffer to be \"gorrister\\0ELLEN\", got \"%s\"\n",
1611 buffer);
1612 }
1613
1614 static void test_wcsncat_s(void)
1615 {
1616 static wchar_t abcW[] = {'a','b','c',0};
1617 int ret;
1618 wchar_t dst[4];
1619 wchar_t src[4];
1620
1621 if (!p_wcsncat_s)
1622 {
1623 win_skip("skipping wcsncat_s tests\n");
1624 return;
1625 }
1626
1627 if (p_set_invalid_parameter_handler)
1628 ok(p_set_invalid_parameter_handler(test_invalid_parameter_handler) == NULL,
1629 "Invalid parameter handler was already set\n");
1630
1631 memcpy(src, abcW, sizeof(abcW));
1632 dst[0] = 0;
1633 ret = p_wcsncat_s(NULL, 4, src, 4);
1634 ok(ret == EINVAL, "err = %d\n", ret);
1635 ret = p_wcsncat_s(dst, 0, src, 4);
1636 ok(ret == EINVAL, "err = %d\n", ret);
1637 ret = p_wcsncat_s(dst, 0, src, _TRUNCATE);
1638 ok(ret == EINVAL, "err = %d\n", ret);
1639 ret = p_wcsncat_s(dst, 4, NULL, 0);
1640 ok(ret == 0, "err = %d\n", ret);
1641
1642 dst[0] = 0;
1643 ret = p_wcsncat_s(dst, 2, src, 4);
1644 ok(ret == ERANGE, "err = %d\n", ret);
1645
1646 dst[0] = 0;
1647 ret = p_wcsncat_s(dst, 2, src, _TRUNCATE);
1648 ok(ret == STRUNCATE, "err = %d\n", ret);
1649 ok(dst[0] == 'a' && dst[1] == 0, "dst is %s\n", wine_dbgstr_w(dst));
1650
1651 memcpy(dst, abcW, sizeof(abcW));
1652 dst[3] = 'd';
1653 ret = p_wcsncat_s(dst, 4, src, 4);
1654 ok(ret == EINVAL, "err = %d\n", ret);
1655
1656 if (p_set_invalid_parameter_handler)
1657 ok(p_set_invalid_parameter_handler(NULL) == test_invalid_parameter_handler,
1658 "Cannot reset invalid parameter handler\n");
1659 }
1660
1661 static void test__mbsnbcat_s(void)
1662 {
1663 unsigned char dest[16];
1664 const unsigned char first[] = "dinosaur";
1665 const unsigned char second[] = "duck";
1666 int ret;
1667
1668 if (!p_mbsnbcat_s)
1669 {
1670 win_skip("Skipping _mbsnbcat_s tests\n");
1671 return;
1672 }
1673
1674 /* Test invalid arguments. */
1675 ret = p_mbsnbcat_s(NULL, 0, NULL, 0);
1676 ok(ret == 0, "Expected _mbsnbcat_s to return 0, got %d\n", ret);
1677
1678 errno = EBADF;
1679 ret = p_mbsnbcat_s(NULL, 10, NULL, 0);
1680 ok(ret == EINVAL, "Expected _mbsnbcat_s to return EINVAL, got %d\n", ret);
1681 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1682
1683 errno = EBADF;
1684 ret = p_mbsnbcat_s(NULL, 0, NULL, 10);
1685 ok(ret == EINVAL, "Expected _mbsnbcat_s to return EINVAL, got %d\n", ret);
1686 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1687
1688 memset(dest, 'X', sizeof(dest));
1689 errno = EBADF;
1690 ret = p_mbsnbcat_s(dest, 0, NULL, 0);
1691 ok(ret == EINVAL, "Expected _mbsnbcat_s to return EINVAL, got %d\n", ret);
1692 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1693 ok(dest[0] == 'X', "Expected the output buffer to be untouched\n");
1694
1695 memset(dest, 'X', sizeof(dest));
1696 errno = EBADF;
1697 ret = p_mbsnbcat_s(dest, 0, second, 0);
1698 ok(ret == EINVAL, "Expected _mbsnbcat_s to return EINVAL, got %d\n", ret);
1699 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1700 ok(dest[0] == 'X', "Expected the output buffer to be untouched\n");
1701
1702 memset(dest, 'X', sizeof(dest));
1703 errno = EBADF;
1704 ret = p_mbsnbcat_s(dest, sizeof(dest), NULL, 0);
1705 ok(ret == EINVAL, "Expected _mbsnbcat_s to return EINVAL, got %d\n", ret);
1706 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1707 ok(dest[0] == '\0', "Expected the output buffer to be null terminated\n");
1708
1709 memset(dest, 'X', sizeof(dest));
1710 errno = EBADF;
1711 ret = p_mbsnbcat_s(dest, sizeof(dest), NULL, 10);
1712 ok(ret == EINVAL, "Expected _mbsnbcat_s to return EINVAL, got %d\n", ret);
1713 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1714 ok(dest[0] == '\0', "Expected the output buffer to be null terminated\n");
1715
1716 memset(dest, 'X', sizeof(dest));
1717 dest[0] = '\0';
1718 ret = p_mbsnbcat_s(dest, sizeof(dest), second, sizeof(second));
1719 ok(ret == 0, "Expected _mbsnbcat_s to return 0, got %d\n", ret);
1720 ok(!memcmp(dest, second, sizeof(second)),
1721 "Expected the output buffer string to be \"duck\"\n");
1722
1723 /* Test source truncation behavior. */
1724 memset(dest, 'X', sizeof(dest));
1725 memcpy(dest, first, sizeof(first));
1726 ret = p_mbsnbcat_s(dest, sizeof(dest), second, 0);
1727 ok(ret == 0, "Expected _mbsnbcat_s to return 0, got %d\n", ret);
1728 ok(!memcmp(dest, first, sizeof(first)),
1729 "Expected the output buffer string to be \"dinosaur\"\n");
1730
1731 memset(dest, 'X', sizeof(dest));
1732 memcpy(dest, first, sizeof(first));
1733 ret = p_mbsnbcat_s(dest, sizeof(dest), second, sizeof(second));
1734 ok(ret == 0, "Expected _mbsnbcat_s to return 0, got %d\n", ret);
1735 ok(!memcmp(dest, "dinosaurduck", sizeof("dinosaurduck")),
1736 "Expected the output buffer string to be \"dinosaurduck\"\n");
1737
1738 memset(dest, 'X', sizeof(dest));
1739 memcpy(dest, first, sizeof(first));
1740 ret = p_mbsnbcat_s(dest, sizeof(dest), second, sizeof(second) + 1);
1741 ok(ret == 0, "Expected _mbsnbcat_s to return 0, got %d\n", ret);
1742 ok(!memcmp(dest, "dinosaurduck", sizeof("dinosaurduck")),
1743 "Expected the output buffer string to be \"dinosaurduck\"\n");
1744
1745 memset(dest, 'X', sizeof(dest));
1746 memcpy(dest, first, sizeof(first));
1747 ret = p_mbsnbcat_s(dest, sizeof(dest), second, sizeof(second) - 1);
1748 ok(ret == 0, "Expected _mbsnbcat_s to return 0, got %d\n", ret);
1749 ok(!memcmp(dest, "dinosaurduck", sizeof("dinosaurduck")),
1750 "Expected the output buffer string to be \"dinosaurduck\"\n");
1751
1752 memset(dest, 'X', sizeof(dest));
1753 memcpy(dest, first, sizeof(first));
1754 ret = p_mbsnbcat_s(dest, sizeof(dest), second, sizeof(second) - 2);
1755 ok(ret == 0, "Expected _mbsnbcat_s to return 0, got %d\n", ret);
1756 ok(!memcmp(dest, "dinosaurduc", sizeof("dinosaurduc")),
1757 "Expected the output buffer string to be \"dinosaurduc\"\n");
1758
1759 /* Test destination truncation behavior. */
1760 memset(dest, 'X', sizeof(dest));
1761 memcpy(dest, first, sizeof(first));
1762 errno = EBADF;
1763 ret = p_mbsnbcat_s(dest, sizeof(first) - 1, second, sizeof(second));
1764 ok(ret == EINVAL, "Expected _mbsnbcat_s to return EINVAL, got %d\n", ret);
1765 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1766 ok(!memcmp(dest, "\0inosaur", sizeof("\0inosaur") - 1),
1767 "Expected the output buffer string to be \"\\0inosaur\" without ending null terminator\n");
1768
1769 memset(dest, 'X', sizeof(dest));
1770 memcpy(dest, first, sizeof(first));
1771 errno = EBADF;
1772 ret = p_mbsnbcat_s(dest, sizeof(first), second, sizeof(second));
1773 ok(ret == ERANGE, "Expected _mbsnbcat_s to return ERANGE, got %d\n", ret);
1774 ok(errno == ERANGE, "Expected errno to be ERANGE, got %d\n", errno);
1775 ok(!memcmp(dest, "\0inosaurd", sizeof("\0inosaurd") - 1),
1776 "Expected the output buffer string to be \"\\0inosaurd\" without ending null terminator\n");
1777
1778 memset(dest, 'X', sizeof(dest));
1779 memcpy(dest, first, sizeof(first));
1780 errno = EBADF;
1781 ret = p_mbsnbcat_s(dest, sizeof(first) + 1, second, sizeof(second));
1782 ok(ret == ERANGE, "Expected _mbsnbcat_s to return ERANGE, got %d\n", ret);
1783 ok(errno == ERANGE, "Expected errno to be ERANGE, got %d\n", errno);
1784 ok(!memcmp(dest, "\0inosaurdu", sizeof("\0inosaurdu") - 1),
1785 "Expected the output buffer string to be \"\\0inosaurdu\" without ending null terminator\n");
1786 }
1787
1788 static void test__mbsupr_s(void)
1789 {
1790 errno_t ret;
1791 unsigned char buffer[20];
1792
1793 if (!p_mbsupr_s)
1794 {
1795 win_skip("Skipping _mbsupr_s tests\n");
1796 return;
1797 }
1798
1799 errno = EBADF;
1800 ret = p_mbsupr_s(NULL, 0);
1801 ok(ret == 0, "Expected _mbsupr_s to return 0, got %d\n", ret);
1802
1803 errno = EBADF;
1804 ret = p_mbsupr_s(NULL, sizeof(buffer));
1805 ok(ret == EINVAL, "Expected _mbsupr_s to return EINVAL, got %d\n", ret);
1806 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1807
1808 errno = EBADF;
1809 ret = p_mbsupr_s(buffer, 0);
1810 ok(ret == EINVAL, "Expected _mbsupr_s to return EINVAL, got %d\n", ret);
1811 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1812
1813 memcpy(buffer, "abcdefgh", sizeof("abcdefgh"));
1814 errno = EBADF;
1815 ret = p_mbsupr_s(buffer, sizeof("abcdefgh"));
1816 ok(ret == 0, "Expected _mbsupr_s to return 0, got %d\n", ret);
1817 ok(!memcmp(buffer, "ABCDEFGH", sizeof("ABCDEFGH")),
1818 "Expected the output buffer to be \"ABCDEFGH\", got \"%s\"\n",
1819 buffer);
1820
1821 memcpy(buffer, "abcdefgh", sizeof("abcdefgh"));
1822 errno = EBADF;
1823 ret = p_mbsupr_s(buffer, sizeof(buffer));
1824 ok(ret == 0, "Expected _mbsupr_s to return 0, got %d\n", ret);
1825 ok(!memcmp(buffer, "ABCDEFGH", sizeof("ABCDEFGH")),
1826 "Expected the output buffer to be \"ABCDEFGH\", got \"%s\"\n",
1827 buffer);
1828
1829 memcpy(buffer, "abcdefgh", sizeof("abcdefgh"));
1830 errno = EBADF;
1831 ret = p_mbsupr_s(buffer, 4);
1832 ok(ret == EINVAL, "Expected _mbsupr_s to return EINVAL, got %d\n", ret);
1833 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1834
1835 memcpy(buffer, "abcdefgh\0ijklmnop", sizeof("abcdefgh\0ijklmnop"));
1836 errno = EBADF;
1837 ret = p_mbsupr_s(buffer, sizeof(buffer));
1838 ok(ret == 0, "Expected _mbsupr_s to return 0, got %d\n", ret);
1839 ok(!memcmp(buffer, "ABCDEFGH\0ijklmnop", sizeof("ABCDEFGH\0ijklmnop")),
1840 "Expected the output buffer to be \"ABCDEFGH\\0ijklmnop\", got \"%s\"\n",
1841 buffer);
1842
1843 }
1844
1845 static void test__mbslwr_s(void)
1846 {
1847 errno_t ret;
1848 unsigned char buffer[20];
1849
1850 if (!p_mbslwr_s)
1851 {
1852 win_skip("Skipping _mbslwr_s tests\n");
1853 return;
1854 }
1855
1856 errno = EBADF;
1857 ret = p_mbslwr_s(NULL, 0);
1858 ok(ret == 0, "Expected _mbslwr_s to return 0, got %d\n", ret);
1859
1860 errno = EBADF;
1861 ret = p_mbslwr_s(NULL, sizeof(buffer));
1862 ok(ret == EINVAL, "Expected _mbslwr_s to return EINVAL, got %d\n", ret);
1863 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1864
1865 errno = EBADF;
1866 ret = p_mbslwr_s(buffer, 0);
1867 ok(ret == EINVAL, "Expected _mbslwr_s to return EINVAL, got %d\n", ret);
1868 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1869
1870 memcpy(buffer, "ABCDEFGH", sizeof("ABCDEFGH"));
1871 errno = EBADF;
1872 ret = p_mbslwr_s(buffer, sizeof("ABCDEFGH"));
1873 ok(ret == 0, "Expected _mbslwr_s to return 0, got %d\n", ret);
1874 ok(!memcmp(buffer, "abcdefgh", sizeof("abcdefgh")),
1875 "Expected the output buffer to be \"abcdefgh\", got \"%s\"\n",
1876 buffer);
1877
1878 memcpy(buffer, "ABCDEFGH", sizeof("ABCDEFGH"));
1879 errno = EBADF;
1880 ret = p_mbslwr_s(buffer, sizeof(buffer));
1881 ok(ret == 0, "Expected _mbslwr_s to return 0, got %d\n", ret);
1882 ok(!memcmp(buffer, "abcdefgh", sizeof("abcdefgh")),
1883 "Expected the output buffer to be \"abcdefgh\", got \"%s\"\n",
1884 buffer);
1885
1886 memcpy(buffer, "ABCDEFGH", sizeof("ABCDEFGH"));
1887 errno = EBADF;
1888 ret = p_mbslwr_s(buffer, 4);
1889 ok(ret == EINVAL, "Expected _mbslwr_s to return EINVAL, got %d\n", ret);
1890 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1891
1892 memcpy(buffer, "ABCDEFGH\0IJKLMNOP", sizeof("ABCDEFGH\0IJKLMNOP"));
1893 errno = EBADF;
1894 ret = p_mbslwr_s(buffer, sizeof(buffer));
1895 ok(ret == 0, "Expected _mbslwr_s to return 0, got %d\n", ret);
1896 ok(!memcmp(buffer, "abcdefgh\0IJKLMNOP", sizeof("abcdefgh\0IJKLMNOP")),
1897 "Expected the output buffer to be \"abcdefgh\\0IJKLMNOP\", got \"%s\"\n",
1898 buffer);
1899 }
1900
1901 static void test__ultoa_s(void)
1902 {
1903 errno_t ret;
1904 char buffer[33];
1905
1906 if (!p_ultoa_s)
1907 {
1908 win_skip("Skipping _ultoa_s tests\n");
1909 return;
1910 }
1911
1912 errno = EBADF;
1913 ret = p_ultoa_s(0, NULL, 0, 0);
1914 ok(ret == EINVAL, "Expected _ultoa_s to return EINVAL, got %d\n", ret);
1915 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1916
1917 memset(buffer, 'X', sizeof(buffer));
1918 errno = EBADF;
1919 ret = p_ultoa_s(0, buffer, 0, 0);
1920 ok(ret == EINVAL, "Expected _ultoa_s to return EINVAL, got %d\n", ret);
1921 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1922 ok(buffer[0] == 'X', "Expected the output buffer to be untouched\n");
1923
1924 memset(buffer, 'X', sizeof(buffer));
1925 errno = EBADF;
1926 ret = p_ultoa_s(0, buffer, sizeof(buffer), 0);
1927 ok(ret == EINVAL, "Expected _ultoa_s to return EINVAL, got %d\n", ret);
1928 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1929 ok(buffer[0] == '\0', "Expected the output buffer to be null terminated\n");
1930
1931 memset(buffer, 'X', sizeof(buffer));
1932 errno = EBADF;
1933 ret = p_ultoa_s(0, buffer, sizeof(buffer), 64);
1934 ok(ret == EINVAL, "Expected _ultoa_s to return EINVAL, got %d\n", ret);
1935 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1936 ok(buffer[0] == '\0', "Expected the output buffer to be null terminated\n");
1937
1938 memset(buffer, 'X', sizeof(buffer));
1939 errno = EBADF;
1940 ret = p_ultoa_s(12345678, buffer, 4, 10);
1941 ok(ret == ERANGE, "Expected _ultoa_s to return ERANGE, got %d\n", ret);
1942 ok(errno == ERANGE, "Expected errno to be ERANGE, got %d\n", errno);
1943 ok(!memcmp(buffer, "\000765", 4),
1944 "Expected the output buffer to be null terminated with truncated output\n");
1945
1946 memset(buffer, 'X', sizeof(buffer));
1947 errno = EBADF;
1948 ret = p_ultoa_s(12345678, buffer, 8, 10);
1949 ok(ret == ERANGE, "Expected _ultoa_s to return ERANGE, got %d\n", ret);
1950 ok(errno == ERANGE, "Expected errno to be ERANGE, got %d\n", errno);
1951 ok(!memcmp(buffer, "\0007654321", 8),
1952 "Expected the output buffer to be null terminated with truncated output\n");
1953
1954 ret = p_ultoa_s(12345678, buffer, 9, 10);
1955 ok(ret == 0, "Expected _ultoa_s to return 0, got %d\n", ret);
1956 ok(!strcmp(buffer, "12345678"),
1957 "Expected output buffer string to be \"12345678\", got \"%s\"\n",
1958 buffer);
1959
1960 ret = p_ultoa_s(43690, buffer, sizeof(buffer), 2);
1961 ok(ret == 0, "Expected _ultoa_s to return 0, got %d\n", ret);
1962 ok(!strcmp(buffer, "1010101010101010"),
1963 "Expected output buffer string to be \"1010101010101010\", got \"%s\"\n",
1964 buffer);
1965
1966 ret = p_ultoa_s(1092009, buffer, sizeof(buffer), 36);
1967 ok(ret == 0, "Expected _ultoa_s to return 0, got %d\n", ret);
1968 ok(!strcmp(buffer, "nell"),
1969 "Expected output buffer string to be \"nell\", got \"%s\"\n",
1970 buffer);
1971
1972 ret = p_ultoa_s(5704, buffer, sizeof(buffer), 18);
1973 ok(ret == 0, "Expected _ultoa_s to return 0, got %d\n", ret);
1974 ok(!strcmp(buffer, "hag"),
1975 "Expected output buffer string to be \"hag\", got \"%s\"\n",
1976 buffer);
1977 }
1978
1979 static void test_wctob(void)
1980 {
1981 int ret;
1982
1983 if(!p_wctob || !setlocale(LC_ALL, "chinese-traditional")) {
1984 win_skip("Skipping wctob tests\n");
1985 return;
1986 }
1987
1988 ret = p_wctob(0x8141);
1989 ok(ret == EOF, "ret = %x\n", ret);
1990
1991 ret = p_wctob(0x81);
1992 ok(ret == EOF, "ret = %x\n", ret);
1993
1994 ret = p_wctob(0xe0);
1995 ok(ret == 0x61, "ret = %x\n", ret);
1996
1997 _setmbcp(1250);
1998 ret = p_wctob(0x81);
1999 ok(ret == EOF, "ret = %x\n", ret);
2000
2001 setlocale(LC_ALL, "C");
2002 ret = p_wctob(0x8141);
2003 ok(ret == EOF, "ret = %x\n", ret);
2004
2005 ret = p_wctob(0x81);
2006 ok(ret == (int)(char)0x81, "ret = %x\n", ret);
2007
2008 ret = p_wctob(0xe0);
2009 ok(ret == (int)(char)0xe0, "ret = %x\n", ret);
2010 }
2011
2012 static void test_tolower(void)
2013 {
2014 int ret;
2015
2016 ret = p_tolower(0x41);
2017 ok(ret == 0x61, "ret = %x\n", ret);
2018
2019 ret = p_tolower(0xF4);
2020 ok(ret == 0xF4, "ret = %x\n", ret);
2021
2022 ret = p_tolower((char)0xF4);
2023 ok(ret==0xF4/*Vista+*/ || ret==(char)0xF4, "ret = %x\n", ret);
2024
2025 /* is it using different locale (CP_ACP) for negative values?? */
2026 /* current implementation matches msvcr90 behaviour */
2027 ret = p_tolower((char)0xD0);
2028 todo_wine ok(ret==0xF0/*Vista+*/ || ret==(char)0xD0, "ret = %x\n", ret);
2029
2030 ret = p_tolower(0xD0);
2031 ok(ret == 0xD0, "ret = %x\n", ret);
2032
2033 if(!setlocale(LC_ALL, "us")) {
2034 win_skip("skipping tolower tests that depends on locale\n");
2035 return;
2036 }
2037
2038 ret = p_tolower((char)0xD0);
2039 ok(ret == 0xF0, "ret = %x\n", ret);
2040
2041 ret = p_tolower(0xD0);
2042 ok(ret == 0xF0, "ret = %x\n", ret);
2043
2044 setlocale(LC_ALL, "C");
2045 }
2046
2047 START_TEST(string)
2048 {
2049 char mem[100];
2050 static const char xilstring[]="c:/xilinx";
2051 int nLen;
2052
2053 hMsvcrt = GetModuleHandleA("msvcrt.dll");
2054 if (!hMsvcrt)
2055 hMsvcrt = GetModuleHandleA("msvcrtd.dll");
2056 ok(hMsvcrt != 0, "GetModuleHandleA failed\n");
2057 SET(pmemcpy,"memcpy");
2058 SET(pmemcmp,"memcmp");
2059 SET(p_mbctype,"_mbctype");
2060 SET(p__mb_cur_max,"__mb_cur_max");
2061 pstrcpy_s = (void *)GetProcAddress( hMsvcrt,"strcpy_s" );
2062 pstrcat_s = (void *)GetProcAddress( hMsvcrt,"strcat_s" );
2063 p_mbsnbcat_s = (void *)GetProcAddress( hMsvcrt,"_mbsnbcat_s" );
2064 p_mbsnbcpy_s = (void *)GetProcAddress( hMsvcrt,"_mbsnbcpy_s" );
2065 p_wcscpy_s = (void *)GetProcAddress( hMsvcrt,"wcscpy_s" );
2066 p_wcsncpy_s = (void *)GetProcAddress( hMsvcrt,"wcsncpy_s" );
2067 p_wcsncat_s = (void *)GetProcAddress( hMsvcrt,"wcsncat_s" );
2068 p_wcsupr_s = (void *)GetProcAddress( hMsvcrt,"_wcsupr_s" );
2069 p_strnlen = (void *)GetProcAddress( hMsvcrt,"strnlen" );
2070 p_strtoi64 = (void *)GetProcAddress(hMsvcrt, "_strtoi64");
2071 p_strtoui64 = (void *)GetProcAddress(hMsvcrt, "_strtoui64");
2072 pmbstowcs_s = (void *)GetProcAddress(hMsvcrt, "mbstowcs_s");
2073 pwcstombs_s = (void *)GetProcAddress(hMsvcrt, "wcstombs_s");
2074 pwcsrtombs = (void *)GetProcAddress(hMsvcrt, "wcsrtombs");
2075 p_gcvt_s = (void *)GetProcAddress(hMsvcrt, "_gcvt_s");
2076 p_itoa_s = (void *)GetProcAddress(hMsvcrt, "_itoa_s");
2077 p_strlwr_s = (void *)GetProcAddress(hMsvcrt, "_strlwr_s");
2078 p_ultoa_s = (void *)GetProcAddress(hMsvcrt, "_ultoa_s");
2079 p_set_invalid_parameter_handler = (void *) GetProcAddress(hMsvcrt, "_set_invalid_parameter_handler");
2080 p_wcslwr_s = (void*)GetProcAddress(hMsvcrt, "_wcslwr_s");
2081 p_mbsupr_s = (void*)GetProcAddress(hMsvcrt, "_mbsupr_s");
2082 p_mbslwr_s = (void*)GetProcAddress(hMsvcrt, "_mbslwr_s");
2083 p_wctob = (void*)GetProcAddress(hMsvcrt, "wctob");
2084 p_tolower = (void*)GetProcAddress(hMsvcrt, "tolower");
2085
2086 /* MSVCRT memcpy behaves like memmove for overlapping moves,
2087 MFC42 CString::Insert seems to rely on that behaviour */
2088 strcpy(mem,xilstring);
2089 nLen=strlen(xilstring);
2090 pmemcpy(mem+5, mem,nLen+1);
2091 ok(pmemcmp(mem+5,xilstring, nLen) == 0,
2092 "Got result %s\n",mem+5);
2093
2094 /* Test _swab function */
2095 test_swab();
2096
2097 /* Test ismbblead*/
2098 test_mbcp();
2099 /* test _mbsspn */
2100 test_mbsspn();
2101 test_mbsspnp();
2102 /* test _strdup */
2103 test_strdup();
2104 test_strcpy_s();
2105 test_strcat_s();
2106 test__mbsnbcpy_s();
2107 test_mbcjisjms();
2108 test_mbctombb();
2109 test_ismbclegal();
2110 test_strtok();
2111 test_wcscpy_s();
2112 test__wcsupr_s();
2113 test_strtol();
2114 test_strnlen();
2115 test__strtoi64();
2116 test__strtod();
2117 test_mbstowcs();
2118 test_gcvt();
2119 test__itoa_s();
2120 test__strlwr_s();
2121 test_wcsncat_s();
2122 test__mbsnbcat_s();
2123 test__ultoa_s();
2124 test__wcslwr_s();
2125 test__mbsupr_s();
2126 test__mbslwr_s();
2127 test_wctob();
2128 test_tolower();
2129 }