Release 1.3.7.
[wine/gsoc-2012-control.git] / dlls / msvcrt / tests / string.c
blobaa57b31cc1348df4721ed9c31411df35b3cb7da9
1 /*
2 * Unit test suite for string functions.
4 * Copyright 2004 Uwe Bonnes
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.
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.
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
21 #include "wine/test.h"
22 #include "winbase.h"
23 #include <string.h>
24 #include <mbstring.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <mbctype.h>
28 #include <locale.h>
29 #include <errno.h>
30 #include <limits.h>
32 static char *buf_to_string(const unsigned char *bin, int len, int nr)
34 static char buf[2][1024];
35 char *w = buf[nr];
36 int i;
38 for (i = 0; i < len; i++)
40 sprintf(w, "%02x ", (unsigned char)bin[i]);
41 w += strlen(w);
43 return buf[nr];
46 static void __cdecl test_invalid_parameter_handler(const wchar_t *expression,
47 const wchar_t *function, const wchar_t *file,
48 unsigned line, uintptr_t arg)
50 /* we just ignore handler calls */
53 #define expect_eq(expr, value, type, format) { type ret = (expr); ok((value) == ret, #expr " expected " format " got " format "\n", value, ret); }
54 #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 static void* (__cdecl *pmemcpy)(void *, const void *, size_t n);
57 static int* (__cdecl *pmemcmp)(void *, const void *, size_t n);
58 static int (__cdecl *pstrcpy_s)(char *dst, size_t len, const char *src);
59 static int (__cdecl *pstrcat_s)(char *dst, size_t len, const char *src);
60 static int (__cdecl *p_mbsnbcat_s)(unsigned char *dst, size_t size, const unsigned char *src, size_t count);
61 static int (__cdecl *p_mbsnbcpy_s)(unsigned char * dst, size_t size, const unsigned char * src, size_t count);
62 static int (__cdecl *p_wcscpy_s)(wchar_t *wcDest, size_t size, const wchar_t *wcSrc);
63 static int (__cdecl *p_wcsncat_s)(wchar_t *dst, size_t elem, const wchar_t *src, size_t count);
64 static int (__cdecl *p_wcsupr_s)(wchar_t *str, size_t size);
65 static size_t (__cdecl *p_strnlen)(const char *, size_t);
66 static __int64 (__cdecl *p_strtoi64)(const char *, char **, int);
67 static unsigned __int64 (__cdecl *p_strtoui64)(const char *, char **, int);
68 static int (__cdecl *pwcstombs_s)(size_t*,char*,size_t,const wchar_t*,size_t);
69 static int (__cdecl *pmbstowcs_s)(size_t*,wchar_t*,size_t,const char*,size_t);
70 static errno_t (__cdecl *p_gcvt_s)(char*,size_t,double,int);
71 static errno_t (__cdecl *p_itoa_s)(int,char*,size_t,int);
72 static errno_t (__cdecl *p_strlwr_s)(char*,size_t);
73 static errno_t (__cdecl *p_ultoa_s)(__msvcrt_ulong,char*,size_t,int);
74 static int *p__mb_cur_max;
75 static unsigned char *p_mbctype;
76 static _invalid_parameter_handler (__cdecl *p_set_invalid_parameter_handler)(_invalid_parameter_handler);
78 #define SETNOFAIL(x,y) x = (void*)GetProcAddress(hMsvcrt,y)
79 #define SET(x,y) SETNOFAIL(x,y); ok(x != NULL, "Export '%s' not found\n", y)
81 static HMODULE hMsvcrt;
83 static void test_swab( void ) {
84 char original[] = "BADCFEHGJILKNMPORQTSVUXWZY@#";
85 char expected1[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ@#";
86 char expected2[] = "ABCDEFGHIJKLMNOPQRSTUVWX$";
87 char expected3[] = "$";
89 char from[30];
90 char to[30];
92 int testsize;
94 /* Test 1 - normal even case */
95 memset(to,'$', sizeof(to));
96 memset(from,'@', sizeof(from));
97 testsize = 26;
98 memcpy(from, original, testsize);
99 _swab( from, to, testsize );
100 ok(memcmp(to,expected1,testsize) == 0, "Testing even size %d returned '%*.*s'\n", testsize, testsize, testsize, to);
102 /* Test 2 - uneven case */
103 memset(to,'$', sizeof(to));
104 memset(from,'@', sizeof(from));
105 testsize = 25;
106 memcpy(from, original, testsize);
107 _swab( from, to, testsize );
108 ok(memcmp(to,expected2,testsize) == 0, "Testing odd size %d returned '%*.*s'\n", testsize, testsize, testsize, to);
110 /* Test 3 - from = to */
111 memset(to,'$', sizeof(to));
112 memset(from,'@', sizeof(from));
113 testsize = 26;
114 memcpy(to, original, testsize);
115 _swab( to, to, testsize );
116 ok(memcmp(to,expected1,testsize) == 0, "Testing overlapped size %d returned '%*.*s'\n", testsize, testsize, testsize, to);
118 /* Test 4 - 1 bytes */
119 memset(to,'$', sizeof(to));
120 memset(from,'@', sizeof(from));
121 testsize = 1;
122 memcpy(from, original, testsize);
123 _swab( from, to, testsize );
124 ok(memcmp(to,expected3,testsize) == 0, "Testing small size %d returned '%*.*s'\n", testsize, testsize, testsize, to);
127 #if 0 /* use this to generate more tests */
129 static void test_codepage(int cp)
131 int i;
132 int prev;
133 int count = 1;
135 ok(_setmbcp(cp) == 0, "Couldn't set mbcp\n");
137 prev = p_mbctype[0];
138 printf("static int result_cp_%d_mbctype[] = { ", cp);
139 for (i = 1; i < 257; i++)
141 if (p_mbctype[i] != prev)
143 printf("0x%x,%d, ", prev, count);
144 prev = p_mbctype[i];
145 count = 1;
147 else
148 count++;
150 printf("0x%x,%d };\n", prev, count);
153 #else
155 /* RLE-encoded mbctype tables for given codepages */
156 static int result_cp_932_mbctype[] = { 0x0,65, 0x8,1, 0x18,26, 0x8,6, 0x28,26, 0x8,4,
157 0x0,1, 0x8,1, 0xc,31, 0x8,1, 0xa,5, 0x9,58, 0xc,29, 0,3 };
158 static int result_cp_936_mbctype[] = { 0x0,65, 0x8,1, 0x18,26, 0x8,6, 0x28,26, 0x8,6,
159 0xc,126, 0,1 };
160 static int result_cp_949_mbctype[] = { 0x0,66, 0x18,26, 0x8,6, 0x28,26, 0x8,6, 0xc,126,
161 0,1 };
162 static int result_cp_950_mbctype[] = { 0x0,65, 0x8,1, 0x18,26, 0x8,6, 0x28,26, 0x8,4,
163 0x0,2, 0x4,32, 0xc,94, 0,1 };
165 static void test_cp_table(int cp, int *result)
167 int i;
168 int count = 0;
169 int curr = 0;
170 _setmbcp(cp);
171 for (i = 0; i < 256; i++)
173 if (count == 0)
175 curr = result[0];
176 count = result[1];
177 result += 2;
179 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);
180 count--;
184 #define test_codepage(num) test_cp_table(num, result_cp_##num##_mbctype);
186 #endif
188 static void test_mbcp(void)
190 int mb_orig_max = *p__mb_cur_max;
191 int curr_mbcp = _getmbcp();
192 unsigned char *mbstring = (unsigned char *)"\xb0\xb1\xb2 \xb3\xb4 \xb5"; /* incorrect string */
193 unsigned char *mbstring2 = (unsigned char *)"\xb0\xb1\xb2\xb3Q\xb4\xb5"; /* correct string */
194 unsigned char *mbsonlylead = (unsigned char *)"\xb0\0\xb1\xb2 \xb3";
195 unsigned char buf[16];
196 int step;
198 /* _mbtype tests */
200 /* An SBCS codepage test. The ctype of characters on e.g. CP1252 or CP1250 differs slightly
201 * between versions of Windows. Also Windows 9x seems to ignore the codepage and always uses
202 * CP1252 (or the ACP?) so we test only a few ASCII characters */
203 _setmbcp(1252);
204 expect_eq(p_mbctype[10], 0, char, "%x");
205 expect_eq(p_mbctype[50], 0, char, "%x");
206 expect_eq(p_mbctype[66], _SBUP, char, "%x");
207 expect_eq(p_mbctype[100], _SBLOW, char, "%x");
208 expect_eq(p_mbctype[128], 0, char, "%x");
209 _setmbcp(1250);
210 expect_eq(p_mbctype[10], 0, char, "%x");
211 expect_eq(p_mbctype[50], 0, char, "%x");
212 expect_eq(p_mbctype[66], _SBUP, char, "%x");
213 expect_eq(p_mbctype[100], _SBLOW, char, "%x");
214 expect_eq(p_mbctype[128], 0, char, "%x");
216 /* double byte code pages */
217 test_codepage(932);
218 test_codepage(936);
219 test_codepage(949);
220 test_codepage(950);
222 _setmbcp(936);
223 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);
224 ok(_ismbblead('\354'), "\354 should be a lead byte\n");
225 ok(_ismbblead(' ') == FALSE, "' ' should not be a lead byte\n");
226 ok(_ismbblead(0x1234b0), "0x1234b0 should not be a lead byte\n");
227 ok(_ismbblead(0x123420) == FALSE, "0x123420 should not be a lead byte\n");
228 ok(_ismbbtrail('\xb0'), "\xa0 should be a trail byte\n");
229 ok(_ismbbtrail(' ') == FALSE, "' ' should not be a trail byte\n");
231 /* _ismbslead */
232 expect_eq(_ismbslead(mbstring, &mbstring[0]), -1, int, "%d");
233 expect_eq(_ismbslead(mbstring, &mbstring[1]), FALSE, int, "%d");
234 expect_eq(_ismbslead(mbstring, &mbstring[2]), -1, int, "%d");
235 expect_eq(_ismbslead(mbstring, &mbstring[3]), FALSE, int, "%d");
236 expect_eq(_ismbslead(mbstring, &mbstring[4]), -1, int, "%d");
237 expect_eq(_ismbslead(mbstring, &mbstring[5]), FALSE, int, "%d");
238 expect_eq(_ismbslead(mbstring, &mbstring[6]), FALSE, int, "%d");
239 expect_eq(_ismbslead(mbstring, &mbstring[7]), -1, int, "%d");
240 expect_eq(_ismbslead(mbstring, &mbstring[8]), FALSE, int, "%d");
242 expect_eq(_ismbslead(mbsonlylead, &mbsonlylead[0]), -1, int, "%d");
243 expect_eq(_ismbslead(mbsonlylead, &mbsonlylead[1]), FALSE, int, "%d");
244 expect_eq(_ismbslead(mbsonlylead, &mbsonlylead[2]), FALSE, int, "%d");
245 expect_eq(_ismbslead(mbsonlylead, &mbsonlylead[5]), FALSE, int, "%d");
247 /* _ismbstrail */
248 expect_eq(_ismbstrail(mbstring, &mbstring[0]), FALSE, int, "%d");
249 expect_eq(_ismbstrail(mbstring, &mbstring[1]), -1, int, "%d");
250 expect_eq(_ismbstrail(mbstring, &mbstring[2]), FALSE, int, "%d");
251 expect_eq(_ismbstrail(mbstring, &mbstring[3]), -1, int, "%d");
252 expect_eq(_ismbstrail(mbstring, &mbstring[4]), FALSE, int, "%d");
253 expect_eq(_ismbstrail(mbstring, &mbstring[5]), -1, int, "%d");
254 expect_eq(_ismbstrail(mbstring, &mbstring[6]), FALSE, int, "%d");
255 expect_eq(_ismbstrail(mbstring, &mbstring[7]), FALSE, int, "%d");
256 expect_eq(_ismbstrail(mbstring, &mbstring[8]), -1, int, "%d");
258 expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[0]), FALSE, int, "%d");
259 expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[1]), -1, int, "%d");
260 expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[2]), FALSE, int, "%d");
261 expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[3]), FALSE, int, "%d");
262 expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[4]), FALSE, int, "%d");
263 expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[5]), FALSE, int, "%d");
265 /* _mbsbtype */
266 expect_eq(_mbsbtype(mbstring, 0), _MBC_LEAD, int, "%d");
267 expect_eq(_mbsbtype(mbstring, 1), _MBC_TRAIL, int, "%d");
268 expect_eq(_mbsbtype(mbstring, 2), _MBC_LEAD, int, "%d");
269 expect_eq(_mbsbtype(mbstring, 3), _MBC_ILLEGAL, int, "%d");
270 expect_eq(_mbsbtype(mbstring, 4), _MBC_LEAD, int, "%d");
271 expect_eq(_mbsbtype(mbstring, 5), _MBC_TRAIL, int, "%d");
272 expect_eq(_mbsbtype(mbstring, 6), _MBC_SINGLE, int, "%d");
273 expect_eq(_mbsbtype(mbstring, 7), _MBC_LEAD, int, "%d");
274 expect_eq(_mbsbtype(mbstring, 8), _MBC_ILLEGAL, int, "%d");
276 expect_eq(_mbsbtype(mbsonlylead, 0), _MBC_LEAD, int, "%d");
277 expect_eq(_mbsbtype(mbsonlylead, 1), _MBC_ILLEGAL, int, "%d");
278 expect_eq(_mbsbtype(mbsonlylead, 2), _MBC_ILLEGAL, int, "%d");
279 expect_eq(_mbsbtype(mbsonlylead, 3), _MBC_ILLEGAL, int, "%d");
280 expect_eq(_mbsbtype(mbsonlylead, 4), _MBC_ILLEGAL, int, "%d");
281 expect_eq(_mbsbtype(mbsonlylead, 5), _MBC_ILLEGAL, int, "%d");
283 /* _mbsnextc */
284 expect_eq(_mbsnextc(mbstring), 0xb0b1, int, "%x");
285 expect_eq(_mbsnextc(&mbstring[2]), 0xb220, int, "%x"); /* lead + invalid tail */
286 expect_eq(_mbsnextc(&mbstring[3]), 0x20, int, "%x"); /* single char */
288 /* _mbclen/_mbslen */
289 expect_eq(_mbclen(mbstring), 2, int, "%d");
290 expect_eq(_mbclen(&mbstring[2]), 2, int, "%d");
291 expect_eq(_mbclen(&mbstring[3]), 1, int, "%d");
292 expect_eq(_mbslen(mbstring2), 4, int, "%d");
293 expect_eq(_mbslen(mbsonlylead), 0, int, "%d"); /* lead + NUL not counted as character */
294 expect_eq(_mbslen(mbstring), 4, int, "%d"); /* lead + invalid trail counted */
296 /* _mbccpy/_mbsncpy */
297 memset(buf, 0xff, sizeof(buf));
298 _mbccpy(buf, mbstring);
299 expect_bin(buf, "\xb0\xb1\xff", 3);
301 memset(buf, 0xff, sizeof(buf));
302 _mbsncpy(buf, mbstring, 1);
303 expect_bin(buf, "\xb0\xb1\xff", 3);
304 memset(buf, 0xff, sizeof(buf));
305 _mbsncpy(buf, mbstring, 2);
306 expect_bin(buf, "\xb0\xb1\xb2 \xff", 5);
307 memset(buf, 0xff, sizeof(buf));
308 _mbsncpy(buf, mbstring, 3);
309 expect_bin(buf, "\xb0\xb1\xb2 \xb3\xb4\xff", 7);
310 memset(buf, 0xff, sizeof(buf));
311 _mbsncpy(buf, mbstring, 4);
312 expect_bin(buf, "\xb0\xb1\xb2 \xb3\xb4 \xff", 8);
313 memset(buf, 0xff, sizeof(buf));
314 _mbsncpy(buf, mbstring, 5);
315 expect_bin(buf, "\xb0\xb1\xb2 \xb3\xb4 \0\0\xff", 10);
316 memset(buf, 0xff, sizeof(buf));
317 _mbsncpy(buf, mbsonlylead, 6);
318 expect_bin(buf, "\0\0\0\0\0\0\0\xff", 8);
320 memset(buf, 0xff, sizeof(buf));
321 _mbsnbcpy(buf, mbstring2, 2);
322 expect_bin(buf, "\xb0\xb1\xff", 3);
323 _mbsnbcpy(buf, mbstring2, 3);
324 expect_bin(buf, "\xb0\xb1\0\xff", 4);
325 _mbsnbcpy(buf, mbstring2, 4);
326 expect_bin(buf, "\xb0\xb1\xb2\xb3\xff", 5);
327 memset(buf, 0xff, sizeof(buf));
328 _mbsnbcpy(buf, mbsonlylead, 5);
329 expect_bin(buf, "\0\0\0\0\0\xff", 6);
331 /* _mbsinc/mbsdec */
332 step = _mbsinc(mbstring) - mbstring;
333 ok(step == 2, "_mbsinc adds %d (exp. 2)\n", step);
334 step = _mbsinc(&mbstring[2]) - &mbstring[2]; /* lead + invalid tail */
335 ok(step == 2, "_mbsinc adds %d (exp. 2)\n", step);
337 step = _mbsninc(mbsonlylead, 1) - mbsonlylead;
338 ok(step == 0, "_mbsninc adds %d (exp. 0)\n", step);
339 step = _mbsninc(mbsonlylead, 2) - mbsonlylead; /* lead + NUL byte + lead + char */
340 ok(step == 0, "_mbsninc adds %d (exp. 0)\n", step);
341 step = _mbsninc(mbstring2, 0) - mbstring2;
342 ok(step == 0, "_mbsninc adds %d (exp. 2)\n", step);
343 step = _mbsninc(mbstring2, 1) - mbstring2;
344 ok(step == 2, "_mbsninc adds %d (exp. 2)\n", step);
345 step = _mbsninc(mbstring2, 2) - mbstring2;
346 ok(step == 4, "_mbsninc adds %d (exp. 4)\n", step);
347 step = _mbsninc(mbstring2, 3) - mbstring2;
348 ok(step == 5, "_mbsninc adds %d (exp. 5)\n", step);
349 step = _mbsninc(mbstring2, 4) - mbstring2;
350 ok(step == 7, "_mbsninc adds %d (exp. 7)\n", step);
351 step = _mbsninc(mbstring2, 5) - mbstring2;
352 ok(step == 7, "_mbsninc adds %d (exp. 7)\n", step);
353 step = _mbsninc(mbstring2, 17) - mbstring2;
354 ok(step == 7, "_mbsninc adds %d (exp. 7)\n", step);
356 /* functions that depend on locale codepage, not mbcp.
357 * we hope the current locale to be SBCS because setlocale(LC_ALL, ".1252") seems not to work yet
358 * (as of Wine 0.9.43)
360 if (*p__mb_cur_max == 1)
362 expect_eq(mblen((char *)mbstring, 3), 1, int, "%x");
363 expect_eq(_mbstrlen((char *)mbstring2), 7, int, "%d");
365 else
366 skip("Current locale has double-byte charset - could leave to false positives\n");
368 _setmbcp(1361);
369 expect_eq(_ismbblead(0x80), 0, int, "%d");
370 todo_wine {
371 expect_eq(_ismbblead(0x81), 1, int, "%d");
372 expect_eq(_ismbblead(0x83), 1, int, "%d");
374 expect_eq(_ismbblead(0x84), 1, int, "%d");
375 expect_eq(_ismbblead(0xd3), 1, int, "%d");
376 expect_eq(_ismbblead(0xd7), 0, int, "%d");
377 todo_wine {
378 expect_eq(_ismbblead(0xd8), 1, int, "%d");
380 expect_eq(_ismbblead(0xd9), 1, int, "%d");
382 expect_eq(_ismbbtrail(0x30), 0, int, "%d");
383 expect_eq(_ismbbtrail(0x31), 1, int, "%d");
384 expect_eq(_ismbbtrail(0x7e), 1, int, "%d");
385 expect_eq(_ismbbtrail(0x7f), 0, int, "%d");
386 expect_eq(_ismbbtrail(0x80), 0, int, "%d");
387 expect_eq(_ismbbtrail(0x81), 1, int, "%d");
388 expect_eq(_ismbbtrail(0xfe), 1, int, "%d");
389 expect_eq(_ismbbtrail(0xff), 0, int, "%d");
391 _setmbcp(curr_mbcp);
394 static void test_mbsspn( void)
396 unsigned char str1[]="cabernet";
397 unsigned char str2[]="shiraz";
398 unsigned char set[]="abc";
399 unsigned char empty[]="";
400 int ret;
401 ret=_mbsspn( str1, set);
402 ok( ret==3, "_mbsspn returns %d should be 3\n", ret);
403 ret=_mbsspn( str2, set);
404 ok( ret==0, "_mbsspn returns %d should be 0\n", ret);
405 ret=_mbsspn( str1, empty);
406 ok( ret==0, "_mbsspn returns %d should be 0\n", ret);
409 static void test_mbsspnp( void)
411 unsigned char str1[]="cabernet";
412 unsigned char str2[]="shiraz";
413 unsigned char set[]="abc";
414 unsigned char empty[]="";
415 unsigned char full[]="abcenrt";
416 unsigned char* ret;
417 ret=_mbsspnp( str1, set);
418 ok( ret[0]=='e', "_mbsspnp returns %c should be e\n", ret[0]);
419 ret=_mbsspnp( str2, set);
420 ok( ret[0]=='s', "_mbsspnp returns %c should be s\n", ret[0]);
421 ret=_mbsspnp( str1, empty);
422 ok( ret[0]=='c', "_mbsspnp returns %c should be c\n", ret[0]);
423 ret=_mbsspnp( str1, full);
424 ok( ret==NULL, "_mbsspnp returns %p should be NULL\n", ret);
427 static void test_strdup(void)
429 char *str;
430 str = _strdup( 0 );
431 ok( str == 0, "strdup returns %s should be 0\n", str);
432 free( str );
435 static void test_strcpy_s(void)
437 char dest[8];
438 const char *small = "small";
439 const char *big = "atoolongstringforthislittledestination";
440 int ret;
442 if(!pstrcpy_s)
444 skip("strcpy_s not found\n");
445 return;
448 memset(dest, 'X', sizeof(dest));
449 ret = pstrcpy_s(dest, sizeof(dest), small);
450 ok(ret == 0, "Copying a string into a big enough destination returned %d, expected 0\n", ret);
451 ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == 'l' &&
452 dest[4] == 'l' && dest[5] == '\0'&& dest[6] == 'X' && dest[7] == 'X',
453 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
454 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
456 memset(dest, 'X', sizeof(dest));
457 ret = pstrcpy_s(dest, 0, big);
458 ok(ret == EINVAL, "Copying into a destination of size 0 returned %d, expected EINVAL\n", ret);
459 ok(dest[0] == 'X' && dest[1] == 'X' && dest[2] == 'X' && dest[3] == 'X' &&
460 dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
461 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
462 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
463 ret = pstrcpy_s(dest, 0, NULL);
464 ok(ret == EINVAL, "Copying into a destination of size 0 returned %d, expected EINVAL\n", ret);
465 ok(dest[0] == 'X' && dest[1] == 'X' && dest[2] == 'X' && dest[3] == 'X' &&
466 dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
467 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
468 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
470 memset(dest, 'X', sizeof(dest));
471 ret = pstrcpy_s(dest, sizeof(dest), big);
472 ok(ret == ERANGE, "Copying a big string in a small location returned %d, expected ERANGE\n", ret);
473 ok(dest[0] == '\0'&& dest[1] == 't' && dest[2] == 'o' && dest[3] == 'o' &&
474 dest[4] == 'l' && dest[5] == 'o' && dest[6] == 'n' && dest[7] == 'g',
475 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
476 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
478 memset(dest, 'X', sizeof(dest));
479 ret = pstrcpy_s(dest, sizeof(dest), NULL);
480 ok(ret == EINVAL, "Copying from a NULL source string returned %d, expected EINVAL\n", ret);
481 ok(dest[0] == '\0'&& dest[1] == 'X' && dest[2] == 'X' && dest[3] == 'X' &&
482 dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
483 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
484 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
486 ret = pstrcpy_s(NULL, sizeof(dest), small);
487 ok(ret == EINVAL, "Copying a big string a NULL dest returned %d, expected EINVAL\n", ret);
490 static void test_strcat_s(void)
492 char dest[8];
493 const char *small = "sma";
494 int ret;
496 if(!pstrcat_s)
498 skip("strcat_s not found\n");
499 return;
502 memset(dest, 'X', sizeof(dest));
503 dest[0] = '\0';
504 ret = pstrcat_s(dest, sizeof(dest), small);
505 ok(ret == 0, "strcat_s: Copying a string into a big enough destination returned %d, expected 0\n", ret);
506 ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == '\0'&&
507 dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
508 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
509 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
510 ret = pstrcat_s(dest, sizeof(dest), small);
511 ok(ret == 0, "strcat_s: Attaching a string to a big enough destination returned %d, expected 0\n", ret);
512 ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == 's' &&
513 dest[4] == 'm' && dest[5] == 'a' && dest[6] == '\0'&& dest[7] == 'X',
514 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
515 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
517 ret = pstrcat_s(dest, sizeof(dest), small);
518 ok(ret == ERANGE, "strcat_s: Attaching a string to a filled up destination returned %d, expected ERANGE\n", ret);
519 ok(dest[0] == '\0'&& dest[1] == 'm' && dest[2] == 'a' && dest[3] == 's' &&
520 dest[4] == 'm' && dest[5] == 'a' && dest[6] == 's' && dest[7] == 'm',
521 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
522 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
524 memset(dest, 'X', sizeof(dest));
525 dest[0] = 'a';
526 dest[1] = '\0';
528 ret = pstrcat_s(dest, 0, small);
529 ok(ret == EINVAL, "strcat_s: Source len = 0 returned %d, expected EINVAL\n", ret);
530 ok(dest[0] == 'a' && dest[1] == '\0'&& dest[2] == 'X' && dest[3] == 'X' &&
531 dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
532 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
533 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
535 ret = pstrcat_s(dest, 0, NULL);
536 ok(ret == EINVAL, "strcat_s: len = 0 and src = NULL returned %d, expected EINVAL\n", ret);
537 ok(dest[0] == 'a' && dest[1] == '\0'&& dest[2] == 'X' && dest[3] == 'X' &&
538 dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
539 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
540 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
542 ret = pstrcat_s(dest, sizeof(dest), NULL);
543 ok(ret == EINVAL, "strcat_s: Sourcing from NULL returned %d, expected EINVAL\n", ret);
544 ok(dest[0] == '\0'&& dest[1] == '\0'&& dest[2] == 'X' && dest[3] == 'X' &&
545 dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
546 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
547 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
549 ret = pstrcat_s(NULL, sizeof(dest), small);
550 ok(ret == EINVAL, "strcat_s: Writing to a NULL string returned %d, expected EINVAL\n", ret);
553 static void test__mbsnbcpy_s(void)
555 unsigned char dest[8];
556 const unsigned char big[] = "atoolongstringforthislittledestination";
557 const unsigned char small[] = "small";
558 int ret;
560 if(!p_mbsnbcpy_s)
562 skip("_mbsnbcpy_s not found\n");
563 return;
566 memset(dest, 'X', sizeof(dest));
567 ret = p_mbsnbcpy_s(dest, sizeof(dest), small, sizeof(small));
568 ok(ret == 0, "_mbsnbcpy_s: Copying a string into a big enough destination returned %d, expected 0\n", ret);
569 ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == 'l' &&
570 dest[4] == 'l' && dest[5] == '\0'&& dest[6] == 'X' && dest[7] == 'X',
571 "Unexpected return data from _mbsnbcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
572 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
574 /* WTF? */
575 memset(dest, 'X', sizeof(dest));
576 ret = p_mbsnbcpy_s(dest, sizeof(dest) - 2, big, sizeof(small));
577 ok(ret == ERANGE, "_mbsnbcpy_s: Copying a too long string returned %d, expected ERANGE\n", ret);
578 ok(dest[0] == '\0'&& dest[1] == 't' && dest[2] == 'o' && dest[3] == 'o' &&
579 dest[4] == 'l' && dest[5] == 'o' && dest[6] == 'X' && dest[7] == 'X',
580 "Unexpected return data from _mbsnbcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
581 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
583 memset(dest, 'X', sizeof(dest));
584 ret = p_mbsnbcpy_s(dest, sizeof(dest) - 2, big, 4);
585 ok(ret == 0, "_mbsnbcpy_s: Copying a too long string with a count cap returned %d, expected 0\n", ret);
586 ok(dest[0] == 'a' && dest[1] == 't' && dest[2] == 'o' && dest[3] == 'o' &&
587 dest[4] == '\0'&& dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
588 "Unexpected return data from _mbsnbcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
589 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
591 memset(dest, 'X', sizeof(dest));
592 ret = p_mbsnbcpy_s(dest, sizeof(dest) - 2, small, sizeof(small) + 10);
593 ok(ret == 0, "_mbsnbcpy_s: Copying more data than the source string len returned %d, expected 0\n", ret);
594 ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == 'l' &&
595 dest[4] == 'l' && dest[5] == '\0'&& dest[6] == 'X' && dest[7] == 'X',
596 "Unexpected return data from _mbsnbcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
597 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
600 static void test_wcscpy_s(void)
602 static const WCHAR szLongText[] = { 'T','h','i','s','A','L','o','n','g','s','t','r','i','n','g',0 };
603 static WCHAR szDest[18];
604 static WCHAR szDestShort[8];
605 int ret;
607 if(!p_wcscpy_s)
609 skip("wcscpy_s not found\n");
610 return;
613 /* Test NULL Dest */
614 ret = p_wcscpy_s(NULL, 18, szLongText);
615 ok(ret == EINVAL, "p_wcscpy_s expect EINVAL got %d\n", ret);
617 /* Test NULL Source */
618 szDest[0] = 'A';
619 ret = p_wcscpy_s(szDest, 18, NULL);
620 ok(ret == EINVAL, "expected EINVAL got %d\n", ret);
621 ok(szDest[0] == 0, "szDest[0] not 0\n");
623 /* Test invalid size */
624 szDest[0] = 'A';
625 ret = p_wcscpy_s(szDest, 0, szLongText);
626 /* Later versions changed the return value for this case to EINVAL,
627 * and don't modify the result if the dest size is 0.
629 ok(ret == ERANGE || ret == EINVAL, "expected ERANGE/EINVAL got %d\n", ret);
630 ok(szDest[0] == 0 || ret == EINVAL, "szDest[0] not 0\n");
632 /* Copy same buffer size */
633 ret = p_wcscpy_s(szDest, 18, szLongText);
634 ok(ret == 0, "expected 0 got %d\n", ret);
635 ok(lstrcmpW(szDest, szLongText) == 0, "szDest != szLongText\n");
637 /* Copy smaller buffer size */
638 szDest[0] = 'A';
639 ret = p_wcscpy_s(szDestShort, 8, szLongText);
640 ok(ret == ERANGE || ret == EINVAL, "expected ERANGE/EINVAL got %d\n", ret);
641 ok(szDestShort[0] == 0, "szDestShort[0] not 0\n");
644 static void test__wcsupr_s(void)
646 static const WCHAR mixedString[] = {'M', 'i', 'X', 'e', 'D', 'l', 'o', 'w',
647 'e', 'r', 'U', 'P', 'P', 'E', 'R', 0};
648 static const WCHAR expectedString[] = {'M', 'I', 'X', 'E', 'D', 'L', 'O',
649 'W', 'E', 'R', 'U', 'P', 'P', 'E',
650 'R', 0};
651 WCHAR testBuffer[2*sizeof(mixedString)/sizeof(WCHAR)];
652 int ret;
654 if (!p_wcsupr_s)
656 win_skip("_wcsupr_s not found\n");
657 return;
660 /* Test NULL input string and invalid size. */
661 errno = EBADF;
662 ret = p_wcsupr_s(NULL, 0);
663 ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
664 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
666 /* Test NULL input string and valid size. */
667 errno = EBADF;
668 ret = p_wcsupr_s(NULL, sizeof(testBuffer)/sizeof(WCHAR));
669 ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
670 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
672 /* Test empty string with zero size. */
673 errno = EBADF;
674 testBuffer[0] = '\0';
675 ret = p_wcsupr_s(testBuffer, 0);
676 ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
677 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
678 ok(testBuffer[0] == '\0', "Expected the buffer to be unchanged\n");
680 /* Test empty string with size of one. */
681 testBuffer[0] = '\0';
682 ret = p_wcsupr_s(testBuffer, 1);
683 ok(ret == 0, "Expected _wcsupr_s to succeed, got %d\n", ret);
684 ok(testBuffer[0] == '\0', "Expected the buffer to be unchanged\n");
686 /* Test one-byte buffer with zero size. */
687 errno = EBADF;
688 testBuffer[0] = 'x';
689 ret = p_wcsupr_s(testBuffer, 0);
690 ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
691 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
692 ok(testBuffer[0] == '\0', "Expected the first buffer character to be null\n");
694 /* Test one-byte buffer with size of one. */
695 errno = EBADF;
696 testBuffer[0] = 'x';
697 ret = p_wcsupr_s(testBuffer, 1);
698 ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
699 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
700 ok(testBuffer[0] == '\0', "Expected the first buffer character to be null\n");
702 /* Test invalid size. */
703 wcscpy(testBuffer, mixedString);
704 errno = EBADF;
705 ret = p_wcsupr_s(testBuffer, 0);
706 ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
707 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
708 ok(testBuffer[0] == '\0', "Expected the first buffer character to be null\n");
710 /* Test normal string uppercasing. */
711 wcscpy(testBuffer, mixedString);
712 ret = p_wcsupr_s(testBuffer, sizeof(mixedString)/sizeof(WCHAR));
713 ok(ret == 0, "Expected _wcsupr_s to succeed, got %d\n", ret);
714 ok(!wcscmp(testBuffer, expectedString), "Expected the string to be fully upper-case\n");
716 /* Test uppercasing with a shorter buffer size count. */
717 wcscpy(testBuffer, mixedString);
718 errno = EBADF;
719 ret = p_wcsupr_s(testBuffer, sizeof(mixedString)/sizeof(WCHAR) - 1);
720 ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
721 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
722 ok(testBuffer[0] == '\0', "Expected the first buffer character to be null\n");
724 /* Test uppercasing with a longer buffer size count. */
725 wcscpy(testBuffer, mixedString);
726 ret = p_wcsupr_s(testBuffer, sizeof(testBuffer)/sizeof(WCHAR));
727 ok(ret == 0, "Expected _wcsupr_s to succeed, got %d\n", ret);
728 ok(!wcscmp(testBuffer, expectedString), "Expected the string to be fully upper-case\n");
731 static void test_mbcjisjms(void)
733 /* List of value-pairs to test. The test assumes the last pair to be {0, ..} */
734 unsigned int jisjms[][2] = { {0x2020, 0}, {0x2021, 0}, {0x2120, 0}, {0x2121, 0x8140},
735 {0x7f7f, 0}, {0x7f7e, 0}, {0x7e7f, 0}, {0x7e7e, 0xeffc},
736 {0x2121FFFF, 0}, {0x2223, 0x81a1}, {0x237e, 0x829e}, {0, 0}};
737 unsigned int ret, exp, i;
739 i = 0;
742 ret = _mbcjistojms(jisjms[i][0]);
744 if(_getmbcp() == 932) /* Japanese codepage? */
745 exp = jisjms[i][1];
746 else
747 exp = jisjms[i][0]; /* If not, no conversion */
749 ok(ret == exp, "Expected 0x%x, got 0x%x\n", exp, ret);
750 } while(jisjms[i++][0] != 0);
753 static void test_mbctombb(void)
755 static const unsigned int mbcmbb_932[][2] = {
756 {0x829e, 0x829e}, {0x829f, 0xa7}, {0x82f1, 0xdd}, {0x82f2, 0x82f2},
757 {0x833f, 0x833f}, {0x8340, 0xa7}, {0x837e, 0xd0}, {0x837f, 0x837f},
758 {0x8380, 0xd1}, {0x8396, 0xb9}, {0x8397, 0x8397}, {0x813f, 0x813f},
759 {0x8140, 0x20}, {0x814c, 0x814c}, {0x814f, 0x5e}, {0x8197, 0x40},
760 {0x8198, 0x8198}, {0x8258, 0x39}, {0x8259, 0x8259}, {0x825f, 0x825f},
761 {0x8260, 0x41}, {0x82f1, 0xdd}, {0x82f2, 0x82f2}, {0,0}};
762 unsigned int exp, ret, i;
763 unsigned int prev_cp = _getmbcp();
765 _setmbcp(932);
766 for (i = 0; mbcmbb_932[i][0] != 0; i++)
768 ret = _mbctombb(mbcmbb_932[i][0]);
769 exp = mbcmbb_932[i][1];
770 ok(ret == exp, "Expected 0x%x, got 0x%x\n", exp, ret);
772 _setmbcp(prev_cp);
775 static void test_ismbclegal(void) {
776 unsigned int prev_cp = _getmbcp();
777 int ret, exp, err;
778 unsigned int i;
780 _setmbcp(932); /* Japanese */
781 err = 0;
782 for(i = 0; i < 0x10000; i++) {
783 ret = _ismbclegal(i);
784 exp = ((HIBYTE(i) >= 0x81 && HIBYTE(i) <= 0x9F) ||
785 (HIBYTE(i) >= 0xE0 && HIBYTE(i) <= 0xFC)) &&
786 ((LOBYTE(i) >= 0x40 && LOBYTE(i) <= 0x7E) ||
787 (LOBYTE(i) >= 0x80 && LOBYTE(i) <= 0xFC));
788 if(ret != exp) {
789 err = 1;
790 break;
793 ok(!err, "_ismbclegal (932) : Expected 0x%x, got 0x%x (0x%x)\n", exp, ret, i);
794 _setmbcp(936); /* Chinese (GBK) */
795 err = 0;
796 for(i = 0; i < 0x10000; i++) {
797 ret = _ismbclegal(i);
798 exp = HIBYTE(i) >= 0x81 && HIBYTE(i) <= 0xFE &&
799 LOBYTE(i) >= 0x40 && LOBYTE(i) <= 0xFE;
800 if(ret != exp) {
801 err = 1;
802 break;
805 ok(!err, "_ismbclegal (936) : Expected 0x%x, got 0x%x (0x%x)\n", exp, ret, i);
806 _setmbcp(949); /* Korean */
807 err = 0;
808 for(i = 0; i < 0x10000; i++) {
809 ret = _ismbclegal(i);
810 exp = HIBYTE(i) >= 0x81 && HIBYTE(i) <= 0xFE &&
811 LOBYTE(i) >= 0x41 && LOBYTE(i) <= 0xFE;
812 if(ret != exp) {
813 err = 1;
814 break;
817 ok(!err, "_ismbclegal (949) : Expected 0x%x, got 0x%x (0x%x)\n", exp, ret, i);
818 _setmbcp(950); /* Chinese (Big5) */
819 err = 0;
820 for(i = 0; i < 0x10000; i++) {
821 ret = _ismbclegal(i);
822 exp = HIBYTE(i) >= 0x81 && HIBYTE(i) <= 0xFE &&
823 ((LOBYTE(i) >= 0x40 && LOBYTE(i) <= 0x7E) ||
824 (LOBYTE(i) >= 0xA1 && LOBYTE(i) <= 0xFE));
825 if(ret != exp) {
826 err = 1;
827 break;
830 ok(!err, "_ismbclegal (950) : Expected 0x%x, got 0x%x (0x%x)\n", exp, ret, i);
831 _setmbcp(1361); /* Korean (Johab) */
832 err = 0;
833 for(i = 0; i < 0x10000; i++) {
834 ret = _ismbclegal(i);
835 exp = ((HIBYTE(i) >= 0x81 && HIBYTE(i) <= 0xD3) ||
836 (HIBYTE(i) >= 0xD8 && HIBYTE(i) <= 0xF9)) &&
837 ((LOBYTE(i) >= 0x31 && LOBYTE(i) <= 0x7E) ||
838 (LOBYTE(i) >= 0x81 && LOBYTE(i) <= 0xFE)) &&
839 HIBYTE(i) != 0xDF;
840 if(ret != exp) {
841 err = 1;
842 break;
845 todo_wine ok(!err, "_ismbclegal (1361) : Expected 0x%x, got 0x%x (0x%x)\n", exp, ret, i);
847 _setmbcp(prev_cp);
850 static const struct {
851 const char* string;
852 const char* delimiter;
853 int exp_offsetret1; /* returned offset from string after first call to strtok()
854 -1 means NULL */
855 int exp_offsetret2; /* returned offset from string after second call to strtok()
856 -1 means NULL */
857 int exp_offsetret3; /* returned offset from string after third call to strtok()
858 -1 means NULL */
859 } testcases_strtok[] = {
860 { "red cabernet", " ", 0, 4, -1 },
861 { "sparkling white riesling", " ", 0, 10, 16 },
862 { " pale cream sherry", "e ", 1, 6, 9 },
863 /* end mark */
864 { 0}
867 static void test_strtok(void)
869 int i;
870 char *strret;
871 char teststr[100];
872 for( i = 0; testcases_strtok[i].string; i++){
873 strcpy( teststr, testcases_strtok[i].string);
874 strret = strtok( teststr, testcases_strtok[i].delimiter);
875 ok( (int)(strret - teststr) == testcases_strtok[i].exp_offsetret1 ||
876 (!strret && testcases_strtok[i].exp_offsetret1 == -1),
877 "string (%p) \'%s\' return %p\n",
878 teststr, testcases_strtok[i].string, strret);
879 if( !strret) continue;
880 strret = strtok( NULL, testcases_strtok[i].delimiter);
881 ok( (int)(strret - teststr) == testcases_strtok[i].exp_offsetret2 ||
882 (!strret && testcases_strtok[i].exp_offsetret2 == -1),
883 "second call string (%p) \'%s\' return %p\n",
884 teststr, testcases_strtok[i].string, strret);
885 if( !strret) continue;
886 strret = strtok( NULL, testcases_strtok[i].delimiter);
887 ok( (int)(strret - teststr) == testcases_strtok[i].exp_offsetret3 ||
888 (!strret && testcases_strtok[i].exp_offsetret3 == -1),
889 "third call string (%p) \'%s\' return %p\n",
890 teststr, testcases_strtok[i].string, strret);
894 static void test_strtol(void)
896 char* e;
897 LONG l;
898 ULONG ul;
900 /* errno is only set in case of error, so reset errno to EBADF to check for errno modification */
901 /* errno is modified on W2K8+ */
902 errno = EBADF;
903 l = strtol("-1234", &e, 0);
904 ok(l==-1234, "wrong value %d\n", l);
905 ok(errno == EBADF || broken(errno == 0), "wrong errno %d\n", errno);
906 errno = EBADF;
907 ul = strtoul("1234", &e, 0);
908 ok(ul==1234, "wrong value %u\n", ul);
909 ok(errno == EBADF || broken(errno == 0), "wrong errno %d\n", errno);
911 errno = EBADF;
912 l = strtol("2147483647L", &e, 0);
913 ok(l==2147483647, "wrong value %d\n", l);
914 ok(errno == EBADF || broken(errno == 0), "wrong errno %d\n", errno);
915 errno = EBADF;
916 l = strtol("-2147483648L", &e, 0);
917 ok(l==-2147483647L - 1, "wrong value %d\n", l);
918 ok(errno == EBADF || broken(errno == 0), "wrong errno %d\n", errno);
919 errno = EBADF;
920 ul = strtoul("4294967295UL", &e, 0);
921 ok(ul==4294967295ul, "wrong value %u\n", ul);
922 ok(errno == EBADF || broken(errno == 0), "wrong errno %d\n", errno);
924 errno = 0;
925 l = strtol("9223372036854775807L", &e, 0);
926 ok(l==2147483647, "wrong value %d\n", l);
927 ok(errno == ERANGE, "wrong errno %d\n", errno);
928 errno = 0;
929 ul = strtoul("9223372036854775807L", &e, 0);
930 ok(ul==4294967295ul, "wrong value %u\n", ul);
931 ok(errno == ERANGE, "wrong errno %d\n", errno);
934 static void test_strnlen(void)
936 static const char str[] = "string";
937 size_t res;
939 if(!p_strnlen) {
940 win_skip("strnlen not found\n");
941 return;
944 res = p_strnlen(str, 20);
945 ok(res == 6, "Returned length = %d\n", (int)res);
947 res = p_strnlen(str, 3);
948 ok(res == 3, "Returned length = %d\n", (int)res);
950 res = p_strnlen(NULL, 0);
951 ok(res == 0, "Returned length = %d\n", (int)res);
954 static void test__strtoi64(void)
956 static const char no1[] = "31923";
957 static const char no2[] = "-213312";
958 static const char no3[] = "12aa";
959 static const char no4[] = "abc12";
960 static const char overflow[] = "99999999999999999999";
961 static const char neg_overflow[] = "-99999999999999999999";
962 static const char hex[] = "0x123";
963 static const char oct[] = "000123";
964 static const char blanks[] = " 12 212.31";
966 __int64 res;
967 unsigned __int64 ures;
968 char *endpos;
970 if(!p_strtoi64 || !p_strtoui64) {
971 win_skip("_strtoi64 or _strtoui64 not found\n");
972 return;
975 errno = 0xdeadbeef;
976 res = p_strtoi64(no1, NULL, 10);
977 ok(res == 31923, "res != 31923\n");
978 res = p_strtoi64(no2, NULL, 10);
979 ok(res == -213312, "res != -213312\n");
980 res = p_strtoi64(no3, NULL, 10);
981 ok(res == 12, "res != 12\n");
982 res = p_strtoi64(no4, &endpos, 10);
983 ok(res == 0, "res != 0\n");
984 ok(endpos == no4, "Scanning was not stopped on first character\n");
985 res = p_strtoi64(hex, &endpos, 10);
986 ok(res == 0, "res != 0\n");
987 ok(endpos == hex+1, "Incorrect endpos (%p-%p)\n", hex, endpos);
988 res = p_strtoi64(oct, &endpos, 10);
989 ok(res == 123, "res != 123\n");
990 ok(endpos == oct+strlen(oct), "Incorrect endpos (%p-%p)\n", oct, endpos);
991 res = p_strtoi64(blanks, &endpos, 10);
992 ok(res == 12, "res != 12\n");
993 ok(endpos == blanks+10, "Incorrect endpos (%p-%p)\n", blanks, endpos);
994 ok(errno == 0xdeadbeef, "errno = %x\n", errno);
996 errno = 0xdeadbeef;
997 res = p_strtoi64(overflow, &endpos, 10);
998 ok(res == _I64_MAX, "res != _I64_MAX\n");
999 ok(endpos == overflow+strlen(overflow), "Incorrect endpos (%p-%p)\n", overflow, endpos);
1000 ok(errno == ERANGE, "errno = %x\n", errno);
1002 errno = 0xdeadbeef;
1003 res = p_strtoi64(neg_overflow, &endpos, 10);
1004 ok(res == _I64_MIN, "res != _I64_MIN\n");
1005 ok(endpos == neg_overflow+strlen(neg_overflow), "Incorrect endpos (%p-%p)\n", neg_overflow, endpos);
1006 ok(errno == ERANGE, "errno = %x\n", errno);
1008 errno = 0xdeadbeef;
1009 res = p_strtoi64(no1, &endpos, 16);
1010 ok(res == 203043, "res != 203043\n");
1011 ok(endpos == no1+strlen(no1), "Incorrect endpos (%p-%p)\n", no1, endpos);
1012 res = p_strtoi64(no2, &endpos, 16);
1013 ok(res == -2175762, "res != -2175762\n");
1014 ok(endpos == no2+strlen(no2), "Incorrect endpos (%p-%p)\n", no2, endpos);
1015 res = p_strtoi64(no3, &endpos, 16);
1016 ok(res == 4778, "res != 4778\n");
1017 ok(endpos == no3+strlen(no3), "Incorrect endpos (%p-%p)\n", no3, endpos);
1018 res = p_strtoi64(no4, &endpos, 16);
1019 ok(res == 703506, "res != 703506\n");
1020 ok(endpos == no4+strlen(no4), "Incorrect endpos (%p-%p)\n", no4, endpos);
1021 res = p_strtoi64(hex, &endpos, 16);
1022 ok(res == 291, "res != 291\n");
1023 ok(endpos == hex+strlen(hex), "Incorrect endpos (%p-%p)\n", hex, endpos);
1024 res = p_strtoi64(oct, &endpos, 16);
1025 ok(res == 291, "res != 291\n");
1026 ok(endpos == oct+strlen(oct), "Incorrect endpos (%p-%p)\n", oct, endpos);
1027 res = p_strtoi64(blanks, &endpos, 16);
1028 ok(res == 18, "res != 18\n");
1029 ok(endpos == blanks+10, "Incorrect endpos (%p-%p)\n", blanks, endpos);
1030 ok(errno == 0xdeadbeef, "errno = %x\n", errno);
1032 errno = 0xdeadbeef;
1033 res = p_strtoi64(hex, &endpos, 36);
1034 ok(res == 1541019, "res != 1541019\n");
1035 ok(endpos == hex+strlen(hex), "Incorrect endpos (%p-%p)\n", hex, endpos);
1036 ok(errno == 0xdeadbeef, "errno = %x\n", errno);
1038 errno = 0xdeadbeef;
1039 res = p_strtoi64(no1, &endpos, 0);
1040 ok(res == 31923, "res != 31923\n");
1041 ok(endpos == no1+strlen(no1), "Incorrect endpos (%p-%p)\n", no1, endpos);
1042 res = p_strtoi64(no2, &endpos, 0);
1043 ok(res == -213312, "res != -213312\n");
1044 ok(endpos == no2+strlen(no2), "Incorrect endpos (%p-%p)\n", no2, endpos);
1045 res = p_strtoi64(no3, &endpos, 10);
1046 ok(res == 12, "res != 12\n");
1047 ok(endpos == no3+2, "Incorrect endpos (%p-%p)\n", no3, endpos);
1048 res = p_strtoi64(no4, &endpos, 10);
1049 ok(res == 0, "res != 0\n");
1050 ok(endpos == no4, "Incorrect endpos (%p-%p)\n", no4, endpos);
1051 res = p_strtoi64(hex, &endpos, 10);
1052 ok(res == 0, "res != 0\n");
1053 ok(endpos == hex+1, "Incorrect endpos (%p-%p)\n", hex, endpos);
1054 res = p_strtoi64(oct, &endpos, 10);
1055 ok(res == 123, "res != 123\n");
1056 ok(endpos == oct+strlen(oct), "Incorrect endpos (%p-%p)\n", oct, endpos);
1057 res = p_strtoi64(blanks, &endpos, 10);
1058 ok(res == 12, "res != 12\n");
1059 ok(endpos == blanks+10, "Incorrect endpos (%p-%p)\n", blanks, endpos);
1060 ok(errno == 0xdeadbeef, "errno = %x\n", errno);
1062 errno = 0xdeadbeef;
1063 ures = p_strtoui64(no1, &endpos, 0);
1064 ok(ures == 31923, "ures != 31923\n");
1065 ok(endpos == no1+strlen(no1), "Incorrect endpos (%p-%p)\n", no1, endpos);
1066 ures = p_strtoui64(no2, &endpos, 0);
1067 ok(ures == -213312, "ures != -213312\n");
1068 ok(endpos == no2+strlen(no2), "Incorrect endpos (%p-%p)\n", no2, endpos);
1069 ures = p_strtoui64(no3, &endpos, 10);
1070 ok(ures == 12, "ures != 12\n");
1071 ok(endpos == no3+2, "Incorrect endpos (%p-%p)\n", no3, endpos);
1072 ures = p_strtoui64(no4, &endpos, 10);
1073 ok(ures == 0, "ures != 0\n");
1074 ok(endpos == no4, "Incorrect endpos (%p-%p)\n", no4, endpos);
1075 ures = p_strtoui64(hex, &endpos, 10);
1076 ok(ures == 0, "ures != 0\n");
1077 ok(endpos == hex+1, "Incorrect endpos (%p-%p)\n", hex, endpos);
1078 ures = p_strtoui64(oct, &endpos, 10);
1079 ok(ures == 123, "ures != 123\n");
1080 ok(endpos == oct+strlen(oct), "Incorrect endpos (%p-%p)\n", oct, endpos);
1081 ures = p_strtoui64(blanks, &endpos, 10);
1082 ok(ures == 12, "ures != 12\n");
1083 ok(endpos == blanks+10, "Incorrect endpos (%p-%p)\n", blanks, endpos);
1084 ok(errno == 0xdeadbeef, "errno = %x\n", errno);
1086 errno = 0xdeadbeef;
1087 ures = p_strtoui64(overflow, &endpos, 10);
1088 ok(ures == _UI64_MAX, "ures != _UI64_MAX\n");
1089 ok(endpos == overflow+strlen(overflow), "Incorrect endpos (%p-%p)\n", overflow, endpos);
1090 ok(errno == ERANGE, "errno = %x\n", errno);
1092 errno = 0xdeadbeef;
1093 ures = p_strtoui64(neg_overflow, &endpos, 10);
1094 ok(ures == 1, "ures != 1\n");
1095 ok(endpos == neg_overflow+strlen(neg_overflow), "Incorrect endpos (%p-%p)\n", neg_overflow, endpos);
1096 ok(errno == ERANGE, "errno = %x\n", errno);
1099 static inline BOOL almost_equal(double d1, double d2) {
1100 if(d1-d2>-1e-30 && d1-d2<1e-30)
1101 return TRUE;
1102 return FALSE;
1105 static void test__strtod(void)
1107 const char double1[] = "12.1";
1108 const char double2[] = "-13.721";
1109 const char double3[] = "INF";
1110 const char double4[] = ".21e12";
1111 const char double5[] = "214353e-3";
1112 const char overflow[] = "1d9999999999999999999";
1113 const char white_chars[] = " d10";
1115 char *end;
1116 double d;
1118 d = strtod(double1, &end);
1119 ok(almost_equal(d, 12.1), "d = %lf\n", d);
1120 ok(end == double1+4, "incorrect end (%d)\n", (int)(end-double1));
1122 d = strtod(double2, &end);
1123 ok(almost_equal(d, -13.721), "d = %lf\n", d);
1124 ok(end == double2+7, "incorrect end (%d)\n", (int)(end-double2));
1126 d = strtod(double3, &end);
1127 ok(almost_equal(d, 0), "d = %lf\n", d);
1128 ok(end == double3, "incorrect end (%d)\n", (int)(end-double3));
1130 d = strtod(double4, &end);
1131 ok(almost_equal(d, 210000000000.0), "d = %lf\n", d);
1132 ok(end == double4+6, "incorrect end (%d)\n", (int)(end-double4));
1134 d = strtod(double5, &end);
1135 ok(almost_equal(d, 214.353), "d = %lf\n", d);
1136 ok(end == double5+9, "incorrect end (%d)\n", (int)(end-double5));
1138 d = strtod("12.1d2", NULL);
1139 ok(almost_equal(d, 12.1e2), "d = %lf\n", d);
1141 d = strtod(white_chars, &end);
1142 ok(almost_equal(d, 0), "d = %lf\n", d);
1143 ok(end == white_chars, "incorrect end (%d)\n", (int)(end-white_chars));
1145 /* Set locale with non '.' decimal point (',') */
1146 if(!setlocale(LC_ALL, "Polish")) {
1147 win_skip("system with limited locales\n");
1148 return;
1151 d = strtod("12.1", NULL);
1152 ok(almost_equal(d, 12.0), "d = %lf\n", d);
1154 d = strtod("12,1", NULL);
1155 ok(almost_equal(d, 12.1), "d = %lf\n", d);
1157 setlocale(LC_ALL, "C");
1159 /* Precision tests */
1160 d = strtod("0.1", NULL);
1161 ok(almost_equal(d, 0.1), "d = %lf\n", d);
1162 d = strtod("-0.1", NULL);
1163 ok(almost_equal(d, -0.1), "d = %lf\n", d);
1164 d = strtod("0.1281832188491894198128921", NULL);
1165 ok(almost_equal(d, 0.1281832188491894198128921), "d = %lf\n", d);
1166 d = strtod("0.82181281288121", NULL);
1167 ok(almost_equal(d, 0.82181281288121), "d = %lf\n", d);
1168 d = strtod("21921922352523587651128218821", NULL);
1169 ok(almost_equal(d, 21921922352523587651128218821.0), "d = %lf\n", d);
1170 d = strtod("0.1d238", NULL);
1171 ok(almost_equal(d, 0.1e238L), "d = %lf\n", d);
1172 d = strtod("0.1D-4736", NULL);
1173 ok(almost_equal(d, 0.1e-4736L), "d = %lf\n", d);
1175 errno = 0xdeadbeef;
1176 d = strtod(overflow, &end);
1177 ok(errno == ERANGE, "errno = %x\n", errno);
1178 ok(end == overflow+21, "incorrect end (%d)\n", (int)(end-overflow));
1180 errno = 0xdeadbeef;
1181 strtod("-1d309", NULL);
1182 ok(errno == ERANGE, "errno = %x\n", errno);
1185 static void test_mbstowcs(void)
1187 static const wchar_t wSimple[] = { 't','e','x','t',0 };
1188 static const wchar_t wHiragana[] = { 0x3042,0x3043,0 };
1189 static const char mSimple[] = "text";
1190 static const char mHiragana[] = { 0x82,0xa0,0x82,0xa1,0 };
1192 wchar_t wOut[6];
1193 char mOut[6];
1194 size_t ret;
1195 int err;
1197 wOut[4] = '!'; wOut[5] = '\0';
1198 mOut[4] = '!'; mOut[5] = '\0';
1200 ret = mbstowcs(NULL, mSimple, 0);
1201 ok(ret == 4, "mbstowcs did not return 4\n");
1203 ret = mbstowcs(wOut, mSimple, 4);
1204 ok(ret == 4, "mbstowcs did not return 4\n");
1205 ok(!memcmp(wOut, wSimple, 4*sizeof(wchar_t)), "wOut = %s\n", wine_dbgstr_w(wOut));
1206 ok(wOut[4] == '!', "wOut[4] != \'!\'\n");
1208 ret = wcstombs(NULL, wSimple, 0);
1209 ok(ret == 4, "wcstombs did not return 4\n");
1211 ret = wcstombs(mOut, wSimple, 6);
1212 ok(ret == 4, "wcstombs did not return 4\n");
1213 ok(!memcmp(mOut, mSimple, 5*sizeof(char)), "mOut = %s\n", mOut);
1215 ret = wcstombs(mOut, wSimple, 2);
1216 ok(ret == 2, "wcstombs did not return 2\n");
1217 ok(!memcmp(mOut, mSimple, 5*sizeof(char)), "mOut = %s\n", mOut);
1219 if(!setlocale(LC_ALL, "Japanese_Japan.932")) {
1220 win_skip("Japanese_Japan.932 locale not available\n");
1221 return;
1224 ret = mbstowcs(wOut, mHiragana, 6);
1225 ok(ret == 2, "mbstowcs did not return 2\n");
1226 ok(!memcmp(wOut, wHiragana, sizeof(wHiragana)), "wOut = %s\n", wine_dbgstr_w(wOut));
1228 ret = wcstombs(mOut, wHiragana, 6);
1229 ok(ret == 4, "wcstombs did not return 4\n");
1230 ok(!memcmp(mOut, mHiragana, sizeof(mHiragana)), "mOut = %s\n", mOut);
1232 if(!pmbstowcs_s || !pwcstombs_s) {
1233 win_skip("mbstowcs_s or wcstombs_s not available\n");
1234 return;
1237 err = pmbstowcs_s(&ret, wOut, 6, mSimple, _TRUNCATE);
1238 ok(err == 0, "err = %d\n", err);
1239 ok(ret == 5, "ret = %d\n", (int)ret);
1240 ok(!memcmp(wOut, wSimple, sizeof(wSimple)), "wOut = %s\n", wine_dbgstr_w(wOut));
1242 err = pmbstowcs_s(&ret, wOut, 6, mHiragana, _TRUNCATE);
1243 ok(err == 0, "err = %d\n", err);
1244 ok(ret == 3, "ret = %d\n", (int)ret);
1245 ok(!memcmp(wOut, wHiragana, sizeof(wHiragana)), "wOut = %s\n", wine_dbgstr_w(wOut));
1247 err = pwcstombs_s(&ret, mOut, 6, wSimple, _TRUNCATE);
1248 ok(err == 0, "err = %d\n", err);
1249 ok(ret == 5, "ret = %d\n", (int)ret);
1250 ok(!memcmp(mOut, mSimple, sizeof(mSimple)), "mOut = %s\n", mOut);
1252 err = pwcstombs_s(&ret, mOut, 6, wHiragana, _TRUNCATE);
1253 ok(err == 0, "err = %d\n", err);
1254 ok(ret == 5, "ret = %d\n", (int)ret);
1255 ok(!memcmp(mOut, mHiragana, sizeof(mHiragana)), "mOut = %s\n", mOut);
1258 static void test_gcvt(void)
1260 char buf[1024], *res;
1261 errno_t err;
1263 if(!p_gcvt_s) {
1264 win_skip("Skipping _gcvt tests\n");
1265 return;
1268 errno = 0;
1269 res = _gcvt(1.2, -1, buf);
1270 ok(res == NULL, "res != NULL\n");
1271 ok(errno == ERANGE, "errno = %d\n", errno);
1273 errno = 0;
1274 res = _gcvt(1.2, 5, NULL);
1275 ok(res == NULL, "res != NULL\n");
1276 ok(errno == EINVAL, "errno = %d\n", errno);
1278 res = gcvt(1.2, 5, buf);
1279 ok(res == buf, "res != buf\n");
1280 ok(!strcmp(buf, "1.2"), "buf = %s\n", buf);
1282 buf[0] = 'x';
1283 err = p_gcvt_s(buf, 5, 1.2, 10);
1284 ok(err == ERANGE, "err = %d\n", err);
1285 ok(buf[0] == '\0', "buf[0] = %c\n", buf[0]);
1287 buf[0] = 'x';
1288 err = p_gcvt_s(buf, 4, 123456, 2);
1289 ok(err == ERANGE, "err = %d\n", err);
1290 ok(buf[0] == '\0', "buf[0] = %c\n", buf[0]);
1293 static void test__itoa_s(void)
1295 errno_t ret;
1296 char buffer[33];
1298 if (!p_itoa_s)
1300 win_skip("Skipping _itoa_s tests\n");
1301 return;
1304 if (p_set_invalid_parameter_handler)
1305 ok(p_set_invalid_parameter_handler(test_invalid_parameter_handler) == NULL,
1306 "Invalid parameter handler was already set\n");
1308 errno = EBADF;
1309 ret = p_itoa_s(0, NULL, 0, 0);
1310 ok(ret == EINVAL, "Expected _itoa_s to return EINVAL, got %d\n", ret);
1311 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1313 memset(buffer, 'X', sizeof(buffer));
1314 errno = EBADF;
1315 ret = p_itoa_s(0, buffer, 0, 0);
1316 ok(ret == EINVAL, "Expected _itoa_s to return EINVAL, got %d\n", ret);
1317 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1318 ok(buffer[0] == 'X', "Expected the output buffer to be untouched\n");
1320 memset(buffer, 'X', sizeof(buffer));
1321 errno = EBADF;
1322 ret = p_itoa_s(0, buffer, sizeof(buffer), 0);
1323 ok(ret == EINVAL, "Expected _itoa_s to return EINVAL, got %d\n", ret);
1324 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1325 ok(buffer[0] == '\0', "Expected the output buffer to be null terminated\n");
1327 memset(buffer, 'X', sizeof(buffer));
1328 errno = EBADF;
1329 ret = p_itoa_s(0, buffer, sizeof(buffer), 64);
1330 ok(ret == EINVAL, "Expected _itoa_s to return EINVAL, got %d\n", ret);
1331 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1332 ok(buffer[0] == '\0', "Expected the output buffer to be null terminated\n");
1334 memset(buffer, 'X', sizeof(buffer));
1335 errno = EBADF;
1336 ret = p_itoa_s(12345678, buffer, 4, 10);
1337 ok(ret == ERANGE, "Expected _itoa_s to return ERANGE, got %d\n", ret);
1338 ok(errno == ERANGE, "Expected errno to be ERANGE, got %d\n", errno);
1339 ok(!memcmp(buffer, "\000765", 4),
1340 "Expected the output buffer to be null terminated with truncated output\n");
1342 memset(buffer, 'X', sizeof(buffer));
1343 errno = EBADF;
1344 ret = p_itoa_s(12345678, buffer, 8, 10);
1345 ok(ret == ERANGE, "Expected _itoa_s to return ERANGE, got %d\n", ret);
1346 ok(errno == ERANGE, "Expected errno to be ERANGE, got %d\n", errno);
1347 ok(!memcmp(buffer, "\0007654321", 8),
1348 "Expected the output buffer to be null terminated with truncated output\n");
1350 memset(buffer, 'X', sizeof(buffer));
1351 errno = EBADF;
1352 ret = p_itoa_s(-12345678, buffer, 9, 10);
1353 ok(ret == ERANGE, "Expected _itoa_s to return ERANGE, got %d\n", ret);
1354 ok(errno == ERANGE, "Expected errno to be ERANGE, got %d\n", errno);
1355 ok(!memcmp(buffer, "\00087654321", 9),
1356 "Expected the output buffer to be null terminated with truncated output\n");
1358 ret = p_itoa_s(12345678, buffer, 9, 10);
1359 ok(ret == 0, "Expected _itoa_s to return 0, got %d\n", ret);
1360 ok(!strcmp(buffer, "12345678"),
1361 "Expected output buffer string to be \"12345678\", got \"%s\"\n",
1362 buffer);
1364 ret = p_itoa_s(43690, buffer, sizeof(buffer), 2);
1365 ok(ret == 0, "Expected _itoa_s to return 0, got %d\n", ret);
1366 ok(!strcmp(buffer, "1010101010101010"),
1367 "Expected output buffer string to be \"1010101010101010\", got \"%s\"\n",
1368 buffer);
1370 ret = p_itoa_s(1092009, buffer, sizeof(buffer), 36);
1371 ok(ret == 0, "Expected _itoa_s to return 0, got %d\n", ret);
1372 ok(!strcmp(buffer, "nell"),
1373 "Expected output buffer string to be \"nell\", got \"%s\"\n",
1374 buffer);
1376 ret = p_itoa_s(5704, buffer, sizeof(buffer), 18);
1377 ok(ret == 0, "Expected _itoa_s to return 0, got %d\n", ret);
1378 ok(!strcmp(buffer, "hag"),
1379 "Expected output buffer string to be \"hag\", got \"%s\"\n",
1380 buffer);
1382 ret = p_itoa_s(-12345678, buffer, sizeof(buffer), 10);
1383 ok(ret == 0, "Expected _itoa_s to return 0, got %d\n", ret);
1384 ok(!strcmp(buffer, "-12345678"),
1385 "Expected output buffer string to be \"-12345678\", got \"%s\"\n",
1386 buffer);
1387 if (p_set_invalid_parameter_handler)
1388 ok(p_set_invalid_parameter_handler(NULL) == test_invalid_parameter_handler,
1389 "Cannot reset invalid parameter handler\n");
1392 static void test__strlwr_s(void)
1394 errno_t ret;
1395 char buffer[20];
1397 if (!p_strlwr_s)
1399 win_skip("Skipping _strlwr_s tests\n");
1400 return;
1403 errno = EBADF;
1404 ret = p_strlwr_s(NULL, 0);
1405 ok(ret == EINVAL, "Expected _strlwr_s to return EINVAL, got %d\n", ret);
1406 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1408 errno = EBADF;
1409 ret = p_strlwr_s(NULL, sizeof(buffer));
1410 ok(ret == EINVAL, "Expected _strlwr_s to return EINVAL, got %d\n", ret);
1411 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1413 errno = EBADF;
1414 ret = p_strlwr_s(buffer, 0);
1415 ok(ret == EINVAL, "Expected _strlwr_s to return EINVAL, got %d\n", ret);
1416 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1418 strcpy(buffer, "GoRrIsTeR");
1419 errno = EBADF;
1420 ret = p_strlwr_s(buffer, 5);
1421 ok(ret == EINVAL, "Expected _strlwr_s to return EINVAL, got %d\n", ret);
1422 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1423 ok(!memcmp(buffer, "\0oRrIsTeR", sizeof("\0oRrIsTeR")),
1424 "Expected the output buffer to be \"gorrIsTeR\"\n");
1426 strcpy(buffer, "GoRrIsTeR");
1427 errno = EBADF;
1428 ret = p_strlwr_s(buffer, sizeof("GoRrIsTeR") - 1);
1429 ok(ret == EINVAL, "Expected _strlwr_s to return EINVAL, got %d\n", ret);
1430 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1431 ok(!memcmp(buffer, "\0oRrIsTeR", sizeof("\0oRrIsTeR")),
1432 "Expected the output buffer to be \"gorrIsTeR\"\n");
1434 strcpy(buffer, "GoRrIsTeR");
1435 ret = p_strlwr_s(buffer, sizeof("GoRrIsTeR"));
1436 ok(ret == 0, "Expected _strlwr_s to return 0, got %d\n", ret);
1437 ok(!strcmp(buffer, "gorrister"),
1438 "Expected the output buffer to be \"gorrister\", got \"%s\"\n",
1439 buffer);
1441 memcpy(buffer, "GoRrIsTeR\0ELLEN", sizeof("GoRrIsTeR\0ELLEN"));
1442 ret = p_strlwr_s(buffer, sizeof(buffer));
1443 ok(ret == 0, "Expected _strlwr_s to return 0, got %d\n", ret);
1444 ok(!memcmp(buffer, "gorrister\0ELLEN", sizeof("gorrister\0ELLEN")),
1445 "Expected the output buffer to be \"gorrister\\0ELLEN\", got \"%s\"\n",
1446 buffer);
1449 static void test_wcsncat_s(void)
1451 static wchar_t abcW[] = {'a','b','c',0};
1452 int ret;
1453 wchar_t dst[4];
1454 wchar_t src[4];
1456 if (!p_wcsncat_s)
1458 win_skip("skipping wcsncat_s tests\n");
1459 return;
1462 memcpy(src, abcW, sizeof(abcW));
1463 dst[0] = 0;
1464 ret = p_wcsncat_s(NULL, 4, src, 4);
1465 ok(ret == EINVAL, "err = %d\n", ret);
1466 ret = p_wcsncat_s(dst, 0, src, 4);
1467 ok(ret == EINVAL, "err = %d\n", ret);
1468 ret = p_wcsncat_s(dst, 0, src, _TRUNCATE);
1469 ok(ret == EINVAL, "err = %d\n", ret);
1470 ret = p_wcsncat_s(dst, 4, NULL, 0);
1471 ok(ret == 0, "err = %d\n", ret);
1473 dst[0] = 0;
1474 ret = p_wcsncat_s(dst, 2, src, 4);
1475 ok(ret == ERANGE, "err = %d\n", ret);
1477 dst[0] = 0;
1478 ret = p_wcsncat_s(dst, 2, src, _TRUNCATE);
1479 ok(ret == STRUNCATE, "err = %d\n", ret);
1480 ok(dst[0] == 'a' && dst[1] == 0, "dst is %s\n", wine_dbgstr_w(dst));
1482 memcpy(dst, abcW, sizeof(abcW));
1483 dst[3] = 'd';
1484 ret = p_wcsncat_s(dst, 4, src, 4);
1485 ok(ret == EINVAL, "err = %d\n", ret);
1488 static void test__mbsnbcat_s(void)
1490 unsigned char dest[16];
1491 const unsigned char first[] = "dinosaur";
1492 const unsigned char second[] = "duck";
1493 int ret;
1495 if (!p_mbsnbcat_s)
1497 win_skip("Skipping _mbsnbcat_s tests\n");
1498 return;
1501 /* Test invalid arguments. */
1502 ret = p_mbsnbcat_s(NULL, 0, NULL, 0);
1503 ok(ret == 0, "Expected _mbsnbcat_s to return 0, got %d\n", ret);
1505 errno = EBADF;
1506 ret = p_mbsnbcat_s(NULL, 10, NULL, 0);
1507 ok(ret == EINVAL, "Expected _mbsnbcat_s to return EINVAL, got %d\n", ret);
1508 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1510 errno = EBADF;
1511 ret = p_mbsnbcat_s(NULL, 0, NULL, 10);
1512 ok(ret == EINVAL, "Expected _mbsnbcat_s to return EINVAL, got %d\n", ret);
1513 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1515 memset(dest, 'X', sizeof(dest));
1516 errno = EBADF;
1517 ret = p_mbsnbcat_s(dest, 0, NULL, 0);
1518 ok(ret == EINVAL, "Expected _mbsnbcat_s to return EINVAL, got %d\n", ret);
1519 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1520 ok(dest[0] == 'X', "Expected the output buffer to be untouched\n");
1522 memset(dest, 'X', sizeof(dest));
1523 errno = EBADF;
1524 ret = p_mbsnbcat_s(dest, 0, second, 0);
1525 ok(ret == EINVAL, "Expected _mbsnbcat_s to return EINVAL, got %d\n", ret);
1526 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1527 ok(dest[0] == 'X', "Expected the output buffer to be untouched\n");
1529 memset(dest, 'X', sizeof(dest));
1530 errno = EBADF;
1531 ret = p_mbsnbcat_s(dest, sizeof(dest), NULL, 0);
1532 ok(ret == EINVAL, "Expected _mbsnbcat_s to return EINVAL, got %d\n", ret);
1533 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1534 ok(dest[0] == '\0', "Expected the output buffer to be null terminated\n");
1536 memset(dest, 'X', sizeof(dest));
1537 errno = EBADF;
1538 ret = p_mbsnbcat_s(dest, sizeof(dest), NULL, 10);
1539 ok(ret == EINVAL, "Expected _mbsnbcat_s to return EINVAL, got %d\n", ret);
1540 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1541 ok(dest[0] == '\0', "Expected the output buffer to be null terminated\n");
1543 memset(dest, 'X', sizeof(dest));
1544 dest[0] = '\0';
1545 ret = p_mbsnbcat_s(dest, sizeof(dest), second, sizeof(second));
1546 ok(ret == 0, "Expected _mbsnbcat_s to return 0, got %d\n", ret);
1547 ok(!memcmp(dest, second, sizeof(second)),
1548 "Expected the output buffer string to be \"duck\"\n");
1550 /* Test source truncation behavior. */
1551 memset(dest, 'X', sizeof(dest));
1552 memcpy(dest, first, sizeof(first));
1553 ret = p_mbsnbcat_s(dest, sizeof(dest), second, 0);
1554 ok(ret == 0, "Expected _mbsnbcat_s to return 0, got %d\n", ret);
1555 ok(!memcmp(dest, first, sizeof(first)),
1556 "Expected the output buffer string to be \"dinosaur\"\n");
1558 memset(dest, 'X', sizeof(dest));
1559 memcpy(dest, first, sizeof(first));
1560 ret = p_mbsnbcat_s(dest, sizeof(dest), second, sizeof(second));
1561 ok(ret == 0, "Expected _mbsnbcat_s to return 0, got %d\n", ret);
1562 ok(!memcmp(dest, "dinosaurduck", sizeof("dinosaurduck")),
1563 "Expected the output buffer string to be \"dinosaurduck\"\n");
1565 memset(dest, 'X', sizeof(dest));
1566 memcpy(dest, first, sizeof(first));
1567 ret = p_mbsnbcat_s(dest, sizeof(dest), second, sizeof(second) + 1);
1568 ok(ret == 0, "Expected _mbsnbcat_s to return 0, got %d\n", ret);
1569 ok(!memcmp(dest, "dinosaurduck", sizeof("dinosaurduck")),
1570 "Expected the output buffer string to be \"dinosaurduck\"\n");
1572 memset(dest, 'X', sizeof(dest));
1573 memcpy(dest, first, sizeof(first));
1574 ret = p_mbsnbcat_s(dest, sizeof(dest), second, sizeof(second) - 1);
1575 ok(ret == 0, "Expected _mbsnbcat_s to return 0, got %d\n", ret);
1576 ok(!memcmp(dest, "dinosaurduck", sizeof("dinosaurduck")),
1577 "Expected the output buffer string to be \"dinosaurduck\"\n");
1579 memset(dest, 'X', sizeof(dest));
1580 memcpy(dest, first, sizeof(first));
1581 ret = p_mbsnbcat_s(dest, sizeof(dest), second, sizeof(second) - 2);
1582 ok(ret == 0, "Expected _mbsnbcat_s to return 0, got %d\n", ret);
1583 ok(!memcmp(dest, "dinosaurduc", sizeof("dinosaurduc")),
1584 "Expected the output buffer string to be \"dinosaurduc\"\n");
1586 /* Test destination truncation behavior. */
1587 memset(dest, 'X', sizeof(dest));
1588 memcpy(dest, first, sizeof(first));
1589 errno = EBADF;
1590 ret = p_mbsnbcat_s(dest, sizeof(first) - 1, second, sizeof(second));
1591 ok(ret == EINVAL, "Expected _mbsnbcat_s to return EINVAL, got %d\n", ret);
1592 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1593 ok(!memcmp(dest, "\0inosaur", sizeof("\0inosaur") - 1),
1594 "Expected the output buffer string to be \"\\0inosaur\" without ending null terminator\n");
1596 memset(dest, 'X', sizeof(dest));
1597 memcpy(dest, first, sizeof(first));
1598 errno = EBADF;
1599 ret = p_mbsnbcat_s(dest, sizeof(first), second, sizeof(second));
1600 ok(ret == ERANGE, "Expected _mbsnbcat_s to return ERANGE, got %d\n", ret);
1601 ok(errno == ERANGE, "Expected errno to be ERANGE, got %d\n", errno);
1602 ok(!memcmp(dest, "\0inosaurd", sizeof("\0inosaurd") - 1),
1603 "Expected the output buffer string to be \"\\0inosaurd\" without ending null terminator\n");
1605 memset(dest, 'X', sizeof(dest));
1606 memcpy(dest, first, sizeof(first));
1607 errno = EBADF;
1608 ret = p_mbsnbcat_s(dest, sizeof(first) + 1, second, sizeof(second));
1609 ok(ret == ERANGE, "Expected _mbsnbcat_s to return ERANGE, got %d\n", ret);
1610 ok(errno == ERANGE, "Expected errno to be ERANGE, got %d\n", errno);
1611 ok(!memcmp(dest, "\0inosaurdu", sizeof("\0inosaurdu") - 1),
1612 "Expected the output buffer string to be \"\\0inosaurdu\" without ending null terminator\n");
1615 static void test__ultoa_s(void)
1617 errno_t ret;
1618 char buffer[33];
1620 if (!p_ultoa_s)
1622 win_skip("Skipping _ultoa_s tests\n");
1623 return;
1626 errno = EBADF;
1627 ret = p_ultoa_s(0, NULL, 0, 0);
1628 ok(ret == EINVAL, "Expected _ultoa_s to return EINVAL, got %d\n", ret);
1629 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1631 memset(buffer, 'X', sizeof(buffer));
1632 errno = EBADF;
1633 ret = p_ultoa_s(0, buffer, 0, 0);
1634 ok(ret == EINVAL, "Expected _ultoa_s to return EINVAL, got %d\n", ret);
1635 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1636 ok(buffer[0] == 'X', "Expected the output buffer to be untouched\n");
1638 memset(buffer, 'X', sizeof(buffer));
1639 errno = EBADF;
1640 ret = p_ultoa_s(0, buffer, sizeof(buffer), 0);
1641 ok(ret == EINVAL, "Expected _ultoa_s to return EINVAL, got %d\n", ret);
1642 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1643 ok(buffer[0] == '\0', "Expected the output buffer to be null terminated\n");
1645 memset(buffer, 'X', sizeof(buffer));
1646 errno = EBADF;
1647 ret = p_ultoa_s(0, buffer, sizeof(buffer), 64);
1648 ok(ret == EINVAL, "Expected _ultoa_s to return EINVAL, got %d\n", ret);
1649 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1650 ok(buffer[0] == '\0', "Expected the output buffer to be null terminated\n");
1652 memset(buffer, 'X', sizeof(buffer));
1653 errno = EBADF;
1654 ret = p_ultoa_s(12345678, buffer, 4, 10);
1655 ok(ret == ERANGE, "Expected _ultoa_s to return ERANGE, got %d\n", ret);
1656 ok(errno == ERANGE, "Expected errno to be ERANGE, got %d\n", errno);
1657 ok(!memcmp(buffer, "\000765", 4),
1658 "Expected the output buffer to be null terminated with truncated output\n");
1660 memset(buffer, 'X', sizeof(buffer));
1661 errno = EBADF;
1662 ret = p_ultoa_s(12345678, buffer, 8, 10);
1663 ok(ret == ERANGE, "Expected _ultoa_s to return ERANGE, got %d\n", ret);
1664 ok(errno == ERANGE, "Expected errno to be ERANGE, got %d\n", errno);
1665 ok(!memcmp(buffer, "\0007654321", 8),
1666 "Expected the output buffer to be null terminated with truncated output\n");
1668 ret = p_ultoa_s(12345678, buffer, 9, 10);
1669 ok(ret == 0, "Expected _ultoa_s to return 0, got %d\n", ret);
1670 ok(!strcmp(buffer, "12345678"),
1671 "Expected output buffer string to be \"12345678\", got \"%s\"\n",
1672 buffer);
1674 ret = p_ultoa_s(43690, buffer, sizeof(buffer), 2);
1675 ok(ret == 0, "Expected _ultoa_s to return 0, got %d\n", ret);
1676 ok(!strcmp(buffer, "1010101010101010"),
1677 "Expected output buffer string to be \"1010101010101010\", got \"%s\"\n",
1678 buffer);
1680 ret = p_ultoa_s(1092009, buffer, sizeof(buffer), 36);
1681 ok(ret == 0, "Expected _ultoa_s to return 0, got %d\n", ret);
1682 ok(!strcmp(buffer, "nell"),
1683 "Expected output buffer string to be \"nell\", got \"%s\"\n",
1684 buffer);
1686 ret = p_ultoa_s(5704, buffer, sizeof(buffer), 18);
1687 ok(ret == 0, "Expected _ultoa_s to return 0, got %d\n", ret);
1688 ok(!strcmp(buffer, "hag"),
1689 "Expected output buffer string to be \"hag\", got \"%s\"\n",
1690 buffer);
1693 START_TEST(string)
1695 char mem[100];
1696 static const char xilstring[]="c:/xilinx";
1697 int nLen;
1699 hMsvcrt = GetModuleHandleA("msvcrt.dll");
1700 if (!hMsvcrt)
1701 hMsvcrt = GetModuleHandleA("msvcrtd.dll");
1702 ok(hMsvcrt != 0, "GetModuleHandleA failed\n");
1703 SET(pmemcpy,"memcpy");
1704 SET(pmemcmp,"memcmp");
1705 SET(p_mbctype,"_mbctype");
1706 SET(p__mb_cur_max,"__mb_cur_max");
1707 pstrcpy_s = (void *)GetProcAddress( hMsvcrt,"strcpy_s" );
1708 pstrcat_s = (void *)GetProcAddress( hMsvcrt,"strcat_s" );
1709 p_mbsnbcat_s = (void *)GetProcAddress( hMsvcrt,"_mbsnbcat_s" );
1710 p_mbsnbcpy_s = (void *)GetProcAddress( hMsvcrt,"_mbsnbcpy_s" );
1711 p_wcscpy_s = (void *)GetProcAddress( hMsvcrt,"wcscpy_s" );
1712 p_wcsncat_s = (void *)GetProcAddress( hMsvcrt,"wcsncat_s" );
1713 p_wcsupr_s = (void *)GetProcAddress( hMsvcrt,"_wcsupr_s" );
1714 p_strnlen = (void *)GetProcAddress( hMsvcrt,"strnlen" );
1715 p_strtoi64 = (void *)GetProcAddress(hMsvcrt, "_strtoi64");
1716 p_strtoui64 = (void *)GetProcAddress(hMsvcrt, "_strtoui64");
1717 pmbstowcs_s = (void *)GetProcAddress(hMsvcrt, "mbstowcs_s");
1718 pwcstombs_s = (void *)GetProcAddress(hMsvcrt, "wcstombs_s");
1719 p_gcvt_s = (void *)GetProcAddress(hMsvcrt, "_gcvt_s");
1720 p_itoa_s = (void *)GetProcAddress(hMsvcrt, "_itoa_s");
1721 p_strlwr_s = (void *)GetProcAddress(hMsvcrt, "_strlwr_s");
1722 p_ultoa_s = (void *)GetProcAddress(hMsvcrt, "_ultoa_s");
1723 p_set_invalid_parameter_handler = (void *) GetProcAddress(hMsvcrt, "_set_invalid_parameter_handler");
1725 /* MSVCRT memcpy behaves like memmove for overlapping moves,
1726 MFC42 CString::Insert seems to rely on that behaviour */
1727 strcpy(mem,xilstring);
1728 nLen=strlen(xilstring);
1729 pmemcpy(mem+5, mem,nLen+1);
1730 ok(pmemcmp(mem+5,xilstring, nLen) == 0,
1731 "Got result %s\n",mem+5);
1733 /* Test _swab function */
1734 test_swab();
1736 /* Test ismbblead*/
1737 test_mbcp();
1738 /* test _mbsspn */
1739 test_mbsspn();
1740 test_mbsspnp();
1741 /* test _strdup */
1742 test_strdup();
1743 test_strcpy_s();
1744 test_strcat_s();
1745 test__mbsnbcpy_s();
1746 test_mbcjisjms();
1747 test_mbctombb();
1748 test_ismbclegal();
1749 test_strtok();
1750 test_wcscpy_s();
1751 test__wcsupr_s();
1752 test_strtol();
1753 test_strnlen();
1754 test__strtoi64();
1755 test__strtod();
1756 test_mbstowcs();
1757 test_gcvt();
1758 test__itoa_s();
1759 test__strlwr_s();
1760 test_wcsncat_s();
1761 test__mbsnbcat_s();
1762 test__ultoa_s();