[MSVCRT_WINETEST]
[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 "winbase.h"
23 #include <string.h>
24 #include <mbstring.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <mbctype.h>
28 #include <locale.h>
29 #include <errno.h>
30
31 static char *buf_to_string(const unsigned char *bin, int len, int nr)
32 {
33 static char buf[2][1024];
34 char *w = buf[nr];
35 int i;
36
37 for (i = 0; i < len; i++)
38 {
39 sprintf(w, "%02x ", (unsigned char)bin[i]);
40 w += strlen(w);
41 }
42 return buf[nr];
43 }
44
45 #define expect_eq(expr, value, type, format) { type ret = (expr); ok((value) == ret, #expr " expected " format " got " format "\n", value, ret); }
46 #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)); }
47
48 static void* (__cdecl *pmemcpy)(void *, const void *, size_t n);
49 static int* (__cdecl *pmemcmp)(void *, const void *, size_t n);
50 static int (__cdecl *pstrcpy_s)(char *dst, size_t len, const char *src);
51 static int (__cdecl *pstrcat_s)(char *dst, size_t len, const char *src);
52 static int (__cdecl *p_mbsnbcpy_s)(unsigned char * dst, size_t size, const unsigned char * src, size_t count);
53 static int (__cdecl *p_wcscpy_s)(wchar_t *wcDest, size_t size, const wchar_t *wcSrc);
54 static int (__cdecl *p_wcsupr_s)(wchar_t *str, size_t size);
55 static int *p__mb_cur_max;
56 static unsigned char *p_mbctype;
57
58 #define SETNOFAIL(x,y) x = (void*)GetProcAddress(hMsvcrt,y)
59 #define SET(x,y) SETNOFAIL(x,y); ok(x != NULL, "Export '%s' not found\n", y)
60
61 HMODULE hMsvcrt;
62
63 static void test_swab( void ) {
64 char original[] = "BADCFEHGJILKNMPORQTSVUXWZY@#";
65 char expected1[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ@#";
66 char expected2[] = "ABCDEFGHIJKLMNOPQRSTUVWX$";
67 char expected3[] = "$";
68
69 char from[30];
70 char to[30];
71
72 int testsize;
73
74 /* Test 1 - normal even case */
75 memset(to,'$', sizeof(to));
76 memset(from,'@', sizeof(from));
77 testsize = 26;
78 memcpy(from, original, testsize);
79 _swab( from, to, testsize );
80 ok(memcmp(to,expected1,testsize) == 0, "Testing even size %d returned '%*.*s'\n", testsize, testsize, testsize, to);
81
82 /* Test 2 - uneven case */
83 memset(to,'$', sizeof(to));
84 memset(from,'@', sizeof(from));
85 testsize = 25;
86 memcpy(from, original, testsize);
87 _swab( from, to, testsize );
88 ok(memcmp(to,expected2,testsize) == 0, "Testing odd size %d returned '%*.*s'\n", testsize, testsize, testsize, to);
89
90 /* Test 3 - from = to */
91 memset(to,'$', sizeof(to));
92 memset(from,'@', sizeof(from));
93 testsize = 26;
94 memcpy(to, original, testsize);
95 _swab( to, to, testsize );
96 ok(memcmp(to,expected1,testsize) == 0, "Testing overlapped size %d returned '%*.*s'\n", testsize, testsize, testsize, to);
97
98 /* Test 4 - 1 bytes */
99 memset(to,'$', sizeof(to));
100 memset(from,'@', sizeof(from));
101 testsize = 1;
102 memcpy(from, original, testsize);
103 _swab( from, to, testsize );
104 ok(memcmp(to,expected3,testsize) == 0, "Testing small size %d returned '%*.*s'\n", testsize, testsize, testsize, to);
105 }
106
107 #if 0 /* use this to generate more tests */
108
109 static void test_codepage(int cp)
110 {
111 int i;
112 int prev;
113 int count = 1;
114
115 ok(_setmbcp(cp) == 0, "Couldn't set mbcp\n");
116
117 prev = p_mbctype[0];
118 printf("static int result_cp_%d_mbctype[] = { ", cp);
119 for (i = 1; i < 257; i++)
120 {
121 if (p_mbctype[i] != prev)
122 {
123 printf("0x%x,%d, ", prev, count);
124 prev = p_mbctype[i];
125 count = 1;
126 }
127 else
128 count++;
129 }
130 printf("0x%x,%d };\n", prev, count);
131 }
132
133 #else
134
135 /* RLE-encoded mbctype tables for given codepages */
136 static int result_cp_932_mbctype[] = { 0x0,65, 0x8,1, 0x18,26, 0x8,6, 0x28,26, 0x8,4,
137 0x0,1, 0x8,1, 0xc,31, 0x8,1, 0xa,5, 0x9,58, 0xc,29, 0,3 };
138 static int result_cp_936_mbctype[] = { 0x0,65, 0x8,1, 0x18,26, 0x8,6, 0x28,26, 0x8,6,
139 0xc,126, 0,1 };
140 static int result_cp_949_mbctype[] = { 0x0,66, 0x18,26, 0x8,6, 0x28,26, 0x8,6, 0xc,126,
141 0,1 };
142 static int result_cp_950_mbctype[] = { 0x0,65, 0x8,1, 0x18,26, 0x8,6, 0x28,26, 0x8,4,
143 0x0,2, 0x4,32, 0xc,94, 0,1 };
144
145 static void test_cp_table(int cp, int *result)
146 {
147 int i;
148 int count = 0;
149 int curr = 0;
150 _setmbcp(cp);
151 for (i = 0; i < 256; i++)
152 {
153 if (count == 0)
154 {
155 curr = result[0];
156 count = result[1];
157 result += 2;
158 }
159 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);
160 count--;
161 }
162 }
163
164 #define test_codepage(num) test_cp_table(num, result_cp_##num##_mbctype);
165
166 #endif
167
168 static void test_mbcp(void)
169 {
170 int mb_orig_max = *p__mb_cur_max;
171 int curr_mbcp = _getmbcp();
172 unsigned char *mbstring = (unsigned char *)"\xb0\xb1\xb2 \xb3\xb4 \xb5"; /* incorrect string */
173 unsigned char *mbstring2 = (unsigned char *)"\xb0\xb1\xb2\xb3Q\xb4\xb5"; /* correct string */
174 unsigned char *mbsonlylead = (unsigned char *)"\xb0\0\xb1\xb2 \xb3";
175 unsigned char buf[16];
176 int step;
177
178 /* _mbtype tests */
179
180 /* An SBCS codepage test. The ctype of characters on e.g. CP1252 or CP1250 differs slightly
181 * between versions of Windows. Also Windows 9x seems to ignore the codepage and always uses
182 * CP1252 (or the ACP?) so we test only a few ASCII characters */
183 _setmbcp(1252);
184 expect_eq(p_mbctype[10], 0, char, "%x");
185 expect_eq(p_mbctype[50], 0, char, "%x");
186 expect_eq(p_mbctype[66], _SBUP, char, "%x");
187 expect_eq(p_mbctype[100], _SBLOW, char, "%x");
188 expect_eq(p_mbctype[128], 0, char, "%x");
189 _setmbcp(1250);
190 expect_eq(p_mbctype[10], 0, char, "%x");
191 expect_eq(p_mbctype[50], 0, char, "%x");
192 expect_eq(p_mbctype[66], _SBUP, char, "%x");
193 expect_eq(p_mbctype[100], _SBLOW, char, "%x");
194 expect_eq(p_mbctype[128], 0, char, "%x");
195
196 /* double byte code pages */
197 test_codepage(932);
198 test_codepage(936);
199 test_codepage(949);
200 test_codepage(950);
201
202 _setmbcp(936);
203 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);
204 ok(_ismbblead('\354'), "\354 should be a lead byte\n");
205 ok(_ismbblead(' ') == FALSE, "' ' should not be a lead byte\n");
206 ok(_ismbblead(0x1234b0), "0x1234b0 should not be a lead byte\n");
207 ok(_ismbblead(0x123420) == FALSE, "0x123420 should not be a lead byte\n");
208 ok(_ismbbtrail('\xb0'), "\xa0 should be a trail byte\n");
209 ok(_ismbbtrail(' ') == FALSE, "' ' should not be a trail byte\n");
210
211 /* _ismbslead */
212 expect_eq(_ismbslead(mbstring, &mbstring[0]), -1, int, "%d");
213 expect_eq(_ismbslead(mbstring, &mbstring[1]), FALSE, int, "%d");
214 expect_eq(_ismbslead(mbstring, &mbstring[2]), -1, int, "%d");
215 expect_eq(_ismbslead(mbstring, &mbstring[3]), FALSE, int, "%d");
216 expect_eq(_ismbslead(mbstring, &mbstring[4]), -1, int, "%d");
217 expect_eq(_ismbslead(mbstring, &mbstring[5]), FALSE, int, "%d");
218 expect_eq(_ismbslead(mbstring, &mbstring[6]), FALSE, int, "%d");
219 expect_eq(_ismbslead(mbstring, &mbstring[7]), -1, int, "%d");
220 expect_eq(_ismbslead(mbstring, &mbstring[8]), FALSE, int, "%d");
221
222 expect_eq(_ismbslead(mbsonlylead, &mbsonlylead[0]), -1, int, "%d");
223 expect_eq(_ismbslead(mbsonlylead, &mbsonlylead[1]), FALSE, int, "%d");
224 expect_eq(_ismbslead(mbsonlylead, &mbsonlylead[2]), FALSE, int, "%d");
225 expect_eq(_ismbslead(mbsonlylead, &mbsonlylead[5]), FALSE, int, "%d");
226
227 /* _ismbstrail */
228 expect_eq(_ismbstrail(mbstring, &mbstring[0]), FALSE, int, "%d");
229 expect_eq(_ismbstrail(mbstring, &mbstring[1]), -1, int, "%d");
230 expect_eq(_ismbstrail(mbstring, &mbstring[2]), FALSE, int, "%d");
231 expect_eq(_ismbstrail(mbstring, &mbstring[3]), -1, int, "%d");
232 expect_eq(_ismbstrail(mbstring, &mbstring[4]), FALSE, int, "%d");
233 expect_eq(_ismbstrail(mbstring, &mbstring[5]), -1, int, "%d");
234 expect_eq(_ismbstrail(mbstring, &mbstring[6]), FALSE, int, "%d");
235 expect_eq(_ismbstrail(mbstring, &mbstring[7]), FALSE, int, "%d");
236 expect_eq(_ismbstrail(mbstring, &mbstring[8]), -1, int, "%d");
237
238 expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[0]), FALSE, int, "%d");
239 expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[1]), -1, int, "%d");
240 expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[2]), FALSE, int, "%d");
241 expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[3]), FALSE, int, "%d");
242 expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[4]), FALSE, int, "%d");
243 expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[5]), FALSE, int, "%d");
244
245 /* _mbsbtype */
246 expect_eq(_mbsbtype(mbstring, 0), _MBC_LEAD, int, "%d");
247 expect_eq(_mbsbtype(mbstring, 1), _MBC_TRAIL, int, "%d");
248 expect_eq(_mbsbtype(mbstring, 2), _MBC_LEAD, int, "%d");
249 expect_eq(_mbsbtype(mbstring, 3), _MBC_ILLEGAL, int, "%d");
250 expect_eq(_mbsbtype(mbstring, 4), _MBC_LEAD, int, "%d");
251 expect_eq(_mbsbtype(mbstring, 5), _MBC_TRAIL, int, "%d");
252 expect_eq(_mbsbtype(mbstring, 6), _MBC_SINGLE, int, "%d");
253 expect_eq(_mbsbtype(mbstring, 7), _MBC_LEAD, int, "%d");
254 expect_eq(_mbsbtype(mbstring, 8), _MBC_ILLEGAL, int, "%d");
255
256 expect_eq(_mbsbtype(mbsonlylead, 0), _MBC_LEAD, int, "%d");
257 expect_eq(_mbsbtype(mbsonlylead, 1), _MBC_ILLEGAL, int, "%d");
258 expect_eq(_mbsbtype(mbsonlylead, 2), _MBC_ILLEGAL, int, "%d");
259 expect_eq(_mbsbtype(mbsonlylead, 3), _MBC_ILLEGAL, int, "%d");
260 expect_eq(_mbsbtype(mbsonlylead, 4), _MBC_ILLEGAL, int, "%d");
261 expect_eq(_mbsbtype(mbsonlylead, 5), _MBC_ILLEGAL, int, "%d");
262
263 /* _mbsnextc */
264 expect_eq(_mbsnextc(mbstring), 0xb0b1, int, "%x");
265 expect_eq(_mbsnextc(&mbstring[2]), 0xb220, int, "%x"); /* lead + invalid tail */
266 expect_eq(_mbsnextc(&mbstring[3]), 0x20, int, "%x"); /* single char */
267
268 /* _mbclen/_mbslen */
269 expect_eq(_mbclen(mbstring), 2, int, "%d");
270 expect_eq(_mbclen(&mbstring[2]), 2, int, "%d");
271 expect_eq(_mbclen(&mbstring[3]), 1, int, "%d");
272 expect_eq(_mbslen(mbstring2), 4, int, "%d");
273 expect_eq(_mbslen(mbsonlylead), 0, int, "%d"); /* lead + NUL not counted as character */
274 expect_eq(_mbslen(mbstring), 4, int, "%d"); /* lead + invalid trail counted */
275
276 /* _mbccpy/_mbsncpy */
277 memset(buf, 0xff, sizeof(buf));
278 _mbccpy(buf, mbstring);
279 expect_bin(buf, "\xb0\xb1\xff", 3);
280
281 memset(buf, 0xff, sizeof(buf));
282 _mbsncpy(buf, mbstring, 1);
283 expect_bin(buf, "\xb0\xb1\xff", 3);
284 memset(buf, 0xff, sizeof(buf));
285 _mbsncpy(buf, mbstring, 2);
286 expect_bin(buf, "\xb0\xb1\xb2 \xff", 5);
287 memset(buf, 0xff, sizeof(buf));
288 _mbsncpy(buf, mbstring, 3);
289 expect_bin(buf, "\xb0\xb1\xb2 \xb3\xb4\xff", 7);
290 memset(buf, 0xff, sizeof(buf));
291 _mbsncpy(buf, mbstring, 4);
292 expect_bin(buf, "\xb0\xb1\xb2 \xb3\xb4 \xff", 8);
293 memset(buf, 0xff, sizeof(buf));
294 _mbsncpy(buf, mbstring, 5);
295 expect_bin(buf, "\xb0\xb1\xb2 \xb3\xb4 \0\0\xff", 10);
296 memset(buf, 0xff, sizeof(buf));
297 _mbsncpy(buf, mbsonlylead, 6);
298 expect_bin(buf, "\0\0\0\0\0\0\0\xff", 8);
299
300 memset(buf, 0xff, sizeof(buf));
301 _mbsnbcpy(buf, mbstring2, 2);
302 expect_bin(buf, "\xb0\xb1\xff", 3);
303 _mbsnbcpy(buf, mbstring2, 3);
304 expect_bin(buf, "\xb0\xb1\0\xff", 4);
305 _mbsnbcpy(buf, mbstring2, 4);
306 expect_bin(buf, "\xb0\xb1\xb2\xb3\xff", 5);
307 memset(buf, 0xff, sizeof(buf));
308 _mbsnbcpy(buf, mbsonlylead, 5);
309 expect_bin(buf, "\0\0\0\0\0\xff", 6);
310
311 /* _mbsinc/mbsdec */
312 step = _mbsinc(mbstring) - mbstring;
313 ok(step == 2, "_mbsinc adds %d (exp. 2)\n", step);
314 step = _mbsinc(&mbstring[2]) - &mbstring[2]; /* lead + invalid tail */
315 ok(step == 2, "_mbsinc adds %d (exp. 2)\n", step);
316
317 step = _mbsninc(mbsonlylead, 1) - mbsonlylead;
318 ok(step == 0, "_mbsninc adds %d (exp. 0)\n", step);
319 step = _mbsninc(mbsonlylead, 2) - mbsonlylead; /* lead + NUL byte + lead + char */
320 ok(step == 0, "_mbsninc adds %d (exp. 0)\n", step);
321 step = _mbsninc(mbstring2, 0) - mbstring2;
322 ok(step == 0, "_mbsninc adds %d (exp. 2)\n", step);
323 step = _mbsninc(mbstring2, 1) - mbstring2;
324 ok(step == 2, "_mbsninc adds %d (exp. 2)\n", step);
325 step = _mbsninc(mbstring2, 2) - mbstring2;
326 ok(step == 4, "_mbsninc adds %d (exp. 4)\n", step);
327 step = _mbsninc(mbstring2, 3) - mbstring2;
328 ok(step == 5, "_mbsninc adds %d (exp. 5)\n", step);
329 step = _mbsninc(mbstring2, 4) - mbstring2;
330 ok(step == 7, "_mbsninc adds %d (exp. 7)\n", step);
331 step = _mbsninc(mbstring2, 5) - mbstring2;
332 ok(step == 7, "_mbsninc adds %d (exp. 7)\n", step);
333 step = _mbsninc(mbstring2, 17) - mbstring2;
334 ok(step == 7, "_mbsninc adds %d (exp. 7)\n", step);
335
336 /* functions that depend on locale codepage, not mbcp.
337 * we hope the current locale to be SBCS because setlocale(LC_ALL, ".1252") seems not to work yet
338 * (as of Wine 0.9.43)
339 */
340 if (*p__mb_cur_max == 1)
341 {
342 expect_eq(mblen((char *)mbstring, 3), 1, int, "%x");
343 expect_eq(_mbstrlen((char *)mbstring2), 7, int, "%d");
344 }
345 else
346 skip("Current locale has double-byte charset - could leave to false positives\n");
347
348 _setmbcp(1361);
349 expect_eq(_ismbblead(0x80), 0, int, "%d");
350 todo_wine {
351 expect_eq(_ismbblead(0x81), 1, int, "%d");
352 expect_eq(_ismbblead(0x83), 1, int, "%d");
353 }
354 expect_eq(_ismbblead(0x84), 1, int, "%d");
355 expect_eq(_ismbblead(0xd3), 1, int, "%d");
356 expect_eq(_ismbblead(0xd7), 0, int, "%d");
357 todo_wine {
358 expect_eq(_ismbblead(0xd8), 1, int, "%d");
359 }
360 expect_eq(_ismbblead(0xd9), 1, int, "%d");
361
362 expect_eq(_ismbbtrail(0x30), 0, int, "%d");
363 expect_eq(_ismbbtrail(0x31), 1, int, "%d");
364 expect_eq(_ismbbtrail(0x7e), 1, int, "%d");
365 expect_eq(_ismbbtrail(0x7f), 0, int, "%d");
366 expect_eq(_ismbbtrail(0x80), 0, int, "%d");
367 expect_eq(_ismbbtrail(0x81), 1, int, "%d");
368 expect_eq(_ismbbtrail(0xfe), 1, int, "%d");
369 expect_eq(_ismbbtrail(0xff), 0, int, "%d");
370
371 _setmbcp(curr_mbcp);
372 }
373
374 static void test_mbsspn( void)
375 {
376 unsigned char str1[]="cabernet";
377 unsigned char str2[]="shiraz";
378 unsigned char set[]="abc";
379 unsigned char empty[]="";
380 int ret;
381 ret=_mbsspn( str1, set);
382 ok( ret==3, "_mbsspn returns %d should be 3\n", ret);
383 ret=_mbsspn( str2, set);
384 ok( ret==0, "_mbsspn returns %d should be 0\n", ret);
385 ret=_mbsspn( str1, empty);
386 ok( ret==0, "_mbsspn returns %d should be 0\n", ret);
387 }
388
389 static void test_mbsspnp( void)
390 {
391 unsigned char str1[]="cabernet";
392 unsigned char str2[]="shiraz";
393 unsigned char set[]="abc";
394 unsigned char empty[]="";
395 unsigned char full[]="abcenrt";
396 unsigned char* ret;
397 ret=_mbsspnp( str1, set);
398 ok( ret[0]=='e', "_mbsspnp returns %c should be e\n", ret[0]);
399 ret=_mbsspnp( str2, set);
400 ok( ret[0]=='s', "_mbsspnp returns %c should be s\n", ret[0]);
401 ret=_mbsspnp( str1, empty);
402 ok( ret[0]=='c', "_mbsspnp returns %c should be c\n", ret[0]);
403 ret=_mbsspnp( str1, full);
404 ok( ret==NULL, "_mbsspnp returns %p should be NULL\n", ret);
405 }
406
407 static void test_strdup(void)
408 {
409 char *str;
410 str = _strdup( 0 );
411 ok( str == 0, "strdup returns %s should be 0\n", str);
412 free( str );
413 }
414
415 static void test_strcpy_s(void)
416 {
417 char dest[8];
418 const char *small = "small";
419 const char *big = "atoolongstringforthislittledestination";
420 int ret;
421
422 if(!pstrcpy_s)
423 {
424 skip("strcpy_s not found\n");
425 return;
426 }
427
428 memset(dest, 'X', sizeof(dest));
429 ret = pstrcpy_s(dest, sizeof(dest), small);
430 ok(ret == 0, "Copying a string into a big enough destination returned %d, expected 0\n", ret);
431 ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == 'l' &&
432 dest[4] == 'l' && dest[5] == '\0'&& dest[6] == 'X' && dest[7] == 'X',
433 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
434 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
435
436 memset(dest, 'X', sizeof(dest));
437 ret = pstrcpy_s(dest, 0, big);
438 ok(ret == EINVAL, "Copying into a destination of size 0 returned %d, expected EINVAL\n", ret);
439 ok(dest[0] == 'X' && dest[1] == 'X' && dest[2] == 'X' && dest[3] == 'X' &&
440 dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
441 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
442 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
443 ret = pstrcpy_s(dest, 0, NULL);
444 ok(ret == EINVAL, "Copying into a destination of size 0 returned %d, expected EINVAL\n", ret);
445 ok(dest[0] == 'X' && dest[1] == 'X' && dest[2] == 'X' && dest[3] == 'X' &&
446 dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
447 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
448 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
449
450 memset(dest, 'X', sizeof(dest));
451 ret = pstrcpy_s(dest, sizeof(dest), big);
452 ok(ret == ERANGE, "Copying a big string in a small location returned %d, expected ERANGE\n", ret);
453 ok(dest[0] == '\0'&& dest[1] == 't' && dest[2] == 'o' && dest[3] == 'o' &&
454 dest[4] == 'l' && dest[5] == 'o' && dest[6] == 'n' && dest[7] == 'g',
455 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
456 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
457
458 memset(dest, 'X', sizeof(dest));
459 ret = pstrcpy_s(dest, sizeof(dest), NULL);
460 ok(ret == EINVAL, "Copying from a NULL source string returned %d, expected EINVAL\n", ret);
461 ok(dest[0] == '\0'&& dest[1] == 'X' && dest[2] == 'X' && dest[3] == 'X' &&
462 dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
463 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
464 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
465
466 ret = pstrcpy_s(NULL, sizeof(dest), small);
467 ok(ret == EINVAL, "Copying a big string a NULL dest returned %d, expected EINVAL\n", ret);
468 }
469
470 static void test_strcat_s(void)
471 {
472 char dest[8];
473 const char *small = "sma";
474 int ret;
475
476 if(!pstrcat_s)
477 {
478 skip("strcat_s not found\n");
479 return;
480 }
481
482 memset(dest, 'X', sizeof(dest));
483 dest[0] = '\0';
484 ret = pstrcat_s(dest, sizeof(dest), small);
485 ok(ret == 0, "strcat_s: Copying a string into a big enough destination returned %d, expected 0\n", ret);
486 ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == '\0'&&
487 dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
488 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
489 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
490 ret = pstrcat_s(dest, sizeof(dest), small);
491 ok(ret == 0, "strcat_s: Attaching a string to a big enough destination returned %d, expected 0\n", ret);
492 ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == 's' &&
493 dest[4] == 'm' && dest[5] == 'a' && dest[6] == '\0'&& dest[7] == 'X',
494 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
495 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
496
497 ret = pstrcat_s(dest, sizeof(dest), small);
498 ok(ret == ERANGE, "strcat_s: Attaching a string to a filled up destination returned %d, expected ERANGE\n", ret);
499 ok(dest[0] == '\0'&& dest[1] == 'm' && dest[2] == 'a' && dest[3] == 's' &&
500 dest[4] == 'm' && dest[5] == 'a' && dest[6] == 's' && dest[7] == 'm',
501 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
502 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
503
504 memset(dest, 'X', sizeof(dest));
505 dest[0] = 'a';
506 dest[1] = '\0';
507
508 ret = pstrcat_s(dest, 0, small);
509 ok(ret == EINVAL, "strcat_s: Source len = 0 returned %d, expected EINVAL\n", ret);
510 ok(dest[0] == 'a' && dest[1] == '\0'&& dest[2] == 'X' && dest[3] == 'X' &&
511 dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
512 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
513 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
514
515 ret = pstrcat_s(dest, 0, NULL);
516 ok(ret == EINVAL, "strcat_s: len = 0 and src = NULL returned %d, expected EINVAL\n", ret);
517 ok(dest[0] == 'a' && dest[1] == '\0'&& dest[2] == 'X' && dest[3] == 'X' &&
518 dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
519 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
520 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
521
522 ret = pstrcat_s(dest, sizeof(dest), NULL);
523 ok(ret == EINVAL, "strcat_s: Sourcing from NULL returned %d, expected EINVAL\n", ret);
524 ok(dest[0] == '\0'&& dest[1] == '\0'&& dest[2] == 'X' && dest[3] == 'X' &&
525 dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
526 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
527 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
528
529 ret = pstrcat_s(NULL, sizeof(dest), small);
530 ok(ret == EINVAL, "strcat_s: Writing to a NULL string returned %d, expected EINVAL\n", ret);
531 }
532
533 static void test__mbsnbcpy_s(void)
534 {
535 unsigned char dest[8];
536 const unsigned char big[] = "atoolongstringforthislittledestination";
537 const unsigned char small[] = "small";
538 int ret;
539
540 if(!p_mbsnbcpy_s)
541 {
542 skip("_mbsnbcpy_s not found\n");
543 return;
544 }
545
546 memset(dest, 'X', sizeof(dest));
547 ret = p_mbsnbcpy_s(dest, sizeof(dest), small, sizeof(small));
548 ok(ret == 0, "_mbsnbcpy_s: Copying a string into a big enough destination returned %d, expected 0\n", ret);
549 ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == 'l' &&
550 dest[4] == 'l' && dest[5] == '\0'&& dest[6] == 'X' && dest[7] == 'X',
551 "Unexpected return data from _mbsnbcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
552 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
553
554 /* WTF? */
555 memset(dest, 'X', sizeof(dest));
556 ret = p_mbsnbcpy_s(dest, sizeof(dest) - 2, big, sizeof(small));
557 ok(ret == ERANGE, "_mbsnbcpy_s: Copying a too long string returned %d, expected ERANGE\n", ret);
558 ok(dest[0] == '\0'&& dest[1] == 't' && dest[2] == 'o' && dest[3] == 'o' &&
559 dest[4] == 'l' && dest[5] == 'o' && dest[6] == 'X' && dest[7] == 'X',
560 "Unexpected return data from _mbsnbcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
561 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
562
563 memset(dest, 'X', sizeof(dest));
564 ret = p_mbsnbcpy_s(dest, sizeof(dest) - 2, big, 4);
565 ok(ret == 0, "_mbsnbcpy_s: Copying a too long string with a count cap returned %d, expected 0\n", ret);
566 ok(dest[0] == 'a' && dest[1] == 't' && dest[2] == 'o' && dest[3] == 'o' &&
567 dest[4] == '\0'&& dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
568 "Unexpected return data from _mbsnbcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
569 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
570
571 memset(dest, 'X', sizeof(dest));
572 ret = p_mbsnbcpy_s(dest, sizeof(dest) - 2, small, sizeof(small) + 10);
573 ok(ret == 0, "_mbsnbcpy_s: Copying more data than the source string len returned %d, expected 0\n", ret);
574 ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == 'l' &&
575 dest[4] == 'l' && dest[5] == '\0'&& dest[6] == 'X' && dest[7] == 'X',
576 "Unexpected return data from _mbsnbcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
577 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
578 }
579
580 static void test_wcscpy_s(void)
581 {
582 static const WCHAR szLongText[] = { 'T','h','i','s','A','L','o','n','g','s','t','r','i','n','g',0 };
583 static WCHAR szDest[18];
584 static WCHAR szDestShort[8];
585 int ret;
586
587 if(!p_wcscpy_s)
588 {
589 skip("wcscpy_s not found\n");
590 return;
591 }
592
593 /* Test NULL Dest */
594 ret = p_wcscpy_s(NULL, 18, szLongText);
595 ok(ret == EINVAL, "p_wcscpy_s expect EINVAL got %d\n", ret);
596
597 /* Test NULL Source */
598 szDest[0] = 'A';
599 ret = p_wcscpy_s(szDest, 18, NULL);
600 ok(ret == EINVAL, "expected EINVAL got %d\n", ret);
601 ok(szDest[0] == 0, "szDest[0] not 0\n");
602
603 /* Test invalid size */
604 szDest[0] = 'A';
605 ret = p_wcscpy_s(szDest, 0, szLongText);
606 /* Later versions changed the return value for this case to EINVAL,
607 * and don't modify the result if the dest size is 0.
608 */
609 ok(ret == ERANGE || ret == EINVAL, "expected ERANGE/EINVAL got %d\n", ret);
610 ok(szDest[0] == 0 || ret == EINVAL, "szDest[0] not 0\n");
611
612 /* Copy same buffer size */
613 ret = p_wcscpy_s(szDest, 18, szLongText);
614 ok(ret == 0, "expected 0 got %d\n", ret);
615 ok(lstrcmpW(szDest, szLongText) == 0, "szDest != szLongText\n");
616
617 /* Copy smaller buffer size */
618 szDest[0] = 'A';
619 ret = p_wcscpy_s(szDestShort, 8, szLongText);
620 ok(ret == ERANGE || ret == EINVAL, "expected ERANGE/EINVAL got %d\n", ret);
621 ok(szDestShort[0] == 0, "szDestShort[0] not 0\n");
622 }
623
624 static void test__wcsupr_s(void)
625 {
626 static const WCHAR mixedString[] = {'M', 'i', 'X', 'e', 'D', 'l', 'o', 'w',
627 'e', 'r', 'U', 'P', 'P', 'E', 'R', 0};
628 static const WCHAR expectedString[] = {'M', 'I', 'X', 'E', 'D', 'L', 'O',
629 'W', 'E', 'R', 'U', 'P', 'P', 'E',
630 'R', 0};
631 WCHAR testBuffer[2*sizeof(mixedString)/sizeof(WCHAR)];
632 int ret;
633
634 if (!p_wcsupr_s)
635 {
636 win_skip("_wcsupr_s not found\n");
637 return;
638 }
639
640 /* Test NULL input string and invalid size. */
641 errno = EBADF;
642 ret = p_wcsupr_s(NULL, 0);
643 ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
644 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
645
646 /* Test NULL input string and valid size. */
647 errno = EBADF;
648 ret = p_wcsupr_s(NULL, sizeof(testBuffer)/sizeof(WCHAR));
649 ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
650 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
651
652 /* Test empty string with zero size. */
653 errno = EBADF;
654 testBuffer[0] = '\0';
655 ret = p_wcsupr_s(testBuffer, 0);
656 ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
657 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
658 ok(testBuffer[0] == '\0', "Expected the buffer to be unchanged\n");
659
660 /* Test empty string with size of one. */
661 testBuffer[0] = '\0';
662 ret = p_wcsupr_s(testBuffer, 1);
663 ok(ret == 0, "Expected _wcsupr_s to succeed, got %d\n", ret);
664 ok(testBuffer[0] == '\0', "Expected the buffer to be unchanged\n");
665
666 /* Test one-byte buffer with zero size. */
667 errno = EBADF;
668 testBuffer[0] = 'x';
669 ret = p_wcsupr_s(testBuffer, 0);
670 ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
671 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
672 ok(testBuffer[0] == '\0', "Expected the first buffer character to be null\n");
673
674 /* Test one-byte buffer with size of one. */
675 errno = EBADF;
676 testBuffer[0] = 'x';
677 ret = p_wcsupr_s(testBuffer, 1);
678 ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
679 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
680 ok(testBuffer[0] == '\0', "Expected the first buffer character to be null\n");
681
682 /* Test invalid size. */
683 wcscpy(testBuffer, mixedString);
684 errno = EBADF;
685 ret = p_wcsupr_s(testBuffer, 0);
686 ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
687 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
688 ok(testBuffer[0] == '\0', "Expected the first buffer character to be null\n");
689
690 /* Test normal string uppercasing. */
691 wcscpy(testBuffer, mixedString);
692 ret = p_wcsupr_s(testBuffer, sizeof(mixedString)/sizeof(WCHAR));
693 ok(ret == 0, "Expected _wcsupr_s to succeed, got %d\n", ret);
694 ok(!wcscmp(testBuffer, expectedString), "Expected the string to be fully upper-case\n");
695
696 /* Test uppercasing with a shorter buffer size count. */
697 wcscpy(testBuffer, mixedString);
698 errno = EBADF;
699 ret = p_wcsupr_s(testBuffer, sizeof(mixedString)/sizeof(WCHAR) - 1);
700 ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
701 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
702 ok(testBuffer[0] == '\0', "Expected the first buffer character to be null\n");
703
704 /* Test uppercasing with a longer buffer size count. */
705 wcscpy(testBuffer, mixedString);
706 ret = p_wcsupr_s(testBuffer, sizeof(testBuffer)/sizeof(WCHAR));
707 ok(ret == 0, "Expected _wcsupr_s to succeed, got %d\n", ret);
708 ok(!wcscmp(testBuffer, expectedString), "Expected the string to be fully upper-case\n");
709 }
710
711 static void test_mbcjisjms(void)
712 {
713 /* List of value-pairs to test. The test assumes the last pair to be {0, ..} */
714 unsigned int jisjms[][2] = { {0x2020, 0}, {0x2021, 0}, {0x2120, 0}, {0x2121, 0x8140},
715 {0x7f7f, 0}, {0x7f7e, 0}, {0x7e7f, 0}, {0x7e7e, 0xeffc},
716 {0x2121FFFF, 0}, {0x2223, 0x81a1}, {0x237e, 0x829e}, {0, 0}};
717 unsigned int ret, exp, i;
718
719 i = 0;
720 do
721 {
722 ret = _mbcjistojms(jisjms[i][0]);
723
724 if(_getmbcp() == 932) /* Japanese codepage? */
725 exp = jisjms[i][1];
726 else
727 exp = jisjms[i][0]; /* If not, no conversion */
728
729 ok(ret == exp, "Expected 0x%x, got 0x%x\n", exp, ret);
730 } while(jisjms[i++][0] != 0);
731 }
732
733 static void test_mbctombb(void)
734 {
735 static const unsigned int mbcmbb_932[][2] = {
736 {0x829e, 0x829e}, {0x829f, 0xa7}, {0x82f1, 0xdd}, {0x82f2, 0x82f2},
737 {0x833f, 0x833f}, {0x8340, 0xa7}, {0x837e, 0xd0}, {0x837f, 0x837f},
738 {0x8380, 0xd1}, {0x8396, 0xb9}, {0x8397, 0x8397}, {0x813f, 0x813f},
739 {0x8140, 0x20}, {0x814c, 0x814c}, {0x814f, 0x5e}, {0x8197, 0x40},
740 {0x8198, 0x8198}, {0x8258, 0x39}, {0x8259, 0x8259}, {0x825f, 0x825f},
741 {0x8260, 0x41}, {0x82f1, 0xdd}, {0x82f2, 0x82f2}, {0,0}};
742 unsigned int exp, ret, i;
743 unsigned int prev_cp = _getmbcp();
744
745 _setmbcp(932);
746 for (i = 0; mbcmbb_932[i][0] != 0; i++)
747 {
748 ret = _mbctombb(mbcmbb_932[i][0]);
749 exp = mbcmbb_932[i][1];
750 ok(ret == exp, "Expected 0x%x, got 0x%x\n", exp, ret);
751 }
752 _setmbcp(prev_cp);
753 }
754
755 static void test_ismbclegal(void) {
756 unsigned int prev_cp = _getmbcp();
757 int ret, exp, err;
758 unsigned int i;
759
760 _setmbcp(932); /* Japanese */
761 err = 0;
762 for(i = 0; i < 0x10000; i++) {
763 ret = _ismbclegal(i);
764 exp = ((HIBYTE(i) >= 0x81 && HIBYTE(i) <= 0x9F) ||
765 (HIBYTE(i) >= 0xE0 && HIBYTE(i) <= 0xFC)) &&
766 ((LOBYTE(i) >= 0x40 && LOBYTE(i) <= 0x7E) ||
767 (LOBYTE(i) >= 0x80 && LOBYTE(i) <= 0xFC));
768 if(ret != exp) {
769 err = 1;
770 break;
771 }
772 }
773 ok(!err, "_ismbclegal (932) : Expected 0x%x, got 0x%x (0x%x)\n", exp, ret, i);
774 _setmbcp(936); /* Chinese (GBK) */
775 err = 0;
776 for(i = 0; i < 0x10000; i++) {
777 ret = _ismbclegal(i);
778 exp = HIBYTE(i) >= 0x81 && HIBYTE(i) <= 0xFE &&
779 LOBYTE(i) >= 0x40 && LOBYTE(i) <= 0xFE;
780 if(ret != exp) {
781 err = 1;
782 break;
783 }
784 }
785 ok(!err, "_ismbclegal (936) : Expected 0x%x, got 0x%x (0x%x)\n", exp, ret, i);
786 _setmbcp(949); /* Korean */
787 err = 0;
788 for(i = 0; i < 0x10000; i++) {
789 ret = _ismbclegal(i);
790 exp = HIBYTE(i) >= 0x81 && HIBYTE(i) <= 0xFE &&
791 LOBYTE(i) >= 0x41 && LOBYTE(i) <= 0xFE;
792 if(ret != exp) {
793 err = 1;
794 break;
795 }
796 }
797 ok(!err, "_ismbclegal (949) : Expected 0x%x, got 0x%x (0x%x)\n", exp, ret, i);
798 _setmbcp(950); /* Chinese (Big5) */
799 err = 0;
800 for(i = 0; i < 0x10000; i++) {
801 ret = _ismbclegal(i);
802 exp = HIBYTE(i) >= 0x81 && HIBYTE(i) <= 0xFE &&
803 ((LOBYTE(i) >= 0x40 && LOBYTE(i) <= 0x7E) ||
804 (LOBYTE(i) >= 0xA1 && LOBYTE(i) <= 0xFE));
805 if(ret != exp) {
806 err = 1;
807 break;
808 }
809 }
810 ok(!err, "_ismbclegal (950) : Expected 0x%x, got 0x%x (0x%x)\n", exp, ret, i);
811 _setmbcp(1361); /* Korean (Johab) */
812 err = 0;
813 for(i = 0; i < 0x10000; i++) {
814 ret = _ismbclegal(i);
815 exp = ((HIBYTE(i) >= 0x81 && HIBYTE(i) <= 0xD3) ||
816 (HIBYTE(i) >= 0xD8 && HIBYTE(i) <= 0xF9)) &&
817 ((LOBYTE(i) >= 0x31 && LOBYTE(i) <= 0x7E) ||
818 (LOBYTE(i) >= 0x81 && LOBYTE(i) <= 0xFE)) &&
819 HIBYTE(i) != 0xDF;
820 if(ret != exp) {
821 err = 1;
822 break;
823 }
824 }
825 todo_wine ok(!err, "_ismbclegal (1361) : Expected 0x%x, got 0x%x (0x%x)\n", exp, ret, i);
826
827 _setmbcp(prev_cp);
828 }
829
830 static const struct {
831 const char* string;
832 const char* delimiter;
833 int exp_offsetret1; /* returned offset from string after first call to strtok()
834 -1 means NULL */
835 int exp_offsetret2; /* returned offset from string after second call to strtok()
836 -1 means NULL */
837 int exp_offsetret3; /* returned offset from string after third call to strtok()
838 -1 means NULL */
839 } testcases_strtok[] = {
840 { "red cabernet", " ", 0, 4, -1 },
841 { "sparkling white riesling", " ", 0, 10, 16 },
842 { " pale cream sherry", "e ", 1, 6, 9 },
843 /* end mark */
844 { 0}
845 };
846
847 static void test_strtok(void)
848 {
849 int i;
850 char *strret;
851 char teststr[100];
852 for( i = 0; testcases_strtok[i].string; i++){
853 strcpy( teststr, testcases_strtok[i].string);
854 strret = strtok( teststr, testcases_strtok[i].delimiter);
855 ok( (int)(strret - teststr) == testcases_strtok[i].exp_offsetret1 ||
856 (!strret && testcases_strtok[i].exp_offsetret1 == -1),
857 "string (%p) \'%s\' return %p\n",
858 teststr, testcases_strtok[i].string, strret);
859 if( !strret) continue;
860 strret = strtok( NULL, testcases_strtok[i].delimiter);
861 ok( (int)(strret - teststr) == testcases_strtok[i].exp_offsetret2 ||
862 (!strret && testcases_strtok[i].exp_offsetret2 == -1),
863 "second call string (%p) \'%s\' return %p\n",
864 teststr, testcases_strtok[i].string, strret);
865 if( !strret) continue;
866 strret = strtok( NULL, testcases_strtok[i].delimiter);
867 ok( (int)(strret - teststr) == testcases_strtok[i].exp_offsetret3 ||
868 (!strret && testcases_strtok[i].exp_offsetret3 == -1),
869 "third call string (%p) \'%s\' return %p\n",
870 teststr, testcases_strtok[i].string, strret);
871 }
872 }
873
874 static void test_strtol(void)
875 {
876 char* e;
877 LONG l;
878 ULONG ul;
879
880 /* errno is only set in case of error, so reset errno to EBADF to check for errno modification */
881 /* errno is modified on W2K8+ */
882 errno = EBADF;
883 l = strtol("-1234", &e, 0);
884 ok(l==-1234, "wrong value %d\n", l);
885 ok(errno == EBADF || broken(errno == 0), "wrong errno %d\n", errno);
886 errno = EBADF;
887 ul = strtoul("1234", &e, 0);
888 ok(ul==1234, "wrong value %u\n", ul);
889 ok(errno == EBADF || broken(errno == 0), "wrong errno %d\n", errno);
890
891 errno = EBADF;
892 l = strtol("2147483647L", &e, 0);
893 ok(l==2147483647, "wrong value %d\n", l);
894 ok(errno == EBADF || broken(errno == 0), "wrong errno %d\n", errno);
895 errno = EBADF;
896 l = strtol("-2147483648L", &e, 0);
897 ok(l==-2147483647L - 1, "wrong value %d\n", l);
898 ok(errno == EBADF || broken(errno == 0), "wrong errno %d\n", errno);
899 errno = EBADF;
900 ul = strtoul("4294967295UL", &e, 0);
901 ok(ul==4294967295ul, "wrong value %u\n", ul);
902 ok(errno == EBADF || broken(errno == 0), "wrong errno %d\n", errno);
903
904 errno = 0;
905 l = strtol("9223372036854775807L", &e, 0);
906 ok(l==2147483647, "wrong value %d\n", l);
907 ok(errno == ERANGE, "wrong errno %d\n", errno);
908 errno = 0;
909 ul = strtoul("9223372036854775807L", &e, 0);
910 ok(ul==4294967295ul, "wrong value %u\n", ul);
911 ok(errno == ERANGE, "wrong errno %d\n", errno);
912 }
913
914 START_TEST(string)
915 {
916 char mem[100];
917 static const char xilstring[]="c:/xilinx";
918 int nLen;
919
920 hMsvcrt = GetModuleHandleA("msvcrt.dll");
921 if (!hMsvcrt)
922 hMsvcrt = GetModuleHandleA("msvcrtd.dll");
923 ok(hMsvcrt != 0, "GetModuleHandleA failed\n");
924 SET(pmemcpy,"memcpy");
925 SET(pmemcmp,"memcmp");
926 SET(p_mbctype,"_mbctype");
927 SET(p__mb_cur_max,"__mb_cur_max");
928 pstrcpy_s = (void *)GetProcAddress( hMsvcrt,"strcpy_s" );
929 pstrcat_s = (void *)GetProcAddress( hMsvcrt,"strcat_s" );
930 p_mbsnbcpy_s = (void *)GetProcAddress( hMsvcrt,"_mbsnbcpy_s" );
931 p_wcscpy_s = (void *)GetProcAddress( hMsvcrt,"wcscpy_s" );
932 p_wcsupr_s = (void *)GetProcAddress( hMsvcrt,"_wcsupr_s" );
933
934 /* MSVCRT memcpy behaves like memmove for overlapping moves,
935 MFC42 CString::Insert seems to rely on that behaviour */
936 strcpy(mem,xilstring);
937 nLen=strlen(xilstring);
938 pmemcpy(mem+5, mem,nLen+1);
939 ok(pmemcmp(mem+5,xilstring, nLen) == 0,
940 "Got result %s\n",mem+5);
941
942 /* Test _swab function */
943 test_swab();
944
945 /* Test ismbblead*/
946 test_mbcp();
947 /* test _mbsspn */
948 test_mbsspn();
949 test_mbsspnp();
950 /* test _strdup */
951 test_strdup();
952 test_strcpy_s();
953 test_strcat_s();
954 test__mbsnbcpy_s();
955 test_mbcjisjms();
956 test_mbctombb();
957 test_ismbclegal();
958 test_strtok();
959 test_wcscpy_s();
960 test__wcsupr_s();
961 test_strtol();
962 }