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