Porting fixes.
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 #include <stdlib.h>
21 #include <stdio.h>
22
23 #include "wine/test.h"
24 #include "winbase.h"
25 #include "winerror.h"
26 #include "winnls.h"
27 #define NO_SHLWAPI_REG
28 #define NO_SHLWAPI_PATH
29 #define NO_SHLWAPI_GDI
30 #define NO_SHLWAPI_STREAM
31 #include "shlwapi.h"
32
33 /* StrToInt/StrToIntEx results */
34 typedef struct tagStrToIntResult
35 {
36   const char* string;
37   int str_to_int;
38   int str_to_int_ex;
39   int str_to_int_hex;
40 } StrToIntResult;
41
42 static const StrToIntResult StrToInt_results[] = {
43      { "1099", 1099, 1099, 1099 },
44      { "+88987", 0, 88987, 88987 },
45      { "012", 12, 12, 12 },
46      { "-55", -55, -55, -55 },
47      { "-0", 0, 0, 0 },
48      { "0x44ff", 0, 0, 0x44ff },
49      { "+0x44f4", 0, 0, 0x44f4 },
50      { "-0x44fd", 0, 0, 0x44fd },
51      { "+ 88987", 0, 0, 0 },
52      { "- 55", 0, 0, 0 },
53      { "- 0", 0, 0, 0 },
54      { "+ 0x44f4", 0, 0, 0 },
55      { "--0x44fd", 0, 0, 0 },
56      { " 1999", 0, 1999, 1999 },
57      { " +88987", 0, 88987, 88987 },
58      { " 012", 0, 12, 12 },
59      { " -55", 0, -55, -55 },
60      { " 0x44ff", 0, 0, 0x44ff },
61      { " +0x44f4", 0, 0, 0x44f4 },
62      { " -0x44fd", 0, 0, 0x44fd },
63      { NULL, 0, 0, 0 }
64 };
65
66 /* pStrFormatByteSize64/StrFormatKBSize results */
67 typedef struct tagStrFormatSizeResult
68 {
69   LONGLONG value;
70   const char* byte_size_64;
71   const char* kb_size;
72 } StrFormatSizeResult;
73
74
75 static const StrFormatSizeResult StrFormatSize_results[] = {
76   { -1023, "-1023 bytes", "0 KB"},
77   { -24, "-24 bytes", "0 KB"},
78   { 309, "309 bytes", "1 KB"},
79   { 10191, "9.95 KB", "10 KB"},
80   { 100353, "98.0 KB", "99 KB"},
81   { 1022286, "998 KB", "999 KB"},
82   { 1046862, "0.99 MB", "1,023 KB"},
83   { 1048574619, "999 MB", "1,023,999 KB"},
84   { 1073741775, "0.99 GB", "1,048,576 KB"},
85   { ((LONGLONG)0x000000f9 << 32) | 0xfffff94e, "999 GB", "1,048,575,999 KB"},
86   { ((LONGLONG)0x000000ff << 32) | 0xfffffa9b, "0.99 TB", "1,073,741,823 KB"},
87   { ((LONGLONG)0x0003e7ff << 32) | 0xfffffa9b, "999 TB", "1,073,741,823,999 KB"},
88   { ((LONGLONG)0x0003ffff << 32) | 0xfffffbe8, "0.99 PB", "1,099,511,627,775 KB"},
89   { ((LONGLONG)0x0f9fffff << 32) | 0xfffffd35, "999 PB", "1,099,511,627,776,000 KB"},
90   { ((LONGLONG)0x0fffffff << 32) | 0xfffffa9b, "0.99 EB", "1,125,899,906,842,623 KB"},
91   { 0, NULL, NULL }
92 };
93
94 /* StrFormatByteSize64/StrFormatKBSize results */
95 typedef struct tagStrFromTimeIntervalResult
96 {
97   DWORD ms;
98   int   digits;
99   const char* time_interval;
100 } StrFromTimeIntervalResult;
101
102
103 static const StrFromTimeIntervalResult StrFromTimeInterval_results[] = {
104   { 1, 1, " 0 sec" },
105   { 1, 2, " 0 sec" },
106   { 1, 3, " 0 sec" },
107   { 1, 4, " 0 sec" },
108   { 1, 5, " 0 sec" },
109   { 1, 6, " 0 sec" },
110   { 1, 7, " 0 sec" },
111
112   { 1000000, 1, " 10 min" },
113   { 1000000, 2, " 16 min" },
114   { 1000000, 3, " 16 min 40 sec" },
115   { 1000000, 4, " 16 min 40 sec" },
116   { 1000000, 5, " 16 min 40 sec" },
117   { 1000000, 6, " 16 min 40 sec" },
118   { 1000000, 7, " 16 min 40 sec" },
119
120   { 1999999, 1, " 30 min" },
121   { 1999999, 2, " 33 min" },
122   { 1999999, 3, " 33 min 20 sec" },
123   { 1999999, 4, " 33 min 20 sec" },
124   { 1999999, 5, " 33 min 20 sec" },
125   { 1999999, 6, " 33 min 20 sec" },
126   { 1999999, 7, " 33 min 20 sec" },
127
128   { 3999997, 1, " 1 hr" },
129   { 3999997, 2, " 1 hr 6 min" },
130   { 3999997, 3, " 1 hr 6 min 40 sec" },
131   { 3999997, 4, " 1 hr 6 min 40 sec" },
132   { 3999997, 5, " 1 hr 6 min 40 sec" },
133   { 3999997, 6, " 1 hr 6 min 40 sec" },
134   { 3999997, 7, " 1 hr 6 min 40 sec" },
135
136   { 149999851, 7, " 41 hr 40 min 0 sec" },
137   { 150999850, 1, " 40 hr" },
138   { 150999850, 2, " 41 hr" },
139   { 150999850, 3, " 41 hr 50 min" },
140   { 150999850, 4, " 41 hr 56 min" },
141   { 150999850, 5, " 41 hr 56 min 40 sec" },
142   { 150999850, 6, " 41 hr 56 min 40 sec" },
143   { 150999850, 7, " 41 hr 56 min 40 sec" },
144
145   { 493999507, 1, " 100 hr" },
146   { 493999507, 2, " 130 hr" },
147   { 493999507, 3, " 137 hr" },
148   { 493999507, 4, " 137 hr 10 min" },
149   { 493999507, 5, " 137 hr 13 min" },
150   { 493999507, 6, " 137 hr 13 min 20 sec" },
151   { 493999507, 7, " 137 hr 13 min 20 sec" },
152
153   { 0, 0, NULL }
154 };
155
156 static void test_StrChrA(void)
157 {
158   char string[129];
159   WORD count;
160
161   /* this test crashes on win2k SP4 */
162   /*ok(!StrChrA(NULL,'\0'), "found a character in a NULL string!");*/
163
164   for (count = 32; count < 128; count++)
165     string[count] = (char)count;
166   string[128] = '\0';
167
168   for (count = 32; count < 128; count++)
169   {
170     LPSTR result = StrChrA(string+32, count);
171     ok(result - string == count,
172         "found char '%c' in wrong place: got %d, expected %d\n",
173         count, result - string, count);
174   }
175
176   for (count = 32; count < 128; count++)
177   {
178     LPSTR result = StrChrA(string+count+1, count);
179     ok(!result, "found char '%c' not in the string\n", count);
180   }
181 }
182
183 static void test_StrChrW(void)
184 {
185   WCHAR string[16385];
186   WORD count;
187
188   /* this test crashes on win2k SP4 */
189   /*ok(!StrChrW(NULL,'\0'), "found a character in a NULL string!");*/
190
191   for (count = 32; count < 16384; count++)
192     string[count] = count;
193   string[16384] = '\0';
194
195   for (count = 32; count < 16384; count++)
196   {
197     LPWSTR result = StrChrW(string+32, count);
198     ok((result - string) == count, "found char %d in wrong place", count);
199   }
200
201   for (count = 32; count < 16384; count++)
202   {
203     LPWSTR result = StrChrW(string+count+1, count);
204     ok(!result, "found char not in the string");
205   }
206 }
207
208 static void test_StrChrIA(void)
209 {
210   char string[129];
211   WORD count;
212
213   /* this test crashes on win2k SP4 */
214   /*ok(!StrChrIA(NULL,'\0'), "found a character in a NULL string!");*/
215
216   for (count = 32; count < 128; count++)
217     string[count] = (char)count;
218   string[128] = '\0';
219
220   for (count = 'A'; count <= 'X'; count++)
221   {
222     LPSTR result = StrChrIA(string+32, count);
223
224     ok(result - string == count, "found char '%c' in wrong place", count);
225     ok(StrChrIA(result, count)!=NULL, "didn't find lowercase '%c'", count);
226   }
227
228   for (count = 'a'; count < 'z'; count++)
229   {
230     LPSTR result = StrChrIA(string+count+1, count);
231     ok(!result, "found char not in the string");
232   }
233 }
234
235 static void test_StrChrIW(void)
236 {
237   WCHAR string[129];
238   WORD count;
239
240   /* this test crashes on win2k SP4 */
241   /*ok(!StrChrIA(NULL,'\0'), "found a character in a NULL string!");*/
242
243   for (count = 32; count < 128; count++)
244     string[count] = count;
245   string[128] = '\0';
246
247   for (count = 'A'; count <= 'X'; count++)
248   {
249     LPWSTR result = StrChrIW(string+32, count);
250
251     ok(result - string == count, "found char '%c' in wrong place", count);
252     ok(StrChrIW(result, count)!=NULL, "didn't find lowercase '%c'", count);
253   }
254
255   for (count = 'a'; count < 'z'; count++)
256   {
257     LPWSTR result = StrChrIW(string+count+1, count);
258     ok(!result, "found char not in the string");
259   }
260 }
261
262 static void test_StrRChrA(void)
263 {
264   char string[129];
265   WORD count;
266
267   /* this test crashes on win2k SP4 */
268   /*ok(!StrRChrA(NULL, NULL,'\0'), "found a character in a NULL string!");*/
269
270   for (count = 32; count < 128; count++)
271     string[count] = (char)count;
272   string[128] = '\0';
273
274   for (count = 32; count < 128; count++)
275   {
276     LPSTR result = StrRChrA(string+32, NULL, count);
277     ok(result - string == count, "found char %d in wrong place", count);
278   }
279
280   for (count = 32; count < 128; count++)
281   {
282     LPSTR result = StrRChrA(string+count+1, NULL, count);
283     ok(!result, "found char not in the string");
284   }
285
286   for (count = 32; count < 128; count++)
287   {
288     LPSTR result = StrRChrA(string+count+1, string + 127, count);
289     ok(!result, "found char not in the string");
290   }
291 }
292
293 static void test_StrRChrW(void)
294 {
295   WCHAR string[129];
296   WORD count;
297
298   /* this test crashes on win2k SP4 */
299   /*ok(!StrRChrW(NULL, NULL,'\0'), "found a character in a NULL string!");*/
300
301   for (count = 32; count < 128; count++)
302     string[count] = count;
303   string[128] = '\0';
304
305   for (count = 32; count < 128; count++)
306   {
307     LPWSTR result = StrRChrW(string+32, NULL, count);
308     ok(result - string == count,
309         "found char %d in wrong place: got %d, expected %d\n",
310         count, result - string, count);
311   }
312
313   for (count = 32; count < 128; count++)
314   {
315     LPWSTR result = StrRChrW(string+count+1, NULL, count);
316     ok(!result, "found char %d not in the string\n", count);
317   }
318
319   for (count = 32; count < 128; count++)
320   {
321     LPWSTR result = StrRChrW(string+count+1, string + 127, count);
322     ok(!result, "found char %d not in the string\n", count);
323   }
324 }
325
326 static void test_StrCpyW(void)
327 {
328   WCHAR szSrc[256];
329   WCHAR szBuff[256];
330   const StrFormatSizeResult* result = StrFormatSize_results;
331
332
333   while(result->value)
334   {
335     MultiByteToWideChar(0,0,result->byte_size_64,-1,szSrc,sizeof(szSrc)/sizeof(WCHAR));
336
337     StrCpyW(szBuff, szSrc);
338     ok(!StrCmpW(szSrc, szBuff), "Copied string %s wrong", result->byte_size_64);
339     result++;
340   }
341 }
342
343
344 static void test_StrToIntA(void)
345 {
346   const StrToIntResult *result = StrToInt_results;
347   int return_val;
348
349   while (result->string)
350   {
351     return_val = StrToIntA(result->string);
352     ok(return_val == result->str_to_int, "converted '%s' wrong (%d)",
353        result->string, return_val);
354     result++;
355   }
356 }
357
358 static void test_StrToIntW(void)
359 {
360   WCHAR szBuff[256];
361   const StrToIntResult *result = StrToInt_results;
362   int return_val;
363
364   while (result->string)
365   {
366     MultiByteToWideChar(0,0,result->string,-1,szBuff,sizeof(szBuff)/sizeof(WCHAR));
367     return_val = StrToIntW(szBuff);
368     ok(return_val == result->str_to_int, "converted '%s' wrong (%d)",
369        result->string, return_val);
370     result++;
371   }
372 }
373
374 static void test_StrToIntExA(void)
375 {
376   const StrToIntResult *result = StrToInt_results;
377   int return_val;
378   BOOL bRet;
379
380   while (result->string)
381   {
382     return_val = -1;
383     bRet = StrToIntExA(result->string,0,&return_val);
384     ok(!bRet || return_val != -1, "No result returned from '%s'",
385        result->string);
386     if (bRet)
387       ok(return_val == result->str_to_int_ex, "converted '%s' wrong (%d)",
388          result->string, return_val);
389     result++;
390   }
391
392   result = StrToInt_results;
393   while (result->string)
394   {
395     return_val = -1;
396     bRet = StrToIntExA(result->string,STIF_SUPPORT_HEX,&return_val);
397     ok(!bRet || return_val != -1, "No result returned from '%s'",
398        result->string);
399     if (bRet)
400       ok(return_val == result->str_to_int_hex, "converted '%s' wrong (%d)",
401          result->string, return_val);
402     result++;
403   }
404 }
405
406 static void test_StrToIntExW(void)
407 {
408   WCHAR szBuff[256];
409   const StrToIntResult *result = StrToInt_results;
410   int return_val;
411   BOOL bRet;
412
413   while (result->string)
414   {
415     return_val = -1;
416     MultiByteToWideChar(0,0,result->string,-1,szBuff,sizeof(szBuff)/sizeof(WCHAR));
417     bRet = StrToIntExW(szBuff, 0, &return_val);
418     ok(!bRet || return_val != -1, "No result returned from '%s'",
419        result->string);
420     if (bRet)
421       ok(return_val == result->str_to_int_ex, "converted '%s' wrong (%d)",
422          result->string, return_val);
423     result++;
424   }
425
426   result = StrToInt_results;
427   while (result->string)
428   {
429     return_val = -1;
430     MultiByteToWideChar(0,0,result->string,-1,szBuff,sizeof(szBuff)/sizeof(WCHAR));
431     bRet = StrToIntExW(szBuff, STIF_SUPPORT_HEX, &return_val);
432     ok(!bRet || return_val != -1, "No result returned from '%s'",
433        result->string);
434     if (bRet)
435       ok(return_val == result->str_to_int_hex, "converted '%s' wrong (%d)",
436          result->string, return_val);
437     result++;
438   }
439 }
440
441 static void test_StrDupA()
442 {
443   LPSTR lpszStr;
444   const StrFormatSizeResult* result = StrFormatSize_results;
445
446   while(result->value)
447   {
448     lpszStr = StrDupA(result->byte_size_64);
449
450     ok(lpszStr != NULL, "Dup failed");
451     if (lpszStr)
452     {
453       ok(!strcmp(result->byte_size_64, lpszStr), "Copied string wrong");
454       LocalFree((HLOCAL)lpszStr);
455     }
456     result++;
457   }
458
459   /* Later versions of shlwapi return NULL for this, but earlier versions
460    * returned an empty string (as Wine does).
461    */
462   lpszStr = StrDupA(NULL);
463   ok(lpszStr == NULL || *lpszStr == '\0', "NULL string returned %p", lpszStr);
464 }
465
466 static void test_StrFormatByteSize64A(void)
467 {
468 /* this test fails on locales which do not use '.' as a decimal separator */
469 #if 0
470   char szBuff[256];
471   const StrFormatSizeResult* result = StrFormatSize_results;
472
473   while(result->value)
474   {
475     StrFormatByteSize64A(result->value, szBuff, 256);
476
477     ok(!strcmp(result->byte_size_64, szBuff),
478         "Formatted %lx%08lx wrong: got %s, expected %s\n",
479        (LONG)(result->value >> 32), (LONG)result->value, szBuff, result->byte_size_64);
480
481     result++;
482   }
483 #endif
484 }
485
486 static void test_StrFormatKBSizeW(void)
487 {
488 /* FIXME: Awaiting NLS fixes in kernel before these succeed */
489 #if 0
490   WCHAR szBuffW[256];
491   char szBuff[256];
492   const StrFormatSizeResult* result = StrFormatSize_results;
493
494   while(result->value)
495   {
496     StrFormatKBSizeW(result->value, szBuffW, 256);
497     WideCharToMultiByte(0,0,szBuffW,-1,szBuff,sizeof(szBuff)/sizeof(WCHAR),0,0);
498     ok(!strcmp(result->kb_size, szBuff),
499         "Formatted %lx%08lx wrong: got %s, expected %s\n",
500        (LONG)(result->value >> 32), (LONG)result->value, szBuff, result->kb_size);
501     result++;
502   }
503 #endif
504 }
505
506 static void test_StrFormatKBSizeA(void)
507 {
508 /* this test fails on locales which do not use '.' as a decimal separator */
509 #if 0
510   char szBuff[256];
511   const StrFormatSizeResult* result = StrFormatSize_results;
512
513   while(result->value)
514   {
515     StrFormatKBSizeA(result->value, szBuff, 256);
516
517     ok(!strcmp(result->kb_size, szBuff),
518         "Formatted %lx%08lx wrong: got %s, expected %s\n",
519        (LONG)(result->value >> 32), (LONG)result->value, szBuff, result->kb_size);
520     result++;
521   }
522 #endif
523 }
524
525 void test_StrFromTimeIntervalA(void)
526 {
527   char szBuff[256];
528   const StrFromTimeIntervalResult* result = StrFromTimeInterval_results;
529
530   while(result->ms)
531   {
532     StrFromTimeIntervalA(szBuff, 256, result->ms, result->digits);
533
534     ok(!strcmp(result->time_interval, szBuff), "Formatted %ld %d wrong",
535        result->ms, result->digits);
536     result++;
537   }
538 }
539
540 START_TEST(string)
541 {
542   test_StrChrA();
543   test_StrChrW();
544   test_StrChrIA();
545   test_StrChrIW();
546   test_StrRChrA();
547   test_StrRChrW();
548   test_StrCpyW();
549   test_StrToIntA();
550   test_StrToIntW();
551   test_StrToIntExA();
552   test_StrToIntExW();
553   test_StrDupA();
554   test_StrFormatByteSize64A();
555   test_StrFormatKBSizeA();
556   test_StrFormatKBSizeW();
557   test_StrFromTimeIntervalA();
558 }