Remove support for performance tests since that never worked
[reactos.git] / reactos / regtests / shared / regtests.h
1 /*
2 * PROJECT: ReactOS kernel
3 * FILE: regtests/shared/regtests.h
4 * PURPOSE: Regression testing
5 * PROGRAMMER: Casper S. Hornstrup (chorns@users.sourceforge.net)
6 * UPDATE HISTORY:
7 * 06-07-2003 CSH Created
8 */
9 #include <stdio.h>
10 #include <string.h>
11
12 typedef DWORD (STDCALL _LPTHREAD_START_ROUTINE)(LPVOID lpParameter);
13
14 typedef struct __FILETIME
15 {
16 DWORD dwLowDateTime;
17 DWORD dwHighDateTime;
18 } _FILETIME, *_PFILETIME, *_LPFILETIME;
19
20 extern void SetupOnce();
21
22 #define _SetupOnce() \
23 void SetupOnce()
24
25 /* Valid values for Command parameter of TestRoutine */
26 #define TESTCMD_RUN 0 /* Buffer contains information about what failed */
27 #define TESTCMD_TESTTYPE 1 /* Buffer contains type of test */
28 #define TESTCMD_TESTNAME 2 /* Buffer contains description of test */
29 #define TESTCMD_TIMEOUT 3 /* Buffer contains timeout for test (DWORD, default is 5000 ms) */
30
31 /* Test types */
32 #define TT_NORMAL 0
33
34 /* Valid values for return values of TestRoutine */
35 #define TS_TIMEDOUT ((DWORD)-2)
36 #define TS_EXCEPTION ((DWORD)-1)
37 #define TS_OK 0
38 #define TS_FAILED 1
39
40 extern int _Result;
41 extern char *_Buffer;
42
43 /* Macros to simplify tests */
44 #define _DispatcherTypeTimeout(FunctionName, TestName, TestType, TimeOut) \
45 void \
46 FunctionName(int Command) \
47 { \
48 switch (Command) \
49 { \
50 case TESTCMD_RUN: \
51 RunTest(); \
52 break; \
53 case TESTCMD_TESTTYPE: \
54 *(PDWORD)_Buffer = (DWORD)TestType; \
55 break; \
56 case TESTCMD_TESTNAME: \
57 strcpy(_Buffer, TestName); \
58 break; \
59 case TESTCMD_TIMEOUT: \
60 *(PDWORD)_Buffer = (DWORD)TimeOut; \
61 break; \
62 default: \
63 _Result = TS_FAILED; \
64 break; \
65 } \
66 }
67
68 #define _DispatcherTimeout(FunctionName, TestName, TimeOut) \
69 _DispatcherTypeTimeout(FunctionName, TestName, TT_NORMAL, TimeOut)
70
71 #define _DispatcherType(FunctionName, TestName, TestType) \
72 _DispatcherTypeTimeout(FunctionName, TestName, TestType, 5000)
73
74 #define _Dispatcher(FunctionName, TestName) \
75 _DispatcherTimeout(FunctionName, TestName, 5000)
76
77 static inline void
78 AppendAssertion(char *message)
79 {
80 if (strlen(_Buffer) != 0)
81 strcat(_Buffer, "\n");
82 strcat(_Buffer, message);
83 _Result = TS_FAILED;
84 }
85
86 #define _AssertTrue(_Condition) \
87 { \
88 if (!(_Condition)) \
89 { \
90 char _message[100]; \
91 sprintf(_message, "Condition was not true at %s:%d", \
92 __FILE__, __LINE__); \
93 AppendAssertion(_message); \
94 } \
95 }
96
97 #define _AssertFalse(_Condition) \
98 { \
99 if (_Condition) \
100 { \
101 char _message[100]; \
102 sprintf(_message, "Condition was not false at %s:%d", \
103 __FILE__, __LINE__); \
104 AppendAssertion(_message); \
105 } \
106 }
107
108 #define _AssertEqualValue(_Expected, _Actual) \
109 { \
110 ULONG __Expected = (ULONG) (_Expected); \
111 ULONG __Actual = (ULONG) (_Actual); \
112 if ((__Expected) != (__Actual)) \
113 { \
114 char _message[100]; \
115 sprintf(_message, "Expected %ld/0x%.08lx was %ld/0x%.08lx at %s:%d", \
116 (__Expected), (__Expected), (__Actual), (__Actual), __FILE__, __LINE__); \
117 AppendAssertion(_message); \
118 } \
119 }
120
121 #define _AssertEqualWideString(_Expected, _Actual) \
122 { \
123 LPWSTR __Expected = (LPWSTR) (_Expected); \
124 LPWSTR __Actual = (LPWSTR) (_Actual); \
125 if (wcscmp((__Expected), (__Actual)) != 0) \
126 { \
127 char _message[100]; \
128 sprintf(_message, "Expected %S was %S at %s:%d", \
129 (__Expected), (__Actual), __FILE__, __LINE__); \
130 AppendAssertion(_message); \
131 } \
132 }
133
134 #define _AssertNotEqualValue(_Expected, _Actual) \
135 { \
136 ULONG __Expected = (ULONG) (_Expected); \
137 ULONG __Actual = (ULONG) (_Actual); \
138 if ((__Expected) == (__Actual)) \
139 { \
140 char _message[100]; \
141 sprintf(_message, "Actual value expected to be different from %ld/0x%.08lx at %s:%d", \
142 (__Expected), (__Expected), __FILE__, __LINE__); \
143 AppendAssertion(_message); \
144 } \
145 }
146
147
148 /*
149 * Test routine prototype
150 * Command - The command to process
151 */
152 typedef void (*TestRoutine)(int Command);
153
154 /*
155 * Test output routine prototype
156 * Buffer - Address of buffer with text to output
157 */
158 typedef void (*TestOutputRoutine)(char *Buffer);
159
160 /*
161 * Test driver entry routine.
162 * OutputRoutine - Output routine.
163 * TestName - If NULL all tests are run. If non-NULL specifies the test to be run
164 */
165 typedef void STDCALL (*TestDriverMain)(TestOutputRoutine OutputRoutine, char *TestName);
166
167 typedef struct __TEST
168 {
169 LIST_ENTRY ListEntry;
170 TestRoutine Routine;
171 } _TEST, *_PTEST;
172
173 extern VOID InitializeTests();
174 extern VOID RegisterTests();
175 extern VOID PerformTests(TestOutputRoutine OutputRoutine, LPSTR TestName);
176
177
178 typedef struct __API_DESCRIPTION
179 {
180 PCHAR FileName;
181 PCHAR FunctionName;
182 PCHAR ForwardedFunctionName;
183 PVOID FunctionAddress;
184 PVOID MockFunctionAddress;
185 } _API_DESCRIPTION, *_PAPI_DESCRIPTION;
186
187 extern _API_DESCRIPTION ExternalDependencies[];
188 extern ULONG MaxExternalDependency;
189
190 HANDLE STDCALL
191 _GetModuleHandleA(LPCSTR lpModuleName);
192
193 PVOID STDCALL
194 _GetProcAddress(HANDLE hModule,
195 LPCSTR lpProcName);
196
197 HANDLE STDCALL
198 _LoadLibraryA(LPCSTR lpLibFileName);
199
200 VOID STDCALL
201 _ExitProcess(UINT uExitCode);
202
203 HANDLE STDCALL
204 _CreateThread(LPSECURITY_ATTRIBUTES lpThreadAttributes, DWORD dwStackSize,
205 _LPTHREAD_START_ROUTINE lpStartAddress, LPVOID lpParameter,
206 DWORD dwCreationFlags, LPDWORD lpThreadId);
207
208 WINBOOL STDCALL
209 _TerminateThread(HANDLE hThread, DWORD dwExitCode);
210
211 DWORD STDCALL
212 _WaitForSingleObject(HANDLE hHandle, DWORD dwMilliseconds);
213
214 DWORD STDCALL
215 _GetLastError();
216
217 VOID STDCALL
218 _CloseHandle(HANDLE handle);
219
220 BOOL STDCALL
221 _GetThreadTimes(HANDLE hThread,
222 _LPFILETIME lpCreationTime,
223 _LPFILETIME lpExitTime,
224 _LPFILETIME lpKernelTime,
225 _LPFILETIME lpUserTime);
226
227 BOOL STDCALL
228 _SetPriorityClass(HANDLE hProcess, DWORD dwPriorityClass);
229
230 BOOL STDCALL
231 _SetThreadPriority(HANDLE hThread, int nPriority);
232
233 HANDLE STDCALL
234 _GetCurrentProcess();
235
236 HANDLE STDCALL
237 _GetCurrentThread();
238
239 VOID STDCALL
240 _Sleep(DWORD dwMilliseconds);
241
242
243 static inline PCHAR
244 FrameworkGetExportedFunctionNameInternal(_PAPI_DESCRIPTION ApiDescription)
245 {
246 if (ApiDescription->ForwardedFunctionName != NULL)
247 {
248 return ApiDescription->ForwardedFunctionName;
249 }
250 else
251 {
252 return ApiDescription->FunctionName;
253 }
254 }
255
256 static inline PVOID
257 FrameworkGetFunction(_PAPI_DESCRIPTION ApiDescription)
258 {
259 HANDLE hModule;
260 PVOID function = NULL;
261 PCHAR exportedFunctionName;
262
263 exportedFunctionName = FrameworkGetExportedFunctionNameInternal(ApiDescription);
264
265 hModule = _GetModuleHandleA(ApiDescription->FileName);
266 if (hModule != NULL)
267 {
268 function = _GetProcAddress(hModule, exportedFunctionName);
269 }
270 else
271 {
272 hModule = _LoadLibraryA(ApiDescription->FileName);
273 if (hModule != NULL)
274 {
275 function = _GetProcAddress(hModule, exportedFunctionName);
276 //FreeLibrary(hModule);
277 }
278 }
279 return function;
280 }
281
282 static inline PVOID
283 FrameworkGetHookInternal(ULONG index)
284 {
285 PVOID address;
286 PCHAR exportedFunctionName;
287
288 if (index > MaxExternalDependency)
289 return NULL;
290
291 if (ExternalDependencies[index].MockFunctionAddress != NULL)
292 return ExternalDependencies[index].MockFunctionAddress;
293
294 if (ExternalDependencies[index].FunctionAddress != NULL)
295 return ExternalDependencies[index].FunctionAddress;
296
297 exportedFunctionName = FrameworkGetExportedFunctionNameInternal(&ExternalDependencies[index]);
298
299 printf("Calling function '%s' in DLL '%s'.\n",
300 exportedFunctionName,
301 ExternalDependencies[index].FileName);
302
303 address = FrameworkGetFunction(&ExternalDependencies[index]);
304
305 if (address == NULL)
306 {
307 printf("Function '%s' not found in DLL '%s'.\n",
308 exportedFunctionName,
309 ExternalDependencies[index].FileName);
310 }
311 ExternalDependencies[index].FunctionAddress = address;
312
313 return address;
314 }
315
316
317 static inline VOID
318 _SetHook(PCHAR name,
319 PVOID address)
320 {
321 _PAPI_DESCRIPTION api;
322 ULONG index;
323
324 for (index = 0; index <= MaxExternalDependency; index++)
325 {
326 api = &ExternalDependencies[index];
327 if (strcmp(api->FunctionName, name) == 0)
328 {
329 api->MockFunctionAddress = address;
330 return;
331 }
332 }
333 }
334
335 typedef struct __HOOK
336 {
337 PCHAR FunctionName;
338 PVOID FunctionAddress;
339 } _HOOK, *_PHOOK;
340
341 static inline VOID
342 _SetHooks(_PHOOK hookTable)
343 {
344 _PHOOK hook;
345
346 hook = &hookTable[0];
347 while (hook->FunctionName != NULL)
348 {
349 _SetHook(hook->FunctionName,
350 hook->FunctionAddress);
351 hook++;
352 }
353 }
354
355 static inline VOID
356 _UnsetHooks(_PHOOK hookTable)
357 {
358 _PHOOK hook;
359
360 hook = &hookTable[0];
361 while (hook->FunctionName != NULL)
362 {
363 _SetHook(hook->FunctionName,
364 NULL);
365 hook++;
366 }
367 }
368
369 static inline VOID
370 _UnsetAllHooks()
371 {
372 _PAPI_DESCRIPTION api;
373 ULONG index;
374
375 for (index = 0; index <= MaxExternalDependency; index++)
376 {
377 api = &ExternalDependencies[index];
378 api->MockFunctionAddress = NULL;
379 }
380 }