shlwapi: Fixed the string tests on Vista.
[wine/multimedia.git] / dlls / shlwapi / tests / string.c
blob0875221095b3aa76ce6bf99049f661319582c9b6
1 /* Unit test suite for SHLWAPI string functions
3 * Copyright 2003 Jon Griffiths
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.
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.
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
20 #include <stdio.h>
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"
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);
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);
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, ...);
62 static int strcmpW(const WCHAR *str1, const WCHAR *str2)
64 while (*str1 && (*str1 == *str2)) { str1++; str2++; }
65 return *str1 - *str2;
68 /* StrToInt/StrToIntEx results */
69 typedef struct tagStrToIntResult
71 const char* string;
72 int str_to_int;
73 int str_to_int_ex;
74 int str_to_int_hex;
75 } StrToIntResult;
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 }
101 /* pStrFormatByteSize64/StrFormatKBSize results */
102 typedef struct tagStrFormatSizeResult
104 LONGLONG value;
105 const char* byte_size_64;
106 const char* kb_size;
107 } StrFormatSizeResult;
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 }
129 /* StrFormatByteSize64/StrFormatKBSize results */
130 typedef struct tagStrFromTimeIntervalResult
132 DWORD ms;
133 int digits;
134 const char* time_interval;
135 } StrFromTimeIntervalResult;
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" },
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" },
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" },
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" },
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" },
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" },
188 { 0, 0, NULL }
191 static void test_StrChrA(void)
193 char string[129];
194 WORD count;
196 /* this test crashes on win2k SP4 */
197 /*ok(!StrChrA(NULL,'\0'), "found a character in a NULL string!\n");*/
199 for (count = 32; count < 128; count++)
200 string[count] = (char)count;
201 string[128] = '\0';
203 for (count = 32; count < 128; count++)
205 LPSTR result = StrChrA(string+32, count);
206 ok(result - string == count,
207 "found char '%c' in wrong place: got %d, expected %d\n",
208 count, result - string, count);
211 for (count = 32; count < 128; count++)
213 LPSTR result = StrChrA(string+count+1, count);
214 ok(!result, "found char '%c' not in the string\n", count);
218 static void test_StrChrW(void)
220 WCHAR string[16385];
221 WORD count;
223 /* this test crashes on win2k SP4 */
224 /*ok(!StrChrW(NULL,'\0'), "found a character in a NULL string!\n");*/
226 for (count = 32; count < 16384; count++)
227 string[count] = count;
228 string[16384] = '\0';
230 for (count = 32; count < 16384; count++)
232 LPWSTR result = StrChrW(string+32, count);
233 ok((result - string) == count, "found char %d in wrong place\n", count);
236 for (count = 32; count < 16384; count++)
238 LPWSTR result = StrChrW(string+count+1, count);
239 ok(!result, "found char not in the string\n");
243 static void test_StrChrIA(void)
245 char string[129];
246 WORD count;
248 /* this test crashes on win2k SP4 */
249 /*ok(!StrChrIA(NULL,'\0'), "found a character in a NULL string!\n");*/
251 for (count = 32; count < 128; count++)
252 string[count] = (char)count;
253 string[128] = '\0';
255 for (count = 'A'; count <= 'X'; count++)
257 LPSTR result = StrChrIA(string+32, count);
259 ok(result - string == count, "found char '%c' in wrong place\n", count);
260 ok(StrChrIA(result, count)!=NULL, "didn't find lowercase '%c'\n", count);
263 for (count = 'a'; count < 'z'; count++)
265 LPSTR result = StrChrIA(string+count+1, count);
266 ok(!result, "found char not in the string\n");
270 static void test_StrChrIW(void)
272 WCHAR string[129];
273 WORD count;
275 /* this test crashes on win2k SP4 */
276 /*ok(!StrChrIA(NULL,'\0'), "found a character in a NULL string!\n");*/
278 for (count = 32; count < 128; count++)
279 string[count] = count;
280 string[128] = '\0';
282 for (count = 'A'; count <= 'X'; count++)
284 LPWSTR result = StrChrIW(string+32, count);
286 ok(result - string == count, "found char '%c' in wrong place\n", count);
287 ok(StrChrIW(result, count)!=NULL, "didn't find lowercase '%c'\n", count);
290 for (count = 'a'; count < 'z'; count++)
292 LPWSTR result = StrChrIW(string+count+1, count);
293 ok(!result, "found char not in the string\n");
297 static void test_StrRChrA(void)
299 char string[129];
300 WORD count;
302 /* this test crashes on win2k SP4 */
303 /*ok(!StrRChrA(NULL, NULL,'\0'), "found a character in a NULL string!\n");*/
305 for (count = 32; count < 128; count++)
306 string[count] = (char)count;
307 string[128] = '\0';
309 for (count = 32; count < 128; count++)
311 LPSTR result = StrRChrA(string+32, NULL, count);
312 ok(result - string == count, "found char %d in wrong place\n", count);
315 for (count = 32; count < 128; count++)
317 LPSTR result = StrRChrA(string+count+1, NULL, count);
318 ok(!result, "found char not in the string\n");
321 for (count = 32; count < 128; count++)
323 LPSTR result = StrRChrA(string+count+1, string + 127, count);
324 ok(!result, "found char not in the string\n");
328 static void test_StrRChrW(void)
330 WCHAR string[129];
331 WORD count;
333 /* this test crashes on win2k SP4 */
334 /*ok(!StrRChrW(NULL, NULL,'\0'), "found a character in a NULL string!\n");*/
336 for (count = 32; count < 128; count++)
337 string[count] = count;
338 string[128] = '\0';
340 for (count = 32; count < 128; count++)
342 LPWSTR result = StrRChrW(string+32, NULL, count);
343 ok(result - string == count,
344 "found char %d in wrong place: got %d, expected %d\n",
345 count, result - string, count);
348 for (count = 32; count < 128; count++)
350 LPWSTR result = StrRChrW(string+count+1, NULL, count);
351 ok(!result, "found char %d not in the string\n", count);
354 for (count = 32; count < 128; count++)
356 LPWSTR result = StrRChrW(string+count+1, string + 127, count);
357 ok(!result, "found char %d not in the string\n", count);
361 static void test_StrCpyW(void)
363 WCHAR szSrc[256];
364 WCHAR szBuff[256];
365 const StrFormatSizeResult* result = StrFormatSize_results;
368 while(result->value)
370 MultiByteToWideChar(0,0,result->byte_size_64,-1,szSrc,sizeof(szSrc)/sizeof(WCHAR));
372 StrCpyW(szBuff, szSrc);
373 ok(!StrCmpW(szSrc, szBuff), "Copied string %s wrong\n", result->byte_size_64);
374 result++;
379 static void test_StrToIntA(void)
381 const StrToIntResult *result = StrToInt_results;
382 int return_val;
384 while (result->string)
386 return_val = StrToIntA(result->string);
387 ok(return_val == result->str_to_int, "converted '%s' wrong (%d)\n",
388 result->string, return_val);
389 result++;
393 static void test_StrToIntW(void)
395 WCHAR szBuff[256];
396 const StrToIntResult *result = StrToInt_results;
397 int return_val;
399 while (result->string)
401 MultiByteToWideChar(0,0,result->string,-1,szBuff,sizeof(szBuff)/sizeof(WCHAR));
402 return_val = StrToIntW(szBuff);
403 ok(return_val == result->str_to_int, "converted '%s' wrong (%d)\n",
404 result->string, return_val);
405 result++;
409 static void test_StrToIntExA(void)
411 const StrToIntResult *result = StrToInt_results;
412 int return_val;
413 BOOL bRet;
415 while (result->string)
417 return_val = -1;
418 bRet = StrToIntExA(result->string,0,&return_val);
419 ok(!bRet || return_val != -1, "No result returned from '%s'\n",
420 result->string);
421 if (bRet)
422 ok(return_val == result->str_to_int_ex, "converted '%s' wrong (%d)\n",
423 result->string, return_val);
424 result++;
427 result = StrToInt_results;
428 while (result->string)
430 return_val = -1;
431 bRet = StrToIntExA(result->string,STIF_SUPPORT_HEX,&return_val);
432 ok(!bRet || return_val != -1, "No result returned from '%s'\n",
433 result->string);
434 if (bRet)
435 ok(return_val == result->str_to_int_hex, "converted '%s' wrong (%d)\n",
436 result->string, return_val);
437 result++;
441 static void test_StrToIntExW(void)
443 WCHAR szBuff[256];
444 const StrToIntResult *result = StrToInt_results;
445 int return_val;
446 BOOL bRet;
448 while (result->string)
450 return_val = -1;
451 MultiByteToWideChar(0,0,result->string,-1,szBuff,sizeof(szBuff)/sizeof(WCHAR));
452 bRet = StrToIntExW(szBuff, 0, &return_val);
453 ok(!bRet || return_val != -1, "No result returned from '%s'\n",
454 result->string);
455 if (bRet)
456 ok(return_val == result->str_to_int_ex, "converted '%s' wrong (%d)\n",
457 result->string, return_val);
458 result++;
461 result = StrToInt_results;
462 while (result->string)
464 return_val = -1;
465 MultiByteToWideChar(0,0,result->string,-1,szBuff,sizeof(szBuff)/sizeof(WCHAR));
466 bRet = StrToIntExW(szBuff, STIF_SUPPORT_HEX, &return_val);
467 ok(!bRet || return_val != -1, "No result returned from '%s'\n",
468 result->string);
469 if (bRet)
470 ok(return_val == result->str_to_int_hex, "converted '%s' wrong (%d)\n",
471 result->string, return_val);
472 result++;
476 static void test_StrDupA(void)
478 LPSTR lpszStr;
479 const StrFormatSizeResult* result = StrFormatSize_results;
481 while(result->value)
483 lpszStr = StrDupA(result->byte_size_64);
485 ok(lpszStr != NULL, "Dup failed\n");
486 if (lpszStr)
488 ok(!strcmp(result->byte_size_64, lpszStr), "Copied string wrong\n");
489 LocalFree((HLOCAL)lpszStr);
491 result++;
494 /* Later versions of shlwapi return NULL for this, but earlier versions
495 * returned an empty string (as Wine does).
497 lpszStr = StrDupA(NULL);
498 ok(lpszStr == NULL || *lpszStr == '\0', "NULL string returned %p\n", lpszStr);
499 LocalFree((HLOCAL)lpszStr);
502 static void test_StrFormatByteSize64A(void)
504 char szBuff[256];
505 const StrFormatSizeResult* result = StrFormatSize_results;
507 if (!pStrFormatByteSize64A)
509 skip("StrFormatByteSize64A() is not available. Tests skipped\n");
510 return;
513 while(result->value)
515 pStrFormatByteSize64A(result->value, szBuff, 256);
517 ok(!strcmp(result->byte_size_64, szBuff),
518 "Formatted %x%08x wrong: got %s, expected %s\n",
519 (LONG)(result->value >> 32), (LONG)result->value, szBuff, result->byte_size_64);
521 result++;
525 static void test_StrFormatKBSizeW(void)
527 WCHAR szBuffW[256];
528 char szBuff[256];
529 const StrFormatSizeResult* result = StrFormatSize_results;
531 if (!pStrFormatKBSizeW)
533 skip("StrFormatKBSizeW() is not available. Tests skipped\n");
534 return;
537 while(result->value)
539 pStrFormatKBSizeW(result->value, szBuffW, 256);
540 WideCharToMultiByte(0,0,szBuffW,-1,szBuff,sizeof(szBuff)/sizeof(WCHAR),0,0);
541 ok(!strcmp(result->kb_size, szBuff),
542 "Formatted %x%08x wrong: got %s, expected %s\n",
543 (LONG)(result->value >> 32), (LONG)result->value, szBuff, result->kb_size);
544 result++;
548 static void test_StrFormatKBSizeA(void)
550 char szBuff[256];
551 const StrFormatSizeResult* result = StrFormatSize_results;
553 if (!pStrFormatKBSizeA)
555 skip("StrFormatKBSizeA() is not available. Tests skipped\n");
556 return;
559 while(result->value)
561 pStrFormatKBSizeA(result->value, szBuff, 256);
563 ok(!strcmp(result->kb_size, szBuff),
564 "Formatted %x%08x wrong: got %s, expected %s\n",
565 (LONG)(result->value >> 32), (LONG)result->value, szBuff, result->kb_size);
566 result++;
570 static void test_StrFromTimeIntervalA(void)
572 char szBuff[256];
573 const StrFromTimeIntervalResult* result = StrFromTimeInterval_results;
575 while(result->ms)
577 StrFromTimeIntervalA(szBuff, 256, result->ms, result->digits);
579 ok(!strcmp(result->time_interval, szBuff), "Formatted %d %d wrong\n",
580 result->ms, result->digits);
581 result++;
585 static void test_StrCmpA(void)
587 static const char str1[] = {'a','b','c','d','e','f'};
588 static const char str2[] = {'a','B','c','d','e','f'};
589 ok(0 != StrCmpNA(str1, str2, 6), "StrCmpNA is case-insensitive\n");
590 ok(0 == StrCmpNIA(str1, str2, 6), "StrCmpNIA is case-sensitive\n");
591 ok(!ChrCmpIA('a', 'a'), "ChrCmpIA doesn't work at all!\n");
592 ok(!ChrCmpIA('b', 'B'), "ChrCmpIA is not case-insensitive\n");
593 ok(ChrCmpIA('a', 'z'), "ChrCmpIA believes that a == z!\n");
595 if (pStrIsIntlEqualA)
597 ok(pStrIsIntlEqualA(FALSE, str1, str2, 5), "StrIsIntlEqualA(FALSE,...) isn't case-insensitive\n");
598 ok(!pStrIsIntlEqualA(TRUE, str1, str2, 5), "StrIsIntlEqualA(TRUE,...) isn't case-sensitive\n");
600 else
601 skip("StrIsIntlEqualA() is not available. Tests skipped\n");
603 if (pIntlStrEqWorkerA)
605 ok(pIntlStrEqWorkerA(FALSE, str1, str2, 5), "IntlStrEqWorkerA(FALSE,...) isn't case-insensitive\n");
606 ok(!pIntlStrEqWorkerA(TRUE, str1, str2, 5), "pIntlStrEqWorkerA(TRUE,...) isn't case-sensitive\n");
608 else
609 skip("IntlStrEqWorkerA() is not available. Tests skipped\n");
612 static void test_StrCmpW(void)
614 static const WCHAR str1[] = {'a','b','c','d','e','f'};
615 static const WCHAR str2[] = {'a','B','c','d','e','f'};
616 ok(0 != StrCmpNW(str1, str2, 5), "StrCmpNW is case-insensitive\n");
617 ok(0 == StrCmpNIW(str1, str2, 5), "StrCmpNIW is case-sensitive\n");
618 ok(!ChrCmpIW('a', 'a'), "ChrCmpIW doesn't work at all!\n");
619 ok(!ChrCmpIW('b', 'B'), "ChrCmpIW is not case-insensitive\n");
620 ok(ChrCmpIW('a', 'z'), "ChrCmpIW believes that a == z!\n");
622 if (pStrIsIntlEqualW)
624 ok(pStrIsIntlEqualW(FALSE, str1, str2, 5), "StrIsIntlEqualW(FALSE,...) isn't case-insensitive\n");
625 ok(!pStrIsIntlEqualW(TRUE, str1, str2, 5), "StrIsIntlEqualW(TRUE,...) isn't case-sensitive\n");
627 else
628 skip("StrIsIntlEqualW() is not available. Tests skipped\n");
630 if (pIntlStrEqWorkerW)
632 ok(pIntlStrEqWorkerW(FALSE, str1, str2, 5), "IntlStrEqWorkerW(FALSE,...) isn't case-insensitive\n");
633 ok(!pIntlStrEqWorkerW(TRUE, str1, str2, 5), "IntlStrEqWorkerW(TRUE,...) isn't case-sensitive\n");
635 else
636 skip("IntlStrEqWorkerW() is not available. Tests skipped\n");
639 static WCHAR *CoDupStrW(const char* src)
641 INT len = MultiByteToWideChar(CP_ACP, 0, src, -1, NULL, 0);
642 WCHAR* szTemp = CoTaskMemAlloc(len * sizeof(WCHAR));
643 MultiByteToWideChar(CP_ACP, 0, src, -1, szTemp, len);
644 return szTemp;
647 static void test_StrRetToBSTR(void)
649 static const WCHAR szTestW[] = { 'T','e','s','t','\0' };
650 ITEMIDLIST iidl[10];
651 BSTR bstr;
652 STRRET strret;
653 HRESULT ret;
655 if (!pStrRetToBSTR)
657 skip("StrRetToBSTR() is not available. Tests skipped\n");
658 return;
661 strret.uType = STRRET_WSTR;
662 U(strret).pOleStr = CoDupStrW("Test");
663 bstr = 0;
664 ret = pStrRetToBSTR(&strret, NULL, &bstr);
665 ok(ret == S_OK && bstr && !strcmpW(bstr, szTestW),
666 "STRRET_WSTR: dup failed, ret=0x%08x, bstr %p\n", ret, bstr);
667 if (bstr)
668 SysFreeString(bstr);
670 strret.uType = STRRET_CSTR;
671 lstrcpyA(U(strret).cStr, "Test");
672 ret = pStrRetToBSTR(&strret, NULL, &bstr);
673 ok(ret == S_OK && bstr && !strcmpW(bstr, szTestW),
674 "STRRET_CSTR: dup failed, ret=0x%08x, bstr %p\n", ret, bstr);
675 if (bstr)
676 SysFreeString(bstr);
678 strret.uType = STRRET_OFFSET;
679 U(strret).uOffset = 1;
680 strcpy((char*)&iidl, " Test");
681 ret = pStrRetToBSTR(&strret, iidl, &bstr);
682 ok(ret == S_OK && bstr && !strcmpW(bstr, szTestW),
683 "STRRET_OFFSET: dup failed, ret=0x%08x, bstr %p\n", ret, bstr);
684 if (bstr)
685 SysFreeString(bstr);
687 /* Native crashes if str is NULL */
690 static void test_StrCpyNXA(void)
692 LPCSTR lpSrc = "hello";
693 LPSTR lpszRes;
694 char dest[8];
696 if (!pStrCpyNXA)
698 skip("StrCpyNXA() is not available. Tests skipped\n");
699 return;
702 memset(dest, '\n', sizeof(dest));
703 lpszRes = pStrCpyNXA(dest, lpSrc, sizeof(dest)/sizeof(dest[0]));
704 ok(lpszRes == dest + 5 && !memcmp(dest, "hello\0\n\n", sizeof(dest)),
705 "StrCpyNXA: expected %p, \"hello\\0\\n\\n\", got %p, \"%d,%d,%d,%d,%d,%d,%d,%d\"\n",
706 dest + 5, lpszRes, dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
709 static void test_StrCpyNXW(void)
711 static const WCHAR lpInit[] = { '\n','\n','\n','\n','\n','\n','\n','\n' };
712 static const WCHAR lpSrc[] = { 'h','e','l','l','o','\0' };
713 static const WCHAR lpRes[] = { 'h','e','l','l','o','\0','\n','\n' };
714 LPWSTR lpszRes;
715 WCHAR dest[8];
717 if (!pStrCpyNXW)
719 skip("StrCpyNXW() is not available. Tests skipped\n");
720 return;
723 memcpy(dest, lpInit, sizeof(lpInit));
724 lpszRes = pStrCpyNXW(dest, lpSrc, sizeof(dest)/sizeof(dest[0]));
725 ok(lpszRes == dest + 5 && !memcmp(dest, lpRes, sizeof(dest)),
726 "StrCpyNXW: expected %p, \"hello\\0\\n\\n\", got %p, \"%d,%d,%d,%d,%d,%d,%d,%d\"\n",
727 dest + 5, lpszRes, dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
730 #define check_strrstri(type, str, pos, needle, exp) \
731 ret##type = StrRStrI##type(str, str+pos, needle); \
732 ok(ret##type == (exp), "Type " #type ", expected %p but got %p (string base %p)\n", \
733 (exp), ret##type, str);
735 static void test_StrRStrI(void)
737 static const CHAR szTest[] = "yAxxxxAy";
738 static const CHAR szTest2[] = "ABABABAB";
739 static const WCHAR wszTest[] = {'y','A','x','x','x','x','A','y',0};
740 static const WCHAR wszTest2[] = {'A','B','A','B','A','B','A','B',0};
742 static const WCHAR wszPattern1[] = {'A',0};
743 static const WCHAR wszPattern2[] = {'a','X',0};
744 static const WCHAR wszPattern3[] = {'A','y',0};
745 static const WCHAR wszPattern4[] = {'a','b',0};
746 LPWSTR retW;
747 LPSTR retA;
749 check_strrstri(A, szTest, 4, "A", szTest+1);
750 check_strrstri(A, szTest, 4, "aX", szTest+1);
751 check_strrstri(A, szTest, 4, "Ay", NULL);
752 check_strrstri(W, wszTest, 4, wszPattern1, wszTest+1);
753 check_strrstri(W, wszTest, 4, wszPattern2, wszTest+1);
754 check_strrstri(W, wszTest, 4, wszPattern3, NULL);
756 check_strrstri(A, szTest2, 4, "ab", szTest2+2);
757 check_strrstri(A, szTest2, 3, "ab", szTest2+2);
758 check_strrstri(A, szTest2, 2, "ab", szTest2);
759 check_strrstri(A, szTest2, 1, "ab", szTest2);
760 check_strrstri(A, szTest2, 0, "ab", NULL);
761 check_strrstri(W, wszTest2, 4, wszPattern4, wszTest2+2);
762 check_strrstri(W, wszTest2, 3, wszPattern4, wszTest2+2);
763 check_strrstri(W, wszTest2, 2, wszPattern4, wszTest2);
764 check_strrstri(W, wszTest2, 1, wszPattern4, wszTest2);
765 check_strrstri(W, wszTest2, 0, wszPattern4, NULL);
769 static void test_SHAnsiToAnsi(void)
771 char dest[8];
772 DWORD dwRet;
774 if (!pSHAnsiToAnsi)
776 skip("SHAnsiToAnsi() is not available. Tests skipped\n");
777 return;
780 memset(dest, '\n', sizeof(dest));
781 dwRet = pSHAnsiToAnsi("hello", dest, sizeof(dest)/sizeof(dest[0]));
782 ok(dwRet == 6 && !memcmp(dest, "hello\0\n\n", sizeof(dest)),
783 "SHAnsiToAnsi: expected 6, \"hello\\0\\n\\n\", got %d, \"%d,%d,%d,%d,%d,%d,%d,%d\"\n",
784 dwRet, dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
787 static void test_SHUnicodeToUnicode(void)
789 static const WCHAR lpInit[] = { '\n','\n','\n','\n','\n','\n','\n','\n' };
790 static const WCHAR lpSrc[] = { 'h','e','l','l','o','\0' };
791 static const WCHAR lpRes[] = { 'h','e','l','l','o','\0','\n','\n' };
792 WCHAR dest[8];
793 DWORD dwRet;
795 if (!pSHUnicodeToUnicode)
797 skip("SHUnicodeToUnicode() is not available. Tests skipped\n");
798 return;
801 memcpy(dest, lpInit, sizeof(lpInit));
802 dwRet = pSHUnicodeToUnicode(lpSrc, dest, sizeof(dest)/sizeof(dest[0]));
803 ok(dwRet == 6 && !memcmp(dest, lpRes, sizeof(dest)),
804 "SHUnicodeToUnicode: expected 6, \"hello\\0\\n\\n\", got %d, \"%d,%d,%d,%d,%d,%d,%d,%d\"\n",
805 dwRet, dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
808 static void test_StrXXX_overflows(void)
810 CHAR str1[2*MAX_PATH+1], buf[2*MAX_PATH];
811 WCHAR wstr1[2*MAX_PATH+1], wbuf[2*MAX_PATH];
812 const WCHAR fmt[] = {'%','s',0};
813 STRRET strret;
814 int ret;
815 int i;
817 for (i=0; i<2*MAX_PATH; i++)
819 str1[i] = '0'+(i%10);
820 wstr1[i] = '0'+(i%10);
822 str1[2*MAX_PATH] = 0;
823 wstr1[2*MAX_PATH] = 0;
825 memset(buf, 0xbf, sizeof(buf));
826 expect_eq(StrCpyNA(buf, str1, 10), buf, PCHAR, "%p");
827 expect_eq(buf[9], 0, CHAR, "%x");
828 expect_eq(buf[10], '\xbf', CHAR, "%x");
830 if (pStrCatBuffA)
832 expect_eq(pStrCatBuffA(buf, str1, 100), buf, PCHAR, "%p");
833 expect_eq(buf[99], 0, CHAR, "%x");
834 expect_eq(buf[100], '\xbf', CHAR, "%x");
836 else
837 skip("StrCatBuffA() is not available. Tests skipped\n");
839 memset(wbuf, 0xbf, sizeof(wbuf));
840 expect_eq(StrCpyNW(wbuf, wstr1, 10), wbuf, PWCHAR, "%p");
841 expect_eq(wbuf[9], 0, WCHAR, "%x");
842 expect_eq(wbuf[10], (WCHAR)0xbfbf, WCHAR, "%x");
844 if (pStrCatBuffW)
846 expect_eq(pStrCatBuffW(wbuf, wstr1, 100), wbuf, PWCHAR, "%p");
847 expect_eq(wbuf[99], 0, WCHAR, "%x");
848 expect_eq(wbuf[100], (WCHAR)0xbfbf, WCHAR, "%x");
850 else
851 skip("StrCatBuffW() is not available. Tests skipped\n");
853 if (pStrRetToBufW)
855 memset(wbuf, 0xbf, sizeof(wbuf));
856 strret.uType = STRRET_WSTR;
857 U(strret).pOleStr = StrDupW(wstr1);
858 expect_eq2(pStrRetToBufW(&strret, NULL, wbuf, 10), S_OK, HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER) /* Vista */, HRESULT, "%x");
859 expect_eq(wbuf[9], 0, WCHAR, "%x");
860 expect_eq(wbuf[10], (WCHAR)0xbfbf, WCHAR, "%x");
862 else
863 skip("StrRetToBufW() is not available. Tests skipped\n");
865 if (pStrRetToBufA)
867 memset(buf, 0xbf, sizeof(buf));
868 strret.uType = STRRET_CSTR;
869 StrCpyN(U(strret).cStr, str1, MAX_PATH);
870 expect_eq2(pStrRetToBufA(&strret, NULL, buf, 10), S_OK, HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER) /* Vista */, HRESULT, "%x");
871 expect_eq(buf[9], 0, CHAR, "%x");
872 expect_eq(buf[10], (CHAR)0xbf, CHAR, "%x");
874 else
875 skip("StrRetToBufA() is not available. Tests skipped\n");
877 if (pwnsprintfA)
879 memset(buf, 0xbf, sizeof(buf));
880 ret = pwnsprintfA(buf, 10, "%s", str1);
881 ok(broken(ret == 9) || ret == -1 /* Vista */, "Unexpected wsnprintfA return %d, expected 9 or -1\n", ret);
882 expect_eq(buf[9], 0, CHAR, "%x");
883 expect_eq(buf[10], (CHAR)0xbf, CHAR, "%x");
885 else
886 skip("wnsprintfA() is not available. Tests skipped\n");
888 if (pwnsprintfW)
890 memset(wbuf, 0xbf, sizeof(wbuf));
891 ret = pwnsprintfW(wbuf, 10, fmt, wstr1);
892 ok(broken(ret == 9) || ret == -1 /* Vista */, "Unexpected wsnprintfW return %d, expected 9 or -1\n", ret);
893 expect_eq(wbuf[9], 0, WCHAR, "%x");
894 expect_eq(wbuf[10], (WCHAR)0xbfbf, WCHAR, "%x");
896 else
897 skip("wnsprintfW() is not available. Tests skipped\n");
900 START_TEST(string)
902 HMODULE hShlwapi;
903 TCHAR thousandDelim[8];
904 TCHAR decimalDelim[8];
905 CoInitialize(0);
907 GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, thousandDelim, 8);
908 GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, decimalDelim, 8);
910 hShlwapi = GetModuleHandleA("shlwapi");
911 pIntlStrEqWorkerA = (void *)GetProcAddress(hShlwapi, "IntlStrEqWorkerA");
912 pIntlStrEqWorkerW = (void *)GetProcAddress(hShlwapi, "IntlStrEqWorkerW");
913 pSHAnsiToAnsi = (void *)GetProcAddress(hShlwapi, (LPSTR)345);
914 pSHUnicodeToUnicode = (void *)GetProcAddress(hShlwapi, (LPSTR)346);
915 pStrCatBuffA = (void *)GetProcAddress(hShlwapi, "StrCatBuffA");
916 pStrCatBuffW = (void *)GetProcAddress(hShlwapi, "StrCatBuffW");
917 pStrCpyNXA = (void *)GetProcAddress(hShlwapi, (LPSTR)399);
918 pStrCpyNXW = (void *)GetProcAddress(hShlwapi, (LPSTR)400);
919 pStrFormatByteSize64A = (void *)GetProcAddress(hShlwapi, "StrFormatByteSize64A");
920 pStrFormatKBSizeA = (void *)GetProcAddress(hShlwapi, "StrFormatKBSizeA");
921 pStrFormatKBSizeW = (void *)GetProcAddress(hShlwapi, "StrFormatKBSizeW");
922 pStrIsIntlEqualA = (void *)GetProcAddress(hShlwapi, "StrIsIntlEqualA");
923 pStrIsIntlEqualW = (void *)GetProcAddress(hShlwapi, "StrIsIntlEqualW");
924 pStrRetToBSTR = (void *)GetProcAddress(hShlwapi, "StrRetToBSTR");
925 pStrRetToBufA = (void *)GetProcAddress(hShlwapi, "StrRetToBufA");
926 pStrRetToBufW = (void *)GetProcAddress(hShlwapi, "StrRetToBufW");
927 pwnsprintfA = (void *)GetProcAddress(hShlwapi, "wnsprintfA");
928 pwnsprintfW = (void *)GetProcAddress(hShlwapi, "wnsprintfW");
930 test_StrChrA();
931 test_StrChrW();
932 test_StrChrIA();
933 test_StrChrIW();
934 test_StrRChrA();
935 test_StrRChrW();
936 test_StrCpyW();
937 test_StrToIntA();
938 test_StrToIntW();
939 test_StrToIntExA();
940 test_StrToIntExW();
941 test_StrDupA();
942 if (lstrcmp(thousandDelim, ",")==0 && lstrcmp(decimalDelim, ".")==0)
944 /* these tests are locale-dependent */
945 test_StrFormatByteSize64A();
946 test_StrFormatKBSizeA();
947 test_StrFormatKBSizeW();
950 /* language-dependent test */
951 if (PRIMARYLANGID(GetUserDefaultLangID()) != LANG_ENGLISH)
952 trace("Skipping StrFromTimeInterval test for non English language\n");
953 else
954 test_StrFromTimeIntervalA();
956 test_StrCmpA();
957 test_StrCmpW();
958 test_StrRetToBSTR();
959 test_StrCpyNXA();
960 test_StrCpyNXW();
961 test_StrRStrI();
962 test_SHAnsiToAnsi();
963 test_SHUnicodeToUnicode();
964 test_StrXXX_overflows();
966 CoUninitialize();