Debian Codegraph @

src/Platforms/armcc/UtestPlatform.cpp

raw symbols SCIP
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
45 #define far // eliminate "meaningless type qualifier" warning
46
47 #include "CppUTest/PlatformSpecificFunctions.h"
48
49 static jmp_buf test_exit_jmp_buf[10];
50 static int jmp_buf_index = 0;
51
52 TestOutput::WorkingEnvironment PlatformSpecificGetWorkingEnvironment()
53 {
54 return TestOutput::eclipse;
55 }
56
57 static void DummyPlatformSpecificRunTestInASeperateProcess(UtestShell* shell, TestPlugin*, TestResult* result)
58 {
59 result->addFailure(TestFailure(shell, "-p doesn't work on this platform, as it is lacking fork.\b"));
60 }
61
62 static int DummyPlatformSpecificFork(void)
63 {
64 return 0;
65 }
66
67 static int DummyPlatformSpecificWaitPid(int, int*, int)
68 {
69 return 0;
70 }
71
72 void (*PlatformSpecificRunTestInASeperateProcess)(UtestShell* shell, TestPlugin* plugin, TestResult* result) =
73 DummyPlatformSpecificRunTestInASeperateProcess;
74 int (*PlatformSpecificFork)(void) = DummyPlatformSpecificFork;
75 int (*PlatformSpecificWaitPid)(int, int*, int) = DummyPlatformSpecificWaitPid;
76
77 extern "C" {
78
79 static int PlatformSpecificSetJmpImplementation(void (*function) (void* data), void* data)
80 {
81 if (0 == setjmp(test_exit_jmp_buf[jmp_buf_index])) {
82 jmp_buf_index++;
83 function(data);
84 jmp_buf_index--;
85 return 1;
86 }
87 return 0;
88 }
89
90 static void PlatformSpecificLongJmpImplementation()
91 {
92 jmp_buf_index--;
93 longjmp(test_exit_jmp_buf[jmp_buf_index], 1);
94 }
95
96 static void PlatformSpecificRestoreJumpBufferImplementation()
97 {
98 jmp_buf_index--;
99 }
100
101 void (*PlatformSpecificLongJmp)() = PlatformSpecificLongJmpImplementation;
102 int (*PlatformSpecificSetJmp)(void (*)(void*), void*) = PlatformSpecificSetJmpImplementation;
103 void (*PlatformSpecificRestoreJumpBuffer)() = PlatformSpecificRestoreJumpBufferImplementation;
104
105 ///////////// Time in millis
106 /*
107 * In Keil MDK-ARM, clock() default implementation used semihosting.
108 * Resolutions is user adjustable (1 ms for now)
109 */
110 static long TimeInMillisImplementation()
111 {
112 clock_t t = clock();
113 return t;
114 }
115
116 ///////////// Time in String
117
118 static const char* DummyTimeStringImplementation()
119 {
120 time_t tm = 0;
121 return ctime(&tm);
122 }
123
124 long (*GetPlatformSpecificTimeInMillis)() = TimeInMillisImplementation;
125 const char* (*GetPlatformSpecificTimeString)() = DummyTimeStringImplementation;
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 return fopen(filename, flag);
132 }
133
134 static void PlatformSpecificFPutsImplementation(const char* str, PlatformSpecificFile file)
135 {
136 fputs(str, (FILE*)file);
137 }
138
139 static void PlatformSpecificFCloseImplementation(PlatformSpecificFile file)
140 {
141 fclose((FILE*)file);
142 }
143
144 static void PlatformSpecificFlushImplementation()
145 {
146 fflush(stdout);
147 }
148
149 PlatformSpecificFile (*PlatformSpecificFOpen)(const char*, const char*) = PlatformSpecificFOpenImplementation;
150 void (*PlatformSpecificFPuts)(const char*, PlatformSpecificFile) = PlatformSpecificFPutsImplementation;
151 void (*PlatformSpecificFClose)(PlatformSpecificFile) = PlatformSpecificFCloseImplementation;
152
153 int (*PlatformSpecificPutchar)(int) = putchar;
154 void (*PlatformSpecificFlush)() = PlatformSpecificFlushImplementation;
155
156 void* (*PlatformSpecificMalloc)(size_t size) = malloc;
157 void* (*PlatformSpecificRealloc)(void*, size_t) = realloc;
158 void (*PlatformSpecificFree)(void* memory) = free;
159 void* (*PlatformSpecificMemCpy)(void*, const void*, size_t) = memcpy;
160 void* (*PlatformSpecificMemset)(void*, int, size_t) = memset;
161
162 static int IsNanImplementation(double d)
163 {
164 return isnan(d);
165 }
166
167 static int IsInfImplementation(double d)
168 {
169 return isinf(d);
170 }
171
172 static int AtExitImplementation(void(*func)(void))
173 {
174 return atexit(func);
175 }
176
177 double (*PlatformSpecificFabs)(double) = fabs;
178 int (*PlatformSpecificIsNan)(double) = IsNanImplementation;
179 int (*PlatformSpecificIsInf)(double) = IsInfImplementation;
180 int (*PlatformSpecificAtExit)(void(*func)(void)) = AtExitImplementation;
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 }