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