include/mscvpdb.h: Use flexible array members for the rest of structures.
[wine.git] / dlls / msvcr100 / tests / msvcr100.c
blob7bde5c7935df6c08885da6849e4914d4025650dd
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 #include <locale.h>
31 #define DEFINE_EXPECT(func) \
32 static BOOL expect_ ## func = FALSE, called_ ## func = FALSE
34 #define SET_EXPECT(func) \
35 expect_ ## func = TRUE
37 #define CHECK_EXPECT2(func) \
38 do { \
39 ok(expect_ ##func, "unexpected call " #func "\n"); \
40 called_ ## func = TRUE; \
41 }while(0)
43 #define CHECK_EXPECT(func) \
44 do { \
45 CHECK_EXPECT2(func); \
46 expect_ ## func = FALSE; \
47 }while(0)
49 #define CHECK_CALLED(func) \
50 do { \
51 ok(called_ ## func, "expected " #func "\n"); \
52 expect_ ## func = called_ ## func = FALSE; \
53 }while(0)
55 DEFINE_EXPECT(invalid_parameter_handler);
56 DEFINE_EXPECT(yield_func);
58 static _invalid_parameter_handler (__cdecl *p_set_invalid_parameter_handler)(_invalid_parameter_handler);
60 static void __cdecl test_invalid_parameter_handler(const wchar_t *expression,
61 const wchar_t *function, const wchar_t *file,
62 unsigned line, uintptr_t arg)
64 CHECK_EXPECT(invalid_parameter_handler);
65 ok(expression == NULL, "expression is not NULL\n");
66 ok(function == NULL, "function is not NULL\n");
67 ok(file == NULL, "file is not NULL\n");
68 ok(line == 0, "line = %u\n", line);
69 ok(arg == 0, "arg = %Ix\n", arg);
72 #ifdef __i386__
74 #include "pshpack1.h"
75 struct thiscall_thunk
77 BYTE pop_eax; /* popl %eax (ret addr) */
78 BYTE pop_edx; /* popl %edx (func) */
79 BYTE pop_ecx; /* popl %ecx (this) */
80 BYTE push_eax; /* pushl %eax */
81 WORD jmp_edx; /* jmp *%edx */
83 #include "poppack.h"
85 static ULONG_PTR (WINAPI *call_thiscall_func1)( void *func, void *this );
86 static ULONG_PTR (WINAPI *call_thiscall_func2)( void *func, void *this, const void *a );
87 static ULONG_PTR (WINAPI *call_thiscall_func3)( void *func, void *this, const void *a, const void *b );
89 static void init_thiscall_thunk(void)
91 struct thiscall_thunk *thunk = VirtualAlloc( NULL, sizeof(*thunk),
92 MEM_COMMIT, PAGE_EXECUTE_READWRITE );
93 thunk->pop_eax = 0x58; /* popl %eax */
94 thunk->pop_edx = 0x5a; /* popl %edx */
95 thunk->pop_ecx = 0x59; /* popl %ecx */
96 thunk->push_eax = 0x50; /* pushl %eax */
97 thunk->jmp_edx = 0xe2ff; /* jmp *%edx */
98 call_thiscall_func1 = (void *)thunk;
99 call_thiscall_func2 = (void *)thunk;
100 call_thiscall_func3 = (void *)thunk;
103 #define call_func1(func,_this) call_thiscall_func1(func,_this)
104 #define call_func2(func,_this,a) call_thiscall_func2(func,_this,(const void*)(a))
105 #define call_func3(func,_this,a,b) call_thiscall_func3(func,_this,(const void*)(a),(const void*)(b))
107 #else
109 #define init_thiscall_thunk()
110 #define call_func1(func,_this) func(_this)
111 #define call_func2(func,_this,a) func(_this,a)
112 #define call_func3(func,_this,a,b) func(_this,a,b)
114 #endif /* __i386__ */
116 #undef __thiscall
117 #ifdef __i386__
118 #define __thiscall __stdcall
119 #else
120 #define __thiscall __cdecl
121 #endif
123 typedef unsigned char MSVCRT_bool;
125 typedef enum
127 SPINWAIT_INIT,
128 SPINWAIT_SPIN,
129 SPINWAIT_YIELD,
130 SPINWAIT_DONE
131 } SpinWait_state;
133 typedef void (__cdecl *yield_func)(void);
135 typedef struct
137 ULONG spin;
138 ULONG unknown;
139 SpinWait_state state;
140 yield_func yield_func;
141 } SpinWait;
143 typedef struct {
144 CRITICAL_SECTION cs;
145 } _ReentrantBlockingLock;
147 typedef struct {
148 char pad[64];
149 } event;
151 typedef struct {
152 void *vtable;
153 } Context;
155 typedef struct {
156 void *policy_container;
157 } SchedulerPolicy;
159 struct SchedulerVtbl;
160 typedef struct {
161 struct SchedulerVtbl *vtable;
162 } Scheduler;
164 struct SchedulerVtbl {
165 Scheduler* (__thiscall *vector_dor)(Scheduler*);
166 unsigned int (__thiscall *Id)(Scheduler*);
167 unsigned int (__thiscall *GetNumberOfVirtualProcessors)(Scheduler*);
168 SchedulerPolicy* (__thiscall *GetPolicy)(Scheduler*);
169 unsigned int (__thiscall *Reference)(Scheduler*);
170 unsigned int (__thiscall *Release)(Scheduler*);
171 void (__thiscall *RegisterShutdownEvent)(Scheduler*,HANDLE);
172 void (__thiscall *Attach)(Scheduler*);
173 /* CreateScheduleGroup */
174 /* ScheduleTask */
177 static int* (__cdecl *p_errno)(void);
178 static int (__cdecl *p_wmemcpy_s)(wchar_t *dest, size_t numberOfElements, const wchar_t *src, size_t count);
179 static int (__cdecl *p_wmemmove_s)(wchar_t *dest, size_t numberOfElements, const wchar_t *src, size_t count);
180 static FILE* (__cdecl *p_fopen)(const char*,const char*);
181 static int (__cdecl *p_fclose)(FILE*);
182 static size_t (__cdecl *p_fread_s)(void*,size_t,size_t,size_t,FILE*);
183 static void (__cdecl *p_lock_file)(FILE*);
184 static void (__cdecl *p_unlock_file)(FILE*);
185 static void* (__cdecl *p__aligned_offset_malloc)(size_t, size_t, size_t);
186 static void (__cdecl *p__aligned_free)(void*);
187 static size_t (__cdecl *p__aligned_msize)(void*, size_t, size_t);
188 static int (__cdecl *p_atoi)(const char*);
190 static SpinWait* (__thiscall *pSpinWait_ctor_yield)(SpinWait*, yield_func);
191 static void (__thiscall *pSpinWait_dtor)(SpinWait*);
192 static void (__thiscall *pSpinWait__DoYield)(SpinWait*);
193 static ULONG (__thiscall *pSpinWait__NumberOfSpins)(SpinWait*);
194 static void (__thiscall *pSpinWait__SetSpinCount)(SpinWait*, unsigned int);
195 static MSVCRT_bool (__thiscall *pSpinWait__ShouldSpinAgain)(SpinWait*);
196 static MSVCRT_bool (__thiscall *pSpinWait__SpinOnce)(SpinWait*);
198 static void* (__thiscall *preader_writer_lock_ctor)(void*);
199 static void (__thiscall *preader_writer_lock_dtor)(void*);
200 static void (__thiscall *preader_writer_lock_lock)(void*);
201 static void (__thiscall *preader_writer_lock_lock_read)(void*);
202 static void (__thiscall *preader_writer_lock_unlock)(void*);
203 static MSVCRT_bool (__thiscall *preader_writer_lock_try_lock)(void*);
204 static MSVCRT_bool (__thiscall *preader_writer_lock_try_lock_read)(void*);
206 static _ReentrantBlockingLock* (__thiscall *p_ReentrantBlockingLock_ctor)(_ReentrantBlockingLock*);
207 static void (__thiscall *p_ReentrantBlockingLock_dtor)(_ReentrantBlockingLock*);
208 static void (__thiscall *p_ReentrantBlockingLock__Acquire)(_ReentrantBlockingLock*);
209 static void (__thiscall *p_ReentrantBlockingLock__Release)(_ReentrantBlockingLock*);
210 static MSVCRT_bool (__thiscall *p_ReentrantBlockingLock__TryAcquire)(_ReentrantBlockingLock*);
211 static _ReentrantBlockingLock* (__thiscall *p_NonReentrantBlockingLock_ctor)(_ReentrantBlockingLock*);
212 static void (__thiscall *p_NonReentrantBlockingLock_dtor)(_ReentrantBlockingLock*);
213 static void (__thiscall *p_NonReentrantBlockingLock__Acquire)(_ReentrantBlockingLock*);
214 static void (__thiscall *p_NonReentrantBlockingLock__Release)(_ReentrantBlockingLock*);
215 static MSVCRT_bool (__thiscall *p_NonReentrantBlockingLock__TryAcquire)(_ReentrantBlockingLock*);
217 static event* (__thiscall *p_event_ctor)(event*);
218 static void (__thiscall *p_event_dtor)(event*);
219 static void (__thiscall *p_event_reset)(event*);
220 static void (__thiscall *p_event_set)(event*);
221 static size_t (__thiscall *p_event_wait)(event*, unsigned int);
222 static int (__cdecl *p_event_wait_for_multiple)(event**, size_t, MSVCRT_bool, unsigned int);
224 static Context* (__cdecl *p_Context_CurrentContext)(void);
225 static unsigned int (__cdecl *p_Context_Id)(void);
226 static SchedulerPolicy* (__thiscall *p_SchedulerPolicy_ctor)(SchedulerPolicy*);
227 static void (__thiscall *p_SchedulerPolicy_SetConcurrencyLimits)(SchedulerPolicy*, unsigned int, unsigned int);
228 static void (__thiscall *p_SchedulerPolicy_dtor)(SchedulerPolicy*);
229 static Scheduler* (__cdecl *p_Scheduler_Create)(SchedulerPolicy*);
230 static Scheduler* (__cdecl *p_CurrentScheduler_Get)(void);
231 static void (__cdecl *p_CurrentScheduler_Detach)(void);
232 static unsigned int (__cdecl *p_CurrentScheduler_Id)(void);
234 static int (__cdecl *p__memicmp)(const char*, const char*, size_t);
235 static int (__cdecl *p__memicmp_l)(const char*, const char*, size_t,_locale_t);
237 static char* (__cdecl *p_setlocale)(int, const char*);
238 static size_t (__cdecl *p___strncnt)(const char*, size_t);
240 static int (__cdecl *p_strcmp)(const char *, const char *);
241 static int (__cdecl *p_strncmp)(const char *, const char *, size_t);
243 /* make sure we use the correct errno */
244 #undef errno
245 #define errno (*p_errno())
247 #define SETNOFAIL(x,y) x = (void*)GetProcAddress(hcrt,y)
248 #define SET(x,y) do { SETNOFAIL(x,y); ok(x != NULL, "Export '%s' not found\n", y); } while(0)
250 static BOOL init(void)
252 HMODULE hcrt;
254 SetLastError(0xdeadbeef);
255 hcrt = LoadLibraryA("msvcr100.dll");
256 if (!hcrt) {
257 win_skip("msvcr100.dll not installed (got %ld)\n", GetLastError());
258 return FALSE;
261 SET(p_errno, "_errno");
262 SET(p_set_invalid_parameter_handler, "_set_invalid_parameter_handler");
263 SET(p_wmemcpy_s, "wmemcpy_s");
264 SET(p_wmemmove_s, "wmemmove_s");
265 SET(p_fopen, "fopen");
266 SET(p_fclose, "fclose");
267 SET(p_fread_s, "fread_s");
268 SET(p_lock_file, "_lock_file");
269 SET(p_unlock_file, "_unlock_file");
270 SET(p__aligned_offset_malloc, "_aligned_offset_malloc");
271 SET(p__aligned_free, "_aligned_free");
272 SET(p__aligned_msize, "_aligned_msize");
273 SET(p_atoi, "atoi");
274 SET(p__memicmp, "_memicmp");
275 SET(p__memicmp_l, "_memicmp_l");
276 SET(p_setlocale, "setlocale");
277 SET(p___strncnt, "__strncnt");
279 SET(p_Context_Id, "?Id@Context@Concurrency@@SAIXZ");
280 SET(p_CurrentScheduler_Detach, "?Detach@CurrentScheduler@Concurrency@@SAXXZ");
281 SET(p_CurrentScheduler_Id, "?Id@CurrentScheduler@Concurrency@@SAIXZ");
283 SET(p_strcmp, "strcmp");
284 SET(p_strncmp, "strncmp");
286 if(sizeof(void*) == 8) { /* 64-bit initialization */
287 SET(pSpinWait_ctor_yield, "??0?$_SpinWait@$00@details@Concurrency@@QEAA@P6AXXZ@Z");
288 SET(pSpinWait_dtor, "??_F?$_SpinWait@$00@details@Concurrency@@QEAAXXZ");
289 SET(pSpinWait__DoYield, "?_DoYield@?$_SpinWait@$00@details@Concurrency@@IEAAXXZ");
290 SET(pSpinWait__NumberOfSpins, "?_NumberOfSpins@?$_SpinWait@$00@details@Concurrency@@IEAAKXZ");
291 SET(pSpinWait__SetSpinCount, "?_SetSpinCount@?$_SpinWait@$00@details@Concurrency@@QEAAXI@Z");
292 SET(pSpinWait__ShouldSpinAgain, "?_ShouldSpinAgain@?$_SpinWait@$00@details@Concurrency@@IEAA_NXZ");
293 SET(pSpinWait__SpinOnce, "?_SpinOnce@?$_SpinWait@$00@details@Concurrency@@QEAA_NXZ");
295 SET(preader_writer_lock_ctor, "??0reader_writer_lock@Concurrency@@QEAA@XZ");
296 SET(preader_writer_lock_dtor, "??1reader_writer_lock@Concurrency@@QEAA@XZ");
297 SET(preader_writer_lock_lock, "?lock@reader_writer_lock@Concurrency@@QEAAXXZ");
298 SET(preader_writer_lock_lock_read, "?lock_read@reader_writer_lock@Concurrency@@QEAAXXZ");
299 SET(preader_writer_lock_unlock, "?unlock@reader_writer_lock@Concurrency@@QEAAXXZ");
300 SET(preader_writer_lock_try_lock, "?try_lock@reader_writer_lock@Concurrency@@QEAA_NXZ");
301 SET(preader_writer_lock_try_lock_read, "?try_lock_read@reader_writer_lock@Concurrency@@QEAA_NXZ");
303 SET(p_ReentrantBlockingLock_ctor, "??0_ReentrantBlockingLock@details@Concurrency@@QEAA@XZ");
304 SET(p_ReentrantBlockingLock_dtor, "??1_ReentrantBlockingLock@details@Concurrency@@QEAA@XZ");
305 SET(p_ReentrantBlockingLock__Acquire, "?_Acquire@_ReentrantBlockingLock@details@Concurrency@@QEAAXXZ");
306 SET(p_ReentrantBlockingLock__Release, "?_Release@_ReentrantBlockingLock@details@Concurrency@@QEAAXXZ");
307 SET(p_ReentrantBlockingLock__TryAcquire, "?_TryAcquire@_ReentrantBlockingLock@details@Concurrency@@QEAA_NXZ");
308 SET(p_NonReentrantBlockingLock_ctor, "??0_NonReentrantBlockingLock@details@Concurrency@@QEAA@XZ");
309 SET(p_NonReentrantBlockingLock_dtor, "??1_NonReentrantBlockingLock@details@Concurrency@@QEAA@XZ");
310 SET(p_NonReentrantBlockingLock__Acquire, "?_Acquire@_NonReentrantBlockingLock@details@Concurrency@@QEAAXXZ");
311 SET(p_NonReentrantBlockingLock__Release, "?_Release@_NonReentrantBlockingLock@details@Concurrency@@QEAAXXZ");
312 SET(p_NonReentrantBlockingLock__TryAcquire, "?_TryAcquire@_NonReentrantBlockingLock@details@Concurrency@@QEAA_NXZ");
314 SET(p_event_ctor, "??0event@Concurrency@@QEAA@XZ");
315 SET(p_event_dtor, "??1event@Concurrency@@QEAA@XZ");
316 SET(p_event_reset, "?reset@event@Concurrency@@QEAAXXZ");
317 SET(p_event_set, "?set@event@Concurrency@@QEAAXXZ");
318 SET(p_event_wait, "?wait@event@Concurrency@@QEAA_KI@Z");
319 SET(p_event_wait_for_multiple, "?wait_for_multiple@event@Concurrency@@SA_KPEAPEAV12@_K_NI@Z");
321 SET(p_Context_CurrentContext, "?CurrentContext@Context@Concurrency@@SAPEAV12@XZ");
322 SET(p_SchedulerPolicy_ctor, "??0SchedulerPolicy@Concurrency@@QEAA@XZ");
323 SET(p_SchedulerPolicy_SetConcurrencyLimits, "?SetConcurrencyLimits@SchedulerPolicy@Concurrency@@QEAAXII@Z");
324 SET(p_SchedulerPolicy_dtor, "??1SchedulerPolicy@Concurrency@@QEAA@XZ");
325 SET(p_Scheduler_Create, "?Create@Scheduler@Concurrency@@SAPEAV12@AEBVSchedulerPolicy@2@@Z");
326 SET(p_CurrentScheduler_Get, "?Get@CurrentScheduler@Concurrency@@SAPEAVScheduler@2@XZ");
327 } else {
328 SET(pSpinWait_ctor_yield, "??0?$_SpinWait@$00@details@Concurrency@@QAE@P6AXXZ@Z");
329 SET(pSpinWait_dtor, "??_F?$_SpinWait@$00@details@Concurrency@@QAEXXZ");
330 SET(pSpinWait__DoYield, "?_DoYield@?$_SpinWait@$00@details@Concurrency@@IAEXXZ");
331 SET(pSpinWait__NumberOfSpins, "?_NumberOfSpins@?$_SpinWait@$00@details@Concurrency@@IAEKXZ");
332 SET(pSpinWait__SetSpinCount, "?_SetSpinCount@?$_SpinWait@$00@details@Concurrency@@QAEXI@Z");
333 SET(pSpinWait__ShouldSpinAgain, "?_ShouldSpinAgain@?$_SpinWait@$00@details@Concurrency@@IAE_NXZ");
334 SET(pSpinWait__SpinOnce, "?_SpinOnce@?$_SpinWait@$00@details@Concurrency@@QAE_NXZ");
336 SET(preader_writer_lock_ctor, "??0reader_writer_lock@Concurrency@@QAE@XZ");
337 SET(preader_writer_lock_dtor, "??1reader_writer_lock@Concurrency@@QAE@XZ");
338 SET(preader_writer_lock_lock, "?lock@reader_writer_lock@Concurrency@@QAEXXZ");
339 SET(preader_writer_lock_lock_read, "?lock_read@reader_writer_lock@Concurrency@@QAEXXZ");
340 SET(preader_writer_lock_unlock, "?unlock@reader_writer_lock@Concurrency@@QAEXXZ");
341 SET(preader_writer_lock_try_lock, "?try_lock@reader_writer_lock@Concurrency@@QAE_NXZ");
342 SET(preader_writer_lock_try_lock_read, "?try_lock_read@reader_writer_lock@Concurrency@@QAE_NXZ");
344 SET(p_ReentrantBlockingLock_ctor, "??0_ReentrantBlockingLock@details@Concurrency@@QAE@XZ");
345 SET(p_ReentrantBlockingLock_dtor, "??1_ReentrantBlockingLock@details@Concurrency@@QAE@XZ");
346 SET(p_ReentrantBlockingLock__Acquire, "?_Acquire@_ReentrantBlockingLock@details@Concurrency@@QAEXXZ");
347 SET(p_ReentrantBlockingLock__Release, "?_Release@_ReentrantBlockingLock@details@Concurrency@@QAEXXZ");
348 SET(p_ReentrantBlockingLock__TryAcquire, "?_TryAcquire@_ReentrantBlockingLock@details@Concurrency@@QAE_NXZ");
349 SET(p_NonReentrantBlockingLock_ctor, "??0_NonReentrantBlockingLock@details@Concurrency@@QAE@XZ");
350 SET(p_NonReentrantBlockingLock_dtor, "??1_NonReentrantBlockingLock@details@Concurrency@@QAE@XZ");
351 SET(p_NonReentrantBlockingLock__Acquire, "?_Acquire@_NonReentrantBlockingLock@details@Concurrency@@QAEXXZ");
352 SET(p_NonReentrantBlockingLock__Release, "?_Release@_NonReentrantBlockingLock@details@Concurrency@@QAEXXZ");
353 SET(p_NonReentrantBlockingLock__TryAcquire, "?_TryAcquire@_NonReentrantBlockingLock@details@Concurrency@@QAE_NXZ");
355 SET(p_event_ctor, "??0event@Concurrency@@QAE@XZ");
356 SET(p_event_dtor, "??1event@Concurrency@@QAE@XZ");
357 SET(p_event_reset, "?reset@event@Concurrency@@QAEXXZ");
358 SET(p_event_set, "?set@event@Concurrency@@QAEXXZ");
359 SET(p_event_wait, "?wait@event@Concurrency@@QAEII@Z");
360 SET(p_event_wait_for_multiple, "?wait_for_multiple@event@Concurrency@@SAIPAPAV12@I_NI@Z");
362 SET(p_Context_CurrentContext, "?CurrentContext@Context@Concurrency@@SAPAV12@XZ");
363 SET(p_SchedulerPolicy_ctor, "??0SchedulerPolicy@Concurrency@@QAE@XZ");
364 SET(p_SchedulerPolicy_SetConcurrencyLimits, "?SetConcurrencyLimits@SchedulerPolicy@Concurrency@@QAEXII@Z");
365 SET(p_SchedulerPolicy_dtor, "??1SchedulerPolicy@Concurrency@@QAE@XZ");
366 SET(p_Scheduler_Create, "?Create@Scheduler@Concurrency@@SAPAV12@ABVSchedulerPolicy@2@@Z");
367 SET(p_CurrentScheduler_Get, "?Get@CurrentScheduler@Concurrency@@SAPAVScheduler@2@XZ");
370 init_thiscall_thunk();
371 return TRUE;
374 #define okwchars(dst, b0, b1, b2, b3, b4, b5, b6, b7) \
375 ok(dst[0] == b0 && dst[1] == b1 && dst[2] == b2 && dst[3] == b3 && \
376 dst[4] == b4 && dst[5] == b5 && dst[6] == b6 && dst[7] == b7, \
377 "Bad result: 0x%04x 0x%04x 0x%04x 0x%04x 0x%04x 0x%04x 0x%04x 0x%04x\n",\
378 dst[0], dst[1], dst[2], dst[3], dst[4], dst[5], dst[6], dst[7])
380 static void test_wmemcpy_s(void)
382 static wchar_t dest[8], buf[32];
383 static const wchar_t tiny[] = L"T\0INY";
384 static const wchar_t big[] = L"atoolongstring";
385 const wchar_t XX = 0x5858; /* two 'X' bytes */
386 int ret;
388 ok(p_set_invalid_parameter_handler(test_invalid_parameter_handler) == NULL,
389 "Invalid parameter handler was already set\n");
391 /* Normal */
392 memset(dest, 'X', sizeof(dest));
393 ret = p_wmemcpy_s(dest, ARRAY_SIZE(dest), tiny, ARRAY_SIZE(tiny));
394 ok(ret == 0, "Copying a buffer into a big enough destination returned %d, expected 0\n", ret);
395 okwchars(dest, tiny[0], tiny[1], tiny[2], tiny[3], tiny[4], tiny[5], XX, XX);
397 /* Vary source size */
398 errno = 0xdeadbeef;
399 SET_EXPECT(invalid_parameter_handler);
400 memset(dest, 'X', sizeof(dest));
401 ret = p_wmemcpy_s(dest, ARRAY_SIZE(dest), big, ARRAY_SIZE(big));
402 ok(errno == ERANGE, "Copying a big buffer to a small destination errno %d, expected ERANGE\n", errno);
403 ok(ret == ERANGE, "Copying a big buffer to a small destination returned %d, expected ERANGE\n", ret);
404 okwchars(dest, 0, 0, 0, 0, 0, 0, 0, 0);
405 CHECK_CALLED(invalid_parameter_handler);
407 /* Replace source with NULL */
408 errno = 0xdeadbeef;
409 SET_EXPECT(invalid_parameter_handler);
410 memset(dest, 'X', sizeof(dest));
411 ret = p_wmemcpy_s(dest, ARRAY_SIZE(dest), NULL, ARRAY_SIZE(tiny));
412 ok(errno == EINVAL, "Copying a NULL source buffer errno %d, expected EINVAL\n", errno);
413 ok(ret == EINVAL, "Copying a NULL source buffer returned %d, expected EINVAL\n", ret);
414 okwchars(dest, 0, 0, 0, 0, 0, 0, 0, 0);
415 CHECK_CALLED(invalid_parameter_handler);
417 /* Vary dest size */
418 errno = 0xdeadbeef;
419 SET_EXPECT(invalid_parameter_handler);
420 memset(dest, 'X', sizeof(dest));
421 ret = p_wmemcpy_s(dest, 0, tiny, ARRAY_SIZE(tiny));
422 ok(errno == ERANGE, "Copying into a destination of size 0 errno %d, expected ERANGE\n", errno);
423 ok(ret == ERANGE, "Copying into a destination of size 0 returned %d, expected ERANGE\n", ret);
424 okwchars(dest, XX, XX, XX, XX, XX, XX, XX, XX);
425 CHECK_CALLED(invalid_parameter_handler);
427 /* Replace dest with NULL */
428 errno = 0xdeadbeef;
429 SET_EXPECT(invalid_parameter_handler);
430 ret = p_wmemcpy_s(NULL, ARRAY_SIZE(dest), tiny, ARRAY_SIZE(tiny));
431 ok(errno == EINVAL, "Copying a tiny buffer to a big NULL destination errno %d, expected EINVAL\n", errno);
432 ok(ret == EINVAL, "Copying a tiny buffer to a big NULL destination returned %d, expected EINVAL\n", ret);
433 CHECK_CALLED(invalid_parameter_handler);
435 /* Combinations */
436 errno = 0xdeadbeef;
437 SET_EXPECT(invalid_parameter_handler);
438 memset(dest, 'X', sizeof(dest));
439 ret = p_wmemcpy_s(dest, 0, NULL, ARRAY_SIZE(tiny));
440 ok(errno == EINVAL, "Copying a NULL buffer into a destination of size 0 errno %d, expected EINVAL\n", errno);
441 ok(ret == EINVAL, "Copying a NULL buffer into a destination of size 0 returned %d, expected EINVAL\n", ret);
442 okwchars(dest, XX, XX, XX, XX, XX, XX, XX, XX);
443 CHECK_CALLED(invalid_parameter_handler);
445 ret = p_wmemcpy_s(buf, ARRAY_SIZE(buf), big, ARRAY_SIZE(big));
446 ok(!ret, "wmemcpy_s returned %d\n", ret);
447 ok(!memcmp(buf, big, sizeof(big)), "unexpected buf\n");
449 ret = p_wmemcpy_s(buf + 1, ARRAY_SIZE(buf) - 1, buf, ARRAY_SIZE(big));
450 ok(!ret, "wmemcpy_s returned %d\n", ret);
451 ok(!memcmp(buf + 1, big, sizeof(big)), "unexpected buf\n");
453 ret = p_wmemcpy_s(buf, ARRAY_SIZE(buf), buf + 1, ARRAY_SIZE(big));
454 ok(!ret, "wmemcpy_s returned %d\n", ret);
455 ok(!memcmp(buf, big, sizeof(big)), "unexpected buf\n");
457 ok(p_set_invalid_parameter_handler(NULL) == test_invalid_parameter_handler,
458 "Cannot reset invalid parameter handler\n");
461 static void test_wmemmove_s(void)
463 static wchar_t dest[8];
464 static const wchar_t tiny[] = L"T\0INY";
465 static const wchar_t big[] = L"atoolongstring";
466 const wchar_t XX = 0x5858; /* two 'X' bytes */
467 int ret;
469 ok(p_set_invalid_parameter_handler(test_invalid_parameter_handler) == NULL,
470 "Invalid parameter handler was already set\n");
472 /* Normal */
473 memset(dest, 'X', sizeof(dest));
474 ret = p_wmemmove_s(dest, ARRAY_SIZE(dest), tiny, ARRAY_SIZE(tiny));
475 ok(ret == 0, "Moving a buffer into a big enough destination returned %d, expected 0\n", ret);
476 okwchars(dest, tiny[0], tiny[1], tiny[2], tiny[3], tiny[4], tiny[5], XX, XX);
478 /* Overlapping */
479 memcpy(dest, big, sizeof(dest));
480 ret = p_wmemmove_s(dest+1, ARRAY_SIZE(dest)-1, dest, ARRAY_SIZE(dest)-1);
481 ok(ret == 0, "Moving a buffer up one char returned %d, expected 0\n", ret);
482 okwchars(dest, big[0], big[0], big[1], big[2], big[3], big[4], big[5], big[6]);
484 /* Vary source size */
485 errno = 0xdeadbeef;
486 SET_EXPECT(invalid_parameter_handler);
487 memset(dest, 'X', sizeof(dest));
488 ret = p_wmemmove_s(dest, ARRAY_SIZE(dest), big, ARRAY_SIZE(big));
489 ok(errno == ERANGE, "Moving a big buffer to a small destination errno %d, expected ERANGE\n", errno);
490 ok(ret == ERANGE, "Moving a big buffer to a small destination returned %d, expected ERANGE\n", ret);
491 okwchars(dest, XX, XX, XX, XX, XX, XX, XX, XX);
492 CHECK_CALLED(invalid_parameter_handler);
494 /* Replace source with NULL */
495 errno = 0xdeadbeef;
496 SET_EXPECT(invalid_parameter_handler);
497 memset(dest, 'X', sizeof(dest));
498 ret = p_wmemmove_s(dest, ARRAY_SIZE(dest), NULL, ARRAY_SIZE(tiny));
499 ok(errno == EINVAL, "Moving a NULL source buffer errno %d, expected EINVAL\n", errno);
500 ok(ret == EINVAL, "Moving a NULL source buffer returned %d, expected EINVAL\n", ret);
501 okwchars(dest, XX, XX, XX, XX, XX, XX, XX, XX);
502 CHECK_CALLED(invalid_parameter_handler);
504 /* Vary dest size */
505 errno = 0xdeadbeef;
506 SET_EXPECT(invalid_parameter_handler);
507 memset(dest, 'X', sizeof(dest));
508 ret = p_wmemmove_s(dest, 0, tiny, ARRAY_SIZE(tiny));
509 ok(errno == ERANGE, "Moving into a destination of size 0 errno %d, expected ERANGE\n", errno);
510 ok(ret == ERANGE, "Moving into a destination of size 0 returned %d, expected ERANGE\n", ret);
511 okwchars(dest, XX, XX, XX, XX, XX, XX, XX, XX);
512 CHECK_CALLED(invalid_parameter_handler);
514 /* Replace dest with NULL */
515 errno = 0xdeadbeef;
516 SET_EXPECT(invalid_parameter_handler);
517 ret = p_wmemmove_s(NULL, ARRAY_SIZE(dest), tiny, ARRAY_SIZE(tiny));
518 ok(errno == EINVAL, "Moving a tiny buffer to a big NULL destination errno %d, expected EINVAL\n", errno);
519 ok(ret == EINVAL, "Moving a tiny buffer to a big NULL destination returned %d, expected EINVAL\n", ret);
520 CHECK_CALLED(invalid_parameter_handler);
522 /* Combinations */
523 errno = 0xdeadbeef;
524 SET_EXPECT(invalid_parameter_handler);
525 memset(dest, 'X', sizeof(dest));
526 ret = p_wmemmove_s(dest, 0, NULL, ARRAY_SIZE(tiny));
527 ok(errno == EINVAL, "Moving a NULL buffer into a destination of size 0 errno %d, expected EINVAL\n", errno);
528 ok(ret == EINVAL, "Moving a NULL buffer into a destination of size 0 returned %d, expected EINVAL\n", ret);
529 okwchars(dest, XX, XX, XX, XX, XX, XX, XX, XX);
530 CHECK_CALLED(invalid_parameter_handler);
532 ok(p_set_invalid_parameter_handler(NULL) == test_invalid_parameter_handler,
533 "Cannot reset invalid parameter handler\n");
536 struct block_file_arg
538 FILE *test_file;
539 HANDLE init;
540 HANDLE finish;
543 static DWORD WINAPI block_file(void *arg)
545 struct block_file_arg *files = arg;
546 p_lock_file(files->test_file);
547 SetEvent(files->init);
548 WaitForSingleObject(files->finish, INFINITE);
549 p_unlock_file(files->test_file);
550 return 0;
553 static void test_fread_s(void)
555 static const char test_file[] = "fread_s.tst";
556 int ret;
557 char buf[10];
558 HANDLE thread;
559 struct block_file_arg arg;
561 FILE *f = fopen(test_file, "w");
562 if(!f) {
563 skip("Error creating test file\n");
564 return;
566 fwrite("test", 1, 4, f);
567 fclose(f);
569 ok(p_set_invalid_parameter_handler(test_invalid_parameter_handler) == NULL,
570 "Invalid parameter handler was already set\n");
572 SET_EXPECT(invalid_parameter_handler);
573 errno = 0xdeadbeef;
574 ret = p_fread_s(buf, sizeof(buf), 1, 1, NULL);
575 ok(ret == 0, "fread_s returned %d, expected 0\n", ret);
576 ok(errno == EINVAL, "errno = %d, expected EINVAL\n", errno);
577 CHECK_CALLED(invalid_parameter_handler);
579 f = p_fopen(test_file, "r");
580 arg.test_file = f;
581 arg.init = CreateEventW(NULL, FALSE, FALSE, NULL);
582 arg.finish = CreateEventW(NULL, FALSE, FALSE, NULL);
583 thread = CreateThread(NULL, 0, block_file, (void*)&arg, 0, NULL);
584 WaitForSingleObject(arg.init, INFINITE);
586 errno = 0xdeadbeef;
587 ret = p_fread_s(NULL, sizeof(buf), 0, 1, f);
588 ok(ret == 0, "fread_s returned %d, expected 0\n", ret);
589 ok(errno == 0xdeadbeef, "errno = %d, expected 0xdeadbeef\n", errno);
590 ret = p_fread_s(NULL, sizeof(buf), 1, 0, f);
591 ok(ret == 0, "fread_s returned %d, expected 0\n", ret);
592 ok(errno == 0xdeadbeef, "errno = %d, expected 0xdeadbeef\n", errno);
594 SetEvent(arg.finish);
595 WaitForSingleObject(thread, INFINITE);
597 SET_EXPECT(invalid_parameter_handler);
598 errno = 0xdeadbeef;
599 ret = p_fread_s(NULL, sizeof(buf), 1, 1, f);
600 ok(ret == 0, "fread_s returned %d, expected 0\n", ret);
601 ok(errno == EINVAL, "errno = %d, expected EINVAL\n", errno);
602 CHECK_CALLED(invalid_parameter_handler);
604 SET_EXPECT(invalid_parameter_handler);
605 errno = 0xdeadbeef;
606 buf[1] = 'a';
607 ret = p_fread_s(buf, 3, 1, 10, f);
608 ok(ret == 0, "fread_s returned %d, expected 0\n", ret);
609 ok(buf[0] == 0, "buf[0] = '%c', expected 0\n", buf[0]);
610 ok(buf[1] == 0, "buf[1] = '%c', expected 0\n", buf[1]);
611 ok(errno == ERANGE, "errno = %d, expected ERANGE\n", errno);
612 CHECK_CALLED(invalid_parameter_handler);
614 SET_EXPECT(invalid_parameter_handler);
615 errno = 0xdeadbeef;
616 ret = p_fread_s(buf, 2, 1, 10, f);
617 ok(ret == 0, "fread_s returned %d, expected 0\n", ret);
618 ok(buf[0] == 0, "buf[0] = '%c', expected 0\n", buf[0]);
619 ok(errno == ERANGE, "errno = %d, expected ERANGE\n", errno);
620 CHECK_CALLED(invalid_parameter_handler);
622 memset(buf, 'a', sizeof(buf));
623 ret = p_fread_s(buf, sizeof(buf), 3, 10, f);
624 ok(ret==1, "fread_s returned %d, expected 1\n", ret);
625 ok(buf[0] == 'e', "buf[0] = '%c', expected 'e'\n", buf[0]);
626 ok(buf[1] == 's', "buf[1] = '%c', expected 's'\n", buf[1]);
627 ok(buf[2] == 't', "buf[2] = '%c', expected 't'\n", buf[2]);
628 ok(buf[3] == 'a', "buf[3] = '%c', expected 'a'\n", buf[3]);
629 p_fclose(f);
631 ok(p_set_invalid_parameter_handler(NULL) == test_invalid_parameter_handler,
632 "Cannot reset invalid parameter handler\n");
633 CloseHandle(arg.init);
634 CloseHandle(arg.finish);
635 CloseHandle(thread);
636 unlink(test_file);
639 static void test__aligned_msize(void)
641 void *mem;
642 int ret;
644 mem = p__aligned_offset_malloc(23, 16, 7);
645 ret = p__aligned_msize(mem, 16, 7);
646 ok(ret == 23, "_aligned_msize returned %d\n", ret);
647 ret = p__aligned_msize(mem, 15, 7);
648 ok(ret == 24, "_aligned_msize returned %d\n", ret);
649 ret = p__aligned_msize(mem, 11, 7);
650 ok(ret == 28, "_aligned_msize returned %d\n", ret);
651 ret = p__aligned_msize(mem, 1, 7);
652 ok(ret == 39-sizeof(void*), "_aligned_msize returned %d\n", ret);
653 ret = p__aligned_msize(mem, 8, 0);
654 todo_wine ok(ret == 32, "_aligned_msize returned %d\n", ret);
655 p__aligned_free(mem);
657 mem = p__aligned_offset_malloc(3, 16, 0);
658 ret = p__aligned_msize(mem, 16, 0);
659 ok(ret == 3, "_aligned_msize returned %d\n", ret);
660 ret = p__aligned_msize(mem, 11, 0);
661 ok(ret == 8, "_aligned_msize returned %d\n", ret);
662 p__aligned_free(mem);
665 static void test_atoi(void)
667 int r;
669 r = p_atoi("0");
670 ok(r == 0, "atoi(0) = %d\n", r);
672 r = p_atoi("-1");
673 ok(r == -1, "atoi(-1) = %d\n", r);
675 r = p_atoi("1");
676 ok(r == 1, "atoi(1) = %d\n", r);
678 r = p_atoi("4294967296");
679 ok(r == 2147483647, "atoi(4294967296) = %d\n", r);
682 static void __cdecl test_yield_func(void)
684 CHECK_EXPECT(yield_func);
687 static void test__SpinWait(void)
689 SpinWait sp;
690 ULONG ul;
691 MSVCRT_bool b;
693 call_func2(pSpinWait_ctor_yield, &sp, test_yield_func);
695 SET_EXPECT(yield_func);
696 call_func1(pSpinWait__DoYield, &sp);
697 CHECK_CALLED(yield_func);
699 ul = call_func1(pSpinWait__NumberOfSpins, &sp);
700 ok(ul == 1, "_SpinWait::_NumberOfSpins returned %lu\n", ul);
702 sp.spin = 2;
703 b = call_func1(pSpinWait__ShouldSpinAgain, &sp);
704 ok(b, "_SpinWait::_ShouldSpinAgain returned %x\n", b);
705 ok(sp.spin == 1, "sp.spin = %lu\n", sp.spin);
706 b = call_func1(pSpinWait__ShouldSpinAgain, &sp);
707 ok(!b, "_SpinWait::_ShouldSpinAgain returned %x\n", b);
708 ok(sp.spin == 0, "sp.spin = %lu\n", sp.spin);
709 b = call_func1(pSpinWait__ShouldSpinAgain, &sp);
710 ok(b, "_SpinWait::_ShouldSpinAgain returned %x\n", b);
711 ok(sp.spin == -1, "sp.spin = %lu\n", sp.spin);
713 call_func2(pSpinWait__SetSpinCount, &sp, 2);
714 b = call_func1(pSpinWait__SpinOnce, &sp);
715 ok(b, "_SpinWait::_SpinOnce returned %x\n", b);
716 ok(sp.spin == 1, "sp.spin = %lu\n", sp.spin);
717 b = call_func1(pSpinWait__SpinOnce, &sp);
718 ok(b, "_SpinWait::_SpinOnce returned %x\n", b);
719 ok(sp.spin == 0, "sp.spin = %lu\n", sp.spin);
720 SET_EXPECT(yield_func);
721 b = call_func1(pSpinWait__SpinOnce, &sp);
722 ok(b, "_SpinWait::_SpinOnce returned %x\n", b);
723 ok(sp.spin == 0, "sp.spin = %lu\n", sp.spin);
724 CHECK_CALLED(yield_func);
725 b = call_func1(pSpinWait__SpinOnce, &sp);
726 ok(!b, "_SpinWait::_SpinOnce returned %x\n", b);
727 ok(sp.spin==0 || sp.spin==4000, "sp.spin = %lu\n", sp.spin);
729 call_func1(pSpinWait_dtor, &sp);
732 static DWORD WINAPI lock_read_thread(void *rw_lock)
734 return (MSVCRT_bool)call_func1(preader_writer_lock_try_lock_read, rw_lock);
737 static void test_reader_writer_lock(void)
739 /* define reader_writer_lock data big enough to hold every version of structure */
740 char rw_lock[100];
741 MSVCRT_bool ret;
742 HANDLE thread;
743 DWORD d;
745 call_func1(preader_writer_lock_ctor, rw_lock);
747 ret = call_func1(preader_writer_lock_try_lock, rw_lock);
748 ok(ret, "reader_writer_lock:try_lock returned %x\n", ret);
749 ret = call_func1(preader_writer_lock_try_lock, rw_lock);
750 ok(!ret, "reader_writer_lock:try_lock returned %x\n", ret);
751 ret = call_func1(preader_writer_lock_try_lock_read, rw_lock);
752 ok(!ret, "reader_writer_lock:try_lock_read returned %x\n", ret);
753 call_func1(preader_writer_lock_unlock, rw_lock);
755 /* test lock for read on already locked section */
756 ret = call_func1(preader_writer_lock_try_lock_read, rw_lock);
757 ok(ret, "reader_writer_lock:try_lock_read returned %x\n", ret);
758 ret = call_func1(preader_writer_lock_try_lock_read, rw_lock);
759 ok(ret, "reader_writer_lock:try_lock_read returned %x\n", ret);
760 ret = call_func1(preader_writer_lock_try_lock, rw_lock);
761 ok(!ret, "reader_writer_lock:try_lock returned %x\n", ret);
762 call_func1(preader_writer_lock_unlock, rw_lock);
763 ret = call_func1(preader_writer_lock_try_lock, rw_lock);
764 ok(!ret, "reader_writer_lock:try_lock returned %x\n", ret);
765 call_func1(preader_writer_lock_unlock, rw_lock);
766 ret = call_func1(preader_writer_lock_try_lock, rw_lock);
767 ok(ret, "reader_writer_lock:try_lock returned %x\n", ret);
768 call_func1(preader_writer_lock_unlock, rw_lock);
770 call_func1(preader_writer_lock_lock, rw_lock);
771 thread = CreateThread(NULL, 0, lock_read_thread, rw_lock, 0, NULL);
772 ok(thread != NULL, "CreateThread failed: %ld\n", GetLastError());
773 WaitForSingleObject(thread, INFINITE);
774 ok(GetExitCodeThread(thread, &d), "GetExitCodeThread failed\n");
775 ok(!d, "reader_writer_lock::try_lock_read succeeded on already locked object\n");
776 CloseHandle(thread);
777 call_func1(preader_writer_lock_unlock, rw_lock);
779 call_func1(preader_writer_lock_lock_read, rw_lock);
780 thread = CreateThread(NULL, 0, lock_read_thread, rw_lock, 0, NULL);
781 ok(thread != NULL, "CreateThread failed: %ld\n", GetLastError());
782 WaitForSingleObject(thread, INFINITE);
783 ok(GetExitCodeThread(thread, &d), "GetExitCodeThread failed\n");
784 ok(d, "reader_writer_lock::try_lock_read failed on object locked for reading\n");
785 CloseHandle(thread);
786 call_func1(preader_writer_lock_unlock, rw_lock);
788 call_func1(preader_writer_lock_dtor, rw_lock);
791 static void test__ReentrantBlockingLock(void)
793 _ReentrantBlockingLock rbl;
794 MSVCRT_bool ret;
796 call_func1(p_ReentrantBlockingLock_ctor, &rbl);
797 ret = call_func1(p_ReentrantBlockingLock__TryAcquire, &rbl);
798 ok(ret, "_ReentrantBlockingLock__TryAcquire failed\n");
799 call_func1(p_ReentrantBlockingLock__Acquire, &rbl);
800 ret = call_func1(p_ReentrantBlockingLock__TryAcquire, &rbl);
801 ok(ret, "_ReentrantBlockingLock__TryAcquire failed\n");
802 call_func1(p_ReentrantBlockingLock__Release, &rbl);
803 call_func1(p_ReentrantBlockingLock__Release, &rbl);
804 call_func1(p_ReentrantBlockingLock__Release, &rbl);
805 call_func1(p_ReentrantBlockingLock_dtor, &rbl);
807 call_func1(p_NonReentrantBlockingLock_ctor, &rbl);
808 ret = call_func1(p_NonReentrantBlockingLock__TryAcquire, &rbl);
809 ok(ret, "_NonReentrantBlockingLock__TryAcquire failed\n");
810 call_func1(p_NonReentrantBlockingLock__Acquire, &rbl);
811 ret = call_func1(p_NonReentrantBlockingLock__TryAcquire, &rbl);
812 ok(ret, "_NonReentrantBlockingLock__TryAcquire failed\n");
813 call_func1(p_NonReentrantBlockingLock__Release, &rbl);
814 call_func1(p_NonReentrantBlockingLock__Release, &rbl);
815 call_func1(p_NonReentrantBlockingLock__Release, &rbl);
816 call_func1(p_NonReentrantBlockingLock_dtor, &rbl);
819 static DWORD WINAPI test_event_thread(void *arg)
821 event *evt = arg;
822 call_func1(p_event_set, evt);
823 return 0;
826 static DWORD WINAPI multiple_events_thread(void *arg)
828 event **events = arg;
830 Sleep(50);
831 call_func1(p_event_set, events[0]);
832 call_func1(p_event_reset, events[0]);
833 call_func1(p_event_set, events[1]);
834 call_func1(p_event_reset, events[1]);
835 return 0;
838 static void test_event(void)
840 int i;
841 int ret;
842 event evt;
843 event *evts[70];
844 HANDLE thread;
845 HANDLE threads[ARRAY_SIZE(evts)];
847 call_func1(p_event_ctor, &evt);
849 ret = call_func2(p_event_wait, &evt, 100);
850 ok(ret == -1, "expected -1, got %d\n", ret);
852 call_func1(p_event_set, &evt);
853 ret = call_func2(p_event_wait, &evt, 100);
854 ok(!ret, "expected 0, got %d\n", ret);
856 ret = call_func2(p_event_wait, &evt, 100);
857 ok(!ret, "expected 0, got %d\n", ret);
859 call_func1(p_event_reset, &evt);
860 ret = call_func2(p_event_wait, &evt, 100);
861 ok(ret == -1, "expected -1, got %d\n", ret);
863 thread = CreateThread(NULL, 0, test_event_thread, (void*)&evt, 0, NULL);
864 ret = call_func2(p_event_wait, &evt, 5000);
865 ok(!ret, "expected 0, got %d\n", ret);
866 WaitForSingleObject(thread, INFINITE);
867 CloseHandle(thread);
869 if (0) /* crashes on Windows */
870 p_event_wait_for_multiple(NULL, 10, TRUE, 0);
872 for (i = 0; i < ARRAY_SIZE(evts); i++) {
873 evts[i] = malloc(sizeof(evt));
874 call_func1(p_event_ctor, evts[i]);
877 ret = p_event_wait_for_multiple(evts, 0, TRUE, 100);
878 ok(!ret, "expected 0, got %d\n", ret);
880 ret = p_event_wait_for_multiple(evts, ARRAY_SIZE(evts), TRUE, 100);
881 ok(ret == -1, "expected -1, got %d\n", ret);
883 /* reset and test wait for multiple with all */
884 for (i = 0; i < ARRAY_SIZE(evts); i++)
885 threads[i] = CreateThread(NULL, 0, test_event_thread, (void*)evts[i], 0, NULL);
887 ret = p_event_wait_for_multiple(evts, ARRAY_SIZE(evts), TRUE, 5000);
888 ok(ret != -1, "didn't expect -1\n");
890 for (i = 0; i < ARRAY_SIZE(evts); i++) {
891 WaitForSingleObject(threads[i], INFINITE);
892 CloseHandle(threads[i]);
895 /* reset and test wait for multiple with any */
896 call_func1(p_event_reset, evts[0]);
898 thread = CreateThread(NULL, 0, test_event_thread, (void*)evts[0], 0, NULL);
899 ret = p_event_wait_for_multiple(evts, 1, FALSE, 5000);
900 ok(!ret, "expected 0, got %d\n", ret);
901 WaitForSingleObject(thread, INFINITE);
902 CloseHandle(thread);
904 call_func1(p_event_reset, evts[0]);
905 call_func1(p_event_reset, evts[1]);
906 thread = CreateThread(NULL, 0, multiple_events_thread, (void*)evts, 0, NULL);
907 ret = p_event_wait_for_multiple(evts, 2, TRUE, 500);
908 ok(ret == -1, "expected -1, got %d\n", ret);
909 WaitForSingleObject(thread, INFINITE);
910 CloseHandle(thread);
912 call_func1(p_event_reset, evts[0]);
913 call_func1(p_event_set, evts[1]);
914 ret = p_event_wait_for_multiple(evts, 2, FALSE, 0);
915 ok(ret == 1, "expected 1, got %d\n", ret);
917 for (i = 0; i < ARRAY_SIZE(evts); i++) {
918 call_func1(p_event_dtor, evts[i]);
919 free(evts[i]);
922 call_func1(p_event_dtor, &evt);
925 static DWORD WINAPI external_context_thread(void *arg)
927 unsigned int id;
928 Context *ctx;
930 id = p_Context_Id();
931 ok(id == -1, "Context::Id() = %u\n", id);
933 ctx = p_Context_CurrentContext();
934 ok(ctx != NULL, "Context::CurrentContext() = NULL\n");
935 id = p_Context_Id();
936 ok(id == 1, "Context::Id() = %u\n", id);
937 return 0;
940 static void test_ExternalContextBase(void)
942 unsigned int id;
943 Context *ctx;
944 HANDLE thread;
946 id = p_Context_Id();
947 ok(id == -1, "Context::Id() = %u\n", id);
949 ctx = p_Context_CurrentContext();
950 ok(ctx != NULL, "Context::CurrentContext() = NULL\n");
951 id = p_Context_Id();
952 ok(id == 0, "Context::Id() = %u\n", id);
954 ctx = p_Context_CurrentContext();
955 ok(ctx != NULL, "Context::CurrentContext() = NULL\n");
956 id = p_Context_Id();
957 ok(id == 0, "Context::Id() = %u\n", id);
959 thread = CreateThread(NULL, 0, external_context_thread, NULL, 0, NULL);
960 ok(thread != NULL, "CreateThread failed: %ld\n", GetLastError());
961 WaitForSingleObject(thread, INFINITE);
962 CloseHandle(thread);
965 static void test_Scheduler(void)
967 Scheduler *scheduler, *current_scheduler;
968 SchedulerPolicy policy;
969 unsigned int i;
971 call_func1(p_SchedulerPolicy_ctor, &policy);
972 scheduler = p_Scheduler_Create(&policy);
973 ok(scheduler != NULL, "Scheduler::Create() = NULL\n");
975 call_func1(scheduler->vtable->Attach, scheduler);
976 current_scheduler = p_CurrentScheduler_Get();
977 ok(current_scheduler == scheduler, "CurrentScheduler::Get() = %p, expected %p\n",
978 current_scheduler, scheduler);
979 p_CurrentScheduler_Detach();
981 current_scheduler = p_CurrentScheduler_Get();
982 ok(current_scheduler != scheduler, "scheduler has not changed after detach\n");
983 call_func1(scheduler->vtable->Release, scheduler);
985 i = p_CurrentScheduler_Id();
986 ok(!i, "CurrentScheduler::Id() = %u\n", i);
988 call_func3(p_SchedulerPolicy_SetConcurrencyLimits, &policy, 1, 1);
989 scheduler = p_Scheduler_Create(&policy);
990 ok(scheduler != NULL, "Scheduler::Create() = NULL\n");
992 i = call_func1(scheduler->vtable->GetNumberOfVirtualProcessors, scheduler);
993 ok(i == 1, "Scheduler::GetNumberOfVirtualProcessors() = %u\n", i);
994 call_func1(scheduler->vtable->Release, scheduler);
995 call_func1(p_SchedulerPolicy_dtor, &policy);
998 static void test__memicmp(void)
1000 static const char *s1 = "abc";
1001 static const char *s2 = "aBd";
1002 int ret;
1004 ok(p_set_invalid_parameter_handler(test_invalid_parameter_handler) == NULL,
1005 "Invalid parameter handler was already set\n");
1007 ret = p__memicmp(NULL, NULL, 0);
1008 ok(!ret, "got %d\n", ret);
1010 SET_EXPECT(invalid_parameter_handler);
1011 ret = p__memicmp(NULL, NULL, 1);
1012 ok(ret == _NLSCMPERROR, "got %d\n", ret);
1013 ok(errno == EINVAL, "errno = %d, expected EINVAL\n", errno);
1014 CHECK_CALLED(invalid_parameter_handler);
1016 SET_EXPECT(invalid_parameter_handler);
1017 ret = p__memicmp(s1, NULL, 1);
1018 ok(ret == _NLSCMPERROR, "got %d\n", ret);
1019 ok(errno == EINVAL, "errno = %d, expected EINVAL\n", errno);
1020 CHECK_CALLED(invalid_parameter_handler);
1022 SET_EXPECT(invalid_parameter_handler);
1023 ret = p__memicmp(NULL, s2, 1);
1024 ok(ret == _NLSCMPERROR, "got %d\n", ret);
1025 ok(errno == EINVAL, "errno = %d, expected EINVAL\n", errno);
1026 CHECK_CALLED(invalid_parameter_handler);
1028 ret = p__memicmp(s1, s2, 2);
1029 ok(!ret, "got %d\n", ret);
1031 ret = p__memicmp(s1, s2, 3);
1032 ok(ret == -1, "got %d\n", ret);
1034 ok(p_set_invalid_parameter_handler(NULL) == test_invalid_parameter_handler,
1035 "Cannot reset invalid parameter handler\n");
1038 static void test__memicmp_l(void)
1040 static const char *s1 = "abc";
1041 static const char *s2 = "aBd";
1042 int ret;
1044 ok(p_set_invalid_parameter_handler(test_invalid_parameter_handler) == NULL,
1045 "Invalid parameter handler was already set\n");
1047 ret = p__memicmp_l(NULL, NULL, 0, NULL);
1048 ok(!ret, "got %d\n", ret);
1050 SET_EXPECT(invalid_parameter_handler);
1051 ret = p__memicmp_l(NULL, NULL, 1, NULL);
1052 ok(ret == _NLSCMPERROR, "got %d\n", ret);
1053 ok(errno == EINVAL, "errno = %d, expected EINVAL\n", errno);
1054 CHECK_CALLED(invalid_parameter_handler);
1056 SET_EXPECT(invalid_parameter_handler);
1057 ret = p__memicmp_l(s1, NULL, 1, NULL);
1058 ok(ret == _NLSCMPERROR, "got %d\n", ret);
1059 ok(errno == EINVAL, "errno = %d, expected EINVAL\n", errno);
1060 CHECK_CALLED(invalid_parameter_handler);
1062 SET_EXPECT(invalid_parameter_handler);
1063 ret = p__memicmp_l(NULL, s2, 1, NULL);
1064 ok(ret == _NLSCMPERROR, "got %d\n", ret);
1065 ok(errno == EINVAL, "errno = %d, expected EINVAL\n", errno);
1066 CHECK_CALLED(invalid_parameter_handler);
1068 ret = p__memicmp_l(s1, s2, 2, NULL);
1069 ok(!ret, "got %d\n", ret);
1071 ret = p__memicmp_l(s1, s2, 3, NULL);
1072 ok(ret == -1, "got %d\n", ret);
1074 ok(p_set_invalid_parameter_handler(NULL) == test_invalid_parameter_handler,
1075 "Cannot reset invalid parameter handler\n");
1078 static void test_setlocale(void)
1080 char *ret;
1082 ret = p_setlocale(LC_ALL, "en-US");
1083 ok(!ret, "got %p\n", ret);
1086 static void test___strncnt(void)
1088 static const struct
1090 const char *str;
1091 size_t size;
1092 size_t ret;
1094 strncnt_tests[] =
1096 { NULL, 0, 0 },
1097 { "a", 0, 0 },
1098 { "a", 1, 1 },
1099 { "a", 10, 1 },
1100 { "abc", 1, 1 },
1102 unsigned int i;
1103 size_t ret;
1105 if (0) /* crashes */
1106 ret = p___strncnt(NULL, 1);
1108 for (i = 0; i < ARRAY_SIZE(strncnt_tests); ++i)
1110 ret = p___strncnt(strncnt_tests[i].str, strncnt_tests[i].size);
1111 ok(ret == strncnt_tests[i].ret, "%u: unexpected return value %u.\n", i, (int)ret);
1115 static void test_strcmp(void)
1117 int ret = p_strcmp( "abc", "abcd" );
1118 ok( ret == -1, "wrong ret %d\n", ret );
1119 ret = p_strcmp( "", "abc" );
1120 ok( ret == -1, "wrong ret %d\n", ret );
1121 ret = p_strcmp( "abc", "ab\xa0" );
1122 ok( ret == -1, "wrong ret %d\n", ret );
1123 ret = p_strcmp( "ab\xb0", "ab\xa0" );
1124 ok( ret == 1, "wrong ret %d\n", ret );
1125 ret = p_strcmp( "ab\xc2", "ab\xc2" );
1126 ok( ret == 0, "wrong ret %d\n", ret );
1128 ret = p_strncmp( "abc", "abcd", 3 );
1129 ok( ret == 0, "wrong ret %d\n", ret );
1130 #ifdef _WIN64
1131 ret = p_strncmp( "", "abc", 3 );
1132 ok( ret == -1, "wrong ret %d\n", ret );
1133 ret = p_strncmp( "abc", "ab\xa0", 4 );
1134 ok( ret == -1, "wrong ret %d\n", ret );
1135 ret = p_strncmp( "ab\xb0", "ab\xa0", 3 );
1136 ok( ret == 1, "wrong ret %d\n", ret );
1137 #else
1138 ret = p_strncmp( "", "abc", 3 );
1139 ok( ret == 0 - 'a', "wrong ret %d\n", ret );
1140 ret = p_strncmp( "abc", "ab\xa0", 4 );
1141 ok( ret == 'c' - 0xa0, "wrong ret %d\n", ret );
1142 ret = p_strncmp( "ab\xb0", "ab\xa0", 3 );
1143 ok( ret == 0xb0 - 0xa0, "wrong ret %d\n", ret );
1144 #endif
1145 ret = p_strncmp( "ab\xb0", "ab\xa0", 2 );
1146 ok( ret == 0, "wrong ret %d\n", ret );
1147 ret = p_strncmp( "ab\xc2", "ab\xc2", 3 );
1148 ok( ret == 0, "wrong ret %d\n", ret );
1149 ret = p_strncmp( "abc", "abd", 0 );
1150 ok( ret == 0, "wrong ret %d\n", ret );
1151 ret = p_strncmp( "abc", "abc", 12 );
1152 ok( ret == 0, "wrong ret %d\n", ret );
1155 START_TEST(msvcr100)
1157 if (!init())
1158 return;
1160 test_ExternalContextBase();
1161 test_Scheduler();
1162 test_wmemcpy_s();
1163 test_wmemmove_s();
1164 test_fread_s();
1165 test__aligned_msize();
1166 test_atoi();
1167 test__SpinWait();
1168 test_reader_writer_lock();
1169 test__ReentrantBlockingLock();
1170 test_event();
1171 test__memicmp();
1172 test__memicmp_l();
1173 test_setlocale();
1174 test___strncnt();
1175 test_strcmp();