winhttp: Implement IWinHttpRequest::Open.
[wine] / dlls / oleaut32 / tests / varformat.c
1 /*
2  * VARFORMAT test program
3  *
4  * Copyright 1998 Jean-Claude Cote
5  * Copyright 2006 Google (Benjamin Arai)
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include <math.h>
25 #include <float.h>
26
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winsock.h"
30 #include "wine/test.h"
31 #include "winuser.h"
32 #include "wingdi.h"
33 #include "winnls.h"
34 #include "winerror.h"
35 #include "winnt.h"
36
37 #include "wtypes.h"
38 #include "oleauto.h"
39
40 static HMODULE hOleaut32;
41
42 static HRESULT (WINAPI *pVarFormatNumber)(LPVARIANT,int,int,int,int,ULONG,BSTR*);
43 static HRESULT (WINAPI *pVarFormat)(LPVARIANT,LPOLESTR,int,int,ULONG,BSTR*);
44 static HRESULT (WINAPI *pVarWeekdayName)(int,int,int,ULONG,BSTR*);
45
46 /* Have I8/UI8 data type? */
47 #define HAVE_OLEAUT32_I8      HAVE_FUNC(VarI8FromI1)
48
49 /* Is a given function exported from oleaut32? */
50 #define HAVE_FUNC(func) ((void*)GetProcAddress(hOleaut32, #func) != NULL)
51
52 /* Get a conversion function ptr, return if function not available */
53 #define CHECKPTR(func) p##func = (void*)GetProcAddress(hOleaut32, #func); \
54   if (!p##func) { trace("function " # func " not available, not testing it\n"); return; }
55
56 static inline int strcmpW( const WCHAR *str1, const WCHAR *str2 )
57 {
58     while (*str1 && (*str1 == *str2)) { str1++; str2++; }
59     return *str1 - *str2;
60 }
61
62 #define FMT_NUMBER(vt,val) \
63   VariantInit(&v); V_VT(&v) = vt; val(&v) = 1; \
64   hres = pVarFormatNumber(&v,2,0,0,0,0,&str); \
65   ok(hres == S_OK, "VarFormatNumber (vt %d): returned %8x\n", vt, hres); \
66   if (hres == S_OK) { \
67     ok(str && strcmpW(str,szResult1) == 0, \
68        "VarFormatNumber (vt %d): string different\n", vt); \
69     SysFreeString(str); \
70   }
71
72 static void test_VarFormatNumber(void)
73 {
74   static const WCHAR szSrc1[] = { '1','\0' };
75   static const WCHAR szResult1[] = { '1','.','0','0','\0' };
76   static const WCHAR szSrc2[] = { '-','1','\0' };
77   static const WCHAR szResult2[] = { '(','1','.','0','0',')','\0' };
78   char buff[8];
79   HRESULT hres;
80   VARIANT v;
81   BSTR str = NULL;
82
83   CHECKPTR(VarFormatNumber);
84
85   GetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, buff, sizeof(buff)/sizeof(char));
86   if (buff[0] != '.' || buff[1])
87   {
88     skip("Skipping VarFormatNumber tests as decimal separator is '%s'\n", buff);
89     return;
90   }
91
92   FMT_NUMBER(VT_I1, V_I1);
93   FMT_NUMBER(VT_UI1, V_UI1);
94   FMT_NUMBER(VT_I2, V_I2);
95   FMT_NUMBER(VT_UI2, V_UI2);
96   FMT_NUMBER(VT_I4, V_I4);
97   FMT_NUMBER(VT_UI4, V_UI4);
98   if (HAVE_OLEAUT32_I8)
99   {
100     FMT_NUMBER(VT_I8, V_I8);
101     FMT_NUMBER(VT_UI8, V_UI8);
102   }
103   FMT_NUMBER(VT_R4, V_R4);
104   FMT_NUMBER(VT_R8, V_R8);
105   FMT_NUMBER(VT_BOOL, V_BOOL);
106
107   V_VT(&v) = VT_BSTR;
108   V_BSTR(&v) = SysAllocString(szSrc1);
109
110   hres = pVarFormatNumber(&v,2,0,0,0,0,&str);
111   ok(hres == S_OK, "VarFormatNumber (bstr): returned %8x\n", hres);
112   if (hres == S_OK)
113     ok(str && strcmpW(str, szResult1) == 0, "VarFormatNumber (bstr): string different\n");
114   SysFreeString(V_BSTR(&v));
115   SysFreeString(str);
116
117   V_BSTR(&v) = SysAllocString(szSrc2);
118   hres = pVarFormatNumber(&v,2,0,-1,0,0,&str);
119   ok(hres == S_OK, "VarFormatNumber (bstr): returned %8x\n", hres);
120   if (hres == S_OK)
121     ok(str && strcmpW(str, szResult2) == 0, "VarFormatNumber (-bstr): string different\n");
122   SysFreeString(V_BSTR(&v));
123   SysFreeString(str);
124 }
125
126 #define SIGNED_VTBITS (VTBIT_I1|VTBIT_I2|VTBIT_I4|VTBIT_I8|VTBIT_R4|VTBIT_R8)
127
128 static const char *szVarFmtFail = "VT %d|0x%04x Format %s: expected 0x%08x, '%s', got 0x%08x, '%s'\n";
129 #define VARFMT(vt,v,val,fmt,ret,str) do { \
130   out = NULL; \
131   V_VT(&in) = (vt); v(&in) = val; \
132   if (fmt) MultiByteToWideChar(CP_ACP, 0, fmt, -1, buffW, sizeof(buffW)/sizeof(WCHAR)); \
133   hres = pVarFormat(&in,fmt ? buffW : NULL,fd,fw,flags,&out); \
134   if (SUCCEEDED(hres)) WideCharToMultiByte(CP_ACP, 0, out, -1, buff, sizeof(buff),0,0); \
135   else buff[0] = '\0'; \
136   ok(hres == ret && (FAILED(ret) || !strcmp(buff, str)), \
137      szVarFmtFail, \
138      (vt)&VT_TYPEMASK,(vt)&~VT_TYPEMASK,fmt?fmt:"<null>",ret,str,hres,buff); \
139   SysFreeString(out); \
140   } while(0)
141
142 typedef struct tagFMTRES
143 {
144   LPCSTR fmt;
145   LPCSTR one_res;
146   LPCSTR zero_res;
147 } FMTRES;
148
149 static const FMTRES VarFormat_results[] =
150 {
151   { NULL, "1", "0" },
152   { "", "1", "0" },
153   { "General Number", "1", "0" },
154   { "Percent", "100.00%", "0.00%" },
155   { "Standard", "1.00", "0.00" },
156   { "Scientific","1.00E+00", "0.00E+00" },
157   { "True/False", "True", "False" },
158   { "On/Off", "On", "Off" },
159   { "Yes/No", "Yes", "No" },
160   { "#", "1", "" },
161   { "##", "1", "" },
162   { "#.#", "1.", "." },
163   { "0", "1", "0" },
164   { "00", "01", "00" },
165   { "0.0", "1.0", "0.0" },
166   { "00\\c\\o\\p\\y", "01copy","00copy" },
167   { "\"pos\";\"neg\"", "pos", "pos" },
168   { "\"pos\";\"neg\";\"zero\"","pos", "zero" }
169 };
170
171 typedef struct tagFMTDATERES
172 {
173   DATE   val;
174   LPCSTR fmt;
175   LPCSTR res;
176 } FMTDATERES;
177
178 static const FMTDATERES VarFormat_date_results[] =
179 {
180   { 0.0, "w", "7" },
181   { 0.0, "w", "6" },
182   { 0.0, "w", "5" },
183   { 0.0, "w", "4" },
184   { 0.0, "w", "3" },
185   { 0.0, "w", "2" },
186   { 0.0, "w", "1" }, /* First 7 entries must remain in this order! */
187   { 2.525, "am/pm", "pm" },
188   { 2.525, "AM/PM", "PM" },
189   { 2.525, "A/P", "P" },
190   { 2.525, "a/p", "p" },
191   { 2.525, "q", "1" },
192   { 2.525, "d", "1" },
193   { 2.525, "dd", "01" },
194   { 2.525, "ddd", "Mon" },
195   { 2.525, "dddd", "Monday" },
196   { 2.525, "mmm", "Jan" },
197   { 2.525, "mmmm", "January" },
198   { 2.525, "y", "1" },
199   { 2.525, "yy", "00" },
200   { 2.525, "yyy", "001" },
201   { 2.525, "yyyy", "1900" },
202   { 2.525, "dd mm yyyy hh:mm:ss", "01 01 1900 12:36:00" },
203   { 2.525, "dd mm yyyy mm", "01 01 1900 01" },
204   { 2.525, "dd mm yyyy :mm", "01 01 1900 :01" },
205   { 2.525, "dd mm yyyy hh:mm", "01 01 1900 12:36" },
206   { 2.525, "mm mm", "01 01" },
207   { 2.525, "mm :mm:ss", "01 :01:00" },
208   { 2.525, "mm :ss:mm", "01 :00:01" },
209   { 2.525, "hh:mm :ss:mm", "12:36 :00:01" },
210   { 2.525, "hh:dd :mm:mm", "12:01 :01:01" },
211   { 2.525, "dd:hh :mm:mm", "01:12 :36:01" },
212   { 2.525, "hh :mm:mm", "12 :36:01" },
213   { 2.525, "dd :mm:mm", "01 :01:01" },
214   { 2.525, "dd :mm:nn", "01 :01:36" },
215   { 2.725, "hh:nn:ss A/P", "05:24:00 P" },
216   { 40531.0, "dddd", "Sunday" },
217   { 40531.0, "ddd", "Sun" }
218 };
219
220 /* The following tests require that the time separator is a colon (:) */
221 static const FMTDATERES VarFormat_namedtime_results[] =
222 {
223   { 2.525, "short time", "12:36" },
224   { 2.525, "medium time", "12:36 PM" },
225   { 2.525, "long time", "12:36:00 PM" }
226 };
227
228 #define VNUMFMT(vt,v) \
229   for (i = 0; i < sizeof(VarFormat_results)/sizeof(FMTRES); i++) \
230   { \
231     VARFMT(vt,v,1,VarFormat_results[i].fmt,S_OK,VarFormat_results[i].one_res); \
232     VARFMT(vt,v,0,VarFormat_results[i].fmt,S_OK,VarFormat_results[i].zero_res); \
233   } \
234   if ((1 << vt) & SIGNED_VTBITS) \
235   { \
236     VARFMT(vt,v,-1,"\"pos\";\"neg\"",S_OK,"neg"); \
237     VARFMT(vt,v,-1,"\"pos\";\"neg\";\"zero\"",S_OK,"neg"); \
238   }
239
240 static void test_VarFormat(void)
241 {
242   static const WCHAR szTesting[] = { 't','e','s','t','i','n','g','\0' };
243   static const WCHAR szNum[] = { '3','9','6','9','7','.','1','1','\0' };
244   size_t i;
245   WCHAR buffW[256];
246   char buff[256];
247   VARIANT in;
248   VARIANT_BOOL bTrue = VARIANT_TRUE, bFalse = VARIANT_FALSE;
249   int fd = 0, fw = 0;
250   ULONG flags = 0;
251   BSTR bstrin, out = NULL;
252   HRESULT hres;
253
254   CHECKPTR(VarFormat);
255
256   if (PRIMARYLANGID(LANGIDFROMLCID(GetUserDefaultLCID())) != LANG_ENGLISH)
257   {
258     skip("Skipping VarFormat tests for non english language\n");
259     return;
260   }
261   GetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, buff, sizeof(buff)/sizeof(char));
262   if (buff[0] != '.' || buff[1])
263   {
264     skip("Skipping VarFormat tests as decimal separator is '%s'\n", buff);
265     return;
266   }
267   GetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_IDIGITS, buff, sizeof(buff)/sizeof(char));
268   if (buff[0] != '2' || buff[1])
269   {
270     skip("Skipping VarFormat tests as decimal places is '%s'\n", buff);
271     return;
272   }
273
274   VARFMT(VT_BOOL,V_BOOL,VARIANT_TRUE,"True/False",S_OK,"True");
275   VARFMT(VT_BOOL,V_BOOL,VARIANT_FALSE,"True/False",S_OK,"False");
276
277   VNUMFMT(VT_I1,V_I1);
278   VNUMFMT(VT_I2,V_I2);
279   VNUMFMT(VT_I4,V_I4);
280   if (HAVE_OLEAUT32_I8)
281   {
282     VNUMFMT(VT_I8,V_I8);
283   }
284   VNUMFMT(VT_INT,V_INT);
285   VNUMFMT(VT_UI1,V_UI1);
286   VNUMFMT(VT_UI2,V_UI2);
287   VNUMFMT(VT_UI4,V_UI4);
288   if (HAVE_OLEAUT32_I8)
289   {
290     VNUMFMT(VT_UI8,V_UI8);
291   }
292   VNUMFMT(VT_UINT,V_UINT);
293   VNUMFMT(VT_R4,V_R4);
294   VNUMFMT(VT_R8,V_R8);
295
296   /* Reference types are dereferenced */
297   VARFMT(VT_BOOL|VT_BYREF,V_BOOLREF,&bTrue,"True/False",S_OK,"True");
298   VARFMT(VT_BOOL|VT_BYREF,V_BOOLREF,&bFalse,"True/False",S_OK,"False");
299
300   /* Dates */
301   for (i = 0; i < sizeof(VarFormat_date_results)/sizeof(FMTDATERES); i++)
302   {
303     if (i < 7)
304       fd = i + 1; /* Test first day */
305     else
306       fd = 0;
307     VARFMT(VT_DATE,V_DATE,VarFormat_date_results[i].val,
308            VarFormat_date_results[i].fmt,S_OK,
309            VarFormat_date_results[i].res);
310   }
311
312   /* Named time formats */
313   GetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_STIMEFORMAT, buff, sizeof(buff)/sizeof(char));
314   if (strcmp(buff, "h:mm:ss tt"))
315   {
316     skip("Skipping named time tests as time format is '%s'\n", buff);
317   }
318   else
319   {
320     for (i = 0; i < sizeof(VarFormat_namedtime_results)/sizeof(FMTDATERES); i++)
321     {
322       fd = 0;
323       VARFMT(VT_DATE,V_DATE,VarFormat_namedtime_results[i].val,
324              VarFormat_namedtime_results[i].fmt,S_OK,
325              VarFormat_namedtime_results[i].res);
326     }
327   }
328
329   /* Strings */
330   bstrin = SysAllocString(szTesting);
331   VARFMT(VT_BSTR,V_BSTR,bstrin,"",S_OK,"testing");
332   VARFMT(VT_BSTR,V_BSTR,bstrin,"@",S_OK,"testing");
333   VARFMT(VT_BSTR,V_BSTR,bstrin,"&",S_OK,"testing");
334   VARFMT(VT_BSTR,V_BSTR,bstrin,"\\x@\\x@",S_OK,"xtxesting");
335   VARFMT(VT_BSTR,V_BSTR,bstrin,"\\x&\\x&",S_OK,"xtxesting");
336   VARFMT(VT_BSTR,V_BSTR,bstrin,"@\\x",S_OK,"txesting");
337   VARFMT(VT_BSTR,V_BSTR,bstrin,"@@@@@@@@",S_OK," testing");
338   VARFMT(VT_BSTR,V_BSTR,bstrin,"@\\x@@@@@@@",S_OK," xtesting");
339   VARFMT(VT_BSTR,V_BSTR,bstrin,"&&&&&&&&",S_OK,"testing");
340   VARFMT(VT_BSTR,V_BSTR,bstrin,"!&&&&&&&",S_OK,"testing");
341   VARFMT(VT_BSTR,V_BSTR,bstrin,"&&&&&&&!",S_OK,"testing");
342   VARFMT(VT_BSTR,V_BSTR,bstrin,">&&",S_OK,"TESTING");
343   VARFMT(VT_BSTR,V_BSTR,bstrin,"<&&",S_OK,"testing");
344   VARFMT(VT_BSTR,V_BSTR,bstrin,"<&>&",S_OK,"testing");
345   SysFreeString(bstrin);
346   bstrin = SysAllocString(szNum);
347   todo_wine VARFMT(VT_BSTR,V_BSTR,bstrin,"hh:mm",S_OK,"02:38");
348   todo_wine VARFMT(VT_BSTR,V_BSTR,bstrin,"mm-dd-yy",S_OK,"09-06-08");
349   SysFreeString(bstrin);
350   /* Numeric values are converted to strings then output */
351   VARFMT(VT_I1,V_I1,1,"<&>&",S_OK,"1");
352
353   /* Number formats */
354   VARFMT(VT_I4,V_I4,1,"#00000000",S_OK,"00000001");
355   VARFMT(VT_I4,V_I4,1,"000###",S_OK,"000001");
356   VARFMT(VT_I4,V_I4,1,"#00##00#0",S_OK,"00000001");
357   VARFMT(VT_I4,V_I4,1,"1#####0000",S_OK,"10001");
358   VARFMT(VT_I4,V_I4,1,"##abcdefghijklmnopqrstuvwxyz",S_OK,"1abcdefghijklmnopqrstuvwxyz");
359   VARFMT(VT_I4,V_I4,100000,"#,###,###,###",S_OK,"100,000");
360   VARFMT(VT_I4,V_I4,1,"0,000,000,000",S_OK,"0,000,000,001");
361   VARFMT(VT_I4,V_I4,123456789,"#,#.#",S_OK,"123,456,789.");
362   VARFMT(VT_I4,V_I4,123456789,"###, ###, ###",S_OK,"123, 456, 789");
363   VARFMT(VT_I4,V_I4,1,"#;-#",S_OK,"1");
364   VARFMT(VT_I4,V_I4,-1,"#;-#",S_OK,"-1");
365   VARFMT(VT_R8,V_R8,1.23456789,"0#.0#0#0#0#0",S_OK,"01.234567890");
366   VARFMT(VT_R8,V_R8,1.2,"0#.0#0#0#0#0",S_OK,"01.200000000");
367   VARFMT(VT_R8,V_R8,9.87654321,"#0.#0#0#0#0#",S_OK,"9.87654321");
368   VARFMT(VT_R8,V_R8,9.8,"#0.#0#0#0#0#",S_OK,"9.80000000");
369   VARFMT(VT_R8,V_R8,0.00000008,"#0.#0#0#0#0#0",S_OK,"0.0000000800");
370   VARFMT(VT_R8,V_R8,0.00010705,"#0.##########",S_OK,"0.00010705");
371   VARFMT(VT_I4,V_I4,17,"#0",S_OK,"17");
372   VARFMT(VT_I4,V_I4,4711,"#0",S_OK,"4711");
373   VARFMT(VT_I4,V_I4,17,"#00",S_OK,"17");
374   VARFMT(VT_I4,V_I4,100,"0##",S_OK,"100");
375   VARFMT(VT_I4,V_I4,17,"#000",S_OK,"017");
376   VARFMT(VT_I4,V_I4,17,"#0.00",S_OK,"17.00");
377   VARFMT(VT_I4,V_I4,17,"#0000.00",S_OK,"0017.00");
378   VARFMT(VT_I4,V_I4,17,"#.00",S_OK,"17.00");
379   VARFMT(VT_R8,V_R8,1.7,"#.00",S_OK,"1.70");
380   VARFMT(VT_R8,V_R8,.17,"#.00",S_OK,".17");
381   VARFMT(VT_I4,V_I4,17,"#3",S_OK,"173");
382   VARFMT(VT_I4,V_I4,17,"#33",S_OK,"1733");
383   VARFMT(VT_I4,V_I4,17,"#3.33",S_OK,"173.33");
384   VARFMT(VT_I4,V_I4,17,"#3333.33",S_OK,"173333.33");
385   VARFMT(VT_I4,V_I4,17,"#.33",S_OK,"17.33");
386   VARFMT(VT_R8,V_R8,.17,"#.33",S_OK,".33");
387   VARFMT(VT_R8,V_R8,1.7,"0.0000E-000",S_OK,"1.7000E000");
388   VARFMT(VT_R8,V_R8,1.7,"0.0000e-1",S_OK,"1.7000e01");
389   VARFMT(VT_R8,V_R8,86.936849,"#0.000000000000e-000",S_OK,"86.936849000000e000");
390   VARFMT(VT_R8,V_R8,1.7,"#0",S_OK,"2");
391   VARFMT(VT_R8,V_R8,1.7,"#.33",S_OK,"2.33");
392   VARFMT(VT_R8,V_R8,1.7,"#3",S_OK,"23");
393   VARFMT(VT_R8,V_R8,1.73245,"0.0000E+000",S_OK,"1.7325E+000");
394   VARFMT(VT_R8,V_R8,9.9999999,"#0.000000",S_OK,"10.000000");
395   VARFMT(VT_R8,V_R8,1.7,"0.0000e+0#",S_OK,"1.7000e+0");
396   VARFMT(VT_R8,V_R8,100.0001e+0,"0.0000E+0",S_OK,"1.0000E+2");
397   VARFMT(VT_R8,V_R8,1000001,"0.0000e+1",S_OK,"1.0000e+61");
398   VARFMT(VT_R8,V_R8,100.0001e+25,"0.0000e+0",S_OK,"1.0000e+27");
399   VARFMT(VT_R8,V_R8,450.0001e+43,"#000.0000e+0",S_OK,"4500.0010e+42");
400   VARFMT(VT_R8,V_R8,0.0001e-11,"##00.0000e-0",S_OK,"1000.0000e-18");
401   VARFMT(VT_R8,V_R8,0.0317e-11,"0000.0000e-0",S_OK,"3170.0000e-16");
402   VARFMT(VT_R8,V_R8,0.0021e-11,"00##.0000e-0",S_OK,"2100.0000e-17");
403   VARFMT(VT_R8,V_R8,1.0001e-27,"##00.0000e-0",S_OK,"1000.1000e-30");
404   VARFMT(VT_R8,V_R8,47.11,".0000E+0",S_OK,".4711E+2");
405   VARFMT(VT_R8,V_R8,3.0401e-13,"#####.####e-0%",S_OK,"30401.e-15%");
406   VARFMT(VT_R8,V_R8,1.57,"0.00",S_OK,"1.57");
407   VARFMT(VT_R8,V_R8,-1.57,"0.00",S_OK,"-1.57");
408   VARFMT(VT_R8,V_R8,-1.57,"#.##",S_OK,"-1.57");
409   VARFMT(VT_R8,V_R8,-0.1,".#",S_OK,"-.1");
410   VARFMT(VT_R8,V_R8,0.099,"#.#",S_OK,".1");
411   VARFMT(VT_R8,V_R8,0.0999,"#.##",S_OK,".1");
412   VARFMT(VT_R8,V_R8,0.099,"#.##",S_OK,".1");
413   VARFMT(VT_R8,V_R8,0.0099,"#.##",S_OK,".01");
414   VARFMT(VT_R8,V_R8,0.0049,"#.##",S_OK,".");
415   VARFMT(VT_R8,V_R8,0.0094,"#.##",S_OK,".01");
416   VARFMT(VT_R8,V_R8,0.00099,"#.##",S_OK,".");
417   VARFMT(VT_R8,V_R8,0.0995,"#.##",S_OK,".1");
418   VARFMT(VT_R8,V_R8,8.0995,"#.##",S_OK,"8.1");
419   VARFMT(VT_R8,V_R8,0.0994,"#.##",S_OK,".1");
420   VARFMT(VT_R8,V_R8,1.00,"#,##0.00",S_OK,"1.00");
421   VARFMT(VT_R8,V_R8,0.0995,"#.###",S_OK,".1");
422
423
424   /* 'out' is not cleared */
425   out = (BSTR)0x1;
426   hres = pVarFormat(&in,NULL,fd,fw,flags,&out); /* Would crash if out is cleared */
427   ok(hres == S_OK, "got %08x\n", hres);
428   SysFreeString(out);
429   out = NULL;
430
431   /* VT_NULL */
432   V_VT(&in) = VT_NULL;
433   hres = pVarFormat(&in,NULL,fd,fw,0,&out);
434   ok(hres == S_OK, "VarFormat failed with 0x%08x\n", hres);
435   ok(out == NULL, "expected NULL formatted string\n");
436
437   /* Invalid args */
438   hres = pVarFormat(&in,NULL,fd,fw,flags,NULL);
439   ok(hres == E_INVALIDARG, "Null out: expected E_INVALIDARG, got 0x%08x\n", hres);
440   hres = pVarFormat(NULL,NULL,fd,fw,flags,&out);
441   ok(hres == E_INVALIDARG, "Null in: expected E_INVALIDARG, got 0x%08x\n", hres);
442   fd = -1;
443   VARFMT(VT_BOOL,V_BOOL,VARIANT_TRUE,"",E_INVALIDARG,"");
444   fd = 8;
445   VARFMT(VT_BOOL,V_BOOL,VARIANT_TRUE,"",E_INVALIDARG,"");
446   fd = 0; fw = -1;
447   VARFMT(VT_BOOL,V_BOOL,VARIANT_TRUE,"",E_INVALIDARG,"");
448   fw = 4;
449   VARFMT(VT_BOOL,V_BOOL,VARIANT_TRUE,"",E_INVALIDARG,"");
450 }
451
452 static const char *szVarWdnFail =
453     "VarWeekdayName (%d, %d, %d, %d, %x): returned %8x, expected %8x\n";
454 #define VARWDN(iWeekday, fAbbrev, iFirstDay, dwFlags, ret, buff, out, freeOut) \
455 do { \
456   hres = pVarWeekdayName(iWeekday, fAbbrev, iFirstDay, dwFlags, &out); \
457   if (SUCCEEDED(hres)) { \
458     WideCharToMultiByte(CP_ACP, 0, out, -1, buff, sizeof(buff), 0, 0); \
459     if (freeOut) SysFreeString(out); \
460   } else { \
461     buff[0] = '\0'; \
462   } \
463   ok(hres == ret, \
464      szVarWdnFail, \
465      iWeekday, fAbbrev, iFirstDay, dwFlags, &out, hres, ret \
466      ); \
467 } while(0)
468
469 #define VARWDN_F(iWeekday, fAbbrev, iFirstDay, dwFlags, ret) \
470   VARWDN(iWeekday, fAbbrev, iFirstDay, dwFlags, ret, buff, out, 1)
471
472 #define VARWDN_O(iWeekday, fAbbrev, iFirstDay, dwFlags) \
473   VARWDN(iWeekday, fAbbrev, iFirstDay, dwFlags, S_OK, buff, out, 0)
474
475 static void test_VarWeekdayName(void)
476 {
477   char buff[256];
478   BSTR out = NULL;
479   HRESULT hres;
480   int iWeekday, fAbbrev, iFirstDay;
481   BSTR dayNames[7][2]; /* Monday-Sunday, full/abbr */
482   DWORD defaultFirstDay;
483   int firstDay;
484   int day;
485   int size;
486   DWORD localeValue;
487
488   CHECKPTR(VarWeekdayName);
489
490   SetLastError(0xdeadbeef);
491   GetLocaleInfoW(LOCALE_USER_DEFAULT, 0, NULL, 0);
492   if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
493   {
494     win_skip("GetLocaleInfoW is not implemented\n");
495     return;
496   }
497
498   /* Initialize days' names */
499   for (day = 0; day <= 6; ++day)
500   {
501     for (fAbbrev = 0; fAbbrev <= 1; ++fAbbrev)
502     {
503       localeValue = fAbbrev ? LOCALE_SABBREVDAYNAME1 : LOCALE_SDAYNAME1;
504       localeValue += day;
505       size = GetLocaleInfoW(LOCALE_USER_DEFAULT, localeValue, NULL, 0);
506       dayNames[day][fAbbrev] = SysAllocStringLen(NULL, size - 1);
507       GetLocaleInfoW(LOCALE_USER_DEFAULT, localeValue,
508                      dayNames[day][fAbbrev], size);
509     }
510   }
511
512   /* Get the user's first day of week. 0=Monday, .. */
513   GetLocaleInfoW(
514       LOCALE_USER_DEFAULT, LOCALE_IFIRSTDAYOFWEEK | LOCALE_RETURN_NUMBER,
515       (LPWSTR)&defaultFirstDay, sizeof(defaultFirstDay) / sizeof(WCHAR));
516
517   /* Check invalid arguments */
518   VARWDN_F(0, 0, 4, 0, E_INVALIDARG);
519   VARWDN_F(8, 0, 4, 0, E_INVALIDARG);
520   VARWDN_F(4, 0, -1, 0, E_INVALIDARG);
521   VARWDN_F(4, 0, 8, 0, E_INVALIDARG);
522
523   hres = pVarWeekdayName(1, 0, 0, 0, NULL);
524   ok(E_INVALIDARG == hres,
525      "Null pointer: expected E_INVALIDARG, got 0x%08x\n", hres);
526
527   /* Check all combinations */
528   for (iWeekday = 1; iWeekday <= 7; ++iWeekday)
529   {
530     for (fAbbrev = 0; fAbbrev <= 1; ++fAbbrev)
531     {
532       /* 0 = Default, 1 = Sunday, 2 = Monday, .. */
533       for (iFirstDay = 0; iFirstDay <= 7; ++iFirstDay)
534       {
535         VARWDN_O(iWeekday, fAbbrev, iFirstDay, 0);
536         if (iFirstDay == 0)
537           firstDay = defaultFirstDay;
538         else
539           /* Translate from 0=Sunday to 0=Monday in the modulo 7 space */
540           firstDay = iFirstDay - 2;
541         day = (7 + iWeekday - 1 + firstDay) % 7;
542         ok(VARCMP_EQ == VarBstrCmp(out, dayNames[day][fAbbrev],
543                                    LOCALE_USER_DEFAULT, 0),
544            "VarWeekdayName(%d,%d,%d): got wrong dayname: '%s'\n",
545            iWeekday, fAbbrev, iFirstDay, buff);
546         SysFreeString(out);
547       }
548     }
549   }
550
551   /* Cleanup */
552   for (day = 0; day <= 6; ++day)
553   {
554     for (fAbbrev = 0; fAbbrev <= 1; ++fAbbrev)
555     {
556       SysFreeString(dayNames[day][fAbbrev]);
557     }
558   }
559 }
560
561 START_TEST(varformat)
562 {
563   hOleaut32 = GetModuleHandleA("oleaut32.dll");
564
565   test_VarFormatNumber();
566   test_VarFormat();
567   test_VarWeekdayName();
568 }