2 * VARFORMAT test program
4 * Copyright 1998 Jean-Claude Cote
5 * Copyright 2006 Google (Benjamin Arai)
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
31 #include "wine/test.h"
41 static HMODULE hOleaut32;
43 static HRESULT (WINAPI *pVarFormatNumber)(LPVARIANT,int,int,int,int,ULONG,BSTR*);
44 static HRESULT (WINAPI *pVarFormat)(LPVARIANT,LPOLESTR,int,int,ULONG,BSTR*);
45 static HRESULT (WINAPI *pVarWeekdayName)(int,int,int,ULONG,BSTR*);
47 /* Have I8/UI8 data type? */
48 #define HAVE_OLEAUT32_I8 HAVE_FUNC(VarI8FromI1)
50 /* Is a given function exported from oleaut32? */
51 #define HAVE_FUNC(func) ((void*)GetProcAddress(hOleaut32, #func) != NULL)
53 /* Get a conversion function ptr, return if function not available */
54 #define CHECKPTR(func) p##func = (void*)GetProcAddress(hOleaut32, #func); \
55 if (!p##func) { trace("function " # func " not available, not testing it\n"); return; }
57 static inline int strcmpW( const WCHAR *str1, const WCHAR *str2 )
59 while (*str1 && (*str1 == *str2)) { str1++; str2++; }
63 #define FMT_NUMBER(vt,val) \
64 VariantInit(&v); V_VT(&v) = vt; val(&v) = 1; \
65 hres = pVarFormatNumber(&v,2,0,0,0,0,&str); \
66 ok(hres == S_OK, "VarFormatNumber (vt %d): returned %8x\n", vt, hres); \
68 ok(str && strcmpW(str,szResult1) == 0, \
69 "VarFormatNumber (vt %d): string different\n", vt)
71 static void test_VarFormatNumber(void)
73 static const WCHAR szSrc1[] = { '1','\0' };
74 static const WCHAR szResult1[] = { '1','.','0','0','\0' };
75 static const WCHAR szSrc2[] = { '-','1','\0' };
76 static const WCHAR szResult2[] = { '(','1','.','0','0',')','\0' };
82 CHECKPTR(VarFormatNumber);
84 GetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, buff, sizeof(buff)/sizeof(char));
85 if (buff[0] != '.' || buff[1])
87 skip("Skipping VarFormatNumber tests as decimal separator is '%s'\n", buff);
91 FMT_NUMBER(VT_I1, V_I1);
92 FMT_NUMBER(VT_UI1, V_UI1);
93 FMT_NUMBER(VT_I2, V_I2);
94 FMT_NUMBER(VT_UI2, V_UI2);
95 FMT_NUMBER(VT_I4, V_I4);
96 FMT_NUMBER(VT_UI4, V_UI4);
99 FMT_NUMBER(VT_I8, V_I8);
100 FMT_NUMBER(VT_UI8, V_UI8);
102 FMT_NUMBER(VT_R4, V_R4);
103 FMT_NUMBER(VT_R8, V_R8);
104 FMT_NUMBER(VT_BOOL, V_BOOL);
107 V_BSTR(&v) = SysAllocString(szSrc1);
109 hres = pVarFormatNumber(&v,2,0,0,0,0,&str);
110 ok(hres == S_OK, "VarFormatNumber (bstr): returned %8x\n", hres);
112 ok(str && strcmpW(str, szResult1) == 0, "VarFormatNumber (bstr): string different\n");
113 SysFreeString(V_BSTR(&v));
116 V_BSTR(&v) = SysAllocString(szSrc2);
117 hres = pVarFormatNumber(&v,2,0,-1,0,0,&str);
118 ok(hres == S_OK, "VarFormatNumber (bstr): returned %8x\n", hres);
120 ok(str && strcmpW(str, szResult2) == 0, "VarFormatNumber (-bstr): string different\n");
121 SysFreeString(V_BSTR(&v));
125 #define SIGNED_VTBITS (VTBIT_I1|VTBIT_I2|VTBIT_I4|VTBIT_I8|VTBIT_R4|VTBIT_R8)
127 static const char *szVarFmtFail = "VT %d|0x%04x Format %s: expected 0x%08x, '%s', got 0x%08x, '%s'\n";
128 #define VARFMT(vt,v,val,fmt,ret,str) do { \
129 if (out) SysFreeString(out); out = NULL; \
130 V_VT(&in) = (vt); v(&in) = val; \
131 if (fmt) MultiByteToWideChar(CP_ACP, 0, fmt, -1, buffW, sizeof(buffW)/sizeof(WCHAR)); \
132 hres = pVarFormat(&in,fmt ? buffW : NULL,fd,fw,flags,&out); \
133 if (SUCCEEDED(hres)) WideCharToMultiByte(CP_ACP, 0, out, -1, buff, sizeof(buff),0,0); \
134 else buff[0] = '\0'; \
135 ok(hres == ret && (FAILED(ret) || !strcmp(buff, str)), \
137 (vt)&VT_TYPEMASK,(vt)&~VT_TYPEMASK,fmt?fmt:"<null>",ret,str,hres,buff); \
140 typedef struct tagFMTRES
147 static const FMTRES VarFormat_results[] =
151 { "General Number", "1", "0" },
152 { "Percent", "100.00%", "0.00%" },
153 { "Standard", "1.00", "0.00" },
154 { "Scientific","1.00E+00", "0.00E+00" },
155 { "True/False", "True", "False" },
156 { "On/Off", "On", "Off" },
157 { "Yes/No", "Yes", "No" },
160 { "#.#", "1.", "." },
162 { "00", "01", "00" },
163 { "0.0", "1.0", "0.0" },
164 { "00\\c\\o\\p\\y", "01copy","00copy" },
165 { "\"pos\";\"neg\"", "pos", "pos" },
166 { "\"pos\";\"neg\";\"zero\"","pos", "zero" }
169 typedef struct tagFMTDATERES
176 static const FMTDATERES VarFormat_date_results[] =
184 { 0.0, "w", "1" }, /* First 7 entries must remain in this order! */
185 { 2.525, "am/pm", "pm" },
186 { 2.525, "AM/PM", "PM" },
187 { 2.525, "A/P", "P" },
188 { 2.525, "a/p", "p" },
191 { 2.525, "dd", "01" },
192 { 2.525, "ddd", "Mon" },
193 { 2.525, "dddd", "Monday" },
194 { 2.525, "mmm", "Jan" },
195 { 2.525, "mmmm", "January" },
197 { 2.525, "yy", "00" },
198 { 2.525, "yyy", "001" },
199 { 2.525, "yyyy", "1900" },
200 { 2.525, "dd mm yyyy hh:mm:ss", "01 01 1900 12:36:00" },
201 { 2.525, "dd mm yyyy mm", "01 01 1900 01" },
202 { 2.525, "dd mm yyyy :mm", "01 01 1900 :01" },
203 { 2.525, "dd mm yyyy hh:mm", "01 01 1900 12:36" },
204 { 2.525, "mm mm", "01 01" },
205 { 2.525, "mm :mm:ss", "01 :01:00" },
206 { 2.525, "mm :ss:mm", "01 :00:01" },
207 { 2.525, "hh:mm :ss:mm", "12:36 :00:01" },
208 { 2.525, "hh:dd :mm:mm", "12:01 :01:01" },
209 { 2.525, "dd:hh :mm:mm", "01:12 :36:01" },
210 { 2.525, "hh :mm:mm", "12 :36:01" },
211 { 2.525, "dd :mm:mm", "01 :01:01" },
212 { 2.525, "dd :mm:nn", "01 :01:36" },
213 { 2.725, "hh:nn:ss A/P", "05:24:00 P" }
216 #define VNUMFMT(vt,v) \
217 for (i = 0; i < sizeof(VarFormat_results)/sizeof(FMTRES); i++) \
219 VARFMT(vt,v,1,VarFormat_results[i].fmt,S_OK,VarFormat_results[i].one_res); \
220 VARFMT(vt,v,0,VarFormat_results[i].fmt,S_OK,VarFormat_results[i].zero_res); \
222 if ((1 << vt) & SIGNED_VTBITS) \
224 VARFMT(vt,v,-1,"\"pos\";\"neg\"",S_OK,"neg"); \
225 VARFMT(vt,v,-1,"\"pos\";\"neg\";\"zero\"",S_OK,"neg"); \
228 static void test_VarFormat(void)
230 static const WCHAR szTesting[] = { 't','e','s','t','i','n','g','\0' };
235 VARIANT_BOOL bTrue = VARIANT_TRUE, bFalse = VARIANT_FALSE;
238 BSTR bstrin, out = NULL;
243 if (PRIMARYLANGID(LANGIDFROMLCID(GetUserDefaultLCID())) != LANG_ENGLISH)
245 skip("Skipping VarFormat tests for non english language\n");
248 GetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, buff, sizeof(buff)/sizeof(char));
249 if (buff[0] != '.' || buff[1])
251 skip("Skipping VarFormat tests as decimal separator is '%s'\n", buff);
254 GetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_IDIGITS, buff, sizeof(buff)/sizeof(char));
255 if (buff[0] != '2' || buff[1])
257 skip("Skipping VarFormat tests as decimal places is '%s'\n", buff);
261 VARFMT(VT_BOOL,V_BOOL,VARIANT_TRUE,"True/False",S_OK,"True");
262 VARFMT(VT_BOOL,V_BOOL,VARIANT_FALSE,"True/False",S_OK,"False");
267 if (HAVE_OLEAUT32_I8)
271 VNUMFMT(VT_INT,V_INT);
272 VNUMFMT(VT_UI1,V_UI1);
273 VNUMFMT(VT_UI2,V_UI2);
274 VNUMFMT(VT_UI4,V_UI4);
275 if (HAVE_OLEAUT32_I8)
277 VNUMFMT(VT_UI8,V_UI8);
279 VNUMFMT(VT_UINT,V_UINT);
283 /* Reference types are dereferenced */
284 VARFMT(VT_BOOL|VT_BYREF,V_BOOLREF,&bTrue,"True/False",S_OK,"True");
285 VARFMT(VT_BOOL|VT_BYREF,V_BOOLREF,&bFalse,"True/False",S_OK,"False");
288 for (i = 0; i < sizeof(VarFormat_date_results)/sizeof(FMTDATERES); i++)
291 fd = i + 1; /* Test first day */
294 VARFMT(VT_DATE,V_DATE,VarFormat_date_results[i].val,
295 VarFormat_date_results[i].fmt,S_OK,
296 VarFormat_date_results[i].res);
300 bstrin = SysAllocString(szTesting);
301 VARFMT(VT_BSTR,V_BSTR,bstrin,"",S_OK,"testing");
302 VARFMT(VT_BSTR,V_BSTR,bstrin,"@",S_OK,"testing");
303 VARFMT(VT_BSTR,V_BSTR,bstrin,"&",S_OK,"testing");
304 VARFMT(VT_BSTR,V_BSTR,bstrin,"\\x@\\x@",S_OK,"xtxesting");
305 VARFMT(VT_BSTR,V_BSTR,bstrin,"\\x&\\x&",S_OK,"xtxesting");
306 VARFMT(VT_BSTR,V_BSTR,bstrin,"@\\x",S_OK,"txesting");
307 VARFMT(VT_BSTR,V_BSTR,bstrin,"@@@@@@@@",S_OK," testing");
308 VARFMT(VT_BSTR,V_BSTR,bstrin,"@\\x@@@@@@@",S_OK," xtesting");
309 VARFMT(VT_BSTR,V_BSTR,bstrin,"&&&&&&&&",S_OK,"testing");
310 VARFMT(VT_BSTR,V_BSTR,bstrin,"!&&&&&&&",S_OK,"testing");
311 VARFMT(VT_BSTR,V_BSTR,bstrin,"&&&&&&&!",S_OK,"testing");
312 VARFMT(VT_BSTR,V_BSTR,bstrin,">&&",S_OK,"TESTING");
313 VARFMT(VT_BSTR,V_BSTR,bstrin,"<&&",S_OK,"testing");
314 VARFMT(VT_BSTR,V_BSTR,bstrin,"<&>&",S_OK,"testing");
315 SysFreeString(bstrin);
316 /* Numeric values are converted to strings then output */
317 VARFMT(VT_I1,V_I1,1,"<&>&",S_OK,"1");
320 VARFMT(VT_I4,V_I4,1,"#00000000",S_OK,"00000001");
321 VARFMT(VT_I4,V_I4,1,"000###",S_OK,"000001");
322 VARFMT(VT_I4,V_I4,1,"#00##00#0",S_OK,"00000001");
323 VARFMT(VT_I4,V_I4,1,"1#####0000",S_OK,"10001");
325 VARFMT(VT_I4,V_I4,100000,"#,###,###,###",S_OK,"100,000");
327 VARFMT(VT_R8,V_R8,1.23456789,"0#.0#0#0#0#0",S_OK,"01.234567890");
328 VARFMT(VT_R8,V_R8,1.2,"0#.0#0#0#0#0",S_OK,"01.200000000");
329 VARFMT(VT_R8,V_R8,9.87654321,"#0.#0#0#0#0#",S_OK,"9.87654321");
330 VARFMT(VT_R8,V_R8,9.8,"#0.#0#0#0#0#",S_OK,"9.80000000");
331 VARFMT(VT_R8,V_R8,0.00000008,"#0.#0#0#0#0#0",S_OK,"0.0000000800");
332 VARFMT(VT_R8,V_R8,0.00010705,"#0.##########",S_OK,"0.00010705");
333 VARFMT(VT_I4,V_I4,17,"#0",S_OK,"17");
334 VARFMT(VT_I4,V_I4,4711,"#0",S_OK,"4711");
335 VARFMT(VT_I4,V_I4,17,"#00",S_OK,"17");
336 VARFMT(VT_I4,V_I4,100,"0##",S_OK,"100");
337 VARFMT(VT_I4,V_I4,17,"#000",S_OK,"017");
338 VARFMT(VT_I4,V_I4,17,"#0.00",S_OK,"17.00");
339 VARFMT(VT_I4,V_I4,17,"#0000.00",S_OK,"0017.00");
340 VARFMT(VT_I4,V_I4,17,"#.00",S_OK,"17.00");
341 VARFMT(VT_R8,V_R8,1.7,"#.00",S_OK,"1.70");
342 VARFMT(VT_R8,V_R8,.17,"#.00",S_OK,".17");
343 VARFMT(VT_I4,V_I4,17,"#3",S_OK,"173");
344 VARFMT(VT_I4,V_I4,17,"#33",S_OK,"1733");
345 VARFMT(VT_I4,V_I4,17,"#3.33",S_OK,"173.33");
346 VARFMT(VT_I4,V_I4,17,"#3333.33",S_OK,"173333.33");
347 VARFMT(VT_I4,V_I4,17,"#.33",S_OK,"17.33");
348 VARFMT(VT_R8,V_R8,.17,"#.33",S_OK,".33");
349 VARFMT(VT_R8,V_R8,1.7,"0.0000E-000",S_OK,"1.7000E000");
350 VARFMT(VT_R8,V_R8,1.7,"0.0000e-1",S_OK,"1.7000e01");
351 VARFMT(VT_R8,V_R8,86.936849,"#0.000000000000e-000",S_OK,"86.936849000000e000");
352 VARFMT(VT_R8,V_R8,1.7,"#0",S_OK,"2");
353 VARFMT(VT_R8,V_R8,1.7,"#.33",S_OK,"2.33");
354 VARFMT(VT_R8,V_R8,1.7,"#3",S_OK,"23");
355 VARFMT(VT_R8,V_R8,1.73245,"0.0000E+000",S_OK,"1.7325E+000");
356 VARFMT(VT_R8,V_R8,9.9999999,"#0.000000",S_OK,"10.000000");
357 VARFMT(VT_R8,V_R8,1.7,"0.0000e+0#",S_OK,"1.7000e+0");
358 VARFMT(VT_R8,V_R8,100.0001e+0,"0.0000E+0",S_OK,"1.0000E+2");
359 VARFMT(VT_R8,V_R8,1000001,"0.0000e+1",S_OK,"1.0000e+61");
360 VARFMT(VT_R8,V_R8,100.0001e+25,"0.0000e+0",S_OK,"1.0000e+27");
361 VARFMT(VT_R8,V_R8,450.0001e+43,"#000.0000e+0",S_OK,"4500.0010e+42");
362 VARFMT(VT_R8,V_R8,0.0001e-11,"##00.0000e-0",S_OK,"1000.0000e-18");
363 VARFMT(VT_R8,V_R8,0.0317e-11,"0000.0000e-0",S_OK,"3170.0000e-16");
364 VARFMT(VT_R8,V_R8,0.0021e-11,"00##.0000e-0",S_OK,"2100.0000e-17");
365 VARFMT(VT_R8,V_R8,1.0001e-27,"##00.0000e-0",S_OK,"1000.1000e-30");
366 VARFMT(VT_R8,V_R8,47.11,".0000E+0",S_OK,".4711E+2");
367 VARFMT(VT_R8,V_R8,3.0401e-13,"#####.####e-0%",S_OK,"30401.e-15%");
370 /* 'out' is not cleared */
372 pVarFormat(&in,NULL,fd,fw,flags,&out); /* Would crash if out is cleared */
377 hres = pVarFormat(&in,NULL,fd,fw,0,&out);
378 ok(hres == S_OK, "VarFormat failed with 0x%08x\n", hres);
379 ok(out == NULL, "expected NULL formatted string\n");
382 hres = pVarFormat(&in,NULL,fd,fw,flags,NULL);
383 ok(hres == E_INVALIDARG, "Null out: expected E_INVALIDARG, got 0x%08x\n", hres);
384 hres = pVarFormat(NULL,NULL,fd,fw,flags,&out);
385 ok(hres == E_INVALIDARG, "Null in: expected E_INVALIDARG, got 0x%08x\n", hres);
387 VARFMT(VT_BOOL,V_BOOL,VARIANT_TRUE,"",E_INVALIDARG,"");
389 VARFMT(VT_BOOL,V_BOOL,VARIANT_TRUE,"",E_INVALIDARG,"");
391 VARFMT(VT_BOOL,V_BOOL,VARIANT_TRUE,"",E_INVALIDARG,"");
393 VARFMT(VT_BOOL,V_BOOL,VARIANT_TRUE,"",E_INVALIDARG,"");
396 static const char *szVarWdnFail =
397 "VarWeekdayName (%d, %d, %d, %d, %x): returned %8x, expected %8x\n";
398 #define VARWDN(iWeekday, fAbbrev, iFirstDay, dwFlags, ret, buff, out, freeOut) \
400 hres = pVarWeekdayName(iWeekday, fAbbrev, iFirstDay, dwFlags, &out); \
401 if (SUCCEEDED(hres)) { \
402 WideCharToMultiByte(CP_ACP, 0, out, -1, buff, sizeof(buff), 0, 0); \
403 if (freeOut) SysFreeString(out); \
409 iWeekday, fAbbrev, iFirstDay, dwFlags, &out, hres, ret \
413 #define VARWDN_F(iWeekday, fAbbrev, iFirstDay, dwFlags, ret) \
414 VARWDN(iWeekday, fAbbrev, iFirstDay, dwFlags, ret, buff, out, 1)
416 #define VARWDN_O(iWeekday, fAbbrev, iFirstDay, dwFlags) \
417 VARWDN(iWeekday, fAbbrev, iFirstDay, dwFlags, S_OK, buff, out, 0)
419 static void test_VarWeekdayName(void)
424 int iWeekday, fAbbrev, iFirstDay;
425 BSTR dayNames[7][2]; /* Monday-Sunday, full/abbr */
426 DWORD defaultFirstDay;
432 CHECKPTR(VarWeekdayName);
434 SetLastError(0xdeadbeef);
435 GetLocaleInfoW(LOCALE_USER_DEFAULT, 0, NULL, 0);
436 if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
438 skip("GetLocaleInfoW is not implemented\n");
442 /* Initialize days' names */
443 for (day = 0; day <= 6; ++day)
445 for (fAbbrev = 0; fAbbrev <= 1; ++fAbbrev)
447 localeValue = fAbbrev ? LOCALE_SABBREVDAYNAME1 : LOCALE_SDAYNAME1;
449 size = GetLocaleInfoW(LOCALE_USER_DEFAULT, localeValue, NULL, 0);
450 dayNames[day][fAbbrev] = SysAllocStringLen(NULL, size - 1);
451 GetLocaleInfoW(LOCALE_USER_DEFAULT, localeValue,
452 dayNames[day][fAbbrev], size);
456 /* Get the user's first day of week. 0=Monday, .. */
458 LOCALE_USER_DEFAULT, LOCALE_IFIRSTDAYOFWEEK | LOCALE_RETURN_NUMBER,
459 (LPWSTR)&defaultFirstDay, sizeof(defaultFirstDay) / sizeof(WCHAR));
461 /* Check invalid arguments */
462 VARWDN_F(0, 0, 4, 0, E_INVALIDARG);
463 VARWDN_F(8, 0, 4, 0, E_INVALIDARG);
464 VARWDN_F(4, 0, -1, 0, E_INVALIDARG);
465 VARWDN_F(4, 0, 8, 0, E_INVALIDARG);
467 hres = pVarWeekdayName(1, 0, 0, 0, NULL);
468 ok(E_INVALIDARG == hres,
469 "Null pointer: expected E_INVALIDARG, got 0x%08x\n", hres);
471 /* Check all combinations */
472 for (iWeekday = 1; iWeekday <= 7; ++iWeekday)
474 for (fAbbrev = 0; fAbbrev <= 1; ++fAbbrev)
476 /* 0 = Default, 1 = Sunday, 2 = Monday, .. */
477 for (iFirstDay = 0; iFirstDay <= 7; ++iFirstDay)
479 VARWDN_O(iWeekday, fAbbrev, iFirstDay, 0);
481 firstDay = defaultFirstDay;
483 /* Translate from 0=Sunday to 0=Monday in the modulo 7 space */
484 firstDay = iFirstDay - 2;
485 day = (7 + iWeekday - 1 + firstDay) % 7;
486 ok(VARCMP_EQ == VarBstrCmp(out, dayNames[day][fAbbrev],
487 LOCALE_USER_DEFAULT, 0),
488 "VarWeekdayName(%d,%d,%d): got wrong dayname: '%s'\n",
489 iWeekday, fAbbrev, iFirstDay, buff);
496 for (day = 0; day <= 6; ++day)
498 for (fAbbrev = 0; fAbbrev <= 1; ++fAbbrev)
500 SysFreeString(dayNames[day][fAbbrev]);
505 START_TEST(varformat)
507 hOleaut32 = GetModuleHandleA("oleaut32.dll");
509 test_VarFormatNumber();
511 test_VarWeekdayName();