| 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 | |
| 29 | #include <time.h> |
| 30 | #include <stdio.h> |
| 31 | #include <stdarg.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <setjmp.h> |
| 34 | #include <string.h> |
| 35 | #include <ctype.h> |
| 36 | #include <math.h> |
| 37 | #include "CppUTest/TestHarness.h" |
| 38 | #undef malloc |
| 39 | #undef calloc |
| 40 | #undef realloc |
| 41 | #undef free |
| 42 | #undef strdup |
| 43 | #undef strndup |
| 44 | #include "CppUTest/PlatformSpecificFunctions.h" |
| 45 | |
| 46 | static jmp_buf test_exit_jmp_buf[10]; |
| 47 | static int jmp_buf_index = 0; |
| 48 | |
| 49 | TestOutput::WorkingEnvironment PlatformSpecificGetWorkingEnvironment() |
| 50 | { |
| 51 | return TestOutput::eclipse; |
| 52 | } |
| 53 | |
| 54 | static void DummyPlatformSpecificRunTestInASeperateProcess(UtestShell* shell, TestPlugin*, TestResult* result) |
| 55 | { |
| 56 | result->addFailure(TestFailure(shell, "-p doesn't work on this platform, as it is lacking fork.\b")); |
| 57 | } |
| 58 | |
| 59 | static int DummyPlatformSpecificFork(void) |
| 60 | { |
| 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | static int DummyPlatformSpecificWaitPid(int, int*, int) |
| 65 | { |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | void (*PlatformSpecificRunTestInASeperateProcess)(UtestShell* shell, TestPlugin* plugin, TestResult* result) = |
| 70 | DummyPlatformSpecificRunTestInASeperateProcess; |
| 71 | int (*PlatformSpecificFork)(void) = DummyPlatformSpecificFork; |
| 72 | int (*PlatformSpecificWaitPid)(int, int*, int) = DummyPlatformSpecificWaitPid; |
| 73 | |
| 74 | extern "C" { |
| 75 | |
| 76 | static int PlatformSpecificSetJmpImplementation(void (*function) (void* data), void* data) |
| 77 | { |
| 78 | if (0 == setjmp(test_exit_jmp_buf[jmp_buf_index])) { |
| 79 | jmp_buf_index++; |
| 80 | function(data); |
| 81 | jmp_buf_index--; |
| 82 | return 1; |
| 83 | } |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | static void PlatformSpecificLongJmpImplementation() |
| 88 | { |
| 89 | jmp_buf_index--; |
| 90 | longjmp(test_exit_jmp_buf[jmp_buf_index], 1); |
| 91 | } |
| 92 | |
| 93 | static void PlatformSpecificRestoreJumpBufferImplementation() |
| 94 | { |
| 95 | jmp_buf_index--; |
| 96 | } |
| 97 | |
| 98 | void (*PlatformSpecificLongJmp)() = PlatformSpecificLongJmpImplementation; |
| 99 | int (*PlatformSpecificSetJmp)(void (*)(void*), void*) = PlatformSpecificSetJmpImplementation; |
| 100 | void (*PlatformSpecificRestoreJumpBuffer)() = PlatformSpecificRestoreJumpBufferImplementation; |
| 101 | |
| 102 | ///////////// Time in millis |
| 103 | |
| 104 | static long TimeInMillisImplementation() |
| 105 | { |
| 106 | clock_t t = clock(); |
| 107 | |
| 108 | t = t * 10; |
| 109 | |
| 110 | return t; |
| 111 | } |
| 112 | |
| 113 | ///////////// Time in String |
| 114 | |
| 115 | static const char* TimeStringImplementation() |
| 116 | { |
| 117 | time_t tm = time(NULL); |
| 118 | char* pTimeStr = ctime(&tm); |
| 119 | char* newlineChar = strchr(pTimeStr, '\n'); // Find the terminating newline character. |
| 120 | if(newlineChar != NULL) *newlineChar = '\0'; //If newline is found replace it with the string terminator. |
| 121 | return (pTimeStr); |
| 122 | } |
| 123 | |
| 124 | long (*GetPlatformSpecificTimeInMillis)() = TimeInMillisImplementation; |
| 125 | const char* (*GetPlatformSpecificTimeString)() = TimeStringImplementation; |
| 126 | |
| 127 | int (*PlatformSpecificVSNprintf)(char *str, size_t size, const char* format, va_list args) = vsnprintf; |
| 128 | |
| 129 | static PlatformSpecificFile PlatformSpecificFOpenImplementation(const char* filename, const char* flag) |
| 130 | { |
| 131 | static int fileNo = 0; |
| 132 | (void)filename; |
| 133 | (void)flag; |
| 134 | fileNo++; |
| 135 | return (void*)fileNo; |
| 136 | } |
| 137 | |
| 138 | static void PlatformSpecificFPutsImplementation(const char* str, PlatformSpecificFile file) |
| 139 | { |
| 140 | (void)str; |
| 141 | (void)file; |
| 142 | printf("FILE%d:%s",(int)file, str); |
| 143 | } |
| 144 | |
| 145 | static void PlatformSpecificFCloseImplementation(PlatformSpecificFile file) |
| 146 | { |
| 147 | (void)file; |
| 148 | } |
| 149 | |
| 150 | static void PlatformSpecificFlushImplementation() |
| 151 | { |
| 152 | } |
| 153 | |
| 154 | PlatformSpecificFile (*PlatformSpecificFOpen)(const char*, const char*) = PlatformSpecificFOpenImplementation; |
| 155 | void (*PlatformSpecificFPuts)(const char*, PlatformSpecificFile) = PlatformSpecificFPutsImplementation; |
| 156 | void (*PlatformSpecificFClose)(PlatformSpecificFile) = PlatformSpecificFCloseImplementation; |
| 157 | |
| 158 | int (*PlatformSpecificPutchar)(int) = putchar; |
| 159 | void (*PlatformSpecificFlush)() = PlatformSpecificFlushImplementation; |
| 160 | |
| 161 | void* (*PlatformSpecificMalloc)(size_t size) = malloc; |
| 162 | void* (*PlatformSpecificRealloc)(void*, size_t) = realloc; |
| 163 | void (*PlatformSpecificFree)(void* memory) = free; |
| 164 | void* (*PlatformSpecificMemCpy)(void*, const void*, size_t) = memcpy; |
| 165 | void* (*PlatformSpecificMemset)(void*, int, size_t) = memset; |
| 166 | |
| 167 | static int IsNanImplementation(double d) |
| 168 | { |
| 169 | return isnan(d); |
| 170 | } |
| 171 | |
| 172 | static int IsInfImplementation(double d) |
| 173 | { |
| 174 | return isinf(d); |
| 175 | } |
| 176 | |
| 177 | double (*PlatformSpecificFabs)(double) = fabs; |
| 178 | int (*PlatformSpecificIsNan)(double) = IsNanImplementation; |
| 179 | int (*PlatformSpecificIsInf)(double) = IsInfImplementation; |
| 180 | int (*PlatformSpecificAtExit)(void(*func)(void)) = atexit; /// this was undefined before |
| 181 | |
| 182 | static PlatformSpecificMutex DummyMutexCreate(void) |
| 183 | { |
| 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | static void DummyMutexLock(PlatformSpecificMutex) |
| 188 | { |
| 189 | } |
| 190 | |
| 191 | static void DummyMutexUnlock(PlatformSpecificMutex) |
| 192 | { |
| 193 | } |
| 194 | |
| 195 | static void DummyMutexDestroy(PlatformSpecificMutex) |
| 196 | { |
| 197 | } |
| 198 | |
| 199 | PlatformSpecificMutex (*PlatformSpecificMutexCreate)(void) = DummyMutexCreate; |
| 200 | void (*PlatformSpecificMutexLock)(PlatformSpecificMutex) = DummyMutexLock; |
| 201 | void (*PlatformSpecificMutexUnlock)(PlatformSpecificMutex) = DummyMutexUnlock; |
| 202 | void (*PlatformSpecificMutexDestroy)(PlatformSpecificMutex) = DummyMutexDestroy; |
| 203 | |
| 204 | } |