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