| 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 | #if USE_BUFFER_OUTPUT |
| 56 | // Buffer for crude output routine |
| 57 | #define BUFFER_SIZE 4096 |
| 58 | static char buffer [BUFFER_SIZE]; /* "never used" warning is OK */ |
| 59 | static int idx = 0; |
| 60 | #endif |
| 61 | |
| 62 | TestOutput::WorkingEnvironment PlatformSpecificGetWorkingEnvironment() |
| 63 | { |
| 64 | return TestOutput::eclipse; |
| 65 | } |
| 66 | |
| 67 | static void C2000RunTestInASeperateProcess(UtestShell* shell, TestPlugin* plugin, TestResult* result) |
| 68 | { |
| 69 | result->addFailure(TestFailure(shell, "-p doesn't work on this platform, as it is lacking fork.\b")); |
| 70 | } |
| 71 | |
| 72 | void (*PlatformSpecificRunTestInASeperateProcess)(UtestShell*, TestPlugin*, TestResult*) = |
| 73 | C2000RunTestInASeperateProcess; |
| 74 | |
| 75 | extern "C" { |
| 76 | |
| 77 | static int C2000SetJmp(void (*function) (void* data), void* data) |
| 78 | { |
| 79 | if (0 == setjmp(test_exit_jmp_buf[jmp_buf_index])) { |
| 80 | jmp_buf_index++; |
| 81 | function(data); |
| 82 | jmp_buf_index--; |
| 83 | return 1; |
| 84 | } |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | static void C2000LongJmp() |
| 89 | { |
| 90 | jmp_buf_index--; |
| 91 | longjmp(test_exit_jmp_buf[jmp_buf_index], 1); |
| 92 | } |
| 93 | |
| 94 | static void C2000RestoreJumpBuffer() |
| 95 | { |
| 96 | jmp_buf_index--; |
| 97 | } |
| 98 | |
| 99 | int (*PlatformSpecificSetJmp)(void (*function) (void*), void*) = C2000SetJmp; |
| 100 | void (*PlatformSpecificLongJmp)(void) = C2000LongJmp; |
| 101 | void (*PlatformSpecificRestoreJumpBuffer)(void) = C2000RestoreJumpBuffer; |
| 102 | |
| 103 | static long C2000TimeInMillis() |
| 104 | { |
| 105 | /* The TI c2000 platform does not have Posix support and thus lacks struct timespec. |
| 106 | * Also, clock() always returns 0 in the simulator. Hence we work with struct tm.tm_hour |
| 107 | * This has two consequences: |
| 108 | * (1) We need to sum up the part in order to get an "elapsed since" time value, |
| 109 | * rather than just using tm_sec. |
| 110 | * (2) There is a possibility of overflow, since we stop at the hour |
| 111 | * (3) Resolution is 1 s, even though we return ms. |
| 112 | */ |
| 113 | time_t t = time((time_t*)0); |
| 114 | struct tm * ptm = gmtime(&t); |
| 115 | long result = (long) |
| 116 | ((ptm->tm_sec + ptm->tm_min * (time_t)60 + ptm->tm_hour * (time_t)3600) * (time_t)1000); |
| 117 | return result; |
| 118 | } |
| 119 | |
| 120 | static const char* TimeStringImplementation() |
| 121 | { |
| 122 | time_t tm = time(NULL); |
| 123 | return ctime(&tm); |
| 124 | } |
| 125 | |
| 126 | long (*GetPlatformSpecificTimeInMillis)() = C2000TimeInMillis; |
| 127 | const char* (*GetPlatformSpecificTimeString)() = TimeStringImplementation; |
| 128 | |
| 129 | extern int vsnprintf(char*, size_t, const char*, va_list); // not std::vsnprintf() |
| 130 | |
| 131 | extern int (*PlatformSpecificVSNprintf)(char *, size_t, const char*, va_list) = vsnprintf; |
| 132 | |
| 133 | PlatformSpecificFile C2000FOpen(const char* filename, const char* flag) |
| 134 | { |
| 135 | return fopen(filename, flag); |
| 136 | } |
| 137 | |
| 138 | static void C2000FPuts(const char* str, PlatformSpecificFile file) |
| 139 | { |
| 140 | fputs(str, (FILE*)file); |
| 141 | } |
| 142 | |
| 143 | static void C2000FClose(PlatformSpecificFile file) |
| 144 | { |
| 145 | fclose((FILE*)file); |
| 146 | } |
| 147 | |
| 148 | PlatformSpecificFile (*PlatformSpecificFOpen)(const char* filename, const char* flag) = C2000FOpen; |
| 149 | void (*PlatformSpecificFPuts)(const char* str, PlatformSpecificFile file) = C2000FPuts; |
| 150 | void (*PlatformSpecificFClose)(PlatformSpecificFile file) = C2000FClose; |
| 151 | |
| 152 | static int CL2000Putchar(int c) |
| 153 | { |
| 154 | #if USE_BUFFER_OUTPUT |
| 155 | if(idx < BUFFER_SIZE) { |
| 156 | buffer[idx] = (char) c; |
| 157 | idx++; |
| 158 | /* "buffer[idx]" instead of "c" eliminates "never used" warning */ |
| 159 | return (buffer[idx]); |
| 160 | } |
| 161 | else { |
| 162 | return EOF; |
| 163 | } |
| 164 | #else |
| 165 | return putchar(c); |
| 166 | #endif |
| 167 | } |
| 168 | |
| 169 | static void CL2000Flush() |
| 170 | { |
| 171 | fflush(stdout); |
| 172 | } |
| 173 | |
| 174 | extern int (*PlatformSpecificPutchar)(int c) = CL2000Putchar; |
| 175 | extern void (*PlatformSpecificFlush)(void) = CL2000Flush; |
| 176 | |
| 177 | static void* C2000Malloc(size_t size) |
| 178 | { |
| 179 | return (void*)far_malloc((unsigned long)size); |
| 180 | } |
| 181 | |
| 182 | static void* C2000Realloc (void* memory, size_t size) |
| 183 | { |
| 184 | return (void*)far_realloc((long)memory, (unsigned long)size); |
| 185 | } |
| 186 | |
| 187 | static void C2000Free(void* memory) |
| 188 | { |
| 189 | far_free((long)memory); |
| 190 | } |
| 191 | |
| 192 | static void* C2000MemCpy(void* s1, const void* s2, size_t size) |
| 193 | { |
| 194 | return (void*)far_memlcpy((long)s1, (long)s2, size); |
| 195 | } |
| 196 | |
| 197 | static void* C2000Memset(void* mem, int c, size_t size) |
| 198 | { |
| 199 | register unsigned long i = size; |
| 200 | register long p = (long) mem; |
| 201 | while (i--) *__farptr_to_word(p++) = c; |
| 202 | return mem; |
| 203 | } |
| 204 | |
| 205 | void* (*PlatformSpecificMalloc)(size_t size) = C2000Malloc; |
| 206 | void* (*PlatformSpecificRealloc)(void* memory, size_t size) = C2000Realloc; |
| 207 | void (*PlatformSpecificFree)(void* memory) = C2000Free; |
| 208 | void* (*PlatformSpecificMemCpy)(void* s1, const void* s2, size_t size) = C2000MemCpy; |
| 209 | void* (*PlatformSpecificMemset)(void* mem, int c, size_t size) = C2000Memset; |
| 210 | |
| 211 | /* |
| 212 | double PlatformSpecificFabs(double d) |
| 213 | { |
| 214 | return fabs(d); |
| 215 | } |
| 216 | */ |
| 217 | double (*PlatformSpecificFabs)(double) = fabs; |
| 218 | |
| 219 | static int IsNanImplementation(double d) |
| 220 | { |
| 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | static int IsInfImplementation(double d) |
| 225 | { |
| 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | int (*PlatformSpecificIsNan)(double d) = IsNanImplementation; |
| 230 | int (*PlatformSpecificIsInf)(double d) = IsInfImplementation; |
| 231 | |
| 232 | static PlatformSpecificMutex DummyMutexCreate(void) |
| 233 | { |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | static void DummyMutexLock(PlatformSpecificMutex mtx) |
| 238 | { |
| 239 | } |
| 240 | |
| 241 | static void DummyMutexUnlock(PlatformSpecificMutex mtx) |
| 242 | { |
| 243 | } |
| 244 | |
| 245 | static void DummyMutexDestroy(PlatformSpecificMutex mtx) |
| 246 | { |
| 247 | } |
| 248 | |
| 249 | PlatformSpecificMutex (*PlatformSpecificMutexCreate)(void) = DummyMutexCreate; |
| 250 | void (*PlatformSpecificMutexLock)(PlatformSpecificMutex) = DummyMutexLock; |
| 251 | void (*PlatformSpecificMutexUnlock)(PlatformSpecificMutex) = DummyMutexUnlock; |
| 252 | void (*PlatformSpecificMutexDestroy)(PlatformSpecificMutex) = DummyMutexDestroy; |
| 253 | |
| 254 | } |