-Import tkreuzer's time implementation from AMD64 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 "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 *p__mb_cur_max;
55 static unsigned char *p_mbctype;
56
57 #define SETNOFAIL(x,y) x = (void*)GetProcAddress(hMsvcrt,y)
58 #define SET(x,y) SETNOFAIL(x,y); ok(x != NULL, "Export '%s' not found\n", y)
59
60 HMODULE hMsvcrt;
61
62 static void test_swab( void ) {
63 char original[] = "BADCFEHGJILKNMPORQTSVUXWZY@#";
64 char expected1[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ@#";
65 char expected2[] = "ABCDEFGHIJKLMNOPQRSTUVWX$";
66 char expected3[] = "$";
67
68 char from[30];
69 char to[30];
70
71 int testsize;
72
73 /* Test 1 - normal even case */
74 memset(to,'$', sizeof(to));
75 memset(from,'@', sizeof(from));
76 testsize = 26;
77 memcpy(from, original, testsize);
78 _swab( from, to, testsize );
79 ok(memcmp(to,expected1,testsize) == 0, "Testing even size %d returned '%*.*s'\n", testsize, testsize, testsize, to);
80
81 /* Test 2 - uneven case */
82 memset(to,'$', sizeof(to));
83 memset(from,'@', sizeof(from));
84 testsize = 25;
85 memcpy(from, original, testsize);
86 _swab( from, to, testsize );
87 ok(memcmp(to,expected2,testsize) == 0, "Testing odd size %d returned '%*.*s'\n", testsize, testsize, testsize, to);
88
89 /* Test 3 - from = to */
90 memset(to,'$', sizeof(to));
91 memset(from,'@', sizeof(from));
92 testsize = 26;
93 memcpy(to, original, testsize);
94 _swab( to, to, testsize );
95 ok(memcmp(to,expected1,testsize) == 0, "Testing overlapped size %d returned '%*.*s'\n", testsize, testsize, testsize, to);
96
97 /* Test 4 - 1 bytes */
98 memset(to,'$', sizeof(to));
99 memset(from,'@', sizeof(from));
100 testsize = 1;
101 memcpy(from, original, testsize);
102 _swab( from, to, testsize );
103 ok(memcmp(to,expected3,testsize) == 0, "Testing small size %d returned '%*.*s'\n", testsize, testsize, testsize, to);
104 }
105
106 #if 0 /* use this to generate more tests */
107
108 static void test_codepage(int cp)
109 {
110 int i;
111 int prev;
112 int count = 1;
113
114 ok(_setmbcp(cp) == 0, "Couldn't set mbcp\n");
115
116 prev = p_mbctype[0];
117 printf("static int result_cp_%d_mbctype[] = { ", cp);
118 for (i = 1; i < 257; i++)
119 {
120 if (p_mbctype[i] != prev)
121 {
122 printf("0x%x,%d, ", prev, count);
123 prev = p_mbctype[i];
124 count = 1;
125 }
126 else
127 count++;
128 }
129 printf("0x%x,%d };\n", prev, count);
130 }
131
132 #else
133
134 /* RLE-encoded mbctype tables for given codepages */
135 static int result_cp_932_mbctype[] = { 0x0,65, 0x8,1, 0x18,26, 0x8,6, 0x28,26, 0x8,4,
136 0x0,1, 0x8,1, 0xc,31, 0x8,1, 0xa,5, 0x9,58, 0xc,29, 0,3 };
137 static int result_cp_936_mbctype[] = { 0x0,65, 0x8,1, 0x18,26, 0x8,6, 0x28,26, 0x8,6,
138 0xc,126, 0,1 };
139 static int result_cp_949_mbctype[] = { 0x0,66, 0x18,26, 0x8,6, 0x28,26, 0x8,6, 0xc,126,
140 0,1 };
141 static int result_cp_950_mbctype[] = { 0x0,65, 0x8,1, 0x18,26, 0x8,6, 0x28,26, 0x8,4,
142 0x0,2, 0x4,32, 0xc,94, 0,1 };
143
144 static void test_cp_table(int cp, int *result)
145 {
146 int i;
147 int count = 0;
148 int curr = 0;
149 _setmbcp(cp);
150 for (i = 0; i < 256; i++)
151 {
152 if (count == 0)
153 {
154 curr = result[0];
155 count = result[1];
156 result += 2;
157 }
158 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);
159 count--;
160 }
161 }
162
163 #define test_codepage(num) test_cp_table(num, result_cp_##num##_mbctype);
164
165 #endif
166
167 static void test_mbcp(void)
168 {
169 int mb_orig_max = *p__mb_cur_max;
170 int curr_mbcp = _getmbcp();
171 unsigned char *mbstring = (unsigned char *)"\xb0\xb1\xb2 \xb3\xb4 \xb5"; /* incorrect string */
172 unsigned char *mbstring2 = (unsigned char *)"\xb0\xb1\xb2\xb3Q\xb4\xb5"; /* correct string */
173 unsigned char *mbsonlylead = (unsigned char *)"\xb0\0\xb1\xb2 \xb3";
174 unsigned char buf[16];
175 int step;
176
177 /* _mbtype tests */
178
179 /* An SBCS codepage test. The ctype of characters on e.g. CP1252 or CP1250 differs slightly
180 * between versions of Windows. Also Windows 9x seems to ignore the codepage and always uses
181 * CP1252 (or the ACP?) so we test only a few ASCII characters */
182 _setmbcp(1252);
183 expect_eq(p_mbctype[10], 0, char, "%x");
184 expect_eq(p_mbctype[50], 0, char, "%x");
185 expect_eq(p_mbctype[66], _SBUP, char, "%x");
186 expect_eq(p_mbctype[100], _SBLOW, char, "%x");
187 expect_eq(p_mbctype[128], 0, char, "%x");
188 _setmbcp(1250);
189 expect_eq(p_mbctype[10], 0, char, "%x");
190 expect_eq(p_mbctype[50], 0, char, "%x");
191 expect_eq(p_mbctype[66], _SBUP, char, "%x");
192 expect_eq(p_mbctype[100], _SBLOW, char, "%x");
193 expect_eq(p_mbctype[128], 0, char, "%x");
194
195 /* double byte code pages */
196 test_codepage(932);
197 test_codepage(936);
198 test_codepage(949);
199 test_codepage(950);
200
201 _setmbcp(936);
202 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);
203 ok(_ismbblead('\354'), "\354 should be a lead byte\n");
204 ok(_ismbblead(' ') == FALSE, "' ' should not be a lead byte\n");
205 ok(_ismbblead(0x1234b0), "0x1234b0 should not be a lead byte\n");
206 ok(_ismbblead(0x123420) == FALSE, "0x123420 should not be a lead byte\n");
207 ok(_ismbbtrail('\xb0'), "\xa0 should be a trail byte\n");
208 ok(_ismbbtrail(' ') == FALSE, "' ' should not be a trail byte\n");
209
210 /* _ismbslead */
211 expect_eq(_ismbslead(mbstring, &mbstring[0]), -1, int, "%d");
212 expect_eq(_ismbslead(mbstring, &mbstring[1]), FALSE, int, "%d");
213 expect_eq(_ismbslead(mbstring, &mbstring[2]), -1, int, "%d");
214 expect_eq(_ismbslead(mbstring, &mbstring[3]), FALSE, int, "%d");
215 expect_eq(_ismbslead(mbstring, &mbstring[4]), -1, int, "%d");
216 expect_eq(_ismbslead(mbstring, &mbstring[5]), FALSE, int, "%d");
217 expect_eq(_ismbslead(mbstring, &mbstring[6]), FALSE, int, "%d");
218 expect_eq(_ismbslead(mbstring, &mbstring[7]), -1, int, "%d");
219 expect_eq(_ismbslead(mbstring, &mbstring[8]), FALSE, int, "%d");
220
221 expect_eq(_ismbslead(mbsonlylead, &mbsonlylead[0]), -1, int, "%d");
222 expect_eq(_ismbslead(mbsonlylead, &mbsonlylead[1]), FALSE, int, "%d");
223 expect_eq(_ismbslead(mbsonlylead, &mbsonlylead[2]), FALSE, int, "%d");
224 expect_eq(_ismbslead(mbsonlylead, &mbsonlylead[5]), FALSE, int, "%d");
225
226 /* _ismbstrail */
227 expect_eq(_ismbstrail(mbstring, &mbstring[0]), FALSE, int, "%d");
228 expect_eq(_ismbstrail(mbstring, &mbstring[1]), -1, int, "%d");
229 expect_eq(_ismbstrail(mbstring, &mbstring[2]), FALSE, int, "%d");
230 expect_eq(_ismbstrail(mbstring, &mbstring[3]), -1, int, "%d");
231 expect_eq(_ismbstrail(mbstring, &mbstring[4]), FALSE, int, "%d");
232 expect_eq(_ismbstrail(mbstring, &mbstring[5]), -1, int, "%d");
233 expect_eq(_ismbstrail(mbstring, &mbstring[6]), FALSE, int, "%d");
234 expect_eq(_ismbstrail(mbstring, &mbstring[7]), FALSE, int, "%d");
235 expect_eq(_ismbstrail(mbstring, &mbstring[8]), -1, int, "%d");
236
237 expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[0]), FALSE, int, "%d");
238 expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[1]), -1, int, "%d");
239 expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[2]), FALSE, int, "%d");
240 expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[3]), FALSE, int, "%d");
241 expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[4]), FALSE, int, "%d");
242 expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[5]), FALSE, int, "%d");
243
244 /* _mbsbtype */
245 expect_eq(_mbsbtype(mbstring, 0), _MBC_LEAD, int, "%d");
246 expect_eq(_mbsbtype(mbstring, 1), _MBC_TRAIL, int, "%d");
247 expect_eq(_mbsbtype(mbstring, 2), _MBC_LEAD, int, "%d");
248 expect_eq(_mbsbtype(mbstring, 3), _MBC_ILLEGAL, int, "%d");
249 expect_eq(_mbsbtype(mbstring, 4), _MBC_LEAD, int, "%d");
250 expect_eq(_mbsbtype(mbstring, 5), _MBC_TRAIL, int, "%d");
251 expect_eq(_mbsbtype(mbstring, 6), _MBC_SINGLE, int, "%d");
252 expect_eq(_mbsbtype(mbstring, 7), _MBC_LEAD, int, "%d");
253 expect_eq(_mbsbtype(mbstring, 8), _MBC_ILLEGAL, int, "%d");
254
255 expect_eq(_mbsbtype(mbsonlylead, 0), _MBC_LEAD, int, "%d");
256 expect_eq(_mbsbtype(mbsonlylead, 1), _MBC_ILLEGAL, int, "%d");
257 expect_eq(_mbsbtype(mbsonlylead, 2), _MBC_ILLEGAL, int, "%d");
258 expect_eq(_mbsbtype(mbsonlylead, 3), _MBC_ILLEGAL, int, "%d");
259 expect_eq(_mbsbtype(mbsonlylead, 4), _MBC_ILLEGAL, int, "%d");
260 expect_eq(_mbsbtype(mbsonlylead, 5), _MBC_ILLEGAL, int, "%d");
261
262 /* _mbsnextc */
263 expect_eq(_mbsnextc(mbstring), 0xb0b1, int, "%x");
264 expect_eq(_mbsnextc(&mbstring[2]), 0xb220, int, "%x"); /* lead + invalid tail */
265 expect_eq(_mbsnextc(&mbstring[3]), 0x20, int, "%x"); /* single char */
266
267 /* _mbclen/_mbslen */
268 expect_eq(_mbclen(mbstring), 2, int, "%d");
269 expect_eq(_mbclen(&mbstring[2]), 2, int, "%d");
270 expect_eq(_mbclen(&mbstring[3]), 1, int, "%d");
271 expect_eq(_mbslen(mbstring2), 4, int, "%d");
272 expect_eq(_mbslen(mbsonlylead), 0, int, "%d"); /* lead + NUL not counted as character */
273 expect_eq(_mbslen(mbstring), 4, int, "%d"); /* lead + invalid trail counted */
274
275 /* _mbccpy/_mbsncpy */
276 memset(buf, 0xff, sizeof(buf));
277 _mbccpy(buf, mbstring);
278 expect_bin(buf, "\xb0\xb1\xff", 3);
279
280 memset(buf, 0xff, sizeof(buf));
281 _mbsncpy(buf, mbstring, 1);
282 expect_bin(buf, "\xb0\xb1\xff", 3);
283 memset(buf, 0xff, sizeof(buf));
284 _mbsncpy(buf, mbstring, 2);
285 expect_bin(buf, "\xb0\xb1\xb2 \xff", 5);
286 memset(buf, 0xff, sizeof(buf));
287 _mbsncpy(buf, mbstring, 3);
288 expect_bin(buf, "\xb0\xb1\xb2 \xb3\xb4\xff", 7);
289 memset(buf, 0xff, sizeof(buf));
290 _mbsncpy(buf, mbstring, 4);
291 expect_bin(buf, "\xb0\xb1\xb2 \xb3\xb4 \xff", 8);
292 memset(buf, 0xff, sizeof(buf));
293 _mbsncpy(buf, mbstring, 5);
294 expect_bin(buf, "\xb0\xb1\xb2 \xb3\xb4 \0\0\xff", 10);
295 memset(buf, 0xff, sizeof(buf));
296 _mbsncpy(buf, mbsonlylead, 6);
297 expect_bin(buf, "\0\0\0\0\0\0\0\xff", 8);
298
299 memset(buf, 0xff, sizeof(buf));
300 _mbsnbcpy(buf, mbstring2, 2);
301 expect_bin(buf, "\xb0\xb1\xff", 3);
302 _mbsnbcpy(buf, mbstring2, 3);
303 expect_bin(buf, "\xb0\xb1\0\xff", 4);
304 _mbsnbcpy(buf, mbstring2, 4);
305 expect_bin(buf, "\xb0\xb1\xb2\xb3\xff", 5);
306 memset(buf, 0xff, sizeof(buf));
307 _mbsnbcpy(buf, mbsonlylead, 5);
308 expect_bin(buf, "\0\0\0\0\0\xff", 6);
309
310 /* _mbsinc/mbsdec */
311 step = _mbsinc(mbstring) - mbstring;
312 ok(step == 2, "_mbsinc adds %d (exp. 2)\n", step);
313 step = _mbsinc(&mbstring[2]) - &mbstring[2]; /* lead + invalid tail */
314 ok(step == 2, "_mbsinc adds %d (exp. 2)\n", step);
315
316 step = _mbsninc(mbsonlylead, 1) - mbsonlylead;
317 ok(step == 0, "_mbsninc adds %d (exp. 0)\n", step);
318 step = _mbsninc(mbsonlylead, 2) - mbsonlylead; /* lead + NUL byte + lead + char */
319 ok(step == 0, "_mbsninc adds %d (exp. 0)\n", step);
320 step = _mbsninc(mbstring2, 0) - mbstring2;
321 ok(step == 0, "_mbsninc adds %d (exp. 2)\n", step);
322 step = _mbsninc(mbstring2, 1) - mbstring2;
323 ok(step == 2, "_mbsninc adds %d (exp. 2)\n", step);
324 step = _mbsninc(mbstring2, 2) - mbstring2;
325 ok(step == 4, "_mbsninc adds %d (exp. 4)\n", step);
326 step = _mbsninc(mbstring2, 3) - mbstring2;
327 ok(step == 5, "_mbsninc adds %d (exp. 5)\n", step);
328 step = _mbsninc(mbstring2, 4) - mbstring2;
329 ok(step == 7, "_mbsninc adds %d (exp. 7)\n", step);
330 step = _mbsninc(mbstring2, 5) - mbstring2;
331 ok(step == 7, "_mbsninc adds %d (exp. 7)\n", step);
332 step = _mbsninc(mbstring2, 17) - mbstring2;
333 ok(step == 7, "_mbsninc adds %d (exp. 7)\n", step);
334
335 /* functions that depend on locale codepage, not mbcp.
336 * we hope the current locale to be SBCS because setlocale(LC_ALL, ".1252") seems not to work yet
337 * (as of Wine 0.9.43)
338 */
339 if (*p__mb_cur_max == 1)
340 {
341 expect_eq(mblen((char *)mbstring, 3), 1, int, "%x");
342 expect_eq(_mbstrlen((char *)mbstring2), 7, int, "%d");
343 }
344 else
345 skip("Current locale has double-byte charset - could leave to false positives\n");
346
347 _setmbcp(curr_mbcp);
348 }
349
350 static void test_mbsspn( void)
351 {
352 unsigned char str1[]="cabernet";
353 unsigned char str2[]="shiraz";
354 unsigned char set[]="abc";
355 unsigned char empty[]="";
356 int ret;
357 ret=_mbsspn( str1, set);
358 ok( ret==3, "_mbsspn returns %d should be 3\n", ret);
359 ret=_mbsspn( str2, set);
360 ok( ret==0, "_mbsspn returns %d should be 0\n", ret);
361 ret=_mbsspn( str1, empty);
362 ok( ret==0, "_mbsspn returns %d should be 0\n", ret);
363 }
364
365 static void test_mbsspnp( void)
366 {
367 unsigned char str1[]="cabernet";
368 unsigned char str2[]="shiraz";
369 unsigned char set[]="abc";
370 unsigned char empty[]="";
371 unsigned char full[]="abcenrt";
372 unsigned char* ret;
373 ret=_mbsspnp( str1, set);
374 ok( ret[0]=='e', "_mbsspnp returns %c should be e\n", ret[0]);
375 ret=_mbsspnp( str2, set);
376 ok( ret[0]=='s', "_mbsspnp returns %c should be s\n", ret[0]);
377 ret=_mbsspnp( str1, empty);
378 ok( ret[0]=='c', "_mbsspnp returns %c should be c\n", ret[0]);
379 ret=_mbsspnp( str1, full);
380 ok( ret==NULL, "_mbsspnp returns %p should be NULL\n", ret);
381 }
382
383 static void test_strdup(void)
384 {
385 char *str;
386 str = _strdup( 0 );
387 ok( str == 0, "strdup returns %s should be 0\n", str);
388 free( str );
389 }
390
391 static void test_strcpy_s(void)
392 {
393 char dest[8];
394 const char *small = "small";
395 const char *big = "atoolongstringforthislittledestination";
396 int ret;
397
398 if(!pstrcpy_s)
399 {
400 skip("strcpy_s not found\n");
401 return;
402 }
403
404 memset(dest, 'X', sizeof(dest));
405 ret = pstrcpy_s(dest, sizeof(dest), small);
406 ok(ret == 0, "Copying a string into a big enough destination returned %d, expected 0\n", ret);
407 ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == 'l' &&
408 dest[4] == 'l' && dest[5] == '\0'&& dest[6] == 'X' && dest[7] == 'X',
409 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
410 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
411
412 memset(dest, 'X', sizeof(dest));
413 ret = pstrcpy_s(dest, 0, big);
414 ok(ret == EINVAL, "Copying into a destination of size 0 returned %d, expected EINVAL\n", ret);
415 ok(dest[0] == 'X' && dest[1] == 'X' && dest[2] == 'X' && dest[3] == 'X' &&
416 dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
417 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
418 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
419 ret = pstrcpy_s(dest, 0, NULL);
420 ok(ret == EINVAL, "Copying into a destination of size 0 returned %d, expected EINVAL\n", ret);
421 ok(dest[0] == 'X' && dest[1] == 'X' && dest[2] == 'X' && dest[3] == 'X' &&
422 dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
423 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
424 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
425
426 memset(dest, 'X', sizeof(dest));
427 ret = pstrcpy_s(dest, sizeof(dest), big);
428 ok(ret == ERANGE, "Copying a big string in a small location returned %d, expected ERANGE\n", ret);
429 ok(dest[0] == '\0'&& dest[1] == 't' && dest[2] == 'o' && dest[3] == 'o' &&
430 dest[4] == 'l' && dest[5] == 'o' && dest[6] == 'n' && dest[7] == 'g',
431 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
432 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
433
434 memset(dest, 'X', sizeof(dest));
435 ret = pstrcpy_s(dest, sizeof(dest), NULL);
436 ok(ret == EINVAL, "Copying from a NULL source string returned %d, expected EINVAL\n", ret);
437 ok(dest[0] == '\0'&& dest[1] == 'X' && dest[2] == 'X' && dest[3] == 'X' &&
438 dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
439 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
440 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
441
442 ret = pstrcpy_s(NULL, sizeof(dest), small);
443 ok(ret == EINVAL, "Copying a big string a NULL dest returned %d, expected EINVAL\n", ret);
444 }
445
446 static void test_strcat_s(void)
447 {
448 char dest[8];
449 const char *small = "sma";
450 int ret;
451
452 if(!pstrcat_s)
453 {
454 skip("strcat_s not found\n");
455 return;
456 }
457
458 memset(dest, 'X', sizeof(dest));
459 dest[0] = '\0';
460 ret = pstrcat_s(dest, sizeof(dest), small);
461 ok(ret == 0, "strcat_s: Copying a string into a big enough destination returned %d, expected 0\n", ret);
462 ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == '\0'&&
463 dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
464 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
465 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
466 ret = pstrcat_s(dest, sizeof(dest), small);
467 ok(ret == 0, "strcat_s: Attaching a string to a big enough destination returned %d, expected 0\n", ret);
468 ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == 's' &&
469 dest[4] == 'm' && dest[5] == 'a' && dest[6] == '\0'&& dest[7] == 'X',
470 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
471 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
472
473 ret = pstrcat_s(dest, sizeof(dest), small);
474 ok(ret == ERANGE, "strcat_s: Attaching a string to a filled up destination returned %d, expected ERANGE\n", ret);
475 ok(dest[0] == '\0'&& dest[1] == 'm' && dest[2] == 'a' && dest[3] == 's' &&
476 dest[4] == 'm' && dest[5] == 'a' && dest[6] == 's' && dest[7] == 'm',
477 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
478 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
479
480 memset(dest, 'X', sizeof(dest));
481 dest[0] = 'a';
482 dest[1] = '\0';
483
484 ret = pstrcat_s(dest, 0, small);
485 ok(ret == EINVAL, "strcat_s: Source len = 0 returned %d, expected EINVAL\n", ret);
486 ok(dest[0] == 'a' && dest[1] == '\0'&& dest[2] == 'X' && dest[3] == 'X' &&
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
491 ret = pstrcat_s(dest, 0, NULL);
492 ok(ret == EINVAL, "strcat_s: len = 0 and src = NULL returned %d, expected EINVAL\n", ret);
493 ok(dest[0] == 'a' && dest[1] == '\0'&& dest[2] == 'X' && dest[3] == 'X' &&
494 dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
495 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
496 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
497
498 ret = pstrcat_s(dest, sizeof(dest), NULL);
499 ok(ret == EINVAL, "strcat_s: Sourcing from NULL returned %d, expected EINVAL\n", ret);
500 ok(dest[0] == '\0'&& dest[1] == '\0'&& dest[2] == 'X' && dest[3] == 'X' &&
501 dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
502 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
503 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
504
505 ret = pstrcat_s(NULL, sizeof(dest), small);
506 ok(ret == EINVAL, "strcat_s: Writing to a NULL string returned %d, expected EINVAL\n", ret);
507 }
508
509 static void test__mbsnbcpy_s(void)
510 {
511 unsigned char dest[8];
512 const unsigned char big[] = "atoolongstringforthislittledestination";
513 const unsigned char small[] = "small";
514 int ret;
515
516 if(!p_mbsnbcpy_s)
517 {
518 skip("_mbsnbcpy_s not found\n");
519 return;
520 }
521
522 memset(dest, 'X', sizeof(dest));
523 ret = p_mbsnbcpy_s(dest, sizeof(dest), small, sizeof(small));
524 ok(ret == 0, "_mbsnbcpy_s: Copying a string into a big enough destination returned %d, expected 0\n", ret);
525 ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == 'l' &&
526 dest[4] == 'l' && dest[5] == '\0'&& dest[6] == 'X' && dest[7] == 'X',
527 "Unexpected return data from _mbsnbcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
528 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
529
530 /* WTF? */
531 memset(dest, 'X', sizeof(dest));
532 ret = p_mbsnbcpy_s(dest, sizeof(dest) - 2, big, sizeof(small));
533 ok(ret == ERANGE, "_mbsnbcpy_s: Copying a too long string returned %d, expected ERANGE\n", ret);
534 ok(dest[0] == '\0'&& dest[1] == 't' && dest[2] == 'o' && dest[3] == 'o' &&
535 dest[4] == 'l' && dest[5] == 'o' && dest[6] == 'X' && dest[7] == 'X',
536 "Unexpected return data from _mbsnbcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
537 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
538
539 memset(dest, 'X', sizeof(dest));
540 ret = p_mbsnbcpy_s(dest, sizeof(dest) - 2, big, 4);
541 ok(ret == 0, "_mbsnbcpy_s: Copying a too long string with a count cap returned %d, expected 0\n", ret);
542 ok(dest[0] == 'a' && dest[1] == 't' && dest[2] == 'o' && dest[3] == 'o' &&
543 dest[4] == '\0'&& dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
544 "Unexpected return data from _mbsnbcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
545 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
546
547 memset(dest, 'X', sizeof(dest));
548 ret = p_mbsnbcpy_s(dest, sizeof(dest) - 2, small, sizeof(small) + 10);
549 ok(ret == 0, "_mbsnbcpy_s: Copying more data than the source string len returned %d, expected 0\n", ret);
550 ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == 'l' &&
551 dest[4] == 'l' && dest[5] == '\0'&& dest[6] == 'X' && dest[7] == 'X',
552 "Unexpected return data from _mbsnbcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
553 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
554 }
555
556 static void test_wcscpy_s(void)
557 {
558 static const WCHAR szLongText[] = { 'T','h','i','s','A','L','o','n','g','s','t','r','i','n','g',0 };
559 static WCHAR szDest[18];
560 static WCHAR szDestShort[8];
561 int ret;
562
563 if(!p_wcscpy_s)
564 {
565 skip("wcscpy_s not found\n");
566 return;
567 }
568
569 /* Test NULL Dest */
570 ret = p_wcscpy_s(NULL, 18, szLongText);
571 ok(ret == EINVAL, "p_wcscpy_s expect EINVAL got %d\n", ret);
572
573 /* Test NULL Source */
574 szDest[0] = 'A';
575 ret = p_wcscpy_s(szDest, 18, NULL);
576 ok(ret == EINVAL, "expected EINVAL got %d\n", ret);
577 ok(szDest[0] == 0, "szDest[0] not 0\n");
578
579 /* Test invalid size */
580 szDest[0] = 'A';
581 ret = p_wcscpy_s(szDest, 0, szLongText);
582 /* Later versions changed the return value for this case to EINVAL,
583 * and don't modify the result if the dest size is 0.
584 */
585 ok(ret == ERANGE || ret == EINVAL, "expected ERANGE/EINVAL got %d\n", ret);
586 ok(szDest[0] == 0 || ret == EINVAL, "szDest[0] not 0\n");
587
588 /* Copy same buffer size */
589 ret = p_wcscpy_s(szDest, 18, szLongText);
590 ok(ret == 0, "expected 0 got %d\n", ret);
591 ok(lstrcmpW(szDest, szLongText) == 0, "szDest != szLongText\n");
592
593 /* Copy smaller buffer size */
594 szDest[0] = 'A';
595 ret = p_wcscpy_s(szDestShort, 8, szLongText);
596 ok(ret == ERANGE || ret == EINVAL, "expected ERANGE/EINVAL got %d\n", ret);
597 ok(szDestShort[0] == 0, "szDestShort[0] not 0\n");
598 }
599
600 static void test_mbcjisjms(void)
601 {
602 /* List of value-pairs to test. The test assumes the last pair to be {0, ..} */
603 unsigned int jisjms[][2] = { {0x2020, 0}, {0x2021, 0}, {0x2120, 0}, {0x2121, 0x8140},
604 {0x7f7f, 0}, {0x7f7e, 0}, {0x7e7f, 0}, {0x7e7e, 0xeffc},
605 {0x2121FFFF, 0}, {0x2223, 0x81a1}, {0x237e, 0x829e}, {0, 0}};
606 unsigned int ret, exp, i;
607
608 i = 0;
609 do
610 {
611 ret = _mbcjistojms(jisjms[i][0]);
612
613 if(_getmbcp() == 932) /* Japanese codepage? */
614 exp = jisjms[i][1];
615 else
616 exp = jisjms[i][0]; /* If not, no conversion */
617
618 ok(ret == exp, "Expected 0x%x, got 0x%x\n", exp, ret);
619 } while(jisjms[i++][0] != 0);
620 }
621
622 static const struct {
623 const char* string;
624 const char* delimiter;
625 int exp_offsetret1; /* returned offset from string after first call to strtok()
626 -1 means NULL */
627 int exp_offsetret2; /* returned offset from string after second call to strtok()
628 -1 means NULL */
629 int exp_offsetret3; /* returned offset from string after third call to strtok()
630 -1 means NULL */
631 } testcases_strtok[] = {
632 { "red cabernet", " ", 0, 4, -1 },
633 { "sparkling white riesling", " ", 0, 10, 16 },
634 { " pale cream sherry", "e ", 1, 6, 9 },
635 /* end mark */
636 { 0}
637 };
638
639 static void test_strtok(void)
640 {
641 int i;
642 char *strret;
643 char teststr[100];
644 for( i = 0; testcases_strtok[i].string; i++){
645 strcpy( teststr, testcases_strtok[i].string);
646 strret = strtok( teststr, testcases_strtok[i].delimiter);
647 ok( (int)(strret - teststr) == testcases_strtok[i].exp_offsetret1 ||
648 (!strret && testcases_strtok[i].exp_offsetret1 == -1),
649 "string (%p) \'%s\' return %p\n",
650 teststr, testcases_strtok[i].string, strret);
651 if( !strret) continue;
652 strret = strtok( NULL, testcases_strtok[i].delimiter);
653 ok( (int)(strret - teststr) == testcases_strtok[i].exp_offsetret2 ||
654 (!strret && testcases_strtok[i].exp_offsetret2 == -1),
655 "second call string (%p) \'%s\' return %p\n",
656 teststr, testcases_strtok[i].string, strret);
657 if( !strret) continue;
658 strret = strtok( NULL, testcases_strtok[i].delimiter);
659 ok( (int)(strret - teststr) == testcases_strtok[i].exp_offsetret3 ||
660 (!strret && testcases_strtok[i].exp_offsetret3 == -1),
661 "third call string (%p) \'%s\' return %p\n",
662 teststr, testcases_strtok[i].string, strret);
663 }
664 }
665
666 START_TEST(string)
667 {
668 char mem[100];
669 static const char xilstring[]="c:/xilinx";
670 int nLen;
671
672 hMsvcrt = GetModuleHandleA("msvcrt.dll");
673 if (!hMsvcrt)
674 hMsvcrt = GetModuleHandleA("msvcrtd.dll");
675 ok(hMsvcrt != 0, "GetModuleHandleA failed\n");
676 SET(pmemcpy,"memcpy");
677 SET(pmemcmp,"memcmp");
678 SET(p_mbctype,"_mbctype");
679 SET(p__mb_cur_max,"__mb_cur_max");
680 pstrcpy_s = (void *)GetProcAddress( hMsvcrt,"strcpy_s" );
681 pstrcat_s = (void *)GetProcAddress( hMsvcrt,"strcat_s" );
682 p_mbsnbcpy_s = (void *)GetProcAddress( hMsvcrt,"_mbsnbcpy_s" );
683 p_wcscpy_s = (void *)GetProcAddress( hMsvcrt,"wcscpy_s" );
684
685 /* MSVCRT memcpy behaves like memmove for overlapping moves,
686 MFC42 CString::Insert seems to rely on that behaviour */
687 strcpy(mem,xilstring);
688 nLen=strlen(xilstring);
689 pmemcpy(mem+5, mem,nLen+1);
690 ok(pmemcmp(mem+5,xilstring, nLen) == 0,
691 "Got result %s\n",mem+5);
692
693 /* Test _swab function */
694 test_swab();
695
696 /* Test ismbblead*/
697 test_mbcp();
698 /* test _mbsspn */
699 test_mbsspn();
700 test_mbsspnp();
701 /* test _strdup */
702 test_strdup();
703 test_strcpy_s();
704 test_strcat_s();
705 test__mbsnbcpy_s();
706 test_mbcjisjms();
707 test_strtok();
708 test_wcscpy_s();
709 }