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 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*);
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*);
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*);
45 static char* (__cdecl *p_Copy_s)(char*, size_t, const char*, size_t);
47 static unsigned short (__cdecl *p_wctype)(const char*);
48 static MSVCP__Ctypevec (__cdecl *p__Getctype)(void);
51 #define __thiscall __stdcall
53 #define __thiscall __cdecl
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*);
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)
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);
76 /* Emulate a __thiscall */
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 */
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 );
94 static void init_thiscall_thunk(void)
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;
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)
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)
119 #endif /* __i386__ */
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)
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");
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");
140 p_set_invalid_parameter_handler(test_invalid_parameter_handler);
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");
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");
153 SET(p_Copy_s, "?_Copy_s@?$char_traits@D@std@@SAPEADPEAD_KPEBD1@Z");
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");
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");
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");
170 SET(p_Copy_s, "?_Copy_s@?$char_traits@D@std@@SAPADPADIPBDI@Z");
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");
180 init_thiscall_thunk();
184 static void test_assign(void)
186 const char in[] = "abc";
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]);
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]);
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]);
207 static void test_equal(void)
209 static const char in1[] = "abc";
210 static const char in2[] = "ab";
211 static const char in3[] = "a";
212 static const char in4[] = "b";
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);
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);
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);
237 static void test_Copy_s(void)
239 static const char src[] = "abcd";
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);
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);
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",
261 invalid_parameter = 0;
262 ok(errno == 0xdeadbeef, "errno = %d\n", errno);
265 p_Copy_s(NULL, 32, src, 4);
266 ok(invalid_parameter==1, "invalid_parameter = %d\n",
268 invalid_parameter = 0;
269 ok(errno == 0xdeadbeef, "errno = %d\n", errno);
272 p_Copy_s(dest, 32, NULL, 4);
273 ok(invalid_parameter==1, "invalid_parameter = %d\n",
275 invalid_parameter = 0;
276 ok(errno == 0xdeadbeef, "errno = %d\n", errno);
279 static void test_wctype(void)
281 static const struct {
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);
308 static void test__Getctype(void)
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]);
319 p__get_current_locale()->locinfo->lc_handle[LC_COLLATE] = 1;
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]);
328 static void test_allocator_char(void)
330 void *allocator = (void*)0xdeadbeef;
335 allocator = call_func1(p_char_ctor, allocator);
336 ok(allocator == (void*)0xdeadbeef, "allocator = %p\n", allocator);
338 ptr = call_func2(p_char_address, NULL, (void*)0xdeadbeef);
339 ok(ptr == (void*)0xdeadbeef, "incorrect address (%p)\n", ptr);
342 ptr = call_func2(p_char_allocate, allocator, 10);
343 ok(ptr != NULL, "Memory allocation failed\n");
345 ptr[0] = ptr[1] = '#';
347 call_func3(p_char_construct, allocator, ptr, &val);
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]);
353 call_func3(p_char_deallocate, allocator, ptr, -1);
355 size = (unsigned int)call_func1(p_char_max_size, allocator);
356 ok(size == (unsigned int)0xffffffff, "size = %x\n", size);
370 test_allocator_char();
372 ok(!invalid_parameter, "invalid_parameter_handler was invoked too many times\n");