fa50626f9a183841224ab07cd26352034c740ae6
[reactos.git] / rostests / winetests / shlwapi / string.c
1 /* Unit test suite for SHLWAPI string functions
2 *
3 * Copyright 2003 Jon Griffiths
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18 */
19
20 #include <stdio.h>
21
22 #include "wine/test.h"
23 #include "winbase.h"
24 #include "winerror.h"
25 #include "winnls.h"
26 #define NO_SHLWAPI_REG
27 #define NO_SHLWAPI_PATH
28 #define NO_SHLWAPI_GDI
29 #define NO_SHLWAPI_STREAM
30 #include "shlwapi.h"
31 #include "shtypes.h"
32
33 #define expect_eq(expr, val, type, fmt) do { \
34 type ret = expr; \
35 ok(ret == val, "Unexpected value of '" #expr "': " #fmt " instead of " #val "\n", ret); \
36 } while (0);
37
38 #define expect_eq2(expr, val1, val2, type, fmt) do { \
39 type ret = expr; \
40 ok(ret == val1 || ret == val2, "Unexpected value of '" #expr "': " #fmt " instead of " #val1 " or " #val2 "\n", ret); \
41 } while (0);
42
43 static BOOL (WINAPI *pIntlStrEqWorkerA)(BOOL,LPCSTR,LPCSTR,int);
44 static BOOL (WINAPI *pIntlStrEqWorkerW)(BOOL,LPCWSTR,LPCWSTR,int);
45 static DWORD (WINAPI *pSHAnsiToAnsi)(LPCSTR,LPSTR,int);
46 static DWORD (WINAPI *pSHUnicodeToUnicode)(LPCWSTR,LPWSTR,int);
47 static LPSTR (WINAPI *pStrCatBuffA)(LPSTR,LPCSTR,INT);
48 static LPWSTR (WINAPI *pStrCatBuffW)(LPWSTR,LPCWSTR,INT);
49 static LPSTR (WINAPI *pStrCpyNXA)(LPSTR,LPCSTR,int);
50 static LPWSTR (WINAPI *pStrCpyNXW)(LPWSTR,LPCWSTR,int);
51 static LPSTR (WINAPI *pStrFormatByteSize64A)(LONGLONG,LPSTR,UINT);
52 static LPSTR (WINAPI *pStrFormatKBSizeA)(LONGLONG,LPSTR,UINT);
53 static LPWSTR (WINAPI *pStrFormatKBSizeW)(LONGLONG,LPWSTR,UINT);
54 static BOOL (WINAPI *pStrIsIntlEqualA)(BOOL,LPCSTR,LPCSTR,int);
55 static BOOL (WINAPI *pStrIsIntlEqualW)(BOOL,LPCWSTR,LPCWSTR,int);
56 static HRESULT (WINAPI *pStrRetToBSTR)(STRRET*,void*,BSTR*);
57 static HRESULT (WINAPI *pStrRetToBufA)(STRRET*,LPCITEMIDLIST,LPSTR,UINT);
58 static HRESULT (WINAPI *pStrRetToBufW)(STRRET*,LPCITEMIDLIST,LPWSTR,UINT);
59 static INT (WINAPIV *pwnsprintfA)(LPSTR,INT,LPCSTR, ...);
60 static INT (WINAPIV *pwnsprintfW)(LPWSTR,INT,LPCWSTR, ...);
61
62 static int strcmpW(const WCHAR *str1, const WCHAR *str2)
63 {
64 while (*str1 && (*str1 == *str2)) { str1++; str2++; }
65 return *str1 - *str2;
66 }
67
68 /* StrToInt/StrToIntEx results */
69 typedef struct tagStrToIntResult
70 {
71 const char* string;
72 int str_to_int;
73 int str_to_int_ex;
74 int str_to_int_hex;
75 } StrToIntResult;
76
77 static const StrToIntResult StrToInt_results[] = {
78 { "1099", 1099, 1099, 1099 },
79 { "+88987", 0, 88987, 88987 },
80 { "012", 12, 12, 12 },
81 { "-55", -55, -55, -55 },
82 { "-0", 0, 0, 0 },
83 { "0x44ff", 0, 0, 0x44ff },
84 { "+0x44f4", 0, 0, 0x44f4 },
85 { "-0x44fd", 0, 0, 0x44fd },
86 { "+ 88987", 0, 0, 0 },
87 { "- 55", 0, 0, 0 },
88 { "- 0", 0, 0, 0 },
89 { "+ 0x44f4", 0, 0, 0 },
90 { "--0x44fd", 0, 0, 0 },
91 { " 1999", 0, 1999, 1999 },
92 { " +88987", 0, 88987, 88987 },
93 { " 012", 0, 12, 12 },
94 { " -55", 0, -55, -55 },
95 { " 0x44ff", 0, 0, 0x44ff },
96 { " +0x44f4", 0, 0, 0x44f4 },
97 { " -0x44fd", 0, 0, 0x44fd },
98 { NULL, 0, 0, 0 }
99 };
100
101 /* pStrFormatByteSize64/StrFormatKBSize results */
102 typedef struct tagStrFormatSizeResult
103 {
104 LONGLONG value;
105 const char* byte_size_64;
106 const char* kb_size;
107 } StrFormatSizeResult;
108
109
110 static const StrFormatSizeResult StrFormatSize_results[] = {
111 { -1023, "-1023 bytes", "0 KB"},
112 { -24, "-24 bytes", "0 KB"},
113 { 309, "309 bytes", "1 KB"},
114 { 10191, "9.95 KB", "10 KB"},
115 { 100353, "98.0 KB", "99 KB"},
116 { 1022286, "998 KB", "999 KB"},
117 { 1046862, "0.99 MB", "1,023 KB"},
118 { 1048574619, "999 MB", "1,023,999 KB"},
119 { 1073741775, "0.99 GB", "1,048,576 KB"},
120 { ((LONGLONG)0x000000f9 << 32) | 0xfffff94e, "999 GB", "1,048,575,999 KB"},
121 { ((LONGLONG)0x000000ff << 32) | 0xfffffa9b, "0.99 TB", "1,073,741,823 KB"},
122 { ((LONGLONG)0x0003e7ff << 32) | 0xfffffa9b, "999 TB", "1,073,741,823,999 KB"},
123 { ((LONGLONG)0x0003ffff << 32) | 0xfffffbe8, "0.99 PB", "1,099,511,627,775 KB"},
124 { ((LONGLONG)0x0f9fffff << 32) | 0xfffffd35, "999 PB", "1,099,511,627,776,000 KB"},
125 { ((LONGLONG)0x0fffffff << 32) | 0xfffffa9b, "0.99 EB", "1,125,899,906,842,623 KB"},
126 { 0, NULL, NULL }
127 };
128
129 /* StrFormatByteSize64/StrFormatKBSize results */
130 typedef struct tagStrFromTimeIntervalResult
131 {
132 DWORD ms;
133 int digits;
134 const char* time_interval;
135 } StrFromTimeIntervalResult;
136
137
138 static const StrFromTimeIntervalResult StrFromTimeInterval_results[] = {
139 { 1, 1, " 0 sec" },
140 { 1, 2, " 0 sec" },
141 { 1, 3, " 0 sec" },
142 { 1, 4, " 0 sec" },
143 { 1, 5, " 0 sec" },
144 { 1, 6, " 0 sec" },
145 { 1, 7, " 0 sec" },
146
147 { 1000000, 1, " 10 min" },
148 { 1000000, 2, " 16 min" },
149 { 1000000, 3, " 16 min 40 sec" },
150 { 1000000, 4, " 16 min 40 sec" },
151 { 1000000, 5, " 16 min 40 sec" },
152 { 1000000, 6, " 16 min 40 sec" },
153 { 1000000, 7, " 16 min 40 sec" },
154
155 { 1999999, 1, " 30 min" },
156 { 1999999, 2, " 33 min" },
157 { 1999999, 3, " 33 min 20 sec" },
158 { 1999999, 4, " 33 min 20 sec" },
159 { 1999999, 5, " 33 min 20 sec" },
160 { 1999999, 6, " 33 min 20 sec" },
161 { 1999999, 7, " 33 min 20 sec" },
162
163 { 3999997, 1, " 1 hr" },
164 { 3999997, 2, " 1 hr 6 min" },
165 { 3999997, 3, " 1 hr 6 min 40 sec" },
166 { 3999997, 4, " 1 hr 6 min 40 sec" },
167 { 3999997, 5, " 1 hr 6 min 40 sec" },
168 { 3999997, 6, " 1 hr 6 min 40 sec" },
169 { 3999997, 7, " 1 hr 6 min 40 sec" },
170
171 { 149999851, 7, " 41 hr 40 min 0 sec" },
172 { 150999850, 1, " 40 hr" },
173 { 150999850, 2, " 41 hr" },
174 { 150999850, 3, " 41 hr 50 min" },
175 { 150999850, 4, " 41 hr 56 min" },
176 { 150999850, 5, " 41 hr 56 min 40 sec" },
177 { 150999850, 6, " 41 hr 56 min 40 sec" },
178 { 150999850, 7, " 41 hr 56 min 40 sec" },
179
180 { 493999507, 1, " 100 hr" },
181 { 493999507, 2, " 130 hr" },
182 { 493999507, 3, " 137 hr" },
183 { 493999507, 4, " 137 hr 10 min" },
184 { 493999507, 5, " 137 hr 13 min" },
185 { 493999507, 6, " 137 hr 13 min 20 sec" },
186 { 493999507, 7, " 137 hr 13 min 20 sec" },
187
188 { 0, 0, NULL }
189 };
190
191 static void test_StrChrA(void)
192 {
193 char string[129];
194 WORD count;
195
196 /* this test crashes on win2k SP4 */
197 /*ok(!StrChrA(NULL,'\0'), "found a character in a NULL string!\n");*/
198
199 for (count = 32; count < 128; count++)
200 string[count] = (char)count;
201 string[128] = '\0';
202
203 for (count = 32; count < 128; count++)
204 {
205 LPSTR result = StrChrA(string+32, count);
206 INT pos = result - string;
207 ok(pos == count, "found char '%c' in wrong place: got %d, expected %d\n", count, pos, count);
208 }
209
210 for (count = 32; count < 128; count++)
211 {
212 LPSTR result = StrChrA(string+count+1, count);
213 ok(!result, "found char '%c' not in the string\n", count);
214 }
215 }
216
217 static void test_StrChrW(void)
218 {
219 WCHAR string[16385];
220 WORD count;
221
222 /* this test crashes on win2k SP4 */
223 /*ok(!StrChrW(NULL,'\0'), "found a character in a NULL string!\n");*/
224
225 for (count = 32; count < 16384; count++)
226 string[count] = count;
227 string[16384] = '\0';
228
229 for (count = 32; count < 16384; count++)
230 {
231 LPWSTR result = StrChrW(string+32, count);
232 ok((result - string) == count, "found char %d in wrong place\n", count);
233 }
234
235 for (count = 32; count < 16384; count++)
236 {
237 LPWSTR result = StrChrW(string+count+1, count);
238 ok(!result, "found char not in the string\n");
239 }
240 }
241
242 static void test_StrChrIA(void)
243 {
244 char string[129];
245 WORD count;
246
247 /* this test crashes on win2k SP4 */
248 /*ok(!StrChrIA(NULL,'\0'), "found a character in a NULL string!\n");*/
249
250 for (count = 32; count < 128; count++)
251 string[count] = (char)count;
252 string[128] = '\0';
253
254 for (count = 'A'; count <= 'X'; count++)
255 {
256 LPSTR result = StrChrIA(string+32, count);
257
258 ok(result - string == count, "found char '%c' in wrong place\n", count);
259 ok(StrChrIA(result, count)!=NULL, "didn't find lowercase '%c'\n", count);
260 }
261
262 for (count = 'a'; count < 'z'; count++)
263 {
264 LPSTR result = StrChrIA(string+count+1, count);
265 ok(!result, "found char not in the string\n");
266 }
267 }
268
269 static void test_StrChrIW(void)
270 {
271 WCHAR string[129];
272 WORD count;
273
274 /* this test crashes on win2k SP4 */
275 /*ok(!StrChrIA(NULL,'\0'), "found a character in a NULL string!\n");*/
276
277 for (count = 32; count < 128; count++)
278 string[count] = count;
279 string[128] = '\0';
280
281 for (count = 'A'; count <= 'X'; count++)
282 {
283 LPWSTR result = StrChrIW(string+32, count);
284
285 ok(result - string == count, "found char '%c' in wrong place\n", count);
286 ok(StrChrIW(result, count)!=NULL, "didn't find lowercase '%c'\n", count);
287 }
288
289 for (count = 'a'; count < 'z'; count++)
290 {
291 LPWSTR result = StrChrIW(string+count+1, count);
292 ok(!result, "found char not in the string\n");
293 }
294 }
295
296 static void test_StrRChrA(void)
297 {
298 char string[129];
299 WORD count;
300
301 /* this test crashes on win2k SP4 */
302 /*ok(!StrRChrA(NULL, NULL,'\0'), "found a character in a NULL string!\n");*/
303
304 for (count = 32; count < 128; count++)
305 string[count] = (char)count;
306 string[128] = '\0';
307
308 for (count = 32; count < 128; count++)
309 {
310 LPSTR result = StrRChrA(string+32, NULL, count);
311 ok(result - string == count, "found char %d in wrong place\n", count);
312 }
313
314 for (count = 32; count < 128; count++)
315 {
316 LPSTR result = StrRChrA(string+count+1, NULL, count);
317 ok(!result, "found char not in the string\n");
318 }
319
320 for (count = 32; count < 128; count++)
321 {
322 LPSTR result = StrRChrA(string+count+1, string + 127, count);
323 ok(!result, "found char not in the string\n");
324 }
325 }
326
327 static void test_StrRChrW(void)
328 {
329 WCHAR string[129];
330 WORD count;
331
332 /* this test crashes on win2k SP4 */
333 /*ok(!StrRChrW(NULL, NULL,'\0'), "found a character in a NULL string!\n");*/
334
335 for (count = 32; count < 128; count++)
336 string[count] = count;
337 string[128] = '\0';
338
339 for (count = 32; count < 128; count++)
340 {
341 LPWSTR result = StrRChrW(string+32, NULL, count);
342 INT pos = result - string;
343 ok(pos == count, "found char %d in wrong place: got %d, expected %d\n", count, pos, count);
344 }
345
346 for (count = 32; count < 128; count++)
347 {
348 LPWSTR result = StrRChrW(string+count+1, NULL, count);
349 ok(!result, "found char %d not in the string\n", count);
350 }
351
352 for (count = 32; count < 128; count++)
353 {
354 LPWSTR result = StrRChrW(string+count+1, string + 127, count);
355 ok(!result, "found char %d not in the string\n", count);
356 }
357 }
358
359 static void test_StrCpyW(void)
360 {
361 WCHAR szSrc[256];
362 WCHAR szBuff[256];
363 const StrFormatSizeResult* result = StrFormatSize_results;
364
365
366 while(result->value)
367 {
368 MultiByteToWideChar(0,0,result->byte_size_64,-1,szSrc,sizeof(szSrc)/sizeof(WCHAR));
369
370 StrCpyW(szBuff, szSrc);
371 ok(!StrCmpW(szSrc, szBuff), "Copied string %s wrong\n", result->byte_size_64);
372 result++;
373 }
374 }
375
376
377 static void test_StrToIntA(void)
378 {
379 const StrToIntResult *result = StrToInt_results;
380 int return_val;
381
382 while (result->string)
383 {
384 return_val = StrToIntA(result->string);
385 ok(return_val == result->str_to_int, "converted '%s' wrong (%d)\n",
386 result->string, return_val);
387 result++;
388 }
389 }
390
391 static void test_StrToIntW(void)
392 {
393 WCHAR szBuff[256];
394 const StrToIntResult *result = StrToInt_results;
395 int return_val;
396
397 while (result->string)
398 {
399 MultiByteToWideChar(0,0,result->string,-1,szBuff,sizeof(szBuff)/sizeof(WCHAR));
400 return_val = StrToIntW(szBuff);
401 ok(return_val == result->str_to_int, "converted '%s' wrong (%d)\n",
402 result->string, return_val);
403 result++;
404 }
405 }
406
407 static void test_StrToIntExA(void)
408 {
409 const StrToIntResult *result = StrToInt_results;
410 int return_val;
411 BOOL bRet;
412
413 while (result->string)
414 {
415 return_val = -1;
416 bRet = StrToIntExA(result->string,0,&return_val);
417 ok(!bRet || return_val != -1, "No result returned from '%s'\n",
418 result->string);
419 if (bRet)
420 ok(return_val == result->str_to_int_ex, "converted '%s' wrong (%d)\n",
421 result->string, return_val);
422 result++;
423 }
424
425 result = StrToInt_results;
426 while (result->string)
427 {
428 return_val = -1;
429 bRet = StrToIntExA(result->string,STIF_SUPPORT_HEX,&return_val);
430 ok(!bRet || return_val != -1, "No result returned from '%s'\n",
431 result->string);
432 if (bRet)
433 ok(return_val == result->str_to_int_hex, "converted '%s' wrong (%d)\n",
434 result->string, return_val);
435 result++;
436 }
437 }
438
439 static void test_StrToIntExW(void)
440 {
441 WCHAR szBuff[256];
442 const StrToIntResult *result = StrToInt_results;
443 int return_val;
444 BOOL bRet;
445
446 while (result->string)
447 {
448 return_val = -1;
449 MultiByteToWideChar(0,0,result->string,-1,szBuff,sizeof(szBuff)/sizeof(WCHAR));
450 bRet = StrToIntExW(szBuff, 0, &return_val);
451 ok(!bRet || return_val != -1, "No result returned from '%s'\n",
452 result->string);
453 if (bRet)
454 ok(return_val == result->str_to_int_ex, "converted '%s' wrong (%d)\n",
455 result->string, return_val);
456 result++;
457 }
458
459 result = StrToInt_results;
460 while (result->string)
461 {
462 return_val = -1;
463 MultiByteToWideChar(0,0,result->string,-1,szBuff,sizeof(szBuff)/sizeof(WCHAR));
464 bRet = StrToIntExW(szBuff, STIF_SUPPORT_HEX, &return_val);
465 ok(!bRet || return_val != -1, "No result returned from '%s'\n",
466 result->string);
467 if (bRet)
468 ok(return_val == result->str_to_int_hex, "converted '%s' wrong (%d)\n",
469 result->string, return_val);
470 result++;
471 }
472 }
473
474 static void test_StrDupA(void)
475 {
476 LPSTR lpszStr;
477 const StrFormatSizeResult* result = StrFormatSize_results;
478
479 while(result->value)
480 {
481 lpszStr = StrDupA(result->byte_size_64);
482
483 ok(lpszStr != NULL, "Dup failed\n");
484 if (lpszStr)
485 {
486 ok(!strcmp(result->byte_size_64, lpszStr), "Copied string wrong\n");
487 LocalFree(lpszStr);
488 }
489 result++;
490 }
491
492 /* Later versions of shlwapi return NULL for this, but earlier versions
493 * returned an empty string (as Wine does).
494 */
495 lpszStr = StrDupA(NULL);
496 ok(lpszStr == NULL || *lpszStr == '\0', "NULL string returned %p\n", lpszStr);
497 LocalFree(lpszStr);
498 }
499
500 static void test_StrFormatByteSize64A(void)
501 {
502 char szBuff[256];
503 const StrFormatSizeResult* result = StrFormatSize_results;
504
505 if (!pStrFormatByteSize64A)
506 {
507 win_skip("StrFormatByteSize64A() is not available\n");
508 return;
509 }
510
511 while(result->value)
512 {
513 pStrFormatByteSize64A(result->value, szBuff, 256);
514
515 ok(!strcmp(result->byte_size_64, szBuff),
516 "Formatted %x%08x wrong: got %s, expected %s\n",
517 (LONG)(result->value >> 32), (LONG)result->value, szBuff, result->byte_size_64);
518
519 result++;
520 }
521 }
522
523 static void test_StrFormatKBSizeW(void)
524 {
525 WCHAR szBuffW[256];
526 char szBuff[256];
527 const StrFormatSizeResult* result = StrFormatSize_results;
528
529 if (!pStrFormatKBSizeW)
530 {
531 win_skip("StrFormatKBSizeW() is not available\n");
532 return;
533 }
534
535 while(result->value)
536 {
537 pStrFormatKBSizeW(result->value, szBuffW, 256);
538 WideCharToMultiByte(0,0,szBuffW,-1,szBuff,sizeof(szBuff)/sizeof(WCHAR),0,0);
539 ok(!strcmp(result->kb_size, szBuff),
540 "Formatted %x%08x wrong: got %s, expected %s\n",
541 (LONG)(result->value >> 32), (LONG)result->value, szBuff, result->kb_size);
542 result++;
543 }
544 }
545
546 static void test_StrFormatKBSizeA(void)
547 {
548 char szBuff[256];
549 const StrFormatSizeResult* result = StrFormatSize_results;
550
551 if (!pStrFormatKBSizeA)
552 {
553 win_skip("StrFormatKBSizeA() is not available\n");
554 return;
555 }
556
557 while(result->value)
558 {
559 pStrFormatKBSizeA(result->value, szBuff, 256);
560
561 ok(!strcmp(result->kb_size, szBuff),
562 "Formatted %x%08x wrong: got %s, expected %s\n",
563 (LONG)(result->value >> 32), (LONG)result->value, szBuff, result->kb_size);
564 result++;
565 }
566 }
567
568 static void test_StrFromTimeIntervalA(void)
569 {
570 char szBuff[256];
571 const StrFromTimeIntervalResult* result = StrFromTimeInterval_results;
572
573 while(result->ms)
574 {
575 StrFromTimeIntervalA(szBuff, 256, result->ms, result->digits);
576
577 ok(!strcmp(result->time_interval, szBuff), "Formatted %d %d wrong\n",
578 result->ms, result->digits);
579 result++;
580 }
581 }
582
583 static void test_StrCmpA(void)
584 {
585 static const char str1[] = {'a','b','c','d','e','f'};
586 static const char str2[] = {'a','B','c','d','e','f'};
587 ok(0 != StrCmpNA(str1, str2, 6), "StrCmpNA is case-insensitive\n");
588 ok(0 == StrCmpNIA(str1, str2, 6), "StrCmpNIA is case-sensitive\n");
589 ok(!ChrCmpIA('a', 'a'), "ChrCmpIA doesn't work at all!\n");
590 ok(!ChrCmpIA('b', 'B'), "ChrCmpIA is not case-insensitive\n");
591 ok(ChrCmpIA('a', 'z'), "ChrCmpIA believes that a == z!\n");
592
593 if (pStrIsIntlEqualA)
594 {
595 ok(pStrIsIntlEqualA(FALSE, str1, str2, 5), "StrIsIntlEqualA(FALSE,...) isn't case-insensitive\n");
596 ok(!pStrIsIntlEqualA(TRUE, str1, str2, 5), "StrIsIntlEqualA(TRUE,...) isn't case-sensitive\n");
597 }
598 else
599 win_skip("StrIsIntlEqualA() is not available\n");
600
601 if (pIntlStrEqWorkerA)
602 {
603 ok(pIntlStrEqWorkerA(FALSE, str1, str2, 5), "IntlStrEqWorkerA(FALSE,...) isn't case-insensitive\n");
604 ok(!pIntlStrEqWorkerA(TRUE, str1, str2, 5), "pIntlStrEqWorkerA(TRUE,...) isn't case-sensitive\n");
605 }
606 else
607 win_skip("IntlStrEqWorkerA() is not available\n");
608 }
609
610 static void test_StrCmpW(void)
611 {
612 static const WCHAR str1[] = {'a','b','c','d','e','f'};
613 static const WCHAR str2[] = {'a','B','c','d','e','f'};
614 ok(0 != StrCmpNW(str1, str2, 5), "StrCmpNW is case-insensitive\n");
615 ok(0 == StrCmpNIW(str1, str2, 5), "StrCmpNIW is case-sensitive\n");
616 ok(!ChrCmpIW('a', 'a'), "ChrCmpIW doesn't work at all!\n");
617 ok(!ChrCmpIW('b', 'B'), "ChrCmpIW is not case-insensitive\n");
618 ok(ChrCmpIW('a', 'z'), "ChrCmpIW believes that a == z!\n");
619
620 if (pStrIsIntlEqualW)
621 {
622 ok(pStrIsIntlEqualW(FALSE, str1, str2, 5), "StrIsIntlEqualW(FALSE,...) isn't case-insensitive\n");
623 ok(!pStrIsIntlEqualW(TRUE, str1, str2, 5), "StrIsIntlEqualW(TRUE,...) isn't case-sensitive\n");
624 }
625 else
626 win_skip("StrIsIntlEqualW() is not available\n");
627
628 if (pIntlStrEqWorkerW)
629 {
630 ok(pIntlStrEqWorkerW(FALSE, str1, str2, 5), "IntlStrEqWorkerW(FALSE,...) isn't case-insensitive\n");
631 ok(!pIntlStrEqWorkerW(TRUE, str1, str2, 5), "IntlStrEqWorkerW(TRUE,...) isn't case-sensitive\n");
632 }
633 else
634 win_skip("IntlStrEqWorkerW() is not available\n");
635 }
636
637 static WCHAR *CoDupStrW(const char* src)
638 {
639 INT len = MultiByteToWideChar(CP_ACP, 0, src, -1, NULL, 0);
640 WCHAR* szTemp = CoTaskMemAlloc(len * sizeof(WCHAR));
641 MultiByteToWideChar(CP_ACP, 0, src, -1, szTemp, len);
642 return szTemp;
643 }
644
645 static void test_StrRetToBSTR(void)
646 {
647 static const WCHAR szTestW[] = { 'T','e','s','t','\0' };
648 ITEMIDLIST iidl[10];
649 BSTR bstr;
650 STRRET strret;
651 HRESULT ret;
652
653 if (!pStrRetToBSTR)
654 {
655 win_skip("StrRetToBSTR() is not available\n");
656 return;
657 }
658
659 strret.uType = STRRET_WSTR;
660 U(strret).pOleStr = CoDupStrW("Test");
661 bstr = 0;
662 ret = pStrRetToBSTR(&strret, NULL, &bstr);
663 ok(ret == S_OK && bstr && !strcmpW(bstr, szTestW),
664 "STRRET_WSTR: dup failed, ret=0x%08x, bstr %p\n", ret, bstr);
665 SysFreeString(bstr);
666
667 strret.uType = STRRET_CSTR;
668 lstrcpyA(U(strret).cStr, "Test");
669 ret = pStrRetToBSTR(&strret, NULL, &bstr);
670 ok(ret == S_OK && bstr && !strcmpW(bstr, szTestW),
671 "STRRET_CSTR: dup failed, ret=0x%08x, bstr %p\n", ret, bstr);
672 SysFreeString(bstr);
673
674 strret.uType = STRRET_OFFSET;
675 U(strret).uOffset = 1;
676 strcpy((char*)&iidl, " Test");
677 ret = pStrRetToBSTR(&strret, iidl, &bstr);
678 ok(ret == S_OK && bstr && !strcmpW(bstr, szTestW),
679 "STRRET_OFFSET: dup failed, ret=0x%08x, bstr %p\n", ret, bstr);
680 SysFreeString(bstr);
681
682 /* Native crashes if str is NULL */
683 }
684
685 static void test_StrCpyNXA(void)
686 {
687 LPCSTR lpSrc = "hello";
688 LPSTR lpszRes;
689 char dest[8];
690
691 if (!pStrCpyNXA)
692 {
693 win_skip("StrCpyNXA() is not available\n");
694 return;
695 }
696
697 memset(dest, '\n', sizeof(dest));
698 lpszRes = pStrCpyNXA(dest, lpSrc, sizeof(dest)/sizeof(dest[0]));
699 ok(lpszRes == dest + 5 && !memcmp(dest, "hello\0\n\n", sizeof(dest)),
700 "StrCpyNXA: expected %p, \"hello\\0\\n\\n\", got %p, \"%d,%d,%d,%d,%d,%d,%d,%d\"\n",
701 dest + 5, lpszRes, dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
702 }
703
704 static void test_StrCpyNXW(void)
705 {
706 static const WCHAR lpInit[] = { '\n','\n','\n','\n','\n','\n','\n','\n' };
707 static const WCHAR lpSrc[] = { 'h','e','l','l','o','\0' };
708 static const WCHAR lpRes[] = { 'h','e','l','l','o','\0','\n','\n' };
709 LPWSTR lpszRes;
710 WCHAR dest[8];
711
712 if (!pStrCpyNXW)
713 {
714 win_skip("StrCpyNXW() is not available\n");
715 return;
716 }
717
718 memcpy(dest, lpInit, sizeof(lpInit));
719 lpszRes = pStrCpyNXW(dest, lpSrc, sizeof(dest)/sizeof(dest[0]));
720 ok(lpszRes == dest + 5 && !memcmp(dest, lpRes, sizeof(dest)),
721 "StrCpyNXW: expected %p, \"hello\\0\\n\\n\", got %p, \"%d,%d,%d,%d,%d,%d,%d,%d\"\n",
722 dest + 5, lpszRes, dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
723 }
724
725 #define check_strrstri(type, str, pos, needle, exp) \
726 ret##type = StrRStrI##type(str, str+pos, needle); \
727 ok(ret##type == (exp), "Type " #type ", expected %p but got %p (string base %p)\n", \
728 (exp), ret##type, str);
729
730 static void test_StrRStrI(void)
731 {
732 static const CHAR szTest[] = "yAxxxxAy";
733 static const CHAR szTest2[] = "ABABABAB";
734 static const WCHAR wszTest[] = {'y','A','x','x','x','x','A','y',0};
735 static const WCHAR wszTest2[] = {'A','B','A','B','A','B','A','B',0};
736
737 static const WCHAR wszPattern1[] = {'A',0};
738 static const WCHAR wszPattern2[] = {'a','X',0};
739 static const WCHAR wszPattern3[] = {'A','y',0};
740 static const WCHAR wszPattern4[] = {'a','b',0};
741 LPWSTR retW;
742 LPSTR retA;
743
744 check_strrstri(A, szTest, 4, "A", szTest+1);
745 check_strrstri(A, szTest, 4, "aX", szTest+1);
746 check_strrstri(A, szTest, 4, "Ay", NULL);
747 check_strrstri(W, wszTest, 4, wszPattern1, wszTest+1);
748 check_strrstri(W, wszTest, 4, wszPattern2, wszTest+1);
749 check_strrstri(W, wszTest, 4, wszPattern3, NULL);
750
751 check_strrstri(A, szTest2, 4, "ab", szTest2+2);
752 check_strrstri(A, szTest2, 3, "ab", szTest2+2);
753 check_strrstri(A, szTest2, 2, "ab", szTest2);
754 check_strrstri(A, szTest2, 1, "ab", szTest2);
755 check_strrstri(A, szTest2, 0, "ab", NULL);
756 check_strrstri(W, wszTest2, 4, wszPattern4, wszTest2+2);
757 check_strrstri(W, wszTest2, 3, wszPattern4, wszTest2+2);
758 check_strrstri(W, wszTest2, 2, wszPattern4, wszTest2);
759 check_strrstri(W, wszTest2, 1, wszPattern4, wszTest2);
760 check_strrstri(W, wszTest2, 0, wszPattern4, NULL);
761
762 }
763
764 static void test_SHAnsiToAnsi(void)
765 {
766 char dest[8];
767 DWORD dwRet;
768
769 if (!pSHAnsiToAnsi)
770 {
771 win_skip("SHAnsiToAnsi() is not available\n");
772 return;
773 }
774
775 memset(dest, '\n', sizeof(dest));
776 dwRet = pSHAnsiToAnsi("hello", dest, sizeof(dest)/sizeof(dest[0]));
777 ok(dwRet == 6 && !memcmp(dest, "hello\0\n\n", sizeof(dest)),
778 "SHAnsiToAnsi: expected 6, \"hello\\0\\n\\n\", got %d, \"%d,%d,%d,%d,%d,%d,%d,%d\"\n",
779 dwRet, dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
780 }
781
782 static void test_SHUnicodeToUnicode(void)
783 {
784 static const WCHAR lpInit[] = { '\n','\n','\n','\n','\n','\n','\n','\n' };
785 static const WCHAR lpSrc[] = { 'h','e','l','l','o','\0' };
786 static const WCHAR lpRes[] = { 'h','e','l','l','o','\0','\n','\n' };
787 WCHAR dest[8];
788 DWORD dwRet;
789
790 if (!pSHUnicodeToUnicode)
791 {
792 win_skip("SHUnicodeToUnicode() is not available\n");
793 return;
794 }
795
796 memcpy(dest, lpInit, sizeof(lpInit));
797 dwRet = pSHUnicodeToUnicode(lpSrc, dest, sizeof(dest)/sizeof(dest[0]));
798 ok(dwRet == 6 && !memcmp(dest, lpRes, sizeof(dest)),
799 "SHUnicodeToUnicode: expected 6, \"hello\\0\\n\\n\", got %d, \"%d,%d,%d,%d,%d,%d,%d,%d\"\n",
800 dwRet, dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
801 }
802
803 static void test_StrXXX_overflows(void)
804 {
805 CHAR str1[2*MAX_PATH+1], buf[2*MAX_PATH];
806 WCHAR wstr1[2*MAX_PATH+1], wbuf[2*MAX_PATH];
807 const WCHAR fmt[] = {'%','s',0};
808 STRRET strret;
809 int ret;
810 int i;
811
812 for (i=0; i<2*MAX_PATH; i++)
813 {
814 str1[i] = '0'+(i%10);
815 wstr1[i] = '0'+(i%10);
816 }
817 str1[2*MAX_PATH] = 0;
818 wstr1[2*MAX_PATH] = 0;
819
820 memset(buf, 0xbf, sizeof(buf));
821 expect_eq(StrCpyNA(buf, str1, 10), buf, PCHAR, "%p");
822 expect_eq(buf[9], 0, CHAR, "%x");
823 expect_eq(buf[10], '\xbf', CHAR, "%x");
824
825 if (pStrCatBuffA)
826 {
827 expect_eq(pStrCatBuffA(buf, str1, 100), buf, PCHAR, "%p");
828 expect_eq(buf[99], 0, CHAR, "%x");
829 expect_eq(buf[100], '\xbf', CHAR, "%x");
830 }
831 else
832 win_skip("StrCatBuffA() is not available\n");
833
834 memset(wbuf, 0xbf, sizeof(wbuf));
835 expect_eq(StrCpyNW(wbuf, wstr1, 10), wbuf, PWCHAR, "%p");
836 expect_eq(wbuf[9], 0, WCHAR, "%x");
837 expect_eq(wbuf[10], (WCHAR)0xbfbf, WCHAR, "%x");
838
839 if (pStrCatBuffW)
840 {
841 expect_eq(pStrCatBuffW(wbuf, wstr1, 100), wbuf, PWCHAR, "%p");
842 expect_eq(wbuf[99], 0, WCHAR, "%x");
843 expect_eq(wbuf[100], (WCHAR)0xbfbf, WCHAR, "%x");
844 }
845 else
846 win_skip("StrCatBuffW() is not available\n");
847
848 if (pStrRetToBufW)
849 {
850 memset(wbuf, 0xbf, sizeof(wbuf));
851 strret.uType = STRRET_WSTR;
852 U(strret).pOleStr = StrDupW(wstr1);
853 expect_eq2(pStrRetToBufW(&strret, NULL, wbuf, 10), S_OK, HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER) /* Vista */, HRESULT, "%x");
854 expect_eq(wbuf[9], 0, WCHAR, "%x");
855 expect_eq(wbuf[10], (WCHAR)0xbfbf, WCHAR, "%x");
856 }
857 else
858 win_skip("StrRetToBufW() is not available\n");
859
860 if (pStrRetToBufA)
861 {
862 memset(buf, 0xbf, sizeof(buf));
863 strret.uType = STRRET_CSTR;
864 StrCpyN(U(strret).cStr, str1, MAX_PATH);
865 expect_eq2(pStrRetToBufA(&strret, NULL, buf, 10), S_OK, HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER) /* Vista */, HRESULT, "%x");
866 expect_eq(buf[9], 0, CHAR, "%x");
867 expect_eq(buf[10], (CHAR)0xbf, CHAR, "%x");
868 }
869 else
870 win_skip("StrRetToBufA() is not available\n");
871
872 if (pwnsprintfA)
873 {
874 memset(buf, 0xbf, sizeof(buf));
875 ret = pwnsprintfA(buf, 10, "%s", str1);
876 ok(broken(ret == 9) || ret == -1 /* Vista */, "Unexpected wsnprintfA return %d, expected 9 or -1\n", ret);
877 expect_eq(buf[9], 0, CHAR, "%x");
878 expect_eq(buf[10], (CHAR)0xbf, CHAR, "%x");
879 }
880 else
881 win_skip("wnsprintfA() is not available\n");
882
883 if (pwnsprintfW)
884 {
885 memset(wbuf, 0xbf, sizeof(wbuf));
886 ret = pwnsprintfW(wbuf, 10, fmt, wstr1);
887 ok(broken(ret == 9) || ret == -1 /* Vista */, "Unexpected wsnprintfW return %d, expected 9 or -1\n", ret);
888 expect_eq(wbuf[9], 0, WCHAR, "%x");
889 expect_eq(wbuf[10], (WCHAR)0xbfbf, WCHAR, "%x");
890 }
891 else
892 win_skip("wnsprintfW() is not available\n");
893 }
894
895 START_TEST(string)
896 {
897 HMODULE hShlwapi;
898 TCHAR thousandDelim[8];
899 TCHAR decimalDelim[8];
900 CoInitialize(0);
901
902 GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, thousandDelim, 8);
903 GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, decimalDelim, 8);
904
905 hShlwapi = GetModuleHandleA("shlwapi");
906 pIntlStrEqWorkerA = (void *)GetProcAddress(hShlwapi, "IntlStrEqWorkerA");
907 pIntlStrEqWorkerW = (void *)GetProcAddress(hShlwapi, "IntlStrEqWorkerW");
908 pSHAnsiToAnsi = (void *)GetProcAddress(hShlwapi, (LPSTR)345);
909 pSHUnicodeToUnicode = (void *)GetProcAddress(hShlwapi, (LPSTR)346);
910 pStrCatBuffA = (void *)GetProcAddress(hShlwapi, "StrCatBuffA");
911 pStrCatBuffW = (void *)GetProcAddress(hShlwapi, "StrCatBuffW");
912 pStrCpyNXA = (void *)GetProcAddress(hShlwapi, (LPSTR)399);
913 pStrCpyNXW = (void *)GetProcAddress(hShlwapi, (LPSTR)400);
914 pStrFormatByteSize64A = (void *)GetProcAddress(hShlwapi, "StrFormatByteSize64A");
915 pStrFormatKBSizeA = (void *)GetProcAddress(hShlwapi, "StrFormatKBSizeA");
916 pStrFormatKBSizeW = (void *)GetProcAddress(hShlwapi, "StrFormatKBSizeW");
917 pStrIsIntlEqualA = (void *)GetProcAddress(hShlwapi, "StrIsIntlEqualA");
918 pStrIsIntlEqualW = (void *)GetProcAddress(hShlwapi, "StrIsIntlEqualW");
919 pStrRetToBSTR = (void *)GetProcAddress(hShlwapi, "StrRetToBSTR");
920 pStrRetToBufA = (void *)GetProcAddress(hShlwapi, "StrRetToBufA");
921 pStrRetToBufW = (void *)GetProcAddress(hShlwapi, "StrRetToBufW");
922 pwnsprintfA = (void *)GetProcAddress(hShlwapi, "wnsprintfA");
923 pwnsprintfW = (void *)GetProcAddress(hShlwapi, "wnsprintfW");
924
925 test_StrChrA();
926 test_StrChrW();
927 test_StrChrIA();
928 test_StrChrIW();
929 test_StrRChrA();
930 test_StrRChrW();
931 test_StrCpyW();
932 test_StrToIntA();
933 test_StrToIntW();
934 test_StrToIntExA();
935 test_StrToIntExW();
936 test_StrDupA();
937 if (lstrcmp(thousandDelim, ",")==0 && lstrcmp(decimalDelim, ".")==0)
938 {
939 /* these tests are locale-dependent */
940 test_StrFormatByteSize64A();
941 test_StrFormatKBSizeA();
942 test_StrFormatKBSizeW();
943 }
944
945 /* language-dependent test */
946 if (PRIMARYLANGID(GetUserDefaultLangID()) != LANG_ENGLISH)
947 trace("Skipping StrFromTimeInterval test for non English language\n");
948 else
949 test_StrFromTimeIntervalA();
950
951 test_StrCmpA();
952 test_StrCmpW();
953 test_StrRetToBSTR();
954 test_StrCpyNXA();
955 test_StrCpyNXW();
956 test_StrRStrI();
957 test_SHAnsiToAnsi();
958 test_SHUnicodeToUnicode();
959 test_StrXXX_overflows();
960
961 CoUninitialize();
962 }