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