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