2 * Copyright 2010 Piotr Caban for CodeWeavers
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.
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.
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
24 #include "wine/test.h"
38 /* basic_string<char, char_traits<char>, allocator<char>> */
39 #define BUF_SIZE_CHAR 16
44 char buf[BUF_SIZE_CHAR];
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*);
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*);
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*);
63 static char* (__cdecl *p_Copy_s)(char*, size_t, const char*, size_t);
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);
71 #define __thiscall __stdcall
73 #define __thiscall __cdecl
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*);
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*);
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)
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);
106 /* Emulate a __thiscall */
109 #include "pshpack1.h"
110 struct thiscall_thunk
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 */
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;
127 static void init_thiscall_thunk(void)
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;
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))
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)
156 #endif /* __i386__ */
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)
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");
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");
177 p_set_invalid_parameter_handler(test_invalid_parameter_handler);
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");
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");
191 SET(p_Copy_s, "?_Copy_s@?$char_traits@D@std@@SAPEADPEAD_KPEBD1@Z");
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");
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");
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");
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");
219 SET(p_Copy_s, "?_Copy_s@?$char_traits@D@std@@SAPADPADIPBDI@Z");
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");
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");
240 init_thiscall_thunk();
244 static void test_assign(void)
246 const char in[] = "abc";
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]);
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]);
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]);
267 static void test_equal(void)
269 static const char in1[] = "abc";
270 static const char in2[] = "ab";
271 static const char in3[] = "a";
272 static const char in4[] = "b";
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);
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);
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);
297 static void test_Copy_s(void)
299 static const char src[] = "abcd";
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);
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);
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",
321 invalid_parameter = 0;
322 ok(errno == 0xdeadbeef, "errno = %d\n", errno);
325 p_Copy_s(NULL, 32, src, 4);
326 ok(invalid_parameter==1, "invalid_parameter = %d\n",
328 invalid_parameter = 0;
329 ok(errno == 0xdeadbeef, "errno = %d\n", errno);
332 p_Copy_s(dest, 32, NULL, 4);
333 ok(invalid_parameter==1, "invalid_parameter = %d\n",
335 invalid_parameter = 0;
336 ok(errno == 0xdeadbeef, "errno = %d\n", errno);
339 static void test_wctype(void)
341 static const struct {
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);
368 static void test__Getctype(void)
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]);
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]);
388 static void test__Getcoll(void)
390 ULONGLONG (__cdecl *p__Getcoll_arg)(MSVCP__Collvec*);
393 MSVCP__Collvec collvec;
397 p__get_current_locale()->locinfo->lc_handle[LC_COLLATE] = 0x7654321;
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);
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);
409 static void test_allocator_char(void)
411 void *allocator = (void*)0xdeadbeef;
416 allocator = call_func1(p_char_ctor, allocator);
417 ok(allocator == (void*)0xdeadbeef, "allocator = %p\n", allocator);
419 ptr = call_func2(p_char_address, NULL, (void*)0xdeadbeef);
420 ok(ptr == (void*)0xdeadbeef, "incorrect address (%p)\n", ptr);
423 ptr = call_func2(p_char_allocate, allocator, 10);
424 ok(ptr != NULL, "Memory allocation failed\n");
426 ptr[0] = ptr[1] = '#';
428 call_func3(p_char_construct, allocator, ptr, &val);
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]);
434 call_func3(p_char_deallocate, allocator, ptr, -1);
436 size = (unsigned int)call_func1(p_char_max_size, allocator);
437 ok(size == (unsigned int)0xffffffff, "size = %x\n", size);
440 static void test_virtual_call(void)
443 basic_string_char bstr;
445 char str1[] = "test";
446 char str2[] = "TEST";
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);
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);
480 test_allocator_char();
482 ok(!invalid_parameter, "invalid_parameter_handler was invoked too many times\n");