msvcrt/tests: Add Context::Id tests.
[wine.git] / dlls / msvcr100 / tests / msvcr100.c
blob295e28799df013e96bf7375421c5eb322185170e
1 /*
2 * Copyright 2012 Dan Kegel
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
19 #include <errno.h>
20 #include <stdarg.h>
21 #include <stdlib.h>
22 #include <wchar.h>
23 #include <stdio.h>
25 #include <windef.h>
26 #include <winbase.h>
27 #include "wine/test.h"
29 #define DEFINE_EXPECT(func) \
30 static BOOL expect_ ## func = FALSE, called_ ## func = FALSE
32 #define SET_EXPECT(func) \
33 expect_ ## func = TRUE
35 #define CHECK_EXPECT2(func) \
36 do { \
37 ok(expect_ ##func, "unexpected call " #func "\n"); \
38 called_ ## func = TRUE; \
39 }while(0)
41 #define CHECK_EXPECT(func) \
42 do { \
43 CHECK_EXPECT2(func); \
44 expect_ ## func = FALSE; \
45 }while(0)
47 #define CHECK_CALLED(func) \
48 do { \
49 ok(called_ ## func, "expected " #func "\n"); \
50 expect_ ## func = called_ ## func = FALSE; \
51 }while(0)
53 DEFINE_EXPECT(invalid_parameter_handler);
54 DEFINE_EXPECT(yield_func);
56 static _invalid_parameter_handler (__cdecl *p_set_invalid_parameter_handler)(_invalid_parameter_handler);
58 static void __cdecl test_invalid_parameter_handler(const wchar_t *expression,
59 const wchar_t *function, const wchar_t *file,
60 unsigned line, uintptr_t arg)
62 CHECK_EXPECT(invalid_parameter_handler);
63 ok(expression == NULL, "expression is not NULL\n");
64 ok(function == NULL, "function is not NULL\n");
65 ok(file == NULL, "file is not NULL\n");
66 ok(line == 0, "line = %u\n", line);
67 ok(arg == 0, "arg = %lx\n", (UINT_PTR)arg);
70 #ifdef __i386__
72 #include "pshpack1.h"
73 struct thiscall_thunk
75 BYTE pop_eax; /* popl %eax (ret addr) */
76 BYTE pop_edx; /* popl %edx (func) */
77 BYTE pop_ecx; /* popl %ecx (this) */
78 BYTE push_eax; /* pushl %eax */
79 WORD jmp_edx; /* jmp *%edx */
81 #include "poppack.h"
83 static ULONG_PTR (WINAPI *call_thiscall_func1)( void *func, void *this );
84 static ULONG_PTR (WINAPI *call_thiscall_func2)( void *func, void *this, const void *a );
86 static void init_thiscall_thunk(void)
88 struct thiscall_thunk *thunk = VirtualAlloc( NULL, sizeof(*thunk),
89 MEM_COMMIT, PAGE_EXECUTE_READWRITE );
90 thunk->pop_eax = 0x58; /* popl %eax */
91 thunk->pop_edx = 0x5a; /* popl %edx */
92 thunk->pop_ecx = 0x59; /* popl %ecx */
93 thunk->push_eax = 0x50; /* pushl %eax */
94 thunk->jmp_edx = 0xe2ff; /* jmp *%edx */
95 call_thiscall_func1 = (void *)thunk;
96 call_thiscall_func2 = (void *)thunk;
99 #define call_func1(func,_this) call_thiscall_func1(func,_this)
100 #define call_func2(func,_this,a) call_thiscall_func2(func,_this,(const void*)(a))
102 #else
104 #define init_thiscall_thunk()
105 #define call_func1(func,_this) func(_this)
106 #define call_func2(func,_this,a) func(_this,a)
108 #endif /* __i386__ */
110 #undef __thiscall
111 #ifdef __i386__
112 #define __thiscall __stdcall
113 #else
114 #define __thiscall __cdecl
115 #endif
117 typedef unsigned char MSVCRT_bool;
119 typedef enum
121 SPINWAIT_INIT,
122 SPINWAIT_SPIN,
123 SPINWAIT_YIELD,
124 SPINWAIT_DONE
125 } SpinWait_state;
127 typedef void (__cdecl *yield_func)(void);
129 typedef struct
131 ULONG spin;
132 ULONG unknown;
133 SpinWait_state state;
134 yield_func yield_func;
135 } SpinWait;
137 typedef struct {
138 CRITICAL_SECTION cs;
139 } _ReentrantBlockingLock;
141 typedef struct {
142 char pad[64];
143 } event;
145 typedef struct {
146 void *vtable;
147 } Context;
149 static int* (__cdecl *p_errno)(void);
150 static int (__cdecl *p_wmemcpy_s)(wchar_t *dest, size_t numberOfElements, const wchar_t *src, size_t count);
151 static int (__cdecl *p_wmemmove_s)(wchar_t *dest, size_t numberOfElements, const wchar_t *src, size_t count);
152 static FILE* (__cdecl *p_fopen)(const char*,const char*);
153 static int (__cdecl *p_fclose)(FILE*);
154 static size_t (__cdecl *p_fread_s)(void*,size_t,size_t,size_t,FILE*);
155 static void (__cdecl *p_lock_file)(FILE*);
156 static void (__cdecl *p_unlock_file)(FILE*);
157 static void* (__cdecl *p__aligned_offset_malloc)(size_t, size_t, size_t);
158 static void (__cdecl *p__aligned_free)(void*);
159 static size_t (__cdecl *p__aligned_msize)(void*, size_t, size_t);
160 static int (__cdecl *p_atoi)(const char*);
162 static SpinWait* (__thiscall *pSpinWait_ctor_yield)(SpinWait*, yield_func);
163 static void (__thiscall *pSpinWait_dtor)(SpinWait*);
164 static void (__thiscall *pSpinWait__DoYield)(SpinWait*);
165 static ULONG (__thiscall *pSpinWait__NumberOfSpins)(SpinWait*);
166 static void (__thiscall *pSpinWait__SetSpinCount)(SpinWait*, unsigned int);
167 static MSVCRT_bool (__thiscall *pSpinWait__ShouldSpinAgain)(SpinWait*);
168 static MSVCRT_bool (__thiscall *pSpinWait__SpinOnce)(SpinWait*);
170 static void* (__thiscall *preader_writer_lock_ctor)(void*);
171 static void (__thiscall *preader_writer_lock_dtor)(void*);
172 static void (__thiscall *preader_writer_lock_lock)(void*);
173 static void (__thiscall *preader_writer_lock_lock_read)(void*);
174 static void (__thiscall *preader_writer_lock_unlock)(void*);
175 static MSVCRT_bool (__thiscall *preader_writer_lock_try_lock)(void*);
176 static MSVCRT_bool (__thiscall *preader_writer_lock_try_lock_read)(void*);
178 static _ReentrantBlockingLock* (__thiscall *p_ReentrantBlockingLock_ctor)(_ReentrantBlockingLock*);
179 static void (__thiscall *p_ReentrantBlockingLock_dtor)(_ReentrantBlockingLock*);
180 static void (__thiscall *p_ReentrantBlockingLock__Acquire)(_ReentrantBlockingLock*);
181 static void (__thiscall *p_ReentrantBlockingLock__Release)(_ReentrantBlockingLock*);
182 static MSVCRT_bool (__thiscall *p_ReentrantBlockingLock__TryAcquire)(_ReentrantBlockingLock*);
183 static _ReentrantBlockingLock* (__thiscall *p_NonReentrantBlockingLock_ctor)(_ReentrantBlockingLock*);
184 static void (__thiscall *p_NonReentrantBlockingLock_dtor)(_ReentrantBlockingLock*);
185 static void (__thiscall *p_NonReentrantBlockingLock__Acquire)(_ReentrantBlockingLock*);
186 static void (__thiscall *p_NonReentrantBlockingLock__Release)(_ReentrantBlockingLock*);
187 static MSVCRT_bool (__thiscall *p_NonReentrantBlockingLock__TryAcquire)(_ReentrantBlockingLock*);
189 static event* (__thiscall *p_event_ctor)(event*);
190 static void (__thiscall *p_event_dtor)(event*);
191 static void (__thiscall *p_event_reset)(event*);
192 static void (__thiscall *p_event_set)(event*);
193 static size_t (__thiscall *p_event_wait)(event*, unsigned int);
194 static int (__cdecl *p_event_wait_for_multiple)(event**, size_t, MSVCRT_bool, unsigned int);
196 static Context* (__cdecl *p_Context_CurrentContext)(void);
197 unsigned int (__cdecl *p_Context_Id)(void);
199 /* make sure we use the correct errno */
200 #undef errno
201 #define errno (*p_errno())
203 #define SETNOFAIL(x,y) x = (void*)GetProcAddress(hcrt,y)
204 #define SET(x,y) do { SETNOFAIL(x,y); ok(x != NULL, "Export '%s' not found\n", y); } while(0)
206 static BOOL init(void)
208 HMODULE hcrt;
210 SetLastError(0xdeadbeef);
211 hcrt = LoadLibraryA("msvcr100.dll");
212 if (!hcrt) {
213 win_skip("msvcr100.dll not installed (got %d)\n", GetLastError());
214 return FALSE;
217 SET(p_errno, "_errno");
218 SET(p_set_invalid_parameter_handler, "_set_invalid_parameter_handler");
219 SET(p_wmemcpy_s, "wmemcpy_s");
220 SET(p_wmemmove_s, "wmemmove_s");
221 SET(p_fopen, "fopen");
222 SET(p_fclose, "fclose");
223 SET(p_fread_s, "fread_s");
224 SET(p_lock_file, "_lock_file");
225 SET(p_unlock_file, "_unlock_file");
226 SET(p__aligned_offset_malloc, "_aligned_offset_malloc");
227 SET(p__aligned_free, "_aligned_free");
228 SET(p__aligned_msize, "_aligned_msize");
229 SET(p_atoi, "atoi");
231 SET(p_Context_Id, "?Id@Context@Concurrency@@SAIXZ");
233 if(sizeof(void*) == 8) { /* 64-bit initialization */
234 SET(pSpinWait_ctor_yield, "??0?$_SpinWait@$00@details@Concurrency@@QEAA@P6AXXZ@Z");
235 SET(pSpinWait_dtor, "??_F?$_SpinWait@$00@details@Concurrency@@QEAAXXZ");
236 SET(pSpinWait__DoYield, "?_DoYield@?$_SpinWait@$00@details@Concurrency@@IEAAXXZ");
237 SET(pSpinWait__NumberOfSpins, "?_NumberOfSpins@?$_SpinWait@$00@details@Concurrency@@IEAAKXZ");
238 SET(pSpinWait__SetSpinCount, "?_SetSpinCount@?$_SpinWait@$00@details@Concurrency@@QEAAXI@Z");
239 SET(pSpinWait__ShouldSpinAgain, "?_ShouldSpinAgain@?$_SpinWait@$00@details@Concurrency@@IEAA_NXZ");
240 SET(pSpinWait__SpinOnce, "?_SpinOnce@?$_SpinWait@$00@details@Concurrency@@QEAA_NXZ");
242 SET(preader_writer_lock_ctor, "??0reader_writer_lock@Concurrency@@QEAA@XZ");
243 SET(preader_writer_lock_dtor, "??1reader_writer_lock@Concurrency@@QEAA@XZ");
244 SET(preader_writer_lock_lock, "?lock@reader_writer_lock@Concurrency@@QEAAXXZ");
245 SET(preader_writer_lock_lock_read, "?lock_read@reader_writer_lock@Concurrency@@QEAAXXZ");
246 SET(preader_writer_lock_unlock, "?unlock@reader_writer_lock@Concurrency@@QEAAXXZ");
247 SET(preader_writer_lock_try_lock, "?try_lock@reader_writer_lock@Concurrency@@QEAA_NXZ");
248 SET(preader_writer_lock_try_lock_read, "?try_lock_read@reader_writer_lock@Concurrency@@QEAA_NXZ");
250 SET(p_ReentrantBlockingLock_ctor, "??0_ReentrantBlockingLock@details@Concurrency@@QEAA@XZ");
251 SET(p_ReentrantBlockingLock_dtor, "??1_ReentrantBlockingLock@details@Concurrency@@QEAA@XZ");
252 SET(p_ReentrantBlockingLock__Acquire, "?_Acquire@_ReentrantBlockingLock@details@Concurrency@@QEAAXXZ");
253 SET(p_ReentrantBlockingLock__Release, "?_Release@_ReentrantBlockingLock@details@Concurrency@@QEAAXXZ");
254 SET(p_ReentrantBlockingLock__TryAcquire, "?_TryAcquire@_ReentrantBlockingLock@details@Concurrency@@QEAA_NXZ");
255 SET(p_NonReentrantBlockingLock_ctor, "??0_NonReentrantBlockingLock@details@Concurrency@@QEAA@XZ");
256 SET(p_NonReentrantBlockingLock_dtor, "??1_NonReentrantBlockingLock@details@Concurrency@@QEAA@XZ");
257 SET(p_NonReentrantBlockingLock__Acquire, "?_Acquire@_NonReentrantBlockingLock@details@Concurrency@@QEAAXXZ");
258 SET(p_NonReentrantBlockingLock__Release, "?_Release@_NonReentrantBlockingLock@details@Concurrency@@QEAAXXZ");
259 SET(p_NonReentrantBlockingLock__TryAcquire, "?_TryAcquire@_NonReentrantBlockingLock@details@Concurrency@@QEAA_NXZ");
261 SET(p_event_ctor, "??0event@Concurrency@@QEAA@XZ");
262 SET(p_event_dtor, "??1event@Concurrency@@QEAA@XZ");
263 SET(p_event_reset, "?reset@event@Concurrency@@QEAAXXZ");
264 SET(p_event_set, "?set@event@Concurrency@@QEAAXXZ");
265 SET(p_event_wait, "?wait@event@Concurrency@@QEAA_KI@Z");
266 SET(p_event_wait_for_multiple, "?wait_for_multiple@event@Concurrency@@SA_KPEAPEAV12@_K_NI@Z");
268 SET(p_Context_CurrentContext, "?CurrentContext@Context@Concurrency@@SAPEAV12@XZ");
269 } else {
270 SET(pSpinWait_ctor_yield, "??0?$_SpinWait@$00@details@Concurrency@@QAE@P6AXXZ@Z");
271 SET(pSpinWait_dtor, "??_F?$_SpinWait@$00@details@Concurrency@@QAEXXZ");
272 SET(pSpinWait__DoYield, "?_DoYield@?$_SpinWait@$00@details@Concurrency@@IAEXXZ");
273 SET(pSpinWait__NumberOfSpins, "?_NumberOfSpins@?$_SpinWait@$00@details@Concurrency@@IAEKXZ");
274 SET(pSpinWait__SetSpinCount, "?_SetSpinCount@?$_SpinWait@$00@details@Concurrency@@QAEXI@Z");
275 SET(pSpinWait__ShouldSpinAgain, "?_ShouldSpinAgain@?$_SpinWait@$00@details@Concurrency@@IAE_NXZ");
276 SET(pSpinWait__SpinOnce, "?_SpinOnce@?$_SpinWait@$00@details@Concurrency@@QAE_NXZ");
278 SET(preader_writer_lock_ctor, "??0reader_writer_lock@Concurrency@@QAE@XZ");
279 SET(preader_writer_lock_dtor, "??1reader_writer_lock@Concurrency@@QAE@XZ");
280 SET(preader_writer_lock_lock, "?lock@reader_writer_lock@Concurrency@@QAEXXZ");
281 SET(preader_writer_lock_lock_read, "?lock_read@reader_writer_lock@Concurrency@@QAEXXZ");
282 SET(preader_writer_lock_unlock, "?unlock@reader_writer_lock@Concurrency@@QAEXXZ");
283 SET(preader_writer_lock_try_lock, "?try_lock@reader_writer_lock@Concurrency@@QAE_NXZ");
284 SET(preader_writer_lock_try_lock_read, "?try_lock_read@reader_writer_lock@Concurrency@@QAE_NXZ");
286 SET(p_ReentrantBlockingLock_ctor, "??0_ReentrantBlockingLock@details@Concurrency@@QAE@XZ");
287 SET(p_ReentrantBlockingLock_dtor, "??1_ReentrantBlockingLock@details@Concurrency@@QAE@XZ");
288 SET(p_ReentrantBlockingLock__Acquire, "?_Acquire@_ReentrantBlockingLock@details@Concurrency@@QAEXXZ");
289 SET(p_ReentrantBlockingLock__Release, "?_Release@_ReentrantBlockingLock@details@Concurrency@@QAEXXZ");
290 SET(p_ReentrantBlockingLock__TryAcquire, "?_TryAcquire@_ReentrantBlockingLock@details@Concurrency@@QAE_NXZ");
291 SET(p_NonReentrantBlockingLock_ctor, "??0_NonReentrantBlockingLock@details@Concurrency@@QAE@XZ");
292 SET(p_NonReentrantBlockingLock_dtor, "??1_NonReentrantBlockingLock@details@Concurrency@@QAE@XZ");
293 SET(p_NonReentrantBlockingLock__Acquire, "?_Acquire@_NonReentrantBlockingLock@details@Concurrency@@QAEXXZ");
294 SET(p_NonReentrantBlockingLock__Release, "?_Release@_NonReentrantBlockingLock@details@Concurrency@@QAEXXZ");
295 SET(p_NonReentrantBlockingLock__TryAcquire, "?_TryAcquire@_NonReentrantBlockingLock@details@Concurrency@@QAE_NXZ");
297 SET(p_event_ctor, "??0event@Concurrency@@QAE@XZ");
298 SET(p_event_dtor, "??1event@Concurrency@@QAE@XZ");
299 SET(p_event_reset, "?reset@event@Concurrency@@QAEXXZ");
300 SET(p_event_set, "?set@event@Concurrency@@QAEXXZ");
301 SET(p_event_wait, "?wait@event@Concurrency@@QAEII@Z");
302 SET(p_event_wait_for_multiple, "?wait_for_multiple@event@Concurrency@@SAIPAPAV12@I_NI@Z");
304 SET(p_Context_CurrentContext, "?CurrentContext@Context@Concurrency@@SAPAV12@XZ");
307 init_thiscall_thunk();
308 return TRUE;
311 #define NUMELMS(array) (sizeof(array)/sizeof((array)[0]))
313 #define okwchars(dst, b0, b1, b2, b3, b4, b5, b6, b7) \
314 ok(dst[0] == b0 && dst[1] == b1 && dst[2] == b2 && dst[3] == b3 && \
315 dst[4] == b4 && dst[5] == b5 && dst[6] == b6 && dst[7] == b7, \
316 "Bad result: 0x%04x 0x%04x 0x%04x 0x%04x 0x%04x 0x%04x 0x%04x 0x%04x\n",\
317 dst[0], dst[1], dst[2], dst[3], dst[4], dst[5], dst[6], dst[7])
319 static void test_wmemcpy_s(void)
321 static wchar_t dest[8];
322 static const wchar_t tiny[] = {'T',0,'I','N','Y',0};
323 static const wchar_t big[] = {'a','t','o','o','l','o','n','g','s','t','r','i','n','g',0};
324 const wchar_t XX = 0x5858; /* two 'X' bytes */
325 int ret;
327 ok(p_set_invalid_parameter_handler(test_invalid_parameter_handler) == NULL,
328 "Invalid parameter handler was already set\n");
330 /* Normal */
331 memset(dest, 'X', sizeof(dest));
332 ret = p_wmemcpy_s(dest, NUMELMS(dest), tiny, NUMELMS(tiny));
333 ok(ret == 0, "Copying a buffer into a big enough destination returned %d, expected 0\n", ret);
334 okwchars(dest, tiny[0], tiny[1], tiny[2], tiny[3], tiny[4], tiny[5], XX, XX);
336 /* Vary source size */
337 errno = 0xdeadbeef;
338 SET_EXPECT(invalid_parameter_handler);
339 memset(dest, 'X', sizeof(dest));
340 ret = p_wmemcpy_s(dest, NUMELMS(dest), big, NUMELMS(big));
341 ok(errno == ERANGE, "Copying a big buffer to a small destination errno %d, expected ERANGE\n", errno);
342 ok(ret == ERANGE, "Copying a big buffer to a small destination returned %d, expected ERANGE\n", ret);
343 okwchars(dest, 0, 0, 0, 0, 0, 0, 0, 0);
344 CHECK_CALLED(invalid_parameter_handler);
346 /* Replace source with NULL */
347 errno = 0xdeadbeef;
348 SET_EXPECT(invalid_parameter_handler);
349 memset(dest, 'X', sizeof(dest));
350 ret = p_wmemcpy_s(dest, NUMELMS(dest), NULL, NUMELMS(tiny));
351 ok(errno == EINVAL, "Copying a NULL source buffer errno %d, expected EINVAL\n", errno);
352 ok(ret == EINVAL, "Copying a NULL source buffer returned %d, expected EINVAL\n", ret);
353 okwchars(dest, 0, 0, 0, 0, 0, 0, 0, 0);
354 CHECK_CALLED(invalid_parameter_handler);
356 /* Vary dest size */
357 errno = 0xdeadbeef;
358 SET_EXPECT(invalid_parameter_handler);
359 memset(dest, 'X', sizeof(dest));
360 ret = p_wmemcpy_s(dest, 0, tiny, NUMELMS(tiny));
361 ok(errno == ERANGE, "Copying into a destination of size 0 errno %d, expected ERANGE\n", errno);
362 ok(ret == ERANGE, "Copying into a destination of size 0 returned %d, expected ERANGE\n", ret);
363 okwchars(dest, XX, XX, XX, XX, XX, XX, XX, XX);
364 CHECK_CALLED(invalid_parameter_handler);
366 /* Replace dest with NULL */
367 errno = 0xdeadbeef;
368 SET_EXPECT(invalid_parameter_handler);
369 ret = p_wmemcpy_s(NULL, NUMELMS(dest), tiny, NUMELMS(tiny));
370 ok(errno == EINVAL, "Copying a tiny buffer to a big NULL destination errno %d, expected EINVAL\n", errno);
371 ok(ret == EINVAL, "Copying a tiny buffer to a big NULL destination returned %d, expected EINVAL\n", ret);
372 CHECK_CALLED(invalid_parameter_handler);
374 /* Combinations */
375 errno = 0xdeadbeef;
376 SET_EXPECT(invalid_parameter_handler);
377 memset(dest, 'X', sizeof(dest));
378 ret = p_wmemcpy_s(dest, 0, NULL, NUMELMS(tiny));
379 ok(errno == EINVAL, "Copying a NULL buffer into a destination of size 0 errno %d, expected EINVAL\n", errno);
380 ok(ret == EINVAL, "Copying a NULL buffer into a destination of size 0 returned %d, expected EINVAL\n", ret);
381 okwchars(dest, XX, XX, XX, XX, XX, XX, XX, XX);
382 CHECK_CALLED(invalid_parameter_handler);
384 ok(p_set_invalid_parameter_handler(NULL) == test_invalid_parameter_handler,
385 "Cannot reset invalid parameter handler\n");
388 static void test_wmemmove_s(void)
390 static wchar_t dest[8];
391 static const wchar_t tiny[] = {'T',0,'I','N','Y',0};
392 static const wchar_t big[] = {'a','t','o','o','l','o','n','g','s','t','r','i','n','g',0};
393 const wchar_t XX = 0x5858; /* two 'X' bytes */
394 int ret;
396 ok(p_set_invalid_parameter_handler(test_invalid_parameter_handler) == NULL,
397 "Invalid parameter handler was already set\n");
399 /* Normal */
400 memset(dest, 'X', sizeof(dest));
401 ret = p_wmemmove_s(dest, NUMELMS(dest), tiny, NUMELMS(tiny));
402 ok(ret == 0, "Moving a buffer into a big enough destination returned %d, expected 0\n", ret);
403 okwchars(dest, tiny[0], tiny[1], tiny[2], tiny[3], tiny[4], tiny[5], XX, XX);
405 /* Overlapping */
406 memcpy(dest, big, sizeof(dest));
407 ret = p_wmemmove_s(dest+1, NUMELMS(dest)-1, dest, NUMELMS(dest)-1);
408 ok(ret == 0, "Moving a buffer up one char returned %d, expected 0\n", ret);
409 okwchars(dest, big[0], big[0], big[1], big[2], big[3], big[4], big[5], big[6]);
411 /* Vary source size */
412 errno = 0xdeadbeef;
413 SET_EXPECT(invalid_parameter_handler);
414 memset(dest, 'X', sizeof(dest));
415 ret = p_wmemmove_s(dest, NUMELMS(dest), big, NUMELMS(big));
416 ok(errno == ERANGE, "Moving a big buffer to a small destination errno %d, expected ERANGE\n", errno);
417 ok(ret == ERANGE, "Moving a big buffer to a small destination returned %d, expected ERANGE\n", ret);
418 okwchars(dest, XX, XX, XX, XX, XX, XX, XX, XX);
419 CHECK_CALLED(invalid_parameter_handler);
421 /* Replace source with NULL */
422 errno = 0xdeadbeef;
423 SET_EXPECT(invalid_parameter_handler);
424 memset(dest, 'X', sizeof(dest));
425 ret = p_wmemmove_s(dest, NUMELMS(dest), NULL, NUMELMS(tiny));
426 ok(errno == EINVAL, "Moving a NULL source buffer errno %d, expected EINVAL\n", errno);
427 ok(ret == EINVAL, "Moving a NULL source buffer returned %d, expected EINVAL\n", ret);
428 okwchars(dest, XX, XX, XX, XX, XX, XX, XX, XX);
429 CHECK_CALLED(invalid_parameter_handler);
431 /* Vary dest size */
432 errno = 0xdeadbeef;
433 SET_EXPECT(invalid_parameter_handler);
434 memset(dest, 'X', sizeof(dest));
435 ret = p_wmemmove_s(dest, 0, tiny, NUMELMS(tiny));
436 ok(errno == ERANGE, "Moving into a destination of size 0 errno %d, expected ERANGE\n", errno);
437 ok(ret == ERANGE, "Moving into a destination of size 0 returned %d, expected ERANGE\n", ret);
438 okwchars(dest, XX, XX, XX, XX, XX, XX, XX, XX);
439 CHECK_CALLED(invalid_parameter_handler);
441 /* Replace dest with NULL */
442 errno = 0xdeadbeef;
443 SET_EXPECT(invalid_parameter_handler);
444 ret = p_wmemmove_s(NULL, NUMELMS(dest), tiny, NUMELMS(tiny));
445 ok(errno == EINVAL, "Moving a tiny buffer to a big NULL destination errno %d, expected EINVAL\n", errno);
446 ok(ret == EINVAL, "Moving a tiny buffer to a big NULL destination returned %d, expected EINVAL\n", ret);
447 CHECK_CALLED(invalid_parameter_handler);
449 /* Combinations */
450 errno = 0xdeadbeef;
451 SET_EXPECT(invalid_parameter_handler);
452 memset(dest, 'X', sizeof(dest));
453 ret = p_wmemmove_s(dest, 0, NULL, NUMELMS(tiny));
454 ok(errno == EINVAL, "Moving a NULL buffer into a destination of size 0 errno %d, expected EINVAL\n", errno);
455 ok(ret == EINVAL, "Moving a NULL buffer into a destination of size 0 returned %d, expected EINVAL\n", ret);
456 okwchars(dest, XX, XX, XX, XX, XX, XX, XX, XX);
457 CHECK_CALLED(invalid_parameter_handler);
459 ok(p_set_invalid_parameter_handler(NULL) == test_invalid_parameter_handler,
460 "Cannot reset invalid parameter handler\n");
463 struct block_file_arg
465 FILE *test_file;
466 HANDLE init;
467 HANDLE finish;
470 static DWORD WINAPI block_file(void *arg)
472 struct block_file_arg *files = arg;
473 p_lock_file(files->test_file);
474 SetEvent(files->init);
475 WaitForSingleObject(files->finish, INFINITE);
476 p_unlock_file(files->test_file);
477 return 0;
480 static void test_fread_s(void)
482 static const char test_file[] = "fread_s.tst";
483 int ret;
484 char buf[10];
485 HANDLE thread;
486 struct block_file_arg arg;
488 FILE *f = fopen(test_file, "w");
489 if(!f) {
490 skip("Error creating test file\n");
491 return;
493 fwrite("test", 1, 4, f);
494 fclose(f);
496 ok(p_set_invalid_parameter_handler(test_invalid_parameter_handler) == NULL,
497 "Invalid parameter handler was already set\n");
499 SET_EXPECT(invalid_parameter_handler);
500 errno = 0xdeadbeef;
501 ret = p_fread_s(buf, sizeof(buf), 1, 1, NULL);
502 ok(ret == 0, "fread_s returned %d, expected 0\n", ret);
503 ok(errno == EINVAL, "errno = %d, expected EINVAL\n", errno);
504 CHECK_CALLED(invalid_parameter_handler);
506 f = p_fopen(test_file, "r");
507 arg.test_file = f;
508 arg.init = CreateEventW(NULL, FALSE, FALSE, NULL);
509 arg.finish = CreateEventW(NULL, FALSE, FALSE, NULL);
510 thread = CreateThread(NULL, 0, block_file, (void*)&arg, 0, NULL);
511 WaitForSingleObject(arg.init, INFINITE);
513 errno = 0xdeadbeef;
514 ret = p_fread_s(NULL, sizeof(buf), 0, 1, f);
515 ok(ret == 0, "fread_s returned %d, expected 0\n", ret);
516 ok(errno == 0xdeadbeef, "errno = %d, expected 0xdeadbeef\n", errno);
517 ret = p_fread_s(NULL, sizeof(buf), 1, 0, f);
518 ok(ret == 0, "fread_s returned %d, expected 0\n", ret);
519 ok(errno == 0xdeadbeef, "errno = %d, expected 0xdeadbeef\n", errno);
521 SetEvent(arg.finish);
522 WaitForSingleObject(thread, INFINITE);
524 SET_EXPECT(invalid_parameter_handler);
525 errno = 0xdeadbeef;
526 ret = p_fread_s(NULL, sizeof(buf), 1, 1, f);
527 ok(ret == 0, "fread_s returned %d, expected 0\n", ret);
528 ok(errno == EINVAL, "errno = %d, expected EINVAL\n", errno);
529 CHECK_CALLED(invalid_parameter_handler);
531 SET_EXPECT(invalid_parameter_handler);
532 errno = 0xdeadbeef;
533 buf[1] = 'a';
534 ret = p_fread_s(buf, 3, 1, 10, f);
535 ok(ret == 0, "fread_s returned %d, expected 0\n", ret);
536 ok(buf[0] == 0, "buf[0] = '%c', expected 0\n", buf[0]);
537 ok(buf[1] == 0, "buf[1] = '%c', expected 0\n", buf[1]);
538 ok(errno == ERANGE, "errno = %d, expected ERANGE\n", errno);
539 CHECK_CALLED(invalid_parameter_handler);
541 SET_EXPECT(invalid_parameter_handler);
542 errno = 0xdeadbeef;
543 ret = p_fread_s(buf, 2, 1, 10, f);
544 ok(ret == 0, "fread_s returned %d, expected 0\n", ret);
545 ok(buf[0] == 0, "buf[0] = '%c', expected 0\n", buf[0]);
546 ok(errno == ERANGE, "errno = %d, expected ERANGE\n", errno);
547 CHECK_CALLED(invalid_parameter_handler);
549 memset(buf, 'a', sizeof(buf));
550 ret = p_fread_s(buf, sizeof(buf), 3, 10, f);
551 ok(ret==1, "fread_s returned %d, expected 1\n", ret);
552 ok(buf[0] == 'e', "buf[0] = '%c', expected 'e'\n", buf[0]);
553 ok(buf[1] == 's', "buf[1] = '%c', expected 's'\n", buf[1]);
554 ok(buf[2] == 't', "buf[2] = '%c', expected 't'\n", buf[2]);
555 ok(buf[3] == 'a', "buf[3] = '%c', expected 'a'\n", buf[3]);
556 p_fclose(f);
558 ok(p_set_invalid_parameter_handler(NULL) == test_invalid_parameter_handler,
559 "Cannot reset invalid parameter handler\n");
560 CloseHandle(arg.init);
561 CloseHandle(arg.finish);
562 CloseHandle(thread);
563 unlink(test_file);
566 static void test__aligned_msize(void)
568 void *mem;
569 int ret;
571 mem = p__aligned_offset_malloc(23, 16, 7);
572 ret = p__aligned_msize(mem, 16, 7);
573 ok(ret == 23, "_aligned_msize returned %d\n", ret);
574 ret = p__aligned_msize(mem, 15, 7);
575 ok(ret == 24, "_aligned_msize returned %d\n", ret);
576 ret = p__aligned_msize(mem, 11, 7);
577 ok(ret == 28, "_aligned_msize returned %d\n", ret);
578 ret = p__aligned_msize(mem, 1, 7);
579 ok(ret == 39-sizeof(void*), "_aligned_msize returned %d\n", ret);
580 ret = p__aligned_msize(mem, 8, 0);
581 todo_wine ok(ret == 32, "_aligned_msize returned %d\n", ret);
582 p__aligned_free(mem);
584 mem = p__aligned_offset_malloc(3, 16, 0);
585 ret = p__aligned_msize(mem, 16, 0);
586 ok(ret == 3, "_aligned_msize returned %d\n", ret);
587 ret = p__aligned_msize(mem, 11, 0);
588 ok(ret == 8, "_aligned_msize returned %d\n", ret);
589 p__aligned_free(mem);
592 static void test_atoi(void)
594 int r;
596 r = p_atoi("0");
597 ok(r == 0, "atoi(0) = %d\n", r);
599 r = p_atoi("-1");
600 ok(r == -1, "atoi(-1) = %d\n", r);
602 r = p_atoi("1");
603 ok(r == 1, "atoi(1) = %d\n", r);
605 r = p_atoi("4294967296");
606 ok(r == 2147483647, "atoi(4294967296) = %d\n", r);
609 static void __cdecl test_yield_func(void)
611 CHECK_EXPECT(yield_func);
614 static void test__SpinWait(void)
616 SpinWait sp;
617 ULONG ul;
618 MSVCRT_bool b;
620 call_func2(pSpinWait_ctor_yield, &sp, test_yield_func);
622 SET_EXPECT(yield_func);
623 call_func1(pSpinWait__DoYield, &sp);
624 CHECK_CALLED(yield_func);
626 ul = call_func1(pSpinWait__NumberOfSpins, &sp);
627 ok(ul == 1, "_SpinWait::_NumberOfSpins returned %u\n", ul);
629 sp.spin = 2;
630 b = call_func1(pSpinWait__ShouldSpinAgain, &sp);
631 ok(b, "_SpinWait::_ShouldSpinAgain returned %x\n", b);
632 ok(sp.spin == 1, "sp.spin = %u\n", sp.spin);
633 b = call_func1(pSpinWait__ShouldSpinAgain, &sp);
634 ok(!b, "_SpinWait::_ShouldSpinAgain returned %x\n", b);
635 ok(sp.spin == 0, "sp.spin = %u\n", sp.spin);
636 b = call_func1(pSpinWait__ShouldSpinAgain, &sp);
637 ok(b, "_SpinWait::_ShouldSpinAgain returned %x\n", b);
638 ok(sp.spin == -1, "sp.spin = %u\n", sp.spin);
640 call_func2(pSpinWait__SetSpinCount, &sp, 2);
641 b = call_func1(pSpinWait__SpinOnce, &sp);
642 ok(b, "_SpinWait::_SpinOnce returned %x\n", b);
643 ok(sp.spin == 1, "sp.spin = %u\n", sp.spin);
644 b = call_func1(pSpinWait__SpinOnce, &sp);
645 ok(b, "_SpinWait::_SpinOnce returned %x\n", b);
646 ok(sp.spin == 0, "sp.spin = %u\n", sp.spin);
647 SET_EXPECT(yield_func);
648 b = call_func1(pSpinWait__SpinOnce, &sp);
649 ok(b, "_SpinWait::_SpinOnce returned %x\n", b);
650 ok(sp.spin == 0, "sp.spin = %u\n", sp.spin);
651 CHECK_CALLED(yield_func);
652 b = call_func1(pSpinWait__SpinOnce, &sp);
653 ok(!b, "_SpinWait::_SpinOnce returned %x\n", b);
654 ok(sp.spin==0 || sp.spin==4000, "sp.spin = %u\n", sp.spin);
656 call_func1(pSpinWait_dtor, &sp);
659 static DWORD WINAPI lock_read_thread(void *rw_lock)
661 return (MSVCRT_bool)call_func1(preader_writer_lock_try_lock_read, rw_lock);
664 static void test_reader_writer_lock(void)
666 /* define reader_writer_lock data big enough to hold every version of structure */
667 char rw_lock[100];
668 MSVCRT_bool ret;
669 HANDLE thread;
670 DWORD d;
672 call_func1(preader_writer_lock_ctor, rw_lock);
674 ret = call_func1(preader_writer_lock_try_lock, rw_lock);
675 ok(ret, "reader_writer_lock:try_lock returned %x\n", ret);
676 ret = call_func1(preader_writer_lock_try_lock, rw_lock);
677 ok(!ret, "reader_writer_lock:try_lock returned %x\n", ret);
678 ret = call_func1(preader_writer_lock_try_lock_read, rw_lock);
679 ok(!ret, "reader_writer_lock:try_lock_read returned %x\n", ret);
680 call_func1(preader_writer_lock_unlock, rw_lock);
682 /* test lock for read on already locked section */
683 ret = call_func1(preader_writer_lock_try_lock_read, rw_lock);
684 ok(ret, "reader_writer_lock:try_lock_read returned %x\n", ret);
685 ret = call_func1(preader_writer_lock_try_lock_read, rw_lock);
686 ok(ret, "reader_writer_lock:try_lock_read returned %x\n", ret);
687 ret = call_func1(preader_writer_lock_try_lock, rw_lock);
688 ok(!ret, "reader_writer_lock:try_lock returned %x\n", ret);
689 call_func1(preader_writer_lock_unlock, rw_lock);
690 ret = call_func1(preader_writer_lock_try_lock, rw_lock);
691 ok(!ret, "reader_writer_lock:try_lock returned %x\n", ret);
692 call_func1(preader_writer_lock_unlock, rw_lock);
693 ret = call_func1(preader_writer_lock_try_lock, rw_lock);
694 ok(ret, "reader_writer_lock:try_lock returned %x\n", ret);
695 call_func1(preader_writer_lock_unlock, rw_lock);
697 call_func1(preader_writer_lock_lock, rw_lock);
698 thread = CreateThread(NULL, 0, lock_read_thread, rw_lock, 0, NULL);
699 ok(thread != NULL, "CreateThread failed: %d\n", GetLastError());
700 WaitForSingleObject(thread, INFINITE);
701 ok(GetExitCodeThread(thread, &d), "GetExitCodeThread failed\n");
702 ok(!d, "reader_writer_lock::try_lock_read succeeded on already locked object\n");
703 CloseHandle(thread);
704 call_func1(preader_writer_lock_unlock, rw_lock);
706 call_func1(preader_writer_lock_lock_read, rw_lock);
707 thread = CreateThread(NULL, 0, lock_read_thread, rw_lock, 0, NULL);
708 ok(thread != NULL, "CreateThread failed: %d\n", GetLastError());
709 WaitForSingleObject(thread, INFINITE);
710 ok(GetExitCodeThread(thread, &d), "GetExitCodeThread failed\n");
711 ok(d, "reader_writer_lock::try_lock_read failed on object locked for reading\n");
712 CloseHandle(thread);
713 call_func1(preader_writer_lock_unlock, rw_lock);
715 call_func1(preader_writer_lock_dtor, rw_lock);
718 static void test__ReentrantBlockingLock(void)
720 _ReentrantBlockingLock rbl;
721 MSVCRT_bool ret;
723 call_func1(p_ReentrantBlockingLock_ctor, &rbl);
724 ret = call_func1(p_ReentrantBlockingLock__TryAcquire, &rbl);
725 ok(ret, "_ReentrantBlockingLock__TryAcquire failed\n");
726 call_func1(p_ReentrantBlockingLock__Acquire, &rbl);
727 ret = call_func1(p_ReentrantBlockingLock__TryAcquire, &rbl);
728 ok(ret, "_ReentrantBlockingLock__TryAcquire failed\n");
729 call_func1(p_ReentrantBlockingLock__Release, &rbl);
730 call_func1(p_ReentrantBlockingLock__Release, &rbl);
731 call_func1(p_ReentrantBlockingLock__Release, &rbl);
732 call_func1(p_ReentrantBlockingLock_dtor, &rbl);
734 call_func1(p_NonReentrantBlockingLock_ctor, &rbl);
735 ret = call_func1(p_NonReentrantBlockingLock__TryAcquire, &rbl);
736 ok(ret, "_NonReentrantBlockingLock__TryAcquire failed\n");
737 call_func1(p_NonReentrantBlockingLock__Acquire, &rbl);
738 ret = call_func1(p_NonReentrantBlockingLock__TryAcquire, &rbl);
739 ok(ret, "_NonReentrantBlockingLock__TryAcquire failed\n");
740 call_func1(p_NonReentrantBlockingLock__Release, &rbl);
741 call_func1(p_NonReentrantBlockingLock__Release, &rbl);
742 call_func1(p_NonReentrantBlockingLock__Release, &rbl);
743 call_func1(p_NonReentrantBlockingLock_dtor, &rbl);
746 static DWORD WINAPI test_event_thread(void *arg)
748 event *evt = arg;
749 call_func1(p_event_set, evt);
750 return 0;
753 static DWORD WINAPI multiple_events_thread(void *arg)
755 event **events = arg;
757 Sleep(50);
758 call_func1(p_event_set, events[0]);
759 call_func1(p_event_reset, events[0]);
760 call_func1(p_event_set, events[1]);
761 call_func1(p_event_reset, events[1]);
762 return 0;
765 static void test_event(void)
767 int i;
768 int ret;
769 event evt;
770 event *evts[70];
771 HANDLE thread;
772 HANDLE threads[NUMELMS(evts)];
774 call_func1(p_event_ctor, &evt);
776 ret = call_func2(p_event_wait, &evt, 100);
777 ok(ret == -1, "expected -1, got %d\n", ret);
779 call_func1(p_event_set, &evt);
780 ret = call_func2(p_event_wait, &evt, 100);
781 ok(!ret, "expected 0, got %d\n", ret);
783 ret = call_func2(p_event_wait, &evt, 100);
784 ok(!ret, "expected 0, got %d\n", ret);
786 call_func1(p_event_reset, &evt);
787 ret = call_func2(p_event_wait, &evt, 100);
788 ok(ret == -1, "expected -1, got %d\n", ret);
790 thread = CreateThread(NULL, 0, test_event_thread, (void*)&evt, 0, NULL);
791 ret = call_func2(p_event_wait, &evt, 5000);
792 ok(!ret, "expected 0, got %d\n", ret);
793 WaitForSingleObject(thread, INFINITE);
794 CloseHandle(thread);
796 if (0) /* crashes on Windows */
797 p_event_wait_for_multiple(NULL, 10, TRUE, 0);
799 for (i = 0; i < NUMELMS(evts); i++) {
800 evts[i] = malloc(sizeof(evt));
801 call_func1(p_event_ctor, evts[i]);
804 ret = p_event_wait_for_multiple(evts, 0, TRUE, 100);
805 ok(!ret, "expected 0, got %d\n", ret);
807 ret = p_event_wait_for_multiple(evts, NUMELMS(evts), TRUE, 100);
808 ok(ret == -1, "expected -1, got %d\n", ret);
810 /* reset and test wait for multiple with all */
811 for (i = 0; i < NUMELMS(evts); i++)
812 threads[i] = CreateThread(NULL, 0, test_event_thread, (void*)evts[i], 0, NULL);
814 ret = p_event_wait_for_multiple(evts, NUMELMS(evts), TRUE, 5000);
815 ok(ret != -1, "didn't expect -1\n");
817 for (i = 0; i < NUMELMS(evts); i++) {
818 WaitForSingleObject(threads[i], INFINITE);
819 CloseHandle(threads[i]);
822 /* reset and test wait for multiple with any */
823 call_func1(p_event_reset, evts[0]);
825 thread = CreateThread(NULL, 0, test_event_thread, (void*)evts[0], 0, NULL);
826 ret = p_event_wait_for_multiple(evts, 1, FALSE, 5000);
827 ok(!ret, "expected 0, got %d\n", ret);
828 WaitForSingleObject(thread, INFINITE);
829 CloseHandle(thread);
831 call_func1(p_event_reset, evts[0]);
832 call_func1(p_event_reset, evts[1]);
833 thread = CreateThread(NULL, 0, multiple_events_thread, (void*)evts, 0, NULL);
834 ret = p_event_wait_for_multiple(evts, 2, TRUE, 500);
835 ok(ret == -1, "expected -1, got %d\n", ret);
836 WaitForSingleObject(thread, INFINITE);
837 CloseHandle(thread);
839 call_func1(p_event_reset, evts[0]);
840 call_func1(p_event_set, evts[1]);
841 ret = p_event_wait_for_multiple(evts, 2, FALSE, 0);
842 ok(ret == 1, "expected 1, got %d\n", ret);
844 for (i = 0; i < NUMELMS(evts); i++) {
845 call_func1(p_event_dtor, evts[i]);
846 free(evts[i]);
849 call_func1(p_event_dtor, &evt);
852 static DWORD WINAPI external_context_thread(void *arg)
854 unsigned int id;
855 Context *ctx;
857 id = p_Context_Id();
858 ok(id == -1, "Context::Id() = %u\n", id);
860 ctx = p_Context_CurrentContext();
861 ok(ctx != NULL, "Context::CurrentContext() = NULL\n");
862 id = p_Context_Id();
863 ok(id == 1, "Context::Id() = %u\n", id);
864 return 0;
867 static void test_ExternalContextBase(void)
869 unsigned int id;
870 Context *ctx;
871 HANDLE thread;
873 id = p_Context_Id();
874 ok(id == -1, "Context::Id() = %u\n", id);
876 ctx = p_Context_CurrentContext();
877 ok(ctx != NULL, "Context::CurrentContext() = NULL\n");
878 id = p_Context_Id();
879 ok(id == 0, "Context::Id() = %u\n", id);
881 ctx = p_Context_CurrentContext();
882 ok(ctx != NULL, "Context::CurrentContext() = NULL\n");
883 id = p_Context_Id();
884 ok(id == 0, "Context::Id() = %u\n", id);
886 thread = CreateThread(NULL, 0, external_context_thread, NULL, 0, NULL);
887 ok(thread != NULL, "CreateThread failed: %d\n", GetLastError());
888 WaitForSingleObject(thread, INFINITE);
891 START_TEST(msvcr100)
893 if (!init())
894 return;
896 test_ExternalContextBase();
897 test_wmemcpy_s();
898 test_wmemmove_s();
899 test_fread_s();
900 test__aligned_msize();
901 test_atoi();
902 test__SpinWait();
903 test_reader_writer_lock();
904 test__ReentrantBlockingLock();
905 test_event();