Merge the following revisions from kernel-fun branch:
[reactos.git] / rostests / winetests / msvcrt / string.c
1 /*
2 * Unit test suite for string functions.
3 *
4 * Copyright 2004 Uwe Bonnes
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #include "wine/test.h"
22 #include <string.h>
23 #include <mbstring.h>
24 #include <wchar.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <mbctype.h>
28 #include <locale.h>
29 #include <errno.h>
30 #include <limits.h>
31 #include <math.h>
32
33 /* make it use a definition from string.h */
34 #undef strncpy
35 #include "winbase.h"
36 #include "winnls.h"
37
38 static char *buf_to_string(const unsigned char *bin, int len, int nr)
39 {
40 static char buf[2][1024];
41 char *w = buf[nr];
42 int i;
43
44 for (i = 0; i < len; i++)
45 {
46 sprintf(w, "%02x ", (unsigned char)bin[i]);
47 w += strlen(w);
48 }
49 return buf[nr];
50 }
51
52 #define expect_eq(expr, value, type, format) { type ret = (expr); ok((value) == ret, #expr " expected " format " got " format "\n", value, ret); }
53 #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)); }
54
55 static void* (__cdecl *pmemcpy)(void *, const void *, size_t n);
56 static int (__cdecl *p_memcpy_s)(void *, size_t, const void *, size_t);
57 static int (__cdecl *p_memmove_s)(void *, size_t, const void *, size_t);
58 static int* (__cdecl *pmemcmp)(void *, const void *, size_t n);
59 static int (__cdecl *pstrcpy_s)(char *dst, size_t len, const char *src);
60 static int (__cdecl *pstrcat_s)(char *dst, size_t len, const char *src);
61 static int (__cdecl *p_mbsnbcat_s)(unsigned char *dst, size_t size, const unsigned char *src, size_t count);
62 static int (__cdecl *p_mbsnbcpy_s)(unsigned char * dst, size_t size, const unsigned char * src, size_t count);
63 static int (__cdecl *p__mbscpy_s)(unsigned char*, size_t, const unsigned char*);
64 static int (__cdecl *p_wcscpy_s)(wchar_t *wcDest, size_t size, const wchar_t *wcSrc);
65 static int (__cdecl *p_wcsncpy_s)(wchar_t *wcDest, size_t size, const wchar_t *wcSrc, size_t count);
66 static int (__cdecl *p_wcsncat_s)(wchar_t *dst, size_t elem, const wchar_t *src, size_t count);
67 static int (__cdecl *p_wcsupr_s)(wchar_t *str, size_t size);
68 static size_t (__cdecl *p_strnlen)(const char *, size_t);
69 static __int64 (__cdecl *p_strtoi64)(const char *, char **, int);
70 static unsigned __int64 (__cdecl *p_strtoui64)(const char *, char **, int);
71 static __int64 (__cdecl *p_wcstoi64)(const wchar_t *, wchar_t **, int);
72 static unsigned __int64 (__cdecl *p_wcstoui64)(const wchar_t *, wchar_t **, int);
73 static int (__cdecl *pwcstombs_s)(size_t*,char*,size_t,const wchar_t*,size_t);
74 static int (__cdecl *pmbstowcs_s)(size_t*,wchar_t*,size_t,const char*,size_t);
75 static size_t (__cdecl *p_mbsrtowcs)(wchar_t*, const char**, size_t, mbstate_t*);
76 static size_t (__cdecl *pwcsrtombs)(char*, const wchar_t**, size_t, int*);
77 static errno_t (__cdecl *p_gcvt_s)(char*,size_t,double,int);
78 static errno_t (__cdecl *p_itoa_s)(int,char*,size_t,int);
79 static errno_t (__cdecl *p_strlwr_s)(char*,size_t);
80 static errno_t (__cdecl *p_ultoa_s)(__msvcrt_ulong,char*,size_t,int);
81 static int *p__mb_cur_max;
82 static unsigned char *p_mbctype;
83 static int (__cdecl *p_wcslwr_s)(wchar_t*,size_t);
84 static errno_t (__cdecl *p_mbsupr_s)(unsigned char *str, size_t numberOfElements);
85 static errno_t (__cdecl *p_mbslwr_s)(unsigned char *str, size_t numberOfElements);
86 static int (__cdecl *p_wctob)(wint_t);
87 static size_t (__cdecl *p_wcrtomb)(char*, wchar_t, mbstate_t*);
88 static int (__cdecl *p_tolower)(int);
89 static size_t (__cdecl *p_mbrlen)(const char*, size_t, mbstate_t*);
90 static size_t (__cdecl *p_mbrtowc)(wchar_t*, const char*, size_t, mbstate_t*);
91 static int (__cdecl *p__atodbl_l)(_CRT_DOUBLE*,char*,_locale_t);
92 static double (__cdecl *p__atof_l)(const char*,_locale_t);
93 static double (__cdecl *p__strtod_l)(const char *,char**,_locale_t);
94 static int (__cdecl *p__strnset_s)(char*,size_t,int,size_t);
95 static int (__cdecl *p__wcsset_s)(wchar_t*,size_t,wchar_t);
96
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)
99
100 static HMODULE hMsvcrt;
101
102 static void test_swab( void ) {
103 char original[] = "BADCFEHGJILKNMPORQTSVUXWZY@#";
104 char expected1[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ@#";
105 char expected2[] = "ABCDEFGHIJKLMNOPQRSTUVWX$";
106 char expected3[] = "$";
107
108 char from[30];
109 char to[30];
110
111 int testsize;
112
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);
120
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);
128
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);
136
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);
144 }
145
146 #if 0 /* use this to generate more tests */
147
148 static void test_codepage(int cp)
149 {
150 int i;
151 int prev;
152 int count = 1;
153
154 ok(_setmbcp(cp) == 0, "Couldn't set mbcp\n");
155
156 prev = p_mbctype[0];
157 printf("static int result_cp_%d_mbctype[] = { ", cp);
158 for (i = 1; i < 257; i++)
159 {
160 if (p_mbctype[i] != prev)
161 {
162 printf("0x%x,%d, ", prev, count);
163 prev = p_mbctype[i];
164 count = 1;
165 }
166 else
167 count++;
168 }
169 printf("0x%x,%d };\n", prev, count);
170 }
171
172 #else
173
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 };
183
184 static void test_cp_table(int cp, int *result)
185 {
186 int i;
187 int count = 0;
188 int curr = 0;
189 _setmbcp(cp);
190 for (i = 0; i < 256; i++)
191 {
192 if (count == 0)
193 {
194 curr = result[0];
195 count = result[1];
196 result += 2;
197 }
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--;
200 }
201 }
202
203 #define test_codepage(num) test_cp_table(num, result_cp_##num##_mbctype);
204
205 #endif
206
207 static void test_mbcp(void)
208 {
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;
217
218 /* _mbtype tests */
219
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");
235
236 /* double byte code pages */
237 test_codepage(932);
238 test_codepage(936);
239 test_codepage(949);
240 test_codepage(950);
241
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");
250
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");
261
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");
266
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");
277
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");
284
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");
295
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");
302
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 */
307
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 */
315
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");
328 }
329
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);
350 }
351 setlocale(LC_ALL, "C");
352
353 /* _mbccpy/_mbsncpy */
354 memset(buf, 0xff, sizeof(buf));
355 _mbccpy(buf, mbstring);
356 expect_bin(buf, "\xb0\xb1\xff", 3);
357
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);
376
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);
387
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);
393
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);
412
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)
416 */
417 GetCPInfo(GetACP(), &cp_info);
418 if (cp_info.MaxCharSize == 1)
419 {
420 expect_eq(mblen((char *)mbstring, 3), 1, int, "%x");
421 expect_eq(_mbstrlen((char *)mbstring2), 7, int, "%d");
422 }
423 else
424 skip("Current locale has double-byte charset - could lead to false positives\n");
425
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");
431 }
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");
437
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");
446
447 _setmbcp(curr_mbcp);
448 }
449
450 static void test_mbsspn( void)
451 {
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);
463 }
464
465 static void test_mbsspnp( void)
466 {
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);
481 }
482
483 static void test_strdup(void)
484 {
485 char *str;
486 str = _strdup( 0 );
487 ok( str == 0, "strdup returns %s should be 0\n", str);
488 free( str );
489 }
490
491 static void test_strcpy_s(void)
492 {
493 char dest[8];
494 const char *small = "small";
495 const char *big = "atoolongstringforthislittledestination";
496 int ret;
497
498 if(!pstrcpy_s)
499 {
500 win_skip("strcpy_s not found\n");
501 return;
502 }
503
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]);
511
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]);
525
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]);
533
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]);
541
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);
544 }
545
546 #define NUMELMS(array) (sizeof(array)/sizeof((array)[0]))
547
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])
553
554 static void test_memcpy_s(void)
555 {
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 win_skip("memcpy_s not found\n");
562 return;
563 }
564
565 /* Normal */
566 memset(dest, 'X', sizeof(dest));
567 ret = p_memcpy_s(dest, NUMELMS(dest), tiny, NUMELMS(tiny));
568 ok(ret == 0, "Copying a buffer into a big enough destination returned %d, expected 0\n", ret);
569 okchars(dest, tiny[0], tiny[1], tiny[2], tiny[3], tiny[4], tiny[5], 'X', 'X');
570
571 /* Vary source size */
572 errno = 0xdeadbeef;
573 memset(dest, 'X', sizeof(dest));
574 ret = p_memcpy_s(dest, NUMELMS(dest), big, NUMELMS(big));
575 ok(ret == ERANGE, "Copying a big buffer to a small destination returned %d, expected ERANGE\n", ret);
576 ok(errno == ERANGE, "errno is %d, expected ERANGE\n", errno);
577 okchars(dest, 0, 0, 0, 0, 0, 0, 0, 0);
578
579 /* Replace source with NULL */
580 errno = 0xdeadbeef;
581 memset(dest, 'X', sizeof(dest));
582 ret = p_memcpy_s(dest, NUMELMS(dest), NULL, NUMELMS(tiny));
583 ok(ret == EINVAL, "Copying a NULL source buffer returned %d, expected EINVAL\n", ret);
584 ok(errno == EINVAL, "errno is %d, expected EINVAL\n", errno);
585 okchars(dest, 0, 0, 0, 0, 0, 0, 0, 0);
586
587 /* Vary dest size */
588 errno = 0xdeadbeef;
589 memset(dest, 'X', sizeof(dest));
590 ret = p_memcpy_s(dest, 0, tiny, NUMELMS(tiny));
591 ok(ret == ERANGE, "Copying into a destination of size 0 returned %d, expected ERANGE\n", ret);
592 ok(errno == ERANGE, "errno is %d, expected ERANGE\n", errno);
593 okchars(dest, 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X');
594
595 /* Replace dest with NULL */
596 errno = 0xdeadbeef;
597 ret = p_memcpy_s(NULL, NUMELMS(dest), tiny, NUMELMS(tiny));
598 ok(ret == EINVAL, "Copying a tiny buffer to a big NULL destination returned %d, expected EINVAL\n", ret);
599 ok(errno == EINVAL, "errno is %d, expected EINVAL\n", errno);
600
601 /* Combinations */
602 errno = 0xdeadbeef;
603 memset(dest, 'X', sizeof(dest));
604 ret = p_memcpy_s(dest, 0, NULL, NUMELMS(tiny));
605 ok(ret == EINVAL, "Copying a NULL buffer into a destination of size 0 returned %d, expected EINVAL\n", ret);
606 ok(errno == EINVAL, "errno is %d, expected EINVAL\n", errno);
607 okchars(dest, 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X');
608 }
609
610 static void test_memmove_s(void)
611 {
612 static char dest[8];
613 static const char tiny[] = {'T',0,'I','N','Y',0};
614 static const char big[] = {'a','t','o','o','l','o','n','g','s','t','r','i','n','g',0};
615 int ret;
616 if (!p_memmove_s) {
617 win_skip("memmove_s not found\n");
618 return;
619 }
620
621 /* Normal */
622 memset(dest, 'X', sizeof(dest));
623 ret = p_memmove_s(dest, NUMELMS(dest), tiny, NUMELMS(tiny));
624 ok(ret == 0, "Moving a buffer into a big enough destination returned %d, expected 0\n", ret);
625 okchars(dest, tiny[0], tiny[1], tiny[2], tiny[3], tiny[4], tiny[5], 'X', 'X');
626
627 /* Overlapping */
628 memcpy(dest, big, sizeof(dest));
629 ret = p_memmove_s(dest+1, NUMELMS(dest)-1, dest, NUMELMS(dest)-1);
630 ok(ret == 0, "Moving a buffer up one char returned %d, expected 0\n", ret);
631 okchars(dest, big[0], big[0], big[1], big[2], big[3], big[4], big[5], big[6]);
632
633 /* Vary source size */
634 errno = 0xdeadbeef;
635 memset(dest, 'X', sizeof(dest));
636 ret = p_memmove_s(dest, NUMELMS(dest), big, NUMELMS(big));
637 ok(ret == ERANGE, "Moving a big buffer to a small destination returned %d, expected ERANGE\n", ret);
638 ok(errno == ERANGE, "errno is %d, expected ERANGE\n", errno);
639 okchars(dest, 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X');
640
641 /* Replace source with NULL */
642 errno = 0xdeadbeef;
643 memset(dest, 'X', sizeof(dest));
644 ret = p_memmove_s(dest, NUMELMS(dest), NULL, NUMELMS(tiny));
645 ok(ret == EINVAL, "Moving a NULL source buffer returned %d, expected EINVAL\n", ret);
646 ok(errno == EINVAL, "errno is %d, expected EINVAL\n", errno);
647 okchars(dest, 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X');
648
649 /* Vary dest size */
650 errno = 0xdeadbeef;
651 memset(dest, 'X', sizeof(dest));
652 ret = p_memmove_s(dest, 0, tiny, NUMELMS(tiny));
653 ok(ret == ERANGE, "Moving into a destination of size 0 returned %d, expected ERANGE\n", ret);
654 ok(errno == ERANGE, "errno is %d, expected ERANGE\n", errno);
655 okchars(dest, 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X');
656
657 /* Replace dest with NULL */
658 errno = 0xdeadbeef;
659 ret = p_memmove_s(NULL, NUMELMS(dest), tiny, NUMELMS(tiny));
660 ok(ret == EINVAL, "Moving a tiny buffer to a big NULL destination returned %d, expected EINVAL\n", ret);
661 ok(errno == EINVAL, "errno is %d, expected EINVAL\n", errno);
662
663 /* Combinations */
664 errno = 0xdeadbeef;
665 memset(dest, 'X', sizeof(dest));
666 ret = p_memmove_s(dest, 0, NULL, NUMELMS(tiny));
667 ok(ret == EINVAL, "Moving a NULL buffer into a destination of size 0 returned %d, expected EINVAL\n", ret);
668 ok(errno == EINVAL, "errno is %d, expected EINVAL\n", errno);
669 okchars(dest, 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X');
670 }
671
672 static void test_strcat_s(void)
673 {
674 char dest[8];
675 const char *small = "sma";
676 int ret;
677
678 if(!pstrcat_s)
679 {
680 win_skip("strcat_s not found\n");
681 return;
682 }
683
684 memset(dest, 'X', sizeof(dest));
685 dest[0] = '\0';
686 ret = pstrcat_s(dest, sizeof(dest), small);
687 ok(ret == 0, "strcat_s: Copying a string into a big enough destination returned %d, expected 0\n", ret);
688 ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == '\0'&&
689 dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
690 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
691 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
692 ret = pstrcat_s(dest, sizeof(dest), small);
693 ok(ret == 0, "strcat_s: Attaching a string to a big enough destination returned %d, expected 0\n", ret);
694 ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == 's' &&
695 dest[4] == 'm' && dest[5] == 'a' && dest[6] == '\0'&& dest[7] == 'X',
696 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
697 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
698
699 ret = pstrcat_s(dest, sizeof(dest), small);
700 ok(ret == ERANGE, "strcat_s: Attaching a string to a filled up destination returned %d, expected ERANGE\n", ret);
701 ok(dest[0] == '\0'&& dest[1] == 'm' && dest[2] == 'a' && dest[3] == 's' &&
702 dest[4] == 'm' && dest[5] == 'a' && dest[6] == 's' && dest[7] == 'm',
703 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
704 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
705
706 memset(dest, 'X', sizeof(dest));
707 dest[0] = 'a';
708 dest[1] = '\0';
709
710 ret = pstrcat_s(dest, 0, small);
711 ok(ret == EINVAL, "strcat_s: Source len = 0 returned %d, expected EINVAL\n", ret);
712 ok(dest[0] == 'a' && dest[1] == '\0'&& dest[2] == 'X' && dest[3] == 'X' &&
713 dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
714 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
715 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
716
717 ret = pstrcat_s(dest, 0, NULL);
718 ok(ret == EINVAL, "strcat_s: len = 0 and src = NULL returned %d, expected EINVAL\n", ret);
719 ok(dest[0] == 'a' && dest[1] == '\0'&& dest[2] == 'X' && dest[3] == 'X' &&
720 dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
721 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
722 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
723
724 ret = pstrcat_s(dest, sizeof(dest), NULL);
725 ok(ret == EINVAL, "strcat_s: Sourcing from NULL returned %d, expected EINVAL\n", ret);
726 ok(dest[0] == '\0'&& dest[1] == '\0'&& dest[2] == 'X' && dest[3] == 'X' &&
727 dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
728 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
729 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
730
731 ret = pstrcat_s(NULL, sizeof(dest), small);
732 ok(ret == EINVAL, "strcat_s: Writing to a NULL string returned %d, expected EINVAL\n", ret);
733 }
734
735 static void test__mbsnbcpy_s(void)
736 {
737 unsigned char dest[8];
738 const unsigned char big[] = "atoolongstringforthislittledestination";
739 const unsigned char small[] = "small";
740 int ret;
741
742 if(!p_mbsnbcpy_s)
743 {
744 win_skip("_mbsnbcpy_s not found\n");
745 return;
746 }
747
748 memset(dest, 'X', sizeof(dest));
749 ret = p_mbsnbcpy_s(dest, sizeof(dest), small, sizeof(small));
750 ok(ret == 0, "_mbsnbcpy_s: Copying a string into a big enough destination returned %d, expected 0\n", ret);
751 ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == 'l' &&
752 dest[4] == 'l' && dest[5] == '\0'&& dest[6] == 'X' && dest[7] == 'X',
753 "Unexpected return data from _mbsnbcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
754 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
755
756 /* WTF? */
757 memset(dest, 'X', sizeof(dest));
758 ret = p_mbsnbcpy_s(dest, sizeof(dest) - 2, big, sizeof(small));
759 ok(ret == ERANGE, "_mbsnbcpy_s: Copying a too long string returned %d, expected ERANGE\n", ret);
760 ok(dest[0] == '\0'&& dest[1] == 't' && dest[2] == 'o' && dest[3] == 'o' &&
761 dest[4] == 'l' && dest[5] == 'o' && dest[6] == 'X' && dest[7] == 'X',
762 "Unexpected return data from _mbsnbcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
763 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
764
765 memset(dest, 'X', sizeof(dest));
766 ret = p_mbsnbcpy_s(dest, sizeof(dest) - 2, big, 4);
767 ok(ret == 0, "_mbsnbcpy_s: Copying a too long string with a count cap returned %d, expected 0\n", ret);
768 ok(dest[0] == 'a' && dest[1] == 't' && dest[2] == 'o' && dest[3] == 'o' &&
769 dest[4] == '\0'&& dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
770 "Unexpected return data from _mbsnbcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
771 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
772
773 memset(dest, 'X', sizeof(dest));
774 ret = p_mbsnbcpy_s(dest, sizeof(dest) - 2, small, sizeof(small) + 10);
775 ok(ret == 0, "_mbsnbcpy_s: Copying more data than the source string len returned %d, expected 0\n", ret);
776 ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == 'l' &&
777 dest[4] == 'l' && dest[5] == '\0'&& 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]);
780 }
781
782 static void test__mbscpy_s(void)
783 {
784 const unsigned char src[] = "source string";
785 unsigned char dest[16];
786 int ret;
787
788 if(!p__mbscpy_s)
789 {
790 win_skip("_mbscpy_s not found\n");
791 return;
792 }
793
794 ret = p__mbscpy_s(NULL, 0, src);
795 ok(ret == EINVAL, "got %d\n", ret);
796 ret = p__mbscpy_s(NULL, sizeof(dest), src);
797 ok(ret == EINVAL, "got %d\n", ret);
798 ret = p__mbscpy_s(dest, 0, src);
799 ok(ret == EINVAL, "got %d\n", ret);
800 dest[0] = 'x';
801 ret = p__mbscpy_s(dest, sizeof(dest), NULL);
802 ok(ret == EINVAL, "got %d\n", ret);
803 ok(!dest[0], "dest buffer was not modified on invalid argument\n");
804
805 memset(dest, 'X', sizeof(dest));
806 ret = p__mbscpy_s(dest, sizeof(dest), src);
807 ok(!ret, "got %d\n", ret);
808 ok(!memcmp(dest, src, sizeof(src)), "dest = %s\n", dest);
809 ok(dest[sizeof(src)] == 'X', "unused part of buffer was modified\n");
810
811 memset(dest, 'X', sizeof(dest));
812 ret = p__mbscpy_s(dest, 4, src);
813 ok(ret == ERANGE, "got %d\n", ret);
814 ok(!dest[0], "incorrect dest buffer (%d)\n", dest[0]);
815 ok(dest[1] == src[1], "incorrect dest buffer (%d)\n", dest[1]);
816 }
817
818 static void test_wcscpy_s(void)
819 {
820 static const WCHAR szLongText[] = { 'T','h','i','s','A','L','o','n','g','s','t','r','i','n','g',0 };
821 static WCHAR szDest[18];
822 static WCHAR szDestShort[8];
823 int ret;
824
825 if(!p_wcscpy_s)
826 {
827 win_skip("wcscpy_s not found\n");
828 return;
829 }
830
831 /* Test NULL Dest */
832 errno = EBADF;
833 ret = p_wcscpy_s(NULL, 18, szLongText);
834 ok(ret == EINVAL, "p_wcscpy_s expect EINVAL got %d\n", ret);
835 ok(errno == EINVAL, "expected errno EINVAL got %d\n", errno);
836
837 /* Test NULL Source */
838 errno = EBADF;
839 szDest[0] = 'A';
840 ret = p_wcscpy_s(szDest, 18, NULL);
841 ok(ret == EINVAL, "expected EINVAL got %d\n", ret);
842 ok(errno == EINVAL, "expected errno EINVAL got %d\n", errno);
843 ok(szDest[0] == 0, "szDest[0] not 0, got %c\n", szDest[0]);
844
845 /* Test invalid size */
846 errno = EBADF;
847 szDest[0] = 'A';
848 ret = p_wcscpy_s(szDest, 0, szLongText);
849 /* Later versions changed the return value for this case to EINVAL,
850 * and don't modify the result if the dest size is 0.
851 */
852 ok(ret == ERANGE || ret == EINVAL, "expected ERANGE/EINVAL got %d\n", ret);
853 ok(errno == ERANGE || errno == EINVAL, "expected errno ERANGE/EINVAL got %d\n", errno);
854 ok(szDest[0] == 0 || ret == EINVAL, "szDest[0] not 0\n");
855
856 /* Copy same buffer size */
857 ret = p_wcscpy_s(szDest, 18, szLongText);
858 ok(ret == 0, "expected 0 got %d\n", ret);
859 ok(lstrcmpW(szDest, szLongText) == 0, "szDest != szLongText\n");
860
861 /* Copy smaller buffer size */
862 errno = EBADF;
863 szDest[0] = 'A';
864 ret = p_wcscpy_s(szDestShort, 8, szLongText);
865 ok(ret == ERANGE || ret == EINVAL, "expected ERANGE/EINVAL got %d\n", ret);
866 ok(errno == ERANGE || errno == EINVAL, "expected errno ERANGE/EINVAL got %d\n", errno);
867 ok(szDestShort[0] == 0, "szDestShort[0] not 0\n");
868
869 if(!p_wcsncpy_s)
870 {
871 win_skip("wcsncpy_s not found\n");
872 return;
873 }
874
875 ret = p_wcsncpy_s(NULL, 18, szLongText, sizeof(szLongText)/sizeof(WCHAR));
876 ok(ret == EINVAL, "p_wcsncpy_s expect EINVAL got %d\n", ret);
877
878 szDest[0] = 'A';
879 ret = p_wcsncpy_s(szDest, 18, NULL, 1);
880 ok(ret == EINVAL, "expected EINVAL got %d\n", ret);
881 ok(szDest[0] == 0, "szDest[0] not 0\n");
882
883 szDest[0] = 'A';
884 ret = p_wcsncpy_s(szDest, 18, NULL, 0);
885 ok(ret == 0, "expected ERROR_SUCCESS got %d\n", ret);
886 ok(szDest[0] == 0, "szDest[0] not 0\n");
887
888 szDest[0] = 'A';
889 ret = p_wcsncpy_s(szDest, 0, szLongText, sizeof(szLongText)/sizeof(WCHAR));
890 ok(ret == ERANGE || ret == EINVAL, "expected ERANGE/EINVAL got %d\n", ret);
891 ok(szDest[0] == 0 || ret == EINVAL, "szDest[0] not 0\n");
892
893 ret = p_wcsncpy_s(szDest, 18, szLongText, sizeof(szLongText)/sizeof(WCHAR));
894 ok(ret == 0, "expected 0 got %d\n", ret);
895 ok(lstrcmpW(szDest, szLongText) == 0, "szDest != szLongText\n");
896
897 szDest[0] = 'A';
898 ret = p_wcsncpy_s(szDestShort, 8, szLongText, sizeof(szLongText)/sizeof(WCHAR));
899 ok(ret == ERANGE || ret == EINVAL, "expected ERANGE/EINVAL got %d\n", ret);
900 ok(szDestShort[0] == 0, "szDestShort[0] not 0\n");
901
902 szDest[0] = 'A';
903 ret = p_wcsncpy_s(szDest, 5, szLongText, -1);
904 ok(ret == STRUNCATE, "expected STRUNCATE got %d\n", ret);
905 ok(szDest[4] == 0, "szDest[4] not 0\n");
906 ok(!memcmp(szDest, szLongText, 4*sizeof(WCHAR)), "szDest = %s\n", wine_dbgstr_w(szDest));
907
908 ret = p_wcsncpy_s(NULL, 0, (void*)0xdeadbeef, 0);
909 ok(ret == 0, "ret = %d\n", ret);
910
911 szDestShort[0] = '1';
912 szDestShort[1] = 0;
913 ret = p_wcsncpy_s(szDestShort+1, 4, szDestShort, -1);
914 ok(ret == STRUNCATE, "expected ERROR_SUCCESS got %d\n", ret);
915 ok(szDestShort[0]=='1' && szDestShort[1]=='1' && szDestShort[2]=='1' && szDestShort[3]=='1',
916 "szDestShort = %s\n", wine_dbgstr_w(szDestShort));
917 }
918
919 static void test__wcsupr_s(void)
920 {
921 static const WCHAR mixedString[] = {'M', 'i', 'X', 'e', 'D', 'l', 'o', 'w',
922 'e', 'r', 'U', 'P', 'P', 'E', 'R', 0};
923 static const WCHAR expectedString[] = {'M', 'I', 'X', 'E', 'D', 'L', 'O',
924 'W', 'E', 'R', 'U', 'P', 'P', 'E',
925 'R', 0};
926 WCHAR testBuffer[2*sizeof(mixedString)/sizeof(WCHAR)];
927 int ret;
928
929 if (!p_wcsupr_s)
930 {
931 win_skip("_wcsupr_s not found\n");
932 return;
933 }
934
935 /* Test NULL input string and invalid size. */
936 errno = EBADF;
937 ret = p_wcsupr_s(NULL, 0);
938 ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
939 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
940
941 /* Test NULL input string and valid size. */
942 errno = EBADF;
943 ret = p_wcsupr_s(NULL, sizeof(testBuffer)/sizeof(WCHAR));
944 ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
945 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
946
947 /* Test empty string with zero size. */
948 errno = EBADF;
949 testBuffer[0] = '\0';
950 ret = p_wcsupr_s(testBuffer, 0);
951 ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
952 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
953 ok(testBuffer[0] == '\0', "Expected the buffer to be unchanged\n");
954
955 /* Test empty string with size of one. */
956 testBuffer[0] = '\0';
957 ret = p_wcsupr_s(testBuffer, 1);
958 ok(ret == 0, "Expected _wcsupr_s to succeed, got %d\n", ret);
959 ok(testBuffer[0] == '\0', "Expected the buffer to be unchanged\n");
960
961 /* Test one-byte buffer with zero size. */
962 errno = EBADF;
963 testBuffer[0] = 'x';
964 ret = p_wcsupr_s(testBuffer, 0);
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");
968
969 /* Test one-byte buffer with size of one. */
970 errno = EBADF;
971 testBuffer[0] = 'x';
972 ret = p_wcsupr_s(testBuffer, 1);
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");
976
977 /* Test invalid size. */
978 wcscpy(testBuffer, mixedString);
979 errno = EBADF;
980 ret = p_wcsupr_s(testBuffer, 0);
981 ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
982 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
983 ok(testBuffer[0] == '\0', "Expected the first buffer character to be null\n");
984
985 /* Test normal string uppercasing. */
986 wcscpy(testBuffer, mixedString);
987 ret = p_wcsupr_s(testBuffer, sizeof(mixedString)/sizeof(WCHAR));
988 ok(ret == 0, "Expected _wcsupr_s to succeed, got %d\n", ret);
989 ok(!wcscmp(testBuffer, expectedString), "Expected the string to be fully upper-case\n");
990
991 /* Test uppercasing with a shorter buffer size count. */
992 wcscpy(testBuffer, mixedString);
993 errno = EBADF;
994 ret = p_wcsupr_s(testBuffer, sizeof(mixedString)/sizeof(WCHAR) - 1);
995 ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
996 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
997 ok(testBuffer[0] == '\0', "Expected the first buffer character to be null\n");
998
999 /* Test uppercasing with a longer buffer size count. */
1000 wcscpy(testBuffer, mixedString);
1001 ret = p_wcsupr_s(testBuffer, sizeof(testBuffer)/sizeof(WCHAR));
1002 ok(ret == 0, "Expected _wcsupr_s to succeed, got %d\n", ret);
1003 ok(!wcscmp(testBuffer, expectedString), "Expected the string to be fully upper-case\n");
1004 }
1005
1006 static void test__wcslwr_s(void)
1007 {
1008 static const WCHAR mixedString[] = {'M', 'i', 'X', 'e', 'D', 'l', 'o', 'w',
1009 'e', 'r', 'U', 'P', 'P', 'E', 'R', 0};
1010 static const WCHAR expectedString[] = {'m', 'i', 'x', 'e', 'd', 'l', 'o',
1011 'w', 'e', 'r', 'u', 'p', 'p', 'e',
1012 'r', 0};
1013 WCHAR buffer[2*sizeof(mixedString)/sizeof(WCHAR)];
1014 int ret;
1015
1016 if (!p_wcslwr_s)
1017 {
1018 win_skip("_wcslwr_s not found\n");
1019 return;
1020 }
1021
1022 /* Test NULL input string and invalid size. */
1023 errno = EBADF;
1024 ret = p_wcslwr_s(NULL, 0);
1025 ok(ret == EINVAL, "expected EINVAL, got %d\n", ret);
1026 ok(errno == EINVAL, "expected errno EINVAL, got %d\n", errno);
1027
1028 /* Test NULL input string and valid size. */
1029 errno = EBADF;
1030 ret = p_wcslwr_s(NULL, sizeof(buffer)/sizeof(wchar_t));
1031 ok(ret == EINVAL, "expected EINVAL, got %d\n", ret);
1032 ok(errno == EINVAL, "expected errno EINVAL, got %d\n", errno);
1033
1034 /* Test empty string with zero size. */
1035 errno = EBADF;
1036 buffer[0] = 'a';
1037 ret = p_wcslwr_s(buffer, 0);
1038 ok(ret == EINVAL, "expected EINVAL, got %d\n", ret);
1039 ok(errno == EINVAL, "expected errno EINVAL, got %d\n", errno);
1040 ok(buffer[0] == 0, "expected empty string\n");
1041
1042 /* Test empty string with size of one. */
1043 buffer[0] = 0;
1044 ret = p_wcslwr_s(buffer, 1);
1045 ok(ret == 0, "got %d\n", ret);
1046 ok(buffer[0] == 0, "expected buffer to be unchanged\n");
1047
1048 /* Test one-byte buffer with zero size. */
1049 errno = EBADF;
1050 buffer[0] = 'x';
1051 ret = p_wcslwr_s(buffer, 0);
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");
1055
1056 /* Test one-byte buffer with size of one. */
1057 errno = EBADF;
1058 buffer[0] = 'x';
1059 ret = p_wcslwr_s(buffer, 1);
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");
1063
1064 /* Test invalid size. */
1065 wcscpy(buffer, mixedString);
1066 errno = EBADF;
1067 ret = p_wcslwr_s(buffer, 0);
1068 ok(ret == EINVAL, "Expected EINVAL, got %d\n", ret);
1069 ok(errno == EINVAL, "expected errno to be EINVAL, got %d\n", errno);
1070 ok(buffer[0] == '\0', "expected empty string\n");
1071
1072 /* Test normal string uppercasing. */
1073 wcscpy(buffer, mixedString);
1074 ret = p_wcslwr_s(buffer, sizeof(mixedString)/sizeof(WCHAR));
1075 ok(ret == 0, "expected 0, got %d\n", ret);
1076 ok(!wcscmp(buffer, expectedString), "expected lowercase\n");
1077
1078 /* Test uppercasing with a shorter buffer size count. */
1079 wcscpy(buffer, mixedString);
1080 errno = EBADF;
1081 ret = p_wcslwr_s(buffer, sizeof(mixedString)/sizeof(WCHAR) - 1);
1082 ok(ret == EINVAL, "expected EINVAL, got %d\n", ret);
1083 ok(errno == EINVAL, "expected errno to be EINVAL, got %d\n", errno);
1084 ok(buffer[0] == '\0', "expected empty string\n");
1085
1086 /* Test uppercasing with a longer buffer size count. */
1087 wcscpy(buffer, mixedString);
1088 ret = p_wcslwr_s(buffer, sizeof(buffer)/sizeof(WCHAR));
1089 ok(ret == 0, "expected 0, got %d\n", ret);
1090 ok(!wcscmp(buffer, expectedString), "expected lowercase\n");
1091 }
1092
1093 static void test_mbcjisjms(void)
1094 {
1095 /* List of value-pairs to test. The test assumes the last pair to be {0, ..} */
1096 unsigned int jisjms[][2] = { {0x2020, 0}, {0x2021, 0}, {0x2120, 0}, {0x2121, 0x8140},
1097 {0x7f7f, 0}, {0x7f7e, 0}, {0x7e7f, 0}, {0x7e7e, 0xeffc},
1098 {0x255f, 0x837e}, {0x2560, 0x8380}, {0x2561, 0x8381},
1099 {0x2121FFFF, 0}, {0x2223, 0x81a1}, {0x237e, 0x829e}, {0, 0}};
1100 int cp[] = { 932, 936, 939, 950, 1361, _MB_CP_SBCS };
1101 unsigned int i, j;
1102 int prev_cp = _getmbcp();
1103
1104 for (i = 0; i < sizeof(cp)/sizeof(cp[0]); i++)
1105 {
1106 _setmbcp(cp[i]);
1107 for (j = 0; jisjms[j][0] != 0; j++)
1108 {
1109 unsigned int ret, exp;
1110 ret = _mbcjistojms(jisjms[j][0]);
1111 exp = (cp[i] == 932) ? jisjms[j][1] : jisjms[j][0];
1112 ok(ret == exp, "Expected 0x%x, got 0x%x (0x%x, codepage=%d)\n",
1113 exp, ret, jisjms[j][0], cp[i]);
1114 }
1115 }
1116 _setmbcp(prev_cp);
1117 }
1118
1119 static void test_mbcjmsjis(void)
1120 {
1121 /* List of value-pairs to test. The test assumes the last pair to be {0, ..} */
1122 unsigned int jmsjis[][2] = { {0x80fc, 0}, {0x813f, 0}, {0x8140, 0x2121},
1123 {0x817e, 0x215f}, {0x817f, 0}, {0x8180, 0x2160},
1124 {0x819e, 0x217e}, {0x819f, 0x2221}, {0x81fc, 0x227e},
1125 {0x81fd, 0}, {0x9ffc, 0x5e7e}, {0x9ffd, 0},
1126 {0xa040, 0}, {0xdffc, 0}, {0xe040, 0x5f21},
1127 {0xeffc, 0x7e7e}, {0xf040, 0}, {0x21, 0}, {0, 0}};
1128 int cp[] = { 932, 936, 939, 950, 1361, _MB_CP_SBCS };
1129 unsigned int i, j;
1130 int prev_cp = _getmbcp();
1131
1132 for (i = 0; i < sizeof(cp)/sizeof(cp[0]); i++)
1133 {
1134 _setmbcp(cp[i]);
1135 for (j = 0; jmsjis[j][0] != 0; j++)
1136 {
1137 unsigned int ret, exp;
1138 ret = _mbcjmstojis(jmsjis[j][0]);
1139 exp = (cp[i] == 932) ? jmsjis[j][1] : jmsjis[j][0];
1140 ok(ret == exp, "Expected 0x%x, got 0x%x (0x%x, codepage=%d)\n",
1141 exp, ret, jmsjis[j][0], cp[i]);
1142 }
1143 }
1144 _setmbcp(prev_cp);
1145 }
1146
1147 static void test_mbbtombc(void)
1148 {
1149 static const unsigned int mbbmbc[][2] = {
1150 {0x1f, 0x1f}, {0x20, 0x8140}, {0x39, 0x8258}, {0x40, 0x8197},
1151 {0x41, 0x8260}, {0x5e, 0x814f}, {0x7e, 0x8150}, {0x7f, 0x7f},
1152 {0x80, 0x80}, {0x81, 0x81}, {0xa0, 0xa0}, {0xa7, 0x8340},
1153 {0xb0, 0x815b}, {0xd1, 0x8380}, {0xff, 0xff}, {0,0}};
1154 int cp[] = { 932, 936, 939, 950, 1361, _MB_CP_SBCS };
1155 int i, j;
1156 int prev_cp = _getmbcp();
1157
1158 for (i = 0; i < sizeof(cp)/sizeof(cp[0]); i++)
1159 {
1160 _setmbcp(cp[i]);
1161 for (j = 0; mbbmbc[j][0] != 0; j++)
1162 {
1163 unsigned int exp, ret;
1164 ret = _mbbtombc(mbbmbc[j][0]);
1165 exp = (cp[i] == 932) ? mbbmbc[j][1] : mbbmbc[j][0];
1166 ok(ret == exp, "Expected 0x%x, got 0x%x (0x%x, codepage %d)\n",
1167 exp, ret, mbbmbc[j][0], cp[i]);
1168 }
1169 }
1170 _setmbcp(prev_cp);
1171 }
1172
1173 static void test_mbctombb(void)
1174 {
1175 static const unsigned int mbcmbb_932[][2] = {
1176 {0x829e, 0x829e}, {0x829f, 0xa7}, {0x82f1, 0xdd}, {0x82f2, 0x82f2},
1177 {0x833f, 0x833f}, {0x8340, 0xa7}, {0x837e, 0xd0}, {0x837f, 0x837f},
1178 {0x8380, 0xd1}, {0x8396, 0xb9}, {0x8397, 0x8397}, {0x813f, 0x813f},
1179 {0x8140, 0x20}, {0x814c, 0x814c}, {0x814f, 0x5e}, {0x8197, 0x40},
1180 {0x8198, 0x8198}, {0x8258, 0x39}, {0x8259, 0x8259}, {0x825f, 0x825f},
1181 {0x8260, 0x41}, {0x82f1, 0xdd}, {0x82f2, 0x82f2}, {0,0}};
1182 unsigned int exp, ret, i;
1183 unsigned int prev_cp = _getmbcp();
1184
1185 _setmbcp(932);
1186 for (i = 0; mbcmbb_932[i][0] != 0; i++)
1187 {
1188 ret = _mbctombb(mbcmbb_932[i][0]);
1189 exp = mbcmbb_932[i][1];
1190 ok(ret == exp, "Expected 0x%x, got 0x%x\n", exp, ret);
1191 }
1192 _setmbcp(prev_cp);
1193 }
1194
1195 static void test_ismbclegal(void) {
1196 unsigned int prev_cp = _getmbcp();
1197 int ret, exp, err;
1198 unsigned int i;
1199
1200 _setmbcp(932); /* Japanese */
1201 err = 0;
1202 for(i = 0; i < 0x10000; i++) {
1203 ret = _ismbclegal(i);
1204 exp = ((HIBYTE(i) >= 0x81 && HIBYTE(i) <= 0x9F) ||
1205 (HIBYTE(i) >= 0xE0 && HIBYTE(i) <= 0xFC)) &&
1206 ((LOBYTE(i) >= 0x40 && LOBYTE(i) <= 0x7E) ||
1207 (LOBYTE(i) >= 0x80 && LOBYTE(i) <= 0xFC));
1208 if(ret != exp) {
1209 err = 1;
1210 break;
1211 }
1212 }
1213 ok(!err, "_ismbclegal (932) : Expected 0x%x, got 0x%x (0x%x)\n", exp, ret, i);
1214 _setmbcp(936); /* Chinese (GBK) */
1215 err = 0;
1216 for(i = 0; i < 0x10000; i++) {
1217 ret = _ismbclegal(i);
1218 exp = HIBYTE(i) >= 0x81 && HIBYTE(i) <= 0xFE &&
1219 LOBYTE(i) >= 0x40 && LOBYTE(i) <= 0xFE;
1220 if(ret != exp) {
1221 err = 1;
1222 break;
1223 }
1224 }
1225 ok(!err, "_ismbclegal (936) : Expected 0x%x, got 0x%x (0x%x)\n", exp, ret, i);
1226 _setmbcp(949); /* Korean */
1227 err = 0;
1228 for(i = 0; i < 0x10000; i++) {
1229 ret = _ismbclegal(i);
1230 exp = HIBYTE(i) >= 0x81 && HIBYTE(i) <= 0xFE &&
1231 LOBYTE(i) >= 0x41 && LOBYTE(i) <= 0xFE;
1232 if(ret != exp) {
1233 err = 1;
1234 break;
1235 }
1236 }
1237 ok(!err, "_ismbclegal (949) : Expected 0x%x, got 0x%x (0x%x)\n", exp, ret, i);
1238 _setmbcp(950); /* Chinese (Big5) */
1239 err = 0;
1240 for(i = 0; i < 0x10000; i++) {
1241 ret = _ismbclegal(i);
1242 exp = HIBYTE(i) >= 0x81 && HIBYTE(i) <= 0xFE &&
1243 ((LOBYTE(i) >= 0x40 && LOBYTE(i) <= 0x7E) ||
1244 (LOBYTE(i) >= 0xA1 && LOBYTE(i) <= 0xFE));
1245 if(ret != exp) {
1246 err = 1;
1247 break;
1248 }
1249 }
1250 ok(!err, "_ismbclegal (950) : Expected 0x%x, got 0x%x (0x%x)\n", exp, ret, i);
1251 _setmbcp(1361); /* Korean (Johab) */
1252 err = 0;
1253 for(i = 0; i < 0x10000; i++) {
1254 ret = _ismbclegal(i);
1255 exp = ((HIBYTE(i) >= 0x81 && HIBYTE(i) <= 0xD3) ||
1256 (HIBYTE(i) >= 0xD8 && HIBYTE(i) <= 0xF9)) &&
1257 ((LOBYTE(i) >= 0x31 && LOBYTE(i) <= 0x7E) ||
1258 (LOBYTE(i) >= 0x81 && LOBYTE(i) <= 0xFE)) &&
1259 HIBYTE(i) != 0xDF;
1260 if(ret != exp) {
1261 err = 1;
1262 break;
1263 }
1264 }
1265 todo_wine ok(!err, "_ismbclegal (1361) : Expected 0x%x, got 0x%x (0x%x)\n", exp, ret, i);
1266
1267 _setmbcp(prev_cp);
1268 }
1269
1270 static const struct {
1271 const char* string;
1272 const char* delimiter;
1273 int exp_offsetret1; /* returned offset from string after first call to strtok()
1274 -1 means NULL */
1275 int exp_offsetret2; /* returned offset from string after second call to strtok()
1276 -1 means NULL */
1277 int exp_offsetret3; /* returned offset from string after third call to strtok()
1278 -1 means NULL */
1279 } testcases_strtok[] = {
1280 { "red cabernet", " ", 0, 4, -1 },
1281 { "sparkling white riesling", " ", 0, 10, 16 },
1282 { " pale cream sherry", "e ", 1, 6, 9 },
1283 /* end mark */
1284 { 0}
1285 };
1286
1287 static void test_strtok(void)
1288 {
1289 int i;
1290 char *strret;
1291 char teststr[100];
1292 for( i = 0; testcases_strtok[i].string; i++){
1293 strcpy( teststr, testcases_strtok[i].string);
1294 strret = strtok( teststr, testcases_strtok[i].delimiter);
1295 ok( (int)(strret - teststr) == testcases_strtok[i].exp_offsetret1 ||
1296 (!strret && testcases_strtok[i].exp_offsetret1 == -1),
1297 "string (%p) \'%s\' return %p\n",
1298 teststr, testcases_strtok[i].string, strret);
1299 if( !strret) continue;
1300 strret = strtok( NULL, testcases_strtok[i].delimiter);
1301 ok( (int)(strret - teststr) == testcases_strtok[i].exp_offsetret2 ||
1302 (!strret && testcases_strtok[i].exp_offsetret2 == -1),
1303 "second call string (%p) \'%s\' return %p\n",
1304 teststr, testcases_strtok[i].string, strret);
1305 if( !strret) continue;
1306 strret = strtok( NULL, testcases_strtok[i].delimiter);
1307 ok( (int)(strret - teststr) == testcases_strtok[i].exp_offsetret3 ||
1308 (!strret && testcases_strtok[i].exp_offsetret3 == -1),
1309 "third call string (%p) \'%s\' return %p\n",
1310 teststr, testcases_strtok[i].string, strret);
1311 }
1312 }
1313
1314 static void test_strtol(void)
1315 {
1316 char* e;
1317 LONG l;
1318 ULONG ul;
1319
1320 /* errno is only set in case of error, so reset errno to EBADF to check for errno modification */
1321 /* errno is modified on W2K8+ */
1322 errno = EBADF;
1323 l = strtol("-1234", &e, 0);
1324 ok(l==-1234, "wrong value %d\n", l);
1325 ok(errno == EBADF || broken(errno == 0), "wrong errno %d\n", errno);
1326 errno = EBADF;
1327 ul = strtoul("1234", &e, 0);
1328 ok(ul==1234, "wrong value %u\n", ul);
1329 ok(errno == EBADF || broken(errno == 0), "wrong errno %d\n", errno);
1330
1331 errno = EBADF;
1332 l = strtol("2147483647L", &e, 0);
1333 ok(l==2147483647, "wrong value %d\n", l);
1334 ok(errno == EBADF || broken(errno == 0), "wrong errno %d\n", errno);
1335 errno = EBADF;
1336 l = strtol("-2147483648L", &e, 0);
1337 ok(l==-2147483647L - 1, "wrong value %d\n", l);
1338 ok(errno == EBADF || broken(errno == 0), "wrong errno %d\n", errno);
1339 errno = EBADF;
1340 ul = strtoul("4294967295UL", &e, 0);
1341 ok(ul==4294967295ul, "wrong value %u\n", ul);
1342 ok(errno == EBADF || broken(errno == 0), "wrong errno %d\n", errno);
1343
1344 errno = 0;
1345 l = strtol("9223372036854775807L", &e, 0);
1346 ok(l==2147483647, "wrong value %d\n", l);
1347 ok(errno == ERANGE, "wrong errno %d\n", errno);
1348 errno = 0;
1349 ul = strtoul("9223372036854775807L", &e, 0);
1350 ok(ul==4294967295ul, "wrong value %u\n", ul);
1351 ok(errno == ERANGE, "wrong errno %d\n", errno);
1352
1353 errno = 0;
1354 ul = strtoul("-2", NULL, 0);
1355 ok(ul == -2, "wrong value %u\n", ul);
1356 ok(errno == 0, "wrong errno %d\n", errno);
1357
1358 errno = 0;
1359 ul = strtoul("-4294967294", NULL, 0);
1360 ok(ul == 2, "wrong value %u\n", ul);
1361 ok(errno == 0, "wrong errno %d\n", errno);
1362
1363 errno = 0;
1364 ul = strtoul("-4294967295", NULL, 0);
1365 ok(ul==1, "wrong value %u\n", ul);
1366 ok(errno == 0, "wrong errno %d\n", errno);
1367
1368 errno = 0;
1369 ul = strtoul("-4294967296", NULL, 0);
1370 ok(ul == 1, "wrong value %u\n", ul);
1371 ok(errno == ERANGE, "wrong errno %d\n", errno);
1372 }
1373
1374 static void test_strnlen(void)
1375 {
1376 static const char str[] = "string";
1377 size_t res;
1378
1379 if(!p_strnlen) {
1380 win_skip("strnlen not found\n");
1381 return;
1382 }
1383
1384 res = p_strnlen(str, 20);
1385 ok(res == 6, "Returned length = %d\n", (int)res);
1386
1387 res = p_strnlen(str, 3);
1388 ok(res == 3, "Returned length = %d\n", (int)res);
1389
1390 res = p_strnlen(NULL, 0);
1391 ok(res == 0, "Returned length = %d\n", (int)res);
1392 }
1393
1394 static void test__strtoi64(void)
1395 {
1396 static const char no1[] = "31923";
1397 static const char no2[] = "-213312";
1398 static const char no3[] = "12aa";
1399 static const char no4[] = "abc12";
1400 static const char overflow[] = "99999999999999999999";
1401 static const char neg_overflow[] = "-99999999999999999999";
1402 static const char hex[] = "0x123";
1403 static const char oct[] = "000123";
1404 static const char blanks[] = " 12 212.31";
1405
1406 __int64 res;
1407 unsigned __int64 ures;
1408 char *endpos;
1409
1410 if(!p_strtoi64 || !p_strtoui64) {
1411 win_skip("_strtoi64 or _strtoui64 not found\n");
1412 return;
1413 }
1414
1415 errno = 0xdeadbeef;
1416 res = p_strtoi64(no1, NULL, 10);
1417 ok(res == 31923, "res != 31923\n");
1418 res = p_strtoi64(no2, NULL, 10);
1419 ok(res == -213312, "res != -213312\n");
1420 res = p_strtoi64(no3, NULL, 10);
1421 ok(res == 12, "res != 12\n");
1422 res = p_strtoi64(no4, &endpos, 10);
1423 ok(res == 0, "res != 0\n");
1424 ok(endpos == no4, "Scanning was not stopped on first character\n");
1425 res = p_strtoi64(hex, &endpos, 10);
1426 ok(res == 0, "res != 0\n");
1427 ok(endpos == hex+1, "Incorrect endpos (%p-%p)\n", hex, endpos);
1428 res = p_strtoi64(oct, &endpos, 10);
1429 ok(res == 123, "res != 123\n");
1430 ok(endpos == oct+strlen(oct), "Incorrect endpos (%p-%p)\n", oct, endpos);
1431 res = p_strtoi64(blanks, &endpos, 10);
1432 ok(res == 12, "res != 12\n");
1433 ok(endpos == blanks+10, "Incorrect endpos (%p-%p)\n", blanks, endpos);
1434 ok(errno == 0xdeadbeef, "errno = %x\n", errno);
1435
1436 errno = 0xdeadbeef;
1437 res = p_strtoi64(overflow, &endpos, 10);
1438 ok(res == _I64_MAX, "res != _I64_MAX\n");
1439 ok(endpos == overflow+strlen(overflow), "Incorrect endpos (%p-%p)\n", overflow, endpos);
1440 ok(errno == ERANGE, "errno = %x\n", errno);
1441
1442 errno = 0xdeadbeef;
1443 res = p_strtoi64(neg_overflow, &endpos, 10);
1444 ok(res == _I64_MIN, "res != _I64_MIN\n");
1445 ok(endpos == neg_overflow+strlen(neg_overflow), "Incorrect endpos (%p-%p)\n", neg_overflow, endpos);
1446 ok(errno == ERANGE, "errno = %x\n", errno);
1447
1448 errno = 0xdeadbeef;
1449 res = p_strtoi64(no1, &endpos, 16);
1450 ok(res == 203043, "res != 203043\n");
1451 ok(endpos == no1+strlen(no1), "Incorrect endpos (%p-%p)\n", no1, endpos);
1452 res = p_strtoi64(no2, &endpos, 16);
1453 ok(res == -2175762, "res != -2175762\n");
1454 ok(endpos == no2+strlen(no2), "Incorrect endpos (%p-%p)\n", no2, endpos);
1455 res = p_strtoi64(no3, &endpos, 16);
1456 ok(res == 4778, "res != 4778\n");
1457 ok(endpos == no3+strlen(no3), "Incorrect endpos (%p-%p)\n", no3, endpos);
1458 res = p_strtoi64(no4, &endpos, 16);
1459 ok(res == 703506, "res != 703506\n");
1460 ok(endpos == no4+strlen(no4), "Incorrect endpos (%p-%p)\n", no4, endpos);
1461 res = p_strtoi64(hex, &endpos, 16);
1462 ok(res == 291, "res != 291\n");
1463 ok(endpos == hex+strlen(hex), "Incorrect endpos (%p-%p)\n", hex, endpos);
1464 res = p_strtoi64(oct, &endpos, 16);
1465 ok(res == 291, "res != 291\n");
1466 ok(endpos == oct+strlen(oct), "Incorrect endpos (%p-%p)\n", oct, endpos);
1467 res = p_strtoi64(blanks, &endpos, 16);
1468 ok(res == 18, "res != 18\n");
1469 ok(endpos == blanks+10, "Incorrect endpos (%p-%p)\n", blanks, endpos);
1470 ok(errno == 0xdeadbeef, "errno = %x\n", errno);
1471
1472 errno = 0xdeadbeef;
1473 res = p_strtoi64(hex, &endpos, 36);
1474 ok(res == 1541019, "res != 1541019\n");
1475 ok(endpos == hex+strlen(hex), "Incorrect endpos (%p-%p)\n", hex, endpos);
1476 ok(errno == 0xdeadbeef, "errno = %x\n", errno);
1477
1478 errno = 0xdeadbeef;
1479 res = p_strtoi64(no1, &endpos, 0);
1480 ok(res == 31923, "res != 31923\n");
1481 ok(endpos == no1+strlen(no1), "Incorrect endpos (%p-%p)\n", no1, endpos);
1482 res = p_strtoi64(no2, &endpos, 0);
1483 ok(res == -213312, "res != -213312\n");
1484 ok(endpos == no2+strlen(no2), "Incorrect endpos (%p-%p)\n", no2, endpos);
1485 res = p_strtoi64(no3, &endpos, 10);
1486 ok(res == 12, "res != 12\n");
1487 ok(endpos == no3+2, "Incorrect endpos (%p-%p)\n", no3, endpos);
1488 res = p_strtoi64(no4, &endpos, 10);
1489 ok(res == 0, "res != 0\n");
1490 ok(endpos == no4, "Incorrect endpos (%p-%p)\n", no4, endpos);
1491 res = p_strtoi64(hex, &endpos, 10);
1492 ok(res == 0, "res != 0\n");
1493 ok(endpos == hex+1, "Incorrect endpos (%p-%p)\n", hex, endpos);
1494 res = p_strtoi64(oct, &endpos, 10);
1495 ok(res == 123, "res != 123\n");
1496 ok(endpos == oct+strlen(oct), "Incorrect endpos (%p-%p)\n", oct, endpos);
1497 res = p_strtoi64(blanks, &endpos, 10);
1498 ok(res == 12, "res != 12\n");
1499 ok(endpos == blanks+10, "Incorrect endpos (%p-%p)\n", blanks, endpos);
1500 ok(errno == 0xdeadbeef, "errno = %x\n", errno);
1501
1502 errno = 0xdeadbeef;
1503 ures = p_strtoui64(no1, &endpos, 0);
1504 ok(ures == 31923, "ures != 31923\n");
1505 ok(endpos == no1+strlen(no1), "Incorrect endpos (%p-%p)\n", no1, endpos);
1506 ures = p_strtoui64(no2, &endpos, 0);
1507 ok(ures == -213312, "ures != -213312\n");
1508 ok(endpos == no2+strlen(no2), "Incorrect endpos (%p-%p)\n", no2, endpos);
1509 ures = p_strtoui64(no3, &endpos, 10);
1510 ok(ures == 12, "ures != 12\n");
1511 ok(endpos == no3+2, "Incorrect endpos (%p-%p)\n", no3, endpos);
1512 ures = p_strtoui64(no4, &endpos, 10);
1513 ok(ures == 0, "ures != 0\n");
1514 ok(endpos == no4, "Incorrect endpos (%p-%p)\n", no4, endpos);
1515 ures = p_strtoui64(hex, &endpos, 10);
1516 ok(ures == 0, "ures != 0\n");
1517 ok(endpos == hex+1, "Incorrect endpos (%p-%p)\n", hex, endpos);
1518 ures = p_strtoui64(oct, &endpos, 10);
1519 ok(ures == 123, "ures != 123\n");
1520 ok(endpos == oct+strlen(oct), "Incorrect endpos (%p-%p)\n", oct, endpos);
1521 ures = p_strtoui64(blanks, &endpos, 10);
1522 ok(ures == 12, "ures != 12\n");
1523 ok(endpos == blanks+10, "Incorrect endpos (%p-%p)\n", blanks, endpos);
1524 ok(errno == 0xdeadbeef, "errno = %x\n", errno);
1525
1526 errno = 0xdeadbeef;
1527 ures = p_strtoui64(overflow, &endpos, 10);
1528 ok(ures == _UI64_MAX, "ures != _UI64_MAX\n");
1529 ok(endpos == overflow+strlen(overflow), "Incorrect endpos (%p-%p)\n", overflow, endpos);
1530 ok(errno == ERANGE, "errno = %x\n", errno);
1531
1532 errno = 0xdeadbeef;
1533 ures = p_strtoui64(neg_overflow, &endpos, 10);
1534 ok(ures == 1, "ures != 1\n");
1535 ok(endpos == neg_overflow+strlen(neg_overflow), "Incorrect endpos (%p-%p)\n", neg_overflow, endpos);
1536 ok(errno == ERANGE, "errno = %x\n", errno);
1537 }
1538
1539 static inline BOOL almost_equal(double d1, double d2) {
1540 if(d1-d2>-1e-30 && d1-d2<1e-30)
1541 return TRUE;
1542 return FALSE;
1543 }
1544
1545 static void test__strtod(void)
1546 {
1547 const char double1[] = "12.1";
1548 const char double2[] = "-13.721";
1549 const char double3[] = "INF";
1550 const char double4[] = ".21e12";
1551 const char double5[] = "214353e-3";
1552 const char overflow[] = "1d9999999999999999999";
1553 const char white_chars[] = " d10";
1554
1555 char *end;
1556 double d;
1557
1558 d = strtod(double1, &end);
1559 ok(almost_equal(d, 12.1), "d = %lf\n", d);
1560 ok(end == double1+4, "incorrect end (%d)\n", (int)(end-double1));
1561
1562 d = strtod(double2, &end);
1563 ok(almost_equal(d, -13.721), "d = %lf\n", d);
1564 ok(end == double2+7, "incorrect end (%d)\n", (int)(end-double2));
1565
1566 d = strtod(double3, &end);
1567 ok(almost_equal(d, 0), "d = %lf\n", d);
1568 ok(end == double3, "incorrect end (%d)\n", (int)(end-double3));
1569
1570 d = strtod(double4, &end);
1571 ok(almost_equal(d, 210000000000.0), "d = %lf\n", d);
1572 ok(end == double4+6, "incorrect end (%d)\n", (int)(end-double4));
1573
1574 d = strtod(double5, &end);
1575 ok(almost_equal(d, 214.353), "d = %lf\n", d);
1576 ok(end == double5+9, "incorrect end (%d)\n", (int)(end-double5));
1577
1578 d = strtod("12.1d2", NULL);
1579 ok(almost_equal(d, 12.1e2), "d = %lf\n", d);
1580
1581 d = strtod(white_chars, &end);
1582 ok(almost_equal(d, 0), "d = %lf\n", d);
1583 ok(end == white_chars, "incorrect end (%d)\n", (int)(end-white_chars));
1584
1585 if (!p__strtod_l)
1586 win_skip("_strtod_l not found\n");
1587 else
1588 {
1589 errno = EBADF;
1590 d = strtod(NULL, NULL);
1591 ok(almost_equal(d, 0.0), "d = %lf\n", d);
1592 ok(errno == EINVAL, "errno = %x\n", errno);
1593
1594 errno = EBADF;
1595 end = (char *)0xdeadbeef;
1596 d = strtod(NULL, &end);
1597 ok(almost_equal(d, 0.0), "d = %lf\n", d);
1598 ok(errno == EINVAL, "errno = %x\n", errno);
1599 ok(!end, "incorrect end ptr %p\n", end);
1600
1601 errno = EBADF;
1602 d = p__strtod_l(NULL, NULL, NULL);
1603 ok(almost_equal(d, 0.0), "d = %lf\n", d);
1604 ok(errno == EINVAL, "errno = %x\n", errno);
1605 }
1606
1607 /* Set locale with non '.' decimal point (',') */
1608 if(!setlocale(LC_ALL, "Polish")) {
1609 win_skip("system with limited locales\n");
1610 return;
1611 }
1612
1613 d = strtod("12.1", NULL);
1614 ok(almost_equal(d, 12.0), "d = %lf\n", d);
1615
1616 d = strtod("12,1", NULL);
1617 ok(almost_equal(d, 12.1), "d = %lf\n", d);
1618
1619 setlocale(LC_ALL, "C");
1620
1621 /* Precision tests */
1622 d = strtod("0.1", NULL);
1623 ok(almost_equal(d, 0.1), "d = %lf\n", d);
1624 d = strtod("-0.1", NULL);
1625 ok(almost_equal(d, -0.1), "d = %lf\n", d);
1626 d = strtod("0.1281832188491894198128921", NULL);
1627 ok(almost_equal(d, 0.1281832188491894198128921), "d = %lf\n", d);
1628 d = strtod("0.82181281288121", NULL);
1629 ok(almost_equal(d, 0.82181281288121), "d = %lf\n", d);
1630 d = strtod("21921922352523587651128218821", NULL);
1631 ok(almost_equal(d, 21921922352523587651128218821.0), "d = %lf\n", d);
1632 d = strtod("0.1d238", NULL);
1633 ok(almost_equal(d, 0.1e238L), "d = %lf\n", d);
1634 d = strtod("0.1D-4736", NULL);
1635 ok(almost_equal(d, 0.1e-4736L), "d = %lf\n", d);
1636
1637 errno = 0xdeadbeef;
1638 strtod(overflow, &end);
1639 ok(errno == ERANGE, "errno = %x\n", errno);
1640 ok(end == overflow+21, "incorrect end (%d)\n", (int)(end-overflow));
1641
1642 errno = 0xdeadbeef;
1643 strtod("-1d309", NULL);
1644 ok(errno == ERANGE, "errno = %x\n", errno);
1645 }
1646
1647 static void test_mbstowcs(void)
1648 {
1649 static const wchar_t wSimple[] = { 't','e','x','t',0 };
1650 static const wchar_t wHiragana[] = { 0x3042,0x3043,0 };
1651 static const char mSimple[] = "text";
1652 static const char mHiragana[] = { 0x82,0xa0,0x82,0xa1,0 };
1653
1654 const wchar_t *pwstr;
1655 wchar_t wOut[6];
1656 char mOut[6];
1657 size_t ret;
1658 int err;
1659 const char *pmbstr;
1660 mbstate_t state;
1661
1662 wOut[4] = '!'; wOut[5] = '\0';
1663 mOut[4] = '!'; mOut[5] = '\0';
1664
1665 if(pmbstowcs_s) {
1666 /* crashes on some systems */
1667 errno = 0xdeadbeef;
1668 ret = mbstowcs(wOut, NULL, 4);
1669 ok(ret == -1, "mbstowcs did not return -1\n");
1670 ok(errno == EINVAL, "errno = %d\n", errno);
1671 }
1672
1673 ret = mbstowcs(NULL, mSimple, 0);
1674 ok(ret == 4, "mbstowcs did not return 4\n");
1675
1676 ret = mbstowcs(wOut, mSimple, 4);
1677 ok(ret == 4, "mbstowcs did not return 4\n");
1678 ok(!memcmp(wOut, wSimple, 4*sizeof(wchar_t)), "wOut = %s\n", wine_dbgstr_w(wOut));
1679 ok(wOut[4] == '!', "wOut[4] != \'!\'\n");
1680
1681 ret = wcstombs(NULL, wSimple, 0);
1682 ok(ret == 4, "wcstombs did not return 4\n");
1683
1684 ret = wcstombs(mOut, wSimple, 6);
1685 ok(ret == 4, "wcstombs did not return 4\n");
1686 ok(!memcmp(mOut, mSimple, 5*sizeof(char)), "mOut = %s\n", mOut);
1687
1688 ret = wcstombs(mOut, wSimple, 2);
1689 ok(ret == 2, "wcstombs did not return 2\n");
1690 ok(!memcmp(mOut, mSimple, 5*sizeof(char)), "mOut = %s\n", mOut);
1691
1692 if(!setlocale(LC_ALL, "Japanese_Japan.932")) {
1693 win_skip("Japanese_Japan.932 locale not available\n");
1694 return;
1695 }
1696
1697 ret = mbstowcs(wOut, mHiragana, 6);
1698 ok(ret == 2, "mbstowcs did not return 2\n");
1699 ok(!memcmp(wOut, wHiragana, sizeof(wHiragana)), "wOut = %s\n", wine_dbgstr_w(wOut));
1700
1701 ret = wcstombs(mOut, wHiragana, 6);
1702 ok(ret == 4, "wcstombs did not return 4\n");
1703 ok(!memcmp(mOut, mHiragana, sizeof(mHiragana)), "mOut = %s\n", mOut);
1704
1705 if(!pmbstowcs_s || !pwcstombs_s) {
1706 setlocale(LC_ALL, "C");
1707 win_skip("mbstowcs_s or wcstombs_s not available\n");
1708 return;
1709 }
1710
1711 err = pmbstowcs_s(&ret, wOut, 6, mSimple, _TRUNCATE);
1712 ok(err == 0, "err = %d\n", err);
1713 ok(ret == 5, "mbstowcs_s did not return 5\n");
1714 ok(!memcmp(wOut, wSimple, sizeof(wSimple)), "wOut = %s\n", wine_dbgstr_w(wOut));
1715
1716 err = pmbstowcs_s(&ret, wOut, 6, mHiragana, _TRUNCATE);
1717 ok(err == 0, "err = %d\n", err);
1718 ok(ret == 3, "mbstowcs_s did not return 3\n");
1719 ok(!memcmp(wOut, wHiragana, sizeof(wHiragana)), "wOut = %s\n", wine_dbgstr_w(wOut));
1720
1721 err = pmbstowcs_s(&ret, NULL, 0, mHiragana, 1);
1722 ok(err == 0, "err = %d\n", err);
1723 ok(ret == 3, "mbstowcs_s did not return 3\n");
1724
1725 err = pwcstombs_s(&ret, mOut, 6, wSimple, _TRUNCATE);
1726 ok(err == 0, "err = %d\n", err);
1727 ok(ret == 5, "wcstombs_s did not return 5\n");
1728 ok(!memcmp(mOut, mSimple, sizeof(mSimple)), "mOut = %s\n", mOut);
1729
1730 err = pwcstombs_s(&ret, mOut, 6, wHiragana, _TRUNCATE);
1731 ok(err == 0, "err = %d\n", err);
1732 ok(ret == 5, "wcstombs_s did not return 5\n");
1733 ok(!memcmp(mOut, mHiragana, sizeof(mHiragana)), "mOut = %s\n", mOut);
1734
1735 err = pwcstombs_s(&ret, NULL, 0, wHiragana, 1);
1736 ok(err == 0, "err = %d\n", err);
1737 ok(ret == 5, "wcstombs_s did not return 5\n");
1738
1739 if(!pwcsrtombs) {
1740 setlocale(LC_ALL, "C");
1741 win_skip("wcsrtombs not available\n");
1742 return;
1743 }
1744
1745 pwstr = wSimple;
1746 err = -3;
1747 ret = pwcsrtombs(mOut, &pwstr, 4, &err);
1748 ok(ret == 4, "wcsrtombs did not return 4\n");
1749 ok(err == 0, "err = %d\n", err);
1750 ok(pwstr == wSimple+4, "pwstr = %p (wszSimple = %p)\n", pwstr, wSimple);
1751 ok(!memcmp(mOut, mSimple, ret), "mOut = %s\n", mOut);
1752
1753 pwstr = wSimple;
1754 ret = pwcsrtombs(mOut, &pwstr, 5, NULL);
1755 ok(ret == 4, "wcsrtombs did not return 4\n");
1756 ok(pwstr == NULL, "pwstr != NULL\n");
1757 ok(!memcmp(mOut, mSimple, sizeof(mSimple)), "mOut = %s\n", mOut);
1758
1759 if(!p_mbsrtowcs) {
1760 setlocale(LC_ALL, "C");
1761 win_skip("mbsrtowcs not available\n");
1762 return;
1763 }
1764
1765 pmbstr = mHiragana;
1766 ret = p_mbsrtowcs(wOut, &pmbstr, 6, NULL);
1767 ok(ret == 2, "mbsrtowcs did not return 2\n");
1768 ok(!memcmp(wOut, wHiragana, sizeof(wHiragana)), "wOut = %s\n", wine_dbgstr_w(wOut));
1769 ok(!pmbstr, "pmbstr != NULL\n");
1770
1771 state = mHiragana[0];
1772 pmbstr = mHiragana+1;
1773 ret = p_mbsrtowcs(wOut, &pmbstr, 6, &state);
1774 ok(ret == 2, "mbsrtowcs did not return 2\n");
1775 ok(wOut[0] == 0x3042, "wOut[0] = %x\n", wOut[0]);
1776 ok(wOut[1] == 0xff61, "wOut[1] = %x\n", wOut[1]);
1777 ok(wOut[2] == 0, "wOut[2] = %x\n", wOut[2]);
1778 ok(!pmbstr, "pmbstr != NULL\n");
1779
1780 errno = EBADF;
1781 ret = p_mbsrtowcs(wOut, NULL, 6, &state);
1782 ok(ret == -1, "mbsrtowcs did not return -1\n");
1783 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1784
1785 setlocale(LC_ALL, "C");
1786 }
1787
1788 static void test_gcvt(void)
1789 {
1790 char buf[1024], *res;
1791 errno_t err;
1792
1793 if(!p_gcvt_s) {
1794 win_skip("Skipping _gcvt tests\n");
1795 return;
1796 }
1797
1798 errno = 0;
1799 res = _gcvt(1.2, -1, buf);
1800 ok(res == NULL, "res != NULL\n");
1801 ok(errno == ERANGE, "errno = %d\n", errno);
1802
1803 errno = 0;
1804 res = _gcvt(1.2, 5, NULL);
1805 ok(res == NULL, "res != NULL\n");
1806 ok(errno == EINVAL, "errno = %d\n", errno);
1807
1808 res = gcvt(1.2, 5, buf);
1809 ok(res == buf, "res != buf\n");
1810 ok(!strcmp(buf, "1.2"), "buf = %s\n", buf);
1811
1812 buf[0] = 'x';
1813 err = p_gcvt_s(buf, 5, 1.2, 10);
1814 ok(err == ERANGE, "err = %d\n", err);
1815 ok(buf[0] == '\0', "buf[0] = %c\n", buf[0]);
1816
1817 buf[0] = 'x';
1818 err = p_gcvt_s(buf, 4, 123456, 2);
1819 ok(err == ERANGE, "err = %d\n", err);
1820 ok(buf[0] == '\0', "buf[0] = %c\n", buf[0]);
1821 }
1822
1823 static void test__itoa_s(void)
1824 {
1825 errno_t ret;
1826 char buffer[33];
1827
1828 if (!p_itoa_s)
1829 {
1830 win_skip("Skipping _itoa_s tests\n");
1831 return;
1832 }
1833
1834 errno = EBADF;
1835 ret = p_itoa_s(0, NULL, 0, 0);
1836 ok(ret == EINVAL, "Expected _itoa_s to return EINVAL, got %d\n", ret);
1837 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1838
1839 memset(buffer, 'X', sizeof(buffer));
1840 errno = EBADF;
1841 ret = p_itoa_s(0, buffer, 0, 0);
1842 ok(ret == EINVAL, "Expected _itoa_s to return EINVAL, got %d\n", ret);
1843 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1844 ok(buffer[0] == 'X', "Expected the output buffer to be untouched\n");
1845
1846 memset(buffer, 'X', sizeof(buffer));
1847 errno = EBADF;
1848 ret = p_itoa_s(0, buffer, sizeof(buffer), 0);
1849 ok(ret == EINVAL, "Expected _itoa_s to return EINVAL, got %d\n", ret);
1850 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1851 ok(buffer[0] == '\0', "Expected the output buffer to be null terminated\n");
1852
1853 memset(buffer, 'X', sizeof(buffer));
1854 errno = EBADF;
1855 ret = p_itoa_s(0, buffer, sizeof(buffer), 64);
1856 ok(ret == EINVAL, "Expected _itoa_s to return EINVAL, got %d\n", ret);
1857 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1858 ok(buffer[0] == '\0', "Expected the output buffer to be null terminated\n");
1859
1860 memset(buffer, 'X', sizeof(buffer));
1861 errno = EBADF;
1862 ret = p_itoa_s(12345678, buffer, 4, 10);
1863 ok(ret == ERANGE, "Expected _itoa_s to return ERANGE, got %d\n", ret);
1864 ok(errno == ERANGE, "Expected errno to be ERANGE, got %d\n", errno);
1865 ok(!memcmp(buffer, "\000765", 4),
1866 "Expected the output buffer to be null terminated with truncated output\n");
1867
1868 memset(buffer, 'X', sizeof(buffer));
1869 errno = EBADF;
1870 ret = p_itoa_s(12345678, buffer, 8, 10);
1871 ok(ret == ERANGE, "Expected _itoa_s to return ERANGE, got %d\n", ret);
1872 ok(errno == ERANGE, "Expected errno to be ERANGE, got %d\n", errno);
1873 ok(!memcmp(buffer, "\0007654321", 8),
1874 "Expected the output buffer to be null terminated with truncated output\n");
1875
1876 memset(buffer, 'X', sizeof(buffer));
1877 errno = EBADF;
1878 ret = p_itoa_s(-12345678, buffer, 9, 10);
1879 ok(ret == ERANGE, "Expected _itoa_s to return ERANGE, got %d\n", ret);
1880 ok(errno == ERANGE, "Expected errno to be ERANGE, got %d\n", errno);
1881 ok(!memcmp(buffer, "\00087654321", 9),
1882 "Expected the output buffer to be null terminated with truncated output\n");
1883
1884 ret = p_itoa_s(12345678, buffer, 9, 10);
1885 ok(ret == 0, "Expected _itoa_s to return 0, got %d\n", ret);
1886 ok(!strcmp(buffer, "12345678"),
1887 "Expected output buffer string to be \"12345678\", got \"%s\"\n",
1888 buffer);
1889
1890 ret = p_itoa_s(43690, buffer, sizeof(buffer), 2);
1891 ok(ret == 0, "Expected _itoa_s to return 0, got %d\n", ret);
1892 ok(!strcmp(buffer, "1010101010101010"),
1893 "Expected output buffer string to be \"1010101010101010\", got \"%s\"\n",
1894 buffer);
1895
1896 ret = p_itoa_s(1092009, buffer, sizeof(buffer), 36);
1897 ok(ret == 0, "Expected _itoa_s to return 0, got %d\n", ret);
1898 ok(!strcmp(buffer, "nell"),
1899 "Expected output buffer string to be \"nell\", got \"%s\"\n",
1900 buffer);
1901
1902 ret = p_itoa_s(5704, buffer, sizeof(buffer), 18);
1903 ok(ret == 0, "Expected _itoa_s to return 0, got %d\n", ret);
1904 ok(!strcmp(buffer, "hag"),
1905 "Expected output buffer string to be \"hag\", got \"%s\"\n",
1906 buffer);
1907
1908 ret = p_itoa_s(-12345678, buffer, sizeof(buffer), 10);
1909 ok(ret == 0, "Expected _itoa_s to return 0, got %d\n", ret);
1910 ok(!strcmp(buffer, "-12345678"),
1911 "Expected output buffer string to be \"-12345678\", got \"%s\"\n",
1912 buffer);
1913
1914 itoa(100, buffer, 100);
1915 ok(!strcmp(buffer, "10"),
1916 "Expected output buffer string to be \"10\", got \"%s\"\n", buffer);
1917 }
1918
1919 static void test__strlwr_s(void)
1920 {
1921 errno_t ret;
1922 char buffer[20];
1923
1924 if (!p_strlwr_s)
1925 {
1926 win_skip("Skipping _strlwr_s tests\n");
1927 return;
1928 }
1929
1930 errno = EBADF;
1931 ret = p_strlwr_s(NULL, 0);
1932 ok(ret == EINVAL, "Expected _strlwr_s to return EINVAL, got %d\n", ret);
1933 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1934
1935 errno = EBADF;
1936 ret = p_strlwr_s(NULL, sizeof(buffer));
1937 ok(ret == EINVAL, "Expected _strlwr_s to return EINVAL, got %d\n", ret);
1938 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1939
1940 errno = EBADF;
1941 ret = p_strlwr_s(buffer, 0);
1942 ok(ret == EINVAL, "Expected _strlwr_s to return EINVAL, got %d\n", ret);
1943 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1944
1945 strcpy(buffer, "GoRrIsTeR");
1946 errno = EBADF;
1947 ret = p_strlwr_s(buffer, 5);
1948 ok(ret == EINVAL, "Expected _strlwr_s to return EINVAL, got %d\n", ret);
1949 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1950 ok(!memcmp(buffer, "\0oRrIsTeR", sizeof("\0oRrIsTeR")),
1951 "Expected the output buffer to be \"\\0oRrIsTeR\"\n");
1952
1953 strcpy(buffer, "GoRrIsTeR");
1954 errno = EBADF;
1955 ret = p_strlwr_s(buffer, sizeof("GoRrIsTeR") - 1);
1956 ok(ret == EINVAL, "Expected _strlwr_s to return EINVAL, got %d\n", ret);
1957 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1958 ok(!memcmp(buffer, "\0oRrIsTeR", sizeof("\0oRrIsTeR")),
1959 "Expected the output buffer to be \"\\0oRrIsTeR\"\n");
1960
1961 strcpy(buffer, "GoRrIsTeR");
1962 ret = p_strlwr_s(buffer, sizeof("GoRrIsTeR"));
1963 ok(ret == 0, "Expected _strlwr_s to return 0, got %d\n", ret);
1964 ok(!strcmp(buffer, "gorrister"),
1965 "Expected the output buffer to be \"gorrister\", got \"%s\"\n",
1966 buffer);
1967
1968 memcpy(buffer, "GoRrIsTeR\0ELLEN", sizeof("GoRrIsTeR\0ELLEN"));
1969 ret = p_strlwr_s(buffer, sizeof(buffer));
1970 ok(ret == 0, "Expected _strlwr_s to return 0, got %d\n", ret);
1971 ok(!memcmp(buffer, "gorrister\0ELLEN", sizeof("gorrister\0ELLEN")),
1972 "Expected the output buffer to be \"gorrister\\0ELLEN\", got \"%s\"\n",
1973 buffer);
1974 }
1975
1976 static void test_wcsncat_s(void)
1977 {
1978 static wchar_t abcW[] = {'a','b','c',0};
1979 int ret;
1980 wchar_t dst[4];
1981 wchar_t src[4];
1982
1983 if (!p_wcsncat_s)
1984 {
1985 win_skip("skipping wcsncat_s tests\n");
1986 return;
1987 }
1988
1989 memcpy(src, abcW, sizeof(abcW));
1990 dst[0] = 0;
1991 ret = p_wcsncat_s(NULL, 4, src, 4);
1992 ok(ret == EINVAL, "err = %d\n", ret);
1993 ret = p_wcsncat_s(dst, 0, src, 4);
1994 ok(ret == EINVAL, "err = %d\n", ret);
1995 ret = p_wcsncat_s(dst, 0, src, _TRUNCATE);
1996 ok(ret == EINVAL, "err = %d\n", ret);
1997 ret = p_wcsncat_s(dst, 4, NULL, 0);
1998 ok(ret == 0, "err = %d\n", ret);
1999
2000 dst[0] = 0;
2001 ret = p_wcsncat_s(dst, 2, src, 4);
2002 ok(ret == ERANGE, "err = %d\n", ret);
2003
2004 dst[0] = 0;
2005 ret = p_wcsncat_s(dst, 2, src, _TRUNCATE);
2006 ok(ret == STRUNCATE, "err = %d\n", ret);
2007 ok(dst[0] == 'a' && dst[1] == 0, "dst is %s\n", wine_dbgstr_w(dst));
2008
2009 memcpy(dst, abcW, sizeof(abcW));
2010 dst[3] = 'd';
2011 ret = p_wcsncat_s(dst, 4, src, 4);
2012 ok(ret == EINVAL, "err = %d\n", ret);
2013 }
2014
2015 static void test__mbsnbcat_s(void)
2016 {
2017 unsigned char dest[16];
2018 const unsigned char first[] = "dinosaur";
2019 const unsigned char second[] = "duck";
2020 int ret;
2021
2022 if (!p_mbsnbcat_s)
2023 {
2024 win_skip("Skipping _mbsnbcat_s tests\n");
2025 return;
2026 }
2027
2028 /* Test invalid arguments. */
2029 ret = p_mbsnbcat_s(NULL, 0, NULL, 0);
2030 ok(ret == 0, "Expected _mbsnbcat_s to return 0, got %d\n", ret);
2031
2032 errno = EBADF;
2033 ret = p_mbsnbcat_s(NULL, 10, NULL, 0);
2034 ok(ret == EINVAL, "Expected _mbsnbcat_s to return EINVAL, got %d\n", ret);
2035 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2036
2037 errno = EBADF;
2038 ret = p_mbsnbcat_s(NULL, 0, NULL, 10);
2039 ok(ret == EINVAL, "Expected _mbsnbcat_s to return EINVAL, got %d\n", ret);
2040 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2041
2042 memset(dest, 'X', sizeof(dest));
2043 errno = EBADF;
2044 ret = p_mbsnbcat_s(dest, 0, NULL, 0);
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] == 'X', "Expected the output buffer to be untouched\n");
2048
2049 memset(dest, 'X', sizeof(dest));
2050 errno = EBADF;
2051 ret = p_mbsnbcat_s(dest, 0, second, 0);
2052 ok(ret == EINVAL, "Expected _mbsnbcat_s to return EINVAL, got %d\n", ret);
2053 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2054 ok(dest[0] == 'X', "Expected the output buffer to be untouched\n");
2055
2056 memset(dest, 'X', sizeof(dest));
2057 errno = EBADF;
2058 ret = p_mbsnbcat_s(dest, sizeof(dest), NULL, 0);
2059 ok(ret == EINVAL, "Expected _mbsnbcat_s to return EINVAL, got %d\n", ret);
2060 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2061 ok(dest[0] == '\0', "Expected the output buffer to be null terminated\n");
2062
2063 memset(dest, 'X', sizeof(dest));
2064 errno = EBADF;
2065 ret = p_mbsnbcat_s(dest, sizeof(dest), NULL, 10);
2066 ok(ret == EINVAL, "Expected _mbsnbcat_s to return EINVAL, got %d\n", ret);
2067 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2068 ok(dest[0] == '\0', "Expected the output buffer to be null terminated\n");
2069
2070 memset(dest, 'X', sizeof(dest));
2071 dest[0] = '\0';
2072 ret = p_mbsnbcat_s(dest, sizeof(dest), second, sizeof(second));
2073 ok(ret == 0, "Expected _mbsnbcat_s to return 0, got %d\n", ret);
2074 ok(!memcmp(dest, second, sizeof(second)),
2075 "Expected the output buffer string to be \"duck\"\n");
2076
2077 /* Test source truncation behavior. */
2078 memset(dest, 'X', sizeof(dest));
2079 memcpy(dest, first, sizeof(first));
2080 ret = p_mbsnbcat_s(dest, sizeof(dest), second, 0);
2081 ok(ret == 0, "Expected _mbsnbcat_s to return 0, got %d\n", ret);
2082 ok(!memcmp(dest, first, sizeof(first)),
2083 "Expected the output buffer string to be \"dinosaur\"\n");
2084
2085 memset(dest, 'X', sizeof(dest));
2086 memcpy(dest, first, sizeof(first));
2087 ret = p_mbsnbcat_s(dest, sizeof(dest), second, sizeof(second));
2088 ok(ret == 0, "Expected _mbsnbcat_s to return 0, got %d\n", ret);
2089 ok(!memcmp(dest, "dinosaurduck", sizeof("dinosaurduck")),
2090 "Expected the output buffer string to be \"dinosaurduck\"\n");
2091
2092 memset(dest, 'X', sizeof(dest));
2093 memcpy(dest, first, sizeof(first));
2094 ret = p_mbsnbcat_s(dest, sizeof(dest), second, sizeof(second) + 1);
2095 ok(ret == 0, "Expected _mbsnbcat_s to return 0, got %d\n", ret);
2096 ok(!memcmp(dest, "dinosaurduck", sizeof("dinosaurduck")),
2097 "Expected the output buffer string to be \"dinosaurduck\"\n");
2098
2099 memset(dest, 'X', sizeof(dest));
2100 memcpy(dest, first, sizeof(first));
2101 ret = p_mbsnbcat_s(dest, sizeof(dest), second, sizeof(second) - 1);
2102 ok(ret == 0, "Expected _mbsnbcat_s to return 0, got %d\n", ret);
2103 ok(!memcmp(dest, "dinosaurduck", sizeof("dinosaurduck")),
2104 "Expected the output buffer string to be \"dinosaurduck\"\n");
2105
2106 memset(dest, 'X', sizeof(dest));
2107 memcpy(dest, first, sizeof(first));
2108 ret = p_mbsnbcat_s(dest, sizeof(dest), second, sizeof(second) - 2);
2109 ok(ret == 0, "Expected _mbsnbcat_s to return 0, got %d\n", ret);
2110 ok(!memcmp(dest, "dinosaurduc", sizeof("dinosaurduc")),
2111 "Expected the output buffer string to be \"dinosaurduc\"\n");
2112
2113 /* Test destination truncation behavior. */
2114 memset(dest, 'X', sizeof(dest));
2115 memcpy(dest, first, sizeof(first));
2116 errno = EBADF;
2117 ret = p_mbsnbcat_s(dest, sizeof(first) - 1, second, sizeof(second));
2118 ok(ret == EINVAL, "Expected _mbsnbcat_s to return EINVAL, got %d\n", ret);
2119 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2120 ok(!memcmp(dest, "\0inosaur", sizeof("\0inosaur") - 1),
2121 "Expected the output buffer string to be \"\\0inosaur\" without ending null terminator\n");
2122
2123 memset(dest, 'X', sizeof(dest));
2124 memcpy(dest, first, sizeof(first));
2125 errno = EBADF;
2126 ret = p_mbsnbcat_s(dest, sizeof(first), second, sizeof(second));
2127 ok(ret == ERANGE, "Expected _mbsnbcat_s to return ERANGE, got %d\n", ret);
2128 ok(errno == ERANGE, "Expected errno to be ERANGE, got %d\n", errno);
2129 ok(!memcmp(dest, "\0inosaurd", sizeof("\0inosaurd") - 1),
2130 "Expected the output buffer string to be \"\\0inosaurd\" without ending null terminator\n");
2131
2132 memset(dest, 'X', sizeof(dest));
2133 memcpy(dest, first, sizeof(first));
2134 errno = EBADF;
2135 ret = p_mbsnbcat_s(dest, sizeof(first) + 1, second, sizeof(second));
2136 ok(ret == ERANGE, "Expected _mbsnbcat_s to return ERANGE, got %d\n", ret);
2137 ok(errno == ERANGE, "Expected errno to be ERANGE, got %d\n", errno);
2138 ok(!memcmp(dest, "\0inosaurdu", sizeof("\0inosaurdu") - 1),
2139 "Expected the output buffer string to be \"\\0inosaurdu\" without ending null terminator\n");
2140 }
2141
2142 static void test__mbsupr_s(void)
2143 {
2144 errno_t ret;
2145 unsigned char buffer[20];
2146
2147 if (!p_mbsupr_s)
2148 {
2149 win_skip("Skipping _mbsupr_s tests\n");
2150 return;
2151 }
2152
2153 errno = EBADF;
2154 ret = p_mbsupr_s(NULL, 0);
2155 ok(ret == 0, "Expected _mbsupr_s to return 0, got %d\n", ret);
2156
2157 errno = EBADF;
2158 ret = p_mbsupr_s(NULL, sizeof(buffer));
2159 ok(ret == EINVAL, "Expected _mbsupr_s to return EINVAL, got %d\n", ret);
2160 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2161
2162 errno = EBADF;
2163 ret = p_mbsupr_s(buffer, 0);
2164 ok(ret == EINVAL, "Expected _mbsupr_s to return EINVAL, got %d\n", ret);
2165 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2166
2167 memcpy(buffer, "abcdefgh", sizeof("abcdefgh"));
2168 errno = EBADF;
2169 ret = p_mbsupr_s(buffer, sizeof("abcdefgh"));
2170 ok(ret == 0, "Expected _mbsupr_s to return 0, got %d\n", ret);
2171 ok(!memcmp(buffer, "ABCDEFGH", sizeof("ABCDEFGH")),
2172 "Expected the output buffer to be \"ABCDEFGH\", got \"%s\"\n",
2173 buffer);
2174
2175 memcpy(buffer, "abcdefgh", sizeof("abcdefgh"));
2176 errno = EBADF;
2177 ret = p_mbsupr_s(buffer, sizeof(buffer));
2178 ok(ret == 0, "Expected _mbsupr_s to return 0, got %d\n", ret);
2179 ok(!memcmp(buffer, "ABCDEFGH", sizeof("ABCDEFGH")),
2180 "Expected the output buffer to be \"ABCDEFGH\", got \"%s\"\n",
2181 buffer);
2182
2183 memcpy(buffer, "abcdefgh", sizeof("abcdefgh"));
2184 errno = EBADF;
2185 ret = p_mbsupr_s(buffer, 4);
2186 ok(ret == EINVAL, "Expected _mbsupr_s to return EINVAL, got %d\n", ret);
2187 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2188
2189 memcpy(buffer, "abcdefgh\0ijklmnop", sizeof("abcdefgh\0ijklmnop"));
2190 errno = EBADF;
2191 ret = p_mbsupr_s(buffer, sizeof(buffer));
2192 ok(ret == 0, "Expected _mbsupr_s to return 0, got %d\n", ret);
2193 ok(!memcmp(buffer, "ABCDEFGH\0ijklmnop", sizeof("ABCDEFGH\0ijklmnop")),
2194 "Expected the output buffer to be \"ABCDEFGH\\0ijklmnop\", got \"%s\"\n",
2195 buffer);
2196
2197 }
2198
2199 static void test__mbslwr_s(void)
2200 {
2201 errno_t ret;
2202 unsigned char buffer[20];
2203
2204 if (!p_mbslwr_s)
2205 {
2206 win_skip("Skipping _mbslwr_s tests\n");
2207 return;
2208 }
2209
2210 errno = EBADF;
2211 ret = p_mbslwr_s(NULL, 0);
2212 ok(ret == 0, "Expected _mbslwr_s to return 0, got %d\n", ret);
2213
2214 errno = EBADF;
2215 ret = p_mbslwr_s(NULL, sizeof(buffer));
2216 ok(ret == EINVAL, "Expected _mbslwr_s to return EINVAL, got %d\n", ret);
2217 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2218
2219 errno = EBADF;
2220 ret = p_mbslwr_s(buffer, 0);
2221 ok(ret == EINVAL, "Expected _mbslwr_s to return EINVAL, got %d\n", ret);
2222 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2223
2224 memcpy(buffer, "ABCDEFGH", sizeof("ABCDEFGH"));
2225 errno = EBADF;
2226 ret = p_mbslwr_s(buffer, sizeof("ABCDEFGH"));
2227 ok(ret == 0, "Expected _mbslwr_s to return 0, got %d\n", ret);
2228 ok(!memcmp(buffer, "abcdefgh", sizeof("abcdefgh")),
2229 "Expected the output buffer to be \"abcdefgh\", got \"%s\"\n",
2230 buffer);
2231
2232 memcpy(buffer, "ABCDEFGH", sizeof("ABCDEFGH"));
2233 errno = EBADF;
2234 ret = p_mbslwr_s(buffer, sizeof(buffer));
2235 ok(ret == 0, "Expected _mbslwr_s to return 0, got %d\n", ret);
2236 ok(!memcmp(buffer, "abcdefgh", sizeof("abcdefgh")),
2237 "Expected the output buffer to be \"abcdefgh\", got \"%s\"\n",
2238 buffer);
2239
2240 memcpy(buffer, "ABCDEFGH", sizeof("ABCDEFGH"));
2241 errno = EBADF;
2242 ret = p_mbslwr_s(buffer, 4);
2243 ok(ret == EINVAL, "Expected _mbslwr_s to return EINVAL, got %d\n", ret);
2244 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2245
2246 memcpy(buffer, "ABCDEFGH\0IJKLMNOP", sizeof("ABCDEFGH\0IJKLMNOP"));
2247 errno = EBADF;
2248 ret = p_mbslwr_s(buffer, sizeof(buffer));
2249 ok(ret == 0, "Expected _mbslwr_s to return 0, got %d\n", ret);
2250 ok(!memcmp(buffer, "abcdefgh\0IJKLMNOP", sizeof("abcdefgh\0IJKLMNOP")),
2251 "Expected the output buffer to be \"abcdefgh\\0IJKLMNOP\", got \"%s\"\n",
2252 buffer);
2253 }
2254
2255 static void test__mbstok(void)
2256 {
2257 const unsigned char delim[] = "t";
2258
2259 char str[] = "!.!test";
2260 unsigned char *ret;
2261
2262 strtok(str, "!");
2263
2264 ret = _mbstok(NULL, delim);
2265 /* most versions of msvcrt use the same buffer for strtok and _mbstok */
2266 ok(!ret || broken((char*)ret==str+4),
2267 "_mbstok(NULL, \"t\") = %p, expected NULL (%p)\n", ret, str);
2268
2269 ret = _mbstok(NULL, delim);
2270 ok(!ret, "_mbstok(NULL, \"t\") = %p, expected NULL\n", ret);
2271 }
2272
2273 static void test__ultoa_s(void)
2274 {
2275 errno_t ret;
2276 char buffer[33];
2277
2278 if (!p_ultoa_s)
2279 {
2280 win_skip("Skipping _ultoa_s tests\n");
2281 return;
2282 }
2283
2284 errno = EBADF;
2285 ret = p_ultoa_s(0, NULL, 0, 0);
2286 ok(ret == EINVAL, "Expected _ultoa_s to return EINVAL, got %d\n", ret);
2287 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2288
2289 memset(buffer, 'X', sizeof(buffer));
2290 errno = EBADF;
2291 ret = p_ultoa_s(0, buffer, 0, 0);
2292 ok(ret == EINVAL, "Expected _ultoa_s to return EINVAL, got %d\n", ret);
2293 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2294 ok(buffer[0] == 'X', "Expected the output buffer to be untouched\n");
2295
2296 memset(buffer, 'X', sizeof(buffer));
2297 errno = EBADF;
2298 ret = p_ultoa_s(0, buffer, sizeof(buffer), 0);
2299 ok(ret == EINVAL, "Expected _ultoa_s to return EINVAL, got %d\n", ret);
2300 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2301 ok(buffer[0] == '\0', "Expected the output buffer to be null terminated\n");
2302
2303 memset(buffer, 'X', sizeof(buffer));
2304 errno = EBADF;
2305 ret = p_ultoa_s(0, buffer, sizeof(buffer), 64);
2306 ok(ret == EINVAL, "Expected _ultoa_s to return EINVAL, got %d\n", ret);
2307 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2308 ok(buffer[0] == '\0', "Expected the output buffer to be null terminated\n");
2309
2310 memset(buffer, 'X', sizeof(buffer));
2311 errno = EBADF;
2312 ret = p_ultoa_s(12345678, buffer, 4, 10);
2313 ok(ret == ERANGE, "Expected _ultoa_s to return ERANGE, got %d\n", ret);
2314 ok(errno == ERANGE, "Expected errno to be ERANGE, got %d\n", errno);
2315 ok(!memcmp(buffer, "\000765", 4),
2316 "Expected the output buffer to be null terminated with truncated output\n");
2317
2318 memset(buffer, 'X', sizeof(buffer));
2319 errno = EBADF;
2320 ret = p_ultoa_s(12345678, buffer, 8, 10);
2321 ok(ret == ERANGE, "Expected _ultoa_s to return ERANGE, got %d\n", ret);
2322 ok(errno == ERANGE, "Expected errno to be ERANGE, got %d\n", errno);
2323 ok(!memcmp(buffer, "\0007654321", 8),
2324 "Expected the output buffer to be null terminated with truncated output\n");
2325
2326 ret = p_ultoa_s(12345678, buffer, 9, 10);
2327 ok(ret == 0, "Expected _ultoa_s to return 0, got %d\n", ret);
2328 ok(!strcmp(buffer, "12345678"),
2329 "Expected output buffer string to be \"12345678\", got \"%s\"\n",
2330 buffer);
2331
2332 ret = p_ultoa_s(43690, buffer, sizeof(buffer), 2);
2333 ok(ret == 0, "Expected _ultoa_s to return 0, got %d\n", ret);
2334 ok(!strcmp(buffer, "1010101010101010"),
2335 "Expected output buffer string to be \"1010101010101010\", got \"%s\"\n",
2336 buffer);
2337
2338 ret = p_ultoa_s(1092009, buffer, sizeof(buffer), 36);
2339 ok(ret == 0, "Expected _ultoa_s to return 0, got %d\n", ret);
2340 ok(!strcmp(buffer, "nell"),
2341 "Expected output buffer string to be \"nell\", got \"%s\"\n",
2342 buffer);
2343
2344 ret = p_ultoa_s(5704, buffer, sizeof(buffer), 18);
2345 ok(ret == 0, "Expected _ultoa_s to return 0, got %d\n", ret);
2346 ok(!strcmp(buffer, "hag"),
2347 "Expected output buffer string to be \"hag\", got \"%s\"\n",
2348 buffer);
2349 }
2350
2351 static void test_wctob(void)
2352 {
2353 int ret;
2354
2355 if(!p_wctob || !setlocale(LC_ALL, "chinese-traditional")) {
2356 win_skip("Skipping wctob tests\n");
2357 return;
2358 }
2359
2360 ret = p_wctob(0x8141);
2361 ok(ret == EOF, "ret = %x\n", ret);
2362
2363 ret = p_wctob(0x81);
2364 ok(ret == EOF, "ret = %x\n", ret);
2365
2366 ret = p_wctob(0xe0);
2367 ok(ret == 0x61, "ret = %x\n", ret);
2368
2369 _setmbcp(1250);
2370 ret = p_wctob(0x81);
2371 ok(ret == EOF, "ret = %x\n", ret);
2372
2373 setlocale(LC_ALL, "C");
2374 ret = p_wctob(0x8141);
2375 ok(ret == EOF, "ret = %x\n", ret);
2376
2377 ret = p_wctob(0x81);
2378 ok(ret == (int)(char)0x81, "ret = %x\n", ret);
2379
2380 ret = p_wctob(0x9f);
2381 ok(ret == (int)(char)0x9f, "ret = %x\n", ret);
2382
2383 ret = p_wctob(0xe0);
2384 ok(ret == (int)(char)0xe0, "ret = %x\n", ret);
2385 }
2386 static void test_wctomb(void)
2387 {
2388 mbstate_t state;
2389 unsigned char dst[10];
2390 size_t ret;
2391
2392 if(!p_wcrtomb || !setlocale(LC_ALL, "Japanese_Japan.932")) {
2393 win_skip("wcrtomb tests\n");
2394 return;
2395 }
2396
2397 ret = p_wcrtomb(NULL, 0x3042, NULL);
2398 ok(ret == 2, "wcrtomb did not return 2\n");
2399
2400 state = 1;
2401 dst[2] = 'a';
2402 ret = p_wcrtomb((char*)dst, 0x3042, &state);
2403 ok(ret == 2, "wcrtomb did not return 2\n");
2404 ok(state == 0, "state != 0\n");
2405 ok(dst[0] == 0x82, "dst[0] = %x, expected 0x82\n", dst[0]);
2406 ok(dst[1] == 0xa0, "dst[1] = %x, expected 0xa0\n", dst[1]);
2407 ok(dst[2] == 'a', "dst[2] != 'a'\n");
2408
2409 ret = p_wcrtomb((char*)dst, 0x3043, NULL);
2410 ok(ret == 2, "wcrtomb did not return 2\n");
2411 ok(dst[0] == 0x82, "dst[0] = %x, expected 0x82\n", dst[0]);
2412 ok(dst[1] == 0xa1, "dst[1] = %x, expected 0xa1\n", dst[1]);
2413
2414 ret = p_wcrtomb((char*)dst, 0x20, NULL);
2415 ok(ret == 1, "wcrtomb did not return 1\n");
2416 ok(dst[0] == 0x20, "dst[0] = %x, expected 0x20\n", dst[0]);
2417
2418 ret = p_wcrtomb((char*)dst, 0xffff, NULL);
2419 ok(ret == -1, "wcrtomb did not return -1\n");
2420 ok(dst[0] == 0x3f, "dst[0] = %x, expected 0x3f\n", dst[0]);
2421
2422 setlocale(LC_ALL, "C");
2423 }
2424
2425 static void test_tolower(void)
2426 {
2427 char ch, lch;
2428 int ret, len;
2429
2430 /* test C locale when locale was never changed */
2431 ret = p_tolower(0x41);
2432 ok(ret == 0x61, "ret = %x\n", ret);
2433
2434 ret = p_tolower(0xF4);
2435 ok(ret == 0xF4, "ret = %x\n", ret);
2436
2437 errno = 0xdeadbeef;
2438 ret = p_tolower((char)0xF4);
2439 todo_wine ok(ret == (char)0xF4, "ret = %x\n", ret);
2440 todo_wine ok(errno == 0xdeadbeef, "errno = %d\n", errno);
2441
2442 errno = 0xdeadbeef;
2443 ret = p_tolower((char)0xD0);
2444 todo_wine ok(ret == (char)0xD0, "ret = %x\n", ret);
2445 todo_wine ok(errno == 0xdeadbeef, "errno = %d\n", errno);
2446
2447 /* test C locale after setting locale */
2448 if(!setlocale(LC_ALL, "us")) {
2449 win_skip("skipping tolower tests that depends on locale\n");
2450 return;
2451 }
2452 setlocale(LC_ALL, "C");
2453
2454 ch = 0xF4;
2455 errno = 0xdeadbeef;
2456 ret = p_tolower(ch);
2457 len = LCMapStringA(0, LCMAP_LOWERCASE, &ch, 1, &lch, 1);
2458 if(len)
2459 ok(ret==(unsigned char)lch || broken(ret==ch)/*WinXP-*/, "ret = %x\n", ret);
2460 else
2461 ok(ret == ch, "ret = %x\n", ret);
2462 if(!len || ret==(unsigned char)lch)
2463 ok(errno == EILSEQ, "errno = %d\n", errno);
2464
2465 ch = 0xD0;
2466 errno = 0xdeadbeef;
2467 ret = p_tolower(ch);
2468 len = LCMapStringA(0, LCMAP_LOWERCASE, &ch, 1, &lch, 1);
2469 if(len)
2470 ok(ret==(unsigned char)lch || broken(ret==ch)/*WinXP-*/, "ret = %x\n", ret);
2471 else
2472 ok(ret == ch, "ret = %x\n", ret);
2473 if(!len || ret==(unsigned char)lch)
2474 ok(errno == EILSEQ, "errno = %d\n", errno);
2475
2476 ret = p_tolower(0xD0);
2477 ok(ret == 0xD0, "ret = %x\n", ret);
2478
2479 ok(setlocale(LC_ALL, "us") != NULL, "setlocale failed\n");
2480
2481 ret = p_tolower((char)0xD0);
2482 ok(ret == 0xF0, "ret = %x\n", ret);
2483
2484 ret = p_tolower(0xD0);
2485 ok(ret == 0xF0, "ret = %x\n", ret);
2486
2487 setlocale(LC_ALL, "C");
2488 }
2489
2490 static void test__atodbl(void)
2491 {
2492 _CRT_DOUBLE d;
2493 char num[32];
2494 int ret;
2495
2496 if(!p__atodbl_l) {
2497 /* Old versions of msvcrt use different values for _OVERFLOW and _UNDERFLOW
2498 * Because of this lets skip _atodbl tests when _atodbl_l is not available */
2499 win_skip("_atodbl_l is not available\n");
2500 return;
2501 }
2502
2503 num[0] = 0;
2504 ret = p__atodbl_l(&d, num, NULL);
2505 ok(ret == 0, "_atodbl_l(&d, \"\", NULL) returned %d, expected 0\n", ret);
2506 ok(d.x == 0, "d.x = %lf, expected 0\n", d.x);
2507 ret = _atodbl(&d, num);
2508 ok(ret == 0, "_atodbl(&d, \"\") returned %d, expected 0\n", ret);
2509 ok(d.x == 0, "d.x = %lf, expected 0\n", d.x);
2510
2511 strcpy(num, "t");
2512 ret = p__atodbl_l(&d, num, NULL);
2513 ok(ret == 0, "_atodbl_l(&d, \"t\", NULL) returned %d, expected 0\n", ret);
2514 ok(d.x == 0, "d.x = %lf, expected 0\n", d.x);
2515 ret = _atodbl(&d, num);
2516 ok(ret == 0, "_atodbl(&d, \"t\") returned %d, expected 0\n", ret);
2517 ok(d.x == 0, "d.x = %lf, expected 0\n", d.x);
2518
2519 strcpy(num, "0");
2520 ret = p__atodbl_l(&d, num, NULL);
2521 ok(ret == 0, "_atodbl_l(&d, \"0\", NULL) returned %d, expected 0\n", ret);
2522 ok(d.x == 0, "d.x = %lf, expected 0\n", d.x);
2523 ret = _atodbl(&d, num);
2524 ok(ret == 0, "_atodbl(&d, \"0\") returned %d, expected 0\n", ret);
2525 ok(d.x == 0, "d.x = %lf, expected 0\n", d.x);
2526
2527 strcpy(num, "123");
2528 ret = p__atodbl_l(&d, num, NULL);
2529 ok(ret == 0, "_atodbl_l(&d, \"123\", NULL) returned %d, expected 0\n", ret);
2530 ok(d.x == 123, "d.x = %lf, expected 123\n", d.x);
2531 ret = _atodbl(&d, num);
2532 ok(ret == 0, "_atodbl(&d, \"123\") returned %d, expected 0\n", ret);
2533 ok(d.x == 123, "d.x = %lf, expected 123\n", d.x);
2534
2535 strcpy(num, "1e-309");
2536 ret = p__atodbl_l(&d, num, NULL);
2537 ok(ret == _UNDERFLOW, "_atodbl_l(&d, \"1e-309\", NULL) returned %d, expected _UNDERFLOW\n", ret);
2538 ok(d.x!=0 && almost_equal(d.x, 0), "d.x = %le, expected 0\n", d.x);
2539 ret = _atodbl(&d, num);
2540 ok(ret == _UNDERFLOW, "_atodbl(&d, \"1e-309\") returned %d, expected _UNDERFLOW\n", ret);
2541 ok(d.x!=0 && almost_equal(d.x, 0), "d.x = %le, expected 0\n", d.x);
2542
2543 strcpy(num, "1e309");
2544 ret = p__atodbl_l(&d, num, NULL);
2545 ok(ret == _OVERFLOW, "_atodbl_l(&d, \"1e309\", NULL) returned %d, expected _OVERFLOW\n", ret);
2546 ret = _atodbl(&d, num);
2547 ok(ret == _OVERFLOW, "_atodbl(&d, \"1e309\") returned %d, expected _OVERFLOW\n", ret);
2548 }
2549
2550 static void test__stricmp(void)
2551 {
2552 int ret;
2553
2554 ret = _stricmp("test", "test");
2555 ok(ret == 0, "_stricmp returned %d\n", ret);
2556 ret = _stricmp("a", "z");
2557 ok(ret < 0, "_stricmp returned %d\n", ret);
2558 ret = _stricmp("z", "a");
2559 ok(ret > 0, "_stricmp returned %d\n", ret);
2560 ret = _stricmp("\xa5", "\xb9");
2561 ok(ret < 0, "_stricmp returned %d\n", ret);
2562
2563 if(!setlocale(LC_ALL, "polish")) {
2564 win_skip("stricmp tests\n");
2565 return;
2566 }
2567
2568 ret = _stricmp("test", "test");
2569 ok(ret == 0, "_stricmp returned %d\n", ret);
2570 ret = _stricmp("a", "z");
2571 ok(ret < 0, "_stricmp returned %d\n", ret);
2572 ret = _stricmp("z", "a");
2573 ok(ret > 0, "_stricmp returned %d\n", ret);
2574 ret = _stricmp("\xa5", "\xb9");
2575 ok(ret == 0, "_stricmp returned %d\n", ret);
2576 ret = _stricmp("a", "\xb9");
2577 ok(ret < 0, "_stricmp returned %d\n", ret);
2578
2579 setlocale(LC_ALL, "C");
2580 }
2581
2582 static void test__wcstoi64(void)
2583 {
2584 static const WCHAR digit[] = { '9', 0 };
2585 static const WCHAR stock[] = { 0x3231, 0 }; /* PARENTHESIZED IDEOGRAPH STOCK */
2586 static const WCHAR tamil[] = { 0x0bef, 0 }; /* TAMIL DIGIT NINE */
2587 static const WCHAR thai[] = { 0x0e59, 0 }; /* THAI DIGIT NINE */
2588 static const WCHAR fullwidth[] = { 0xff19, 0 }; /* FULLWIDTH DIGIT NINE */
2589 static const WCHAR hex[] = { 0xff19, 'f', 0x0e59, 0xff46, 0 };
2590
2591 __int64 res;
2592 unsigned __int64 ures;
2593 WCHAR *endpos;
2594
2595 if (!p_wcstoi64 || !p_wcstoui64) {
2596 win_skip("_wcstoi64 or _wcstoui64 not found\n");
2597 return;
2598 }
2599
2600 res = p_wcstoi64(digit, NULL, 10);
2601 ok(res == 9, "res != 9\n");
2602 res = p_wcstoi64(stock, &endpos, 10);
2603 ok(res == 0, "res != 0\n");
2604 ok(endpos == stock, "Incorrect endpos (%p-%p)\n", stock, endpos);
2605 res = p_wcstoi64(tamil, &endpos, 10);
2606 ok(res == 0, "res != 0\n");
2607 ok(endpos == tamil, "Incorrect endpos (%p-%p)\n", tamil, endpos);
2608 res = p_wcstoi64(thai, NULL, 10);
2609 todo_wine ok(res == 9, "res != 9\n");
2610 res = p_wcstoi64(fullwidth, NULL, 10);
2611 todo_wine ok(res == 9, "res != 9\n");
2612 res = p_wcstoi64(hex, NULL, 16);
2613 todo_wine ok(res == 0x9f9, "res != 0x9f9\n");
2614
2615 ures = p_wcstoui64(digit, NULL, 10);
2616 ok(ures == 9, "ures != 9\n");
2617 ures = p_wcstoui64(stock, &endpos, 10);
2618 ok(ures == 0, "ures != 0\n");
2619 ok(endpos == stock, "Incorrect endpos (%p-%p)\n", stock, endpos);
2620 ures = p_wcstoui64(tamil, &endpos, 10);
2621 ok(ures == 0, "ures != 0\n");
2622 ok(endpos == tamil, "Incorrect endpos (%p-%p)\n", tamil, endpos);
2623 ures = p_wcstoui64(thai, NULL, 10);
2624 todo_wine ok(ures == 9, "ures != 9\n");
2625 ures = p_wcstoui64(fullwidth, NULL, 10);
2626 todo_wine ok(ures == 9, "ures != 9\n");
2627 ures = p_wcstoui64(hex, NULL, 16);
2628 todo_wine ok(ures == 0x9f9, "ures != 0x9f9\n");
2629
2630 return;
2631 }
2632
2633 static void test_atoi(void)
2634 {
2635 int r;
2636
2637 r = atoi("0");
2638 ok(r == 0, "atoi(0) = %d\n", r);
2639
2640 r = atoi("-1");
2641 ok(r == -1, "atoi(-1) = %d\n", r);
2642
2643 r = atoi("1");
2644 ok(r == 1, "atoi(1) = %d\n", r);
2645
2646 r = atoi("4294967296");
2647 ok(r == 0, "atoi(4294967296) = %d\n", r);
2648 }
2649
2650 static void test_atof(void)
2651 {
2652 double d;
2653
2654 d = atof("0.0");
2655 ok(almost_equal(d, 0.0), "d = %lf\n", d);
2656
2657 d = atof("1.0");
2658 ok(almost_equal(d, 1.0), "d = %lf\n", d);
2659
2660 d = atof("-1.0");
2661 ok(almost_equal(d, -1.0), "d = %lf\n", d);
2662
2663 if (!p__atof_l)
2664 {
2665 win_skip("_atof_l not found\n");
2666 return;
2667 }
2668
2669 errno = EBADF;
2670 d = atof(NULL);
2671 ok(almost_equal(d, 0.0), "d = %lf\n", d);
2672 ok(errno == EINVAL, "errno = %x\n", errno);
2673
2674 errno = EBADF;
2675 d = p__atof_l(NULL, NULL);
2676 ok(almost_equal(d, 0.0), "d = %lf\n", d);
2677 ok(errno == EINVAL, "errno = %x\n", errno);
2678 }
2679
2680 static void test_strncpy(void)
2681 {
2682 #define TEST_STRNCPY_LEN 10
2683 char *ret;
2684 char dst[TEST_STRNCPY_LEN + 1];
2685 char not_null_terminated[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0'};
2686
2687 /* strlen(src) > TEST_STRNCPY_LEN */
2688 ret = strncpy(dst, "01234567890123456789", TEST_STRNCPY_LEN);
2689 ok(ret == dst, "ret != dst\n");
2690 ok(!strncmp(dst, "0123456789", TEST_STRNCPY_LEN), "dst != 0123456789\n");
2691
2692 /* without null-terminated */
2693 ret = strncpy(dst, not_null_terminated, TEST_STRNCPY_LEN);
2694 ok(ret == dst, "ret != dst\n");
2695 ok(!strncmp(dst, "0123456789", TEST_STRNCPY_LEN), "dst != 0123456789\n");
2696
2697 /* strlen(src) < TEST_STRNCPY_LEN */
2698 strcpy(dst, "0123456789");
2699 ret = strncpy(dst, "012345", TEST_STRNCPY_LEN);
2700 ok(ret == dst, "ret != dst\n");
2701 ok(!strcmp(dst, "012345"), "dst != 012345\n");
2702 ok(dst[TEST_STRNCPY_LEN - 1] == '\0', "dst[TEST_STRNCPY_LEN - 1] != 0\n");
2703
2704 /* strlen(src) == TEST_STRNCPY_LEN */
2705 ret = strncpy(dst, "0123456789", TEST_STRNCPY_LEN);
2706 ok(ret == dst, "ret != dst\n");
2707 ok(!strncmp(dst, "0123456789", TEST_STRNCPY_LEN), "dst != 0123456789\n");
2708 }
2709
2710 static void test_strxfrm(void)
2711 {
2712 char dest[256];
2713 size_t ret;
2714
2715 /* crashes on old version of msvcrt */
2716 if(p__atodbl_l) {
2717 errno = 0xdeadbeef;
2718 ret = strxfrm(NULL, "src", 1);
2719 ok(ret == INT_MAX, "ret = %d\n", (int)ret);
2720 ok(errno == EINVAL, "errno = %d\n", errno);
2721
2722 errno = 0xdeadbeef;
2723 ret = strxfrm(dest, NULL, 100);
2724 ok(ret == INT_MAX, "ret = %d\n", (int)ret);
2725 ok(errno == EINVAL, "errno = %d\n", errno);
2726 }
2727
2728 ret = strxfrm(NULL, "src", 0);
2729 ok(ret == 3, "ret = %d\n", (int)ret);
2730 dest[0] = 'a';
2731 ret = strxfrm(dest, "src", 0);
2732 ok(ret == 3, "ret = %d\n", (int)ret);
2733 ok(dest[0] == 'a', "dest[0] = %d\n", dest[0]);
2734
2735 dest[3] = 'a';
2736 ret = strxfrm(dest, "src", 5);
2737 ok(ret == 3, "ret = %d\n", (int)ret);
2738 ok(!strcmp(dest, "src"), "dest = %s\n", dest);
2739
2740 errno = 0xdeadbeef;
2741 dest[1] = 'a';
2742 ret = strxfrm(dest, "src", 1);
2743 ok(ret == 3, "ret = %d\n", (int)ret);
2744 ok(dest[0] == 's', "dest[0] = %d\n", dest[0]);
2745 ok(dest[1] == 'a', "dest[1] = %d\n", dest[1]);
2746 ok(errno == 0xdeadbeef, "errno = %d\n", errno);
2747
2748 ret = strxfrm(dest, "", 5);
2749 ok(ret == 0, "ret = %d\n", (int)ret);
2750 ok(!dest[0], "dest[0] = %d\n", dest[0]);
2751
2752 if(!setlocale(LC_ALL, "polish")) {
2753 win_skip("stxfrm tests\n");
2754 return;
2755 }
2756
2757 ret = strxfrm(NULL, "src", 0);
2758 ok(ret < sizeof(dest)-1, "ret = %d\n", (int)ret);
2759 dest[0] = 'a';
2760 ret = strxfrm(dest, "src", 0);
2761 ok(ret < sizeof(dest)-1, "ret = %d\n", (int)ret);
2762 ok(dest[0] == 'a', "dest[0] = %d\n", dest[0]);
2763
2764 ret = strxfrm(dest, "src", ret+1);
2765 ok(ret < sizeof(dest)-1, "ret = %d\n", (int)ret);
2766 ok(dest[0], "dest[0] = 0\n");
2767
2768 errno = 0xdeadbeef;
2769 dest[0] = 'a';
2770 ret = strxfrm(dest, "src", 5);
2771 ok(ret>5 && ret<sizeof(dest)-1, "ret = %d\n", (int)ret);
2772 ok(!dest[0] || broken(!p__atodbl_l && dest[0]=='a'), "dest[0] = %d\n", dest[0]);
2773
2774 setlocale(LC_ALL, "C");
2775 }
2776
2777 static void test__strnset_s(void)
2778 {
2779 char buf[5] = {0};
2780 int r;
2781
2782 if(!p__strnset_s) {
2783 win_skip("_strnset_s not available\n");
2784 return;
2785 }
2786
2787 r = p__strnset_s(NULL, 0, 'a', 0);
2788 ok(r == 0, "r = %d\n", r);
2789
2790 buf[0] = buf[1] = buf[2] = 'b';
2791 r = p__strnset_s(buf, sizeof(buf), 'a', 2);
2792 ok(r == 0, "r = %d\n", r);
2793 ok(!strcmp(buf, "aab"), "buf = %s\n", buf);
2794
2795 r = p__strnset_s(buf, 0, 'a', 0);
2796 ok(r == EINVAL, "r = %d\n", r);
2797
2798 r = p__strnset_s(NULL, 0, 'a', 1);
2799 ok(r == EINVAL, "r = %d\n", r);
2800
2801 buf[3] = 'b';
2802 r = p__strnset_s(buf, sizeof(buf)-1, 'c', 2);
2803 ok(r == EINVAL, "r = %d\n", r);
2804 ok(!buf[0] && buf[1]=='c' && buf[2]=='b', "buf = %s\n", buf);
2805 }
2806
2807 static void test__wcsset_s(void)
2808 {
2809 wchar_t str[10];
2810 int r;
2811
2812 if(!p__wcsset_s) {
2813 win_skip("_wcsset_s not available\n");
2814 return;
2815 }
2816
2817 r = p__wcsset_s(NULL, 0, 'a');
2818 ok(r == EINVAL, "r = %d\n", r);
2819
2820 str[0] = 'a';
2821 r = p__wcsset_s(str, 0, 'a');
2822 ok(r == EINVAL, "r = %d\n", r);
2823 ok(str[0] == 'a', "str[0] = %d\n", str[0]);
2824
2825 str[0] = 'a';
2826 str[1] = 'b';
2827 r = p__wcsset_s(str, 2, 'c');
2828 ok(r == EINVAL, "r = %d\n", r);
2829 ok(!str[0], "str[0] = %d\n", str[0]);
2830 ok(str[1] == 'b', "str[1] = %d\n", str[1]);
2831
2832 str[0] = 'a';
2833 str[1] = 0;
2834 str[2] = 'b';
2835 r = p__wcsset_s(str, 3, 'c');
2836 ok(str[0] == 'c', "str[0] = %d\n", str[0]);
2837 ok(str[1] == 0, "str[1] = %d\n", str[1]);
2838 ok(str[2] == 'b', "str[2] = %d\n", str[2]);
2839 }
2840
2841 START_TEST(string)
2842 {
2843 char mem[100];
2844 static const char xilstring[]="c:/xilinx";
2845 int nLen;
2846
2847 hMsvcrt = GetModuleHandleA("msvcrt.dll");
2848 if (!hMsvcrt)
2849 hMsvcrt = GetModuleHandleA("msvcrtd.dll");
2850 ok(hMsvcrt != 0, "GetModuleHandleA failed\n");
2851 SET(pmemcpy,"memcpy");
2852 p_memcpy_s = (void*)GetProcAddress( hMsvcrt, "memcpy_s" );
2853 p_memmove_s = (void*)GetProcAddress( hMsvcrt, "memmove_s" );
2854 SET(pmemcmp,"memcmp");
2855 SET(p_mbctype,"_mbctype");
2856 SET(p__mb_cur_max,"__mb_cur_max");
2857 pstrcpy_s = (void *)GetProcAddress( hMsvcrt,"strcpy_s" );
2858 pstrcat_s = (void *)GetProcAddress( hMsvcrt,"strcat_s" );
2859 p_mbsnbcat_s = (void *)GetProcAddress( hMsvcrt,"_mbsnbcat_s" );
2860 p_mbsnbcpy_s = (void *)GetProcAddress( hMsvcrt,"_mbsnbcpy_s" );
2861 p__mbscpy_s = (void *)GetProcAddress( hMsvcrt,"_mbscpy_s" );
2862 p_wcscpy_s = (void *)GetProcAddress( hMsvcrt,"wcscpy_s" );
2863 p_wcsncpy_s = (void *)GetProcAddress( hMsvcrt,"wcsncpy_s" );
2864 p_wcsncat_s = (void *)GetProcAddress( hMsvcrt,"wcsncat_s" );
2865 p_wcsupr_s = (void *)GetProcAddress( hMsvcrt,"_wcsupr_s" );
2866 p_strnlen = (void *)GetProcAddress( hMsvcrt,"strnlen" );
2867 p_strtoi64 = (void *)GetProcAddress(hMsvcrt, "_strtoi64");
2868 p_strtoui64 = (void *)GetProcAddress(hMsvcrt, "_strtoui64");
2869 p_wcstoi64 = (void *)GetProcAddress(hMsvcrt, "_wcstoi64");
2870 p_wcstoui64 = (void *)GetProcAddress(hMsvcrt, "_wcstoui64");
2871 pmbstowcs_s = (void *)GetProcAddress(hMsvcrt, "mbstowcs_s");
2872 pwcstombs_s = (void *)GetProcAddress(hMsvcrt, "wcstombs_s");
2873 pwcsrtombs = (void *)GetProcAddress(hMsvcrt, "wcsrtombs");
2874 p_gcvt_s = (void *)GetProcAddress(hMsvcrt, "_gcvt_s");
2875 p_itoa_s = (void *)GetProcAddress(hMsvcrt, "_itoa_s");
2876 p_strlwr_s = (void *)GetProcAddress(hMsvcrt, "_strlwr_s");
2877 p_ultoa_s = (void *)GetProcAddress(hMsvcrt, "_ultoa_s");
2878 p_wcslwr_s = (void*)GetProcAddress(hMsvcrt, "_wcslwr_s");
2879 p_mbsupr_s = (void*)GetProcAddress(hMsvcrt, "_mbsupr_s");
2880 p_mbslwr_s = (void*)GetProcAddress(hMsvcrt, "_mbslwr_s");
2881 p_wctob = (void*)GetProcAddress(hMsvcrt, "wctob");
2882 p_wcrtomb = (void*)GetProcAddress(hMsvcrt, "wcrtomb");
2883 p_tolower = (void*)GetProcAddress(hMsvcrt, "tolower");
2884 p_mbrlen = (void*)GetProcAddress(hMsvcrt, "mbrlen");
2885 p_mbrtowc = (void*)GetProcAddress(hMsvcrt, "mbrtowc");
2886 p_mbsrtowcs = (void*)GetProcAddress(hMsvcrt, "mbsrtowcs");
2887 p__atodbl_l = (void*)GetProcAddress(hMsvcrt, "_atodbl_l");
2888 p__atof_l = (void*)GetProcAddress(hMsvcrt, "_atof_l");
2889 p__strtod_l = (void*)GetProcAddress(hMsvcrt, "_strtod_l");
2890 p__strnset_s = (void*)GetProcAddress(hMsvcrt, "_strnset_s");
2891 p__wcsset_s = (void*)GetProcAddress(hMsvcrt, "_wcsset_s");
2892
2893 /* MSVCRT memcpy behaves like memmove for overlapping moves,
2894 MFC42 CString::Insert seems to rely on that behaviour */
2895 strcpy(mem,xilstring);
2896 nLen=strlen(xilstring);
2897 pmemcpy(mem+5, mem,nLen+1);
2898 ok(pmemcmp(mem+5,xilstring, nLen) == 0,
2899 "Got result %s\n",mem+5);
2900
2901 /* run tolower tests first */
2902 test_tolower();
2903 test_swab();
2904 test_mbcp();
2905 test_mbsspn();
2906 test_mbsspnp();
2907 test_strdup();
2908 test_strcpy_s();
2909 test_memcpy_s();
2910 test_memmove_s();
2911 test_strcat_s();
2912 test__mbsnbcpy_s();
2913 test__mbscpy_s();
2914 test_mbcjisjms();
2915 test_mbcjmsjis();
2916 test_mbbtombc();
2917 test_mbctombb();
2918 test_ismbclegal();
2919 test_strtok();
2920 test__mbstok();
2921 test_wcscpy_s();
2922 test__wcsupr_s();
2923 test_strtol();
2924 test_strnlen();
2925 test__strtoi64();
2926 test__strtod();
2927 test_mbstowcs();
2928 test_gcvt();
2929 test__itoa_s();
2930 test__strlwr_s();
2931 test_wcsncat_s();
2932 test__mbsnbcat_s();
2933 test__ultoa_s();
2934 test__wcslwr_s();
2935 test__mbsupr_s();
2936 test__mbslwr_s();
2937 test_wctob();
2938 test_wctomb();
2939 test__atodbl();
2940 test__stricmp();
2941 test__wcstoi64();
2942 test_atoi();
2943 test_atof();
2944 test_strncpy();
2945 test_strxfrm();
2946 test__strnset_s();
2947 test__wcsset_s();
2948 }