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