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
23 #include "wine/test.h"
25 static void* (__cdecl *p_set_invalid_parameter_handler)(void*);
27 static void (__cdecl *p_char_assign)(void*, const void*);
28 static void (__cdecl *p_wchar_assign)(void*, const void*);
29 static void (__cdecl *p_short_assign)(void*, const void*);
31 static BYTE (__cdecl *p_char_eq)(const void*, const void*);
32 static BYTE (__cdecl *p_wchar_eq)(const void*, const void*);
33 static BYTE (__cdecl *p_short_eq)(const void*, const void*);
35 static char* (__cdecl *p_Copy_s)(char*, size_t, const char*, size_t);
37 static unsigned short (__cdecl *p_wctype)(const char*);
40 #define __thiscall __stdcall
42 #define __thiscall __cdecl
45 static char* (__thiscall *p_char_address)(void*, char*);
46 static void* (__thiscall *p_char_ctor)(void*);
47 static void (__thiscall *p_char_deallocate)(void*, char*, size_t);
48 static char* (__thiscall *p_char_allocate)(void*, size_t);
49 static void (__thiscall *p_char_construct)(void*, char*, const char*);
50 static size_t (__thiscall *p_char_max_size)(void*);
52 static int invalid_parameter = 0;
53 static void __cdecl test_invalid_parameter_handler(const wchar_t *expression,
54 const wchar_t *function, const wchar_t *file,
55 unsigned line, uintptr_t arg)
57 ok(expression == NULL, "expression is not NULL\n");
58 ok(function == NULL, "function is not NULL\n");
59 ok(file == NULL, "file is not NULL\n");
60 ok(line == 0, "line = %u\n", line);
61 ok(arg == 0, "arg = %lx\n", (UINT_PTR)arg);
65 /* Emulate a __thiscall */
71 BYTE pop_eax; /* popl %eax (ret addr) */
72 BYTE pop_edx; /* popl %edx (func) */
73 BYTE pop_ecx; /* popl %ecx (this) */
74 BYTE push_eax; /* pushl %eax */
75 WORD jmp_edx; /* jmp *%edx */
79 static void * (WINAPI *call_thiscall_func1)( void *func, void *this );
80 static void * (WINAPI *call_thiscall_func2)( void *func, void *this, const void *a );
81 static void * (WINAPI *call_thiscall_func3)( void *func, void *this, const void *a, const void *b );
83 static void init_thiscall_thunk(void)
85 struct thiscall_thunk *thunk = VirtualAlloc( NULL, sizeof(*thunk),
86 MEM_COMMIT, PAGE_EXECUTE_READWRITE );
87 thunk->pop_eax = 0x58; /* popl %eax */
88 thunk->pop_edx = 0x5a; /* popl %edx */
89 thunk->pop_ecx = 0x59; /* popl %ecx */
90 thunk->push_eax = 0x50; /* pushl %eax */
91 thunk->jmp_edx = 0xe2ff; /* jmp *%edx */
92 call_thiscall_func1 = (void *)thunk;
93 call_thiscall_func2 = (void *)thunk;
94 call_thiscall_func3 = (void *)thunk;
97 #define call_func1(func,_this) call_thiscall_func1(func,_this)
98 #define call_func2(func,_this,a) call_thiscall_func2(func,_this,(const void*)a)
99 #define call_func3(func,_this,a,b) call_thiscall_func3(func,_this,(const void*)a,(const void*)b)
103 #define init_thiscall_thunk()
104 #define call_func1(func,_this) func(_this)
105 #define call_func2(func,_this,a) func(_this,a)
106 #define call_func3(func,_this,a,b) func(_this,a,b)
108 #endif /* __i386__ */
110 #define SETNOFAIL(x,y) x = (void*)GetProcAddress(msvcp,y)
111 #define SET(x,y) do { SETNOFAIL(x,y); ok(x != NULL, "Export '%s' not found\n", y); } while(0)
112 static BOOL init(void)
114 HMODULE msvcr = LoadLibraryA("msvcr90.dll");
115 HMODULE msvcp = LoadLibraryA("msvcp90.dll");
116 if(!msvcr || !msvcp) {
117 win_skip("msvcp90.dll or msvcrt90.dll not installed\n");
121 p_set_invalid_parameter_handler = (void*)GetProcAddress(msvcr, "_set_invalid_parameter_handler");
122 if(!p_set_invalid_parameter_handler) {
123 win_skip("Error setting tests environment\n");
127 p_set_invalid_parameter_handler(test_invalid_parameter_handler);
129 SET(p_wctype, "wctype");
130 if(sizeof(void*) == 8) { /* 64-bit initialization */
131 SET(p_char_assign, "?assign@?$char_traits@D@std@@SAXAEADAEBD@Z");
132 SET(p_wchar_assign, "?assign@?$char_traits@_W@std@@SAXAEA_WAEB_W@Z");
133 SET(p_short_assign, "?assign@?$char_traits@G@std@@SAXAEAGAEBG@Z");
135 SET(p_char_eq, "?eq@?$char_traits@D@std@@SA_NAEBD0@Z");
136 SET(p_wchar_eq, "?eq@?$char_traits@_W@std@@SA_NAEB_W0@Z");
137 SET(p_short_eq, "?eq@?$char_traits@G@std@@SA_NAEBG0@Z");
139 SET(p_Copy_s, "?_Copy_s@?$char_traits@D@std@@SAPEADPEAD_KPEBD1@Z");
141 SET(p_char_address, "?address@?$allocator@D@std@@QEBAPEADAEAD@Z");
142 SET(p_char_ctor, "??0?$allocator@D@std@@QEAA@XZ");
143 SET(p_char_deallocate, "?deallocate@?$allocator@D@std@@QEAAXPEAD_K@Z");
144 SET(p_char_allocate, "?allocate@?$allocator@D@std@@QEAAPEAD_K@Z");
145 SET(p_char_construct, "?construct@?$allocator@D@std@@QEAAXPEADAEBD@Z");
146 SET(p_char_max_size, "?max_size@?$allocator@D@std@@QEBA_KXZ");
148 SET(p_char_assign, "?assign@?$char_traits@D@std@@SAXAADABD@Z");
149 SET(p_wchar_assign, "?assign@?$char_traits@_W@std@@SAXAA_WAB_W@Z");
150 SET(p_short_assign, "?assign@?$char_traits@G@std@@SAXAAGABG@Z");
152 SET(p_char_eq, "?eq@?$char_traits@D@std@@SA_NABD0@Z");
153 SET(p_wchar_eq, "?eq@?$char_traits@_W@std@@SA_NAB_W0@Z");
154 SET(p_short_eq, "?eq@?$char_traits@G@std@@SA_NABG0@Z");
156 SET(p_Copy_s, "?_Copy_s@?$char_traits@D@std@@SAPADPADIPBDI@Z");
158 SET(p_char_address, "?address@?$allocator@D@std@@QBEPADAAD@Z");
159 SET(p_char_ctor, "??0?$allocator@D@std@@QAE@XZ");
160 SET(p_char_deallocate, "?deallocate@?$allocator@D@std@@QAEXPADI@Z");
161 SET(p_char_allocate, "?allocate@?$allocator@D@std@@QAEPADI@Z");
162 SET(p_char_construct, "?construct@?$allocator@D@std@@QAEXPADABD@Z");
163 SET(p_char_max_size, "?max_size@?$allocator@D@std@@QBEIXZ");
166 init_thiscall_thunk();
170 static void test_assign(void)
172 const char in[] = "abc";
176 p_char_assign(out, in);
177 ok(out[0] == in[0], "out[0] = %c\n", out[0]);
178 ok(out[1] == '#', "out[1] = %c\n", out[1]);
181 p_wchar_assign(out, in);
182 ok(*((char*)out)==in[0] && *((char*)out+1)==in[1],
183 "out[0] = %d, out[1] = %d\n", (int)out[0], (int)out[1]);
184 ok(out[2] == '#', "out[2] = %c\n", out[2]);
187 p_short_assign(out, in);
188 ok(*((char*)out)==in[0] && *((char*)out+1)==in[1],
189 "out[0] = %d, out[1] = %d\n", (int)out[0], (int)out[1]);
190 ok(out[2] == '#', "out[2] = %c\n", out[2]);
193 static void test_equal(void)
195 static const char in1[] = "abc";
196 static const char in2[] = "ab";
197 static const char in3[] = "a";
198 static const char in4[] = "b";
201 ret = p_char_eq(in1, in2);
202 ok(ret == TRUE, "ret = %d\n", (int)ret);
203 ret = p_char_eq(in1, in3);
204 ok(ret == TRUE, "ret = %d\n", (int)ret);
205 ret = p_char_eq(in1, in4);
206 ok(ret == FALSE, "ret = %d\n", (int)ret);
208 ret = p_wchar_eq(in1, in2);
209 ok(ret == TRUE, "ret = %d\n", (int)ret);
210 ret = p_wchar_eq(in1, in3);
211 ok(ret == FALSE, "ret = %d\n", (int)ret);
212 ret = p_wchar_eq(in1, in4);
213 ok(ret == FALSE, "ret = %d\n", (int)ret);
215 ret = p_short_eq(in1, in2);
216 ok(ret == TRUE, "ret = %d\n", (int)ret);
217 ret = p_short_eq(in1, in3);
218 ok(ret == FALSE, "ret = %d\n", (int)ret);
219 ret = p_short_eq(in1, in4);
220 ok(ret == FALSE, "ret = %d\n", (int)ret);
223 static void test_Copy_s(void)
225 static const char src[] = "abcd";
230 ret = p_Copy_s(dest, 4, src, 4);
231 ok(ret == dest, "ret != dest\n");
232 ok(dest[4] == '#', "dest[4] != '#'\n");
233 ok(!memcmp(dest, src, sizeof(char[4])), "dest = %s\n", dest);
235 ret = p_Copy_s(dest, 32, src, 4);
236 ok(ret == dest, "ret != dest\n");
237 ok(dest[4] == '#', "dest[4] != '#'\n");
238 ok(!memcmp(dest, src, sizeof(char[4])), "dest = %s\n", dest);
242 ret = p_Copy_s(dest, 3, src, 4);
243 ok(ret == dest, "ret != dest\n");
244 ok(dest[0] == '\0', "dest[0] != 0\n");
245 ok(invalid_parameter==1, "invalid_parameter = %d\n",
247 invalid_parameter = 0;
248 ok(errno == 0xdeadbeef, "errno = %d\n", errno);
251 p_Copy_s(NULL, 32, src, 4);
252 ok(invalid_parameter==1, "invalid_parameter = %d\n",
254 invalid_parameter = 0;
255 ok(errno == 0xdeadbeef, "errno = %d\n", errno);
258 p_Copy_s(dest, 32, NULL, 4);
259 ok(invalid_parameter==1, "invalid_parameter = %d\n",
261 invalid_parameter = 0;
262 ok(errno == 0xdeadbeef, "errno = %d\n", errno);
265 static void test_wctype(void)
267 static const struct {
288 for(i=0; i<sizeof(properties)/sizeof(properties[0]); i++) {
289 ret = p_wctype(properties[i].name);
290 ok(properties[i].mask == ret, "%d - Expected %x, got %x\n", i, properties[i].mask, ret);
294 static void test_allocator_char(void)
296 void *allocator = (void*)0xdeadbeef;
301 allocator = call_func1(p_char_ctor, allocator);
302 ok(allocator == (void*)0xdeadbeef, "allocator = %p\n", allocator);
304 ptr = call_func2(p_char_address, NULL, (void*)0xdeadbeef);
305 ok(ptr == (void*)0xdeadbeef, "incorrect address (%p)\n", ptr);
308 ptr = call_func2(p_char_allocate, allocator, 10);
309 ok(ptr != NULL, "Memory allocation failed\n");
311 ptr[0] = ptr[1] = '#';
313 call_func3(p_char_construct, allocator, ptr, &val);
315 call_func3(p_char_construct, allocator, (ptr+1), &val);
316 ok(ptr[0] == 'a', "ptr[0] = %c\n", ptr[0]);
317 ok(ptr[1] == 'b', "ptr[1] = %c\n", ptr[1]);
319 call_func3(p_char_deallocate, allocator, ptr, -1);
321 size = (unsigned int)call_func1(p_char_max_size, allocator);
322 ok(size == (unsigned int)0xffffffff, "size = %x\n", size);
335 test_allocator_char();
337 ok(!invalid_parameter, "invalid_parameter_handler was invoked too many times\n");