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);
79 static errno_t (__cdecl *p_mbsupr_s)(unsigned char *str, size_t numberOfElements);
80 static errno_t (__cdecl *p_mbslwr_s)(unsigned char *str, size_t numberOfElements);
82 #define SETNOFAIL(x,y) x = (void*)GetProcAddress(hMsvcrt,y)
83 #define SET(x,y) SETNOFAIL(x,y); ok(x != NULL, "Export '%s' not found\n", y)
85 static HMODULE hMsvcrt;
87 static void test_swab( void ) {
88 char original[] = "BADCFEHGJILKNMPORQTSVUXWZY@#";
89 char expected1[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ@#";
90 char expected2[] = "ABCDEFGHIJKLMNOPQRSTUVWX$";
91 char expected3[] = "$";
98 /* Test 1 - normal even case */
99 memset(to,'$', sizeof(to));
100 memset(from,'@', sizeof(from));
102 memcpy(from, original, testsize);
103 _swab( from, to, testsize );
104 ok(memcmp(to,expected1,testsize) == 0, "Testing even size %d returned '%*.*s'\n", testsize, testsize, testsize, to);
106 /* Test 2 - uneven case */
107 memset(to,'$', sizeof(to));
108 memset(from,'@', sizeof(from));
110 memcpy(from, original, testsize);
111 _swab( from, to, testsize );
112 ok(memcmp(to,expected2,testsize) == 0, "Testing odd size %d returned '%*.*s'\n", testsize, testsize, testsize, to);
114 /* Test 3 - from = to */
115 memset(to,'$', sizeof(to));
116 memset(from,'@', sizeof(from));
118 memcpy(to, original, testsize);
119 _swab( to, to, testsize );
120 ok(memcmp(to,expected1,testsize) == 0, "Testing overlapped size %d returned '%*.*s'\n", testsize, testsize, testsize, to);
122 /* Test 4 - 1 bytes */
123 memset(to,'$', sizeof(to));
124 memset(from,'@', sizeof(from));
126 memcpy(from, original, testsize);
127 _swab( from, to, testsize );
128 ok(memcmp(to,expected3,testsize) == 0, "Testing small size %d returned '%*.*s'\n", testsize, testsize, testsize, to);
131 #if 0 /* use this to generate more tests */
133 static void test_codepage(int cp)
139 ok(_setmbcp(cp) == 0, "Couldn't set mbcp\n");
142 printf("static int result_cp_%d_mbctype[] = { ", cp);
143 for (i = 1; i < 257; i++)
145 if (p_mbctype[i] != prev)
147 printf("0x%x,%d, ", prev, count);
154 printf("0x%x,%d };\n", prev, count);
159 /* RLE-encoded mbctype tables for given codepages */
160 static int result_cp_932_mbctype[] = { 0x0,65, 0x8,1, 0x18,26, 0x8,6, 0x28,26, 0x8,4,
161 0x0,1, 0x8,1, 0xc,31, 0x8,1, 0xa,5, 0x9,58, 0xc,29, 0,3 };
162 static int result_cp_936_mbctype[] = { 0x0,65, 0x8,1, 0x18,26, 0x8,6, 0x28,26, 0x8,6,
164 static int result_cp_949_mbctype[] = { 0x0,66, 0x18,26, 0x8,6, 0x28,26, 0x8,6, 0xc,126,
166 static int result_cp_950_mbctype[] = { 0x0,65, 0x8,1, 0x18,26, 0x8,6, 0x28,26, 0x8,4,
167 0x0,2, 0x4,32, 0xc,94, 0,1 };
169 static void test_cp_table(int cp, int *result)
175 for (i = 0; i < 256; i++)
183 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);
188 #define test_codepage(num) test_cp_table(num, result_cp_##num##_mbctype);
192 static void test_mbcp(void)
194 int mb_orig_max = *p__mb_cur_max;
195 int curr_mbcp = _getmbcp();
196 unsigned char *mbstring = (unsigned char *)"\xb0\xb1\xb2 \xb3\xb4 \xb5"; /* incorrect string */
197 unsigned char *mbstring2 = (unsigned char *)"\xb0\xb1\xb2\xb3Q\xb4\xb5"; /* correct string */
198 unsigned char *mbsonlylead = (unsigned char *)"\xb0\0\xb1\xb2 \xb3";
199 unsigned char buf[16];
205 /* An SBCS codepage test. The ctype of characters on e.g. CP1252 or CP1250 differs slightly
206 * between versions of Windows. Also Windows 9x seems to ignore the codepage and always uses
207 * CP1252 (or the ACP?) so we test only a few ASCII characters */
209 expect_eq(p_mbctype[10], 0, char, "%x");
210 expect_eq(p_mbctype[50], 0, char, "%x");
211 expect_eq(p_mbctype[66], _SBUP, char, "%x");
212 expect_eq(p_mbctype[100], _SBLOW, char, "%x");
213 expect_eq(p_mbctype[128], 0, char, "%x");
215 expect_eq(p_mbctype[10], 0, char, "%x");
216 expect_eq(p_mbctype[50], 0, char, "%x");
217 expect_eq(p_mbctype[66], _SBUP, char, "%x");
218 expect_eq(p_mbctype[100], _SBLOW, char, "%x");
219 expect_eq(p_mbctype[128], 0, char, "%x");
221 /* double byte code pages */
228 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);
229 ok(_ismbblead('\354'), "\354 should be a lead byte\n");
230 ok(_ismbblead(' ') == FALSE, "' ' should not be a lead byte\n");
231 ok(_ismbblead(0x1234b0), "0x1234b0 should not be a lead byte\n");
232 ok(_ismbblead(0x123420) == FALSE, "0x123420 should not be a lead byte\n");
233 ok(_ismbbtrail('\xb0'), "\xa0 should be a trail byte\n");
234 ok(_ismbbtrail(' ') == FALSE, "' ' should not be a trail byte\n");
237 expect_eq(_ismbslead(mbstring, &mbstring[0]), -1, int, "%d");
238 expect_eq(_ismbslead(mbstring, &mbstring[1]), FALSE, int, "%d");
239 expect_eq(_ismbslead(mbstring, &mbstring[2]), -1, int, "%d");
240 expect_eq(_ismbslead(mbstring, &mbstring[3]), FALSE, int, "%d");
241 expect_eq(_ismbslead(mbstring, &mbstring[4]), -1, int, "%d");
242 expect_eq(_ismbslead(mbstring, &mbstring[5]), FALSE, int, "%d");
243 expect_eq(_ismbslead(mbstring, &mbstring[6]), FALSE, int, "%d");
244 expect_eq(_ismbslead(mbstring, &mbstring[7]), -1, int, "%d");
245 expect_eq(_ismbslead(mbstring, &mbstring[8]), FALSE, int, "%d");
247 expect_eq(_ismbslead(mbsonlylead, &mbsonlylead[0]), -1, int, "%d");
248 expect_eq(_ismbslead(mbsonlylead, &mbsonlylead[1]), FALSE, int, "%d");
249 expect_eq(_ismbslead(mbsonlylead, &mbsonlylead[2]), FALSE, int, "%d");
250 expect_eq(_ismbslead(mbsonlylead, &mbsonlylead[5]), FALSE, int, "%d");
253 expect_eq(_ismbstrail(mbstring, &mbstring[0]), FALSE, int, "%d");
254 expect_eq(_ismbstrail(mbstring, &mbstring[1]), -1, int, "%d");
255 expect_eq(_ismbstrail(mbstring, &mbstring[2]), FALSE, int, "%d");
256 expect_eq(_ismbstrail(mbstring, &mbstring[3]), -1, int, "%d");
257 expect_eq(_ismbstrail(mbstring, &mbstring[4]), FALSE, int, "%d");
258 expect_eq(_ismbstrail(mbstring, &mbstring[5]), -1, int, "%d");
259 expect_eq(_ismbstrail(mbstring, &mbstring[6]), FALSE, int, "%d");
260 expect_eq(_ismbstrail(mbstring, &mbstring[7]), FALSE, int, "%d");
261 expect_eq(_ismbstrail(mbstring, &mbstring[8]), -1, int, "%d");
263 expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[0]), FALSE, int, "%d");
264 expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[1]), -1, int, "%d");
265 expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[2]), FALSE, int, "%d");
266 expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[3]), FALSE, int, "%d");
267 expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[4]), FALSE, int, "%d");
268 expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[5]), FALSE, int, "%d");
271 expect_eq(_mbsbtype(mbstring, 0), _MBC_LEAD, int, "%d");
272 expect_eq(_mbsbtype(mbstring, 1), _MBC_TRAIL, int, "%d");
273 expect_eq(_mbsbtype(mbstring, 2), _MBC_LEAD, int, "%d");
274 expect_eq(_mbsbtype(mbstring, 3), _MBC_ILLEGAL, int, "%d");
275 expect_eq(_mbsbtype(mbstring, 4), _MBC_LEAD, int, "%d");
276 expect_eq(_mbsbtype(mbstring, 5), _MBC_TRAIL, int, "%d");
277 expect_eq(_mbsbtype(mbstring, 6), _MBC_SINGLE, int, "%d");
278 expect_eq(_mbsbtype(mbstring, 7), _MBC_LEAD, int, "%d");
279 expect_eq(_mbsbtype(mbstring, 8), _MBC_ILLEGAL, int, "%d");
281 expect_eq(_mbsbtype(mbsonlylead, 0), _MBC_LEAD, int, "%d");
282 expect_eq(_mbsbtype(mbsonlylead, 1), _MBC_ILLEGAL, int, "%d");
283 expect_eq(_mbsbtype(mbsonlylead, 2), _MBC_ILLEGAL, int, "%d");
284 expect_eq(_mbsbtype(mbsonlylead, 3), _MBC_ILLEGAL, int, "%d");
285 expect_eq(_mbsbtype(mbsonlylead, 4), _MBC_ILLEGAL, int, "%d");
286 expect_eq(_mbsbtype(mbsonlylead, 5), _MBC_ILLEGAL, int, "%d");
289 expect_eq(_mbsnextc(mbstring), 0xb0b1, int, "%x");
290 expect_eq(_mbsnextc(&mbstring[2]), 0xb220, int, "%x"); /* lead + invalid tail */
291 expect_eq(_mbsnextc(&mbstring[3]), 0x20, int, "%x"); /* single char */
293 /* _mbclen/_mbslen */
294 expect_eq(_mbclen(mbstring), 2, int, "%d");
295 expect_eq(_mbclen(&mbstring[2]), 2, int, "%d");
296 expect_eq(_mbclen(&mbstring[3]), 1, int, "%d");
297 expect_eq(_mbslen(mbstring2), 4, int, "%d");
298 expect_eq(_mbslen(mbsonlylead), 0, int, "%d"); /* lead + NUL not counted as character */
299 expect_eq(_mbslen(mbstring), 4, int, "%d"); /* lead + invalid trail counted */
301 /* _mbccpy/_mbsncpy */
302 memset(buf, 0xff, sizeof(buf));
303 _mbccpy(buf, mbstring);
304 expect_bin(buf, "\xb0\xb1\xff", 3);
306 memset(buf, 0xff, sizeof(buf));
307 _mbsncpy(buf, mbstring, 1);
308 expect_bin(buf, "\xb0\xb1\xff", 3);
309 memset(buf, 0xff, sizeof(buf));
310 _mbsncpy(buf, mbstring, 2);
311 expect_bin(buf, "\xb0\xb1\xb2 \xff", 5);
312 memset(buf, 0xff, sizeof(buf));
313 _mbsncpy(buf, mbstring, 3);
314 expect_bin(buf, "\xb0\xb1\xb2 \xb3\xb4\xff", 7);
315 memset(buf, 0xff, sizeof(buf));
316 _mbsncpy(buf, mbstring, 4);
317 expect_bin(buf, "\xb0\xb1\xb2 \xb3\xb4 \xff", 8);
318 memset(buf, 0xff, sizeof(buf));
319 _mbsncpy(buf, mbstring, 5);
320 expect_bin(buf, "\xb0\xb1\xb2 \xb3\xb4 \0\0\xff", 10);
321 memset(buf, 0xff, sizeof(buf));
322 _mbsncpy(buf, mbsonlylead, 6);
323 expect_bin(buf, "\0\0\0\0\0\0\0\xff", 8);
325 memset(buf, 0xff, sizeof(buf));
326 _mbsnbcpy(buf, mbstring2, 2);
327 expect_bin(buf, "\xb0\xb1\xff", 3);
328 _mbsnbcpy(buf, mbstring2, 3);
329 expect_bin(buf, "\xb0\xb1\0\xff", 4);
330 _mbsnbcpy(buf, mbstring2, 4);
331 expect_bin(buf, "\xb0\xb1\xb2\xb3\xff", 5);
332 memset(buf, 0xff, sizeof(buf));
333 _mbsnbcpy(buf, mbsonlylead, 5);
334 expect_bin(buf, "\0\0\0\0\0\xff", 6);
337 step = _mbsinc(mbstring) - mbstring;
338 ok(step == 2, "_mbsinc adds %d (exp. 2)\n", step);
339 step = _mbsinc(&mbstring[2]) - &mbstring[2]; /* lead + invalid tail */
340 ok(step == 2, "_mbsinc adds %d (exp. 2)\n", step);
342 step = _mbsninc(mbsonlylead, 1) - mbsonlylead;
343 ok(step == 0, "_mbsninc adds %d (exp. 0)\n", step);
344 step = _mbsninc(mbsonlylead, 2) - mbsonlylead; /* lead + NUL byte + lead + char */
345 ok(step == 0, "_mbsninc adds %d (exp. 0)\n", step);
346 step = _mbsninc(mbstring2, 0) - mbstring2;
347 ok(step == 0, "_mbsninc adds %d (exp. 2)\n", step);
348 step = _mbsninc(mbstring2, 1) - mbstring2;
349 ok(step == 2, "_mbsninc adds %d (exp. 2)\n", step);
350 step = _mbsninc(mbstring2, 2) - mbstring2;
351 ok(step == 4, "_mbsninc adds %d (exp. 4)\n", step);
352 step = _mbsninc(mbstring2, 3) - mbstring2;
353 ok(step == 5, "_mbsninc adds %d (exp. 5)\n", step);
354 step = _mbsninc(mbstring2, 4) - mbstring2;
355 ok(step == 7, "_mbsninc adds %d (exp. 7)\n", step);
356 step = _mbsninc(mbstring2, 5) - mbstring2;
357 ok(step == 7, "_mbsninc adds %d (exp. 7)\n", step);
358 step = _mbsninc(mbstring2, 17) - mbstring2;
359 ok(step == 7, "_mbsninc adds %d (exp. 7)\n", step);
361 /* functions that depend on locale codepage, not mbcp.
362 * we hope the current locale to be SBCS because setlocale(LC_ALL, ".1252") seems not to work yet
363 * (as of Wine 0.9.43)
365 GetCPInfo(GetACP(), &cp_info);
366 if (cp_info.MaxCharSize == 1)
368 expect_eq(mblen((char *)mbstring, 3), 1, int, "%x");
369 expect_eq(_mbstrlen((char *)mbstring2), 7, int, "%d");
372 skip("Current locale has double-byte charset - could lead to false positives\n");
375 expect_eq(_ismbblead(0x80), 0, int, "%d");
377 expect_eq(_ismbblead(0x81), 1, int, "%d");
378 expect_eq(_ismbblead(0x83), 1, int, "%d");
380 expect_eq(_ismbblead(0x84), 1, int, "%d");
381 expect_eq(_ismbblead(0xd3), 1, int, "%d");
382 expect_eq(_ismbblead(0xd7), 0, int, "%d");
384 expect_eq(_ismbblead(0xd8), 1, int, "%d");
386 expect_eq(_ismbblead(0xd9), 1, int, "%d");
388 expect_eq(_ismbbtrail(0x30), 0, int, "%d");
389 expect_eq(_ismbbtrail(0x31), 1, int, "%d");
390 expect_eq(_ismbbtrail(0x7e), 1, int, "%d");
391 expect_eq(_ismbbtrail(0x7f), 0, int, "%d");
392 expect_eq(_ismbbtrail(0x80), 0, int, "%d");
393 expect_eq(_ismbbtrail(0x81), 1, int, "%d");
394 expect_eq(_ismbbtrail(0xfe), 1, int, "%d");
395 expect_eq(_ismbbtrail(0xff), 0, int, "%d");
400 static void test_mbsspn( void)
402 unsigned char str1[]="cabernet";
403 unsigned char str2[]="shiraz";
404 unsigned char set[]="abc";
405 unsigned char empty[]="";
407 ret=_mbsspn( str1, set);
408 ok( ret==3, "_mbsspn returns %d should be 3\n", ret);
409 ret=_mbsspn( str2, set);
410 ok( ret==0, "_mbsspn returns %d should be 0\n", ret);
411 ret=_mbsspn( str1, empty);
412 ok( ret==0, "_mbsspn returns %d should be 0\n", ret);
415 static void test_mbsspnp( void)
417 unsigned char str1[]="cabernet";
418 unsigned char str2[]="shiraz";
419 unsigned char set[]="abc";
420 unsigned char empty[]="";
421 unsigned char full[]="abcenrt";
423 ret=_mbsspnp( str1, set);
424 ok( ret[0]=='e', "_mbsspnp returns %c should be e\n", ret[0]);
425 ret=_mbsspnp( str2, set);
426 ok( ret[0]=='s', "_mbsspnp returns %c should be s\n", ret[0]);
427 ret=_mbsspnp( str1, empty);
428 ok( ret[0]=='c', "_mbsspnp returns %c should be c\n", ret[0]);
429 ret=_mbsspnp( str1, full);
430 ok( ret==NULL, "_mbsspnp returns %p should be NULL\n", ret);
433 static void test_strdup(void)
437 ok( str == 0, "strdup returns %s should be 0\n", str);
441 static void test_strcpy_s(void)
444 const char *small = "small";
445 const char *big = "atoolongstringforthislittledestination";
450 skip("strcpy_s not found\n");
454 memset(dest, 'X', sizeof(dest));
455 ret = pstrcpy_s(dest, sizeof(dest), small);
456 ok(ret == 0, "Copying a string into a big enough destination returned %d, expected 0\n", ret);
457 ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == 'l' &&
458 dest[4] == 'l' && dest[5] == '\0'&& dest[6] == 'X' && dest[7] == 'X',
459 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
460 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
462 memset(dest, 'X', sizeof(dest));
463 ret = pstrcpy_s(dest, 0, big);
464 ok(ret == EINVAL, "Copying into a destination of size 0 returned %d, expected EINVAL\n", ret);
465 ok(dest[0] == 'X' && dest[1] == 'X' && dest[2] == 'X' && dest[3] == 'X' &&
466 dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
467 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
468 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
469 ret = pstrcpy_s(dest, 0, NULL);
470 ok(ret == EINVAL, "Copying into a destination of size 0 returned %d, expected EINVAL\n", ret);
471 ok(dest[0] == 'X' && dest[1] == 'X' && dest[2] == 'X' && dest[3] == 'X' &&
472 dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
473 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
474 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
476 memset(dest, 'X', sizeof(dest));
477 ret = pstrcpy_s(dest, sizeof(dest), big);
478 ok(ret == ERANGE, "Copying a big string in a small location returned %d, expected ERANGE\n", ret);
479 ok(dest[0] == '\0'&& dest[1] == 't' && dest[2] == 'o' && dest[3] == 'o' &&
480 dest[4] == 'l' && dest[5] == 'o' && dest[6] == 'n' && dest[7] == 'g',
481 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
482 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
484 memset(dest, 'X', sizeof(dest));
485 ret = pstrcpy_s(dest, sizeof(dest), NULL);
486 ok(ret == EINVAL, "Copying from a NULL source string returned %d, expected EINVAL\n", ret);
487 ok(dest[0] == '\0'&& dest[1] == 'X' && dest[2] == 'X' && dest[3] == 'X' &&
488 dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
489 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
490 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
492 ret = pstrcpy_s(NULL, sizeof(dest), small);
493 ok(ret == EINVAL, "Copying a big string a NULL dest returned %d, expected EINVAL\n", ret);
496 static void test_strcat_s(void)
499 const char *small = "sma";
504 skip("strcat_s not found\n");
508 memset(dest, 'X', sizeof(dest));
510 ret = pstrcat_s(dest, sizeof(dest), small);
511 ok(ret == 0, "strcat_s: Copying a string into a big enough destination returned %d, expected 0\n", ret);
512 ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == '\0'&&
513 dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
514 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
515 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
516 ret = pstrcat_s(dest, sizeof(dest), small);
517 ok(ret == 0, "strcat_s: Attaching a string to a big enough destination returned %d, expected 0\n", ret);
518 ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == 's' &&
519 dest[4] == 'm' && dest[5] == 'a' && dest[6] == '\0'&& dest[7] == 'X',
520 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
521 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 == ERANGE, "strcat_s: Attaching a string to a filled up destination returned %d, expected ERANGE\n", ret);
525 ok(dest[0] == '\0'&& dest[1] == 'm' && dest[2] == 'a' && dest[3] == 's' &&
526 dest[4] == 'm' && dest[5] == 'a' && dest[6] == 's' && dest[7] == 'm',
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]);
530 memset(dest, 'X', sizeof(dest));
534 ret = pstrcat_s(dest, 0, small);
535 ok(ret == EINVAL, "strcat_s: Source len = 0 returned %d, expected EINVAL\n", ret);
536 ok(dest[0] == 'a' && dest[1] == '\0'&& dest[2] == 'X' && dest[3] == 'X' &&
537 dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
538 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
539 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
541 ret = pstrcat_s(dest, 0, NULL);
542 ok(ret == EINVAL, "strcat_s: len = 0 and src = NULL 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]);
548 ret = pstrcat_s(dest, sizeof(dest), NULL);
549 ok(ret == EINVAL, "strcat_s: Sourcing from NULL returned %d, expected EINVAL\n", ret);
550 ok(dest[0] == '\0'&& 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]);
555 ret = pstrcat_s(NULL, sizeof(dest), small);
556 ok(ret == EINVAL, "strcat_s: Writing to a NULL string returned %d, expected EINVAL\n", ret);
559 static void test__mbsnbcpy_s(void)
561 unsigned char dest[8];
562 const unsigned char big[] = "atoolongstringforthislittledestination";
563 const unsigned char small[] = "small";
568 skip("_mbsnbcpy_s not found\n");
572 memset(dest, 'X', sizeof(dest));
573 ret = p_mbsnbcpy_s(dest, sizeof(dest), small, sizeof(small));
574 ok(ret == 0, "_mbsnbcpy_s: Copying a string into a big enough destination returned %d, expected 0\n", ret);
575 ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == 'l' &&
576 dest[4] == 'l' && dest[5] == '\0'&& dest[6] == 'X' && dest[7] == 'X',
577 "Unexpected return data from _mbsnbcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
578 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
581 memset(dest, 'X', sizeof(dest));
582 ret = p_mbsnbcpy_s(dest, sizeof(dest) - 2, big, sizeof(small));
583 ok(ret == ERANGE, "_mbsnbcpy_s: Copying a too long string returned %d, expected ERANGE\n", ret);
584 ok(dest[0] == '\0'&& dest[1] == 't' && dest[2] == 'o' && dest[3] == 'o' &&
585 dest[4] == 'l' && dest[5] == 'o' && dest[6] == 'X' && dest[7] == 'X',
586 "Unexpected return data from _mbsnbcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
587 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
589 memset(dest, 'X', sizeof(dest));
590 ret = p_mbsnbcpy_s(dest, sizeof(dest) - 2, big, 4);
591 ok(ret == 0, "_mbsnbcpy_s: Copying a too long string with a count cap returned %d, expected 0\n", ret);
592 ok(dest[0] == 'a' && dest[1] == 't' && dest[2] == 'o' && dest[3] == 'o' &&
593 dest[4] == '\0'&& dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
594 "Unexpected return data from _mbsnbcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
595 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
597 memset(dest, 'X', sizeof(dest));
598 ret = p_mbsnbcpy_s(dest, sizeof(dest) - 2, small, sizeof(small) + 10);
599 ok(ret == 0, "_mbsnbcpy_s: Copying more data than the source string len returned %d, expected 0\n", ret);
600 ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == 'l' &&
601 dest[4] == 'l' && dest[5] == '\0'&& dest[6] == 'X' && dest[7] == 'X',
602 "Unexpected return data from _mbsnbcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
603 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
606 static void test_wcscpy_s(void)
608 static const WCHAR szLongText[] = { 'T','h','i','s','A','L','o','n','g','s','t','r','i','n','g',0 };
609 static WCHAR szDest[18];
610 static WCHAR szDestShort[8];
615 skip("wcscpy_s not found\n");
620 ret = p_wcscpy_s(NULL, 18, szLongText);
621 ok(ret == EINVAL, "p_wcscpy_s expect EINVAL got %d\n", ret);
623 /* Test NULL Source */
625 ret = p_wcscpy_s(szDest, 18, NULL);
626 ok(ret == EINVAL, "expected EINVAL got %d\n", ret);
627 ok(szDest[0] == 0, "szDest[0] not 0\n");
629 /* Test invalid size */
631 ret = p_wcscpy_s(szDest, 0, szLongText);
632 /* Later versions changed the return value for this case to EINVAL,
633 * and don't modify the result if the dest size is 0.
635 ok(ret == ERANGE || ret == EINVAL, "expected ERANGE/EINVAL got %d\n", ret);
636 ok(szDest[0] == 0 || ret == EINVAL, "szDest[0] not 0\n");
638 /* Copy same buffer size */
639 ret = p_wcscpy_s(szDest, 18, szLongText);
640 ok(ret == 0, "expected 0 got %d\n", ret);
641 ok(lstrcmpW(szDest, szLongText) == 0, "szDest != szLongText\n");
643 /* Copy smaller buffer size */
645 ret = p_wcscpy_s(szDestShort, 8, szLongText);
646 ok(ret == ERANGE || ret == EINVAL, "expected ERANGE/EINVAL got %d\n", ret);
647 ok(szDestShort[0] == 0, "szDestShort[0] not 0\n");
650 static void test__wcsupr_s(void)
652 static const WCHAR mixedString[] = {'M', 'i', 'X', 'e', 'D', 'l', 'o', 'w',
653 'e', 'r', 'U', 'P', 'P', 'E', 'R', 0};
654 static const WCHAR expectedString[] = {'M', 'I', 'X', 'E', 'D', 'L', 'O',
655 'W', 'E', 'R', 'U', 'P', 'P', 'E',
657 WCHAR testBuffer[2*sizeof(mixedString)/sizeof(WCHAR)];
662 win_skip("_wcsupr_s not found\n");
666 /* Test NULL input string and invalid size. */
668 ret = p_wcsupr_s(NULL, 0);
669 ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
670 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
672 /* Test NULL input string and valid size. */
674 ret = p_wcsupr_s(NULL, sizeof(testBuffer)/sizeof(WCHAR));
675 ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
676 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
678 /* Test empty string with zero size. */
680 testBuffer[0] = '\0';
681 ret = p_wcsupr_s(testBuffer, 0);
682 ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
683 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
684 ok(testBuffer[0] == '\0', "Expected the buffer to be unchanged\n");
686 /* Test empty string with size of one. */
687 testBuffer[0] = '\0';
688 ret = p_wcsupr_s(testBuffer, 1);
689 ok(ret == 0, "Expected _wcsupr_s to succeed, got %d\n", ret);
690 ok(testBuffer[0] == '\0', "Expected the buffer to be unchanged\n");
692 /* Test one-byte buffer with zero size. */
695 ret = p_wcsupr_s(testBuffer, 0);
696 ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
697 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
698 ok(testBuffer[0] == '\0', "Expected the first buffer character to be null\n");
700 /* Test one-byte buffer with size of one. */
703 ret = p_wcsupr_s(testBuffer, 1);
704 ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
705 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
706 ok(testBuffer[0] == '\0', "Expected the first buffer character to be null\n");
708 /* Test invalid size. */
709 wcscpy(testBuffer, mixedString);
711 ret = p_wcsupr_s(testBuffer, 0);
712 ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
713 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
714 ok(testBuffer[0] == '\0', "Expected the first buffer character to be null\n");
716 /* Test normal string uppercasing. */
717 wcscpy(testBuffer, mixedString);
718 ret = p_wcsupr_s(testBuffer, sizeof(mixedString)/sizeof(WCHAR));
719 ok(ret == 0, "Expected _wcsupr_s to succeed, got %d\n", ret);
720 ok(!wcscmp(testBuffer, expectedString), "Expected the string to be fully upper-case\n");
722 /* Test uppercasing with a shorter buffer size count. */
723 wcscpy(testBuffer, mixedString);
725 ret = p_wcsupr_s(testBuffer, sizeof(mixedString)/sizeof(WCHAR) - 1);
726 ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
727 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
728 ok(testBuffer[0] == '\0', "Expected the first buffer character to be null\n");
730 /* Test uppercasing with a longer buffer size count. */
731 wcscpy(testBuffer, mixedString);
732 ret = p_wcsupr_s(testBuffer, sizeof(testBuffer)/sizeof(WCHAR));
733 ok(ret == 0, "Expected _wcsupr_s to succeed, got %d\n", ret);
734 ok(!wcscmp(testBuffer, expectedString), "Expected the string to be fully upper-case\n");
737 static void test__wcslwr_s(void)
739 static const WCHAR mixedString[] = {'M', 'i', 'X', 'e', 'D', 'l', 'o', 'w',
740 'e', 'r', 'U', 'P', 'P', 'E', 'R', 0};
741 static const WCHAR expectedString[] = {'m', 'i', 'x', 'e', 'd', 'l', 'o',
742 'w', 'e', 'r', 'u', 'p', 'p', 'e',
744 WCHAR buffer[2*sizeof(mixedString)/sizeof(WCHAR)];
749 win_skip("_wcslwr_s not found\n");
753 /* Test NULL input string and invalid size. */
755 ret = p_wcslwr_s(NULL, 0);
756 ok(ret == EINVAL, "expected EINVAL, got %d\n", ret);
757 ok(errno == EINVAL, "expected errno EINVAL, got %d\n", errno);
759 /* Test NULL input string and valid size. */
761 ret = p_wcslwr_s(NULL, sizeof(buffer)/sizeof(wchar_t));
762 ok(ret == EINVAL, "expected EINVAL, got %d\n", ret);
763 ok(errno == EINVAL, "expected errno EINVAL, got %d\n", errno);
765 /* Test empty string with zero size. */
768 ret = p_wcslwr_s(buffer, 0);
769 ok(ret == EINVAL, "expected EINVAL, got %d\n", ret);
770 ok(errno == EINVAL, "expected errno EINVAL, got %d\n", errno);
771 ok(buffer[0] == 0, "expected empty string\n");
773 /* Test empty string with size of one. */
775 ret = p_wcslwr_s(buffer, 1);
776 ok(ret == 0, "got %d\n", ret);
777 ok(buffer[0] == 0, "expected buffer to be unchanged\n");
779 /* Test one-byte buffer with zero size. */
782 ret = p_wcslwr_s(buffer, 0);
783 ok(ret == EINVAL, "expected EINVAL, got %d\n", ret);
784 ok(errno == EINVAL, "expected errno to be EINVAL, got %d\n", errno);
785 ok(buffer[0] == '\0', "expected empty string\n");
787 /* Test one-byte buffer with size of one. */
790 ret = p_wcslwr_s(buffer, 1);
791 ok(ret == EINVAL, "expected EINVAL, got %d\n", ret);
792 ok(errno == EINVAL, "expected errno to be EINVAL, got %d\n", errno);
793 ok(buffer[0] == '\0', "expected empty string\n");
795 /* Test invalid size. */
796 wcscpy(buffer, mixedString);
798 ret = p_wcslwr_s(buffer, 0);
799 ok(ret == EINVAL, "Expected EINVAL, got %d\n", ret);
800 ok(errno == EINVAL, "expected errno to be EINVAL, got %d\n", errno);
801 ok(buffer[0] == '\0', "expected empty string\n");
803 /* Test normal string uppercasing. */
804 wcscpy(buffer, mixedString);
805 ret = p_wcslwr_s(buffer, sizeof(mixedString)/sizeof(WCHAR));
806 ok(ret == 0, "expected 0, got %d\n", ret);
807 ok(!wcscmp(buffer, expectedString), "expected lowercase\n");
809 /* Test uppercasing with a shorter buffer size count. */
810 wcscpy(buffer, mixedString);
812 ret = p_wcslwr_s(buffer, sizeof(mixedString)/sizeof(WCHAR) - 1);
813 ok(ret == EINVAL, "expected EINVAL, got %d\n", ret);
814 ok(errno == EINVAL, "expected errno to be EINVAL, got %d\n", errno);
815 ok(buffer[0] == '\0', "expected empty string\n");
817 /* Test uppercasing with a longer buffer size count. */
818 wcscpy(buffer, mixedString);
819 ret = p_wcslwr_s(buffer, sizeof(buffer)/sizeof(WCHAR));
820 ok(ret == 0, "expected 0, got %d\n", ret);
821 ok(!wcscmp(buffer, expectedString), "expected lowercase\n");
824 static void test_mbcjisjms(void)
826 /* List of value-pairs to test. The test assumes the last pair to be {0, ..} */
827 unsigned int jisjms[][2] = { {0x2020, 0}, {0x2021, 0}, {0x2120, 0}, {0x2121, 0x8140},
828 {0x7f7f, 0}, {0x7f7e, 0}, {0x7e7f, 0}, {0x7e7e, 0xeffc},
829 {0x2121FFFF, 0}, {0x2223, 0x81a1}, {0x237e, 0x829e}, {0, 0}};
830 unsigned int ret, exp, i;
835 ret = _mbcjistojms(jisjms[i][0]);
837 if(_getmbcp() == 932) /* Japanese codepage? */
840 exp = jisjms[i][0]; /* If not, no conversion */
842 ok(ret == exp, "Expected 0x%x, got 0x%x\n", exp, ret);
843 } while(jisjms[i++][0] != 0);
846 static void test_mbctombb(void)
848 static const unsigned int mbcmbb_932[][2] = {
849 {0x829e, 0x829e}, {0x829f, 0xa7}, {0x82f1, 0xdd}, {0x82f2, 0x82f2},
850 {0x833f, 0x833f}, {0x8340, 0xa7}, {0x837e, 0xd0}, {0x837f, 0x837f},
851 {0x8380, 0xd1}, {0x8396, 0xb9}, {0x8397, 0x8397}, {0x813f, 0x813f},
852 {0x8140, 0x20}, {0x814c, 0x814c}, {0x814f, 0x5e}, {0x8197, 0x40},
853 {0x8198, 0x8198}, {0x8258, 0x39}, {0x8259, 0x8259}, {0x825f, 0x825f},
854 {0x8260, 0x41}, {0x82f1, 0xdd}, {0x82f2, 0x82f2}, {0,0}};
855 unsigned int exp, ret, i;
856 unsigned int prev_cp = _getmbcp();
859 for (i = 0; mbcmbb_932[i][0] != 0; i++)
861 ret = _mbctombb(mbcmbb_932[i][0]);
862 exp = mbcmbb_932[i][1];
863 ok(ret == exp, "Expected 0x%x, got 0x%x\n", exp, ret);
868 static void test_ismbclegal(void) {
869 unsigned int prev_cp = _getmbcp();
873 _setmbcp(932); /* Japanese */
875 for(i = 0; i < 0x10000; i++) {
876 ret = _ismbclegal(i);
877 exp = ((HIBYTE(i) >= 0x81 && HIBYTE(i) <= 0x9F) ||
878 (HIBYTE(i) >= 0xE0 && HIBYTE(i) <= 0xFC)) &&
879 ((LOBYTE(i) >= 0x40 && LOBYTE(i) <= 0x7E) ||
880 (LOBYTE(i) >= 0x80 && LOBYTE(i) <= 0xFC));
886 ok(!err, "_ismbclegal (932) : Expected 0x%x, got 0x%x (0x%x)\n", exp, ret, i);
887 _setmbcp(936); /* Chinese (GBK) */
889 for(i = 0; i < 0x10000; i++) {
890 ret = _ismbclegal(i);
891 exp = HIBYTE(i) >= 0x81 && HIBYTE(i) <= 0xFE &&
892 LOBYTE(i) >= 0x40 && LOBYTE(i) <= 0xFE;
898 ok(!err, "_ismbclegal (936) : Expected 0x%x, got 0x%x (0x%x)\n", exp, ret, i);
899 _setmbcp(949); /* Korean */
901 for(i = 0; i < 0x10000; i++) {
902 ret = _ismbclegal(i);
903 exp = HIBYTE(i) >= 0x81 && HIBYTE(i) <= 0xFE &&
904 LOBYTE(i) >= 0x41 && LOBYTE(i) <= 0xFE;
910 ok(!err, "_ismbclegal (949) : Expected 0x%x, got 0x%x (0x%x)\n", exp, ret, i);
911 _setmbcp(950); /* Chinese (Big5) */
913 for(i = 0; i < 0x10000; i++) {
914 ret = _ismbclegal(i);
915 exp = HIBYTE(i) >= 0x81 && HIBYTE(i) <= 0xFE &&
916 ((LOBYTE(i) >= 0x40 && LOBYTE(i) <= 0x7E) ||
917 (LOBYTE(i) >= 0xA1 && LOBYTE(i) <= 0xFE));
923 ok(!err, "_ismbclegal (950) : Expected 0x%x, got 0x%x (0x%x)\n", exp, ret, i);
924 _setmbcp(1361); /* Korean (Johab) */
926 for(i = 0; i < 0x10000; i++) {
927 ret = _ismbclegal(i);
928 exp = ((HIBYTE(i) >= 0x81 && HIBYTE(i) <= 0xD3) ||
929 (HIBYTE(i) >= 0xD8 && HIBYTE(i) <= 0xF9)) &&
930 ((LOBYTE(i) >= 0x31 && LOBYTE(i) <= 0x7E) ||
931 (LOBYTE(i) >= 0x81 && LOBYTE(i) <= 0xFE)) &&
938 todo_wine ok(!err, "_ismbclegal (1361) : Expected 0x%x, got 0x%x (0x%x)\n", exp, ret, i);
943 static const struct {
945 const char* delimiter;
946 int exp_offsetret1; /* returned offset from string after first call to strtok()
948 int exp_offsetret2; /* returned offset from string after second call to strtok()
950 int exp_offsetret3; /* returned offset from string after third call to strtok()
952 } testcases_strtok[] = {
953 { "red cabernet", " ", 0, 4, -1 },
954 { "sparkling white riesling", " ", 0, 10, 16 },
955 { " pale cream sherry", "e ", 1, 6, 9 },
960 static void test_strtok(void)
965 for( i = 0; testcases_strtok[i].string; i++){
966 strcpy( teststr, testcases_strtok[i].string);
967 strret = strtok( teststr, testcases_strtok[i].delimiter);
968 ok( (int)(strret - teststr) == testcases_strtok[i].exp_offsetret1 ||
969 (!strret && testcases_strtok[i].exp_offsetret1 == -1),
970 "string (%p) \'%s\' return %p\n",
971 teststr, testcases_strtok[i].string, strret);
972 if( !strret) continue;
973 strret = strtok( NULL, testcases_strtok[i].delimiter);
974 ok( (int)(strret - teststr) == testcases_strtok[i].exp_offsetret2 ||
975 (!strret && testcases_strtok[i].exp_offsetret2 == -1),
976 "second call string (%p) \'%s\' return %p\n",
977 teststr, testcases_strtok[i].string, strret);
978 if( !strret) continue;
979 strret = strtok( NULL, testcases_strtok[i].delimiter);
980 ok( (int)(strret - teststr) == testcases_strtok[i].exp_offsetret3 ||
981 (!strret && testcases_strtok[i].exp_offsetret3 == -1),
982 "third call string (%p) \'%s\' return %p\n",
983 teststr, testcases_strtok[i].string, strret);
987 static void test_strtol(void)
993 /* errno is only set in case of error, so reset errno to EBADF to check for errno modification */
994 /* errno is modified on W2K8+ */
996 l = strtol("-1234", &e, 0);
997 ok(l==-1234, "wrong value %d\n", l);
998 ok(errno == EBADF || broken(errno == 0), "wrong errno %d\n", errno);
1000 ul = strtoul("1234", &e, 0);
1001 ok(ul==1234, "wrong value %u\n", ul);
1002 ok(errno == EBADF || broken(errno == 0), "wrong errno %d\n", errno);
1005 l = strtol("2147483647L", &e, 0);
1006 ok(l==2147483647, "wrong value %d\n", l);
1007 ok(errno == EBADF || broken(errno == 0), "wrong errno %d\n", errno);
1009 l = strtol("-2147483648L", &e, 0);
1010 ok(l==-2147483647L - 1, "wrong value %d\n", l);
1011 ok(errno == EBADF || broken(errno == 0), "wrong errno %d\n", errno);
1013 ul = strtoul("4294967295UL", &e, 0);
1014 ok(ul==4294967295ul, "wrong value %u\n", ul);
1015 ok(errno == EBADF || broken(errno == 0), "wrong errno %d\n", errno);
1018 l = strtol("9223372036854775807L", &e, 0);
1019 ok(l==2147483647, "wrong value %d\n", l);
1020 ok(errno == ERANGE, "wrong errno %d\n", errno);
1022 ul = strtoul("9223372036854775807L", &e, 0);
1023 ok(ul==4294967295ul, "wrong value %u\n", ul);
1024 ok(errno == ERANGE, "wrong errno %d\n", errno);
1027 static void test_strnlen(void)
1029 static const char str[] = "string";
1033 win_skip("strnlen not found\n");
1037 res = p_strnlen(str, 20);
1038 ok(res == 6, "Returned length = %d\n", (int)res);
1040 res = p_strnlen(str, 3);
1041 ok(res == 3, "Returned length = %d\n", (int)res);
1043 res = p_strnlen(NULL, 0);
1044 ok(res == 0, "Returned length = %d\n", (int)res);
1047 static void test__strtoi64(void)
1049 static const char no1[] = "31923";
1050 static const char no2[] = "-213312";
1051 static const char no3[] = "12aa";
1052 static const char no4[] = "abc12";
1053 static const char overflow[] = "99999999999999999999";
1054 static const char neg_overflow[] = "-99999999999999999999";
1055 static const char hex[] = "0x123";
1056 static const char oct[] = "000123";
1057 static const char blanks[] = " 12 212.31";
1060 unsigned __int64 ures;
1063 if(!p_strtoi64 || !p_strtoui64) {
1064 win_skip("_strtoi64 or _strtoui64 not found\n");
1069 res = p_strtoi64(no1, NULL, 10);
1070 ok(res == 31923, "res != 31923\n");
1071 res = p_strtoi64(no2, NULL, 10);
1072 ok(res == -213312, "res != -213312\n");
1073 res = p_strtoi64(no3, NULL, 10);
1074 ok(res == 12, "res != 12\n");
1075 res = p_strtoi64(no4, &endpos, 10);
1076 ok(res == 0, "res != 0\n");
1077 ok(endpos == no4, "Scanning was not stopped on first character\n");
1078 res = p_strtoi64(hex, &endpos, 10);
1079 ok(res == 0, "res != 0\n");
1080 ok(endpos == hex+1, "Incorrect endpos (%p-%p)\n", hex, endpos);
1081 res = p_strtoi64(oct, &endpos, 10);
1082 ok(res == 123, "res != 123\n");
1083 ok(endpos == oct+strlen(oct), "Incorrect endpos (%p-%p)\n", oct, endpos);
1084 res = p_strtoi64(blanks, &endpos, 10);
1085 ok(res == 12, "res != 12\n");
1086 ok(endpos == blanks+10, "Incorrect endpos (%p-%p)\n", blanks, endpos);
1087 ok(errno == 0xdeadbeef, "errno = %x\n", errno);
1090 res = p_strtoi64(overflow, &endpos, 10);
1091 ok(res == _I64_MAX, "res != _I64_MAX\n");
1092 ok(endpos == overflow+strlen(overflow), "Incorrect endpos (%p-%p)\n", overflow, endpos);
1093 ok(errno == ERANGE, "errno = %x\n", errno);
1096 res = p_strtoi64(neg_overflow, &endpos, 10);
1097 ok(res == _I64_MIN, "res != _I64_MIN\n");
1098 ok(endpos == neg_overflow+strlen(neg_overflow), "Incorrect endpos (%p-%p)\n", neg_overflow, endpos);
1099 ok(errno == ERANGE, "errno = %x\n", errno);
1102 res = p_strtoi64(no1, &endpos, 16);
1103 ok(res == 203043, "res != 203043\n");
1104 ok(endpos == no1+strlen(no1), "Incorrect endpos (%p-%p)\n", no1, endpos);
1105 res = p_strtoi64(no2, &endpos, 16);
1106 ok(res == -2175762, "res != -2175762\n");
1107 ok(endpos == no2+strlen(no2), "Incorrect endpos (%p-%p)\n", no2, endpos);
1108 res = p_strtoi64(no3, &endpos, 16);
1109 ok(res == 4778, "res != 4778\n");
1110 ok(endpos == no3+strlen(no3), "Incorrect endpos (%p-%p)\n", no3, endpos);
1111 res = p_strtoi64(no4, &endpos, 16);
1112 ok(res == 703506, "res != 703506\n");
1113 ok(endpos == no4+strlen(no4), "Incorrect endpos (%p-%p)\n", no4, endpos);
1114 res = p_strtoi64(hex, &endpos, 16);
1115 ok(res == 291, "res != 291\n");
1116 ok(endpos == hex+strlen(hex), "Incorrect endpos (%p-%p)\n", hex, endpos);
1117 res = p_strtoi64(oct, &endpos, 16);
1118 ok(res == 291, "res != 291\n");
1119 ok(endpos == oct+strlen(oct), "Incorrect endpos (%p-%p)\n", oct, endpos);
1120 res = p_strtoi64(blanks, &endpos, 16);
1121 ok(res == 18, "res != 18\n");
1122 ok(endpos == blanks+10, "Incorrect endpos (%p-%p)\n", blanks, endpos);
1123 ok(errno == 0xdeadbeef, "errno = %x\n", errno);
1126 res = p_strtoi64(hex, &endpos, 36);
1127 ok(res == 1541019, "res != 1541019\n");
1128 ok(endpos == hex+strlen(hex), "Incorrect endpos (%p-%p)\n", hex, endpos);
1129 ok(errno == 0xdeadbeef, "errno = %x\n", errno);
1132 res = p_strtoi64(no1, &endpos, 0);
1133 ok(res == 31923, "res != 31923\n");
1134 ok(endpos == no1+strlen(no1), "Incorrect endpos (%p-%p)\n", no1, endpos);
1135 res = p_strtoi64(no2, &endpos, 0);
1136 ok(res == -213312, "res != -213312\n");
1137 ok(endpos == no2+strlen(no2), "Incorrect endpos (%p-%p)\n", no2, endpos);
1138 res = p_strtoi64(no3, &endpos, 10);
1139 ok(res == 12, "res != 12\n");
1140 ok(endpos == no3+2, "Incorrect endpos (%p-%p)\n", no3, endpos);
1141 res = p_strtoi64(no4, &endpos, 10);
1142 ok(res == 0, "res != 0\n");
1143 ok(endpos == no4, "Incorrect endpos (%p-%p)\n", no4, endpos);
1144 res = p_strtoi64(hex, &endpos, 10);
1145 ok(res == 0, "res != 0\n");
1146 ok(endpos == hex+1, "Incorrect endpos (%p-%p)\n", hex, endpos);
1147 res = p_strtoi64(oct, &endpos, 10);
1148 ok(res == 123, "res != 123\n");
1149 ok(endpos == oct+strlen(oct), "Incorrect endpos (%p-%p)\n", oct, endpos);
1150 res = p_strtoi64(blanks, &endpos, 10);
1151 ok(res == 12, "res != 12\n");
1152 ok(endpos == blanks+10, "Incorrect endpos (%p-%p)\n", blanks, endpos);
1153 ok(errno == 0xdeadbeef, "errno = %x\n", errno);
1156 ures = p_strtoui64(no1, &endpos, 0);
1157 ok(ures == 31923, "ures != 31923\n");
1158 ok(endpos == no1+strlen(no1), "Incorrect endpos (%p-%p)\n", no1, endpos);
1159 ures = p_strtoui64(no2, &endpos, 0);
1160 ok(ures == -213312, "ures != -213312\n");
1161 ok(endpos == no2+strlen(no2), "Incorrect endpos (%p-%p)\n", no2, endpos);
1162 ures = p_strtoui64(no3, &endpos, 10);
1163 ok(ures == 12, "ures != 12\n");
1164 ok(endpos == no3+2, "Incorrect endpos (%p-%p)\n", no3, endpos);
1165 ures = p_strtoui64(no4, &endpos, 10);
1166 ok(ures == 0, "ures != 0\n");
1167 ok(endpos == no4, "Incorrect endpos (%p-%p)\n", no4, endpos);
1168 ures = p_strtoui64(hex, &endpos, 10);
1169 ok(ures == 0, "ures != 0\n");
1170 ok(endpos == hex+1, "Incorrect endpos (%p-%p)\n", hex, endpos);
1171 ures = p_strtoui64(oct, &endpos, 10);
1172 ok(ures == 123, "ures != 123\n");
1173 ok(endpos == oct+strlen(oct), "Incorrect endpos (%p-%p)\n", oct, endpos);
1174 ures = p_strtoui64(blanks, &endpos, 10);
1175 ok(ures == 12, "ures != 12\n");
1176 ok(endpos == blanks+10, "Incorrect endpos (%p-%p)\n", blanks, endpos);
1177 ok(errno == 0xdeadbeef, "errno = %x\n", errno);
1180 ures = p_strtoui64(overflow, &endpos, 10);
1181 ok(ures == _UI64_MAX, "ures != _UI64_MAX\n");
1182 ok(endpos == overflow+strlen(overflow), "Incorrect endpos (%p-%p)\n", overflow, endpos);
1183 ok(errno == ERANGE, "errno = %x\n", errno);
1186 ures = p_strtoui64(neg_overflow, &endpos, 10);
1187 ok(ures == 1, "ures != 1\n");
1188 ok(endpos == neg_overflow+strlen(neg_overflow), "Incorrect endpos (%p-%p)\n", neg_overflow, endpos);
1189 ok(errno == ERANGE, "errno = %x\n", errno);
1192 static inline BOOL almost_equal(double d1, double d2) {
1193 if(d1-d2>-1e-30 && d1-d2<1e-30)
1198 static void test__strtod(void)
1200 const char double1[] = "12.1";
1201 const char double2[] = "-13.721";
1202 const char double3[] = "INF";
1203 const char double4[] = ".21e12";
1204 const char double5[] = "214353e-3";
1205 const char overflow[] = "1d9999999999999999999";
1206 const char white_chars[] = " d10";
1211 d = strtod(double1, &end);
1212 ok(almost_equal(d, 12.1), "d = %lf\n", d);
1213 ok(end == double1+4, "incorrect end (%d)\n", (int)(end-double1));
1215 d = strtod(double2, &end);
1216 ok(almost_equal(d, -13.721), "d = %lf\n", d);
1217 ok(end == double2+7, "incorrect end (%d)\n", (int)(end-double2));
1219 d = strtod(double3, &end);
1220 ok(almost_equal(d, 0), "d = %lf\n", d);
1221 ok(end == double3, "incorrect end (%d)\n", (int)(end-double3));
1223 d = strtod(double4, &end);
1224 ok(almost_equal(d, 210000000000.0), "d = %lf\n", d);
1225 ok(end == double4+6, "incorrect end (%d)\n", (int)(end-double4));
1227 d = strtod(double5, &end);
1228 ok(almost_equal(d, 214.353), "d = %lf\n", d);
1229 ok(end == double5+9, "incorrect end (%d)\n", (int)(end-double5));
1231 d = strtod("12.1d2", NULL);
1232 ok(almost_equal(d, 12.1e2), "d = %lf\n", d);
1234 d = strtod(white_chars, &end);
1235 ok(almost_equal(d, 0), "d = %lf\n", d);
1236 ok(end == white_chars, "incorrect end (%d)\n", (int)(end-white_chars));
1238 /* Set locale with non '.' decimal point (',') */
1239 if(!setlocale(LC_ALL, "Polish")) {
1240 win_skip("system with limited locales\n");
1244 d = strtod("12.1", NULL);
1245 ok(almost_equal(d, 12.0), "d = %lf\n", d);
1247 d = strtod("12,1", NULL);
1248 ok(almost_equal(d, 12.1), "d = %lf\n", d);
1250 setlocale(LC_ALL, "C");
1252 /* Precision tests */
1253 d = strtod("0.1", NULL);
1254 ok(almost_equal(d, 0.1), "d = %lf\n", d);
1255 d = strtod("-0.1", NULL);
1256 ok(almost_equal(d, -0.1), "d = %lf\n", d);
1257 d = strtod("0.1281832188491894198128921", NULL);
1258 ok(almost_equal(d, 0.1281832188491894198128921), "d = %lf\n", d);
1259 d = strtod("0.82181281288121", NULL);
1260 ok(almost_equal(d, 0.82181281288121), "d = %lf\n", d);
1261 d = strtod("21921922352523587651128218821", NULL);
1262 ok(almost_equal(d, 21921922352523587651128218821.0), "d = %lf\n", d);
1263 d = strtod("0.1d238", NULL);
1264 ok(almost_equal(d, 0.1e238L), "d = %lf\n", d);
1265 d = strtod("0.1D-4736", NULL);
1266 ok(almost_equal(d, 0.1e-4736L), "d = %lf\n", d);
1269 strtod(overflow, &end);
1270 ok(errno == ERANGE, "errno = %x\n", errno);
1271 ok(end == overflow+21, "incorrect end (%d)\n", (int)(end-overflow));
1274 strtod("-1d309", NULL);
1275 ok(errno == ERANGE, "errno = %x\n", errno);
1278 static void test_mbstowcs(void)
1280 static const wchar_t wSimple[] = { 't','e','x','t',0 };
1281 static const wchar_t wHiragana[] = { 0x3042,0x3043,0 };
1282 static const char mSimple[] = "text";
1283 static const char mHiragana[] = { 0x82,0xa0,0x82,0xa1,0 };
1290 wOut[4] = '!'; wOut[5] = '\0';
1291 mOut[4] = '!'; mOut[5] = '\0';
1293 ret = mbstowcs(NULL, mSimple, 0);
1294 ok(ret == 4, "mbstowcs did not return 4\n");
1296 ret = mbstowcs(wOut, mSimple, 4);
1297 ok(ret == 4, "mbstowcs did not return 4\n");
1298 ok(!memcmp(wOut, wSimple, 4*sizeof(wchar_t)), "wOut = %s\n", wine_dbgstr_w(wOut));
1299 ok(wOut[4] == '!', "wOut[4] != \'!\'\n");
1301 ret = wcstombs(NULL, wSimple, 0);
1302 ok(ret == 4, "wcstombs did not return 4\n");
1304 ret = wcstombs(mOut, wSimple, 6);
1305 ok(ret == 4, "wcstombs did not return 4\n");
1306 ok(!memcmp(mOut, mSimple, 5*sizeof(char)), "mOut = %s\n", mOut);
1308 ret = wcstombs(mOut, wSimple, 2);
1309 ok(ret == 2, "wcstombs did not return 2\n");
1310 ok(!memcmp(mOut, mSimple, 5*sizeof(char)), "mOut = %s\n", mOut);
1312 if(!setlocale(LC_ALL, "Japanese_Japan.932")) {
1313 win_skip("Japanese_Japan.932 locale not available\n");
1317 ret = mbstowcs(wOut, mHiragana, 6);
1318 ok(ret == 2, "mbstowcs did not return 2\n");
1319 ok(!memcmp(wOut, wHiragana, sizeof(wHiragana)), "wOut = %s\n", wine_dbgstr_w(wOut));
1321 ret = wcstombs(mOut, wHiragana, 6);
1322 ok(ret == 4, "wcstombs did not return 4\n");
1323 ok(!memcmp(mOut, mHiragana, sizeof(mHiragana)), "mOut = %s\n", mOut);
1325 if(!pmbstowcs_s || !pwcstombs_s) {
1326 win_skip("mbstowcs_s or wcstombs_s not available\n");
1330 err = pmbstowcs_s(&ret, wOut, 6, mSimple, _TRUNCATE);
1331 ok(err == 0, "err = %d\n", err);
1332 ok(ret == 5, "ret = %d\n", (int)ret);
1333 ok(!memcmp(wOut, wSimple, sizeof(wSimple)), "wOut = %s\n", wine_dbgstr_w(wOut));
1335 err = pmbstowcs_s(&ret, wOut, 6, mHiragana, _TRUNCATE);
1336 ok(err == 0, "err = %d\n", err);
1337 ok(ret == 3, "ret = %d\n", (int)ret);
1338 ok(!memcmp(wOut, wHiragana, sizeof(wHiragana)), "wOut = %s\n", wine_dbgstr_w(wOut));
1340 err = pmbstowcs_s(&ret, NULL, 0, mHiragana, 1);
1341 ok(err == 0, "err = %d\n", err);
1342 ok(ret == 3, "ret = %d\n", (int)ret);
1344 err = pwcstombs_s(&ret, mOut, 6, wSimple, _TRUNCATE);
1345 ok(err == 0, "err = %d\n", err);
1346 ok(ret == 5, "ret = %d\n", (int)ret);
1347 ok(!memcmp(mOut, mSimple, sizeof(mSimple)), "mOut = %s\n", mOut);
1349 err = pwcstombs_s(&ret, mOut, 6, wHiragana, _TRUNCATE);
1350 ok(err == 0, "err = %d\n", err);
1351 ok(ret == 5, "ret = %d\n", (int)ret);
1352 ok(!memcmp(mOut, mHiragana, sizeof(mHiragana)), "mOut = %s\n", mOut);
1354 err = pwcstombs_s(&ret, NULL, 0, wHiragana, 1);
1355 ok(err == 0, "err = %d\n", err);
1356 ok(ret == 5, "ret = %d\n", (int)ret);
1359 static void test_gcvt(void)
1361 char buf[1024], *res;
1365 win_skip("Skipping _gcvt tests\n");
1370 res = _gcvt(1.2, -1, buf);
1371 ok(res == NULL, "res != NULL\n");
1372 ok(errno == ERANGE, "errno = %d\n", errno);
1375 res = _gcvt(1.2, 5, NULL);
1376 ok(res == NULL, "res != NULL\n");
1377 ok(errno == EINVAL, "errno = %d\n", errno);
1379 res = gcvt(1.2, 5, buf);
1380 ok(res == buf, "res != buf\n");
1381 ok(!strcmp(buf, "1.2"), "buf = %s\n", buf);
1384 err = p_gcvt_s(buf, 5, 1.2, 10);
1385 ok(err == ERANGE, "err = %d\n", err);
1386 ok(buf[0] == '\0', "buf[0] = %c\n", buf[0]);
1389 err = p_gcvt_s(buf, 4, 123456, 2);
1390 ok(err == ERANGE, "err = %d\n", err);
1391 ok(buf[0] == '\0', "buf[0] = %c\n", buf[0]);
1394 static void test__itoa_s(void)
1401 win_skip("Skipping _itoa_s tests\n");
1405 if (p_set_invalid_parameter_handler)
1406 ok(p_set_invalid_parameter_handler(test_invalid_parameter_handler) == NULL,
1407 "Invalid parameter handler was already set\n");
1410 ret = p_itoa_s(0, NULL, 0, 0);
1411 ok(ret == EINVAL, "Expected _itoa_s to return EINVAL, got %d\n", ret);
1412 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1414 memset(buffer, 'X', sizeof(buffer));
1416 ret = p_itoa_s(0, buffer, 0, 0);
1417 ok(ret == EINVAL, "Expected _itoa_s to return EINVAL, got %d\n", ret);
1418 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1419 ok(buffer[0] == 'X', "Expected the output buffer to be untouched\n");
1421 memset(buffer, 'X', sizeof(buffer));
1423 ret = p_itoa_s(0, buffer, sizeof(buffer), 0);
1424 ok(ret == EINVAL, "Expected _itoa_s to return EINVAL, got %d\n", ret);
1425 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1426 ok(buffer[0] == '\0', "Expected the output buffer to be null terminated\n");
1428 memset(buffer, 'X', sizeof(buffer));
1430 ret = p_itoa_s(0, buffer, sizeof(buffer), 64);
1431 ok(ret == EINVAL, "Expected _itoa_s to return EINVAL, got %d\n", ret);
1432 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1433 ok(buffer[0] == '\0', "Expected the output buffer to be null terminated\n");
1435 memset(buffer, 'X', sizeof(buffer));
1437 ret = p_itoa_s(12345678, buffer, 4, 10);
1438 ok(ret == ERANGE, "Expected _itoa_s to return ERANGE, got %d\n", ret);
1439 ok(errno == ERANGE, "Expected errno to be ERANGE, got %d\n", errno);
1440 ok(!memcmp(buffer, "\000765", 4),
1441 "Expected the output buffer to be null terminated with truncated output\n");
1443 memset(buffer, 'X', sizeof(buffer));
1445 ret = p_itoa_s(12345678, buffer, 8, 10);
1446 ok(ret == ERANGE, "Expected _itoa_s to return ERANGE, got %d\n", ret);
1447 ok(errno == ERANGE, "Expected errno to be ERANGE, got %d\n", errno);
1448 ok(!memcmp(buffer, "\0007654321", 8),
1449 "Expected the output buffer to be null terminated with truncated output\n");
1451 memset(buffer, 'X', sizeof(buffer));
1453 ret = p_itoa_s(-12345678, buffer, 9, 10);
1454 ok(ret == ERANGE, "Expected _itoa_s to return ERANGE, got %d\n", ret);
1455 ok(errno == ERANGE, "Expected errno to be ERANGE, got %d\n", errno);
1456 ok(!memcmp(buffer, "\00087654321", 9),
1457 "Expected the output buffer to be null terminated with truncated output\n");
1459 ret = p_itoa_s(12345678, buffer, 9, 10);
1460 ok(ret == 0, "Expected _itoa_s to return 0, got %d\n", ret);
1461 ok(!strcmp(buffer, "12345678"),
1462 "Expected output buffer string to be \"12345678\", got \"%s\"\n",
1465 ret = p_itoa_s(43690, buffer, sizeof(buffer), 2);
1466 ok(ret == 0, "Expected _itoa_s to return 0, got %d\n", ret);
1467 ok(!strcmp(buffer, "1010101010101010"),
1468 "Expected output buffer string to be \"1010101010101010\", got \"%s\"\n",
1471 ret = p_itoa_s(1092009, buffer, sizeof(buffer), 36);
1472 ok(ret == 0, "Expected _itoa_s to return 0, got %d\n", ret);
1473 ok(!strcmp(buffer, "nell"),
1474 "Expected output buffer string to be \"nell\", got \"%s\"\n",
1477 ret = p_itoa_s(5704, buffer, sizeof(buffer), 18);
1478 ok(ret == 0, "Expected _itoa_s to return 0, got %d\n", ret);
1479 ok(!strcmp(buffer, "hag"),
1480 "Expected output buffer string to be \"hag\", got \"%s\"\n",
1483 ret = p_itoa_s(-12345678, buffer, sizeof(buffer), 10);
1484 ok(ret == 0, "Expected _itoa_s to return 0, got %d\n", ret);
1485 ok(!strcmp(buffer, "-12345678"),
1486 "Expected output buffer string to be \"-12345678\", got \"%s\"\n",
1488 if (p_set_invalid_parameter_handler)
1489 ok(p_set_invalid_parameter_handler(NULL) == test_invalid_parameter_handler,
1490 "Cannot reset invalid parameter handler\n");
1493 static void test__strlwr_s(void)
1500 win_skip("Skipping _strlwr_s tests\n");
1505 ret = p_strlwr_s(NULL, 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);
1510 ret = p_strlwr_s(NULL, sizeof(buffer));
1511 ok(ret == EINVAL, "Expected _strlwr_s to return EINVAL, got %d\n", ret);
1512 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1515 ret = p_strlwr_s(buffer, 0);
1516 ok(ret == EINVAL, "Expected _strlwr_s to return EINVAL, got %d\n", ret);
1517 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1519 strcpy(buffer, "GoRrIsTeR");
1521 ret = p_strlwr_s(buffer, 5);
1522 ok(ret == EINVAL, "Expected _strlwr_s to return EINVAL, got %d\n", ret);
1523 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1524 ok(!memcmp(buffer, "\0oRrIsTeR", sizeof("\0oRrIsTeR")),
1525 "Expected the output buffer to be \"gorrIsTeR\"\n");
1527 strcpy(buffer, "GoRrIsTeR");
1529 ret = p_strlwr_s(buffer, sizeof("GoRrIsTeR") - 1);
1530 ok(ret == EINVAL, "Expected _strlwr_s to return EINVAL, got %d\n", ret);
1531 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1532 ok(!memcmp(buffer, "\0oRrIsTeR", sizeof("\0oRrIsTeR")),
1533 "Expected the output buffer to be \"gorrIsTeR\"\n");
1535 strcpy(buffer, "GoRrIsTeR");
1536 ret = p_strlwr_s(buffer, sizeof("GoRrIsTeR"));
1537 ok(ret == 0, "Expected _strlwr_s to return 0, got %d\n", ret);
1538 ok(!strcmp(buffer, "gorrister"),
1539 "Expected the output buffer to be \"gorrister\", got \"%s\"\n",
1542 memcpy(buffer, "GoRrIsTeR\0ELLEN", sizeof("GoRrIsTeR\0ELLEN"));
1543 ret = p_strlwr_s(buffer, sizeof(buffer));
1544 ok(ret == 0, "Expected _strlwr_s to return 0, got %d\n", ret);
1545 ok(!memcmp(buffer, "gorrister\0ELLEN", sizeof("gorrister\0ELLEN")),
1546 "Expected the output buffer to be \"gorrister\\0ELLEN\", got \"%s\"\n",
1550 static void test_wcsncat_s(void)
1552 static wchar_t abcW[] = {'a','b','c',0};
1559 win_skip("skipping wcsncat_s tests\n");
1563 if (p_set_invalid_parameter_handler)
1564 ok(p_set_invalid_parameter_handler(test_invalid_parameter_handler) == NULL,
1565 "Invalid parameter handler was already set\n");
1567 memcpy(src, abcW, sizeof(abcW));
1569 ret = p_wcsncat_s(NULL, 4, src, 4);
1570 ok(ret == EINVAL, "err = %d\n", ret);
1571 ret = p_wcsncat_s(dst, 0, src, 4);
1572 ok(ret == EINVAL, "err = %d\n", ret);
1573 ret = p_wcsncat_s(dst, 0, src, _TRUNCATE);
1574 ok(ret == EINVAL, "err = %d\n", ret);
1575 ret = p_wcsncat_s(dst, 4, NULL, 0);
1576 ok(ret == 0, "err = %d\n", ret);
1579 ret = p_wcsncat_s(dst, 2, src, 4);
1580 ok(ret == ERANGE, "err = %d\n", ret);
1583 ret = p_wcsncat_s(dst, 2, src, _TRUNCATE);
1584 ok(ret == STRUNCATE, "err = %d\n", ret);
1585 ok(dst[0] == 'a' && dst[1] == 0, "dst is %s\n", wine_dbgstr_w(dst));
1587 memcpy(dst, abcW, sizeof(abcW));
1589 ret = p_wcsncat_s(dst, 4, src, 4);
1590 ok(ret == EINVAL, "err = %d\n", ret);
1592 if (p_set_invalid_parameter_handler)
1593 ok(p_set_invalid_parameter_handler(NULL) == test_invalid_parameter_handler,
1594 "Cannot reset invalid parameter handler\n");
1597 static void test__mbsnbcat_s(void)
1599 unsigned char dest[16];
1600 const unsigned char first[] = "dinosaur";
1601 const unsigned char second[] = "duck";
1606 win_skip("Skipping _mbsnbcat_s tests\n");
1610 /* Test invalid arguments. */
1611 ret = p_mbsnbcat_s(NULL, 0, NULL, 0);
1612 ok(ret == 0, "Expected _mbsnbcat_s to return 0, got %d\n", ret);
1615 ret = p_mbsnbcat_s(NULL, 10, NULL, 0);
1616 ok(ret == EINVAL, "Expected _mbsnbcat_s to return EINVAL, got %d\n", ret);
1617 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1620 ret = p_mbsnbcat_s(NULL, 0, NULL, 10);
1621 ok(ret == EINVAL, "Expected _mbsnbcat_s to return EINVAL, got %d\n", ret);
1622 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1624 memset(dest, 'X', sizeof(dest));
1626 ret = p_mbsnbcat_s(dest, 0, NULL, 0);
1627 ok(ret == EINVAL, "Expected _mbsnbcat_s to return EINVAL, got %d\n", ret);
1628 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1629 ok(dest[0] == 'X', "Expected the output buffer to be untouched\n");
1631 memset(dest, 'X', sizeof(dest));
1633 ret = p_mbsnbcat_s(dest, 0, second, 0);
1634 ok(ret == EINVAL, "Expected _mbsnbcat_s to return EINVAL, got %d\n", ret);
1635 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1636 ok(dest[0] == 'X', "Expected the output buffer to be untouched\n");
1638 memset(dest, 'X', sizeof(dest));
1640 ret = p_mbsnbcat_s(dest, sizeof(dest), NULL, 0);
1641 ok(ret == EINVAL, "Expected _mbsnbcat_s to return EINVAL, got %d\n", ret);
1642 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1643 ok(dest[0] == '\0', "Expected the output buffer to be null terminated\n");
1645 memset(dest, 'X', sizeof(dest));
1647 ret = p_mbsnbcat_s(dest, sizeof(dest), NULL, 10);
1648 ok(ret == EINVAL, "Expected _mbsnbcat_s to return EINVAL, got %d\n", ret);
1649 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1650 ok(dest[0] == '\0', "Expected the output buffer to be null terminated\n");
1652 memset(dest, 'X', sizeof(dest));
1654 ret = p_mbsnbcat_s(dest, sizeof(dest), second, sizeof(second));
1655 ok(ret == 0, "Expected _mbsnbcat_s to return 0, got %d\n", ret);
1656 ok(!memcmp(dest, second, sizeof(second)),
1657 "Expected the output buffer string to be \"duck\"\n");
1659 /* Test source truncation behavior. */
1660 memset(dest, 'X', sizeof(dest));
1661 memcpy(dest, first, sizeof(first));
1662 ret = p_mbsnbcat_s(dest, sizeof(dest), second, 0);
1663 ok(ret == 0, "Expected _mbsnbcat_s to return 0, got %d\n", ret);
1664 ok(!memcmp(dest, first, sizeof(first)),
1665 "Expected the output buffer string to be \"dinosaur\"\n");
1667 memset(dest, 'X', sizeof(dest));
1668 memcpy(dest, first, sizeof(first));
1669 ret = p_mbsnbcat_s(dest, sizeof(dest), second, sizeof(second));
1670 ok(ret == 0, "Expected _mbsnbcat_s to return 0, got %d\n", ret);
1671 ok(!memcmp(dest, "dinosaurduck", sizeof("dinosaurduck")),
1672 "Expected the output buffer string to be \"dinosaurduck\"\n");
1674 memset(dest, 'X', sizeof(dest));
1675 memcpy(dest, first, sizeof(first));
1676 ret = p_mbsnbcat_s(dest, sizeof(dest), second, sizeof(second) + 1);
1677 ok(ret == 0, "Expected _mbsnbcat_s to return 0, got %d\n", ret);
1678 ok(!memcmp(dest, "dinosaurduck", sizeof("dinosaurduck")),
1679 "Expected the output buffer string to be \"dinosaurduck\"\n");
1681 memset(dest, 'X', sizeof(dest));
1682 memcpy(dest, first, sizeof(first));
1683 ret = p_mbsnbcat_s(dest, sizeof(dest), second, sizeof(second) - 1);
1684 ok(ret == 0, "Expected _mbsnbcat_s to return 0, got %d\n", ret);
1685 ok(!memcmp(dest, "dinosaurduck", sizeof("dinosaurduck")),
1686 "Expected the output buffer string to be \"dinosaurduck\"\n");
1688 memset(dest, 'X', sizeof(dest));
1689 memcpy(dest, first, sizeof(first));
1690 ret = p_mbsnbcat_s(dest, sizeof(dest), second, sizeof(second) - 2);
1691 ok(ret == 0, "Expected _mbsnbcat_s to return 0, got %d\n", ret);
1692 ok(!memcmp(dest, "dinosaurduc", sizeof("dinosaurduc")),
1693 "Expected the output buffer string to be \"dinosaurduc\"\n");
1695 /* Test destination truncation behavior. */
1696 memset(dest, 'X', sizeof(dest));
1697 memcpy(dest, first, sizeof(first));
1699 ret = p_mbsnbcat_s(dest, sizeof(first) - 1, second, sizeof(second));
1700 ok(ret == EINVAL, "Expected _mbsnbcat_s to return EINVAL, got %d\n", ret);
1701 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1702 ok(!memcmp(dest, "\0inosaur", sizeof("\0inosaur") - 1),
1703 "Expected the output buffer string to be \"\\0inosaur\" without ending null terminator\n");
1705 memset(dest, 'X', sizeof(dest));
1706 memcpy(dest, first, sizeof(first));
1708 ret = p_mbsnbcat_s(dest, sizeof(first), second, sizeof(second));
1709 ok(ret == ERANGE, "Expected _mbsnbcat_s to return ERANGE, got %d\n", ret);
1710 ok(errno == ERANGE, "Expected errno to be ERANGE, got %d\n", errno);
1711 ok(!memcmp(dest, "\0inosaurd", sizeof("\0inosaurd") - 1),
1712 "Expected the output buffer string to be \"\\0inosaurd\" without ending null terminator\n");
1714 memset(dest, 'X', sizeof(dest));
1715 memcpy(dest, first, sizeof(first));
1717 ret = p_mbsnbcat_s(dest, sizeof(first) + 1, second, sizeof(second));
1718 ok(ret == ERANGE, "Expected _mbsnbcat_s to return ERANGE, got %d\n", ret);
1719 ok(errno == ERANGE, "Expected errno to be ERANGE, got %d\n", errno);
1720 ok(!memcmp(dest, "\0inosaurdu", sizeof("\0inosaurdu") - 1),
1721 "Expected the output buffer string to be \"\\0inosaurdu\" without ending null terminator\n");
1724 static void test__mbsupr_s(void)
1727 unsigned char buffer[20];
1731 win_skip("Skipping _mbsupr_s tests\n");
1736 ret = p_mbsupr_s(NULL, 0);
1737 ok(ret == 0, "Expected _mbsupr_s to return 0, got %d\n", ret);
1740 ret = p_mbsupr_s(NULL, sizeof(buffer));
1741 ok(ret == EINVAL, "Expected _mbsupr_s to return EINVAL, got %d\n", ret);
1742 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1745 ret = p_mbsupr_s(buffer, 0);
1746 ok(ret == EINVAL, "Expected _mbsupr_s to return EINVAL, got %d\n", ret);
1747 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1749 memcpy(buffer, "abcdefgh", sizeof("abcdefgh"));
1751 ret = p_mbsupr_s(buffer, sizeof("abcdefgh"));
1752 ok(ret == 0, "Expected _mbsupr_s to return 0, got %d\n", ret);
1753 ok(!memcmp(buffer, "ABCDEFGH", sizeof("ABCDEFGH")),
1754 "Expected the output buffer to be \"ABCDEFGH\", got \"%s\"\n",
1757 memcpy(buffer, "abcdefgh", sizeof("abcdefgh"));
1759 ret = p_mbsupr_s(buffer, sizeof(buffer));
1760 ok(ret == 0, "Expected _mbsupr_s to return 0, got %d\n", ret);
1761 ok(!memcmp(buffer, "ABCDEFGH", sizeof("ABCDEFGH")),
1762 "Expected the output buffer to be \"ABCDEFGH\", got \"%s\"\n",
1765 memcpy(buffer, "abcdefgh", sizeof("abcdefgh"));
1767 ret = p_mbsupr_s(buffer, 4);
1768 ok(ret == EINVAL, "Expected _mbsupr_s to return EINVAL, got %d\n", ret);
1769 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1771 memcpy(buffer, "abcdefgh\0ijklmnop", sizeof("abcdefgh\0ijklmnop"));
1773 ret = p_mbsupr_s(buffer, sizeof(buffer));
1774 ok(ret == 0, "Expected _mbsupr_s to return 0, got %d\n", ret);
1775 ok(!memcmp(buffer, "ABCDEFGH\0ijklmnop", sizeof("ABCDEFGH\0ijklmnop")),
1776 "Expected the output buffer to be \"ABCDEFGH\\0ijklmnop\", got \"%s\"\n",
1781 static void test__mbslwr_s(void)
1784 unsigned char buffer[20];
1788 win_skip("Skipping _mbslwr_s tests\n");
1793 ret = p_mbslwr_s(NULL, 0);
1794 ok(ret == 0, "Expected _mbslwr_s to return 0, got %d\n", ret);
1797 ret = p_mbslwr_s(NULL, sizeof(buffer));
1798 ok(ret == EINVAL, "Expected _mbslwr_s to return EINVAL, got %d\n", ret);
1799 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1802 ret = p_mbslwr_s(buffer, 0);
1803 ok(ret == EINVAL, "Expected _mbslwr_s to return EINVAL, got %d\n", ret);
1804 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1806 memcpy(buffer, "ABCDEFGH", sizeof("ABCDEFGH"));
1808 ret = p_mbslwr_s(buffer, sizeof("ABCDEFGH"));
1809 ok(ret == 0, "Expected _mbslwr_s to return 0, got %d\n", ret);
1810 ok(!memcmp(buffer, "abcdefgh", sizeof("abcdefgh")),
1811 "Expected the output buffer to be \"abcdefgh\", got \"%s\"\n",
1814 memcpy(buffer, "ABCDEFGH", sizeof("ABCDEFGH"));
1816 ret = p_mbslwr_s(buffer, sizeof(buffer));
1817 ok(ret == 0, "Expected _mbslwr_s to return 0, got %d\n", ret);
1818 ok(!memcmp(buffer, "abcdefgh", sizeof("abcdefgh")),
1819 "Expected the output buffer to be \"abcdefgh\", got \"%s\"\n",
1822 memcpy(buffer, "ABCDEFGH", sizeof("ABCDEFGH"));
1824 ret = p_mbslwr_s(buffer, 4);
1825 ok(ret == EINVAL, "Expected _mbslwr_s to return EINVAL, got %d\n", ret);
1826 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1828 memcpy(buffer, "ABCDEFGH\0IJKLMNOP", sizeof("ABCDEFGH\0IJKLMNOP"));
1830 ret = p_mbslwr_s(buffer, sizeof(buffer));
1831 ok(ret == 0, "Expected _mbslwr_s to return 0, got %d\n", ret);
1832 ok(!memcmp(buffer, "abcdefgh\0IJKLMNOP", sizeof("abcdefgh\0IJKLMNOP")),
1833 "Expected the output buffer to be \"abcdefgh\\0IJKLMNOP\", got \"%s\"\n",
1838 static void test__ultoa_s(void)
1845 win_skip("Skipping _ultoa_s tests\n");
1850 ret = p_ultoa_s(0, NULL, 0, 0);
1851 ok(ret == EINVAL, "Expected _ultoa_s to return EINVAL, got %d\n", ret);
1852 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1854 memset(buffer, 'X', sizeof(buffer));
1856 ret = p_ultoa_s(0, buffer, 0, 0);
1857 ok(ret == EINVAL, "Expected _ultoa_s to return EINVAL, got %d\n", ret);
1858 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1859 ok(buffer[0] == 'X', "Expected the output buffer to be untouched\n");
1861 memset(buffer, 'X', sizeof(buffer));
1863 ret = p_ultoa_s(0, buffer, sizeof(buffer), 0);
1864 ok(ret == EINVAL, "Expected _ultoa_s to return EINVAL, got %d\n", ret);
1865 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1866 ok(buffer[0] == '\0', "Expected the output buffer to be null terminated\n");
1868 memset(buffer, 'X', sizeof(buffer));
1870 ret = p_ultoa_s(0, buffer, sizeof(buffer), 64);
1871 ok(ret == EINVAL, "Expected _ultoa_s to return EINVAL, got %d\n", ret);
1872 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1873 ok(buffer[0] == '\0', "Expected the output buffer to be null terminated\n");
1875 memset(buffer, 'X', sizeof(buffer));
1877 ret = p_ultoa_s(12345678, buffer, 4, 10);
1878 ok(ret == ERANGE, "Expected _ultoa_s to return ERANGE, got %d\n", ret);
1879 ok(errno == ERANGE, "Expected errno to be ERANGE, got %d\n", errno);
1880 ok(!memcmp(buffer, "\000765", 4),
1881 "Expected the output buffer to be null terminated with truncated output\n");
1883 memset(buffer, 'X', sizeof(buffer));
1885 ret = p_ultoa_s(12345678, buffer, 8, 10);
1886 ok(ret == ERANGE, "Expected _ultoa_s to return ERANGE, got %d\n", ret);
1887 ok(errno == ERANGE, "Expected errno to be ERANGE, got %d\n", errno);
1888 ok(!memcmp(buffer, "\0007654321", 8),
1889 "Expected the output buffer to be null terminated with truncated output\n");
1891 ret = p_ultoa_s(12345678, buffer, 9, 10);
1892 ok(ret == 0, "Expected _ultoa_s to return 0, got %d\n", ret);
1893 ok(!strcmp(buffer, "12345678"),
1894 "Expected output buffer string to be \"12345678\", got \"%s\"\n",
1897 ret = p_ultoa_s(43690, buffer, sizeof(buffer), 2);
1898 ok(ret == 0, "Expected _ultoa_s to return 0, got %d\n", ret);
1899 ok(!strcmp(buffer, "1010101010101010"),
1900 "Expected output buffer string to be \"1010101010101010\", got \"%s\"\n",
1903 ret = p_ultoa_s(1092009, buffer, sizeof(buffer), 36);
1904 ok(ret == 0, "Expected _ultoa_s to return 0, got %d\n", ret);
1905 ok(!strcmp(buffer, "nell"),
1906 "Expected output buffer string to be \"nell\", got \"%s\"\n",
1909 ret = p_ultoa_s(5704, buffer, sizeof(buffer), 18);
1910 ok(ret == 0, "Expected _ultoa_s to return 0, got %d\n", ret);
1911 ok(!strcmp(buffer, "hag"),
1912 "Expected output buffer string to be \"hag\", got \"%s\"\n",
1919 static const char xilstring[]="c:/xilinx";
1922 hMsvcrt = GetModuleHandleA("msvcrt.dll");
1924 hMsvcrt = GetModuleHandleA("msvcrtd.dll");
1925 ok(hMsvcrt != 0, "GetModuleHandleA failed\n");
1926 SET(pmemcpy,"memcpy");
1927 SET(pmemcmp,"memcmp");
1928 SET(p_mbctype,"_mbctype");
1929 SET(p__mb_cur_max,"__mb_cur_max");
1930 pstrcpy_s = (void *)GetProcAddress( hMsvcrt,"strcpy_s" );
1931 pstrcat_s = (void *)GetProcAddress( hMsvcrt,"strcat_s" );
1932 p_mbsnbcat_s = (void *)GetProcAddress( hMsvcrt,"_mbsnbcat_s" );
1933 p_mbsnbcpy_s = (void *)GetProcAddress( hMsvcrt,"_mbsnbcpy_s" );
1934 p_wcscpy_s = (void *)GetProcAddress( hMsvcrt,"wcscpy_s" );
1935 p_wcsncat_s = (void *)GetProcAddress( hMsvcrt,"wcsncat_s" );
1936 p_wcsupr_s = (void *)GetProcAddress( hMsvcrt,"_wcsupr_s" );
1937 p_strnlen = (void *)GetProcAddress( hMsvcrt,"strnlen" );
1938 p_strtoi64 = (void *)GetProcAddress(hMsvcrt, "_strtoi64");
1939 p_strtoui64 = (void *)GetProcAddress(hMsvcrt, "_strtoui64");
1940 pmbstowcs_s = (void *)GetProcAddress(hMsvcrt, "mbstowcs_s");
1941 pwcstombs_s = (void *)GetProcAddress(hMsvcrt, "wcstombs_s");
1942 p_gcvt_s = (void *)GetProcAddress(hMsvcrt, "_gcvt_s");
1943 p_itoa_s = (void *)GetProcAddress(hMsvcrt, "_itoa_s");
1944 p_strlwr_s = (void *)GetProcAddress(hMsvcrt, "_strlwr_s");
1945 p_ultoa_s = (void *)GetProcAddress(hMsvcrt, "_ultoa_s");
1946 p_set_invalid_parameter_handler = (void *) GetProcAddress(hMsvcrt, "_set_invalid_parameter_handler");
1947 p_wcslwr_s = (void*)GetProcAddress(hMsvcrt, "_wcslwr_s");
1948 p_mbsupr_s = (void*)GetProcAddress(hMsvcrt, "_mbsupr_s");
1949 p_mbslwr_s = (void*)GetProcAddress(hMsvcrt, "_mbslwr_s");
1951 /* MSVCRT memcpy behaves like memmove for overlapping moves,
1952 MFC42 CString::Insert seems to rely on that behaviour */
1953 strcpy(mem,xilstring);
1954 nLen=strlen(xilstring);
1955 pmemcpy(mem+5, mem,nLen+1);
1956 ok(pmemcmp(mem+5,xilstring, nLen) == 0,
1957 "Got result %s\n",mem+5);
1959 /* Test _swab function */