-
Notifications
You must be signed in to change notification settings - Fork 529
Expand file tree
/
Copy pathUtestPlatform.cpp
More file actions
234 lines (193 loc) · 6.66 KB
/
Copy pathUtestPlatform.cpp
File metadata and controls
234 lines (193 loc) · 6.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include <stdlib.h>
#include "CppUTest/TestHarness.h"
#undef malloc
#undef free
#undef calloc
#undef realloc
#undef strdup
#undef strndup
#include <stdio.h>
#include <stdarg.h>
#include <setjmp.h>
#include <string.h>
#include <math.h>
#include <float.h>
#include <time.h>
#include "CppUTest/PlatformSpecificFunctions.h"
#include <Windows.h>
#include <mmsystem.h>
#include <setjmp.h>
#if defined(__STDC_LIB_EXT1__) || defined(__STDC_SECURE_LIB__)
#define MAYBE_SECURE_FOPEN(fp, filename, flag) fopen_s((fp), (filename), (flag))
#define MAYBE_SECURE_VSNPRINTF(str, size, trunc, format, args) _vsnprintf_s((str), (size), (trunc), (format), (args))
#define MAYBE_SECURE_LOCALTIME(_tm, timer) localtime_s((_tm), (timer))
#else
#define MAYBE_SECURE_FOPEN(fp, filename, flag) *(fp) = fopen((filename), (flag))
#define MAYBE_SECURE_VSNPRINTF(str, size, trunc, format, args) _vsnprintf((str), (size), (format), (args))
#define MAYBE_SECURE_LOCALTIME(_tm, timer) memcpy(_tm, localtime(timer), sizeof(tm));
#endif
static jmp_buf test_exit_jmp_buf[10];
static int jmp_buf_index = 0;
static int VisualCppSetJmp(void (*function) (void* data), void* data)
{
if (0 == setjmp(test_exit_jmp_buf[jmp_buf_index])) {
jmp_buf_index++;
function(data);
jmp_buf_index--;
return 1;
}
return 0;
}
CPPUTEST_NORETURN static void VisualCppLongJmp()
{
jmp_buf_index--;
longjmp(test_exit_jmp_buf[jmp_buf_index], 1);
}
static void VisualCppRestoreJumpBuffer()
{
jmp_buf_index--;
}
int (*PlatformSpecificSetJmp)(void (*function) (void*), void* data) = VisualCppSetJmp;
void (*PlatformSpecificLongJmp)(void) = VisualCppLongJmp;
void (*PlatformSpecificRestoreJumpBuffer)(void) = VisualCppRestoreJumpBuffer;
static void VisualCppRunTestInASeperateProcess(UtestShell* shell, TestPlugin* /* plugin */, TestResult* result)
{
result->addFailure(TestFailure(shell, "-p doesn't work on this platform, as it is lacking fork.\b"));
}
void (*PlatformSpecificRunTestInASeperateProcess)(UtestShell* shell, TestPlugin* plugin, TestResult* result) =
VisualCppRunTestInASeperateProcess;
TestOutput::WorkingEnvironment PlatformSpecificGetWorkingEnvironment()
{
return TestOutput::visualStudio;
}
///////////// Time in millis
static unsigned long VisualCppTimeInMillis()
{
static LARGE_INTEGER s_frequency;
static const BOOL s_use_qpc = QueryPerformanceFrequency(&s_frequency);
if (s_use_qpc)
{
LARGE_INTEGER now;
QueryPerformanceCounter(&now);
return (unsigned long)((now.QuadPart * 1000) / s_frequency.QuadPart);
}
else
{
#ifdef TIMERR_NOERROR
return (unsigned long)timeGetTime();
#else
#if !defined(_WIN32_WINNT) || !defined(_WIN32_WINNT_VISTA) || (_WIN32_WINNT < _WIN32_WINNT_VISTA)
return (unsigned long)GetTickCount();
#else
return (unsigned long)GetTickCount64();
#endif
#endif
}
}
unsigned long (*GetPlatformSpecificTimeInMillis)() = VisualCppTimeInMillis;
///////////// Time in String
static const char* VisualCppTimeString()
{
time_t the_time = time(NULLPTR);
struct tm the_local_time;
static char dateTime[80];
MAYBE_SECURE_LOCALTIME(&the_local_time, &the_time);
strftime(dateTime, 80, "%Y-%m-%dT%H:%M:%S", &the_local_time);
return dateTime;
}
const char* (*GetPlatformSpecificTimeString)() = VisualCppTimeString;
////// taken from gcc
static int VisualCppVSNprintf(char *str, size_t size, const char* format, va_list args)
{
char* buf = NULLPTR;
size_t sizeGuess = size;
int result = MAYBE_SECURE_VSNPRINTF( str, size, _TRUNCATE, format, args);
str[size-1] = 0;
while (result == -1)
{
if (buf)
free(buf);
sizeGuess += 10;
buf = (char*)malloc(sizeGuess);
result = MAYBE_SECURE_VSNPRINTF( buf, sizeGuess, _TRUNCATE, format, args);
}
if (buf)
free(buf);
return result;
}
int (*PlatformSpecificVSNprintf)(char *str, size_t size, const char* format, va_list va_args_list) = VisualCppVSNprintf;
static PlatformSpecificFile VisualCppFOpen(const char* filename, const char* flag)
{
FILE* file;
MAYBE_SECURE_FOPEN(&file, filename, flag);
return file;
}
static void VisualCppFPuts(const char* str, PlatformSpecificFile file)
{
fputs(str, (FILE*)file);
}
static void VisualCppFClose(PlatformSpecificFile file)
{
fclose((FILE*)file);
}
PlatformSpecificFile PlatformSpecificStdOut = stdout;
PlatformSpecificFile (*PlatformSpecificFOpen)(const char* filename, const char* flag) = VisualCppFOpen;
void (*PlatformSpecificFPuts)(const char* str, PlatformSpecificFile file) = VisualCppFPuts;
void (*PlatformSpecificFClose)(PlatformSpecificFile file) = VisualCppFClose;
static void VisualCppFlush()
{
fflush(stdout);
}
void (*PlatformSpecificFlush)(void) = VisualCppFlush;
static void* VisualCppMalloc(size_t size)
{
return malloc(size);
}
static void* VisualCppReAlloc(void* memory, size_t size)
{
return realloc(memory, size);
}
static void VisualCppFree(void* memory)
{
free(memory);
}
void (*PlatformSpecificSrand)(unsigned int) = srand;
int (*PlatformSpecificRand)(void) = rand;
void* (*PlatformSpecificMalloc)(size_t size) = VisualCppMalloc;
void* (*PlatformSpecificRealloc)(void* memory, size_t size) = VisualCppReAlloc;
void (*PlatformSpecificFree)(void* memory) = VisualCppFree;
void* (*PlatformSpecificMemCpy)(void* s1, const void* s2, size_t size) = memcpy;
void* (*PlatformSpecificMemset)(void* mem, int c, size_t size) = memset;
static int IsInfImplementation(double d)
{
return !_finite(d);
}
double (*PlatformSpecificFabs)(double d) = fabs;
extern "C" int (*PlatformSpecificIsNan)(double) = _isnan;
extern "C" int (*PlatformSpecificIsInf)(double) = IsInfImplementation;
int (*PlatformSpecificAtExit)(void(*func)(void)) = atexit;
static PlatformSpecificMutex VisualCppMutexCreate(void)
{
CRITICAL_SECTION *critical_section = new CRITICAL_SECTION;
InitializeCriticalSection(critical_section);
return (PlatformSpecificMutex)critical_section;
}
static void VisualCppMutexLock(PlatformSpecificMutex mutex)
{
EnterCriticalSection((CRITICAL_SECTION*)mutex);
}
static void VisualCppMutexUnlock(PlatformSpecificMutex mutex)
{
LeaveCriticalSection((CRITICAL_SECTION*)mutex);
}
static void VisualCppMutexDestroy(PlatformSpecificMutex mutex)
{
CRITICAL_SECTION *critical_section = (CRITICAL_SECTION*)mutex;
DeleteCriticalSection(critical_section);
delete critical_section;
}
PlatformSpecificMutex (*PlatformSpecificMutexCreate)(void) = VisualCppMutexCreate;
void (*PlatformSpecificMutexLock)(PlatformSpecificMutex) = VisualCppMutexLock;
void (*PlatformSpecificMutexUnlock)(PlatformSpecificMutex) = VisualCppMutexUnlock;
void (*PlatformSpecificMutexDestroy)(PlatformSpecificMutex) = VisualCppMutexDestroy;
void (*PlatformSpecificAbort)(void) = abort;