msvcp90/tests: Added _Getcoll tests.
[wine] / dlls / msvcp90 / tests / misc.c
1 /*
2  * Copyright 2010 Piotr Caban for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include <stdio.h>
20 #include <locale.h>
21
22 #include <windef.h>
23 #include <winbase.h>
24 #include "wine/test.h"
25
26 typedef struct {
27     LCID handle;
28     unsigned page;
29     short *table;
30     int delfl;
31 } MSVCP__Ctypevec;
32
33 typedef struct {
34     LCID handle;
35     unsigned page;
36 } MSVCP__Collvec;
37
38 /* basic_string<char, char_traits<char>, allocator<char>> */
39 #define BUF_SIZE_CHAR 16
40 typedef struct
41 {
42     void *allocator;
43     union {
44         char buf[BUF_SIZE_CHAR];
45         char *ptr;
46     } data;
47     size_t size;
48     size_t res;
49 } basic_string_char;
50
51 static void* (__cdecl *p_set_invalid_parameter_handler)(void*);
52 static _locale_t (__cdecl *p__get_current_locale)(void);
53 static void  (__cdecl *p_free)(void*);
54
55 static void (__cdecl *p_char_assign)(void*, const void*);
56 static void (__cdecl *p_wchar_assign)(void*, const void*);
57 static void (__cdecl *p_short_assign)(void*, const void*);
58
59 static BYTE (__cdecl *p_char_eq)(const void*, const void*);
60 static BYTE (__cdecl *p_wchar_eq)(const void*, const void*);
61 static BYTE (__cdecl *p_short_eq)(const void*, const void*);
62
63 static char* (__cdecl *p_Copy_s)(char*, size_t, const char*, size_t);
64
65 static unsigned short (__cdecl *p_wctype)(const char*);
66 static MSVCP__Ctypevec* (__cdecl *p__Getctype)(MSVCP__Ctypevec*);
67 static /*MSVCP__Collvec*/ULONGLONG (__cdecl *p__Getcoll)(void);
68
69 #undef __thiscall
70 #ifdef __i386__
71 #define __thiscall __stdcall
72 #else
73 #define __thiscall __cdecl
74 #endif
75
76 static char* (__thiscall *p_char_address)(void*, char*);
77 static void* (__thiscall *p_char_ctor)(void*);
78 static void (__thiscall *p_char_deallocate)(void*, char*, size_t);
79 static char* (__thiscall *p_char_allocate)(void*, size_t);
80 static void (__thiscall *p_char_construct)(void*, char*, const char*);
81 static size_t (__thiscall *p_char_max_size)(void*);
82
83 void* (__thiscall *p_collate_char_ctor_refs)(void*, size_t);
84 int (__thiscall *p_collate_char_compare)(const void*, const char*,
85         const char*, const char*, const char*);
86 void (__thiscall *p_collate_char_dtor)(void*);
87 void* (__thiscall *p_numpunct_char_ctor)(void*);
88 basic_string_char* (__thiscall *p_numpunct_char_falsename)(void*,basic_string_char*);
89 void (__thiscall *p_numpunct_char_dtor)(void*);
90 static void (__thiscall *p_basic_string_char_dtor)(basic_string_char*);
91 static const char* (__thiscall *p_basic_string_char_cstr)(basic_string_char*);
92
93 static int invalid_parameter = 0;
94 static void __cdecl test_invalid_parameter_handler(const wchar_t *expression,
95         const wchar_t *function, const wchar_t *file,
96         unsigned line, uintptr_t arg)
97 {
98     ok(expression == NULL, "expression is not NULL\n");
99     ok(function == NULL, "function is not NULL\n");
100     ok(file == NULL, "file is not NULL\n");
101     ok(line == 0, "line = %u\n", line);
102     ok(arg == 0, "arg = %lx\n", (UINT_PTR)arg);
103     invalid_parameter++;
104 }
105
106 /* Emulate a __thiscall */
107 #ifdef __i386__
108
109 #include "pshpack1.h"
110 struct thiscall_thunk
111 {
112     BYTE pop_eax;    /* popl  %eax (ret addr) */
113     BYTE pop_edx;    /* popl  %edx (func) */
114     BYTE pop_ecx;    /* popl  %ecx (this) */
115     BYTE push_eax;   /* pushl %eax */
116     WORD jmp_edx;    /* jmp  *%edx */
117 };
118 #include "poppack.h"
119
120 static void * (WINAPI *call_thiscall_func1)( void *func, void *this );
121 static void * (WINAPI *call_thiscall_func2)( void *func, void *this, const void *a );
122 static void * (WINAPI *call_thiscall_func3)( void *func, void *this, const void *a, const void *b );
123 static void * (WINAPI *call_thiscall_func5)( void *func, void *this, const void *a, const void *b,
124         const void *c, const void *d );
125 struct thiscall_thunk_retptr *thunk_retptr;
126
127 static void init_thiscall_thunk(void)
128 {
129     struct thiscall_thunk *thunk = VirtualAlloc( NULL, sizeof(*thunk),
130                                                  MEM_COMMIT, PAGE_EXECUTE_READWRITE );
131     thunk->pop_eax  = 0x58;   /* popl  %eax */
132     thunk->pop_edx  = 0x5a;   /* popl  %edx */
133     thunk->pop_ecx  = 0x59;   /* popl  %ecx */
134     thunk->push_eax = 0x50;   /* pushl %eax */
135     thunk->jmp_edx  = 0xe2ff; /* jmp  *%edx */
136     call_thiscall_func1 = (void *)thunk;
137     call_thiscall_func2 = (void *)thunk;
138     call_thiscall_func3 = (void *)thunk;
139     call_thiscall_func5 = (void *)thunk;
140 }
141
142 #define call_func1(func,_this) call_thiscall_func1(func,_this)
143 #define call_func2(func,_this,a) call_thiscall_func2(func,_this,(const void*)(a))
144 #define call_func3(func,_this,a,b) call_thiscall_func3(func,_this,(const void*)(a),(const void*)(b))
145 #define call_func5(func,_this,a,b,c,d) call_thiscall_func5(func,_this,(const void*)(a),(const void*)(b), \
146         (const void*)(c), (const void *)(d))
147
148 #else
149
150 #define init_thiscall_thunk()
151 #define call_func1(func,_this) func(_this)
152 #define call_func2(func,_this,a) func(_this,a)
153 #define call_func3(func,_this,a,b) func(_this,a,b)
154 #define call_func5(func,_this,a,b,c,d) func(_this,a,b,c,d)
155
156 #endif /* __i386__ */
157
158 #define SETNOFAIL(x,y) x = (void*)GetProcAddress(msvcp,y)
159 #define SET(x,y) do { SETNOFAIL(x,y); ok(x != NULL, "Export '%s' not found\n", y); } while(0)
160 static BOOL init(void)
161 {
162     HMODULE msvcr = LoadLibraryA("msvcr90.dll");
163     HMODULE msvcp = LoadLibraryA("msvcp90.dll");
164     if(!msvcr || !msvcp) {
165         win_skip("msvcp90.dll or msvcrt90.dll not installed\n");
166         return FALSE;
167     }
168
169     p_set_invalid_parameter_handler = (void*)GetProcAddress(msvcr, "_set_invalid_parameter_handler");
170     p__get_current_locale = (void*)GetProcAddress(msvcr, "_get_current_locale");
171     p_free = (void*)GetProcAddress(msvcr, "free");
172     if(!p_set_invalid_parameter_handler || !p__get_current_locale || !p_free) {
173         win_skip("Error setting tests environment\n");
174         return FALSE;
175     }
176
177     p_set_invalid_parameter_handler(test_invalid_parameter_handler);
178
179     SET(p_wctype, "wctype");
180     SET(p__Getctype, "_Getctype");
181     SET(p__Getcoll, "_Getcoll");
182     if(sizeof(void*) == 8) { /* 64-bit initialization */
183         SET(p_char_assign, "?assign@?$char_traits@D@std@@SAXAEADAEBD@Z");
184         SET(p_wchar_assign, "?assign@?$char_traits@_W@std@@SAXAEA_WAEB_W@Z");
185         SET(p_short_assign, "?assign@?$char_traits@G@std@@SAXAEAGAEBG@Z");
186
187         SET(p_char_eq, "?eq@?$char_traits@D@std@@SA_NAEBD0@Z");
188         SET(p_wchar_eq, "?eq@?$char_traits@_W@std@@SA_NAEB_W0@Z");
189         SET(p_short_eq, "?eq@?$char_traits@G@std@@SA_NAEBG0@Z");
190
191         SET(p_Copy_s, "?_Copy_s@?$char_traits@D@std@@SAPEADPEAD_KPEBD1@Z");
192
193         SET(p_char_address, "?address@?$allocator@D@std@@QEBAPEADAEAD@Z");
194         SET(p_char_ctor, "??0?$allocator@D@std@@QEAA@XZ");
195         SET(p_char_deallocate, "?deallocate@?$allocator@D@std@@QEAAXPEAD_K@Z");
196         SET(p_char_allocate, "?allocate@?$allocator@D@std@@QEAAPEAD_K@Z");
197         SET(p_char_construct, "?construct@?$allocator@D@std@@QEAAXPEADAEBD@Z");
198         SET(p_char_max_size, "?max_size@?$allocator@D@std@@QEBA_KXZ");
199
200         SET(p_collate_char_ctor_refs, "??0?$collate@D@std@@QEAA@_K@Z");
201         SET(p_collate_char_compare, "?compare@?$collate@D@std@@QEBAHPEBD000@Z");
202         SET(p_collate_char_dtor, "??1?$collate@D@std@@MEAA@XZ");
203         SET(p_numpunct_char_ctor, "??_F?$numpunct@D@std@@QEAAXXZ");
204         SET(p_numpunct_char_falsename, "?falsename@?$numpunct@D@std@@QEBA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@2@XZ");
205         SET(p_numpunct_char_dtor, "??1?$numpunct@D@std@@MEAA@XZ");
206         SET(p_basic_string_char_dtor,
207                 "??1?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QEAA@XZ");
208         SET(p_basic_string_char_cstr,
209                 "?c_str@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QEBAPEBDXZ");
210     } else {
211         SET(p_char_assign, "?assign@?$char_traits@D@std@@SAXAADABD@Z");
212         SET(p_wchar_assign, "?assign@?$char_traits@_W@std@@SAXAA_WAB_W@Z");
213         SET(p_short_assign, "?assign@?$char_traits@G@std@@SAXAAGABG@Z");
214
215         SET(p_char_eq, "?eq@?$char_traits@D@std@@SA_NABD0@Z");
216         SET(p_wchar_eq, "?eq@?$char_traits@_W@std@@SA_NAB_W0@Z");
217         SET(p_short_eq, "?eq@?$char_traits@G@std@@SA_NABG0@Z");
218
219         SET(p_Copy_s, "?_Copy_s@?$char_traits@D@std@@SAPADPADIPBDI@Z");
220
221         SET(p_char_address, "?address@?$allocator@D@std@@QBEPADAAD@Z");
222         SET(p_char_ctor, "??0?$allocator@D@std@@QAE@XZ");
223         SET(p_char_deallocate, "?deallocate@?$allocator@D@std@@QAEXPADI@Z");
224         SET(p_char_allocate, "?allocate@?$allocator@D@std@@QAEPADI@Z");
225         SET(p_char_construct, "?construct@?$allocator@D@std@@QAEXPADABD@Z");
226         SET(p_char_max_size, "?max_size@?$allocator@D@std@@QBEIXZ");
227
228         SET(p_collate_char_ctor_refs, "??0?$collate@D@std@@QAE@I@Z");
229         SET(p_collate_char_compare, "?compare@?$collate@D@std@@QBEHPBD000@Z");
230         SET(p_collate_char_dtor, "??1?$collate@D@std@@MAE@XZ");
231         SET(p_numpunct_char_ctor, "??_F?$numpunct@D@std@@QAEXXZ");
232         SET(p_numpunct_char_falsename, "?falsename@?$numpunct@D@std@@QBE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@2@XZ");
233         SET(p_numpunct_char_dtor, "??1?$numpunct@D@std@@MAE@XZ");
234         SET(p_basic_string_char_dtor,
235                 "??1?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAE@XZ");
236         SET(p_basic_string_char_cstr,
237                 "?c_str@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QBEPBDXZ");
238     }
239
240     init_thiscall_thunk();
241     return TRUE;
242 }
243
244 static void test_assign(void)
245 {
246     const char in[] = "abc";
247     char out[4];
248
249     out[1] = '#';
250     p_char_assign(out, in);
251     ok(out[0] == in[0], "out[0] = %c\n", out[0]);
252     ok(out[1] == '#', "out[1] = %c\n", out[1]);
253
254     out[2] = '#';
255     p_wchar_assign(out, in);
256     ok(*((char*)out)==in[0] && *((char*)out+1)==in[1],
257             "out[0] = %d, out[1] = %d\n", (int)out[0], (int)out[1]);
258     ok(out[2] == '#', "out[2] = %c\n", out[2]);
259
260     out[2] = '#';
261     p_short_assign(out, in);
262     ok(*((char*)out)==in[0] && *((char*)out+1)==in[1],
263             "out[0] = %d, out[1] = %d\n", (int)out[0], (int)out[1]);
264     ok(out[2] == '#', "out[2] = %c\n", out[2]);
265 }
266
267 static void test_equal(void)
268 {
269     static const char in1[] = "abc";
270     static const char in2[] = "ab";
271     static const char in3[] = "a";
272     static const char in4[] = "b";
273     BYTE ret;
274
275     ret = p_char_eq(in1, in2);
276     ok(ret == TRUE, "ret = %d\n", (int)ret);
277     ret = p_char_eq(in1, in3);
278     ok(ret == TRUE, "ret = %d\n", (int)ret);
279     ret = p_char_eq(in1, in4);
280     ok(ret == FALSE, "ret = %d\n", (int)ret);
281
282     ret = p_wchar_eq(in1, in2);
283     ok(ret == TRUE, "ret = %d\n", (int)ret);
284     ret = p_wchar_eq(in1, in3);
285     ok(ret == FALSE, "ret = %d\n", (int)ret);
286     ret = p_wchar_eq(in1, in4);
287     ok(ret == FALSE, "ret = %d\n", (int)ret);
288
289     ret = p_short_eq(in1, in2);
290     ok(ret == TRUE, "ret = %d\n", (int)ret);
291     ret = p_short_eq(in1, in3);
292     ok(ret == FALSE, "ret = %d\n", (int)ret);
293     ret = p_short_eq(in1, in4);
294     ok(ret == FALSE, "ret = %d\n", (int)ret);
295 }
296
297 static void test_Copy_s(void)
298 {
299     static const char src[] = "abcd";
300     char dest[32], *ret;
301
302     dest[4] = '#';
303     dest[5] = '\0';
304     ret = p_Copy_s(dest, 4, src, 4);
305     ok(ret == dest, "ret != dest\n");
306     ok(dest[4] == '#', "dest[4] != '#'\n");
307     ok(!memcmp(dest, src, sizeof(char[4])), "dest = %s\n", dest);
308
309     ret = p_Copy_s(dest, 32, src, 4);
310     ok(ret == dest, "ret != dest\n");
311     ok(dest[4] == '#', "dest[4] != '#'\n");
312     ok(!memcmp(dest, src, sizeof(char[4])), "dest = %s\n", dest);
313
314     errno = 0xdeadbeef;
315     dest[0] = '#';
316     ret = p_Copy_s(dest, 3, src, 4);
317     ok(ret == dest, "ret != dest\n");
318     ok(dest[0] == '\0', "dest[0] != 0\n");
319     ok(invalid_parameter==1, "invalid_parameter = %d\n",
320             invalid_parameter);
321     invalid_parameter = 0;
322     ok(errno == 0xdeadbeef, "errno = %d\n", errno);
323
324     errno = 0xdeadbeef;
325     p_Copy_s(NULL, 32, src, 4);
326     ok(invalid_parameter==1, "invalid_parameter = %d\n",
327             invalid_parameter);
328     invalid_parameter = 0;
329     ok(errno == 0xdeadbeef, "errno = %d\n", errno);
330
331     errno = 0xdeadbeef;
332     p_Copy_s(dest, 32, NULL, 4);
333     ok(invalid_parameter==1, "invalid_parameter = %d\n",
334             invalid_parameter);
335     invalid_parameter = 0;
336     ok(errno == 0xdeadbeef, "errno = %d\n", errno);
337 }
338
339 static void test_wctype(void)
340 {
341     static const struct {
342         const char *name;
343         unsigned short mask;
344     } properties[] = {
345         { "alnum",  0x107 },
346         { "alpha",  0x103 },
347         { "cntrl",  0x020 },
348         { "digit",  0x004 },
349         { "graph",  0x117 },
350         { "lower",  0x002 },
351         { "print",  0x157 },
352         { "punct",  0x010 },
353         { "space",  0x008 },
354         { "upper",  0x001 },
355         { "xdigit", 0x080 },
356         { "ALNUM",  0x000 },
357         { "Alnum",  0x000 },
358         { "",  0x000 }
359     };
360     int i, ret;
361
362     for(i=0; i<sizeof(properties)/sizeof(properties[0]); i++) {
363         ret = p_wctype(properties[i].name);
364         ok(properties[i].mask == ret, "%d - Expected %x, got %x\n", i, properties[i].mask, ret);
365     }
366 }
367
368 static void test__Getctype(void)
369 {
370     MSVCP__Ctypevec ret;
371
372     ok(p__Getctype(&ret) == &ret, "__Getctype returned incorrect pointer\n");
373     ok(ret.handle == 0, "ret.handle = %d\n", ret.handle);
374     ok(ret.page == 0, "ret.page = %d\n", ret.page);
375     ok(ret.delfl == 1, "ret.delfl = %d\n", ret.delfl);
376     ok(ret.table[0] == 32, "ret.table[0] = %d\n", ret.table[0]);
377     p_free(ret.table);
378
379     p__get_current_locale()->locinfo->lc_handle[LC_COLLATE] = 0x1234567;
380     ok(p__Getctype(&ret) == &ret, "__Getctype returned incorrect pointer\n");
381     ok(ret.handle == 0x1234567, "ret.handle = %d\n", ret.handle);
382     ok(ret.page == 0, "ret.page = %d\n", ret.page);
383     ok(ret.delfl == 1, "ret.delfl = %d\n", ret.delfl);
384     ok(ret.table[0] == 32, "ret.table[0] = %d\n", ret.table[0]);
385     p_free(ret.table);
386 }
387
388 static void test__Getcoll(void)
389 {
390     ULONGLONG (__cdecl *p__Getcoll_arg)(MSVCP__Collvec*);
391
392     union {
393         MSVCP__Collvec collvec;
394         ULONGLONG ull;
395     }ret;
396
397     p__get_current_locale()->locinfo->lc_handle[LC_COLLATE] = 0x7654321;
398     ret.ull = 0;
399     p__Getcoll_arg = (void*)p__Getcoll;
400     p__Getcoll_arg(&ret.collvec);
401     ok(ret.collvec.handle == 0, "ret.handle = %x\n", ret.collvec.handle);
402     ok(ret.collvec.page == 0, "ret.page = %x\n", ret.collvec.page);
403
404     ret.ull = p__Getcoll();
405     ok(ret.collvec.handle == 0x7654321, "ret.collvec.handle = %x\n", ret.collvec.handle);
406     ok(ret.collvec.page == 0, "ret.page = %x\n", ret.collvec.page);
407 }
408
409 static void test_allocator_char(void)
410 {
411     void *allocator = (void*)0xdeadbeef;
412     char *ptr;
413     char val;
414     unsigned int size;
415
416     allocator = call_func1(p_char_ctor, allocator);
417     ok(allocator == (void*)0xdeadbeef, "allocator = %p\n", allocator);
418
419     ptr = call_func2(p_char_address, NULL, (void*)0xdeadbeef);
420     ok(ptr == (void*)0xdeadbeef, "incorrect address (%p)\n", ptr);
421
422     ptr = NULL;
423     ptr = call_func2(p_char_allocate, allocator, 10);
424     ok(ptr != NULL, "Memory allocation failed\n");
425
426     ptr[0] = ptr[1] = '#';
427     val = 'a';
428     call_func3(p_char_construct, allocator, ptr, &val);
429     val = 'b';
430     call_func3(p_char_construct, allocator, (ptr+1), &val);
431     ok(ptr[0] == 'a', "ptr[0] = %c\n", ptr[0]);
432     ok(ptr[1] == 'b', "ptr[1] = %c\n", ptr[1]);
433
434     call_func3(p_char_deallocate, allocator, ptr, -1);
435
436     size = (unsigned int)call_func1(p_char_max_size, allocator);
437     ok(size == (unsigned int)0xffffffff, "size = %x\n", size);
438 }
439
440 static void test_virtual_call(void)
441 {
442     BYTE this[256];
443     basic_string_char bstr;
444     const char *p;
445     char str1[] = "test";
446     char str2[] = "TEST";
447     int ret;
448
449     p__get_current_locale()->locinfo->lc_handle[LC_COLLATE] = 1;
450     call_func2(p_collate_char_ctor_refs, this, 0);
451     ret = (int)call_func5(p_collate_char_compare, this, str1, str1+4, str1, str1+4);
452     ok(ret == 0, "collate<char>::compare returned %d\n", ret);
453     ret = (int)call_func5(p_collate_char_compare, this, str2, str2+4, str1, str1+4);
454     ok(ret == 1, "collate<char>::compare returned %d\n", ret);
455     ret = (int)call_func5(p_collate_char_compare, this, str1, str1+3, str1, str1+4);
456     ok(ret == -1, "collate<char>::compare returned %d\n", ret);
457     call_func1(p_collate_char_dtor, this);
458
459     call_func1(p_numpunct_char_ctor, this);
460     call_func2(p_numpunct_char_falsename, this, &bstr);
461     p = call_func1(p_basic_string_char_cstr, &bstr);
462     ok(!strcmp(p, "false"), "numpunct<char>::falsename returned %s\n", p);
463     call_func1(p_basic_string_char_dtor, &bstr);
464     call_func1(p_numpunct_char_dtor, this);
465 }
466
467 START_TEST(misc)
468 {
469     if(!init())
470         return;
471
472     test_assign();
473     test_equal();
474     test_Copy_s();
475     test_wctype();
476     test__Getctype();
477     test__Getcoll();
478     test_virtual_call();
479
480     test_allocator_char();
481
482     ok(!invalid_parameter, "invalid_parameter_handler was invoked too many times\n");
483 }