If using the default values, also set dwType to REG_SZ as our default
[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   int count;
160
161   ok(!StrChrA(NULL,'\0'), "found a character in a NULL string!");
162
163   for (count = 32; count < 128; count++)
164     string[count] = count;
165   string[128] = '\0';
166
167   for (count = 32; count < 128; count++)
168   {
169     LPSTR result = StrChrA(string+32, count);
170     ok(result - string == count, "found char %d in wrong place", count);
171   }
172
173   for (count = 32; count < 128; count++)
174   {
175     LPSTR result = StrChrA(string+count+1, count);
176     ok(!result, "found char not in the string");
177   }
178 }
179
180 static void test_StrChrW(void)
181 {
182   WCHAR string[16385];
183   int count;
184
185   ok(!StrChrW(NULL,'\0'), "found a character in a NULL string!");
186
187   for (count = 32; count < 16384; count++)
188     string[count] = count;
189   string[16384] = '\0';
190
191   for (count = 32; count < 16384; count++)
192   {
193     LPWSTR result = StrChrW(string+32, count);
194     ok((result - string) == count, "found char %d in wrong place", count);
195   }
196
197   for (count = 32; count < 16384; count++)
198   {
199     LPWSTR result = StrChrW(string+count+1, count);
200     ok(!result, "found char not in the string");
201   }
202 }
203
204 static void test_StrChrIA(void)
205 {
206   char string[129];
207   int count;
208
209   ok(!StrChrIA(NULL,'\0'), "found a character in a NULL string!");
210
211   for (count = 32; count < 128; count++)
212     string[count] = count;
213   string[128] = '\0';
214
215   for (count = 'A'; count <= 'X'; count++)
216   {
217     LPSTR result = StrChrIA(string+32, count);
218
219     ok(result - string == count, "found char '%c' in wrong place", count);
220     ok(StrChrIA(result, count)!=NULL, "didn't find lowercase '%c'", count);
221   }
222
223   for (count = 'a'; count < 'z'; count++)
224   {
225     LPSTR result = StrChrIA(string+count+1, count);
226     ok(!result, "found char not in the string");
227   }
228 }
229
230 static void test_StrChrIW(void)
231 {
232   WCHAR string[129];
233   int count;
234
235   ok(!StrChrIA(NULL,'\0'), "found a character in a NULL string!");
236
237   for (count = 32; count < 128; count++)
238     string[count] = count;
239   string[128] = '\0';
240
241   for (count = 'A'; count <= 'X'; count++)
242   {
243     LPWSTR result = StrChrIW(string+32, count);
244
245     ok(result - string == count, "found char '%c' in wrong place", count);
246     ok(StrChrIW(result, count)!=NULL, "didn't find lowercase '%c'", count);
247   }
248
249   for (count = 'a'; count < 'z'; count++)
250   {
251     LPWSTR result = StrChrIW(string+count+1, count);
252     ok(!result, "found char not in the string");
253   }
254 }
255
256 static void test_StrRChrA(void)
257 {
258   char string[129];
259   int count;
260
261   ok(!StrRChrA(NULL, NULL,'\0'), "found a character in a NULL string!");
262
263   for (count = 32; count < 128; count++)
264     string[count] = count;
265   string[128] = '\0';
266
267   for (count = 32; count < 128; count++)
268   {
269     LPSTR result = StrRChrA(string+32, NULL, count);
270     ok(result - string == count, "found char %d in wrong place", count);
271   }
272
273   for (count = 32; count < 128; count++)
274   {
275     LPSTR result = StrRChrA(string+count+1, NULL, count);
276     ok(!result, "found char not in the string");
277   }
278
279   for (count = 32; count < 128; count++)
280   {
281     LPSTR result = StrRChrA(string+count+1, string + 127, count);
282     ok(!result, "found char not in the string");
283   }
284 }
285
286 static void test_StrRChrW(void)
287 {
288   WCHAR string[16385];
289   int count;
290
291   ok(!StrRChrW(NULL, NULL,'\0'), "found a character in a NULL string!");
292
293   for (count = 32; count < 16384; count++)
294     string[count] = count;
295   string[16384] = '\0';
296
297   for (count = 32; count < 16384; count++)
298   {
299     LPWSTR result = StrRChrW(string+32, NULL, count);
300     ok(result - string == count, "found char %d in wrong place", count);
301   }
302
303   for (count = 32; count < 16384; count++)
304   {
305     LPWSTR result = StrRChrW(string+count+1, NULL, count);
306     ok(!result, "found char not in the string");
307   }
308
309   for (count = 32; count < 16384; count++)
310   {
311     LPWSTR result = StrRChrW(string+count+1, string + 127, count);
312     ok(!result, "found char not in the string");
313   }
314 }
315
316 static void test_StrCpyW(void)
317 {
318   WCHAR szSrc[256];
319   WCHAR szBuff[256];
320   const StrFormatSizeResult* result = StrFormatSize_results;
321
322
323   while(result->value)
324   {
325     MultiByteToWideChar(0,0,result->byte_size_64,-1,szSrc,sizeof(szSrc)/sizeof(WCHAR));
326
327     StrCpyW(szBuff, szSrc);
328     ok(!StrCmpW(szSrc, szBuff), "Copied string %s wrong", result->byte_size_64);
329     result++;
330   }
331 }
332
333
334 static void test_StrToIntA(void)
335 {
336   const StrToIntResult *result = StrToInt_results;
337   int return_val;
338
339   while (result->string)
340   {
341     return_val = StrToIntA(result->string);
342     ok(return_val == result->str_to_int, "converted '%s' wrong (%d)",
343        result->string, return_val);
344     result++;
345   }
346 }
347
348 static void test_StrToIntW(void)
349 {
350   WCHAR szBuff[256];
351   const StrToIntResult *result = StrToInt_results;
352   int return_val;
353
354   while (result->string)
355   {
356     MultiByteToWideChar(0,0,result->string,-1,szBuff,sizeof(szBuff)/sizeof(WCHAR));
357     return_val = StrToIntW(szBuff);
358     ok(return_val == result->str_to_int, "converted '%s' wrong (%d)",
359        result->string, return_val);
360     result++;
361   }
362 }
363
364 static void test_StrToIntExA(void)
365 {
366   const StrToIntResult *result = StrToInt_results;
367   int return_val;
368   BOOL bRet;
369
370   while (result->string)
371   {
372     return_val = -1;
373     bRet = StrToIntExA(result->string,0,&return_val);
374     ok(!bRet || return_val != -1, "No result returned from '%s'",
375        result->string);
376     if (bRet)
377       ok(return_val == result->str_to_int_ex, "converted '%s' wrong (%d)",
378          result->string, return_val);
379     result++;
380   }
381
382   result = StrToInt_results;
383   while (result->string)
384   {
385     return_val = -1;
386     bRet = StrToIntExA(result->string,STIF_SUPPORT_HEX,&return_val);
387     ok(!bRet || return_val != -1, "No result returned from '%s'",
388        result->string);
389     if (bRet)
390       ok(return_val == result->str_to_int_hex, "converted '%s' wrong (%d)",
391          result->string, return_val);
392     result++;
393   }
394 }
395
396 static void test_StrToIntExW(void)
397 {
398   WCHAR szBuff[256];
399   const StrToIntResult *result = StrToInt_results;
400   int return_val;
401   BOOL bRet;
402
403   while (result->string)
404   {
405     return_val = -1;
406     MultiByteToWideChar(0,0,result->string,-1,szBuff,sizeof(szBuff)/sizeof(WCHAR));
407     bRet = StrToIntExW(szBuff, 0, &return_val);
408     ok(!bRet || return_val != -1, "No result returned from '%s'",
409        result->string);
410     if (bRet)
411       ok(return_val == result->str_to_int_ex, "converted '%s' wrong (%d)",
412          result->string, return_val);
413     result++;
414   }
415
416   result = StrToInt_results;
417   while (result->string)
418   {
419     return_val = -1;
420     MultiByteToWideChar(0,0,result->string,-1,szBuff,sizeof(szBuff)/sizeof(WCHAR));
421     bRet = StrToIntExW(szBuff, STIF_SUPPORT_HEX, &return_val);
422     ok(!bRet || return_val != -1, "No result returned from '%s'",
423        result->string);
424     if (bRet)
425       ok(return_val == result->str_to_int_hex, "converted '%s' wrong (%d)",
426          result->string, return_val);
427     result++;
428   }
429 }
430
431 static void test_StrDupA()
432 {
433   LPSTR lpszStr;
434   const StrFormatSizeResult* result = StrFormatSize_results;
435
436   while(result->value)
437   {
438     lpszStr = StrDupA(result->byte_size_64);
439
440     ok(lpszStr != NULL, "Dup failed");
441     if (lpszStr)
442     {
443       ok(!strcmp(result->byte_size_64, lpszStr), "Copied string wrong");
444       LocalFree((HLOCAL)lpszStr);
445     }
446     result++;
447   }
448
449   /* Later versions of shlwapi return NULL for this, but earlier versions
450    * returned an empty string (as Wine does).
451    */
452   lpszStr = StrDupA(NULL);
453   ok(lpszStr == NULL || *lpszStr == '\0', "NULL string returned %p", lpszStr);
454 }
455
456 static void test_StrFormatByteSize64A(void)
457 {
458   char szBuff[256];
459   const StrFormatSizeResult* result = StrFormatSize_results;
460
461   while(result->value)
462   {
463     StrFormatByteSize64A(result->value, szBuff, 256);
464
465     ok(!strcmp(result->byte_size_64, szBuff), "Formatted %lld wrong", result->value);
466
467     result++;
468   }
469 }
470
471 static void test_StrFormatKBSizeW(void)
472 {
473 /* FIXME: Awaiting NLS fixes in kernel before these succeed */
474 #if 0
475   WCHAR szBuffW[256];
476   char szBuff[256];
477   const StrFormatSizeResult* result = StrFormatSize_results;
478
479   while(result->value)
480   {
481     StrFormatKBSizeW(result->value, szBuffW, 256);
482     WideCharToMultiByte(0,0,szBuffW,-1,szBuff,sizeof(szBuff)/sizeof(WCHAR),0,0);
483     ok(!strcmp(result->kb_size, szBuff), "Formatted %lld wrong",
484        result->value);
485     result++;
486   }
487 #endif
488 }
489
490 static void test_StrFormatKBSizeA(void)
491 {
492 #if 0
493   char szBuff[256];
494   const StrFormatSizeResult* result = StrFormatSize_results;
495
496   while(result->value)
497   {
498     StrFormatKBSizeA(result->value, szBuff, 256);
499
500     ok(!strcmp(result->kb_size, szBuff), "Formatted %lld wrong",
501        result->value);
502     result++;
503   }
504 #endif
505 }
506
507 void test_StrFromTimeIntervalA(void)
508 {
509   char szBuff[256];
510   const StrFromTimeIntervalResult* result = StrFromTimeInterval_results;
511
512   while(result->ms)
513   {
514     StrFromTimeIntervalA(szBuff, 256, result->ms, result->digits);
515
516     ok(!strcmp(result->time_interval, szBuff), "Formatted %ld %d wrong",
517        result->ms, result->digits);
518     result++;
519   }
520 }
521
522 START_TEST(string)
523 {
524   test_StrChrA();
525   test_StrChrW();
526   test_StrChrIA();
527   test_StrChrIW();
528   test_StrRChrA();
529   test_StrRChrW();
530   test_StrCpyW();
531   test_StrToIntA();
532   test_StrToIntW();
533   test_StrToIntExA();
534   test_StrToIntExW();
535   test_StrDupA();
536   test_StrFormatByteSize64A();
537   test_StrFormatKBSizeA();
538   test_StrFormatKBSizeW();
539   test_StrFromTimeIntervalA();
540 }