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