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