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