crypt32: Pass context as context_t to Context_Release.
[wine.git] / dlls / msvcrt / tests / string.c
blob3877d18ef1bbae7648759f180a87a16e6fceba54
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 "winnls.h"
24 #include <string.h>
25 #include <mbstring.h>
26 #include <wchar.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <mbctype.h>
30 #include <locale.h>
31 #include <errno.h>
32 #include <limits.h>
33 #include <math.h>
35 static char *buf_to_string(const unsigned char *bin, int len, int nr)
37 static char buf[2][1024];
38 char *w = buf[nr];
39 int i;
41 for (i = 0; i < len; i++)
43 sprintf(w, "%02x ", (unsigned char)bin[i]);
44 w += strlen(w);
46 return buf[nr];
49 static void __cdecl test_invalid_parameter_handler(const wchar_t *expression,
50 const wchar_t *function, const wchar_t *file,
51 unsigned line, uintptr_t arg)
53 /* we just ignore handler calls */
56 #define expect_eq(expr, value, type, format) { type ret = (expr); ok((value) == ret, #expr " expected " format " got " format "\n", value, ret); }
57 #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)); }
59 static void* (__cdecl *pmemcpy)(void *, const void *, size_t n);
60 static int (__cdecl *p_memcpy_s)(void *, size_t, const void *, size_t);
61 static int (__cdecl *p_memmove_s)(void *, size_t, const void *, size_t);
62 static int* (__cdecl *pmemcmp)(void *, const void *, size_t n);
63 static int (__cdecl *pstrcpy_s)(char *dst, size_t len, const char *src);
64 static int (__cdecl *pstrcat_s)(char *dst, size_t len, const char *src);
65 static int (__cdecl *p_mbsnbcat_s)(unsigned char *dst, size_t size, const unsigned char *src, size_t count);
66 static int (__cdecl *p_mbsnbcpy_s)(unsigned char * dst, size_t size, const unsigned char * src, size_t count);
67 static int (__cdecl *p_wcscpy_s)(wchar_t *wcDest, size_t size, const wchar_t *wcSrc);
68 static int (__cdecl *p_wcsncpy_s)(wchar_t *wcDest, size_t size, const wchar_t *wcSrc, size_t count);
69 static int (__cdecl *p_wcsncat_s)(wchar_t *dst, size_t elem, const wchar_t *src, size_t count);
70 static int (__cdecl *p_wcsupr_s)(wchar_t *str, size_t size);
71 static size_t (__cdecl *p_strnlen)(const char *, size_t);
72 static __int64 (__cdecl *p_strtoi64)(const char *, char **, int);
73 static unsigned __int64 (__cdecl *p_strtoui64)(const char *, char **, int);
74 static __int64 (__cdecl *p_wcstoi64)(const wchar_t *, wchar_t **, int);
75 static unsigned __int64 (__cdecl *p_wcstoui64)(const wchar_t *, wchar_t **, int);
76 static int (__cdecl *pwcstombs_s)(size_t*,char*,size_t,const wchar_t*,size_t);
77 static int (__cdecl *pmbstowcs_s)(size_t*,wchar_t*,size_t,const char*,size_t);
78 static size_t (__cdecl *p_mbsrtowcs)(wchar_t*, const char**, size_t, mbstate_t*);
79 static size_t (__cdecl *pwcsrtombs)(char*, const wchar_t**, size_t, int*);
80 static errno_t (__cdecl *p_gcvt_s)(char*,size_t,double,int);
81 static errno_t (__cdecl *p_itoa_s)(int,char*,size_t,int);
82 static errno_t (__cdecl *p_strlwr_s)(char*,size_t);
83 static errno_t (__cdecl *p_ultoa_s)(__msvcrt_ulong,char*,size_t,int);
84 static int *p__mb_cur_max;
85 static unsigned char *p_mbctype;
86 static _invalid_parameter_handler (__cdecl *p_set_invalid_parameter_handler)(_invalid_parameter_handler);
87 static int (__cdecl *p_wcslwr_s)(wchar_t*,size_t);
88 static errno_t (__cdecl *p_mbsupr_s)(unsigned char *str, size_t numberOfElements);
89 static errno_t (__cdecl *p_mbslwr_s)(unsigned char *str, size_t numberOfElements);
90 static int (__cdecl *p_wctob)(wint_t);
91 static size_t (__cdecl *p_wcrtomb)(char*, wchar_t, mbstate_t*);
92 static int (__cdecl *p_tolower)(int);
93 static size_t (__cdecl *p_mbrlen)(const char*, size_t, mbstate_t*);
94 static size_t (__cdecl *p_mbrtowc)(wchar_t*, const char*, size_t, mbstate_t*);
95 static int (__cdecl *p__atodbl_l)(_CRT_DOUBLE*,char*,_locale_t);
97 #define SETNOFAIL(x,y) x = (void*)GetProcAddress(hMsvcrt,y)
98 #define SET(x,y) SETNOFAIL(x,y); ok(x != NULL, "Export '%s' not found\n", y)
100 static HMODULE hMsvcrt;
102 static void test_swab( void ) {
103 char original[] = "BADCFEHGJILKNMPORQTSVUXWZY@#";
104 char expected1[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ@#";
105 char expected2[] = "ABCDEFGHIJKLMNOPQRSTUVWX$";
106 char expected3[] = "$";
108 char from[30];
109 char to[30];
111 int testsize;
113 /* Test 1 - normal even case */
114 memset(to,'$', sizeof(to));
115 memset(from,'@', sizeof(from));
116 testsize = 26;
117 memcpy(from, original, testsize);
118 _swab( from, to, testsize );
119 ok(memcmp(to,expected1,testsize) == 0, "Testing even size %d returned '%*.*s'\n", testsize, testsize, testsize, to);
121 /* Test 2 - uneven case */
122 memset(to,'$', sizeof(to));
123 memset(from,'@', sizeof(from));
124 testsize = 25;
125 memcpy(from, original, testsize);
126 _swab( from, to, testsize );
127 ok(memcmp(to,expected2,testsize) == 0, "Testing odd size %d returned '%*.*s'\n", testsize, testsize, testsize, to);
129 /* Test 3 - from = to */
130 memset(to,'$', sizeof(to));
131 memset(from,'@', sizeof(from));
132 testsize = 26;
133 memcpy(to, original, testsize);
134 _swab( to, to, testsize );
135 ok(memcmp(to,expected1,testsize) == 0, "Testing overlapped size %d returned '%*.*s'\n", testsize, testsize, testsize, to);
137 /* Test 4 - 1 bytes */
138 memset(to,'$', sizeof(to));
139 memset(from,'@', sizeof(from));
140 testsize = 1;
141 memcpy(from, original, testsize);
142 _swab( from, to, testsize );
143 ok(memcmp(to,expected3,testsize) == 0, "Testing small size %d returned '%*.*s'\n", testsize, testsize, testsize, to);
146 #if 0 /* use this to generate more tests */
148 static void test_codepage(int cp)
150 int i;
151 int prev;
152 int count = 1;
154 ok(_setmbcp(cp) == 0, "Couldn't set mbcp\n");
156 prev = p_mbctype[0];
157 printf("static int result_cp_%d_mbctype[] = { ", cp);
158 for (i = 1; i < 257; i++)
160 if (p_mbctype[i] != prev)
162 printf("0x%x,%d, ", prev, count);
163 prev = p_mbctype[i];
164 count = 1;
166 else
167 count++;
169 printf("0x%x,%d };\n", prev, count);
172 #else
174 /* RLE-encoded mbctype tables for given codepages */
175 static int result_cp_932_mbctype[] = { 0x0,65, 0x8,1, 0x18,26, 0x8,6, 0x28,26, 0x8,4,
176 0x0,1, 0x8,1, 0xc,31, 0x8,1, 0xa,5, 0x9,58, 0xc,29, 0,3 };
177 static int result_cp_936_mbctype[] = { 0x0,65, 0x8,1, 0x18,26, 0x8,6, 0x28,26, 0x8,6,
178 0xc,126, 0,1 };
179 static int result_cp_949_mbctype[] = { 0x0,66, 0x18,26, 0x8,6, 0x28,26, 0x8,6, 0xc,126,
180 0,1 };
181 static int result_cp_950_mbctype[] = { 0x0,65, 0x8,1, 0x18,26, 0x8,6, 0x28,26, 0x8,4,
182 0x0,2, 0x4,32, 0xc,94, 0,1 };
184 static void test_cp_table(int cp, int *result)
186 int i;
187 int count = 0;
188 int curr = 0;
189 _setmbcp(cp);
190 for (i = 0; i < 256; i++)
192 if (count == 0)
194 curr = result[0];
195 count = result[1];
196 result += 2;
198 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);
199 count--;
203 #define test_codepage(num) test_cp_table(num, result_cp_##num##_mbctype);
205 #endif
207 static void test_mbcp(void)
209 int mb_orig_max = *p__mb_cur_max;
210 int curr_mbcp = _getmbcp();
211 unsigned char *mbstring = (unsigned char *)"\xb0\xb1\xb2 \xb3\xb4 \xb5"; /* incorrect string */
212 unsigned char *mbstring2 = (unsigned char *)"\xb0\xb1\xb2\xb3Q\xb4\xb5"; /* correct string */
213 unsigned char *mbsonlylead = (unsigned char *)"\xb0\0\xb1\xb2 \xb3";
214 unsigned char buf[16];
215 int step;
216 CPINFO cp_info;
218 /* _mbtype tests */
220 /* An SBCS codepage test. The ctype of characters on e.g. CP1252 or CP1250 differs slightly
221 * between versions of Windows. Also Windows 9x seems to ignore the codepage and always uses
222 * CP1252 (or the ACP?) so we test only a few ASCII characters */
223 _setmbcp(1252);
224 expect_eq(p_mbctype[10], 0, char, "%x");
225 expect_eq(p_mbctype[50], 0, char, "%x");
226 expect_eq(p_mbctype[66], _SBUP, char, "%x");
227 expect_eq(p_mbctype[100], _SBLOW, char, "%x");
228 expect_eq(p_mbctype[128], 0, char, "%x");
229 _setmbcp(1250);
230 expect_eq(p_mbctype[10], 0, char, "%x");
231 expect_eq(p_mbctype[50], 0, char, "%x");
232 expect_eq(p_mbctype[66], _SBUP, char, "%x");
233 expect_eq(p_mbctype[100], _SBLOW, char, "%x");
234 expect_eq(p_mbctype[128], 0, char, "%x");
236 /* double byte code pages */
237 test_codepage(932);
238 test_codepage(936);
239 test_codepage(949);
240 test_codepage(950);
242 _setmbcp(936);
243 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);
244 ok(_ismbblead('\354'), "\354 should be a lead byte\n");
245 ok(_ismbblead(' ') == FALSE, "' ' should not be a lead byte\n");
246 ok(_ismbblead(0x1234b0), "0x1234b0 should not be a lead byte\n");
247 ok(_ismbblead(0x123420) == FALSE, "0x123420 should not be a lead byte\n");
248 ok(_ismbbtrail('\xb0'), "\xa0 should be a trail byte\n");
249 ok(_ismbbtrail(' ') == FALSE, "' ' should not be a trail byte\n");
251 /* _ismbslead */
252 expect_eq(_ismbslead(mbstring, &mbstring[0]), -1, int, "%d");
253 expect_eq(_ismbslead(mbstring, &mbstring[1]), FALSE, int, "%d");
254 expect_eq(_ismbslead(mbstring, &mbstring[2]), -1, int, "%d");
255 expect_eq(_ismbslead(mbstring, &mbstring[3]), FALSE, int, "%d");
256 expect_eq(_ismbslead(mbstring, &mbstring[4]), -1, int, "%d");
257 expect_eq(_ismbslead(mbstring, &mbstring[5]), FALSE, int, "%d");
258 expect_eq(_ismbslead(mbstring, &mbstring[6]), FALSE, int, "%d");
259 expect_eq(_ismbslead(mbstring, &mbstring[7]), -1, int, "%d");
260 expect_eq(_ismbslead(mbstring, &mbstring[8]), FALSE, int, "%d");
262 expect_eq(_ismbslead(mbsonlylead, &mbsonlylead[0]), -1, int, "%d");
263 expect_eq(_ismbslead(mbsonlylead, &mbsonlylead[1]), FALSE, int, "%d");
264 expect_eq(_ismbslead(mbsonlylead, &mbsonlylead[2]), FALSE, int, "%d");
265 expect_eq(_ismbslead(mbsonlylead, &mbsonlylead[5]), FALSE, int, "%d");
267 /* _ismbstrail */
268 expect_eq(_ismbstrail(mbstring, &mbstring[0]), FALSE, int, "%d");
269 expect_eq(_ismbstrail(mbstring, &mbstring[1]), -1, int, "%d");
270 expect_eq(_ismbstrail(mbstring, &mbstring[2]), FALSE, int, "%d");
271 expect_eq(_ismbstrail(mbstring, &mbstring[3]), -1, int, "%d");
272 expect_eq(_ismbstrail(mbstring, &mbstring[4]), FALSE, int, "%d");
273 expect_eq(_ismbstrail(mbstring, &mbstring[5]), -1, int, "%d");
274 expect_eq(_ismbstrail(mbstring, &mbstring[6]), FALSE, int, "%d");
275 expect_eq(_ismbstrail(mbstring, &mbstring[7]), FALSE, int, "%d");
276 expect_eq(_ismbstrail(mbstring, &mbstring[8]), -1, int, "%d");
278 expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[0]), FALSE, int, "%d");
279 expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[1]), -1, int, "%d");
280 expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[2]), FALSE, int, "%d");
281 expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[3]), FALSE, int, "%d");
282 expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[4]), FALSE, int, "%d");
283 expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[5]), FALSE, int, "%d");
285 /* _mbsbtype */
286 expect_eq(_mbsbtype(mbstring, 0), _MBC_LEAD, int, "%d");
287 expect_eq(_mbsbtype(mbstring, 1), _MBC_TRAIL, int, "%d");
288 expect_eq(_mbsbtype(mbstring, 2), _MBC_LEAD, int, "%d");
289 expect_eq(_mbsbtype(mbstring, 3), _MBC_ILLEGAL, int, "%d");
290 expect_eq(_mbsbtype(mbstring, 4), _MBC_LEAD, int, "%d");
291 expect_eq(_mbsbtype(mbstring, 5), _MBC_TRAIL, int, "%d");
292 expect_eq(_mbsbtype(mbstring, 6), _MBC_SINGLE, int, "%d");
293 expect_eq(_mbsbtype(mbstring, 7), _MBC_LEAD, int, "%d");
294 expect_eq(_mbsbtype(mbstring, 8), _MBC_ILLEGAL, int, "%d");
296 expect_eq(_mbsbtype(mbsonlylead, 0), _MBC_LEAD, int, "%d");
297 expect_eq(_mbsbtype(mbsonlylead, 1), _MBC_ILLEGAL, int, "%d");
298 expect_eq(_mbsbtype(mbsonlylead, 2), _MBC_ILLEGAL, int, "%d");
299 expect_eq(_mbsbtype(mbsonlylead, 3), _MBC_ILLEGAL, int, "%d");
300 expect_eq(_mbsbtype(mbsonlylead, 4), _MBC_ILLEGAL, int, "%d");
301 expect_eq(_mbsbtype(mbsonlylead, 5), _MBC_ILLEGAL, int, "%d");
303 /* _mbsnextc */
304 expect_eq(_mbsnextc(mbstring), 0xb0b1, int, "%x");
305 expect_eq(_mbsnextc(&mbstring[2]), 0xb220, int, "%x"); /* lead + invalid tail */
306 expect_eq(_mbsnextc(&mbstring[3]), 0x20, int, "%x"); /* single char */
308 /* _mbclen/_mbslen */
309 expect_eq(_mbclen(mbstring), 2, int, "%d");
310 expect_eq(_mbclen(&mbstring[2]), 2, int, "%d");
311 expect_eq(_mbclen(&mbstring[3]), 1, int, "%d");
312 expect_eq(_mbslen(mbstring2), 4, int, "%d");
313 expect_eq(_mbslen(mbsonlylead), 0, int, "%d"); /* lead + NUL not counted as character */
314 expect_eq(_mbslen(mbstring), 4, int, "%d"); /* lead + invalid trail counted */
316 /* mbrlen */
317 if(!setlocale(LC_ALL, ".936") || !p_mbrlen) {
318 win_skip("mbrlen tests\n");
319 }else {
320 mbstate_t state = 0;
321 expect_eq(p_mbrlen((const char*)mbstring, 2, NULL), 2, int, "%d");
322 expect_eq(p_mbrlen((const char*)&mbstring[2], 2, NULL), 2, int, "%d");
323 expect_eq(p_mbrlen((const char*)&mbstring[3], 2, NULL), 1, int, "%d");
324 expect_eq(p_mbrlen((const char*)mbstring, 1, NULL), -2, int, "%d");
325 expect_eq(p_mbrlen((const char*)mbstring, 1, &state), -2, int, "%d");
326 ok(state == mbstring[0], "incorrect state value (%x)\n", state);
327 expect_eq(p_mbrlen((const char*)&mbstring[1], 1, &state), 2, int, "%d");
330 /* mbrtowc */
331 if(!setlocale(LC_ALL, ".936") || !p_mbrtowc) {
332 win_skip("mbrtowc tests\n");
333 }else {
334 mbstate_t state = 0;
335 wchar_t dst;
336 expect_eq(p_mbrtowc(&dst, (const char*)mbstring, 2, NULL), 2, int, "%d");
337 ok(dst == 0x6c28, "dst = %x, expected 0x6c28\n", dst);
338 expect_eq(p_mbrtowc(&dst, (const char*)mbstring+2, 2, NULL), 2, int, "%d");
339 ok(dst == 0x3f, "dst = %x, expected 0x3f\n", dst);
340 expect_eq(p_mbrtowc(&dst, (const char*)mbstring+3, 2, NULL), 1, int, "%d");
341 ok(dst == 0x20, "dst = %x, expected 0x20\n", dst);
342 expect_eq(p_mbrtowc(&dst, (const char*)mbstring, 1, NULL), -2, int, "%d");
343 ok(dst == 0, "dst = %x, expected 0\n", dst);
344 expect_eq(p_mbrtowc(&dst, (const char*)mbstring, 1, &state), -2, int, "%d");
345 ok(dst == 0, "dst = %x, expected 0\n", dst);
346 ok(state == mbstring[0], "incorrect state value (%x)\n", state);
347 expect_eq(p_mbrtowc(&dst, (const char*)mbstring+1, 1, &state), 2, int, "%d");
348 ok(dst == 0x6c28, "dst = %x, expected 0x6c28\n", dst);
349 ok(state == 0, "incorrect state value (%x)\n", state);
351 setlocale(LC_ALL, "C");
353 /* _mbccpy/_mbsncpy */
354 memset(buf, 0xff, sizeof(buf));
355 _mbccpy(buf, mbstring);
356 expect_bin(buf, "\xb0\xb1\xff", 3);
358 memset(buf, 0xff, sizeof(buf));
359 _mbsncpy(buf, mbstring, 1);
360 expect_bin(buf, "\xb0\xb1\xff", 3);
361 memset(buf, 0xff, sizeof(buf));
362 _mbsncpy(buf, mbstring, 2);
363 expect_bin(buf, "\xb0\xb1\xb2 \xff", 5);
364 memset(buf, 0xff, sizeof(buf));
365 _mbsncpy(buf, mbstring, 3);
366 expect_bin(buf, "\xb0\xb1\xb2 \xb3\xb4\xff", 7);
367 memset(buf, 0xff, sizeof(buf));
368 _mbsncpy(buf, mbstring, 4);
369 expect_bin(buf, "\xb0\xb1\xb2 \xb3\xb4 \xff", 8);
370 memset(buf, 0xff, sizeof(buf));
371 _mbsncpy(buf, mbstring, 5);
372 expect_bin(buf, "\xb0\xb1\xb2 \xb3\xb4 \0\0\xff", 10);
373 memset(buf, 0xff, sizeof(buf));
374 _mbsncpy(buf, mbsonlylead, 6);
375 expect_bin(buf, "\0\0\0\0\0\0\0\xff", 8);
377 memset(buf, 0xff, sizeof(buf));
378 _mbsnbcpy(buf, mbstring2, 2);
379 expect_bin(buf, "\xb0\xb1\xff", 3);
380 _mbsnbcpy(buf, mbstring2, 3);
381 expect_bin(buf, "\xb0\xb1\0\xff", 4);
382 _mbsnbcpy(buf, mbstring2, 4);
383 expect_bin(buf, "\xb0\xb1\xb2\xb3\xff", 5);
384 memset(buf, 0xff, sizeof(buf));
385 _mbsnbcpy(buf, mbsonlylead, 5);
386 expect_bin(buf, "\0\0\0\0\0\xff", 6);
388 /* _mbsinc/mbsdec */
389 step = _mbsinc(mbstring) - mbstring;
390 ok(step == 2, "_mbsinc adds %d (exp. 2)\n", step);
391 step = _mbsinc(&mbstring[2]) - &mbstring[2]; /* lead + invalid tail */
392 ok(step == 2, "_mbsinc adds %d (exp. 2)\n", step);
394 step = _mbsninc(mbsonlylead, 1) - mbsonlylead;
395 ok(step == 0, "_mbsninc adds %d (exp. 0)\n", step);
396 step = _mbsninc(mbsonlylead, 2) - mbsonlylead; /* lead + NUL byte + lead + char */
397 ok(step == 0, "_mbsninc adds %d (exp. 0)\n", step);
398 step = _mbsninc(mbstring2, 0) - mbstring2;
399 ok(step == 0, "_mbsninc adds %d (exp. 2)\n", step);
400 step = _mbsninc(mbstring2, 1) - mbstring2;
401 ok(step == 2, "_mbsninc adds %d (exp. 2)\n", step);
402 step = _mbsninc(mbstring2, 2) - mbstring2;
403 ok(step == 4, "_mbsninc adds %d (exp. 4)\n", step);
404 step = _mbsninc(mbstring2, 3) - mbstring2;
405 ok(step == 5, "_mbsninc adds %d (exp. 5)\n", step);
406 step = _mbsninc(mbstring2, 4) - mbstring2;
407 ok(step == 7, "_mbsninc adds %d (exp. 7)\n", step);
408 step = _mbsninc(mbstring2, 5) - mbstring2;
409 ok(step == 7, "_mbsninc adds %d (exp. 7)\n", step);
410 step = _mbsninc(mbstring2, 17) - mbstring2;
411 ok(step == 7, "_mbsninc adds %d (exp. 7)\n", step);
413 /* functions that depend on locale codepage, not mbcp.
414 * we hope the current locale to be SBCS because setlocale(LC_ALL, ".1252") seems not to work yet
415 * (as of Wine 0.9.43)
417 GetCPInfo(GetACP(), &cp_info);
418 if (cp_info.MaxCharSize == 1)
420 expect_eq(mblen((char *)mbstring, 3), 1, int, "%x");
421 expect_eq(_mbstrlen((char *)mbstring2), 7, int, "%d");
423 else
424 skip("Current locale has double-byte charset - could lead to false positives\n");
426 _setmbcp(1361);
427 expect_eq(_ismbblead(0x80), 0, int, "%d");
428 todo_wine {
429 expect_eq(_ismbblead(0x81), 1, int, "%d");
430 expect_eq(_ismbblead(0x83), 1, int, "%d");
432 expect_eq(_ismbblead(0x84), 1, int, "%d");
433 expect_eq(_ismbblead(0xd3), 1, int, "%d");
434 expect_eq(_ismbblead(0xd7), 0, int, "%d");
435 expect_eq(_ismbblead(0xd8), 1, int, "%d");
436 expect_eq(_ismbblead(0xd9), 1, int, "%d");
438 expect_eq(_ismbbtrail(0x30), 0, int, "%d");
439 expect_eq(_ismbbtrail(0x31), 1, int, "%d");
440 expect_eq(_ismbbtrail(0x7e), 1, int, "%d");
441 expect_eq(_ismbbtrail(0x7f), 0, int, "%d");
442 expect_eq(_ismbbtrail(0x80), 0, int, "%d");
443 expect_eq(_ismbbtrail(0x81), 1, int, "%d");
444 expect_eq(_ismbbtrail(0xfe), 1, int, "%d");
445 expect_eq(_ismbbtrail(0xff), 0, int, "%d");
447 _setmbcp(curr_mbcp);
450 static void test_mbsspn( void)
452 unsigned char str1[]="cabernet";
453 unsigned char str2[]="shiraz";
454 unsigned char set[]="abc";
455 unsigned char empty[]="";
456 int ret;
457 ret=_mbsspn( str1, set);
458 ok( ret==3, "_mbsspn returns %d should be 3\n", ret);
459 ret=_mbsspn( str2, set);
460 ok( ret==0, "_mbsspn returns %d should be 0\n", ret);
461 ret=_mbsspn( str1, empty);
462 ok( ret==0, "_mbsspn returns %d should be 0\n", ret);
465 static void test_mbsspnp( void)
467 unsigned char str1[]="cabernet";
468 unsigned char str2[]="shiraz";
469 unsigned char set[]="abc";
470 unsigned char empty[]="";
471 unsigned char full[]="abcenrt";
472 unsigned char* ret;
473 ret=_mbsspnp( str1, set);
474 ok( ret[0]=='e', "_mbsspnp returns %c should be e\n", ret[0]);
475 ret=_mbsspnp( str2, set);
476 ok( ret[0]=='s', "_mbsspnp returns %c should be s\n", ret[0]);
477 ret=_mbsspnp( str1, empty);
478 ok( ret[0]=='c', "_mbsspnp returns %c should be c\n", ret[0]);
479 ret=_mbsspnp( str1, full);
480 ok( ret==NULL, "_mbsspnp returns %p should be NULL\n", ret);
483 static void test_strdup(void)
485 char *str;
486 str = _strdup( 0 );
487 ok( str == 0, "strdup returns %s should be 0\n", str);
488 free( str );
491 static void test_strcpy_s(void)
493 char dest[8];
494 const char *small = "small";
495 const char *big = "atoolongstringforthislittledestination";
496 int ret;
498 if(!pstrcpy_s)
500 skip("strcpy_s not found\n");
501 return;
504 memset(dest, 'X', sizeof(dest));
505 ret = pstrcpy_s(dest, sizeof(dest), small);
506 ok(ret == 0, "Copying a string into a big enough destination returned %d, expected 0\n", ret);
507 ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == 'l' &&
508 dest[4] == 'l' && dest[5] == '\0'&& dest[6] == 'X' && dest[7] == 'X',
509 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
510 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
512 memset(dest, 'X', sizeof(dest));
513 ret = pstrcpy_s(dest, 0, big);
514 ok(ret == EINVAL, "Copying into a destination of size 0 returned %d, expected EINVAL\n", ret);
515 ok(dest[0] == 'X' && dest[1] == 'X' && dest[2] == 'X' && dest[3] == 'X' &&
516 dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
517 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
518 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
519 ret = pstrcpy_s(dest, 0, NULL);
520 ok(ret == EINVAL, "Copying into a destination of size 0 returned %d, expected EINVAL\n", ret);
521 ok(dest[0] == 'X' && dest[1] == 'X' && dest[2] == 'X' && dest[3] == 'X' &&
522 dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
523 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
524 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
526 memset(dest, 'X', sizeof(dest));
527 ret = pstrcpy_s(dest, sizeof(dest), big);
528 ok(ret == ERANGE, "Copying a big string in a small location returned %d, expected ERANGE\n", ret);
529 ok(dest[0] == '\0'&& dest[1] == 't' && dest[2] == 'o' && dest[3] == 'o' &&
530 dest[4] == 'l' && dest[5] == 'o' && dest[6] == 'n' && dest[7] == 'g',
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]);
534 memset(dest, 'X', sizeof(dest));
535 ret = pstrcpy_s(dest, sizeof(dest), NULL);
536 ok(ret == EINVAL, "Copying from a NULL source string returned %d, expected EINVAL\n", ret);
537 ok(dest[0] == '\0'&& dest[1] == 'X' && 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 = pstrcpy_s(NULL, sizeof(dest), small);
543 ok(ret == EINVAL, "Copying a big string a NULL dest returned %d, expected EINVAL\n", ret);
546 #define NUMELMS(array) (sizeof(array)/sizeof((array)[0]))
548 #define okchars(dst, b0, b1, b2, b3, b4, b5, b6, b7) \
549 ok(dst[0] == b0 && dst[1] == b1 && dst[2] == b2 && dst[3] == b3 && \
550 dst[4] == b4 && dst[5] == b5 && dst[6] == b6 && dst[7] == b7, \
551 "Bad result: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",\
552 dst[0], dst[1], dst[2], dst[3], dst[4], dst[5], dst[6], dst[7])
554 static void test_memcpy_s(void)
556 static char dest[8];
557 static const char tiny[] = {'T',0,'I','N','Y',0};
558 static const char big[] = {'a','t','o','o','l','o','n','g','s','t','r','i','n','g',0};
559 int ret;
560 if (!p_memcpy_s) {
561 skip("memcpy_s not found\n");
562 return;
565 if (p_set_invalid_parameter_handler)
566 ok(p_set_invalid_parameter_handler(test_invalid_parameter_handler) == NULL,
567 "Invalid parameter handler was already set\n");
569 /* Normal */
570 memset(dest, 'X', sizeof(dest));
571 ret = p_memcpy_s(dest, NUMELMS(dest), tiny, NUMELMS(tiny));
572 ok(ret == 0, "Copying a buffer into a big enough destination returned %d, expected 0\n", ret);
573 okchars(dest, tiny[0], tiny[1], tiny[2], tiny[3], tiny[4], tiny[5], 'X', 'X');
575 /* Vary source size */
576 errno = 0xdeadbeef;
577 memset(dest, 'X', sizeof(dest));
578 ret = p_memcpy_s(dest, NUMELMS(dest), big, NUMELMS(big));
579 ok(ret == ERANGE, "Copying a big buffer to a small destination returned %d, expected ERANGE\n", ret);
580 ok(errno == ERANGE, "errno is %d, expected ERANGE\n", errno);
581 okchars(dest, 0, 0, 0, 0, 0, 0, 0, 0);
583 /* Replace source with NULL */
584 errno = 0xdeadbeef;
585 memset(dest, 'X', sizeof(dest));
586 ret = p_memcpy_s(dest, NUMELMS(dest), NULL, NUMELMS(tiny));
587 ok(ret == EINVAL, "Copying a NULL source buffer returned %d, expected EINVAL\n", ret);
588 ok(errno == EINVAL, "errno is %d, expected EINVAL\n", errno);
589 okchars(dest, 0, 0, 0, 0, 0, 0, 0, 0);
591 /* Vary dest size */
592 errno = 0xdeadbeef;
593 memset(dest, 'X', sizeof(dest));
594 ret = p_memcpy_s(dest, 0, tiny, NUMELMS(tiny));
595 ok(ret == ERANGE, "Copying into a destination of size 0 returned %d, expected ERANGE\n", ret);
596 ok(errno == ERANGE, "errno is %d, expected ERANGE\n", errno);
597 okchars(dest, 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X');
599 /* Replace dest with NULL */
600 errno = 0xdeadbeef;
601 ret = p_memcpy_s(NULL, NUMELMS(dest), tiny, NUMELMS(tiny));
602 ok(ret == EINVAL, "Copying a tiny buffer to a big NULL destination returned %d, expected EINVAL\n", ret);
603 ok(errno == EINVAL, "errno is %d, expected EINVAL\n", errno);
605 /* Combinations */
606 errno = 0xdeadbeef;
607 memset(dest, 'X', sizeof(dest));
608 ret = p_memcpy_s(dest, 0, NULL, NUMELMS(tiny));
609 ok(ret == EINVAL, "Copying a NULL buffer into a destination of size 0 returned %d, expected EINVAL\n", ret);
610 ok(errno == EINVAL, "errno is %d, expected EINVAL\n", errno);
611 okchars(dest, 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X');
613 if (p_set_invalid_parameter_handler)
614 ok(p_set_invalid_parameter_handler(NULL) == test_invalid_parameter_handler,
615 "Cannot reset invalid parameter handler\n");
618 static void test_memmove_s(void)
620 static char dest[8];
621 static const char tiny[] = {'T',0,'I','N','Y',0};
622 static const char big[] = {'a','t','o','o','l','o','n','g','s','t','r','i','n','g',0};
623 int ret;
624 if (!p_memmove_s) {
625 skip("memmove_s not found\n");
626 return;
629 if (p_set_invalid_parameter_handler)
630 ok(p_set_invalid_parameter_handler(test_invalid_parameter_handler) == NULL,
631 "Invalid parameter handler was already set\n");
633 /* Normal */
634 memset(dest, 'X', sizeof(dest));
635 ret = p_memmove_s(dest, NUMELMS(dest), tiny, NUMELMS(tiny));
636 ok(ret == 0, "Moving a buffer into a big enough destination returned %d, expected 0\n", ret);
637 okchars(dest, tiny[0], tiny[1], tiny[2], tiny[3], tiny[4], tiny[5], 'X', 'X');
639 /* Overlapping */
640 memcpy(dest, big, sizeof(dest));
641 ret = p_memmove_s(dest+1, NUMELMS(dest)-1, dest, NUMELMS(dest)-1);
642 ok(ret == 0, "Moving a buffer up one char returned %d, expected 0\n", ret);
643 okchars(dest, big[0], big[0], big[1], big[2], big[3], big[4], big[5], big[6]);
645 /* Vary source size */
646 errno = 0xdeadbeef;
647 memset(dest, 'X', sizeof(dest));
648 ret = p_memmove_s(dest, NUMELMS(dest), big, NUMELMS(big));
649 ok(ret == ERANGE, "Moving a big buffer to a small destination returned %d, expected ERANGE\n", ret);
650 ok(errno == ERANGE, "errno is %d, expected ERANGE\n", errno);
651 okchars(dest, 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X');
653 /* Replace source with NULL */
654 errno = 0xdeadbeef;
655 memset(dest, 'X', sizeof(dest));
656 ret = p_memmove_s(dest, NUMELMS(dest), NULL, NUMELMS(tiny));
657 ok(ret == EINVAL, "Moving a NULL source buffer returned %d, expected EINVAL\n", ret);
658 ok(errno == EINVAL, "errno is %d, expected EINVAL\n", errno);
659 okchars(dest, 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X');
661 /* Vary dest size */
662 errno = 0xdeadbeef;
663 memset(dest, 'X', sizeof(dest));
664 ret = p_memmove_s(dest, 0, tiny, NUMELMS(tiny));
665 ok(ret == ERANGE, "Moving into a destination of size 0 returned %d, expected ERANGE\n", ret);
666 ok(errno == ERANGE, "errno is %d, expected ERANGE\n", errno);
667 okchars(dest, 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X');
669 /* Replace dest with NULL */
670 errno = 0xdeadbeef;
671 ret = p_memmove_s(NULL, NUMELMS(dest), tiny, NUMELMS(tiny));
672 ok(ret == EINVAL, "Moving a tiny buffer to a big NULL destination returned %d, expected EINVAL\n", ret);
673 ok(errno == EINVAL, "errno is %d, expected EINVAL\n", errno);
675 /* Combinations */
676 errno = 0xdeadbeef;
677 memset(dest, 'X', sizeof(dest));
678 ret = p_memmove_s(dest, 0, NULL, NUMELMS(tiny));
679 ok(ret == EINVAL, "Moving a NULL buffer into a destination of size 0 returned %d, expected EINVAL\n", ret);
680 ok(errno == EINVAL, "errno is %d, expected EINVAL\n", errno);
681 okchars(dest, 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X');
683 if (p_set_invalid_parameter_handler)
684 ok(p_set_invalid_parameter_handler(NULL) == test_invalid_parameter_handler,
685 "Cannot reset invalid parameter handler\n");
688 static void test_strcat_s(void)
690 char dest[8];
691 const char *small = "sma";
692 int ret;
694 if(!pstrcat_s)
696 skip("strcat_s not found\n");
697 return;
700 memset(dest, 'X', sizeof(dest));
701 dest[0] = '\0';
702 ret = pstrcat_s(dest, sizeof(dest), small);
703 ok(ret == 0, "strcat_s: Copying a string into a big enough destination returned %d, expected 0\n", ret);
704 ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == '\0'&&
705 dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
706 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
707 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
708 ret = pstrcat_s(dest, sizeof(dest), small);
709 ok(ret == 0, "strcat_s: Attaching a string to a big enough destination returned %d, expected 0\n", ret);
710 ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == 's' &&
711 dest[4] == 'm' && dest[5] == 'a' && dest[6] == '\0'&& dest[7] == 'X',
712 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
713 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
715 ret = pstrcat_s(dest, sizeof(dest), small);
716 ok(ret == ERANGE, "strcat_s: Attaching a string to a filled up destination returned %d, expected ERANGE\n", ret);
717 ok(dest[0] == '\0'&& dest[1] == 'm' && dest[2] == 'a' && dest[3] == 's' &&
718 dest[4] == 'm' && dest[5] == 'a' && dest[6] == 's' && dest[7] == 'm',
719 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
720 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
722 memset(dest, 'X', sizeof(dest));
723 dest[0] = 'a';
724 dest[1] = '\0';
726 ret = pstrcat_s(dest, 0, small);
727 ok(ret == EINVAL, "strcat_s: Source len = 0 returned %d, expected EINVAL\n", ret);
728 ok(dest[0] == 'a' && dest[1] == '\0'&& dest[2] == 'X' && dest[3] == 'X' &&
729 dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
730 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
731 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
733 ret = pstrcat_s(dest, 0, NULL);
734 ok(ret == EINVAL, "strcat_s: len = 0 and src = NULL returned %d, expected EINVAL\n", ret);
735 ok(dest[0] == 'a' && dest[1] == '\0'&& dest[2] == 'X' && dest[3] == 'X' &&
736 dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
737 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
738 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
740 ret = pstrcat_s(dest, sizeof(dest), NULL);
741 ok(ret == EINVAL, "strcat_s: Sourcing from NULL returned %d, expected EINVAL\n", ret);
742 ok(dest[0] == '\0'&& dest[1] == '\0'&& dest[2] == 'X' && dest[3] == 'X' &&
743 dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
744 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
745 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
747 ret = pstrcat_s(NULL, sizeof(dest), small);
748 ok(ret == EINVAL, "strcat_s: Writing to a NULL string returned %d, expected EINVAL\n", ret);
751 static void test__mbsnbcpy_s(void)
753 unsigned char dest[8];
754 const unsigned char big[] = "atoolongstringforthislittledestination";
755 const unsigned char small[] = "small";
756 int ret;
758 if(!p_mbsnbcpy_s)
760 skip("_mbsnbcpy_s not found\n");
761 return;
764 memset(dest, 'X', sizeof(dest));
765 ret = p_mbsnbcpy_s(dest, sizeof(dest), small, sizeof(small));
766 ok(ret == 0, "_mbsnbcpy_s: Copying a string into a big enough destination returned %d, expected 0\n", ret);
767 ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == 'l' &&
768 dest[4] == 'l' && dest[5] == '\0'&& dest[6] == 'X' && dest[7] == 'X',
769 "Unexpected return data from _mbsnbcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
770 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
772 /* WTF? */
773 memset(dest, 'X', sizeof(dest));
774 ret = p_mbsnbcpy_s(dest, sizeof(dest) - 2, big, sizeof(small));
775 ok(ret == ERANGE, "_mbsnbcpy_s: Copying a too long string returned %d, expected ERANGE\n", ret);
776 ok(dest[0] == '\0'&& dest[1] == 't' && dest[2] == 'o' && dest[3] == 'o' &&
777 dest[4] == 'l' && dest[5] == 'o' && dest[6] == 'X' && dest[7] == 'X',
778 "Unexpected return data from _mbsnbcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
779 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
781 memset(dest, 'X', sizeof(dest));
782 ret = p_mbsnbcpy_s(dest, sizeof(dest) - 2, big, 4);
783 ok(ret == 0, "_mbsnbcpy_s: Copying a too long string with a count cap returned %d, expected 0\n", ret);
784 ok(dest[0] == 'a' && dest[1] == 't' && dest[2] == 'o' && dest[3] == 'o' &&
785 dest[4] == '\0'&& dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
786 "Unexpected return data from _mbsnbcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
787 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
789 memset(dest, 'X', sizeof(dest));
790 ret = p_mbsnbcpy_s(dest, sizeof(dest) - 2, small, sizeof(small) + 10);
791 ok(ret == 0, "_mbsnbcpy_s: Copying more data than the source string len returned %d, expected 0\n", ret);
792 ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == 'l' &&
793 dest[4] == 'l' && dest[5] == '\0'&& dest[6] == 'X' && dest[7] == 'X',
794 "Unexpected return data from _mbsnbcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
795 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
798 static void test_wcscpy_s(void)
800 static const WCHAR szLongText[] = { 'T','h','i','s','A','L','o','n','g','s','t','r','i','n','g',0 };
801 static WCHAR szDest[18];
802 static WCHAR szDestShort[8];
803 int ret;
805 if(!p_wcscpy_s)
807 win_skip("wcscpy_s not found\n");
808 return;
811 if (p_set_invalid_parameter_handler)
812 ok(p_set_invalid_parameter_handler(test_invalid_parameter_handler) == NULL,
813 "Invalid parameter handler was already set\n");
815 /* Test NULL Dest */
816 errno = EBADF;
817 ret = p_wcscpy_s(NULL, 18, szLongText);
818 ok(ret == EINVAL, "p_wcscpy_s expect EINVAL got %d\n", ret);
819 ok(errno == EINVAL, "expected errno EINVAL got %d\n", errno);
821 /* Test NULL Source */
822 errno = EBADF;
823 szDest[0] = 'A';
824 ret = p_wcscpy_s(szDest, 18, NULL);
825 ok(ret == EINVAL, "expected EINVAL got %d\n", ret);
826 ok(errno == EINVAL, "expected errno EINVAL got %d\n", errno);
827 ok(szDest[0] == 0, "szDest[0] not 0, got %c\n", szDest[0]);
829 /* Test invalid size */
830 errno = EBADF;
831 szDest[0] = 'A';
832 ret = p_wcscpy_s(szDest, 0, szLongText);
833 /* Later versions changed the return value for this case to EINVAL,
834 * and don't modify the result if the dest size is 0.
836 ok(ret == ERANGE || ret == EINVAL, "expected ERANGE/EINVAL got %d\n", ret);
837 ok(errno == ERANGE || errno == EINVAL, "expected errno ERANGE/EINVAL got %d\n", errno);
838 ok(szDest[0] == 0 || ret == EINVAL, "szDest[0] not 0\n");
840 /* Copy same buffer size */
841 ret = p_wcscpy_s(szDest, 18, szLongText);
842 ok(ret == 0, "expected 0 got %d\n", ret);
843 ok(lstrcmpW(szDest, szLongText) == 0, "szDest != szLongText\n");
845 /* Copy smaller buffer size */
846 errno = EBADF;
847 szDest[0] = 'A';
848 ret = p_wcscpy_s(szDestShort, 8, szLongText);
849 ok(ret == ERANGE || ret == EINVAL, "expected ERANGE/EINVAL got %d\n", ret);
850 ok(errno == ERANGE || errno == EINVAL, "expected errno ERANGE/EINVAL got %d\n", errno);
851 ok(szDestShort[0] == 0, "szDestShort[0] not 0\n");
853 if(!p_wcsncpy_s)
855 win_skip("wcsncpy_s not found\n");
857 if (p_set_invalid_parameter_handler)
858 ok(p_set_invalid_parameter_handler(NULL) == test_invalid_parameter_handler,
859 "Cannot reset invalid parameter handler\n");
860 return;
863 ret = p_wcsncpy_s(NULL, 18, szLongText, sizeof(szLongText)/sizeof(WCHAR));
864 ok(ret == EINVAL, "p_wcsncpy_s expect EINVAL got %d\n", ret);
866 szDest[0] = 'A';
867 ret = p_wcsncpy_s(szDest, 18, NULL, 1);
868 ok(ret == EINVAL, "expected EINVAL got %d\n", ret);
869 ok(szDest[0] == 0, "szDest[0] not 0\n");
871 szDest[0] = 'A';
872 ret = p_wcsncpy_s(szDest, 18, NULL, 0);
873 ok(ret == 0, "expected ERROR_SUCCESS got %d\n", ret);
874 ok(szDest[0] == 0, "szDest[0] not 0\n");
876 szDest[0] = 'A';
877 ret = p_wcsncpy_s(szDest, 0, szLongText, sizeof(szLongText)/sizeof(WCHAR));
878 ok(ret == ERANGE || ret == EINVAL, "expected ERANGE/EINVAL got %d\n", ret);
879 ok(szDest[0] == 0 || ret == EINVAL, "szDest[0] not 0\n");
881 ret = p_wcsncpy_s(szDest, 18, szLongText, sizeof(szLongText)/sizeof(WCHAR));
882 ok(ret == 0, "expected 0 got %d\n", ret);
883 ok(lstrcmpW(szDest, szLongText) == 0, "szDest != szLongText\n");
885 szDest[0] = 'A';
886 ret = p_wcsncpy_s(szDestShort, 8, szLongText, sizeof(szLongText)/sizeof(WCHAR));
887 ok(ret == ERANGE || ret == EINVAL, "expected ERANGE/EINVAL got %d\n", ret);
888 ok(szDestShort[0] == 0, "szDestShort[0] not 0\n");
890 szDest[0] = 'A';
891 ret = p_wcsncpy_s(szDest, 5, szLongText, -1);
892 ok(ret == STRUNCATE, "expected STRUNCATE got %d\n", ret);
893 ok(szDest[4] == 0, "szDest[4] not 0\n");
894 ok(!memcmp(szDest, szLongText, 4*sizeof(WCHAR)), "szDest = %s\n", wine_dbgstr_w(szDest));
896 ret = p_wcsncpy_s(NULL, 0, (void*)0xdeadbeef, 0);
897 ok(ret == 0, "ret = %d\n", ret);
899 szDestShort[0] = '1';
900 szDestShort[1] = 0;
901 ret = p_wcsncpy_s(szDestShort+1, 4, szDestShort, -1);
902 ok(ret == STRUNCATE, "expected ERROR_SUCCESS got %d\n", ret);
903 ok(szDestShort[0]=='1' && szDestShort[1]=='1' && szDestShort[2]=='1' && szDestShort[3]=='1',
904 "szDestShort = %s\n", wine_dbgstr_w(szDestShort));
906 if (p_set_invalid_parameter_handler)
907 ok(p_set_invalid_parameter_handler(NULL) == test_invalid_parameter_handler,
908 "Cannot reset invalid parameter handler\n");
911 static void test__wcsupr_s(void)
913 static const WCHAR mixedString[] = {'M', 'i', 'X', 'e', 'D', 'l', 'o', 'w',
914 'e', 'r', 'U', 'P', 'P', 'E', 'R', 0};
915 static const WCHAR expectedString[] = {'M', 'I', 'X', 'E', 'D', 'L', 'O',
916 'W', 'E', 'R', 'U', 'P', 'P', 'E',
917 'R', 0};
918 WCHAR testBuffer[2*sizeof(mixedString)/sizeof(WCHAR)];
919 int ret;
921 if (!p_wcsupr_s)
923 win_skip("_wcsupr_s not found\n");
924 return;
927 /* Test NULL input string and invalid size. */
928 errno = EBADF;
929 ret = p_wcsupr_s(NULL, 0);
930 ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
931 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
933 /* Test NULL input string and valid size. */
934 errno = EBADF;
935 ret = p_wcsupr_s(NULL, sizeof(testBuffer)/sizeof(WCHAR));
936 ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
937 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
939 /* Test empty string with zero size. */
940 errno = EBADF;
941 testBuffer[0] = '\0';
942 ret = p_wcsupr_s(testBuffer, 0);
943 ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
944 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
945 ok(testBuffer[0] == '\0', "Expected the buffer to be unchanged\n");
947 /* Test empty string with size of one. */
948 testBuffer[0] = '\0';
949 ret = p_wcsupr_s(testBuffer, 1);
950 ok(ret == 0, "Expected _wcsupr_s to succeed, got %d\n", ret);
951 ok(testBuffer[0] == '\0', "Expected the buffer to be unchanged\n");
953 /* Test one-byte buffer with zero size. */
954 errno = EBADF;
955 testBuffer[0] = 'x';
956 ret = p_wcsupr_s(testBuffer, 0);
957 ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
958 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
959 ok(testBuffer[0] == '\0', "Expected the first buffer character to be null\n");
961 /* Test one-byte buffer with size of one. */
962 errno = EBADF;
963 testBuffer[0] = 'x';
964 ret = p_wcsupr_s(testBuffer, 1);
965 ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
966 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
967 ok(testBuffer[0] == '\0', "Expected the first buffer character to be null\n");
969 /* Test invalid size. */
970 wcscpy(testBuffer, mixedString);
971 errno = EBADF;
972 ret = p_wcsupr_s(testBuffer, 0);
973 ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
974 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
975 ok(testBuffer[0] == '\0', "Expected the first buffer character to be null\n");
977 /* Test normal string uppercasing. */
978 wcscpy(testBuffer, mixedString);
979 ret = p_wcsupr_s(testBuffer, sizeof(mixedString)/sizeof(WCHAR));
980 ok(ret == 0, "Expected _wcsupr_s to succeed, got %d\n", ret);
981 ok(!wcscmp(testBuffer, expectedString), "Expected the string to be fully upper-case\n");
983 /* Test uppercasing with a shorter buffer size count. */
984 wcscpy(testBuffer, mixedString);
985 errno = EBADF;
986 ret = p_wcsupr_s(testBuffer, sizeof(mixedString)/sizeof(WCHAR) - 1);
987 ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
988 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
989 ok(testBuffer[0] == '\0', "Expected the first buffer character to be null\n");
991 /* Test uppercasing with a longer buffer size count. */
992 wcscpy(testBuffer, mixedString);
993 ret = p_wcsupr_s(testBuffer, sizeof(testBuffer)/sizeof(WCHAR));
994 ok(ret == 0, "Expected _wcsupr_s to succeed, got %d\n", ret);
995 ok(!wcscmp(testBuffer, expectedString), "Expected the string to be fully upper-case\n");
998 static void test__wcslwr_s(void)
1000 static const WCHAR mixedString[] = {'M', 'i', 'X', 'e', 'D', 'l', 'o', 'w',
1001 'e', 'r', 'U', 'P', 'P', 'E', 'R', 0};
1002 static const WCHAR expectedString[] = {'m', 'i', 'x', 'e', 'd', 'l', 'o',
1003 'w', 'e', 'r', 'u', 'p', 'p', 'e',
1004 'r', 0};
1005 WCHAR buffer[2*sizeof(mixedString)/sizeof(WCHAR)];
1006 int ret;
1008 if (!p_wcslwr_s)
1010 win_skip("_wcslwr_s not found\n");
1011 return;
1014 /* Test NULL input string and invalid size. */
1015 errno = EBADF;
1016 ret = p_wcslwr_s(NULL, 0);
1017 ok(ret == EINVAL, "expected EINVAL, got %d\n", ret);
1018 ok(errno == EINVAL, "expected errno EINVAL, got %d\n", errno);
1020 /* Test NULL input string and valid size. */
1021 errno = EBADF;
1022 ret = p_wcslwr_s(NULL, sizeof(buffer)/sizeof(wchar_t));
1023 ok(ret == EINVAL, "expected EINVAL, got %d\n", ret);
1024 ok(errno == EINVAL, "expected errno EINVAL, got %d\n", errno);
1026 /* Test empty string with zero size. */
1027 errno = EBADF;
1028 buffer[0] = 'a';
1029 ret = p_wcslwr_s(buffer, 0);
1030 ok(ret == EINVAL, "expected EINVAL, got %d\n", ret);
1031 ok(errno == EINVAL, "expected errno EINVAL, got %d\n", errno);
1032 ok(buffer[0] == 0, "expected empty string\n");
1034 /* Test empty string with size of one. */
1035 buffer[0] = 0;
1036 ret = p_wcslwr_s(buffer, 1);
1037 ok(ret == 0, "got %d\n", ret);
1038 ok(buffer[0] == 0, "expected buffer to be unchanged\n");
1040 /* Test one-byte buffer with zero size. */
1041 errno = EBADF;
1042 buffer[0] = 'x';
1043 ret = p_wcslwr_s(buffer, 0);
1044 ok(ret == EINVAL, "expected EINVAL, got %d\n", ret);
1045 ok(errno == EINVAL, "expected errno to be EINVAL, got %d\n", errno);
1046 ok(buffer[0] == '\0', "expected empty string\n");
1048 /* Test one-byte buffer with size of one. */
1049 errno = EBADF;
1050 buffer[0] = 'x';
1051 ret = p_wcslwr_s(buffer, 1);
1052 ok(ret == EINVAL, "expected EINVAL, got %d\n", ret);
1053 ok(errno == EINVAL, "expected errno to be EINVAL, got %d\n", errno);
1054 ok(buffer[0] == '\0', "expected empty string\n");
1056 /* Test invalid size. */
1057 wcscpy(buffer, mixedString);
1058 errno = EBADF;
1059 ret = p_wcslwr_s(buffer, 0);
1060 ok(ret == EINVAL, "Expected EINVAL, got %d\n", ret);
1061 ok(errno == EINVAL, "expected errno to be EINVAL, got %d\n", errno);
1062 ok(buffer[0] == '\0', "expected empty string\n");
1064 /* Test normal string uppercasing. */
1065 wcscpy(buffer, mixedString);
1066 ret = p_wcslwr_s(buffer, sizeof(mixedString)/sizeof(WCHAR));
1067 ok(ret == 0, "expected 0, got %d\n", ret);
1068 ok(!wcscmp(buffer, expectedString), "expected lowercase\n");
1070 /* Test uppercasing with a shorter buffer size count. */
1071 wcscpy(buffer, mixedString);
1072 errno = EBADF;
1073 ret = p_wcslwr_s(buffer, sizeof(mixedString)/sizeof(WCHAR) - 1);
1074 ok(ret == EINVAL, "expected EINVAL, got %d\n", ret);
1075 ok(errno == EINVAL, "expected errno to be EINVAL, got %d\n", errno);
1076 ok(buffer[0] == '\0', "expected empty string\n");
1078 /* Test uppercasing with a longer buffer size count. */
1079 wcscpy(buffer, mixedString);
1080 ret = p_wcslwr_s(buffer, sizeof(buffer)/sizeof(WCHAR));
1081 ok(ret == 0, "expected 0, got %d\n", ret);
1082 ok(!wcscmp(buffer, expectedString), "expected lowercase\n");
1085 static void test_mbcjisjms(void)
1087 /* List of value-pairs to test. The test assumes the last pair to be {0, ..} */
1088 unsigned int jisjms[][2] = { {0x2020, 0}, {0x2021, 0}, {0x2120, 0}, {0x2121, 0x8140},
1089 {0x7f7f, 0}, {0x7f7e, 0}, {0x7e7f, 0}, {0x7e7e, 0xeffc},
1090 {0x255f, 0x837e}, {0x2560, 0x8380}, {0x2561, 0x8381},
1091 {0x2121FFFF, 0}, {0x2223, 0x81a1}, {0x237e, 0x829e}, {0, 0}};
1092 int cp[] = { 932, 936, 939, 950, 1361, _MB_CP_SBCS };
1093 unsigned int i, j;
1094 int prev_cp = _getmbcp();
1096 for (i = 0; i < sizeof(cp)/sizeof(cp[0]); i++)
1098 _setmbcp(cp[i]);
1099 for (j = 0; jisjms[j][0] != 0; j++)
1101 unsigned int ret, exp;
1102 ret = _mbcjistojms(jisjms[j][0]);
1103 exp = (cp[i] == 932) ? jisjms[j][1] : jisjms[j][0];
1104 ok(ret == exp, "Expected 0x%x, got 0x%x (0x%x, codepage=%d)\n",
1105 exp, ret, jisjms[j][0], cp[i]);
1108 _setmbcp(prev_cp);
1111 static void test_mbcjmsjis(void)
1113 /* List of value-pairs to test. The test assumes the last pair to be {0, ..} */
1114 unsigned int jmsjis[][2] = { {0x80fc, 0}, {0x813f, 0}, {0x8140, 0x2121},
1115 {0x817e, 0x215f}, {0x817f, 0}, {0x8180, 0x2160},
1116 {0x819e, 0x217e}, {0x819f, 0x2221}, {0x81fc, 0x227e},
1117 {0x81fd, 0}, {0x9ffc, 0x5e7e}, {0x9ffd, 0},
1118 {0xa040, 0}, {0xdffc, 0}, {0xe040, 0x5f21},
1119 {0xeffc, 0x7e7e}, {0xf040, 0}, {0x21, 0}, {0, 0}};
1120 int cp[] = { 932, 936, 939, 950, 1361, _MB_CP_SBCS };
1121 unsigned int i, j;
1122 int prev_cp = _getmbcp();
1124 for (i = 0; i < sizeof(cp)/sizeof(cp[0]); i++)
1126 _setmbcp(cp[i]);
1127 for (j = 0; jmsjis[j][0] != 0; j++)
1129 unsigned int ret, exp;
1130 ret = _mbcjmstojis(jmsjis[j][0]);
1131 exp = (cp[i] == 932) ? jmsjis[j][1] : jmsjis[j][0];
1132 ok(ret == exp, "Expected 0x%x, got 0x%x (0x%x, codepage=%d)\n",
1133 exp, ret, jmsjis[j][0], cp[i]);
1136 _setmbcp(prev_cp);
1139 static void test_mbbtombc(void)
1141 static const unsigned int mbbmbc[][2] = {
1142 {0x1f, 0x1f}, {0x20, 0x8140}, {0x39, 0x8258}, {0x40, 0x8197},
1143 {0x41, 0x8260}, {0x5e, 0x814f}, {0x7e, 0x8150}, {0x7f, 0x7f},
1144 {0x80, 0x80}, {0x81, 0x81}, {0xa0, 0xa0}, {0xa7, 0x8340},
1145 {0xb0, 0x815b}, {0xd1, 0x8380}, {0xff, 0xff}, {0,0}};
1146 int cp[] = { 932, 936, 939, 950, 1361, _MB_CP_SBCS };
1147 int i, j;
1148 int prev_cp = _getmbcp();
1150 for (i = 0; i < sizeof(cp)/sizeof(cp[0]); i++)
1152 _setmbcp(cp[i]);
1153 for (j = 0; mbbmbc[j][0] != 0; j++)
1155 unsigned int exp, ret;
1156 ret = _mbbtombc(mbbmbc[j][0]);
1157 exp = (cp[i] == 932) ? mbbmbc[j][1] : mbbmbc[j][0];
1158 ok(ret == exp, "Expected 0x%x, got 0x%x (0x%x, codepage %d)\n",
1159 exp, ret, mbbmbc[j][0], cp[i]);
1162 _setmbcp(prev_cp);
1165 static void test_mbctombb(void)
1167 static const unsigned int mbcmbb_932[][2] = {
1168 {0x829e, 0x829e}, {0x829f, 0xa7}, {0x82f1, 0xdd}, {0x82f2, 0x82f2},
1169 {0x833f, 0x833f}, {0x8340, 0xa7}, {0x837e, 0xd0}, {0x837f, 0x837f},
1170 {0x8380, 0xd1}, {0x8396, 0xb9}, {0x8397, 0x8397}, {0x813f, 0x813f},
1171 {0x8140, 0x20}, {0x814c, 0x814c}, {0x814f, 0x5e}, {0x8197, 0x40},
1172 {0x8198, 0x8198}, {0x8258, 0x39}, {0x8259, 0x8259}, {0x825f, 0x825f},
1173 {0x8260, 0x41}, {0x82f1, 0xdd}, {0x82f2, 0x82f2}, {0,0}};
1174 unsigned int exp, ret, i;
1175 unsigned int prev_cp = _getmbcp();
1177 _setmbcp(932);
1178 for (i = 0; mbcmbb_932[i][0] != 0; i++)
1180 ret = _mbctombb(mbcmbb_932[i][0]);
1181 exp = mbcmbb_932[i][1];
1182 ok(ret == exp, "Expected 0x%x, got 0x%x\n", exp, ret);
1184 _setmbcp(prev_cp);
1187 static void test_ismbclegal(void) {
1188 unsigned int prev_cp = _getmbcp();
1189 int ret, exp, err;
1190 unsigned int i;
1192 _setmbcp(932); /* Japanese */
1193 err = 0;
1194 for(i = 0; i < 0x10000; i++) {
1195 ret = _ismbclegal(i);
1196 exp = ((HIBYTE(i) >= 0x81 && HIBYTE(i) <= 0x9F) ||
1197 (HIBYTE(i) >= 0xE0 && HIBYTE(i) <= 0xFC)) &&
1198 ((LOBYTE(i) >= 0x40 && LOBYTE(i) <= 0x7E) ||
1199 (LOBYTE(i) >= 0x80 && LOBYTE(i) <= 0xFC));
1200 if(ret != exp) {
1201 err = 1;
1202 break;
1205 ok(!err, "_ismbclegal (932) : Expected 0x%x, got 0x%x (0x%x)\n", exp, ret, i);
1206 _setmbcp(936); /* Chinese (GBK) */
1207 err = 0;
1208 for(i = 0; i < 0x10000; i++) {
1209 ret = _ismbclegal(i);
1210 exp = HIBYTE(i) >= 0x81 && HIBYTE(i) <= 0xFE &&
1211 LOBYTE(i) >= 0x40 && LOBYTE(i) <= 0xFE;
1212 if(ret != exp) {
1213 err = 1;
1214 break;
1217 ok(!err, "_ismbclegal (936) : Expected 0x%x, got 0x%x (0x%x)\n", exp, ret, i);
1218 _setmbcp(949); /* Korean */
1219 err = 0;
1220 for(i = 0; i < 0x10000; i++) {
1221 ret = _ismbclegal(i);
1222 exp = HIBYTE(i) >= 0x81 && HIBYTE(i) <= 0xFE &&
1223 LOBYTE(i) >= 0x41 && LOBYTE(i) <= 0xFE;
1224 if(ret != exp) {
1225 err = 1;
1226 break;
1229 ok(!err, "_ismbclegal (949) : Expected 0x%x, got 0x%x (0x%x)\n", exp, ret, i);
1230 _setmbcp(950); /* Chinese (Big5) */
1231 err = 0;
1232 for(i = 0; i < 0x10000; i++) {
1233 ret = _ismbclegal(i);
1234 exp = HIBYTE(i) >= 0x81 && HIBYTE(i) <= 0xFE &&
1235 ((LOBYTE(i) >= 0x40 && LOBYTE(i) <= 0x7E) ||
1236 (LOBYTE(i) >= 0xA1 && LOBYTE(i) <= 0xFE));
1237 if(ret != exp) {
1238 err = 1;
1239 break;
1242 ok(!err, "_ismbclegal (950) : Expected 0x%x, got 0x%x (0x%x)\n", exp, ret, i);
1243 _setmbcp(1361); /* Korean (Johab) */
1244 err = 0;
1245 for(i = 0; i < 0x10000; i++) {
1246 ret = _ismbclegal(i);
1247 exp = ((HIBYTE(i) >= 0x81 && HIBYTE(i) <= 0xD3) ||
1248 (HIBYTE(i) >= 0xD8 && HIBYTE(i) <= 0xF9)) &&
1249 ((LOBYTE(i) >= 0x31 && LOBYTE(i) <= 0x7E) ||
1250 (LOBYTE(i) >= 0x81 && LOBYTE(i) <= 0xFE)) &&
1251 HIBYTE(i) != 0xDF;
1252 if(ret != exp) {
1253 err = 1;
1254 break;
1257 todo_wine ok(!err, "_ismbclegal (1361) : Expected 0x%x, got 0x%x (0x%x)\n", exp, ret, i);
1259 _setmbcp(prev_cp);
1262 static const struct {
1263 const char* string;
1264 const char* delimiter;
1265 int exp_offsetret1; /* returned offset from string after first call to strtok()
1266 -1 means NULL */
1267 int exp_offsetret2; /* returned offset from string after second call to strtok()
1268 -1 means NULL */
1269 int exp_offsetret3; /* returned offset from string after third call to strtok()
1270 -1 means NULL */
1271 } testcases_strtok[] = {
1272 { "red cabernet", " ", 0, 4, -1 },
1273 { "sparkling white riesling", " ", 0, 10, 16 },
1274 { " pale cream sherry", "e ", 1, 6, 9 },
1275 /* end mark */
1276 { 0}
1279 static void test_strtok(void)
1281 int i;
1282 char *strret;
1283 char teststr[100];
1284 for( i = 0; testcases_strtok[i].string; i++){
1285 strcpy( teststr, testcases_strtok[i].string);
1286 strret = strtok( teststr, testcases_strtok[i].delimiter);
1287 ok( (int)(strret - teststr) == testcases_strtok[i].exp_offsetret1 ||
1288 (!strret && testcases_strtok[i].exp_offsetret1 == -1),
1289 "string (%p) \'%s\' return %p\n",
1290 teststr, testcases_strtok[i].string, strret);
1291 if( !strret) continue;
1292 strret = strtok( NULL, testcases_strtok[i].delimiter);
1293 ok( (int)(strret - teststr) == testcases_strtok[i].exp_offsetret2 ||
1294 (!strret && testcases_strtok[i].exp_offsetret2 == -1),
1295 "second call string (%p) \'%s\' return %p\n",
1296 teststr, testcases_strtok[i].string, strret);
1297 if( !strret) continue;
1298 strret = strtok( NULL, testcases_strtok[i].delimiter);
1299 ok( (int)(strret - teststr) == testcases_strtok[i].exp_offsetret3 ||
1300 (!strret && testcases_strtok[i].exp_offsetret3 == -1),
1301 "third call string (%p) \'%s\' return %p\n",
1302 teststr, testcases_strtok[i].string, strret);
1306 static void test_strtol(void)
1308 char* e;
1309 LONG l;
1310 ULONG ul;
1312 /* errno is only set in case of error, so reset errno to EBADF to check for errno modification */
1313 /* errno is modified on W2K8+ */
1314 errno = EBADF;
1315 l = strtol("-1234", &e, 0);
1316 ok(l==-1234, "wrong value %d\n", l);
1317 ok(errno == EBADF || broken(errno == 0), "wrong errno %d\n", errno);
1318 errno = EBADF;
1319 ul = strtoul("1234", &e, 0);
1320 ok(ul==1234, "wrong value %u\n", ul);
1321 ok(errno == EBADF || broken(errno == 0), "wrong errno %d\n", errno);
1323 errno = EBADF;
1324 l = strtol("2147483647L", &e, 0);
1325 ok(l==2147483647, "wrong value %d\n", l);
1326 ok(errno == EBADF || broken(errno == 0), "wrong errno %d\n", errno);
1327 errno = EBADF;
1328 l = strtol("-2147483648L", &e, 0);
1329 ok(l==-2147483647L - 1, "wrong value %d\n", l);
1330 ok(errno == EBADF || broken(errno == 0), "wrong errno %d\n", errno);
1331 errno = EBADF;
1332 ul = strtoul("4294967295UL", &e, 0);
1333 ok(ul==4294967295ul, "wrong value %u\n", ul);
1334 ok(errno == EBADF || broken(errno == 0), "wrong errno %d\n", errno);
1336 errno = 0;
1337 l = strtol("9223372036854775807L", &e, 0);
1338 ok(l==2147483647, "wrong value %d\n", l);
1339 ok(errno == ERANGE, "wrong errno %d\n", errno);
1340 errno = 0;
1341 ul = strtoul("9223372036854775807L", &e, 0);
1342 ok(ul==4294967295ul, "wrong value %u\n", ul);
1343 ok(errno == ERANGE, "wrong errno %d\n", errno);
1345 errno = 0;
1346 ul = strtoul("-2", NULL, 0);
1347 ok(ul == -2, "wrong value %u\n", ul);
1348 ok(errno == 0, "wrong errno %d\n", errno);
1350 errno = 0;
1351 ul = strtoul("-4294967294", NULL, 0);
1352 ok(ul == 2, "wrong value %u\n", ul);
1353 ok(errno == 0, "wrong errno %d\n", errno);
1355 errno = 0;
1356 ul = strtoul("-4294967295", NULL, 0);
1357 ok(ul==1, "wrong value %u\n", ul);
1358 ok(errno == 0, "wrong errno %d\n", errno);
1360 errno = 0;
1361 ul = strtoul("-4294967296", NULL, 0);
1362 ok(ul == 1, "wrong value %u\n", ul);
1363 ok(errno == ERANGE, "wrong errno %d\n", errno);
1366 static void test_strnlen(void)
1368 static const char str[] = "string";
1369 size_t res;
1371 if(!p_strnlen) {
1372 win_skip("strnlen not found\n");
1373 return;
1376 res = p_strnlen(str, 20);
1377 ok(res == 6, "Returned length = %d\n", (int)res);
1379 res = p_strnlen(str, 3);
1380 ok(res == 3, "Returned length = %d\n", (int)res);
1382 res = p_strnlen(NULL, 0);
1383 ok(res == 0, "Returned length = %d\n", (int)res);
1386 static void test__strtoi64(void)
1388 static const char no1[] = "31923";
1389 static const char no2[] = "-213312";
1390 static const char no3[] = "12aa";
1391 static const char no4[] = "abc12";
1392 static const char overflow[] = "99999999999999999999";
1393 static const char neg_overflow[] = "-99999999999999999999";
1394 static const char hex[] = "0x123";
1395 static const char oct[] = "000123";
1396 static const char blanks[] = " 12 212.31";
1398 __int64 res;
1399 unsigned __int64 ures;
1400 char *endpos;
1402 if(!p_strtoi64 || !p_strtoui64) {
1403 win_skip("_strtoi64 or _strtoui64 not found\n");
1404 return;
1407 errno = 0xdeadbeef;
1408 res = p_strtoi64(no1, NULL, 10);
1409 ok(res == 31923, "res != 31923\n");
1410 res = p_strtoi64(no2, NULL, 10);
1411 ok(res == -213312, "res != -213312\n");
1412 res = p_strtoi64(no3, NULL, 10);
1413 ok(res == 12, "res != 12\n");
1414 res = p_strtoi64(no4, &endpos, 10);
1415 ok(res == 0, "res != 0\n");
1416 ok(endpos == no4, "Scanning was not stopped on first character\n");
1417 res = p_strtoi64(hex, &endpos, 10);
1418 ok(res == 0, "res != 0\n");
1419 ok(endpos == hex+1, "Incorrect endpos (%p-%p)\n", hex, endpos);
1420 res = p_strtoi64(oct, &endpos, 10);
1421 ok(res == 123, "res != 123\n");
1422 ok(endpos == oct+strlen(oct), "Incorrect endpos (%p-%p)\n", oct, endpos);
1423 res = p_strtoi64(blanks, &endpos, 10);
1424 ok(res == 12, "res != 12\n");
1425 ok(endpos == blanks+10, "Incorrect endpos (%p-%p)\n", blanks, endpos);
1426 ok(errno == 0xdeadbeef, "errno = %x\n", errno);
1428 errno = 0xdeadbeef;
1429 res = p_strtoi64(overflow, &endpos, 10);
1430 ok(res == _I64_MAX, "res != _I64_MAX\n");
1431 ok(endpos == overflow+strlen(overflow), "Incorrect endpos (%p-%p)\n", overflow, endpos);
1432 ok(errno == ERANGE, "errno = %x\n", errno);
1434 errno = 0xdeadbeef;
1435 res = p_strtoi64(neg_overflow, &endpos, 10);
1436 ok(res == _I64_MIN, "res != _I64_MIN\n");
1437 ok(endpos == neg_overflow+strlen(neg_overflow), "Incorrect endpos (%p-%p)\n", neg_overflow, endpos);
1438 ok(errno == ERANGE, "errno = %x\n", errno);
1440 errno = 0xdeadbeef;
1441 res = p_strtoi64(no1, &endpos, 16);
1442 ok(res == 203043, "res != 203043\n");
1443 ok(endpos == no1+strlen(no1), "Incorrect endpos (%p-%p)\n", no1, endpos);
1444 res = p_strtoi64(no2, &endpos, 16);
1445 ok(res == -2175762, "res != -2175762\n");
1446 ok(endpos == no2+strlen(no2), "Incorrect endpos (%p-%p)\n", no2, endpos);
1447 res = p_strtoi64(no3, &endpos, 16);
1448 ok(res == 4778, "res != 4778\n");
1449 ok(endpos == no3+strlen(no3), "Incorrect endpos (%p-%p)\n", no3, endpos);
1450 res = p_strtoi64(no4, &endpos, 16);
1451 ok(res == 703506, "res != 703506\n");
1452 ok(endpos == no4+strlen(no4), "Incorrect endpos (%p-%p)\n", no4, endpos);
1453 res = p_strtoi64(hex, &endpos, 16);
1454 ok(res == 291, "res != 291\n");
1455 ok(endpos == hex+strlen(hex), "Incorrect endpos (%p-%p)\n", hex, endpos);
1456 res = p_strtoi64(oct, &endpos, 16);
1457 ok(res == 291, "res != 291\n");
1458 ok(endpos == oct+strlen(oct), "Incorrect endpos (%p-%p)\n", oct, endpos);
1459 res = p_strtoi64(blanks, &endpos, 16);
1460 ok(res == 18, "res != 18\n");
1461 ok(endpos == blanks+10, "Incorrect endpos (%p-%p)\n", blanks, endpos);
1462 ok(errno == 0xdeadbeef, "errno = %x\n", errno);
1464 errno = 0xdeadbeef;
1465 res = p_strtoi64(hex, &endpos, 36);
1466 ok(res == 1541019, "res != 1541019\n");
1467 ok(endpos == hex+strlen(hex), "Incorrect endpos (%p-%p)\n", hex, endpos);
1468 ok(errno == 0xdeadbeef, "errno = %x\n", errno);
1470 errno = 0xdeadbeef;
1471 res = p_strtoi64(no1, &endpos, 0);
1472 ok(res == 31923, "res != 31923\n");
1473 ok(endpos == no1+strlen(no1), "Incorrect endpos (%p-%p)\n", no1, endpos);
1474 res = p_strtoi64(no2, &endpos, 0);
1475 ok(res == -213312, "res != -213312\n");
1476 ok(endpos == no2+strlen(no2), "Incorrect endpos (%p-%p)\n", no2, endpos);
1477 res = p_strtoi64(no3, &endpos, 10);
1478 ok(res == 12, "res != 12\n");
1479 ok(endpos == no3+2, "Incorrect endpos (%p-%p)\n", no3, endpos);
1480 res = p_strtoi64(no4, &endpos, 10);
1481 ok(res == 0, "res != 0\n");
1482 ok(endpos == no4, "Incorrect endpos (%p-%p)\n", no4, endpos);
1483 res = p_strtoi64(hex, &endpos, 10);
1484 ok(res == 0, "res != 0\n");
1485 ok(endpos == hex+1, "Incorrect endpos (%p-%p)\n", hex, endpos);
1486 res = p_strtoi64(oct, &endpos, 10);
1487 ok(res == 123, "res != 123\n");
1488 ok(endpos == oct+strlen(oct), "Incorrect endpos (%p-%p)\n", oct, endpos);
1489 res = p_strtoi64(blanks, &endpos, 10);
1490 ok(res == 12, "res != 12\n");
1491 ok(endpos == blanks+10, "Incorrect endpos (%p-%p)\n", blanks, endpos);
1492 ok(errno == 0xdeadbeef, "errno = %x\n", errno);
1494 errno = 0xdeadbeef;
1495 ures = p_strtoui64(no1, &endpos, 0);
1496 ok(ures == 31923, "ures != 31923\n");
1497 ok(endpos == no1+strlen(no1), "Incorrect endpos (%p-%p)\n", no1, endpos);
1498 ures = p_strtoui64(no2, &endpos, 0);
1499 ok(ures == -213312, "ures != -213312\n");
1500 ok(endpos == no2+strlen(no2), "Incorrect endpos (%p-%p)\n", no2, endpos);
1501 ures = p_strtoui64(no3, &endpos, 10);
1502 ok(ures == 12, "ures != 12\n");
1503 ok(endpos == no3+2, "Incorrect endpos (%p-%p)\n", no3, endpos);
1504 ures = p_strtoui64(no4, &endpos, 10);
1505 ok(ures == 0, "ures != 0\n");
1506 ok(endpos == no4, "Incorrect endpos (%p-%p)\n", no4, endpos);
1507 ures = p_strtoui64(hex, &endpos, 10);
1508 ok(ures == 0, "ures != 0\n");
1509 ok(endpos == hex+1, "Incorrect endpos (%p-%p)\n", hex, endpos);
1510 ures = p_strtoui64(oct, &endpos, 10);
1511 ok(ures == 123, "ures != 123\n");
1512 ok(endpos == oct+strlen(oct), "Incorrect endpos (%p-%p)\n", oct, endpos);
1513 ures = p_strtoui64(blanks, &endpos, 10);
1514 ok(ures == 12, "ures != 12\n");
1515 ok(endpos == blanks+10, "Incorrect endpos (%p-%p)\n", blanks, endpos);
1516 ok(errno == 0xdeadbeef, "errno = %x\n", errno);
1518 errno = 0xdeadbeef;
1519 ures = p_strtoui64(overflow, &endpos, 10);
1520 ok(ures == _UI64_MAX, "ures != _UI64_MAX\n");
1521 ok(endpos == overflow+strlen(overflow), "Incorrect endpos (%p-%p)\n", overflow, endpos);
1522 ok(errno == ERANGE, "errno = %x\n", errno);
1524 errno = 0xdeadbeef;
1525 ures = p_strtoui64(neg_overflow, &endpos, 10);
1526 ok(ures == 1, "ures != 1\n");
1527 ok(endpos == neg_overflow+strlen(neg_overflow), "Incorrect endpos (%p-%p)\n", neg_overflow, endpos);
1528 ok(errno == ERANGE, "errno = %x\n", errno);
1531 static inline BOOL almost_equal(double d1, double d2) {
1532 if(d1-d2>-1e-30 && d1-d2<1e-30)
1533 return TRUE;
1534 return FALSE;
1537 static void test__strtod(void)
1539 const char double1[] = "12.1";
1540 const char double2[] = "-13.721";
1541 const char double3[] = "INF";
1542 const char double4[] = ".21e12";
1543 const char double5[] = "214353e-3";
1544 const char overflow[] = "1d9999999999999999999";
1545 const char white_chars[] = " d10";
1547 char *end;
1548 double d;
1550 d = strtod(double1, &end);
1551 ok(almost_equal(d, 12.1), "d = %lf\n", d);
1552 ok(end == double1+4, "incorrect end (%d)\n", (int)(end-double1));
1554 d = strtod(double2, &end);
1555 ok(almost_equal(d, -13.721), "d = %lf\n", d);
1556 ok(end == double2+7, "incorrect end (%d)\n", (int)(end-double2));
1558 d = strtod(double3, &end);
1559 ok(almost_equal(d, 0), "d = %lf\n", d);
1560 ok(end == double3, "incorrect end (%d)\n", (int)(end-double3));
1562 d = strtod(double4, &end);
1563 ok(almost_equal(d, 210000000000.0), "d = %lf\n", d);
1564 ok(end == double4+6, "incorrect end (%d)\n", (int)(end-double4));
1566 d = strtod(double5, &end);
1567 ok(almost_equal(d, 214.353), "d = %lf\n", d);
1568 ok(end == double5+9, "incorrect end (%d)\n", (int)(end-double5));
1570 d = strtod("12.1d2", NULL);
1571 ok(almost_equal(d, 12.1e2), "d = %lf\n", d);
1573 d = strtod(white_chars, &end);
1574 ok(almost_equal(d, 0), "d = %lf\n", d);
1575 ok(end == white_chars, "incorrect end (%d)\n", (int)(end-white_chars));
1577 /* Set locale with non '.' decimal point (',') */
1578 if(!setlocale(LC_ALL, "Polish")) {
1579 win_skip("system with limited locales\n");
1580 return;
1583 d = strtod("12.1", NULL);
1584 ok(almost_equal(d, 12.0), "d = %lf\n", d);
1586 d = strtod("12,1", NULL);
1587 ok(almost_equal(d, 12.1), "d = %lf\n", d);
1589 setlocale(LC_ALL, "C");
1591 /* Precision tests */
1592 d = strtod("0.1", NULL);
1593 ok(almost_equal(d, 0.1), "d = %lf\n", d);
1594 d = strtod("-0.1", NULL);
1595 ok(almost_equal(d, -0.1), "d = %lf\n", d);
1596 d = strtod("0.1281832188491894198128921", NULL);
1597 ok(almost_equal(d, 0.1281832188491894198128921), "d = %lf\n", d);
1598 d = strtod("0.82181281288121", NULL);
1599 ok(almost_equal(d, 0.82181281288121), "d = %lf\n", d);
1600 d = strtod("21921922352523587651128218821", NULL);
1601 ok(almost_equal(d, 21921922352523587651128218821.0), "d = %lf\n", d);
1602 d = strtod("0.1d238", NULL);
1603 ok(almost_equal(d, 0.1e238L), "d = %lf\n", d);
1604 d = strtod("0.1D-4736", NULL);
1605 ok(almost_equal(d, 0.1e-4736L), "d = %lf\n", d);
1607 errno = 0xdeadbeef;
1608 strtod(overflow, &end);
1609 ok(errno == ERANGE, "errno = %x\n", errno);
1610 ok(end == overflow+21, "incorrect end (%d)\n", (int)(end-overflow));
1612 errno = 0xdeadbeef;
1613 strtod("-1d309", NULL);
1614 ok(errno == ERANGE, "errno = %x\n", errno);
1617 static void test_mbstowcs(void)
1619 static const wchar_t wSimple[] = { 't','e','x','t',0 };
1620 static const wchar_t wHiragana[] = { 0x3042,0x3043,0 };
1621 static const char mSimple[] = "text";
1622 static const char mHiragana[] = { 0x82,0xa0,0x82,0xa1,0 };
1624 const wchar_t *pwstr;
1625 wchar_t wOut[6];
1626 char mOut[6];
1627 size_t ret;
1628 int err;
1629 const char *pmbstr;
1630 mbstate_t state;
1632 wOut[4] = '!'; wOut[5] = '\0';
1633 mOut[4] = '!'; mOut[5] = '\0';
1635 ret = mbstowcs(NULL, mSimple, 0);
1636 ok(ret == 4, "mbstowcs did not return 4\n");
1638 ret = mbstowcs(wOut, mSimple, 4);
1639 ok(ret == 4, "mbstowcs did not return 4\n");
1640 ok(!memcmp(wOut, wSimple, 4*sizeof(wchar_t)), "wOut = %s\n", wine_dbgstr_w(wOut));
1641 ok(wOut[4] == '!', "wOut[4] != \'!\'\n");
1643 ret = wcstombs(NULL, wSimple, 0);
1644 ok(ret == 4, "wcstombs did not return 4\n");
1646 ret = wcstombs(mOut, wSimple, 6);
1647 ok(ret == 4, "wcstombs did not return 4\n");
1648 ok(!memcmp(mOut, mSimple, 5*sizeof(char)), "mOut = %s\n", mOut);
1650 ret = wcstombs(mOut, wSimple, 2);
1651 ok(ret == 2, "wcstombs did not return 2\n");
1652 ok(!memcmp(mOut, mSimple, 5*sizeof(char)), "mOut = %s\n", mOut);
1654 if(!setlocale(LC_ALL, "Japanese_Japan.932")) {
1655 win_skip("Japanese_Japan.932 locale not available\n");
1656 return;
1659 ret = mbstowcs(wOut, mHiragana, 6);
1660 ok(ret == 2, "mbstowcs did not return 2\n");
1661 ok(!memcmp(wOut, wHiragana, sizeof(wHiragana)), "wOut = %s\n", wine_dbgstr_w(wOut));
1663 ret = wcstombs(mOut, wHiragana, 6);
1664 ok(ret == 4, "wcstombs did not return 4\n");
1665 ok(!memcmp(mOut, mHiragana, sizeof(mHiragana)), "mOut = %s\n", mOut);
1667 if(!pmbstowcs_s || !pwcstombs_s) {
1668 setlocale(LC_ALL, "C");
1669 win_skip("mbstowcs_s or wcstombs_s not available\n");
1670 return;
1673 err = pmbstowcs_s(&ret, wOut, 6, mSimple, _TRUNCATE);
1674 ok(err == 0, "err = %d\n", err);
1675 ok(ret == 5, "mbstowcs_s did not return 5\n");
1676 ok(!memcmp(wOut, wSimple, sizeof(wSimple)), "wOut = %s\n", wine_dbgstr_w(wOut));
1678 err = pmbstowcs_s(&ret, wOut, 6, mHiragana, _TRUNCATE);
1679 ok(err == 0, "err = %d\n", err);
1680 ok(ret == 3, "mbstowcs_s did not return 3\n");
1681 ok(!memcmp(wOut, wHiragana, sizeof(wHiragana)), "wOut = %s\n", wine_dbgstr_w(wOut));
1683 err = pmbstowcs_s(&ret, NULL, 0, mHiragana, 1);
1684 ok(err == 0, "err = %d\n", err);
1685 ok(ret == 3, "mbstowcs_s did not return 3\n");
1687 err = pwcstombs_s(&ret, mOut, 6, wSimple, _TRUNCATE);
1688 ok(err == 0, "err = %d\n", err);
1689 ok(ret == 5, "wcstombs_s did not return 5\n");
1690 ok(!memcmp(mOut, mSimple, sizeof(mSimple)), "mOut = %s\n", mOut);
1692 err = pwcstombs_s(&ret, mOut, 6, wHiragana, _TRUNCATE);
1693 ok(err == 0, "err = %d\n", err);
1694 ok(ret == 5, "wcstombs_s did not return 5\n");
1695 ok(!memcmp(mOut, mHiragana, sizeof(mHiragana)), "mOut = %s\n", mOut);
1697 err = pwcstombs_s(&ret, NULL, 0, wHiragana, 1);
1698 ok(err == 0, "err = %d\n", err);
1699 ok(ret == 5, "wcstombs_s did not return 5\n");
1701 if(!pwcsrtombs) {
1702 setlocale(LC_ALL, "C");
1703 win_skip("wcsrtombs not available\n");
1704 return;
1707 pwstr = wSimple;
1708 err = -3;
1709 ret = pwcsrtombs(mOut, &pwstr, 4, &err);
1710 ok(ret == 4, "wcsrtombs did not return 4\n");
1711 ok(err == 0, "err = %d\n", err);
1712 ok(pwstr == wSimple+4, "pwstr = %p (wszSimple = %p)\n", pwstr, wSimple);
1713 ok(!memcmp(mOut, mSimple, ret), "mOut = %s\n", mOut);
1715 pwstr = wSimple;
1716 ret = pwcsrtombs(mOut, &pwstr, 5, NULL);
1717 ok(ret == 4, "wcsrtombs did not return 4\n");
1718 ok(pwstr == NULL, "pwstr != NULL\n");
1719 ok(!memcmp(mOut, mSimple, sizeof(mSimple)), "mOut = %s\n", mOut);
1721 if(!p_mbsrtowcs) {
1722 setlocale(LC_ALL, "C");
1723 win_skip("mbsrtowcs not available\n");
1724 return;
1727 pmbstr = mHiragana;
1728 ret = p_mbsrtowcs(wOut, &pmbstr, 6, NULL);
1729 ok(ret == 2, "mbsrtowcs did not return 2\n");
1730 ok(!memcmp(wOut, wHiragana, sizeof(wHiragana)), "wOut = %s\n", wine_dbgstr_w(wOut));
1731 ok(!pmbstr, "pmbstr != NULL\n");
1733 state = mHiragana[0];
1734 pmbstr = mHiragana+1;
1735 ret = p_mbsrtowcs(wOut, &pmbstr, 6, &state);
1736 ok(ret == 2, "mbsrtowcs did not return 2\n");
1737 ok(wOut[0] == 0x3042, "wOut[0] = %x\n", wOut[0]);
1738 ok(wOut[1] == 0xff61, "wOut[1] = %x\n", wOut[1]);
1739 ok(wOut[2] == 0, "wOut[2] = %x\n", wOut[2]);
1740 ok(!pmbstr, "pmbstr != NULL\n");
1742 if (p_set_invalid_parameter_handler)
1743 ok(p_set_invalid_parameter_handler(test_invalid_parameter_handler) == NULL,
1744 "Invalid parameter handler was already set\n");
1745 errno = EBADF;
1746 ret = p_mbsrtowcs(wOut, NULL, 6, &state);
1747 ok(ret == -1, "mbsrtowcs did not return -1\n");
1748 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1749 if (p_set_invalid_parameter_handler)
1750 ok(p_set_invalid_parameter_handler(NULL) == test_invalid_parameter_handler,
1751 "Cannot reset invalid parameter handler\n");
1753 setlocale(LC_ALL, "C");
1756 static void test_gcvt(void)
1758 char buf[1024], *res;
1759 errno_t err;
1761 if(!p_gcvt_s) {
1762 win_skip("Skipping _gcvt tests\n");
1763 return;
1766 errno = 0;
1767 res = _gcvt(1.2, -1, buf);
1768 ok(res == NULL, "res != NULL\n");
1769 ok(errno == ERANGE, "errno = %d\n", errno);
1771 errno = 0;
1772 res = _gcvt(1.2, 5, NULL);
1773 ok(res == NULL, "res != NULL\n");
1774 ok(errno == EINVAL, "errno = %d\n", errno);
1776 res = gcvt(1.2, 5, buf);
1777 ok(res == buf, "res != buf\n");
1778 ok(!strcmp(buf, "1.2"), "buf = %s\n", buf);
1780 buf[0] = 'x';
1781 err = p_gcvt_s(buf, 5, 1.2, 10);
1782 ok(err == ERANGE, "err = %d\n", err);
1783 ok(buf[0] == '\0', "buf[0] = %c\n", buf[0]);
1785 buf[0] = 'x';
1786 err = p_gcvt_s(buf, 4, 123456, 2);
1787 ok(err == ERANGE, "err = %d\n", err);
1788 ok(buf[0] == '\0', "buf[0] = %c\n", buf[0]);
1791 static void test__itoa_s(void)
1793 errno_t ret;
1794 char buffer[33];
1796 if (!p_itoa_s)
1798 win_skip("Skipping _itoa_s tests\n");
1799 return;
1802 if (p_set_invalid_parameter_handler)
1803 ok(p_set_invalid_parameter_handler(test_invalid_parameter_handler) == NULL,
1804 "Invalid parameter handler was already set\n");
1806 errno = EBADF;
1807 ret = p_itoa_s(0, NULL, 0, 0);
1808 ok(ret == EINVAL, "Expected _itoa_s to return EINVAL, got %d\n", ret);
1809 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1811 memset(buffer, 'X', sizeof(buffer));
1812 errno = EBADF;
1813 ret = p_itoa_s(0, buffer, 0, 0);
1814 ok(ret == EINVAL, "Expected _itoa_s to return EINVAL, got %d\n", ret);
1815 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1816 ok(buffer[0] == 'X', "Expected the output buffer to be untouched\n");
1818 memset(buffer, 'X', sizeof(buffer));
1819 errno = EBADF;
1820 ret = p_itoa_s(0, buffer, sizeof(buffer), 0);
1821 ok(ret == EINVAL, "Expected _itoa_s to return EINVAL, got %d\n", ret);
1822 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1823 ok(buffer[0] == '\0', "Expected the output buffer to be null terminated\n");
1825 memset(buffer, 'X', sizeof(buffer));
1826 errno = EBADF;
1827 ret = p_itoa_s(0, buffer, sizeof(buffer), 64);
1828 ok(ret == EINVAL, "Expected _itoa_s to return EINVAL, got %d\n", ret);
1829 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1830 ok(buffer[0] == '\0', "Expected the output buffer to be null terminated\n");
1832 memset(buffer, 'X', sizeof(buffer));
1833 errno = EBADF;
1834 ret = p_itoa_s(12345678, buffer, 4, 10);
1835 ok(ret == ERANGE, "Expected _itoa_s to return ERANGE, got %d\n", ret);
1836 ok(errno == ERANGE, "Expected errno to be ERANGE, got %d\n", errno);
1837 ok(!memcmp(buffer, "\000765", 4),
1838 "Expected the output buffer to be null terminated with truncated output\n");
1840 memset(buffer, 'X', sizeof(buffer));
1841 errno = EBADF;
1842 ret = p_itoa_s(12345678, buffer, 8, 10);
1843 ok(ret == ERANGE, "Expected _itoa_s to return ERANGE, got %d\n", ret);
1844 ok(errno == ERANGE, "Expected errno to be ERANGE, got %d\n", errno);
1845 ok(!memcmp(buffer, "\0007654321", 8),
1846 "Expected the output buffer to be null terminated with truncated output\n");
1848 memset(buffer, 'X', sizeof(buffer));
1849 errno = EBADF;
1850 ret = p_itoa_s(-12345678, buffer, 9, 10);
1851 ok(ret == ERANGE, "Expected _itoa_s to return ERANGE, got %d\n", ret);
1852 ok(errno == ERANGE, "Expected errno to be ERANGE, got %d\n", errno);
1853 ok(!memcmp(buffer, "\00087654321", 9),
1854 "Expected the output buffer to be null terminated with truncated output\n");
1856 ret = p_itoa_s(12345678, buffer, 9, 10);
1857 ok(ret == 0, "Expected _itoa_s to return 0, got %d\n", ret);
1858 ok(!strcmp(buffer, "12345678"),
1859 "Expected output buffer string to be \"12345678\", got \"%s\"\n",
1860 buffer);
1862 ret = p_itoa_s(43690, buffer, sizeof(buffer), 2);
1863 ok(ret == 0, "Expected _itoa_s to return 0, got %d\n", ret);
1864 ok(!strcmp(buffer, "1010101010101010"),
1865 "Expected output buffer string to be \"1010101010101010\", got \"%s\"\n",
1866 buffer);
1868 ret = p_itoa_s(1092009, buffer, sizeof(buffer), 36);
1869 ok(ret == 0, "Expected _itoa_s to return 0, got %d\n", ret);
1870 ok(!strcmp(buffer, "nell"),
1871 "Expected output buffer string to be \"nell\", got \"%s\"\n",
1872 buffer);
1874 ret = p_itoa_s(5704, buffer, sizeof(buffer), 18);
1875 ok(ret == 0, "Expected _itoa_s to return 0, got %d\n", ret);
1876 ok(!strcmp(buffer, "hag"),
1877 "Expected output buffer string to be \"hag\", got \"%s\"\n",
1878 buffer);
1880 ret = p_itoa_s(-12345678, buffer, sizeof(buffer), 10);
1881 ok(ret == 0, "Expected _itoa_s to return 0, got %d\n", ret);
1882 ok(!strcmp(buffer, "-12345678"),
1883 "Expected output buffer string to be \"-12345678\", got \"%s\"\n",
1884 buffer);
1885 if (p_set_invalid_parameter_handler)
1886 ok(p_set_invalid_parameter_handler(NULL) == test_invalid_parameter_handler,
1887 "Cannot reset invalid parameter handler\n");
1890 static void test__strlwr_s(void)
1892 errno_t ret;
1893 char buffer[20];
1895 if (!p_strlwr_s)
1897 win_skip("Skipping _strlwr_s tests\n");
1898 return;
1901 errno = EBADF;
1902 ret = p_strlwr_s(NULL, 0);
1903 ok(ret == EINVAL, "Expected _strlwr_s to return EINVAL, got %d\n", ret);
1904 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1906 errno = EBADF;
1907 ret = p_strlwr_s(NULL, sizeof(buffer));
1908 ok(ret == EINVAL, "Expected _strlwr_s to return EINVAL, got %d\n", ret);
1909 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1911 errno = EBADF;
1912 ret = p_strlwr_s(buffer, 0);
1913 ok(ret == EINVAL, "Expected _strlwr_s to return EINVAL, got %d\n", ret);
1914 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1916 strcpy(buffer, "GoRrIsTeR");
1917 errno = EBADF;
1918 ret = p_strlwr_s(buffer, 5);
1919 ok(ret == EINVAL, "Expected _strlwr_s to return EINVAL, got %d\n", ret);
1920 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1921 ok(!memcmp(buffer, "\0oRrIsTeR", sizeof("\0oRrIsTeR")),
1922 "Expected the output buffer to be \"\\0oRrIsTeR\"\n");
1924 strcpy(buffer, "GoRrIsTeR");
1925 errno = EBADF;
1926 ret = p_strlwr_s(buffer, sizeof("GoRrIsTeR") - 1);
1927 ok(ret == EINVAL, "Expected _strlwr_s to return EINVAL, got %d\n", ret);
1928 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1929 ok(!memcmp(buffer, "\0oRrIsTeR", sizeof("\0oRrIsTeR")),
1930 "Expected the output buffer to be \"\\0oRrIsTeR\"\n");
1932 strcpy(buffer, "GoRrIsTeR");
1933 ret = p_strlwr_s(buffer, sizeof("GoRrIsTeR"));
1934 ok(ret == 0, "Expected _strlwr_s to return 0, got %d\n", ret);
1935 ok(!strcmp(buffer, "gorrister"),
1936 "Expected the output buffer to be \"gorrister\", got \"%s\"\n",
1937 buffer);
1939 memcpy(buffer, "GoRrIsTeR\0ELLEN", sizeof("GoRrIsTeR\0ELLEN"));
1940 ret = p_strlwr_s(buffer, sizeof(buffer));
1941 ok(ret == 0, "Expected _strlwr_s to return 0, got %d\n", ret);
1942 ok(!memcmp(buffer, "gorrister\0ELLEN", sizeof("gorrister\0ELLEN")),
1943 "Expected the output buffer to be \"gorrister\\0ELLEN\", got \"%s\"\n",
1944 buffer);
1947 static void test_wcsncat_s(void)
1949 static wchar_t abcW[] = {'a','b','c',0};
1950 int ret;
1951 wchar_t dst[4];
1952 wchar_t src[4];
1954 if (!p_wcsncat_s)
1956 win_skip("skipping wcsncat_s tests\n");
1957 return;
1960 if (p_set_invalid_parameter_handler)
1961 ok(p_set_invalid_parameter_handler(test_invalid_parameter_handler) == NULL,
1962 "Invalid parameter handler was already set\n");
1964 memcpy(src, abcW, sizeof(abcW));
1965 dst[0] = 0;
1966 ret = p_wcsncat_s(NULL, 4, src, 4);
1967 ok(ret == EINVAL, "err = %d\n", ret);
1968 ret = p_wcsncat_s(dst, 0, src, 4);
1969 ok(ret == EINVAL, "err = %d\n", ret);
1970 ret = p_wcsncat_s(dst, 0, src, _TRUNCATE);
1971 ok(ret == EINVAL, "err = %d\n", ret);
1972 ret = p_wcsncat_s(dst, 4, NULL, 0);
1973 ok(ret == 0, "err = %d\n", ret);
1975 dst[0] = 0;
1976 ret = p_wcsncat_s(dst, 2, src, 4);
1977 ok(ret == ERANGE, "err = %d\n", ret);
1979 dst[0] = 0;
1980 ret = p_wcsncat_s(dst, 2, src, _TRUNCATE);
1981 ok(ret == STRUNCATE, "err = %d\n", ret);
1982 ok(dst[0] == 'a' && dst[1] == 0, "dst is %s\n", wine_dbgstr_w(dst));
1984 memcpy(dst, abcW, sizeof(abcW));
1985 dst[3] = 'd';
1986 ret = p_wcsncat_s(dst, 4, src, 4);
1987 ok(ret == EINVAL, "err = %d\n", ret);
1989 if (p_set_invalid_parameter_handler)
1990 ok(p_set_invalid_parameter_handler(NULL) == test_invalid_parameter_handler,
1991 "Cannot reset invalid parameter handler\n");
1994 static void test__mbsnbcat_s(void)
1996 unsigned char dest[16];
1997 const unsigned char first[] = "dinosaur";
1998 const unsigned char second[] = "duck";
1999 int ret;
2001 if (!p_mbsnbcat_s)
2003 win_skip("Skipping _mbsnbcat_s tests\n");
2004 return;
2007 /* Test invalid arguments. */
2008 ret = p_mbsnbcat_s(NULL, 0, NULL, 0);
2009 ok(ret == 0, "Expected _mbsnbcat_s to return 0, got %d\n", ret);
2011 errno = EBADF;
2012 ret = p_mbsnbcat_s(NULL, 10, NULL, 0);
2013 ok(ret == EINVAL, "Expected _mbsnbcat_s to return EINVAL, got %d\n", ret);
2014 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2016 errno = EBADF;
2017 ret = p_mbsnbcat_s(NULL, 0, NULL, 10);
2018 ok(ret == EINVAL, "Expected _mbsnbcat_s to return EINVAL, got %d\n", ret);
2019 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2021 memset(dest, 'X', sizeof(dest));
2022 errno = EBADF;
2023 ret = p_mbsnbcat_s(dest, 0, NULL, 0);
2024 ok(ret == EINVAL, "Expected _mbsnbcat_s to return EINVAL, got %d\n", ret);
2025 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2026 ok(dest[0] == 'X', "Expected the output buffer to be untouched\n");
2028 memset(dest, 'X', sizeof(dest));
2029 errno = EBADF;
2030 ret = p_mbsnbcat_s(dest, 0, second, 0);
2031 ok(ret == EINVAL, "Expected _mbsnbcat_s to return EINVAL, got %d\n", ret);
2032 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2033 ok(dest[0] == 'X', "Expected the output buffer to be untouched\n");
2035 memset(dest, 'X', sizeof(dest));
2036 errno = EBADF;
2037 ret = p_mbsnbcat_s(dest, sizeof(dest), NULL, 0);
2038 ok(ret == EINVAL, "Expected _mbsnbcat_s to return EINVAL, got %d\n", ret);
2039 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2040 ok(dest[0] == '\0', "Expected the output buffer to be null terminated\n");
2042 memset(dest, 'X', sizeof(dest));
2043 errno = EBADF;
2044 ret = p_mbsnbcat_s(dest, sizeof(dest), NULL, 10);
2045 ok(ret == EINVAL, "Expected _mbsnbcat_s to return EINVAL, got %d\n", ret);
2046 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2047 ok(dest[0] == '\0', "Expected the output buffer to be null terminated\n");
2049 memset(dest, 'X', sizeof(dest));
2050 dest[0] = '\0';
2051 ret = p_mbsnbcat_s(dest, sizeof(dest), second, sizeof(second));
2052 ok(ret == 0, "Expected _mbsnbcat_s to return 0, got %d\n", ret);
2053 ok(!memcmp(dest, second, sizeof(second)),
2054 "Expected the output buffer string to be \"duck\"\n");
2056 /* Test source truncation behavior. */
2057 memset(dest, 'X', sizeof(dest));
2058 memcpy(dest, first, sizeof(first));
2059 ret = p_mbsnbcat_s(dest, sizeof(dest), second, 0);
2060 ok(ret == 0, "Expected _mbsnbcat_s to return 0, got %d\n", ret);
2061 ok(!memcmp(dest, first, sizeof(first)),
2062 "Expected the output buffer string to be \"dinosaur\"\n");
2064 memset(dest, 'X', sizeof(dest));
2065 memcpy(dest, first, sizeof(first));
2066 ret = p_mbsnbcat_s(dest, sizeof(dest), second, sizeof(second));
2067 ok(ret == 0, "Expected _mbsnbcat_s to return 0, got %d\n", ret);
2068 ok(!memcmp(dest, "dinosaurduck", sizeof("dinosaurduck")),
2069 "Expected the output buffer string to be \"dinosaurduck\"\n");
2071 memset(dest, 'X', sizeof(dest));
2072 memcpy(dest, first, sizeof(first));
2073 ret = p_mbsnbcat_s(dest, sizeof(dest), second, sizeof(second) + 1);
2074 ok(ret == 0, "Expected _mbsnbcat_s to return 0, got %d\n", ret);
2075 ok(!memcmp(dest, "dinosaurduck", sizeof("dinosaurduck")),
2076 "Expected the output buffer string to be \"dinosaurduck\"\n");
2078 memset(dest, 'X', sizeof(dest));
2079 memcpy(dest, first, sizeof(first));
2080 ret = p_mbsnbcat_s(dest, sizeof(dest), second, sizeof(second) - 1);
2081 ok(ret == 0, "Expected _mbsnbcat_s to return 0, got %d\n", ret);
2082 ok(!memcmp(dest, "dinosaurduck", sizeof("dinosaurduck")),
2083 "Expected the output buffer string to be \"dinosaurduck\"\n");
2085 memset(dest, 'X', sizeof(dest));
2086 memcpy(dest, first, sizeof(first));
2087 ret = p_mbsnbcat_s(dest, sizeof(dest), second, sizeof(second) - 2);
2088 ok(ret == 0, "Expected _mbsnbcat_s to return 0, got %d\n", ret);
2089 ok(!memcmp(dest, "dinosaurduc", sizeof("dinosaurduc")),
2090 "Expected the output buffer string to be \"dinosaurduc\"\n");
2092 /* Test destination truncation behavior. */
2093 memset(dest, 'X', sizeof(dest));
2094 memcpy(dest, first, sizeof(first));
2095 errno = EBADF;
2096 ret = p_mbsnbcat_s(dest, sizeof(first) - 1, second, sizeof(second));
2097 ok(ret == EINVAL, "Expected _mbsnbcat_s to return EINVAL, got %d\n", ret);
2098 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2099 ok(!memcmp(dest, "\0inosaur", sizeof("\0inosaur") - 1),
2100 "Expected the output buffer string to be \"\\0inosaur\" without ending null terminator\n");
2102 memset(dest, 'X', sizeof(dest));
2103 memcpy(dest, first, sizeof(first));
2104 errno = EBADF;
2105 ret = p_mbsnbcat_s(dest, sizeof(first), second, sizeof(second));
2106 ok(ret == ERANGE, "Expected _mbsnbcat_s to return ERANGE, got %d\n", ret);
2107 ok(errno == ERANGE, "Expected errno to be ERANGE, got %d\n", errno);
2108 ok(!memcmp(dest, "\0inosaurd", sizeof("\0inosaurd") - 1),
2109 "Expected the output buffer string to be \"\\0inosaurd\" without ending null terminator\n");
2111 memset(dest, 'X', sizeof(dest));
2112 memcpy(dest, first, sizeof(first));
2113 errno = EBADF;
2114 ret = p_mbsnbcat_s(dest, sizeof(first) + 1, second, sizeof(second));
2115 ok(ret == ERANGE, "Expected _mbsnbcat_s to return ERANGE, got %d\n", ret);
2116 ok(errno == ERANGE, "Expected errno to be ERANGE, got %d\n", errno);
2117 ok(!memcmp(dest, "\0inosaurdu", sizeof("\0inosaurdu") - 1),
2118 "Expected the output buffer string to be \"\\0inosaurdu\" without ending null terminator\n");
2121 static void test__mbsupr_s(void)
2123 errno_t ret;
2124 unsigned char buffer[20];
2126 if (!p_mbsupr_s)
2128 win_skip("Skipping _mbsupr_s tests\n");
2129 return;
2132 errno = EBADF;
2133 ret = p_mbsupr_s(NULL, 0);
2134 ok(ret == 0, "Expected _mbsupr_s to return 0, got %d\n", ret);
2136 errno = EBADF;
2137 ret = p_mbsupr_s(NULL, sizeof(buffer));
2138 ok(ret == EINVAL, "Expected _mbsupr_s to return EINVAL, got %d\n", ret);
2139 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2141 errno = EBADF;
2142 ret = p_mbsupr_s(buffer, 0);
2143 ok(ret == EINVAL, "Expected _mbsupr_s to return EINVAL, got %d\n", ret);
2144 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2146 memcpy(buffer, "abcdefgh", sizeof("abcdefgh"));
2147 errno = EBADF;
2148 ret = p_mbsupr_s(buffer, sizeof("abcdefgh"));
2149 ok(ret == 0, "Expected _mbsupr_s to return 0, got %d\n", ret);
2150 ok(!memcmp(buffer, "ABCDEFGH", sizeof("ABCDEFGH")),
2151 "Expected the output buffer to be \"ABCDEFGH\", got \"%s\"\n",
2152 buffer);
2154 memcpy(buffer, "abcdefgh", sizeof("abcdefgh"));
2155 errno = EBADF;
2156 ret = p_mbsupr_s(buffer, sizeof(buffer));
2157 ok(ret == 0, "Expected _mbsupr_s to return 0, got %d\n", ret);
2158 ok(!memcmp(buffer, "ABCDEFGH", sizeof("ABCDEFGH")),
2159 "Expected the output buffer to be \"ABCDEFGH\", got \"%s\"\n",
2160 buffer);
2162 memcpy(buffer, "abcdefgh", sizeof("abcdefgh"));
2163 errno = EBADF;
2164 ret = p_mbsupr_s(buffer, 4);
2165 ok(ret == EINVAL, "Expected _mbsupr_s to return EINVAL, got %d\n", ret);
2166 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2168 memcpy(buffer, "abcdefgh\0ijklmnop", sizeof("abcdefgh\0ijklmnop"));
2169 errno = EBADF;
2170 ret = p_mbsupr_s(buffer, sizeof(buffer));
2171 ok(ret == 0, "Expected _mbsupr_s to return 0, got %d\n", ret);
2172 ok(!memcmp(buffer, "ABCDEFGH\0ijklmnop", sizeof("ABCDEFGH\0ijklmnop")),
2173 "Expected the output buffer to be \"ABCDEFGH\\0ijklmnop\", got \"%s\"\n",
2174 buffer);
2178 static void test__mbslwr_s(void)
2180 errno_t ret;
2181 unsigned char buffer[20];
2183 if (!p_mbslwr_s)
2185 win_skip("Skipping _mbslwr_s tests\n");
2186 return;
2189 errno = EBADF;
2190 ret = p_mbslwr_s(NULL, 0);
2191 ok(ret == 0, "Expected _mbslwr_s to return 0, got %d\n", ret);
2193 errno = EBADF;
2194 ret = p_mbslwr_s(NULL, sizeof(buffer));
2195 ok(ret == EINVAL, "Expected _mbslwr_s to return EINVAL, got %d\n", ret);
2196 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2198 errno = EBADF;
2199 ret = p_mbslwr_s(buffer, 0);
2200 ok(ret == EINVAL, "Expected _mbslwr_s to return EINVAL, got %d\n", ret);
2201 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2203 memcpy(buffer, "ABCDEFGH", sizeof("ABCDEFGH"));
2204 errno = EBADF;
2205 ret = p_mbslwr_s(buffer, sizeof("ABCDEFGH"));
2206 ok(ret == 0, "Expected _mbslwr_s to return 0, got %d\n", ret);
2207 ok(!memcmp(buffer, "abcdefgh", sizeof("abcdefgh")),
2208 "Expected the output buffer to be \"abcdefgh\", got \"%s\"\n",
2209 buffer);
2211 memcpy(buffer, "ABCDEFGH", sizeof("ABCDEFGH"));
2212 errno = EBADF;
2213 ret = p_mbslwr_s(buffer, sizeof(buffer));
2214 ok(ret == 0, "Expected _mbslwr_s to return 0, got %d\n", ret);
2215 ok(!memcmp(buffer, "abcdefgh", sizeof("abcdefgh")),
2216 "Expected the output buffer to be \"abcdefgh\", got \"%s\"\n",
2217 buffer);
2219 memcpy(buffer, "ABCDEFGH", sizeof("ABCDEFGH"));
2220 errno = EBADF;
2221 ret = p_mbslwr_s(buffer, 4);
2222 ok(ret == EINVAL, "Expected _mbslwr_s to return EINVAL, got %d\n", ret);
2223 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2225 memcpy(buffer, "ABCDEFGH\0IJKLMNOP", sizeof("ABCDEFGH\0IJKLMNOP"));
2226 errno = EBADF;
2227 ret = p_mbslwr_s(buffer, sizeof(buffer));
2228 ok(ret == 0, "Expected _mbslwr_s to return 0, got %d\n", ret);
2229 ok(!memcmp(buffer, "abcdefgh\0IJKLMNOP", sizeof("abcdefgh\0IJKLMNOP")),
2230 "Expected the output buffer to be \"abcdefgh\\0IJKLMNOP\", got \"%s\"\n",
2231 buffer);
2234 static void test__mbstok(void)
2236 const unsigned char delim[] = "t";
2238 char str[] = "!.!test";
2239 unsigned char *ret;
2241 strtok(str, "!");
2243 ret = _mbstok(NULL, delim);
2244 /* most versions of msvcrt use the same buffer for strtok and _mbstok */
2245 ok(!ret || broken((char*)ret==str+4),
2246 "_mbstok(NULL, \"t\") = %p, expected NULL (%p)\n", ret, str);
2248 ret = _mbstok(NULL, delim);
2249 ok(!ret, "_mbstok(NULL, \"t\") = %p, expected NULL\n", ret);
2252 static void test__ultoa_s(void)
2254 errno_t ret;
2255 char buffer[33];
2257 if (!p_ultoa_s)
2259 win_skip("Skipping _ultoa_s tests\n");
2260 return;
2263 errno = EBADF;
2264 ret = p_ultoa_s(0, NULL, 0, 0);
2265 ok(ret == EINVAL, "Expected _ultoa_s to return EINVAL, got %d\n", ret);
2266 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2268 memset(buffer, 'X', sizeof(buffer));
2269 errno = EBADF;
2270 ret = p_ultoa_s(0, buffer, 0, 0);
2271 ok(ret == EINVAL, "Expected _ultoa_s to return EINVAL, got %d\n", ret);
2272 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2273 ok(buffer[0] == 'X', "Expected the output buffer to be untouched\n");
2275 memset(buffer, 'X', sizeof(buffer));
2276 errno = EBADF;
2277 ret = p_ultoa_s(0, buffer, sizeof(buffer), 0);
2278 ok(ret == EINVAL, "Expected _ultoa_s to return EINVAL, got %d\n", ret);
2279 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2280 ok(buffer[0] == '\0', "Expected the output buffer to be null terminated\n");
2282 memset(buffer, 'X', sizeof(buffer));
2283 errno = EBADF;
2284 ret = p_ultoa_s(0, buffer, sizeof(buffer), 64);
2285 ok(ret == EINVAL, "Expected _ultoa_s to return EINVAL, got %d\n", ret);
2286 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2287 ok(buffer[0] == '\0', "Expected the output buffer to be null terminated\n");
2289 memset(buffer, 'X', sizeof(buffer));
2290 errno = EBADF;
2291 ret = p_ultoa_s(12345678, buffer, 4, 10);
2292 ok(ret == ERANGE, "Expected _ultoa_s to return ERANGE, got %d\n", ret);
2293 ok(errno == ERANGE, "Expected errno to be ERANGE, got %d\n", errno);
2294 ok(!memcmp(buffer, "\000765", 4),
2295 "Expected the output buffer to be null terminated with truncated output\n");
2297 memset(buffer, 'X', sizeof(buffer));
2298 errno = EBADF;
2299 ret = p_ultoa_s(12345678, buffer, 8, 10);
2300 ok(ret == ERANGE, "Expected _ultoa_s to return ERANGE, got %d\n", ret);
2301 ok(errno == ERANGE, "Expected errno to be ERANGE, got %d\n", errno);
2302 ok(!memcmp(buffer, "\0007654321", 8),
2303 "Expected the output buffer to be null terminated with truncated output\n");
2305 ret = p_ultoa_s(12345678, buffer, 9, 10);
2306 ok(ret == 0, "Expected _ultoa_s to return 0, got %d\n", ret);
2307 ok(!strcmp(buffer, "12345678"),
2308 "Expected output buffer string to be \"12345678\", got \"%s\"\n",
2309 buffer);
2311 ret = p_ultoa_s(43690, buffer, sizeof(buffer), 2);
2312 ok(ret == 0, "Expected _ultoa_s to return 0, got %d\n", ret);
2313 ok(!strcmp(buffer, "1010101010101010"),
2314 "Expected output buffer string to be \"1010101010101010\", got \"%s\"\n",
2315 buffer);
2317 ret = p_ultoa_s(1092009, buffer, sizeof(buffer), 36);
2318 ok(ret == 0, "Expected _ultoa_s to return 0, got %d\n", ret);
2319 ok(!strcmp(buffer, "nell"),
2320 "Expected output buffer string to be \"nell\", got \"%s\"\n",
2321 buffer);
2323 ret = p_ultoa_s(5704, buffer, sizeof(buffer), 18);
2324 ok(ret == 0, "Expected _ultoa_s to return 0, got %d\n", ret);
2325 ok(!strcmp(buffer, "hag"),
2326 "Expected output buffer string to be \"hag\", got \"%s\"\n",
2327 buffer);
2330 static void test_wctob(void)
2332 int ret;
2334 if(!p_wctob || !setlocale(LC_ALL, "chinese-traditional")) {
2335 win_skip("Skipping wctob tests\n");
2336 return;
2339 ret = p_wctob(0x8141);
2340 ok(ret == EOF, "ret = %x\n", ret);
2342 ret = p_wctob(0x81);
2343 ok(ret == EOF, "ret = %x\n", ret);
2345 ret = p_wctob(0xe0);
2346 ok(ret == 0x61, "ret = %x\n", ret);
2348 _setmbcp(1250);
2349 ret = p_wctob(0x81);
2350 ok(ret == EOF, "ret = %x\n", ret);
2352 setlocale(LC_ALL, "C");
2353 ret = p_wctob(0x8141);
2354 ok(ret == EOF, "ret = %x\n", ret);
2356 ret = p_wctob(0x81);
2357 ok(ret == (int)(char)0x81, "ret = %x\n", ret);
2359 ret = p_wctob(0x9f);
2360 ok(ret == (int)(char)0x9f, "ret = %x\n", ret);
2362 ret = p_wctob(0xe0);
2363 ok(ret == (int)(char)0xe0, "ret = %x\n", ret);
2365 static void test_wctomb(void)
2367 mbstate_t state;
2368 unsigned char dst[10];
2369 size_t ret;
2371 if(!p_wcrtomb || !setlocale(LC_ALL, "Japanese_Japan.932")) {
2372 win_skip("wcrtomb tests\n");
2373 return;
2376 ret = p_wcrtomb(NULL, 0x3042, NULL);
2377 ok(ret == 2, "wcrtomb did not return 2\n");
2379 state = 1;
2380 dst[2] = 'a';
2381 ret = p_wcrtomb((char*)dst, 0x3042, &state);
2382 ok(ret == 2, "wcrtomb did not return 2\n");
2383 ok(state == 0, "state != 0\n");
2384 ok(dst[0] == 0x82, "dst[0] = %x, expected 0x82\n", dst[0]);
2385 ok(dst[1] == 0xa0, "dst[1] = %x, expected 0xa0\n", dst[1]);
2386 ok(dst[2] == 'a', "dst[2] != 'a'\n");
2388 ret = p_wcrtomb((char*)dst, 0x3043, NULL);
2389 ok(ret == 2, "wcrtomb did not return 2\n");
2390 ok(dst[0] == 0x82, "dst[0] = %x, expected 0x82\n", dst[0]);
2391 ok(dst[1] == 0xa1, "dst[1] = %x, expected 0xa1\n", dst[1]);
2393 ret = p_wcrtomb((char*)dst, 0x20, NULL);
2394 ok(ret == 1, "wcrtomb did not return 1\n");
2395 ok(dst[0] == 0x20, "dst[0] = %x, expected 0x20\n", dst[0]);
2397 ret = p_wcrtomb((char*)dst, 0xffff, NULL);
2398 ok(ret == -1, "wcrtomb did not return -1\n");
2399 ok(dst[0] == 0x3f, "dst[0] = %x, expected 0x3f\n", dst[0]);
2401 setlocale(LC_ALL, "C");
2404 static void test_tolower(void)
2406 int ret;
2408 ret = p_tolower(0x41);
2409 ok(ret == 0x61, "ret = %x\n", ret);
2411 ret = p_tolower(0xF4);
2412 ok(ret == 0xF4, "ret = %x\n", ret);
2414 ret = p_tolower((char)0xF4);
2415 ok(ret==0xF4/*Vista+*/ || ret==(char)0xF4, "ret = %x\n", ret);
2417 /* is it using different locale (CP_ACP) for negative values?? */
2418 /* current implementation matches msvcr90 behaviour */
2419 ret = p_tolower((char)0xD0);
2420 todo_wine ok(ret==0xF0/*Vista+*/ || ret==(char)0xD0, "ret = %x\n", ret);
2422 ret = p_tolower(0xD0);
2423 ok(ret == 0xD0, "ret = %x\n", ret);
2425 if(!setlocale(LC_ALL, "us")) {
2426 win_skip("skipping tolower tests that depends on locale\n");
2427 return;
2430 ret = p_tolower((char)0xD0);
2431 ok(ret == 0xF0, "ret = %x\n", ret);
2433 ret = p_tolower(0xD0);
2434 ok(ret == 0xF0, "ret = %x\n", ret);
2436 setlocale(LC_ALL, "C");
2439 static void test__atodbl(void)
2441 _CRT_DOUBLE d;
2442 char num[32];
2443 int ret;
2445 if(!p__atodbl_l) {
2446 /* Old versions of msvcrt use different values for _OVERFLOW and _UNDERFLOW
2447 * Because of this lets skip _atodbl tests when _atodbl_l is not available */
2448 win_skip("_atodbl_l is not available\n");
2449 return;
2452 num[0] = 0;
2453 ret = p__atodbl_l(&d, num, NULL);
2454 ok(ret == 0, "_atodbl_l(&d, \"\", NULL) returned %d, expected 0\n", ret);
2455 ok(d.x == 0, "d.x = %lf, expected 0\n", d.x);
2456 ret = _atodbl(&d, num);
2457 ok(ret == 0, "_atodbl(&d, \"\") returned %d, expected 0\n", ret);
2458 ok(d.x == 0, "d.x = %lf, expected 0\n", d.x);
2460 strcpy(num, "t");
2461 ret = p__atodbl_l(&d, num, NULL);
2462 ok(ret == 0, "_atodbl_l(&d, \"t\", NULL) returned %d, expected 0\n", ret);
2463 ok(d.x == 0, "d.x = %lf, expected 0\n", d.x);
2464 ret = _atodbl(&d, num);
2465 ok(ret == 0, "_atodbl(&d, \"t\") returned %d, expected 0\n", ret);
2466 ok(d.x == 0, "d.x = %lf, expected 0\n", d.x);
2468 strcpy(num, "0");
2469 ret = p__atodbl_l(&d, num, NULL);
2470 ok(ret == 0, "_atodbl_l(&d, \"0\", NULL) returned %d, expected 0\n", ret);
2471 ok(d.x == 0, "d.x = %lf, expected 0\n", d.x);
2472 ret = _atodbl(&d, num);
2473 ok(ret == 0, "_atodbl(&d, \"0\") returned %d, expected 0\n", ret);
2474 ok(d.x == 0, "d.x = %lf, expected 0\n", d.x);
2476 strcpy(num, "123");
2477 ret = p__atodbl_l(&d, num, NULL);
2478 ok(ret == 0, "_atodbl_l(&d, \"123\", NULL) returned %d, expected 0\n", ret);
2479 ok(d.x == 123, "d.x = %lf, expected 123\n", d.x);
2480 ret = _atodbl(&d, num);
2481 ok(ret == 0, "_atodbl(&d, \"123\") returned %d, expected 0\n", ret);
2482 ok(d.x == 123, "d.x = %lf, expected 123\n", d.x);
2484 strcpy(num, "1e-309");
2485 ret = p__atodbl_l(&d, num, NULL);
2486 ok(ret == _UNDERFLOW, "_atodbl_l(&d, \"1e-309\", NULL) returned %d, expected _UNDERFLOW\n", ret);
2487 ok(d.x!=0 && almost_equal(d.x, 0), "d.x = %le, expected 0\n", d.x);
2488 ret = _atodbl(&d, num);
2489 ok(ret == _UNDERFLOW, "_atodbl(&d, \"1e-309\") returned %d, expected _UNDERFLOW\n", ret);
2490 ok(d.x!=0 && almost_equal(d.x, 0), "d.x = %le, expected 0\n", d.x);
2492 strcpy(num, "1e309");
2493 ret = p__atodbl_l(&d, num, NULL);
2494 ok(ret == _OVERFLOW, "_atodbl_l(&d, \"1e309\", NULL) returned %d, expected _OVERFLOW\n", ret);
2495 ret = _atodbl(&d, num);
2496 ok(ret == _OVERFLOW, "_atodbl(&d, \"1e309\") returned %d, expected _OVERFLOW\n", ret);
2499 static void test__stricmp(void)
2501 int ret;
2503 ret = _stricmp("test", "test");
2504 ok(ret == 0, "_stricmp returned %d\n", ret);
2505 ret = _stricmp("a", "z");
2506 ok(ret < 0, "_stricmp returned %d\n", ret);
2507 ret = _stricmp("z", "a");
2508 ok(ret > 0, "_stricmp returned %d\n", ret);
2509 ret = _stricmp("\xa5", "\xb9");
2510 ok(ret < 0, "_stricmp returned %d\n", ret);
2512 if(!setlocale(LC_ALL, "polish")) {
2513 win_skip("stricmp tests\n");
2514 return;
2517 ret = _stricmp("test", "test");
2518 ok(ret == 0, "_stricmp returned %d\n", ret);
2519 ret = _stricmp("a", "z");
2520 ok(ret < 0, "_stricmp returned %d\n", ret);
2521 ret = _stricmp("z", "a");
2522 ok(ret > 0, "_stricmp returned %d\n", ret);
2523 ret = _stricmp("\xa5", "\xb9");
2524 ok(ret == 0, "_stricmp returned %d\n", ret);
2526 setlocale(LC_ALL, "C");
2529 static void test__wcstoi64(void)
2531 static const WCHAR digit[] = { '9', 0 };
2532 static const WCHAR stock[] = { 0x3231, 0 }; /* PARENTHESIZED IDEOGRAPH STOCK */
2533 static const WCHAR tamil[] = { 0x0bef, 0 }; /* TAMIL DIGIT NINE */
2534 static const WCHAR thai[] = { 0x0e59, 0 }; /* THAI DIGIT NINE */
2535 static const WCHAR fullwidth[] = { 0xff19, 0 }; /* FULLWIDTH DIGIT NINE */
2536 static const WCHAR hex[] = { 0xff19, 'f', 0x0e59, 0xff46, 0 };
2538 __int64 res;
2539 unsigned __int64 ures;
2540 WCHAR *endpos;
2542 if (!p_wcstoi64 || !p_wcstoui64) {
2543 win_skip("_wcstoi64 or _wcstoui64 not found\n");
2544 return;
2547 res = p_wcstoi64(digit, NULL, 10);
2548 ok(res == 9, "res != 9\n");
2549 res = p_wcstoi64(stock, &endpos, 10);
2550 ok(res == 0, "res != 0\n");
2551 ok(endpos == stock, "Incorrect endpos (%p-%p)\n", stock, endpos);
2552 res = p_wcstoi64(tamil, &endpos, 10);
2553 ok(res == 0, "res != 0\n");
2554 ok(endpos == tamil, "Incorrect endpos (%p-%p)\n", tamil, endpos);
2555 res = p_wcstoi64(thai, NULL, 10);
2556 todo_wine ok(res == 9, "res != 9\n");
2557 res = p_wcstoi64(fullwidth, NULL, 10);
2558 todo_wine ok(res == 9, "res != 9\n");
2559 res = p_wcstoi64(hex, NULL, 16);
2560 todo_wine ok(res == 0x9f9, "res != 0x9f9\n");
2562 ures = p_wcstoui64(digit, NULL, 10);
2563 ok(ures == 9, "ures != 9\n");
2564 ures = p_wcstoui64(stock, &endpos, 10);
2565 ok(ures == 0, "ures != 0\n");
2566 ok(endpos == stock, "Incorrect endpos (%p-%p)\n", stock, endpos);
2567 ures = p_wcstoui64(tamil, &endpos, 10);
2568 ok(ures == 0, "ures != 0\n");
2569 ok(endpos == tamil, "Incorrect endpos (%p-%p)\n", tamil, endpos);
2570 ures = p_wcstoui64(thai, NULL, 10);
2571 todo_wine ok(ures == 9, "ures != 9\n");
2572 ures = p_wcstoui64(fullwidth, NULL, 10);
2573 todo_wine ok(ures == 9, "ures != 9\n");
2574 ures = p_wcstoui64(hex, NULL, 16);
2575 todo_wine ok(ures == 0x9f9, "ures != 0x9f9\n");
2577 return;
2580 static void test_atoi(void)
2582 int r;
2584 r = atoi("0");
2585 ok(r == 0, "atoi(0) = %d\n", r);
2587 r = atoi("-1");
2588 ok(r == -1, "atoi(-1) = %d\n", r);
2590 r = atoi("1");
2591 ok(r == 1, "atoi(1) = %d\n", r);
2593 r = atoi("4294967296");
2594 ok(r == 0, "atoi(4294967296) = %d\n", r);
2597 static void test_strncpy(void)
2599 #define TEST_STRNCPY_LEN 10
2600 char *ret;
2601 char dst[TEST_STRNCPY_LEN + 1];
2602 char not_null_terminated[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0'};
2604 /* strlen(src) > TEST_STRNCPY_LEN */
2605 ret = strncpy(dst, "01234567890123456789", TEST_STRNCPY_LEN);
2606 ok(ret == dst, "ret != dst\n");
2607 ok(!strncmp(dst, "0123456789", TEST_STRNCPY_LEN), "dst != 0123456789\n");
2609 /* without null-terminated */
2610 ret = strncpy(dst, not_null_terminated, TEST_STRNCPY_LEN);
2611 ok(ret == dst, "ret != dst\n");
2612 ok(!strncmp(dst, "0123456789", TEST_STRNCPY_LEN), "dst != 0123456789\n");
2614 /* strlen(src) < TEST_STRNCPY_LEN */
2615 strcpy(dst, "0123456789");
2616 ret = strncpy(dst, "012345", TEST_STRNCPY_LEN);
2617 ok(ret == dst, "ret != dst\n");
2618 ok(!strcmp(dst, "012345"), "dst != 012345\n");
2619 ok(dst[TEST_STRNCPY_LEN - 1] == '\0', "dst[TEST_STRNCPY_LEN - 1] != 0\n");
2621 /* strlen(src) == TEST_STRNCPY_LEN */
2622 ret = strncpy(dst, "0123456789", TEST_STRNCPY_LEN);
2623 ok(ret == dst, "ret != dst\n");
2624 ok(!strncmp(dst, "0123456789", TEST_STRNCPY_LEN), "dst != 0123456789\n");
2627 START_TEST(string)
2629 char mem[100];
2630 static const char xilstring[]="c:/xilinx";
2631 int nLen;
2633 hMsvcrt = GetModuleHandleA("msvcrt.dll");
2634 if (!hMsvcrt)
2635 hMsvcrt = GetModuleHandleA("msvcrtd.dll");
2636 ok(hMsvcrt != 0, "GetModuleHandleA failed\n");
2637 SET(pmemcpy,"memcpy");
2638 p_memcpy_s = (void*)GetProcAddress( hMsvcrt, "memcpy_s" );
2639 p_memmove_s = (void*)GetProcAddress( hMsvcrt, "memmove_s" );
2640 SET(pmemcmp,"memcmp");
2641 SET(p_mbctype,"_mbctype");
2642 SET(p__mb_cur_max,"__mb_cur_max");
2643 pstrcpy_s = (void *)GetProcAddress( hMsvcrt,"strcpy_s" );
2644 pstrcat_s = (void *)GetProcAddress( hMsvcrt,"strcat_s" );
2645 p_mbsnbcat_s = (void *)GetProcAddress( hMsvcrt,"_mbsnbcat_s" );
2646 p_mbsnbcpy_s = (void *)GetProcAddress( hMsvcrt,"_mbsnbcpy_s" );
2647 p_wcscpy_s = (void *)GetProcAddress( hMsvcrt,"wcscpy_s" );
2648 p_wcsncpy_s = (void *)GetProcAddress( hMsvcrt,"wcsncpy_s" );
2649 p_wcsncat_s = (void *)GetProcAddress( hMsvcrt,"wcsncat_s" );
2650 p_wcsupr_s = (void *)GetProcAddress( hMsvcrt,"_wcsupr_s" );
2651 p_strnlen = (void *)GetProcAddress( hMsvcrt,"strnlen" );
2652 p_strtoi64 = (void *)GetProcAddress(hMsvcrt, "_strtoi64");
2653 p_strtoui64 = (void *)GetProcAddress(hMsvcrt, "_strtoui64");
2654 p_wcstoi64 = (void *)GetProcAddress(hMsvcrt, "_wcstoi64");
2655 p_wcstoui64 = (void *)GetProcAddress(hMsvcrt, "_wcstoui64");
2656 pmbstowcs_s = (void *)GetProcAddress(hMsvcrt, "mbstowcs_s");
2657 pwcstombs_s = (void *)GetProcAddress(hMsvcrt, "wcstombs_s");
2658 pwcsrtombs = (void *)GetProcAddress(hMsvcrt, "wcsrtombs");
2659 p_gcvt_s = (void *)GetProcAddress(hMsvcrt, "_gcvt_s");
2660 p_itoa_s = (void *)GetProcAddress(hMsvcrt, "_itoa_s");
2661 p_strlwr_s = (void *)GetProcAddress(hMsvcrt, "_strlwr_s");
2662 p_ultoa_s = (void *)GetProcAddress(hMsvcrt, "_ultoa_s");
2663 p_set_invalid_parameter_handler = (void *) GetProcAddress(hMsvcrt, "_set_invalid_parameter_handler");
2664 p_wcslwr_s = (void*)GetProcAddress(hMsvcrt, "_wcslwr_s");
2665 p_mbsupr_s = (void*)GetProcAddress(hMsvcrt, "_mbsupr_s");
2666 p_mbslwr_s = (void*)GetProcAddress(hMsvcrt, "_mbslwr_s");
2667 p_wctob = (void*)GetProcAddress(hMsvcrt, "wctob");
2668 p_wcrtomb = (void*)GetProcAddress(hMsvcrt, "wcrtomb");
2669 p_tolower = (void*)GetProcAddress(hMsvcrt, "tolower");
2670 p_mbrlen = (void*)GetProcAddress(hMsvcrt, "mbrlen");
2671 p_mbrtowc = (void*)GetProcAddress(hMsvcrt, "mbrtowc");
2672 p_mbsrtowcs = (void*)GetProcAddress(hMsvcrt, "mbsrtowcs");
2673 p__atodbl_l = (void*)GetProcAddress(hMsvcrt, "_atodbl_l");
2675 /* MSVCRT memcpy behaves like memmove for overlapping moves,
2676 MFC42 CString::Insert seems to rely on that behaviour */
2677 strcpy(mem,xilstring);
2678 nLen=strlen(xilstring);
2679 pmemcpy(mem+5, mem,nLen+1);
2680 ok(pmemcmp(mem+5,xilstring, nLen) == 0,
2681 "Got result %s\n",mem+5);
2683 /* Test _swab function */
2684 test_swab();
2686 /* Test ismbblead*/
2687 test_mbcp();
2688 /* test _mbsspn */
2689 test_mbsspn();
2690 test_mbsspnp();
2691 /* test _strdup */
2692 test_strdup();
2693 test_strcpy_s();
2694 test_memcpy_s();
2695 test_memmove_s();
2696 test_strcat_s();
2697 test__mbsnbcpy_s();
2698 test_mbcjisjms();
2699 test_mbcjmsjis();
2700 test_mbbtombc();
2701 test_mbctombb();
2702 test_ismbclegal();
2703 test_strtok();
2704 test__mbstok();
2705 test_wcscpy_s();
2706 test__wcsupr_s();
2707 test_strtol();
2708 test_strnlen();
2709 test__strtoi64();
2710 test__strtod();
2711 test_mbstowcs();
2712 test_gcvt();
2713 test__itoa_s();
2714 test__strlwr_s();
2715 test_wcsncat_s();
2716 test__mbsnbcat_s();
2717 test__ultoa_s();
2718 test__wcslwr_s();
2719 test__mbsupr_s();
2720 test__mbslwr_s();
2721 test_wctob();
2722 test_wctomb();
2723 test_tolower();
2724 test__atodbl();
2725 test__stricmp();
2726 test__wcstoi64();
2727 test_atoi();
2728 test_strncpy();