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