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