2 * Unit test suite for string functions.
4 * Copyright 2004 Uwe Bonnes
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.
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.
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
21 #include "wine/test.h"
33 static char *buf_to_string(const unsigned char *bin, int len, int nr)
35 static char buf[2][1024];
39 for (i = 0; i < len; i++)
41 sprintf(w, "%02x ", (unsigned char)bin[i]);
47 static void __cdecl test_invalid_parameter_handler(const wchar_t *expression,
48 const wchar_t *function, const wchar_t *file,
49 unsigned line, uintptr_t arg)
51 /* we just ignore handler calls */
54 #define expect_eq(expr, value, type, format) { type ret = (expr); ok((value) == ret, #expr " expected " format " got " format "\n", value, ret); }
55 #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)); }
57 static void* (__cdecl *pmemcpy)(void *, const void *, size_t n);
58 static int* (__cdecl *pmemcmp)(void *, const void *, size_t n);
59 static int (__cdecl *pstrcpy_s)(char *dst, size_t len, const char *src);
60 static int (__cdecl *pstrcat_s)(char *dst, size_t len, const char *src);
61 static int (__cdecl *p_mbsnbcat_s)(unsigned char *dst, size_t size, const unsigned char *src, size_t count);
62 static int (__cdecl *p_mbsnbcpy_s)(unsigned char * dst, size_t size, const unsigned char * src, size_t count);
63 static int (__cdecl *p_wcscpy_s)(wchar_t *wcDest, size_t size, const wchar_t *wcSrc);
64 static int (__cdecl *p_wcsncat_s)(wchar_t *dst, size_t elem, const wchar_t *src, size_t count);
65 static int (__cdecl *p_wcsupr_s)(wchar_t *str, size_t size);
66 static size_t (__cdecl *p_strnlen)(const char *, size_t);
67 static __int64 (__cdecl *p_strtoi64)(const char *, char **, int);
68 static unsigned __int64 (__cdecl *p_strtoui64)(const char *, char **, int);
69 static int (__cdecl *pwcstombs_s)(size_t*,char*,size_t,const wchar_t*,size_t);
70 static int (__cdecl *pmbstowcs_s)(size_t*,wchar_t*,size_t,const char*,size_t);
71 static errno_t (__cdecl *p_gcvt_s)(char*,size_t,double,int);
72 static errno_t (__cdecl *p_itoa_s)(int,char*,size_t,int);
73 static errno_t (__cdecl *p_strlwr_s)(char*,size_t);
74 static errno_t (__cdecl *p_ultoa_s)(__msvcrt_ulong,char*,size_t,int);
75 static int *p__mb_cur_max;
76 static unsigned char *p_mbctype;
77 static _invalid_parameter_handler (__cdecl *p_set_invalid_parameter_handler)(_invalid_parameter_handler);
78 static int (__cdecl *p_wcslwr_s)(wchar_t*,size_t);
80 #define SETNOFAIL(x,y) x = (void*)GetProcAddress(hMsvcrt,y)
81 #define SET(x,y) SETNOFAIL(x,y); ok(x != NULL, "Export '%s' not found\n", y)
83 static HMODULE hMsvcrt;
85 static void test_swab( void ) {
86 char original[] = "BADCFEHGJILKNMPORQTSVUXWZY@#";
87 char expected1[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ@#";
88 char expected2[] = "ABCDEFGHIJKLMNOPQRSTUVWX$";
89 char expected3[] = "$";
96 /* Test 1 - normal even case */
97 memset(to,'$', sizeof(to));
98 memset(from,'@', sizeof(from));
100 memcpy(from, original, testsize);
101 _swab( from, to, testsize );
102 ok(memcmp(to,expected1,testsize) == 0, "Testing even size %d returned '%*.*s'\n", testsize, testsize, testsize, to);
104 /* Test 2 - uneven case */
105 memset(to,'$', sizeof(to));
106 memset(from,'@', sizeof(from));
108 memcpy(from, original, testsize);
109 _swab( from, to, testsize );
110 ok(memcmp(to,expected2,testsize) == 0, "Testing odd size %d returned '%*.*s'\n", testsize, testsize, testsize, to);
112 /* Test 3 - from = to */
113 memset(to,'$', sizeof(to));
114 memset(from,'@', sizeof(from));
116 memcpy(to, original, testsize);
117 _swab( to, to, testsize );
118 ok(memcmp(to,expected1,testsize) == 0, "Testing overlapped size %d returned '%*.*s'\n", testsize, testsize, testsize, to);
120 /* Test 4 - 1 bytes */
121 memset(to,'$', sizeof(to));
122 memset(from,'@', sizeof(from));
124 memcpy(from, original, testsize);
125 _swab( from, to, testsize );
126 ok(memcmp(to,expected3,testsize) == 0, "Testing small size %d returned '%*.*s'\n", testsize, testsize, testsize, to);
129 #if 0 /* use this to generate more tests */
131 static void test_codepage(int cp)
137 ok(_setmbcp(cp) == 0, "Couldn't set mbcp\n");
140 printf("static int result_cp_%d_mbctype[] = { ", cp);
141 for (i = 1; i < 257; i++)
143 if (p_mbctype[i] != prev)
145 printf("0x%x,%d, ", prev, count);
152 printf("0x%x,%d };\n", prev, count);
157 /* RLE-encoded mbctype tables for given codepages */
158 static int result_cp_932_mbctype[] = { 0x0,65, 0x8,1, 0x18,26, 0x8,6, 0x28,26, 0x8,4,
159 0x0,1, 0x8,1, 0xc,31, 0x8,1, 0xa,5, 0x9,58, 0xc,29, 0,3 };
160 static int result_cp_936_mbctype[] = { 0x0,65, 0x8,1, 0x18,26, 0x8,6, 0x28,26, 0x8,6,
162 static int result_cp_949_mbctype[] = { 0x0,66, 0x18,26, 0x8,6, 0x28,26, 0x8,6, 0xc,126,
164 static int result_cp_950_mbctype[] = { 0x0,65, 0x8,1, 0x18,26, 0x8,6, 0x28,26, 0x8,4,
165 0x0,2, 0x4,32, 0xc,94, 0,1 };
167 static void test_cp_table(int cp, int *result)
173 for (i = 0; i < 256; i++)
181 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);
186 #define test_codepage(num) test_cp_table(num, result_cp_##num##_mbctype);
190 static void test_mbcp(void)
192 int mb_orig_max = *p__mb_cur_max;
193 int curr_mbcp = _getmbcp();
194 unsigned char *mbstring = (unsigned char *)"\xb0\xb1\xb2 \xb3\xb4 \xb5"; /* incorrect string */
195 unsigned char *mbstring2 = (unsigned char *)"\xb0\xb1\xb2\xb3Q\xb4\xb5"; /* correct string */
196 unsigned char *mbsonlylead = (unsigned char *)"\xb0\0\xb1\xb2 \xb3";
197 unsigned char buf[16];
203 /* An SBCS codepage test. The ctype of characters on e.g. CP1252 or CP1250 differs slightly
204 * between versions of Windows. Also Windows 9x seems to ignore the codepage and always uses
205 * CP1252 (or the ACP?) so we test only a few ASCII characters */
207 expect_eq(p_mbctype[10], 0, char, "%x");
208 expect_eq(p_mbctype[50], 0, char, "%x");
209 expect_eq(p_mbctype[66], _SBUP, char, "%x");
210 expect_eq(p_mbctype[100], _SBLOW, char, "%x");
211 expect_eq(p_mbctype[128], 0, char, "%x");
213 expect_eq(p_mbctype[10], 0, char, "%x");
214 expect_eq(p_mbctype[50], 0, char, "%x");
215 expect_eq(p_mbctype[66], _SBUP, char, "%x");
216 expect_eq(p_mbctype[100], _SBLOW, char, "%x");
217 expect_eq(p_mbctype[128], 0, char, "%x");
219 /* double byte code pages */
226 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);
227 ok(_ismbblead('\354'), "\354 should be a lead byte\n");
228 ok(_ismbblead(' ') == FALSE, "' ' should not be a lead byte\n");
229 ok(_ismbblead(0x1234b0), "0x1234b0 should not be a lead byte\n");
230 ok(_ismbblead(0x123420) == FALSE, "0x123420 should not be a lead byte\n");
231 ok(_ismbbtrail('\xb0'), "\xa0 should be a trail byte\n");
232 ok(_ismbbtrail(' ') == FALSE, "' ' should not be a trail byte\n");
235 expect_eq(_ismbslead(mbstring, &mbstring[0]), -1, int, "%d");
236 expect_eq(_ismbslead(mbstring, &mbstring[1]), FALSE, int, "%d");
237 expect_eq(_ismbslead(mbstring, &mbstring[2]), -1, int, "%d");
238 expect_eq(_ismbslead(mbstring, &mbstring[3]), FALSE, int, "%d");
239 expect_eq(_ismbslead(mbstring, &mbstring[4]), -1, int, "%d");
240 expect_eq(_ismbslead(mbstring, &mbstring[5]), FALSE, int, "%d");
241 expect_eq(_ismbslead(mbstring, &mbstring[6]), FALSE, int, "%d");
242 expect_eq(_ismbslead(mbstring, &mbstring[7]), -1, int, "%d");
243 expect_eq(_ismbslead(mbstring, &mbstring[8]), FALSE, int, "%d");
245 expect_eq(_ismbslead(mbsonlylead, &mbsonlylead[0]), -1, int, "%d");
246 expect_eq(_ismbslead(mbsonlylead, &mbsonlylead[1]), FALSE, int, "%d");
247 expect_eq(_ismbslead(mbsonlylead, &mbsonlylead[2]), FALSE, int, "%d");
248 expect_eq(_ismbslead(mbsonlylead, &mbsonlylead[5]), FALSE, int, "%d");
251 expect_eq(_ismbstrail(mbstring, &mbstring[0]), FALSE, int, "%d");
252 expect_eq(_ismbstrail(mbstring, &mbstring[1]), -1, int, "%d");
253 expect_eq(_ismbstrail(mbstring, &mbstring[2]), FALSE, int, "%d");
254 expect_eq(_ismbstrail(mbstring, &mbstring[3]), -1, int, "%d");
255 expect_eq(_ismbstrail(mbstring, &mbstring[4]), FALSE, int, "%d");
256 expect_eq(_ismbstrail(mbstring, &mbstring[5]), -1, int, "%d");
257 expect_eq(_ismbstrail(mbstring, &mbstring[6]), FALSE, int, "%d");
258 expect_eq(_ismbstrail(mbstring, &mbstring[7]), FALSE, int, "%d");
259 expect_eq(_ismbstrail(mbstring, &mbstring[8]), -1, int, "%d");
261 expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[0]), FALSE, int, "%d");
262 expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[1]), -1, int, "%d");
263 expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[2]), FALSE, int, "%d");
264 expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[3]), FALSE, int, "%d");
265 expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[4]), FALSE, int, "%d");
266 expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[5]), FALSE, int, "%d");
269 expect_eq(_mbsbtype(mbstring, 0), _MBC_LEAD, int, "%d");
270 expect_eq(_mbsbtype(mbstring, 1), _MBC_TRAIL, int, "%d");
271 expect_eq(_mbsbtype(mbstring, 2), _MBC_LEAD, int, "%d");
272 expect_eq(_mbsbtype(mbstring, 3), _MBC_ILLEGAL, int, "%d");
273 expect_eq(_mbsbtype(mbstring, 4), _MBC_LEAD, int, "%d");
274 expect_eq(_mbsbtype(mbstring, 5), _MBC_TRAIL, int, "%d");
275 expect_eq(_mbsbtype(mbstring, 6), _MBC_SINGLE, int, "%d");
276 expect_eq(_mbsbtype(mbstring, 7), _MBC_LEAD, int, "%d");
277 expect_eq(_mbsbtype(mbstring, 8), _MBC_ILLEGAL, int, "%d");
279 expect_eq(_mbsbtype(mbsonlylead, 0), _MBC_LEAD, int, "%d");
280 expect_eq(_mbsbtype(mbsonlylead, 1), _MBC_ILLEGAL, int, "%d");
281 expect_eq(_mbsbtype(mbsonlylead, 2), _MBC_ILLEGAL, int, "%d");
282 expect_eq(_mbsbtype(mbsonlylead, 3), _MBC_ILLEGAL, int, "%d");
283 expect_eq(_mbsbtype(mbsonlylead, 4), _MBC_ILLEGAL, int, "%d");
284 expect_eq(_mbsbtype(mbsonlylead, 5), _MBC_ILLEGAL, int, "%d");
287 expect_eq(_mbsnextc(mbstring), 0xb0b1, int, "%x");
288 expect_eq(_mbsnextc(&mbstring[2]), 0xb220, int, "%x"); /* lead + invalid tail */
289 expect_eq(_mbsnextc(&mbstring[3]), 0x20, int, "%x"); /* single char */
291 /* _mbclen/_mbslen */
292 expect_eq(_mbclen(mbstring), 2, int, "%d");
293 expect_eq(_mbclen(&mbstring[2]), 2, int, "%d");
294 expect_eq(_mbclen(&mbstring[3]), 1, int, "%d");
295 expect_eq(_mbslen(mbstring2), 4, int, "%d");
296 expect_eq(_mbslen(mbsonlylead), 0, int, "%d"); /* lead + NUL not counted as character */
297 expect_eq(_mbslen(mbstring), 4, int, "%d"); /* lead + invalid trail counted */
299 /* _mbccpy/_mbsncpy */
300 memset(buf, 0xff, sizeof(buf));
301 _mbccpy(buf, mbstring);
302 expect_bin(buf, "\xb0\xb1\xff", 3);
304 memset(buf, 0xff, sizeof(buf));
305 _mbsncpy(buf, mbstring, 1);
306 expect_bin(buf, "\xb0\xb1\xff", 3);
307 memset(buf, 0xff, sizeof(buf));
308 _mbsncpy(buf, mbstring, 2);
309 expect_bin(buf, "\xb0\xb1\xb2 \xff", 5);
310 memset(buf, 0xff, sizeof(buf));
311 _mbsncpy(buf, mbstring, 3);
312 expect_bin(buf, "\xb0\xb1\xb2 \xb3\xb4\xff", 7);
313 memset(buf, 0xff, sizeof(buf));
314 _mbsncpy(buf, mbstring, 4);
315 expect_bin(buf, "\xb0\xb1\xb2 \xb3\xb4 \xff", 8);
316 memset(buf, 0xff, sizeof(buf));
317 _mbsncpy(buf, mbstring, 5);
318 expect_bin(buf, "\xb0\xb1\xb2 \xb3\xb4 \0\0\xff", 10);
319 memset(buf, 0xff, sizeof(buf));
320 _mbsncpy(buf, mbsonlylead, 6);
321 expect_bin(buf, "\0\0\0\0\0\0\0\xff", 8);
323 memset(buf, 0xff, sizeof(buf));
324 _mbsnbcpy(buf, mbstring2, 2);
325 expect_bin(buf, "\xb0\xb1\xff", 3);
326 _mbsnbcpy(buf, mbstring2, 3);
327 expect_bin(buf, "\xb0\xb1\0\xff", 4);
328 _mbsnbcpy(buf, mbstring2, 4);
329 expect_bin(buf, "\xb0\xb1\xb2\xb3\xff", 5);
330 memset(buf, 0xff, sizeof(buf));
331 _mbsnbcpy(buf, mbsonlylead, 5);
332 expect_bin(buf, "\0\0\0\0\0\xff", 6);
335 step = _mbsinc(mbstring) - mbstring;
336 ok(step == 2, "_mbsinc adds %d (exp. 2)\n", step);
337 step = _mbsinc(&mbstring[2]) - &mbstring[2]; /* lead + invalid tail */
338 ok(step == 2, "_mbsinc adds %d (exp. 2)\n", step);
340 step = _mbsninc(mbsonlylead, 1) - mbsonlylead;
341 ok(step == 0, "_mbsninc adds %d (exp. 0)\n", step);
342 step = _mbsninc(mbsonlylead, 2) - mbsonlylead; /* lead + NUL byte + lead + char */
343 ok(step == 0, "_mbsninc adds %d (exp. 0)\n", step);
344 step = _mbsninc(mbstring2, 0) - mbstring2;
345 ok(step == 0, "_mbsninc adds %d (exp. 2)\n", step);
346 step = _mbsninc(mbstring2, 1) - mbstring2;
347 ok(step == 2, "_mbsninc adds %d (exp. 2)\n", step);
348 step = _mbsninc(mbstring2, 2) - mbstring2;
349 ok(step == 4, "_mbsninc adds %d (exp. 4)\n", step);
350 step = _mbsninc(mbstring2, 3) - mbstring2;
351 ok(step == 5, "_mbsninc adds %d (exp. 5)\n", step);
352 step = _mbsninc(mbstring2, 4) - mbstring2;
353 ok(step == 7, "_mbsninc adds %d (exp. 7)\n", step);
354 step = _mbsninc(mbstring2, 5) - mbstring2;
355 ok(step == 7, "_mbsninc adds %d (exp. 7)\n", step);
356 step = _mbsninc(mbstring2, 17) - mbstring2;
357 ok(step == 7, "_mbsninc adds %d (exp. 7)\n", step);
359 /* functions that depend on locale codepage, not mbcp.
360 * we hope the current locale to be SBCS because setlocale(LC_ALL, ".1252") seems not to work yet
361 * (as of Wine 0.9.43)
363 GetCPInfo(GetACP(), &cp_info);
364 if (cp_info.MaxCharSize == 1)
366 expect_eq(mblen((char *)mbstring, 3), 1, int, "%x");
367 expect_eq(_mbstrlen((char *)mbstring2), 7, int, "%d");
370 skip("Current locale has double-byte charset - could lead to false positives\n");
373 expect_eq(_ismbblead(0x80), 0, int, "%d");
375 expect_eq(_ismbblead(0x81), 1, int, "%d");
376 expect_eq(_ismbblead(0x83), 1, int, "%d");
378 expect_eq(_ismbblead(0x84), 1, int, "%d");
379 expect_eq(_ismbblead(0xd3), 1, int, "%d");
380 expect_eq(_ismbblead(0xd7), 0, int, "%d");
382 expect_eq(_ismbblead(0xd8), 1, int, "%d");
384 expect_eq(_ismbblead(0xd9), 1, int, "%d");
386 expect_eq(_ismbbtrail(0x30), 0, int, "%d");
387 expect_eq(_ismbbtrail(0x31), 1, int, "%d");
388 expect_eq(_ismbbtrail(0x7e), 1, int, "%d");
389 expect_eq(_ismbbtrail(0x7f), 0, int, "%d");
390 expect_eq(_ismbbtrail(0x80), 0, int, "%d");
391 expect_eq(_ismbbtrail(0x81), 1, int, "%d");
392 expect_eq(_ismbbtrail(0xfe), 1, int, "%d");
393 expect_eq(_ismbbtrail(0xff), 0, int, "%d");
398 static void test_mbsspn( void)
400 unsigned char str1[]="cabernet";
401 unsigned char str2[]="shiraz";
402 unsigned char set[]="abc";
403 unsigned char empty[]="";
405 ret=_mbsspn( str1, set);
406 ok( ret==3, "_mbsspn returns %d should be 3\n", ret);
407 ret=_mbsspn( str2, set);
408 ok( ret==0, "_mbsspn returns %d should be 0\n", ret);
409 ret=_mbsspn( str1, empty);
410 ok( ret==0, "_mbsspn returns %d should be 0\n", ret);
413 static void test_mbsspnp( void)
415 unsigned char str1[]="cabernet";
416 unsigned char str2[]="shiraz";
417 unsigned char set[]="abc";
418 unsigned char empty[]="";
419 unsigned char full[]="abcenrt";
421 ret=_mbsspnp( str1, set);
422 ok( ret[0]=='e', "_mbsspnp returns %c should be e\n", ret[0]);
423 ret=_mbsspnp( str2, set);
424 ok( ret[0]=='s', "_mbsspnp returns %c should be s\n", ret[0]);
425 ret=_mbsspnp( str1, empty);
426 ok( ret[0]=='c', "_mbsspnp returns %c should be c\n", ret[0]);
427 ret=_mbsspnp( str1, full);
428 ok( ret==NULL, "_mbsspnp returns %p should be NULL\n", ret);
431 static void test_strdup(void)
435 ok( str == 0, "strdup returns %s should be 0\n", str);
439 static void test_strcpy_s(void)
442 const char *small = "small";
443 const char *big = "atoolongstringforthislittledestination";
448 skip("strcpy_s not found\n");
452 memset(dest, 'X', sizeof(dest));
453 ret = pstrcpy_s(dest, sizeof(dest), small);
454 ok(ret == 0, "Copying a string into a big enough destination returned %d, expected 0\n", ret);
455 ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == 'l' &&
456 dest[4] == 'l' && dest[5] == '\0'&& dest[6] == 'X' && dest[7] == 'X',
457 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
458 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
460 memset(dest, 'X', sizeof(dest));
461 ret = pstrcpy_s(dest, 0, big);
462 ok(ret == EINVAL, "Copying into a destination of size 0 returned %d, expected EINVAL\n", ret);
463 ok(dest[0] == 'X' && dest[1] == 'X' && dest[2] == 'X' && dest[3] == 'X' &&
464 dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
465 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
466 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
467 ret = pstrcpy_s(dest, 0, NULL);
468 ok(ret == EINVAL, "Copying into a destination of size 0 returned %d, expected EINVAL\n", ret);
469 ok(dest[0] == 'X' && dest[1] == 'X' && dest[2] == 'X' && dest[3] == 'X' &&
470 dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
471 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
472 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
474 memset(dest, 'X', sizeof(dest));
475 ret = pstrcpy_s(dest, sizeof(dest), big);
476 ok(ret == ERANGE, "Copying a big string in a small location returned %d, expected ERANGE\n", ret);
477 ok(dest[0] == '\0'&& dest[1] == 't' && dest[2] == 'o' && dest[3] == 'o' &&
478 dest[4] == 'l' && dest[5] == 'o' && dest[6] == 'n' && dest[7] == 'g',
479 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
480 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
482 memset(dest, 'X', sizeof(dest));
483 ret = pstrcpy_s(dest, sizeof(dest), NULL);
484 ok(ret == EINVAL, "Copying from a NULL source string returned %d, expected EINVAL\n", ret);
485 ok(dest[0] == '\0'&& dest[1] == 'X' && dest[2] == 'X' && dest[3] == 'X' &&
486 dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
487 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
488 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
490 ret = pstrcpy_s(NULL, sizeof(dest), small);
491 ok(ret == EINVAL, "Copying a big string a NULL dest returned %d, expected EINVAL\n", ret);
494 static void test_strcat_s(void)
497 const char *small = "sma";
502 skip("strcat_s not found\n");
506 memset(dest, 'X', sizeof(dest));
508 ret = pstrcat_s(dest, sizeof(dest), small);
509 ok(ret == 0, "strcat_s: Copying a string into a big enough destination returned %d, expected 0\n", ret);
510 ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == '\0'&&
511 dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
512 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
513 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
514 ret = pstrcat_s(dest, sizeof(dest), small);
515 ok(ret == 0, "strcat_s: Attaching a string to a big enough destination returned %d, expected 0\n", ret);
516 ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == 's' &&
517 dest[4] == 'm' && dest[5] == 'a' && dest[6] == '\0'&& dest[7] == 'X',
518 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
519 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
521 ret = pstrcat_s(dest, sizeof(dest), small);
522 ok(ret == ERANGE, "strcat_s: Attaching a string to a filled up destination returned %d, expected ERANGE\n", ret);
523 ok(dest[0] == '\0'&& dest[1] == 'm' && dest[2] == 'a' && dest[3] == 's' &&
524 dest[4] == 'm' && dest[5] == 'a' && dest[6] == 's' && dest[7] == 'm',
525 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
526 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
528 memset(dest, 'X', sizeof(dest));
532 ret = pstrcat_s(dest, 0, small);
533 ok(ret == EINVAL, "strcat_s: Source len = 0 returned %d, expected EINVAL\n", ret);
534 ok(dest[0] == 'a' && dest[1] == '\0'&& dest[2] == 'X' && dest[3] == 'X' &&
535 dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
536 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
537 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
539 ret = pstrcat_s(dest, 0, NULL);
540 ok(ret == EINVAL, "strcat_s: len = 0 and src = NULL returned %d, expected EINVAL\n", ret);
541 ok(dest[0] == 'a' && dest[1] == '\0'&& dest[2] == 'X' && dest[3] == 'X' &&
542 dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
543 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
544 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
546 ret = pstrcat_s(dest, sizeof(dest), NULL);
547 ok(ret == EINVAL, "strcat_s: Sourcing from NULL returned %d, expected EINVAL\n", ret);
548 ok(dest[0] == '\0'&& dest[1] == '\0'&& dest[2] == 'X' && dest[3] == 'X' &&
549 dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
550 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
551 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
553 ret = pstrcat_s(NULL, sizeof(dest), small);
554 ok(ret == EINVAL, "strcat_s: Writing to a NULL string returned %d, expected EINVAL\n", ret);
557 static void test__mbsnbcpy_s(void)
559 unsigned char dest[8];
560 const unsigned char big[] = "atoolongstringforthislittledestination";
561 const unsigned char small[] = "small";
566 skip("_mbsnbcpy_s not found\n");
570 memset(dest, 'X', sizeof(dest));
571 ret = p_mbsnbcpy_s(dest, sizeof(dest), small, sizeof(small));
572 ok(ret == 0, "_mbsnbcpy_s: Copying a string into a big enough destination returned %d, expected 0\n", ret);
573 ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == 'l' &&
574 dest[4] == 'l' && dest[5] == '\0'&& dest[6] == 'X' && dest[7] == 'X',
575 "Unexpected return data from _mbsnbcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
576 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
579 memset(dest, 'X', sizeof(dest));
580 ret = p_mbsnbcpy_s(dest, sizeof(dest) - 2, big, sizeof(small));
581 ok(ret == ERANGE, "_mbsnbcpy_s: Copying a too long string returned %d, expected ERANGE\n", ret);
582 ok(dest[0] == '\0'&& dest[1] == 't' && dest[2] == 'o' && dest[3] == 'o' &&
583 dest[4] == 'l' && dest[5] == 'o' && 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]);
587 memset(dest, 'X', sizeof(dest));
588 ret = p_mbsnbcpy_s(dest, sizeof(dest) - 2, big, 4);
589 ok(ret == 0, "_mbsnbcpy_s: Copying a too long string with a count cap returned %d, expected 0\n", ret);
590 ok(dest[0] == 'a' && dest[1] == 't' && dest[2] == 'o' && dest[3] == 'o' &&
591 dest[4] == '\0'&& dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
592 "Unexpected return data from _mbsnbcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
593 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
595 memset(dest, 'X', sizeof(dest));
596 ret = p_mbsnbcpy_s(dest, sizeof(dest) - 2, small, sizeof(small) + 10);
597 ok(ret == 0, "_mbsnbcpy_s: Copying more data than the source string len returned %d, expected 0\n", ret);
598 ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == 'l' &&
599 dest[4] == 'l' && dest[5] == '\0'&& dest[6] == 'X' && dest[7] == 'X',
600 "Unexpected return data from _mbsnbcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
601 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
604 static void test_wcscpy_s(void)
606 static const WCHAR szLongText[] = { 'T','h','i','s','A','L','o','n','g','s','t','r','i','n','g',0 };
607 static WCHAR szDest[18];
608 static WCHAR szDestShort[8];
613 skip("wcscpy_s not found\n");
618 ret = p_wcscpy_s(NULL, 18, szLongText);
619 ok(ret == EINVAL, "p_wcscpy_s expect EINVAL got %d\n", ret);
621 /* Test NULL Source */
623 ret = p_wcscpy_s(szDest, 18, NULL);
624 ok(ret == EINVAL, "expected EINVAL got %d\n", ret);
625 ok(szDest[0] == 0, "szDest[0] not 0\n");
627 /* Test invalid size */
629 ret = p_wcscpy_s(szDest, 0, szLongText);
630 /* Later versions changed the return value for this case to EINVAL,
631 * and don't modify the result if the dest size is 0.
633 ok(ret == ERANGE || ret == EINVAL, "expected ERANGE/EINVAL got %d\n", ret);
634 ok(szDest[0] == 0 || ret == EINVAL, "szDest[0] not 0\n");
636 /* Copy same buffer size */
637 ret = p_wcscpy_s(szDest, 18, szLongText);
638 ok(ret == 0, "expected 0 got %d\n", ret);
639 ok(lstrcmpW(szDest, szLongText) == 0, "szDest != szLongText\n");
641 /* Copy smaller buffer size */
643 ret = p_wcscpy_s(szDestShort, 8, szLongText);
644 ok(ret == ERANGE || ret == EINVAL, "expected ERANGE/EINVAL got %d\n", ret);
645 ok(szDestShort[0] == 0, "szDestShort[0] not 0\n");
648 static void test__wcsupr_s(void)
650 static const WCHAR mixedString[] = {'M', 'i', 'X', 'e', 'D', 'l', 'o', 'w',
651 'e', 'r', 'U', 'P', 'P', 'E', 'R', 0};
652 static const WCHAR expectedString[] = {'M', 'I', 'X', 'E', 'D', 'L', 'O',
653 'W', 'E', 'R', 'U', 'P', 'P', 'E',
655 WCHAR testBuffer[2*sizeof(mixedString)/sizeof(WCHAR)];
660 win_skip("_wcsupr_s not found\n");
664 /* Test NULL input string and invalid size. */
666 ret = p_wcsupr_s(NULL, 0);
667 ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
668 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
670 /* Test NULL input string and valid size. */
672 ret = p_wcsupr_s(NULL, sizeof(testBuffer)/sizeof(WCHAR));
673 ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
674 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
676 /* Test empty string with zero size. */
678 testBuffer[0] = '\0';
679 ret = p_wcsupr_s(testBuffer, 0);
680 ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
681 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
682 ok(testBuffer[0] == '\0', "Expected the buffer to be unchanged\n");
684 /* Test empty string with size of one. */
685 testBuffer[0] = '\0';
686 ret = p_wcsupr_s(testBuffer, 1);
687 ok(ret == 0, "Expected _wcsupr_s to succeed, got %d\n", ret);
688 ok(testBuffer[0] == '\0', "Expected the buffer to be unchanged\n");
690 /* Test one-byte buffer with zero size. */
693 ret = p_wcsupr_s(testBuffer, 0);
694 ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
695 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
696 ok(testBuffer[0] == '\0', "Expected the first buffer character to be null\n");
698 /* Test one-byte buffer with size of one. */
701 ret = p_wcsupr_s(testBuffer, 1);
702 ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
703 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
704 ok(testBuffer[0] == '\0', "Expected the first buffer character to be null\n");
706 /* Test invalid size. */
707 wcscpy(testBuffer, mixedString);
709 ret = p_wcsupr_s(testBuffer, 0);
710 ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
711 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
712 ok(testBuffer[0] == '\0', "Expected the first buffer character to be null\n");
714 /* Test normal string uppercasing. */
715 wcscpy(testBuffer, mixedString);
716 ret = p_wcsupr_s(testBuffer, sizeof(mixedString)/sizeof(WCHAR));
717 ok(ret == 0, "Expected _wcsupr_s to succeed, got %d\n", ret);
718 ok(!wcscmp(testBuffer, expectedString), "Expected the string to be fully upper-case\n");
720 /* Test uppercasing with a shorter buffer size count. */
721 wcscpy(testBuffer, mixedString);
723 ret = p_wcsupr_s(testBuffer, sizeof(mixedString)/sizeof(WCHAR) - 1);
724 ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
725 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
726 ok(testBuffer[0] == '\0', "Expected the first buffer character to be null\n");
728 /* Test uppercasing with a longer buffer size count. */
729 wcscpy(testBuffer, mixedString);
730 ret = p_wcsupr_s(testBuffer, sizeof(testBuffer)/sizeof(WCHAR));
731 ok(ret == 0, "Expected _wcsupr_s to succeed, got %d\n", ret);
732 ok(!wcscmp(testBuffer, expectedString), "Expected the string to be fully upper-case\n");
735 static void test__wcslwr_s(void)
737 static const WCHAR mixedString[] = {'M', 'i', 'X', 'e', 'D', 'l', 'o', 'w',
738 'e', 'r', 'U', 'P', 'P', 'E', 'R', 0};
739 static const WCHAR expectedString[] = {'m', 'i', 'x', 'e', 'd', 'l', 'o',
740 'w', 'e', 'r', 'u', 'p', 'p', 'e',
742 WCHAR buffer[2*sizeof(mixedString)/sizeof(WCHAR)];
747 win_skip("_wcslwr_s not found\n");
751 /* Test NULL input string and invalid size. */
753 ret = p_wcslwr_s(NULL, 0);
754 ok(ret == EINVAL, "expected EINVAL, got %d\n", ret);
755 ok(errno == EINVAL, "expected errno EINVAL, got %d\n", errno);
757 /* Test NULL input string and valid size. */
759 ret = p_wcslwr_s(NULL, sizeof(buffer)/sizeof(wchar_t));
760 ok(ret == EINVAL, "expected EINVAL, got %d\n", ret);
761 ok(errno == EINVAL, "expected errno EINVAL, got %d\n", errno);
763 /* Test empty string with zero size. */
766 ret = p_wcslwr_s(buffer, 0);
767 ok(ret == EINVAL, "expected EINVAL, got %d\n", ret);
768 ok(errno == EINVAL, "expected errno EINVAL, got %d\n", errno);
769 ok(buffer[0] == 0, "expected empty string\n");
771 /* Test empty string with size of one. */
773 ret = p_wcslwr_s(buffer, 1);
774 ok(ret == 0, "got %d\n", ret);
775 ok(buffer[0] == 0, "expected buffer to be unchanged\n");
777 /* Test one-byte buffer with zero size. */
780 ret = p_wcslwr_s(buffer, 0);
781 ok(ret == EINVAL, "expected EINVAL, got %d\n", ret);
782 ok(errno == EINVAL, "expected errno to be EINVAL, got %d\n", errno);
783 ok(buffer[0] == '\0', "expected empty string\n");
785 /* Test one-byte buffer with size of one. */
788 ret = p_wcslwr_s(buffer, 1);
789 ok(ret == EINVAL, "expected EINVAL, got %d\n", ret);
790 ok(errno == EINVAL, "expected errno to be EINVAL, got %d\n", errno);
791 ok(buffer[0] == '\0', "expected empty string\n");
793 /* Test invalid size. */
794 wcscpy(buffer, mixedString);
796 ret = p_wcslwr_s(buffer, 0);
797 ok(ret == EINVAL, "Expected EINVAL, got %d\n", ret);
798 ok(errno == EINVAL, "expected errno to be EINVAL, got %d\n", errno);
799 ok(buffer[0] == '\0', "expected empty string\n");
801 /* Test normal string uppercasing. */
802 wcscpy(buffer, mixedString);
803 ret = p_wcslwr_s(buffer, sizeof(mixedString)/sizeof(WCHAR));
804 ok(ret == 0, "expected 0, got %d\n", ret);
805 ok(!wcscmp(buffer, expectedString), "expected lowercase\n");
807 /* Test uppercasing with a shorter buffer size count. */
808 wcscpy(buffer, mixedString);
810 ret = p_wcslwr_s(buffer, sizeof(mixedString)/sizeof(WCHAR) - 1);
811 ok(ret == EINVAL, "expected EINVAL, got %d\n", ret);
812 ok(errno == EINVAL, "expected errno to be EINVAL, got %d\n", errno);
813 ok(buffer[0] == '\0', "expected empty string\n");
815 /* Test uppercasing with a longer buffer size count. */
816 wcscpy(buffer, mixedString);
817 ret = p_wcslwr_s(buffer, sizeof(buffer)/sizeof(WCHAR));
818 ok(ret == 0, "expected 0, got %d\n", ret);
819 ok(!wcscmp(buffer, expectedString), "expected lowercase\n");
822 static void test_mbcjisjms(void)
824 /* List of value-pairs to test. The test assumes the last pair to be {0, ..} */
825 unsigned int jisjms[][2] = { {0x2020, 0}, {0x2021, 0}, {0x2120, 0}, {0x2121, 0x8140},
826 {0x7f7f, 0}, {0x7f7e, 0}, {0x7e7f, 0}, {0x7e7e, 0xeffc},
827 {0x2121FFFF, 0}, {0x2223, 0x81a1}, {0x237e, 0x829e}, {0, 0}};
828 unsigned int ret, exp, i;
833 ret = _mbcjistojms(jisjms[i][0]);
835 if(_getmbcp() == 932) /* Japanese codepage? */
838 exp = jisjms[i][0]; /* If not, no conversion */
840 ok(ret == exp, "Expected 0x%x, got 0x%x\n", exp, ret);
841 } while(jisjms[i++][0] != 0);
844 static void test_mbctombb(void)
846 static const unsigned int mbcmbb_932[][2] = {
847 {0x829e, 0x829e}, {0x829f, 0xa7}, {0x82f1, 0xdd}, {0x82f2, 0x82f2},
848 {0x833f, 0x833f}, {0x8340, 0xa7}, {0x837e, 0xd0}, {0x837f, 0x837f},
849 {0x8380, 0xd1}, {0x8396, 0xb9}, {0x8397, 0x8397}, {0x813f, 0x813f},
850 {0x8140, 0x20}, {0x814c, 0x814c}, {0x814f, 0x5e}, {0x8197, 0x40},
851 {0x8198, 0x8198}, {0x8258, 0x39}, {0x8259, 0x8259}, {0x825f, 0x825f},
852 {0x8260, 0x41}, {0x82f1, 0xdd}, {0x82f2, 0x82f2}, {0,0}};
853 unsigned int exp, ret, i;
854 unsigned int prev_cp = _getmbcp();
857 for (i = 0; mbcmbb_932[i][0] != 0; i++)
859 ret = _mbctombb(mbcmbb_932[i][0]);
860 exp = mbcmbb_932[i][1];
861 ok(ret == exp, "Expected 0x%x, got 0x%x\n", exp, ret);
866 static void test_ismbclegal(void) {
867 unsigned int prev_cp = _getmbcp();
871 _setmbcp(932); /* Japanese */
873 for(i = 0; i < 0x10000; i++) {
874 ret = _ismbclegal(i);
875 exp = ((HIBYTE(i) >= 0x81 && HIBYTE(i) <= 0x9F) ||
876 (HIBYTE(i) >= 0xE0 && HIBYTE(i) <= 0xFC)) &&
877 ((LOBYTE(i) >= 0x40 && LOBYTE(i) <= 0x7E) ||
878 (LOBYTE(i) >= 0x80 && LOBYTE(i) <= 0xFC));
884 ok(!err, "_ismbclegal (932) : Expected 0x%x, got 0x%x (0x%x)\n", exp, ret, i);
885 _setmbcp(936); /* Chinese (GBK) */
887 for(i = 0; i < 0x10000; i++) {
888 ret = _ismbclegal(i);
889 exp = HIBYTE(i) >= 0x81 && HIBYTE(i) <= 0xFE &&
890 LOBYTE(i) >= 0x40 && LOBYTE(i) <= 0xFE;
896 ok(!err, "_ismbclegal (936) : Expected 0x%x, got 0x%x (0x%x)\n", exp, ret, i);
897 _setmbcp(949); /* Korean */
899 for(i = 0; i < 0x10000; i++) {
900 ret = _ismbclegal(i);
901 exp = HIBYTE(i) >= 0x81 && HIBYTE(i) <= 0xFE &&
902 LOBYTE(i) >= 0x41 && LOBYTE(i) <= 0xFE;
908 ok(!err, "_ismbclegal (949) : Expected 0x%x, got 0x%x (0x%x)\n", exp, ret, i);
909 _setmbcp(950); /* Chinese (Big5) */
911 for(i = 0; i < 0x10000; i++) {
912 ret = _ismbclegal(i);
913 exp = HIBYTE(i) >= 0x81 && HIBYTE(i) <= 0xFE &&
914 ((LOBYTE(i) >= 0x40 && LOBYTE(i) <= 0x7E) ||
915 (LOBYTE(i) >= 0xA1 && LOBYTE(i) <= 0xFE));
921 ok(!err, "_ismbclegal (950) : Expected 0x%x, got 0x%x (0x%x)\n", exp, ret, i);
922 _setmbcp(1361); /* Korean (Johab) */
924 for(i = 0; i < 0x10000; i++) {
925 ret = _ismbclegal(i);
926 exp = ((HIBYTE(i) >= 0x81 && HIBYTE(i) <= 0xD3) ||
927 (HIBYTE(i) >= 0xD8 && HIBYTE(i) <= 0xF9)) &&
928 ((LOBYTE(i) >= 0x31 && LOBYTE(i) <= 0x7E) ||
929 (LOBYTE(i) >= 0x81 && LOBYTE(i) <= 0xFE)) &&
936 todo_wine ok(!err, "_ismbclegal (1361) : Expected 0x%x, got 0x%x (0x%x)\n", exp, ret, i);
941 static const struct {
943 const char* delimiter;
944 int exp_offsetret1; /* returned offset from string after first call to strtok()
946 int exp_offsetret2; /* returned offset from string after second call to strtok()
948 int exp_offsetret3; /* returned offset from string after third call to strtok()
950 } testcases_strtok[] = {
951 { "red cabernet", " ", 0, 4, -1 },
952 { "sparkling white riesling", " ", 0, 10, 16 },
953 { " pale cream sherry", "e ", 1, 6, 9 },
958 static void test_strtok(void)
963 for( i = 0; testcases_strtok[i].string; i++){
964 strcpy( teststr, testcases_strtok[i].string);
965 strret = strtok( teststr, testcases_strtok[i].delimiter);
966 ok( (int)(strret - teststr) == testcases_strtok[i].exp_offsetret1 ||
967 (!strret && testcases_strtok[i].exp_offsetret1 == -1),
968 "string (%p) \'%s\' return %p\n",
969 teststr, testcases_strtok[i].string, strret);
970 if( !strret) continue;
971 strret = strtok( NULL, testcases_strtok[i].delimiter);
972 ok( (int)(strret - teststr) == testcases_strtok[i].exp_offsetret2 ||
973 (!strret && testcases_strtok[i].exp_offsetret2 == -1),
974 "second call string (%p) \'%s\' return %p\n",
975 teststr, testcases_strtok[i].string, strret);
976 if( !strret) continue;
977 strret = strtok( NULL, testcases_strtok[i].delimiter);
978 ok( (int)(strret - teststr) == testcases_strtok[i].exp_offsetret3 ||
979 (!strret && testcases_strtok[i].exp_offsetret3 == -1),
980 "third call string (%p) \'%s\' return %p\n",
981 teststr, testcases_strtok[i].string, strret);
985 static void test_strtol(void)
991 /* errno is only set in case of error, so reset errno to EBADF to check for errno modification */
992 /* errno is modified on W2K8+ */
994 l = strtol("-1234", &e, 0);
995 ok(l==-1234, "wrong value %d\n", l);
996 ok(errno == EBADF || broken(errno == 0), "wrong errno %d\n", errno);
998 ul = strtoul("1234", &e, 0);
999 ok(ul==1234, "wrong value %u\n", ul);
1000 ok(errno == EBADF || broken(errno == 0), "wrong errno %d\n", errno);
1003 l = strtol("2147483647L", &e, 0);
1004 ok(l==2147483647, "wrong value %d\n", l);
1005 ok(errno == EBADF || broken(errno == 0), "wrong errno %d\n", errno);
1007 l = strtol("-2147483648L", &e, 0);
1008 ok(l==-2147483647L - 1, "wrong value %d\n", l);
1009 ok(errno == EBADF || broken(errno == 0), "wrong errno %d\n", errno);
1011 ul = strtoul("4294967295UL", &e, 0);
1012 ok(ul==4294967295ul, "wrong value %u\n", ul);
1013 ok(errno == EBADF || broken(errno == 0), "wrong errno %d\n", errno);
1016 l = strtol("9223372036854775807L", &e, 0);
1017 ok(l==2147483647, "wrong value %d\n", l);
1018 ok(errno == ERANGE, "wrong errno %d\n", errno);
1020 ul = strtoul("9223372036854775807L", &e, 0);
1021 ok(ul==4294967295ul, "wrong value %u\n", ul);
1022 ok(errno == ERANGE, "wrong errno %d\n", errno);
1025 static void test_strnlen(void)
1027 static const char str[] = "string";
1031 win_skip("strnlen not found\n");
1035 res = p_strnlen(str, 20);
1036 ok(res == 6, "Returned length = %d\n", (int)res);
1038 res = p_strnlen(str, 3);
1039 ok(res == 3, "Returned length = %d\n", (int)res);
1041 res = p_strnlen(NULL, 0);
1042 ok(res == 0, "Returned length = %d\n", (int)res);
1045 static void test__strtoi64(void)
1047 static const char no1[] = "31923";
1048 static const char no2[] = "-213312";
1049 static const char no3[] = "12aa";
1050 static const char no4[] = "abc12";
1051 static const char overflow[] = "99999999999999999999";
1052 static const char neg_overflow[] = "-99999999999999999999";
1053 static const char hex[] = "0x123";
1054 static const char oct[] = "000123";
1055 static const char blanks[] = " 12 212.31";
1058 unsigned __int64 ures;
1061 if(!p_strtoi64 || !p_strtoui64) {
1062 win_skip("_strtoi64 or _strtoui64 not found\n");
1067 res = p_strtoi64(no1, NULL, 10);
1068 ok(res == 31923, "res != 31923\n");
1069 res = p_strtoi64(no2, NULL, 10);
1070 ok(res == -213312, "res != -213312\n");
1071 res = p_strtoi64(no3, NULL, 10);
1072 ok(res == 12, "res != 12\n");
1073 res = p_strtoi64(no4, &endpos, 10);
1074 ok(res == 0, "res != 0\n");
1075 ok(endpos == no4, "Scanning was not stopped on first character\n");
1076 res = p_strtoi64(hex, &endpos, 10);
1077 ok(res == 0, "res != 0\n");
1078 ok(endpos == hex+1, "Incorrect endpos (%p-%p)\n", hex, endpos);
1079 res = p_strtoi64(oct, &endpos, 10);
1080 ok(res == 123, "res != 123\n");
1081 ok(endpos == oct+strlen(oct), "Incorrect endpos (%p-%p)\n", oct, endpos);
1082 res = p_strtoi64(blanks, &endpos, 10);
1083 ok(res == 12, "res != 12\n");
1084 ok(endpos == blanks+10, "Incorrect endpos (%p-%p)\n", blanks, endpos);
1085 ok(errno == 0xdeadbeef, "errno = %x\n", errno);
1088 res = p_strtoi64(overflow, &endpos, 10);
1089 ok(res == _I64_MAX, "res != _I64_MAX\n");
1090 ok(endpos == overflow+strlen(overflow), "Incorrect endpos (%p-%p)\n", overflow, endpos);
1091 ok(errno == ERANGE, "errno = %x\n", errno);
1094 res = p_strtoi64(neg_overflow, &endpos, 10);
1095 ok(res == _I64_MIN, "res != _I64_MIN\n");
1096 ok(endpos == neg_overflow+strlen(neg_overflow), "Incorrect endpos (%p-%p)\n", neg_overflow, endpos);
1097 ok(errno == ERANGE, "errno = %x\n", errno);
1100 res = p_strtoi64(no1, &endpos, 16);
1101 ok(res == 203043, "res != 203043\n");
1102 ok(endpos == no1+strlen(no1), "Incorrect endpos (%p-%p)\n", no1, endpos);
1103 res = p_strtoi64(no2, &endpos, 16);
1104 ok(res == -2175762, "res != -2175762\n");
1105 ok(endpos == no2+strlen(no2), "Incorrect endpos (%p-%p)\n", no2, endpos);
1106 res = p_strtoi64(no3, &endpos, 16);
1107 ok(res == 4778, "res != 4778\n");
1108 ok(endpos == no3+strlen(no3), "Incorrect endpos (%p-%p)\n", no3, endpos);
1109 res = p_strtoi64(no4, &endpos, 16);
1110 ok(res == 703506, "res != 703506\n");
1111 ok(endpos == no4+strlen(no4), "Incorrect endpos (%p-%p)\n", no4, endpos);
1112 res = p_strtoi64(hex, &endpos, 16);
1113 ok(res == 291, "res != 291\n");
1114 ok(endpos == hex+strlen(hex), "Incorrect endpos (%p-%p)\n", hex, endpos);
1115 res = p_strtoi64(oct, &endpos, 16);
1116 ok(res == 291, "res != 291\n");
1117 ok(endpos == oct+strlen(oct), "Incorrect endpos (%p-%p)\n", oct, endpos);
1118 res = p_strtoi64(blanks, &endpos, 16);
1119 ok(res == 18, "res != 18\n");
1120 ok(endpos == blanks+10, "Incorrect endpos (%p-%p)\n", blanks, endpos);
1121 ok(errno == 0xdeadbeef, "errno = %x\n", errno);
1124 res = p_strtoi64(hex, &endpos, 36);
1125 ok(res == 1541019, "res != 1541019\n");
1126 ok(endpos == hex+strlen(hex), "Incorrect endpos (%p-%p)\n", hex, endpos);
1127 ok(errno == 0xdeadbeef, "errno = %x\n", errno);
1130 res = p_strtoi64(no1, &endpos, 0);
1131 ok(res == 31923, "res != 31923\n");
1132 ok(endpos == no1+strlen(no1), "Incorrect endpos (%p-%p)\n", no1, endpos);
1133 res = p_strtoi64(no2, &endpos, 0);
1134 ok(res == -213312, "res != -213312\n");
1135 ok(endpos == no2+strlen(no2), "Incorrect endpos (%p-%p)\n", no2, endpos);
1136 res = p_strtoi64(no3, &endpos, 10);
1137 ok(res == 12, "res != 12\n");
1138 ok(endpos == no3+2, "Incorrect endpos (%p-%p)\n", no3, endpos);
1139 res = p_strtoi64(no4, &endpos, 10);
1140 ok(res == 0, "res != 0\n");
1141 ok(endpos == no4, "Incorrect endpos (%p-%p)\n", no4, endpos);
1142 res = p_strtoi64(hex, &endpos, 10);
1143 ok(res == 0, "res != 0\n");
1144 ok(endpos == hex+1, "Incorrect endpos (%p-%p)\n", hex, endpos);
1145 res = p_strtoi64(oct, &endpos, 10);
1146 ok(res == 123, "res != 123\n");
1147 ok(endpos == oct+strlen(oct), "Incorrect endpos (%p-%p)\n", oct, endpos);
1148 res = p_strtoi64(blanks, &endpos, 10);
1149 ok(res == 12, "res != 12\n");
1150 ok(endpos == blanks+10, "Incorrect endpos (%p-%p)\n", blanks, endpos);
1151 ok(errno == 0xdeadbeef, "errno = %x\n", errno);
1154 ures = p_strtoui64(no1, &endpos, 0);
1155 ok(ures == 31923, "ures != 31923\n");
1156 ok(endpos == no1+strlen(no1), "Incorrect endpos (%p-%p)\n", no1, endpos);
1157 ures = p_strtoui64(no2, &endpos, 0);
1158 ok(ures == -213312, "ures != -213312\n");
1159 ok(endpos == no2+strlen(no2), "Incorrect endpos (%p-%p)\n", no2, endpos);
1160 ures = p_strtoui64(no3, &endpos, 10);
1161 ok(ures == 12, "ures != 12\n");
1162 ok(endpos == no3+2, "Incorrect endpos (%p-%p)\n", no3, endpos);
1163 ures = p_strtoui64(no4, &endpos, 10);
1164 ok(ures == 0, "ures != 0\n");
1165 ok(endpos == no4, "Incorrect endpos (%p-%p)\n", no4, endpos);
1166 ures = p_strtoui64(hex, &endpos, 10);
1167 ok(ures == 0, "ures != 0\n");
1168 ok(endpos == hex+1, "Incorrect endpos (%p-%p)\n", hex, endpos);
1169 ures = p_strtoui64(oct, &endpos, 10);
1170 ok(ures == 123, "ures != 123\n");
1171 ok(endpos == oct+strlen(oct), "Incorrect endpos (%p-%p)\n", oct, endpos);
1172 ures = p_strtoui64(blanks, &endpos, 10);
1173 ok(ures == 12, "ures != 12\n");
1174 ok(endpos == blanks+10, "Incorrect endpos (%p-%p)\n", blanks, endpos);
1175 ok(errno == 0xdeadbeef, "errno = %x\n", errno);
1178 ures = p_strtoui64(overflow, &endpos, 10);
1179 ok(ures == _UI64_MAX, "ures != _UI64_MAX\n");
1180 ok(endpos == overflow+strlen(overflow), "Incorrect endpos (%p-%p)\n", overflow, endpos);
1181 ok(errno == ERANGE, "errno = %x\n", errno);
1184 ures = p_strtoui64(neg_overflow, &endpos, 10);
1185 ok(ures == 1, "ures != 1\n");
1186 ok(endpos == neg_overflow+strlen(neg_overflow), "Incorrect endpos (%p-%p)\n", neg_overflow, endpos);
1187 ok(errno == ERANGE, "errno = %x\n", errno);
1190 static inline BOOL almost_equal(double d1, double d2) {
1191 if(d1-d2>-1e-30 && d1-d2<1e-30)
1196 static void test__strtod(void)
1198 const char double1[] = "12.1";
1199 const char double2[] = "-13.721";
1200 const char double3[] = "INF";
1201 const char double4[] = ".21e12";
1202 const char double5[] = "214353e-3";
1203 const char overflow[] = "1d9999999999999999999";
1204 const char white_chars[] = " d10";
1209 d = strtod(double1, &end);
1210 ok(almost_equal(d, 12.1), "d = %lf\n", d);
1211 ok(end == double1+4, "incorrect end (%d)\n", (int)(end-double1));
1213 d = strtod(double2, &end);
1214 ok(almost_equal(d, -13.721), "d = %lf\n", d);
1215 ok(end == double2+7, "incorrect end (%d)\n", (int)(end-double2));
1217 d = strtod(double3, &end);
1218 ok(almost_equal(d, 0), "d = %lf\n", d);
1219 ok(end == double3, "incorrect end (%d)\n", (int)(end-double3));
1221 d = strtod(double4, &end);
1222 ok(almost_equal(d, 210000000000.0), "d = %lf\n", d);
1223 ok(end == double4+6, "incorrect end (%d)\n", (int)(end-double4));
1225 d = strtod(double5, &end);
1226 ok(almost_equal(d, 214.353), "d = %lf\n", d);
1227 ok(end == double5+9, "incorrect end (%d)\n", (int)(end-double5));
1229 d = strtod("12.1d2", NULL);
1230 ok(almost_equal(d, 12.1e2), "d = %lf\n", d);
1232 d = strtod(white_chars, &end);
1233 ok(almost_equal(d, 0), "d = %lf\n", d);
1234 ok(end == white_chars, "incorrect end (%d)\n", (int)(end-white_chars));
1236 /* Set locale with non '.' decimal point (',') */
1237 if(!setlocale(LC_ALL, "Polish")) {
1238 win_skip("system with limited locales\n");
1242 d = strtod("12.1", NULL);
1243 ok(almost_equal(d, 12.0), "d = %lf\n", d);
1245 d = strtod("12,1", NULL);
1246 ok(almost_equal(d, 12.1), "d = %lf\n", d);
1248 setlocale(LC_ALL, "C");
1250 /* Precision tests */
1251 d = strtod("0.1", NULL);
1252 ok(almost_equal(d, 0.1), "d = %lf\n", d);
1253 d = strtod("-0.1", NULL);
1254 ok(almost_equal(d, -0.1), "d = %lf\n", d);
1255 d = strtod("0.1281832188491894198128921", NULL);
1256 ok(almost_equal(d, 0.1281832188491894198128921), "d = %lf\n", d);
1257 d = strtod("0.82181281288121", NULL);
1258 ok(almost_equal(d, 0.82181281288121), "d = %lf\n", d);
1259 d = strtod("21921922352523587651128218821", NULL);
1260 ok(almost_equal(d, 21921922352523587651128218821.0), "d = %lf\n", d);
1261 d = strtod("0.1d238", NULL);
1262 ok(almost_equal(d, 0.1e238L), "d = %lf\n", d);
1263 d = strtod("0.1D-4736", NULL);
1264 ok(almost_equal(d, 0.1e-4736L), "d = %lf\n", d);
1267 strtod(overflow, &end);
1268 ok(errno == ERANGE, "errno = %x\n", errno);
1269 ok(end == overflow+21, "incorrect end (%d)\n", (int)(end-overflow));
1272 strtod("-1d309", NULL);
1273 ok(errno == ERANGE, "errno = %x\n", errno);
1276 static void test_mbstowcs(void)
1278 static const wchar_t wSimple[] = { 't','e','x','t',0 };
1279 static const wchar_t wHiragana[] = { 0x3042,0x3043,0 };
1280 static const char mSimple[] = "text";
1281 static const char mHiragana[] = { 0x82,0xa0,0x82,0xa1,0 };
1288 wOut[4] = '!'; wOut[5] = '\0';
1289 mOut[4] = '!'; mOut[5] = '\0';
1291 ret = mbstowcs(NULL, mSimple, 0);
1292 ok(ret == 4, "mbstowcs did not return 4\n");
1294 ret = mbstowcs(wOut, mSimple, 4);
1295 ok(ret == 4, "mbstowcs did not return 4\n");
1296 ok(!memcmp(wOut, wSimple, 4*sizeof(wchar_t)), "wOut = %s\n", wine_dbgstr_w(wOut));
1297 ok(wOut[4] == '!', "wOut[4] != \'!\'\n");
1299 ret = wcstombs(NULL, wSimple, 0);
1300 ok(ret == 4, "wcstombs did not return 4\n");
1302 ret = wcstombs(mOut, wSimple, 6);
1303 ok(ret == 4, "wcstombs did not return 4\n");
1304 ok(!memcmp(mOut, mSimple, 5*sizeof(char)), "mOut = %s\n", mOut);
1306 ret = wcstombs(mOut, wSimple, 2);
1307 ok(ret == 2, "wcstombs did not return 2\n");
1308 ok(!memcmp(mOut, mSimple, 5*sizeof(char)), "mOut = %s\n", mOut);
1310 if(!setlocale(LC_ALL, "Japanese_Japan.932")) {
1311 win_skip("Japanese_Japan.932 locale not available\n");
1315 ret = mbstowcs(wOut, mHiragana, 6);
1316 ok(ret == 2, "mbstowcs did not return 2\n");
1317 ok(!memcmp(wOut, wHiragana, sizeof(wHiragana)), "wOut = %s\n", wine_dbgstr_w(wOut));
1319 ret = wcstombs(mOut, wHiragana, 6);
1320 ok(ret == 4, "wcstombs did not return 4\n");
1321 ok(!memcmp(mOut, mHiragana, sizeof(mHiragana)), "mOut = %s\n", mOut);
1323 if(!pmbstowcs_s || !pwcstombs_s) {
1324 win_skip("mbstowcs_s or wcstombs_s not available\n");
1328 err = pmbstowcs_s(&ret, wOut, 6, mSimple, _TRUNCATE);
1329 ok(err == 0, "err = %d\n", err);
1330 ok(ret == 5, "ret = %d\n", (int)ret);
1331 ok(!memcmp(wOut, wSimple, sizeof(wSimple)), "wOut = %s\n", wine_dbgstr_w(wOut));
1333 err = pmbstowcs_s(&ret, wOut, 6, mHiragana, _TRUNCATE);
1334 ok(err == 0, "err = %d\n", err);
1335 ok(ret == 3, "ret = %d\n", (int)ret);
1336 ok(!memcmp(wOut, wHiragana, sizeof(wHiragana)), "wOut = %s\n", wine_dbgstr_w(wOut));
1338 err = pwcstombs_s(&ret, mOut, 6, wSimple, _TRUNCATE);
1339 ok(err == 0, "err = %d\n", err);
1340 ok(ret == 5, "ret = %d\n", (int)ret);
1341 ok(!memcmp(mOut, mSimple, sizeof(mSimple)), "mOut = %s\n", mOut);
1343 err = pwcstombs_s(&ret, mOut, 6, wHiragana, _TRUNCATE);
1344 ok(err == 0, "err = %d\n", err);
1345 ok(ret == 5, "ret = %d\n", (int)ret);
1346 ok(!memcmp(mOut, mHiragana, sizeof(mHiragana)), "mOut = %s\n", mOut);
1349 static void test_gcvt(void)
1351 char buf[1024], *res;
1355 win_skip("Skipping _gcvt tests\n");
1360 res = _gcvt(1.2, -1, buf);
1361 ok(res == NULL, "res != NULL\n");
1362 ok(errno == ERANGE, "errno = %d\n", errno);
1365 res = _gcvt(1.2, 5, NULL);
1366 ok(res == NULL, "res != NULL\n");
1367 ok(errno == EINVAL, "errno = %d\n", errno);
1369 res = gcvt(1.2, 5, buf);
1370 ok(res == buf, "res != buf\n");
1371 ok(!strcmp(buf, "1.2"), "buf = %s\n", buf);
1374 err = p_gcvt_s(buf, 5, 1.2, 10);
1375 ok(err == ERANGE, "err = %d\n", err);
1376 ok(buf[0] == '\0', "buf[0] = %c\n", buf[0]);
1379 err = p_gcvt_s(buf, 4, 123456, 2);
1380 ok(err == ERANGE, "err = %d\n", err);
1381 ok(buf[0] == '\0', "buf[0] = %c\n", buf[0]);
1384 static void test__itoa_s(void)
1391 win_skip("Skipping _itoa_s tests\n");
1395 if (p_set_invalid_parameter_handler)
1396 ok(p_set_invalid_parameter_handler(test_invalid_parameter_handler) == NULL,
1397 "Invalid parameter handler was already set\n");
1400 ret = p_itoa_s(0, NULL, 0, 0);
1401 ok(ret == EINVAL, "Expected _itoa_s to return EINVAL, got %d\n", ret);
1402 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1404 memset(buffer, 'X', sizeof(buffer));
1406 ret = p_itoa_s(0, buffer, 0, 0);
1407 ok(ret == EINVAL, "Expected _itoa_s to return EINVAL, got %d\n", ret);
1408 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1409 ok(buffer[0] == 'X', "Expected the output buffer to be untouched\n");
1411 memset(buffer, 'X', sizeof(buffer));
1413 ret = p_itoa_s(0, buffer, sizeof(buffer), 0);
1414 ok(ret == EINVAL, "Expected _itoa_s to return EINVAL, got %d\n", ret);
1415 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1416 ok(buffer[0] == '\0', "Expected the output buffer to be null terminated\n");
1418 memset(buffer, 'X', sizeof(buffer));
1420 ret = p_itoa_s(0, buffer, sizeof(buffer), 64);
1421 ok(ret == EINVAL, "Expected _itoa_s to return EINVAL, got %d\n", ret);
1422 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1423 ok(buffer[0] == '\0', "Expected the output buffer to be null terminated\n");
1425 memset(buffer, 'X', sizeof(buffer));
1427 ret = p_itoa_s(12345678, buffer, 4, 10);
1428 ok(ret == ERANGE, "Expected _itoa_s to return ERANGE, got %d\n", ret);
1429 ok(errno == ERANGE, "Expected errno to be ERANGE, got %d\n", errno);
1430 ok(!memcmp(buffer, "\000765", 4),
1431 "Expected the output buffer to be null terminated with truncated output\n");
1433 memset(buffer, 'X', sizeof(buffer));
1435 ret = p_itoa_s(12345678, buffer, 8, 10);
1436 ok(ret == ERANGE, "Expected _itoa_s to return ERANGE, got %d\n", ret);
1437 ok(errno == ERANGE, "Expected errno to be ERANGE, got %d\n", errno);
1438 ok(!memcmp(buffer, "\0007654321", 8),
1439 "Expected the output buffer to be null terminated with truncated output\n");
1441 memset(buffer, 'X', sizeof(buffer));
1443 ret = p_itoa_s(-12345678, buffer, 9, 10);
1444 ok(ret == ERANGE, "Expected _itoa_s to return ERANGE, got %d\n", ret);
1445 ok(errno == ERANGE, "Expected errno to be ERANGE, got %d\n", errno);
1446 ok(!memcmp(buffer, "\00087654321", 9),
1447 "Expected the output buffer to be null terminated with truncated output\n");
1449 ret = p_itoa_s(12345678, buffer, 9, 10);
1450 ok(ret == 0, "Expected _itoa_s to return 0, got %d\n", ret);
1451 ok(!strcmp(buffer, "12345678"),
1452 "Expected output buffer string to be \"12345678\", got \"%s\"\n",
1455 ret = p_itoa_s(43690, buffer, sizeof(buffer), 2);
1456 ok(ret == 0, "Expected _itoa_s to return 0, got %d\n", ret);
1457 ok(!strcmp(buffer, "1010101010101010"),
1458 "Expected output buffer string to be \"1010101010101010\", got \"%s\"\n",
1461 ret = p_itoa_s(1092009, buffer, sizeof(buffer), 36);
1462 ok(ret == 0, "Expected _itoa_s to return 0, got %d\n", ret);
1463 ok(!strcmp(buffer, "nell"),
1464 "Expected output buffer string to be \"nell\", got \"%s\"\n",
1467 ret = p_itoa_s(5704, buffer, sizeof(buffer), 18);
1468 ok(ret == 0, "Expected _itoa_s to return 0, got %d\n", ret);
1469 ok(!strcmp(buffer, "hag"),
1470 "Expected output buffer string to be \"hag\", got \"%s\"\n",
1473 ret = p_itoa_s(-12345678, buffer, sizeof(buffer), 10);
1474 ok(ret == 0, "Expected _itoa_s to return 0, got %d\n", ret);
1475 ok(!strcmp(buffer, "-12345678"),
1476 "Expected output buffer string to be \"-12345678\", got \"%s\"\n",
1478 if (p_set_invalid_parameter_handler)
1479 ok(p_set_invalid_parameter_handler(NULL) == test_invalid_parameter_handler,
1480 "Cannot reset invalid parameter handler\n");
1483 static void test__strlwr_s(void)
1490 win_skip("Skipping _strlwr_s tests\n");
1495 ret = p_strlwr_s(NULL, 0);
1496 ok(ret == EINVAL, "Expected _strlwr_s to return EINVAL, got %d\n", ret);
1497 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1500 ret = p_strlwr_s(NULL, sizeof(buffer));
1501 ok(ret == EINVAL, "Expected _strlwr_s to return EINVAL, got %d\n", ret);
1502 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1505 ret = p_strlwr_s(buffer, 0);
1506 ok(ret == EINVAL, "Expected _strlwr_s to return EINVAL, got %d\n", ret);
1507 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1509 strcpy(buffer, "GoRrIsTeR");
1511 ret = p_strlwr_s(buffer, 5);
1512 ok(ret == EINVAL, "Expected _strlwr_s to return EINVAL, got %d\n", ret);
1513 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1514 ok(!memcmp(buffer, "\0oRrIsTeR", sizeof("\0oRrIsTeR")),
1515 "Expected the output buffer to be \"gorrIsTeR\"\n");
1517 strcpy(buffer, "GoRrIsTeR");
1519 ret = p_strlwr_s(buffer, sizeof("GoRrIsTeR") - 1);
1520 ok(ret == EINVAL, "Expected _strlwr_s to return EINVAL, got %d\n", ret);
1521 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1522 ok(!memcmp(buffer, "\0oRrIsTeR", sizeof("\0oRrIsTeR")),
1523 "Expected the output buffer to be \"gorrIsTeR\"\n");
1525 strcpy(buffer, "GoRrIsTeR");
1526 ret = p_strlwr_s(buffer, sizeof("GoRrIsTeR"));
1527 ok(ret == 0, "Expected _strlwr_s to return 0, got %d\n", ret);
1528 ok(!strcmp(buffer, "gorrister"),
1529 "Expected the output buffer to be \"gorrister\", got \"%s\"\n",
1532 memcpy(buffer, "GoRrIsTeR\0ELLEN", sizeof("GoRrIsTeR\0ELLEN"));
1533 ret = p_strlwr_s(buffer, sizeof(buffer));
1534 ok(ret == 0, "Expected _strlwr_s to return 0, got %d\n", ret);
1535 ok(!memcmp(buffer, "gorrister\0ELLEN", sizeof("gorrister\0ELLEN")),
1536 "Expected the output buffer to be \"gorrister\\0ELLEN\", got \"%s\"\n",
1540 static void test_wcsncat_s(void)
1542 static wchar_t abcW[] = {'a','b','c',0};
1549 win_skip("skipping wcsncat_s tests\n");
1553 if (p_set_invalid_parameter_handler)
1554 ok(p_set_invalid_parameter_handler(test_invalid_parameter_handler) == NULL,
1555 "Invalid parameter handler was already set\n");
1557 memcpy(src, abcW, sizeof(abcW));
1559 ret = p_wcsncat_s(NULL, 4, src, 4);
1560 ok(ret == EINVAL, "err = %d\n", ret);
1561 ret = p_wcsncat_s(dst, 0, src, 4);
1562 ok(ret == EINVAL, "err = %d\n", ret);
1563 ret = p_wcsncat_s(dst, 0, src, _TRUNCATE);
1564 ok(ret == EINVAL, "err = %d\n", ret);
1565 ret = p_wcsncat_s(dst, 4, NULL, 0);
1566 ok(ret == 0, "err = %d\n", ret);
1569 ret = p_wcsncat_s(dst, 2, src, 4);
1570 ok(ret == ERANGE, "err = %d\n", ret);
1573 ret = p_wcsncat_s(dst, 2, src, _TRUNCATE);
1574 ok(ret == STRUNCATE, "err = %d\n", ret);
1575 ok(dst[0] == 'a' && dst[1] == 0, "dst is %s\n", wine_dbgstr_w(dst));
1577 memcpy(dst, abcW, sizeof(abcW));
1579 ret = p_wcsncat_s(dst, 4, src, 4);
1580 ok(ret == EINVAL, "err = %d\n", ret);
1582 if (p_set_invalid_parameter_handler)
1583 ok(p_set_invalid_parameter_handler(NULL) == test_invalid_parameter_handler,
1584 "Cannot reset invalid parameter handler\n");
1587 static void test__mbsnbcat_s(void)
1589 unsigned char dest[16];
1590 const unsigned char first[] = "dinosaur";
1591 const unsigned char second[] = "duck";
1596 win_skip("Skipping _mbsnbcat_s tests\n");
1600 /* Test invalid arguments. */
1601 ret = p_mbsnbcat_s(NULL, 0, NULL, 0);
1602 ok(ret == 0, "Expected _mbsnbcat_s to return 0, got %d\n", ret);
1605 ret = p_mbsnbcat_s(NULL, 10, NULL, 0);
1606 ok(ret == EINVAL, "Expected _mbsnbcat_s to return EINVAL, got %d\n", ret);
1607 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1610 ret = p_mbsnbcat_s(NULL, 0, NULL, 10);
1611 ok(ret == EINVAL, "Expected _mbsnbcat_s to return EINVAL, got %d\n", ret);
1612 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1614 memset(dest, 'X', sizeof(dest));
1616 ret = p_mbsnbcat_s(dest, 0, NULL, 0);
1617 ok(ret == EINVAL, "Expected _mbsnbcat_s to return EINVAL, got %d\n", ret);
1618 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1619 ok(dest[0] == 'X', "Expected the output buffer to be untouched\n");
1621 memset(dest, 'X', sizeof(dest));
1623 ret = p_mbsnbcat_s(dest, 0, second, 0);
1624 ok(ret == EINVAL, "Expected _mbsnbcat_s to return EINVAL, got %d\n", ret);
1625 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1626 ok(dest[0] == 'X', "Expected the output buffer to be untouched\n");
1628 memset(dest, 'X', sizeof(dest));
1630 ret = p_mbsnbcat_s(dest, sizeof(dest), NULL, 0);
1631 ok(ret == EINVAL, "Expected _mbsnbcat_s to return EINVAL, got %d\n", ret);
1632 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1633 ok(dest[0] == '\0', "Expected the output buffer to be null terminated\n");
1635 memset(dest, 'X', sizeof(dest));
1637 ret = p_mbsnbcat_s(dest, sizeof(dest), NULL, 10);
1638 ok(ret == EINVAL, "Expected _mbsnbcat_s to return EINVAL, got %d\n", ret);
1639 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1640 ok(dest[0] == '\0', "Expected the output buffer to be null terminated\n");
1642 memset(dest, 'X', sizeof(dest));
1644 ret = p_mbsnbcat_s(dest, sizeof(dest), second, sizeof(second));
1645 ok(ret == 0, "Expected _mbsnbcat_s to return 0, got %d\n", ret);
1646 ok(!memcmp(dest, second, sizeof(second)),
1647 "Expected the output buffer string to be \"duck\"\n");
1649 /* Test source truncation behavior. */
1650 memset(dest, 'X', sizeof(dest));
1651 memcpy(dest, first, sizeof(first));
1652 ret = p_mbsnbcat_s(dest, sizeof(dest), second, 0);
1653 ok(ret == 0, "Expected _mbsnbcat_s to return 0, got %d\n", ret);
1654 ok(!memcmp(dest, first, sizeof(first)),
1655 "Expected the output buffer string to be \"dinosaur\"\n");
1657 memset(dest, 'X', sizeof(dest));
1658 memcpy(dest, first, sizeof(first));
1659 ret = p_mbsnbcat_s(dest, sizeof(dest), second, sizeof(second));
1660 ok(ret == 0, "Expected _mbsnbcat_s to return 0, got %d\n", ret);
1661 ok(!memcmp(dest, "dinosaurduck", sizeof("dinosaurduck")),
1662 "Expected the output buffer string to be \"dinosaurduck\"\n");
1664 memset(dest, 'X', sizeof(dest));
1665 memcpy(dest, first, sizeof(first));
1666 ret = p_mbsnbcat_s(dest, sizeof(dest), second, sizeof(second) + 1);
1667 ok(ret == 0, "Expected _mbsnbcat_s to return 0, got %d\n", ret);
1668 ok(!memcmp(dest, "dinosaurduck", sizeof("dinosaurduck")),
1669 "Expected the output buffer string to be \"dinosaurduck\"\n");
1671 memset(dest, 'X', sizeof(dest));
1672 memcpy(dest, first, sizeof(first));
1673 ret = p_mbsnbcat_s(dest, sizeof(dest), second, sizeof(second) - 1);
1674 ok(ret == 0, "Expected _mbsnbcat_s to return 0, got %d\n", ret);
1675 ok(!memcmp(dest, "dinosaurduck", sizeof("dinosaurduck")),
1676 "Expected the output buffer string to be \"dinosaurduck\"\n");
1678 memset(dest, 'X', sizeof(dest));
1679 memcpy(dest, first, sizeof(first));
1680 ret = p_mbsnbcat_s(dest, sizeof(dest), second, sizeof(second) - 2);
1681 ok(ret == 0, "Expected _mbsnbcat_s to return 0, got %d\n", ret);
1682 ok(!memcmp(dest, "dinosaurduc", sizeof("dinosaurduc")),
1683 "Expected the output buffer string to be \"dinosaurduc\"\n");
1685 /* Test destination truncation behavior. */
1686 memset(dest, 'X', sizeof(dest));
1687 memcpy(dest, first, sizeof(first));
1689 ret = p_mbsnbcat_s(dest, sizeof(first) - 1, second, sizeof(second));
1690 ok(ret == EINVAL, "Expected _mbsnbcat_s to return EINVAL, got %d\n", ret);
1691 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1692 ok(!memcmp(dest, "\0inosaur", sizeof("\0inosaur") - 1),
1693 "Expected the output buffer string to be \"\\0inosaur\" without ending null terminator\n");
1695 memset(dest, 'X', sizeof(dest));
1696 memcpy(dest, first, sizeof(first));
1698 ret = p_mbsnbcat_s(dest, sizeof(first), second, sizeof(second));
1699 ok(ret == ERANGE, "Expected _mbsnbcat_s to return ERANGE, got %d\n", ret);
1700 ok(errno == ERANGE, "Expected errno to be ERANGE, got %d\n", errno);
1701 ok(!memcmp(dest, "\0inosaurd", sizeof("\0inosaurd") - 1),
1702 "Expected the output buffer string to be \"\\0inosaurd\" without ending null terminator\n");
1704 memset(dest, 'X', sizeof(dest));
1705 memcpy(dest, first, sizeof(first));
1707 ret = p_mbsnbcat_s(dest, sizeof(first) + 1, second, sizeof(second));
1708 ok(ret == ERANGE, "Expected _mbsnbcat_s to return ERANGE, got %d\n", ret);
1709 ok(errno == ERANGE, "Expected errno to be ERANGE, got %d\n", errno);
1710 ok(!memcmp(dest, "\0inosaurdu", sizeof("\0inosaurdu") - 1),
1711 "Expected the output buffer string to be \"\\0inosaurdu\" without ending null terminator\n");
1714 static void test__ultoa_s(void)
1721 win_skip("Skipping _ultoa_s tests\n");
1726 ret = p_ultoa_s(0, NULL, 0, 0);
1727 ok(ret == EINVAL, "Expected _ultoa_s to return EINVAL, got %d\n", ret);
1728 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1730 memset(buffer, 'X', sizeof(buffer));
1732 ret = p_ultoa_s(0, buffer, 0, 0);
1733 ok(ret == EINVAL, "Expected _ultoa_s to return EINVAL, got %d\n", ret);
1734 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1735 ok(buffer[0] == 'X', "Expected the output buffer to be untouched\n");
1737 memset(buffer, 'X', sizeof(buffer));
1739 ret = p_ultoa_s(0, buffer, sizeof(buffer), 0);
1740 ok(ret == EINVAL, "Expected _ultoa_s to return EINVAL, got %d\n", ret);
1741 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1742 ok(buffer[0] == '\0', "Expected the output buffer to be null terminated\n");
1744 memset(buffer, 'X', sizeof(buffer));
1746 ret = p_ultoa_s(0, buffer, sizeof(buffer), 64);
1747 ok(ret == EINVAL, "Expected _ultoa_s to return EINVAL, got %d\n", ret);
1748 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1749 ok(buffer[0] == '\0', "Expected the output buffer to be null terminated\n");
1751 memset(buffer, 'X', sizeof(buffer));
1753 ret = p_ultoa_s(12345678, buffer, 4, 10);
1754 ok(ret == ERANGE, "Expected _ultoa_s to return ERANGE, got %d\n", ret);
1755 ok(errno == ERANGE, "Expected errno to be ERANGE, got %d\n", errno);
1756 ok(!memcmp(buffer, "\000765", 4),
1757 "Expected the output buffer to be null terminated with truncated output\n");
1759 memset(buffer, 'X', sizeof(buffer));
1761 ret = p_ultoa_s(12345678, buffer, 8, 10);
1762 ok(ret == ERANGE, "Expected _ultoa_s to return ERANGE, got %d\n", ret);
1763 ok(errno == ERANGE, "Expected errno to be ERANGE, got %d\n", errno);
1764 ok(!memcmp(buffer, "\0007654321", 8),
1765 "Expected the output buffer to be null terminated with truncated output\n");
1767 ret = p_ultoa_s(12345678, buffer, 9, 10);
1768 ok(ret == 0, "Expected _ultoa_s to return 0, got %d\n", ret);
1769 ok(!strcmp(buffer, "12345678"),
1770 "Expected output buffer string to be \"12345678\", got \"%s\"\n",
1773 ret = p_ultoa_s(43690, buffer, sizeof(buffer), 2);
1774 ok(ret == 0, "Expected _ultoa_s to return 0, got %d\n", ret);
1775 ok(!strcmp(buffer, "1010101010101010"),
1776 "Expected output buffer string to be \"1010101010101010\", got \"%s\"\n",
1779 ret = p_ultoa_s(1092009, buffer, sizeof(buffer), 36);
1780 ok(ret == 0, "Expected _ultoa_s to return 0, got %d\n", ret);
1781 ok(!strcmp(buffer, "nell"),
1782 "Expected output buffer string to be \"nell\", got \"%s\"\n",
1785 ret = p_ultoa_s(5704, buffer, sizeof(buffer), 18);
1786 ok(ret == 0, "Expected _ultoa_s to return 0, got %d\n", ret);
1787 ok(!strcmp(buffer, "hag"),
1788 "Expected output buffer string to be \"hag\", got \"%s\"\n",
1795 static const char xilstring[]="c:/xilinx";
1798 hMsvcrt = GetModuleHandleA("msvcrt.dll");
1800 hMsvcrt = GetModuleHandleA("msvcrtd.dll");
1801 ok(hMsvcrt != 0, "GetModuleHandleA failed\n");
1802 SET(pmemcpy,"memcpy");
1803 SET(pmemcmp,"memcmp");
1804 SET(p_mbctype,"_mbctype");
1805 SET(p__mb_cur_max,"__mb_cur_max");
1806 pstrcpy_s = (void *)GetProcAddress( hMsvcrt,"strcpy_s" );
1807 pstrcat_s = (void *)GetProcAddress( hMsvcrt,"strcat_s" );
1808 p_mbsnbcat_s = (void *)GetProcAddress( hMsvcrt,"_mbsnbcat_s" );
1809 p_mbsnbcpy_s = (void *)GetProcAddress( hMsvcrt,"_mbsnbcpy_s" );
1810 p_wcscpy_s = (void *)GetProcAddress( hMsvcrt,"wcscpy_s" );
1811 p_wcsncat_s = (void *)GetProcAddress( hMsvcrt,"wcsncat_s" );
1812 p_wcsupr_s = (void *)GetProcAddress( hMsvcrt,"_wcsupr_s" );
1813 p_strnlen = (void *)GetProcAddress( hMsvcrt,"strnlen" );
1814 p_strtoi64 = (void *)GetProcAddress(hMsvcrt, "_strtoi64");
1815 p_strtoui64 = (void *)GetProcAddress(hMsvcrt, "_strtoui64");
1816 pmbstowcs_s = (void *)GetProcAddress(hMsvcrt, "mbstowcs_s");
1817 pwcstombs_s = (void *)GetProcAddress(hMsvcrt, "wcstombs_s");
1818 p_gcvt_s = (void *)GetProcAddress(hMsvcrt, "_gcvt_s");
1819 p_itoa_s = (void *)GetProcAddress(hMsvcrt, "_itoa_s");
1820 p_strlwr_s = (void *)GetProcAddress(hMsvcrt, "_strlwr_s");
1821 p_ultoa_s = (void *)GetProcAddress(hMsvcrt, "_ultoa_s");
1822 p_set_invalid_parameter_handler = (void *) GetProcAddress(hMsvcrt, "_set_invalid_parameter_handler");
1823 p_wcslwr_s = (void*)GetProcAddress(hMsvcrt, "_wcslwr_s");
1825 /* MSVCRT memcpy behaves like memmove for overlapping moves,
1826 MFC42 CString::Insert seems to rely on that behaviour */
1827 strcpy(mem,xilstring);
1828 nLen=strlen(xilstring);
1829 pmemcpy(mem+5, mem,nLen+1);
1830 ok(pmemcmp(mem+5,xilstring, nLen) == 0,
1831 "Got result %s\n",mem+5);
1833 /* Test _swab function */