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);
64 #define __thiscall __stdcall
66 #define __thiscall __cdecl
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*);
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*);
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)
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);
99 /* Emulate a __thiscall */
102 #include "pshpack1.h"
103 struct thiscall_thunk
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 */
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;
120 static void init_thiscall_thunk(void)
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;
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))
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)
149 #endif /* __i386__ */
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)
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");
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");
170 p_set_invalid_parameter_handler(test_invalid_parameter_handler);
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");
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");
183 SET(p_Copy_s, "?_Copy_s@?$char_traits@D@std@@SAPEADPEAD_KPEBD1@Z");
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");
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");
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");
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");
211 SET(p_Copy_s, "?_Copy_s@?$char_traits@D@std@@SAPADPADIPBDI@Z");
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");
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");
232 init_thiscall_thunk();
236 static void test_assign(void)
238 const char in[] = "abc";
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]);
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]);
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]);
259 static void test_equal(void)
261 static const char in1[] = "abc";
262 static const char in2[] = "ab";
263 static const char in3[] = "a";
264 static const char in4[] = "b";
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);
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);
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);
289 static void test_Copy_s(void)
291 static const char src[] = "abcd";
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);
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);
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",
313 invalid_parameter = 0;
314 ok(errno == 0xdeadbeef, "errno = %d\n", errno);
317 p_Copy_s(NULL, 32, src, 4);
318 ok(invalid_parameter==1, "invalid_parameter = %d\n",
320 invalid_parameter = 0;
321 ok(errno == 0xdeadbeef, "errno = %d\n", errno);
324 p_Copy_s(dest, 32, NULL, 4);
325 ok(invalid_parameter==1, "invalid_parameter = %d\n",
327 invalid_parameter = 0;
328 ok(errno == 0xdeadbeef, "errno = %d\n", errno);
331 static void test_wctype(void)
333 static const struct {
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);
360 static void test__Getctype(void)
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]);
371 p__get_current_locale()->locinfo->lc_handle[LC_COLLATE] = 1;
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]);
380 static void test_allocator_char(void)
382 void *allocator = (void*)0xdeadbeef;
387 allocator = call_func1(p_char_ctor, allocator);
388 ok(allocator == (void*)0xdeadbeef, "allocator = %p\n", allocator);
390 ptr = call_func2(p_char_address, NULL, (void*)0xdeadbeef);
391 ok(ptr == (void*)0xdeadbeef, "incorrect address (%p)\n", ptr);
394 ptr = call_func2(p_char_allocate, allocator, 10);
395 ok(ptr != NULL, "Memory allocation failed\n");
397 ptr[0] = ptr[1] = '#';
399 call_func3(p_char_construct, allocator, ptr, &val);
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]);
405 call_func3(p_char_deallocate, allocator, ptr, -1);
407 size = (unsigned int)call_func1(p_char_max_size, allocator);
408 ok(size == (unsigned int)0xffffffff, "size = %x\n", size);
411 static void test_virtual_call(void)
414 basic_string_char bstr;
416 char str1[] = "test";
417 char str2[] = "TEST";
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);
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);
449 test_allocator_char();
451 ok(!invalid_parameter, "invalid_parameter_handler was invoked too many times\n");