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