| 1 | /* |
| 2 | * Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * * Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * * Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * * Neither the name of the <organization> nor the |
| 13 | * names of its contributors may be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY |
| 17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 19 | * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY |
| 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | /* Un-comment to use buffer instead of std out */ |
| 29 | // #define USE_BUFFER_OUTPUT 1 |
| 30 | #include <cstdlib> |
| 31 | |
| 32 | #include "CppUTest/TestHarness.h" |
| 33 | #undef malloc |
| 34 | #undef free |
| 35 | #undef calloc |
| 36 | #undef realloc |
| 37 | #undef strdup |
| 38 | #undef strndup |
| 39 | |
| 40 | #define far // eliminate "meaningless type qualifier" warning |
| 41 | #include <time.h> |
| 42 | #include <stdio.h> |
| 43 | #include <stdarg.h> |
| 44 | #include <setjmp.h> |
| 45 | #include <string.h> |
| 46 | #include <math.h> |
| 47 | #include <ctype.h> |
| 48 | #undef far |
| 49 | |
| 50 | #include "CppUTest/PlatformSpecificFunctions.h" |
| 51 | |
| 52 | static jmp_buf test_exit_jmp_buf[10]; |
| 53 | static int jmp_buf_index = 0; |
| 54 | |
| 55 | TestOutput::WorkingEnvironment PlatformSpecificGetWorkingEnvironment() |
| 56 | { |
| 57 | return TestOutput::eclipse; |
| 58 | } |
| 59 | |
| 60 | static void DummyRunTestInASeperateProcess(UtestShell* shell, TestPlugin* plugin, TestResult* result) |
| 61 | { |
| 62 | result->addFailure(TestFailure(shell, "-p doesn't work on this platform, as it is lacking fork.\b")); |
| 63 | } |
| 64 | |
| 65 | static int DummyPlatformSpecificFork(void) |
| 66 | { |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | static int DummyPlatformSpecificWaitPid(int, int*, int) |
| 71 | { |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | void (*PlatformSpecificRunTestInASeperateProcess)(UtestShell*, TestPlugin*, TestResult*) = DummyRunTestInASeperateProcess; |
| 76 | int (*PlatformSpecificFork)() = DummyPlatformSpecificFork; |
| 77 | int (*PlatformSpecificWaitPid)(int, int*, int) = DummyPlatformSpecificWaitPid; |
| 78 | |
| 79 | extern "C" { |
| 80 | |
| 81 | static int DosSetJmp(void (*function) (void* data), void* data) |
| 82 | { |
| 83 | if (0 == setjmp(test_exit_jmp_buf[jmp_buf_index])) { |
| 84 | jmp_buf_index++; |
| 85 | function(data); |
| 86 | jmp_buf_index--; |
| 87 | return 1; |
| 88 | } |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | static void DosLongJmp() |
| 93 | { |
| 94 | jmp_buf_index--; |
| 95 | longjmp(test_exit_jmp_buf[jmp_buf_index], 1); |
| 96 | } |
| 97 | |
| 98 | static void DosRestoreJumpBuffer() |
| 99 | { |
| 100 | jmp_buf_index--; |
| 101 | } |
| 102 | |
| 103 | int (*PlatformSpecificSetJmp)(void (*function) (void*), void*) = DosSetJmp; |
| 104 | void (*PlatformSpecificLongJmp)(void) = DosLongJmp; |
| 105 | void (*PlatformSpecificRestoreJumpBuffer)(void) = DosRestoreJumpBuffer; |
| 106 | |
| 107 | static long DosTimeInMillis() |
| 108 | { |
| 109 | return clock() * 1000 / CLOCKS_PER_SEC; |
| 110 | } |
| 111 | |
| 112 | static const char* DosTimeString() |
| 113 | { |
| 114 | time_t tm = time(NULL); |
| 115 | return ctime(&tm); |
| 116 | } |
| 117 | |
| 118 | static int DosVSNprintf(char* str, size_t size, const char* format, va_list args) { |
| 119 | return vsnprintf(str, size, format, args); |
| 120 | } |
| 121 | |
| 122 | long (*GetPlatformSpecificTimeInMillis)() = DosTimeInMillis; |
| 123 | const char* (*GetPlatformSpecificTimeString)() = DosTimeString; |
| 124 | int (*PlatformSpecificVSNprintf)(char *, size_t, const char*, va_list) = DosVSNprintf; |
| 125 | |
| 126 | PlatformSpecificFile DosFOpen(const char* filename, const char* flag) |
| 127 | { |
| 128 | return fopen(filename, flag); |
| 129 | } |
| 130 | |
| 131 | static void DosFPuts(const char* str, PlatformSpecificFile file) |
| 132 | { |
| 133 | fputs(str, (FILE*)file); |
| 134 | } |
| 135 | |
| 136 | static void DosFClose(PlatformSpecificFile file) |
| 137 | { |
| 138 | fclose((FILE*)file); |
| 139 | } |
| 140 | |
| 141 | PlatformSpecificFile (*PlatformSpecificFOpen)(const char* filename, const char* flag) = DosFOpen; |
| 142 | void (*PlatformSpecificFPuts)(const char* str, PlatformSpecificFile file) = DosFPuts; |
| 143 | void (*PlatformSpecificFClose)(PlatformSpecificFile file) = DosFClose; |
| 144 | |
| 145 | static int DosPutchar(int c) |
| 146 | { |
| 147 | return putchar(c); |
| 148 | } |
| 149 | |
| 150 | static void DosFlush() |
| 151 | { |
| 152 | fflush(stdout); |
| 153 | } |
| 154 | |
| 155 | extern int (*PlatformSpecificPutchar)(int c) = DosPutchar; |
| 156 | extern void (*PlatformSpecificFlush)(void) = DosFlush; |
| 157 | |
| 158 | static void* DosMalloc(size_t size) |
| 159 | { |
| 160 | return malloc(size); |
| 161 | } |
| 162 | |
| 163 | static void* DosRealloc (void* memory, size_t size) |
| 164 | { |
| 165 | return realloc(memory, size); |
| 166 | } |
| 167 | |
| 168 | static void DosFree(void* memory) |
| 169 | { |
| 170 | free(memory); |
| 171 | } |
| 172 | |
| 173 | static void* DosMemCpy(void* s1, const void* s2, size_t size) |
| 174 | { |
| 175 | return memcpy(s1, s2, size); |
| 176 | } |
| 177 | |
| 178 | static void* DosMemset(void* mem, int c, size_t size) |
| 179 | { |
| 180 | return memset(mem, c, size); |
| 181 | } |
| 182 | |
| 183 | void* (*PlatformSpecificMalloc)(size_t size) = DosMalloc; |
| 184 | void* (*PlatformSpecificRealloc)(void* memory, size_t size) = DosRealloc; |
| 185 | void (*PlatformSpecificFree)(void* memory) = DosFree; |
| 186 | void* (*PlatformSpecificMemCpy)(void* s1, const void* s2, size_t size) = DosMemCpy; |
| 187 | void* (*PlatformSpecificMemset)(void* mem, int c, size_t size) = DosMemset; |
| 188 | |
| 189 | static void DosSrand(unsigned int seed) |
| 190 | { |
| 191 | srand(seed); |
| 192 | } |
| 193 | |
| 194 | static int DosRand() |
| 195 | { |
| 196 | return rand(); |
| 197 | } |
| 198 | |
| 199 | static double DosFabs(double d) |
| 200 | { |
| 201 | return fabs(d); |
| 202 | } |
| 203 | |
| 204 | static int DosIsNan(double d) |
| 205 | { |
| 206 | return isnan(d); |
| 207 | } |
| 208 | |
| 209 | static int DosIsInf(double d) |
| 210 | { |
| 211 | return isinf(d); |
| 212 | } |
| 213 | |
| 214 | void (*PlatformSpecificSrand)(unsigned int) = DosSrand; |
| 215 | int (*PlatformSpecificRand)(void) = DosRand; |
| 216 | double (*PlatformSpecificFabs)(double) = DosFabs; |
| 217 | int (*PlatformSpecificIsNan)(double d) = DosIsNan; |
| 218 | int (*PlatformSpecificIsInf)(double d) = DosIsInf; |
| 219 | |
| 220 | static PlatformSpecificMutex DummyMutexCreate(void) |
| 221 | { |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | static void DummyMutexLock(PlatformSpecificMutex mtx) |
| 226 | { |
| 227 | } |
| 228 | |
| 229 | static void DummyMutexUnlock(PlatformSpecificMutex mtx) |
| 230 | { |
| 231 | } |
| 232 | |
| 233 | static void DummyMutexDestroy(PlatformSpecificMutex mtx) |
| 234 | { |
| 235 | } |
| 236 | |
| 237 | PlatformSpecificMutex (*PlatformSpecificMutexCreate)(void) = DummyMutexCreate; |
| 238 | void (*PlatformSpecificMutexLock)(PlatformSpecificMutex) = DummyMutexLock; |
| 239 | void (*PlatformSpecificMutexUnlock)(PlatformSpecificMutex) = DummyMutexUnlock; |
| 240 | void (*PlatformSpecificMutexDestroy)(PlatformSpecificMutex) = DummyMutexDestroy; |
| 241 | |
| 242 | } |