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