Fixed some issues found by winapi_check.
[wine] / dlls / kernel / tests / locale.c
1 /*
2  * Very basic unit test for locale functions
3  * Test run on win2K (French)
4  *
5  * Copyright (c) 2002 YASAR Mehmet
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include "wine/test.h"
23 #include "winbase.h"
24 #include "winerror.h"
25 #include "winnls.h"
26
27 #define eq(received, expected, label, type) \
28         ok((received) == (expected), "%s: got " type " instead of " type, (label),(received),(expected))
29
30 #define BUFFER_SIZE             50
31 /* Buffer used by callback function */
32 char GlobalBuffer[BUFFER_SIZE];
33
34 /* TODO :
35  * Unicode versions
36  * EnumTimeFormatsA
37  * EnumDateFormatsA
38  * LCMapStringA
39  * GetUserDefaultLangID
40  * ...
41  */
42
43 void TestGetLocaleInfoA()
44 {
45 int ret, cmp;
46 LCID lcid;
47 char buffer[BUFFER_SIZE], Expected[BUFFER_SIZE];
48
49         strcpy(Expected, "Monday");
50         lcid = MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT );
51         ok (lcid == 0x409, "wrong LCID calculated");
52
53         strcpy(Expected, "xxxxx");
54         memset( buffer, 'x', sizeof (buffer)/sizeof(buffer[0]) );
55         ret = GetLocaleInfoA(lcid, LOCALE_SDAYNAME1, buffer, 0);
56         cmp = strncmp (buffer, Expected, strlen(Expected));
57         ok (cmp == 0, "GetLocaleInfoA got %s instead of %s", buffer, Expected);
58         eq (ret, strlen("Monday") + 1, "GetLocaleInfoA with len=0", "%d");
59
60         strcpy(Expected, "Monxx");
61         memset( buffer, 'x', sizeof (buffer)/sizeof(buffer[0]) );
62         ret = GetLocaleInfoA(lcid, LOCALE_SDAYNAME1, buffer, 3);
63         cmp = strncmp (buffer, Expected, strlen(Expected));
64         ok (cmp == 0, "GetLocaleInfoA got %s instead of %s", buffer, Expected);
65         eq (ret, 0, "GetLocaleInfoA with len = 3", "%d");
66
67         strcpy(Expected, "Monday");
68         memset( buffer, 'x', sizeof (buffer)/sizeof(buffer[0]) );
69         ret = GetLocaleInfoA(lcid, LOCALE_SDAYNAME1, buffer, 10);
70         /* We check also presence of '\0' */
71         cmp = strncmp (buffer, Expected, strlen(Expected) + 1);
72         ok (cmp == 0, "GetLocaleInfoA got %s instead of %s", buffer, Expected);
73         eq (ret, strlen(Expected)+1, "GetLocaleInfoA with len = 10", "%d" );
74
75         /* We check the whole buffer with strncmp */
76         memset( Expected, 'x', sizeof (Expected)/sizeof(Expected[0]) );
77         strcpy(Expected, "Monday");
78         memset( buffer, 'x', sizeof (buffer)/sizeof(buffer[0]) );
79         ret = GetLocaleInfoA(lcid, LOCALE_SDAYNAME1, buffer, BUFFER_SIZE);
80         cmp = strncmp (buffer, Expected, BUFFER_SIZE);
81         ok (cmp == 0, "GetLocaleInfoA got %s instead of %s", buffer, Expected);
82         eq (ret, strlen(Expected)+1, "GetLocaleInfoA with len = 10", "%d" );
83
84 }
85
86
87 void TestGetTimeFormatA()
88 {
89 int ret, error, cmp;
90 SYSTEMTIME  curtime;
91 char buffer[BUFFER_SIZE], format[BUFFER_SIZE], Expected[BUFFER_SIZE];
92 LCID lcid;
93
94         lcid = MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT );
95         strcpy(format, "tt HH':'mm'@'ss");
96
97         todo_wine {
98             /* fill curtime with dummy data */
99             memset(&curtime, 2, sizeof(SYSTEMTIME));
100             ret = GetTimeFormatA(lcid, TIME_FORCE24HOURFORMAT, &curtime, format, buffer, sizeof(buffer));
101             error = GetLastError ();
102             ok (ret == 0, "GetTimeFormat should fail on dummy data");
103             eq (error, ERROR_INVALID_PARAMETER, "GetTimeFormat GetLastError()", "%d");
104         }
105
106         strcpy(Expected, "AM 08:56@13");
107         curtime.wHour = 8;  curtime.wMinute = 56;
108         curtime.wSecond = 13; curtime.wMilliseconds = 22;
109         ret = GetTimeFormatA(lcid, TIME_FORCE24HOURFORMAT, &curtime, format, buffer, sizeof(buffer));
110         cmp = strncmp (Expected, buffer, strlen(Expected)+1);
111         ok (cmp == 0, "GetTimeFormat got %s instead of %s", buffer, Expected);
112         eq (ret, strlen(Expected)+1, "GetTimeFormat", "%d");
113
114         /* test with too small buffers */
115         memset(buffer, 'x', sizeof (buffer)/sizeof(buffer[0]) );
116         strcpy(Expected, "xxxx");
117         ret = GetTimeFormatA(lcid, TIME_FORCE24HOURFORMAT, &curtime, format, buffer, 0);
118         cmp = strncmp (Expected, buffer, 4);
119         ok (cmp == 0, "GetTimeFormat with len=0 got %s instead of %s", buffer, Expected);
120         todo_wine { eq (ret, 12, "GetTimeFormat with len=0", "%d"); }
121
122         memset(buffer, 'x', sizeof (buffer)/sizeof(buffer[0]) );
123         strcpy(Expected, "AMxx");
124         ret = GetTimeFormatA(lcid, TIME_FORCE24HOURFORMAT, &curtime, format, buffer, 2);
125         cmp = strncmp (Expected, buffer, 4);
126         todo_wine { ok (cmp == 0, "GetTimeFormat with len=2 got %s instead of %s", buffer, Expected); }
127         eq (ret, 0, "GetTimeFormat with len=2", "%d");
128 }
129
130 void TestGetDateFormatA()
131 {
132 int ret, error, cmp;
133 SYSTEMTIME  curtime;
134 char buffer[BUFFER_SIZE], format[BUFFER_SIZE], Expected[BUFFER_SIZE];
135 LCID lcid;
136
137         lcid = MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT );
138         strcpy(format, "ddd',' MMM dd yy");
139
140         todo_wine {
141             /* fill curtime with dummy data */
142             memset(&curtime, 2, sizeof(SYSTEMTIME));
143             memset(buffer, 'x', sizeof (buffer)/sizeof(buffer[0]) );
144             ret = GetDateFormatA(lcid, 0, &curtime, format, buffer, sizeof(buffer));
145             error = GetLastError ();
146             ok (ret== 0, "GetDateFormat should fail on dummy data");
147             eq (error, ERROR_INVALID_PARAMETER, "GetDateFormat", "%d");
148         }
149
150         strcpy(Expected, "Sat, May 04 02");
151         memset(buffer, 'x', sizeof (buffer)/sizeof(buffer[0]) );
152         curtime.wYear = 2002;
153         curtime.wMonth = 5;
154         curtime.wDay = 4;
155         curtime.wDayOfWeek = 3;
156         ret = GetDateFormatA(lcid, 0, &curtime, format, buffer, sizeof(buffer));
157         cmp = strncmp (Expected, buffer, strlen(Expected)+1);
158         todo_wine { ok (cmp == 0, "GetDateFormat got %s instead of %s", buffer, Expected); }
159         eq (ret, strlen(Expected)+1, "GetDateFormat", "%d");
160
161         /* test format with "'" */
162         memset(buffer, 'x', sizeof (buffer)/sizeof(buffer[0]) );
163         strcpy(format, "ddd',' MMM dd ''''yy");
164         strcpy(Expected, "Sat, May 04 '02");
165         ret = GetDateFormatA(lcid, 0, &curtime, format, buffer, sizeof(buffer));
166         cmp = strncmp (Expected, buffer, strlen(Expected)+1);
167         todo_wine { ok (cmp == 0, "GetDateFormat got %s instead of %s", buffer, Expected); }
168         eq (ret, (strlen(Expected)+1), "GetDateFormat", "%d");
169
170         /* test with too small buffers */
171         memset(buffer, 'x', sizeof (buffer)/sizeof(buffer[0]) );
172         strcpy(Expected, "xxxx");
173         ret = GetDateFormatA(lcid, 0, &curtime, format, buffer, 0);
174         cmp = strncmp (Expected, buffer, 4);
175         ok (cmp == 0, "GetDateFormat got %s instead of %s", buffer, Expected);
176         todo_wine { eq (ret, 16, "GetDateFormat with len=0", "%d"); }
177
178         memset(buffer, 'x', sizeof (buffer)/sizeof(buffer[0]) );
179         strcpy(Expected, "Saxx");
180         ret = GetDateFormatA(lcid, 0, &curtime, format, buffer, 2);
181         cmp = strncmp (Expected, buffer, 4);
182         todo_wine { ok (cmp == 0, "GetDateFormat got %s instead of %s", buffer, Expected); }
183         eq (ret, 0, "GetDateFormat with len=2", "%d");
184 }
185
186
187 void TestGetCurrencyFormat()
188 {
189 int ret, error, cmp;
190 char buffer[BUFFER_SIZE], Expected[BUFFER_SIZE], format[BUFFER_SIZE];
191 LCID lcid;
192
193         lcid = MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT );
194 #if 0
195         lcid = MAKELCID(MAKELANGID(LANG_FRENCH, SUBLANG_DEFAULT), SORT_DEFAULT );
196 #endif
197
198         memset( buffer, 'x', sizeof (buffer)/sizeof(buffer[0]) );
199         ret = GetCurrencyFormatA(lcid, 0, "23,65", NULL, buffer, sizeof(buffer));
200         error = GetLastError ();
201         cmp = strncmp ("xxxx", buffer, 4);
202
203         ok (cmp == 0, "GetCurrencyFormat should fail with ','");
204         eq (ret, 0, "GetCurrencyFormat with ','", "%d");
205         eq (error, ERROR_INVALID_PARAMETER, "GetCurrencyFormat", "%d");
206
207         memset(Expected, 'x', sizeof (Expected)/sizeof(Expected[0]) );
208         strcpy (Expected, "$23.53");
209         strcpy (format, "23.53");
210         memset( buffer, 'x', sizeof (buffer)/sizeof(buffer[0]) );
211         ret = GetCurrencyFormatA(lcid, 0, format, NULL, buffer, 0);
212         cmp = strncmp ("xxx", buffer, 3);
213         ok (cmp == 0, "GetCurrencyFormat with 0 buffer size");
214         eq (ret, strlen(Expected)+1, "GetCurrencyFormat with len=0", "%d");
215
216         memset( buffer, 'x', sizeof (buffer)/sizeof(buffer[0]) );
217         ret = GetCurrencyFormatA(lcid, 0, format, NULL, buffer, 2);
218         cmp = strncmp ("$2xx", buffer, 4);
219         ok (cmp == 0, "GetCurrencyFormat got %s instead of %s", buffer, Expected);
220         eq (ret, 0, "GetCurrencyFormat with len=2", "%d");
221
222         /* We check the whole buffer with strncmp */
223         memset( buffer, 'x', sizeof (buffer)/sizeof(buffer[0]) );
224         ret = GetCurrencyFormatA(lcid, 0, format, NULL, buffer, sizeof(buffer));
225         cmp = strncmp (Expected, buffer, BUFFER_SIZE);
226         ok (cmp == 0, "GetCurrencyFormat got %s instead of %s", buffer, Expected);
227         eq (ret, strlen(Expected)+1, "GetCurrencyFormat","%d");
228
229 }
230
231
232 void TestGetNumberFormat()
233 {
234 int ret, error, cmp;
235 char buffer[BUFFER_SIZE], Expected[BUFFER_SIZE], input[BUFFER_SIZE];
236 LCID lcid;
237
238         lcid = MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT );
239
240         memset( buffer, 'x', sizeof (buffer)/sizeof(buffer[0]) );
241         ret = GetNumberFormatA(lcid, 0, "23,65", NULL, buffer, sizeof(buffer));
242         error = GetLastError ();
243         cmp = strncmp ("xxx", buffer, 3);
244         ok (cmp == 0, "GetCurrencyFormat");
245         ok (ret == 0, "GetNumberFormat should return 0");
246         eq (error, ERROR_INVALID_PARAMETER, "GetNumberFormat", "%d");
247
248         strcpy(input, "2353");
249         strcpy(Expected, "2,353.00");
250         memset( buffer, 'x', sizeof (buffer)/sizeof(buffer[0]) );
251         ret = GetNumberFormatA(lcid, 0, input, NULL, buffer, 0);
252         cmp = strncmp ("xxx", buffer, 3);
253         ok (cmp == 0, "GetNumberFormat with len=0 got %s instead of %s", buffer, "xxx");
254         eq (ret, strlen(Expected)+1, "GetNumberFormat", "%d");
255
256         strcpy(Expected, "2,xx");
257         memset( buffer, 'x', sizeof (buffer)/sizeof(buffer[0]) );
258         ret = GetNumberFormatA(lcid, 0, input, NULL, buffer, 2);
259         cmp = strncmp (Expected, buffer, 4);
260         ok (cmp == 0, "GetNumberFormat with len=2 got %s instead of %s", buffer, Expected);
261         eq (ret, 0, "GetNumberFormat", "%d");
262
263         /* We check the whole buffer with strncmp */
264         memset(Expected, 'x', sizeof (Expected)/sizeof(Expected[0]) );
265         strcpy(Expected, "2,353.00");
266         memset( buffer, 'x', sizeof (buffer)/sizeof(buffer[0]) );
267         ret = GetNumberFormatA(lcid, 0, input, NULL, buffer, sizeof(buffer));
268         cmp = strncmp (Expected, buffer, BUFFER_SIZE);
269         ok (cmp == 0, "GetNumberFormat got %s instead of %s", buffer, Expected);
270         eq (ret, strlen(Expected)+1, "GetNumberFormat", "%d");
271 }
272
273
274 /* Callback function used by TestEnumTimeFormats */
275 BOOL CALLBACK EnumTimeFormatsProc(char * lpTimeFormatString)
276 {
277         trace("%s\n", lpTimeFormatString);
278         strcpy(GlobalBuffer, lpTimeFormatString);
279 #if 0
280         return TRUE;
281 #endif
282         return FALSE;
283 }
284
285 void TestEnumTimeFormats()
286 {
287 int ret;
288 LCID lcid;
289 char Expected[BUFFER_SIZE];
290
291         lcid = MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT );
292         memset( GlobalBuffer, 'x', sizeof (GlobalBuffer)/sizeof(GlobalBuffer[0]) );
293         strcpy(Expected, "h:mm:ss tt");
294         ret = EnumTimeFormatsA(EnumTimeFormatsProc, lcid, 0);
295
296         eq (ret, 1, "EnumTimeFormats should return 1", "%d");
297         ok (strncmp (GlobalBuffer, Expected, strlen(Expected)) == 0,
298                                 "EnumTimeFormats failed");
299         ok (ret == 1, "EnumTimeFormats should return 1");
300 }
301
302
303 void TestCompareStringA()
304 {
305 int ret;
306 LCID lcid;
307 char buffer1[BUFFER_SIZE], buffer2[BUFFER_SIZE];
308
309         lcid = MAKELCID(MAKELANGID(LANG_FRENCH, SUBLANG_DEFAULT), SORT_DEFAULT );
310
311         strcpy(buffer1, "Salut"); strcpy(buffer2, "Salute");
312         ret = CompareStringA(lcid, NORM_IGNORECASE, buffer1, -1, buffer2, -1);
313         ok (ret== 1, "CompareStringA (st1=%s str2=%s) expected result=1", buffer1, buffer2);
314
315         strcpy(buffer1, "Salut"); strcpy(buffer2, "saLuT");
316         ret = CompareStringA(lcid, NORM_IGNORECASE, buffer1, -1, buffer2, -1);
317         ok (ret== 2, "CompareStringA (st1=%s str2=%s) expected result=2", buffer1, buffer2);
318
319         strcpy(buffer1, "Salut"); strcpy(buffer2, "hola");
320         ret = CompareStringA(lcid, NORM_IGNORECASE, buffer1, -1, buffer2, -1);
321         ok (ret== 3, "CompareStringA (st1=%s str2=%s) expected result=3", buffer1, buffer2);
322
323         strcpy(buffer1, "héhé"); strcpy(buffer2, "hèhè");
324         ret = CompareStringA(lcid, NORM_IGNORECASE, buffer1, -1, buffer2, -1);
325         ok (ret== 1, "CompareStringA (st1=%s str2=%s) expected result=1", buffer1, buffer2);
326
327         lcid = MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT );
328
329         strcpy(buffer1, "héhé"); strcpy(buffer2, "hèhè");
330         ret = CompareStringA(lcid, NORM_IGNORECASE, buffer1, -1, buffer2, -1);
331         ok (ret== 1, "CompareStringA (st1=%s str2=%s) expected result=1", buffer1, buffer2);
332
333         ret = CompareStringA(lcid, NORM_IGNORECASE, buffer1, -1, buffer2, 0);
334         ok (ret== 3, "CompareStringA (st1=%s str2=%s) expected result=3", buffer1, buffer2);
335 }
336
337
338 START_TEST(locale)
339 {
340 #if 0
341         TestEnumTimeFormats();
342 #endif
343         TestGetLocaleInfoA();
344         TestGetTimeFormatA();
345         TestGetDateFormatA();
346         TestGetNumberFormat();
347         TestGetCurrencyFormat();
348         TestCompareStringA();
349 }