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"
33 /* basic_string<char, char_traits<char>, allocator<char>> */
34 #define BUF_SIZE_CHAR 16
39 char buf[BUF_SIZE_CHAR];
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*);
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*);
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*);
58 static char* (__cdecl *p_Copy_s)(char*, size_t, const char*, size_t);
60 static unsigned short (__cdecl *p_wctype)(const char*);
61 static MSVCP__Ctypevec (__cdecl *p__Getctype)(void);
65 #define __thiscall __stdcall
67 #define __thiscall __cdecl
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*);
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*);
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)
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);
100 /* Emulate a __thiscall */
103 #include "pshpack1.h"
104 struct thiscall_thunk
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 */
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;
121 static void init_thiscall_thunk(void)
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;
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))
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)
150 #endif /* __i386__ */
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)
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");
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");
171 p_set_invalid_parameter_handler(test_invalid_parameter_handler);
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");
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");
184 SET(p_Copy_s, "?_Copy_s@?$char_traits@D@std@@SAPEADPEAD_KPEBD1@Z");
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");
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");
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");
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");
212 SET(p_Copy_s, "?_Copy_s@?$char_traits@D@std@@SAPADPADIPBDI@Z");
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");
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");
233 init_thiscall_thunk();
237 static void test_assign(void)
239 const char in[] = "abc";
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]);
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]);
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]);
260 static void test_equal(void)
262 static const char in1[] = "abc";
263 static const char in2[] = "ab";
264 static const char in3[] = "a";
265 static const char in4[] = "b";
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);
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);
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);
290 static void test_Copy_s(void)
292 static const char src[] = "abcd";
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);
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);
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",
314 invalid_parameter = 0;
315 ok(errno == 0xdeadbeef, "errno = %d\n", errno);
318 p_Copy_s(NULL, 32, src, 4);
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(dest, 32, NULL, 4);
326 ok(invalid_parameter==1, "invalid_parameter = %d\n",
328 invalid_parameter = 0;
329 ok(errno == 0xdeadbeef, "errno = %d\n", errno);
332 static void test_wctype(void)
334 static const struct {
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);
361 static void test__Getctype(void)
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]);
372 p__get_current_locale()->locinfo->lc_handle[LC_COLLATE] = 1;
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]);
381 static void test_allocator_char(void)
383 void *allocator = (void*)0xdeadbeef;
388 allocator = call_func1(p_char_ctor, allocator);
389 ok(allocator == (void*)0xdeadbeef, "allocator = %p\n", allocator);
391 ptr = call_func2(p_char_address, NULL, (void*)0xdeadbeef);
392 ok(ptr == (void*)0xdeadbeef, "incorrect address (%p)\n", ptr);
395 ptr = call_func2(p_char_allocate, allocator, 10);
396 ok(ptr != NULL, "Memory allocation failed\n");
398 ptr[0] = ptr[1] = '#';
400 call_func3(p_char_construct, allocator, ptr, &val);
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]);
406 call_func3(p_char_deallocate, allocator, ptr, -1);
408 size = (unsigned int)call_func1(p_char_max_size, allocator);
409 ok(size == (unsigned int)0xffffffff, "size = %x\n", size);
412 static void test_virtual_call(void)
415 basic_string_char bstr;
417 char str1[] = "test";
418 char str2[] = "TEST";
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);
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);
450 test_allocator_char();
452 ok(!invalid_parameter, "invalid_parameter_handler was invoked too many times\n");