- Merge aicom-network-fixes up to r36740
[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
30 size_t _mbstrlen(const char*);
31
32 static char *buf_to_string(const unsigned char *bin, int len, int nr)
33 {
34 static char buf[2][1024];
35 char *w = buf[nr];
36 int i;
37
38 for (i = 0; i < len; i++)
39 {
40 sprintf(w, "%02x ", (unsigned char)bin[i]);
41 w += strlen(w);
42 }
43 return buf[nr];
44 }
45
46 #define expect_eq(expr, value, type, format) { type ret = (expr); ok((value) == ret, #expr " expected " format " got " format "\n", value, ret); }
47 #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)); }
48
49 static void* (*pmemcpy)(void *, const void *, size_t n);
50 static int* (*pmemcmp)(void *, const void *, size_t n);
51
52 #define SETNOFAIL(x,y) x = (void*)GetProcAddress(hMsvcrt,y)
53 #define SET(x,y) SETNOFAIL(x,y); ok(x != NULL, "Export '%s' not found\n", y)
54
55 HMODULE hMsvcrt;
56
57 static void test_swab( void ) {
58 char original[] = "BADCFEHGJILKNMPORQTSVUXWZY@#";
59 char expected1[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ@#";
60 char expected2[] = "ABCDEFGHIJKLMNOPQRSTUVWX$";
61 char expected3[] = "$";
62
63 char from[30];
64 char to[30];
65
66 int testsize;
67
68 /* Test 1 - normal even case */
69 memset(to,'$', sizeof(to));
70 memset(from,'@', sizeof(from));
71 testsize = 26;
72 memcpy(from, original, testsize);
73 _swab( from, to, testsize );
74 ok(memcmp(to,expected1,testsize) == 0, "Testing even size %d returned '%*.*s'\n", testsize, testsize, testsize, to);
75
76 /* Test 2 - uneven case */
77 memset(to,'$', sizeof(to));
78 memset(from,'@', sizeof(from));
79 testsize = 25;
80 memcpy(from, original, testsize);
81 _swab( from, to, testsize );
82 ok(memcmp(to,expected2,testsize) == 0, "Testing odd size %d returned '%*.*s'\n", testsize, testsize, testsize, to);
83
84 /* Test 3 - from = to */
85 memset(to,'$', sizeof(to));
86 memset(from,'@', sizeof(from));
87 testsize = 26;
88 memcpy(to, original, testsize);
89 _swab( to, to, testsize );
90 ok(memcmp(to,expected1,testsize) == 0, "Testing overlapped size %d returned '%*.*s'\n", testsize, testsize, testsize, to);
91
92 /* Test 4 - 1 bytes */
93 memset(to,'$', sizeof(to));
94 memset(from,'@', sizeof(from));
95 testsize = 1;
96 memcpy(from, original, testsize);
97 _swab( from, to, testsize );
98 ok(memcmp(to,expected3,testsize) == 0, "Testing small size %d returned '%*.*s'\n", testsize, testsize, testsize, to);
99 }
100
101 #if 0 /* use this to generate more tests */
102
103 static void test_codepage(int cp)
104 {
105 int i;
106 int prev;
107 int count = 1;
108
109 ok(_setmbcp(cp) == 0, "Couldn't set mbcp\n");
110
111 prev = _mbctype[0];
112 printf("static int result_cp_%d_mbctype[] = { ", cp);
113 for (i = 1; i < 257; i++)
114 {
115 if (_mbctype[i] != prev)
116 {
117 printf("0x%x,%d, ", prev, count);
118 prev = _mbctype[i];
119 count = 1;
120 }
121 else
122 count++;
123 }
124 printf("0x%x,%d };\n", prev, count);
125 }
126
127 #define test_codepage_todo(cp, todo) test_codepage(cp)
128
129 #else
130
131 /* RLE-encoded mbctype tables for given codepages */
132 static int result_cp_932_mbctype[] = { 0x0,65, 0x8,1, 0x18,26, 0x8,6, 0x28,26, 0x8,4,
133 0x0,1, 0x8,1, 0xc,31, 0x8,1, 0xa,5, 0x9,58, 0xc,29, 0,3 };
134 static int result_cp_936_mbctype[] = { 0x0,65, 0x8,1, 0x18,26, 0x8,6, 0x28,26, 0x8,6,
135 0xc,126, 0,1 };
136 static int result_cp_949_mbctype[] = { 0x0,66, 0x18,26, 0x8,6, 0x28,26, 0x8,6, 0xc,126,
137 0,1 };
138 static int result_cp_950_mbctype[] = { 0x0,65, 0x8,1, 0x18,26, 0x8,6, 0x28,26, 0x8,4,
139 0x0,2, 0x4,32, 0xc,94, 0,1 };
140
141 static int todo_none[] = { -2 };
142 static int todo_cp_932[] = { 254, -2 };
143 #if 0
144 void test_cp_table(int cp, int *result, int *todo)
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 if (i == *todo + 1)
159 {
160 todo_wine ok(_mbctype[i] == curr, "CP%d: Mismatch in ctype for character %d - %d instead of %d\n", cp, i-1, _mbctype[i], curr);
161 todo++;
162 }
163 else
164 ok(_mbctype[i] == curr, "CP%d: Mismatch in ctype for character %d - %d instead of %d\n", cp, i-1, _mbctype[i], curr);
165 count--;
166 }
167 }
168
169 #define test_codepage(num) test_cp_table(num, result_cp_##num##_mbctype, todo_none);
170 #define test_codepage_todo(num, todo) test_cp_table(num, result_cp_##num##_mbctype, todo);
171 #endif
172 #endif
173
174 static void test_mbcp(void)
175 {
176 int mb_orig_max = __mb_cur_max;
177 int curr_mbcp = _getmbcp();
178 unsigned char *mbstring = (unsigned char *)"\xb0\xb1\xb2 \xb3\xb4 \xb5"; /* incorrect string */
179 unsigned char *mbstring2 = (unsigned char *)"\xb0\xb1\xb2\xb3Q\xb4\xb5"; /* correct string */
180 unsigned char *mbsonlylead = (unsigned char *)"\xb0\0\xb1\xb2 \xb3";
181 unsigned char buf[16];
182 int step;
183
184 /* _mbtype tests */
185
186 /* An SBCS codepage test. The ctype of characters on e.g. CP1252 or CP1250 differs slightly
187 * between versions of Windows. Also Windows 9x seems to ignore the codepage and always uses
188 * CP1252 (or the ACP?) so we test only a few ASCII characters */
189 _setmbcp(1252);
190 expect_eq(_mbctype[10], 0, char, "%x");
191 expect_eq(_mbctype[50], 0, char, "%x");
192 expect_eq(_mbctype[66], _SBUP, char, "%x");
193 expect_eq(_mbctype[100], _SBLOW, char, "%x");
194 expect_eq(_mbctype[128], 0, char, "%x");
195 _setmbcp(1250);
196 expect_eq(_mbctype[10], 0, char, "%x");
197 expect_eq(_mbctype[50], 0, char, "%x");
198 expect_eq(_mbctype[66], _SBUP, char, "%x");
199 expect_eq(_mbctype[100], _SBLOW, char, "%x");
200 expect_eq(_mbctype[128], 0, char, "%x");
201
202 /* double byte code pages */
203 //test_codepage_todo(932, todo_cp_932);
204 //test_codepage(936);
205 //test_codepage(949);
206 //test_codepage(950);
207
208 _setmbcp(936);
209 ok(__mb_cur_max == mb_orig_max, "__mb_cur_max shouldn't be updated (is %d != %d)\n", __mb_cur_max, mb_orig_max);
210 ok(_ismbblead('\354'), "\354 should be a lead byte\n");
211 ok(_ismbblead(' ') == FALSE, "' ' should not be a lead byte\n");
212 ok(_ismbblead(0x1234b0), "0x1234b0 should not be a lead byte\n");
213 ok(_ismbblead(0x123420) == FALSE, "0x123420 should not be a lead byte\n");
214 ok(_ismbbtrail('\xb0'), "\xa0 should be a trail byte\n");
215 ok(_ismbbtrail(' ') == FALSE, "' ' should not be a trail byte\n");
216
217 /* _ismbslead */
218 expect_eq(_ismbslead(mbstring, &mbstring[0]), -1, int, "%d");
219 expect_eq(_ismbslead(mbstring, &mbstring[1]), FALSE, int, "%d");
220 expect_eq(_ismbslead(mbstring, &mbstring[2]), -1, int, "%d");
221 expect_eq(_ismbslead(mbstring, &mbstring[3]), FALSE, int, "%d");
222 expect_eq(_ismbslead(mbstring, &mbstring[4]), -1, int, "%d");
223 expect_eq(_ismbslead(mbstring, &mbstring[5]), FALSE, int, "%d");
224 expect_eq(_ismbslead(mbstring, &mbstring[6]), FALSE, int, "%d");
225 expect_eq(_ismbslead(mbstring, &mbstring[7]), -1, int, "%d");
226 expect_eq(_ismbslead(mbstring, &mbstring[8]), FALSE, int, "%d");
227
228 expect_eq(_ismbslead(mbsonlylead, &mbsonlylead[0]), -1, int, "%d");
229 expect_eq(_ismbslead(mbsonlylead, &mbsonlylead[1]), FALSE, int, "%d");
230 expect_eq(_ismbslead(mbsonlylead, &mbsonlylead[2]), FALSE, int, "%d");
231 expect_eq(_ismbslead(mbsonlylead, &mbsonlylead[5]), FALSE, int, "%d");
232
233 /* _ismbstrail */
234 expect_eq(_ismbstrail(mbstring, &mbstring[0]), FALSE, int, "%d");
235 expect_eq(_ismbstrail(mbstring, &mbstring[1]), -1, int, "%d");
236 expect_eq(_ismbstrail(mbstring, &mbstring[2]), FALSE, int, "%d");
237 expect_eq(_ismbstrail(mbstring, &mbstring[3]), -1, int, "%d");
238 expect_eq(_ismbstrail(mbstring, &mbstring[4]), FALSE, int, "%d");
239 expect_eq(_ismbstrail(mbstring, &mbstring[5]), -1, int, "%d");
240 expect_eq(_ismbstrail(mbstring, &mbstring[6]), FALSE, int, "%d");
241 expect_eq(_ismbstrail(mbstring, &mbstring[7]), FALSE, int, "%d");
242 expect_eq(_ismbstrail(mbstring, &mbstring[8]), -1, int, "%d");
243
244 expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[0]), FALSE, int, "%d");
245 expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[1]), -1, int, "%d");
246 expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[2]), FALSE, int, "%d");
247 expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[3]), FALSE, int, "%d");
248 expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[4]), FALSE, int, "%d");
249 expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[5]), FALSE, int, "%d");
250
251 /* _mbsbtype */
252 expect_eq(_mbsbtype(mbstring, 0), _MBC_LEAD, int, "%d");
253 expect_eq(_mbsbtype(mbstring, 1), _MBC_TRAIL, int, "%d");
254 expect_eq(_mbsbtype(mbstring, 2), _MBC_LEAD, int, "%d");
255 expect_eq(_mbsbtype(mbstring, 3), _MBC_ILLEGAL, int, "%d");
256 expect_eq(_mbsbtype(mbstring, 4), _MBC_LEAD, int, "%d");
257 expect_eq(_mbsbtype(mbstring, 5), _MBC_TRAIL, int, "%d");
258 expect_eq(_mbsbtype(mbstring, 6), _MBC_SINGLE, int, "%d");
259 expect_eq(_mbsbtype(mbstring, 7), _MBC_LEAD, int, "%d");
260 expect_eq(_mbsbtype(mbstring, 8), _MBC_ILLEGAL, int, "%d");
261
262 expect_eq(_mbsbtype(mbsonlylead, 0), _MBC_LEAD, int, "%d");
263 expect_eq(_mbsbtype(mbsonlylead, 1), _MBC_ILLEGAL, int, "%d");
264 expect_eq(_mbsbtype(mbsonlylead, 2), _MBC_ILLEGAL, int, "%d");
265 expect_eq(_mbsbtype(mbsonlylead, 3), _MBC_ILLEGAL, int, "%d");
266 expect_eq(_mbsbtype(mbsonlylead, 4), _MBC_ILLEGAL, int, "%d");
267 expect_eq(_mbsbtype(mbsonlylead, 5), _MBC_ILLEGAL, int, "%d");
268
269 /* _mbsnextc */
270 expect_eq(_mbsnextc(mbstring), 0xb0b1, int, "%x");
271 expect_eq(_mbsnextc(&mbstring[2]), 0xb220, int, "%x"); /* lead + invalid tail */
272 expect_eq(_mbsnextc(&mbstring[3]), 0x20, int, "%x"); /* single char */
273
274 /* _mbclen/_mbslen */
275 expect_eq(_mbclen(mbstring), 2, int, "%d");
276 expect_eq(_mbclen(&mbstring[2]), 2, int, "%d");
277 expect_eq(_mbclen(&mbstring[3]), 1, int, "%d");
278 expect_eq(_mbslen(mbstring2), 4, int, "%d");
279 expect_eq(_mbslen(mbsonlylead), 0, int, "%d"); /* lead + NUL not counted as character */
280 expect_eq(_mbslen(mbstring), 4, int, "%d"); /* lead + invalid trail counted */
281
282 /* _mbccpy/_mbsncpy */
283 memset(buf, 0xff, sizeof(buf));
284 _mbccpy(buf, mbstring);
285 expect_bin(buf, "\xb0\xb1\xff", 3);
286
287 memset(buf, 0xff, sizeof(buf));
288 _mbsncpy(buf, mbstring, 1);
289 expect_bin(buf, "\xb0\xb1\xff", 3);
290 memset(buf, 0xff, sizeof(buf));
291 _mbsncpy(buf, mbstring, 2);
292 expect_bin(buf, "\xb0\xb1\xb2 \xff", 5);
293 memset(buf, 0xff, sizeof(buf));
294 _mbsncpy(buf, mbstring, 3);
295 expect_bin(buf, "\xb0\xb1\xb2 \xb3\xb4\xff", 7);
296 memset(buf, 0xff, sizeof(buf));
297 _mbsncpy(buf, mbstring, 4);
298 expect_bin(buf, "\xb0\xb1\xb2 \xb3\xb4 \xff", 8);
299 memset(buf, 0xff, sizeof(buf));
300 _mbsncpy(buf, mbstring, 5);
301 expect_bin(buf, "\xb0\xb1\xb2 \xb3\xb4 \0\0\xff", 10);
302 memset(buf, 0xff, sizeof(buf));
303 _mbsncpy(buf, mbsonlylead, 6);
304 expect_bin(buf, "\0\0\0\0\0\0\0\xff", 8);
305
306 memset(buf, 0xff, sizeof(buf));
307 _mbsnbcpy(buf, mbstring2, 2);
308 expect_bin(buf, "\xb0\xb1\xff", 3);
309 _mbsnbcpy(buf, mbstring2, 3);
310 expect_bin(buf, "\xb0\xb1\0\xff", 4);
311 _mbsnbcpy(buf, mbstring2, 4);
312 expect_bin(buf, "\xb0\xb1\xb2\xb3\xff", 5);
313 memset(buf, 0xff, sizeof(buf));
314 _mbsnbcpy(buf, mbsonlylead, 5);
315 expect_bin(buf, "\0\0\0\0\0\xff", 6);
316
317 /* _mbsinc/mbsdec */
318 step = _mbsinc(mbstring) - mbstring;
319 ok(step == 2, "_mbsinc adds %d (exp. 2)\n", step);
320 step = _mbsinc(&mbstring[2]) - &mbstring[2]; /* lead + invalid tail */
321 ok(step == 2, "_mbsinc adds %d (exp. 2)\n", step);
322
323 step = _mbsninc(mbsonlylead, 1) - mbsonlylead;
324 ok(step == 0, "_mbsninc adds %d (exp. 0)\n", step);
325 step = _mbsninc(mbsonlylead, 2) - mbsonlylead; /* lead + NUL byte + lead + char */
326 ok(step == 0, "_mbsninc adds %d (exp. 0)\n", step);
327 step = _mbsninc(mbstring2, 0) - mbstring2;
328 ok(step == 0, "_mbsninc adds %d (exp. 2)\n", step);
329 step = _mbsninc(mbstring2, 1) - mbstring2;
330 ok(step == 2, "_mbsninc adds %d (exp. 2)\n", step);
331 step = _mbsninc(mbstring2, 2) - mbstring2;
332 ok(step == 4, "_mbsninc adds %d (exp. 4)\n", step);
333 step = _mbsninc(mbstring2, 3) - mbstring2;
334 ok(step == 5, "_mbsninc adds %d (exp. 5)\n", step);
335 step = _mbsninc(mbstring2, 4) - mbstring2;
336 ok(step == 7, "_mbsninc adds %d (exp. 7)\n", step);
337 step = _mbsninc(mbstring2, 5) - mbstring2;
338 ok(step == 7, "_mbsninc adds %d (exp. 7)\n", step);
339 step = _mbsninc(mbstring2, 17) - mbstring2;
340 ok(step == 7, "_mbsninc adds %d (exp. 7)\n", step);
341
342 /* functions that depend on locale codepage, not mbcp.
343 * we hope the current locale to be SBCS because setlocale(LC_ALL, ".1252") seems not to work yet
344 * (as of Wine 0.9.43)
345 */
346 if (__mb_cur_max == 1)
347 {
348 expect_eq(mblen((char *)mbstring, 3), 1, int, "%x");
349 expect_eq(_mbstrlen((char *)mbstring2), 7, int, "%d");
350 }
351 else
352 skip("Current locale has double-byte charset - could leave to false positives\n");
353
354 _setmbcp(curr_mbcp);
355 }
356
357 static void test_mbsspn( void)
358 {
359 unsigned char str1[]="cabernet";
360 unsigned char str2[]="shiraz";
361 unsigned char set[]="abc";
362 unsigned char empty[]="";
363 int ret;
364 ret=_mbsspn( str1, set);
365 ok( ret==3, "_mbsspn returns %d should be 3\n", ret);
366 ret=_mbsspn( str2, set);
367 ok( ret==0, "_mbsspn returns %d should be 0\n", ret);
368 ret=_mbsspn( str1, empty);
369 ok( ret==0, "_mbsspn returns %d should be 0\n", ret);
370 }
371
372 static void test_mbsspnp( void)
373 {
374 unsigned char str1[]="cabernet";
375 unsigned char str2[]="shiraz";
376 unsigned char set[]="abc";
377 unsigned char empty[]="";
378 unsigned char full[]="abcenrt";
379 unsigned char* ret;
380 ret=_mbsspnp( str1, set);
381 ok( ret[0]=='e', "_mbsspnp returns %c should be e\n", ret[0]);
382 ret=_mbsspnp( str2, set);
383 ok( ret[0]=='s', "_mbsspnp returns %c should be s\n", ret[0]);
384 ret=_mbsspnp( str1, empty);
385 ok( ret[0]=='c', "_mbsspnp returns %c should be c\n", ret[0]);
386 ret=_mbsspnp( str1, full);
387 ok( ret==NULL, "_mbsspnp returns %p should be NULL\n", ret);
388 }
389
390 static void test_strdup(void)
391 {
392 char *str;
393 str = _strdup( 0 );
394 ok( str == 0, "strdup returns %s should be 0\n", str);
395 free( str );
396 }
397
398 START_TEST(string)
399 {
400 char mem[100];
401 static const char xilstring[]="c:/xilinx";
402 int nLen;
403
404 hMsvcrt = GetModuleHandleA("msvcrt.dll");
405 if (!hMsvcrt)
406 hMsvcrt = GetModuleHandleA("msvcrtd.dll");
407 ok(hMsvcrt != 0, "GetModuleHandleA failed\n");
408 SET(pmemcpy,"memcpy");
409 SET(pmemcmp,"memcmp");
410
411 /* MSVCRT memcpy behaves like memmove for overlapping moves,
412 MFC42 CString::Insert seems to rely on that behaviour */
413 strcpy(mem,xilstring);
414 nLen=strlen(xilstring);
415 pmemcpy(mem+5, mem,nLen+1);
416 ok(pmemcmp(mem+5,xilstring, nLen) == 0,
417 "Got result %s\n",mem+5);
418
419 /* Test _swab function */
420 test_swab();
421
422 /* Test ismbblead*/
423 test_mbcp();
424 /* test _mbsspn */
425 test_mbsspn();
426 test_mbsspnp();
427 /* test _strdup */
428 test_strdup();
429 }