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