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