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