fork

Fork of cpputest by Rohit Grover

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers UtestPlatform.cpp Source File

UtestPlatform.cpp

00001 /*
00002  * Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *     * Redistributions of source code must retain the above copyright
00008  *       notice, this list of conditions and the following disclaimer.
00009  *     * Redistributions in binary form must reproduce the above copyright
00010  *       notice, this list of conditions and the following disclaimer in the
00011  *       documentation and/or other materials provided with the distribution.
00012  *     * Neither the name of the <organization> nor the
00013  *       names of its contributors may be used to endorse or promote products
00014  *       derived from this software without specific prior written permission.
00015  *
00016  * THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY
00017  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00018  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019  * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
00020  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00021  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00022  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00023  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00024  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00025  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026  */
00027 
00028 #include <cstdlib>
00029 #include "CppUTest/TestHarness.h"
00030 #undef malloc
00031 #undef free
00032 #undef calloc
00033 #undef realloc
00034 
00035 #define  far  // eliminate "meaningless type qualifier" warning
00036 #include "CppUTest/TestRegistry.h"
00037 #include <time.h>
00038 #include <stdio.h>
00039 #include <stdarg.h>
00040 #include <setjmp.h>
00041 #include <string.h>
00042 #include <math.h>
00043 #include <ctype.h>
00044 
00045 #include "CppUTest/PlatformSpecificFunctions.h"
00046 
00047 static jmp_buf test_exit_jmp_buf[2];
00048 static int jmp_buf_index = 0;
00049 
00050 /* The ARMCC compiler will compile this function with C++ linkage, unless
00051  * we specifically tell it to use C linkage again, in the function definiton.
00052  */
00053 extern "C" int PlatformSpecificSetJmp(void (*function) (void* data), void* data)
00054 {
00055     if (0 == setjmp(test_exit_jmp_buf[jmp_buf_index])) {
00056         jmp_buf_index++;
00057         function(data);
00058         jmp_buf_index--;
00059         return 1;
00060     }
00061     return 0;
00062 }
00063 
00064 void PlatformSpecificLongJmp()
00065 {
00066     jmp_buf_index--;
00067     longjmp(test_exit_jmp_buf[jmp_buf_index], 1);
00068 }
00069 
00070 void PlatformSpecificRestoreJumpBuffer()
00071 {
00072     jmp_buf_index--;
00073 }
00074 
00075 void PlatformSpecificRunTestInASeperateProcess(UtestShell* shell, TestPlugin* plugin, TestResult* result)
00076 {
00077    printf("-p isn' implemented for armcc. Running inside the process\b");
00078    shell->runOneTest(plugin, *result);
00079 }
00080 
00081 TestOutput::WorkingEnvironment PlatformSpecificGetWorkingEnvironment()
00082 {
00083     return TestOutput::eclipse;
00084 }
00085 
00086 ///////////// Time in millis
00087 /*
00088 *  In Keil MDK-ARM, clock() default implementation used semihosting.
00089 *  Resolutions is user adjustable (1 ms for now)
00090 */
00091 static long TimeInMillisImplementation()
00092 {
00093    clock_t t = clock();
00094    return t;
00095 }
00096 
00097 static long (*timeInMillisFp) () = TimeInMillisImplementation;
00098 
00099 long GetPlatformSpecificTimeInMillis()
00100 {
00101     return timeInMillisFp();
00102 }
00103 
00104 /* The ARMCC compiler will compile this function with C++ linkage, unless
00105  * we specifically tell it to use C linkage again, in the function definiton.
00106  */
00107 extern "C" void SetPlatformSpecificTimeInMillisMethod(long (*platformSpecific) ())
00108 {
00109     timeInMillisFp = (platformSpecific == 0) ? TimeInMillisImplementation : platformSpecific;
00110 }
00111 
00112 ///////////// Time in String
00113 
00114 static const char* TimeStringImplementation()
00115 {
00116     time_t tm = time(NULL);
00117     return ctime(&tm);
00118 }
00119 
00120 static const char* (*timeStringFp) () = TimeStringImplementation;
00121 
00122 const char* GetPlatformSpecificTimeString()
00123 {
00124     return timeStringFp();
00125 }
00126 
00127 /* The ARMCC compiler will compile this function with C++ linkage, unless
00128  * we specifically tell it to use C linkage again, in the function definiton.
00129  */
00130 extern "C" void SetPlatformSpecificTimeStringMethod(const char* (*platformMethod) ())
00131 {
00132     timeStringFp = (platformMethod == 0) ? TimeStringImplementation : platformMethod;
00133 }
00134 
00135 int PlatformSpecificAtoI(const char* str)
00136 {
00137    return atoi(str);
00138 }
00139 
00140 size_t PlatformSpecificStrLen(const char* str)
00141 {
00142    return strlen(str);
00143 }
00144 
00145 int PlatformSpecificStrCmp(const char* s1, const char* s2)
00146 {
00147    return strcmp(s1, s2);
00148 }
00149 
00150 int PlatformSpecificStrNCmp(const char* s1, const char* s2, size_t size)
00151 {
00152    return strncmp(s1, s2, size);
00153 }
00154 
00155 char* PlatformSpecificStrStr(const char* s1, const char* s2)
00156 {
00157    return strstr((char*)s1, (char*)s2);
00158 }
00159 
00160 /* The ARMCC compiler will compile this function with C++ linkage, unless
00161  * we specifically tell it to use C linkage again, in the function definiton.
00162  */
00163 // extern "C" int PlatformSpecificVSNprintf(char *str, size_t size, const char* format, va_list args)
00164 int PlatformSpecificVSNprintf(char *str, size_t size, const char* format, va_list args)
00165 {
00166    return vsnprintf( str, size, format, args);
00167 }
00168 
00169 char PlatformSpecificToLower(char c)
00170 {
00171     return tolower(c);
00172 }
00173 
00174 PlatformSpecificFile PlatformSpecificFOpen(const char* filename, const char* flag)
00175 {
00176    return fopen(filename, flag);
00177 }
00178 
00179 
00180 void PlatformSpecificFPuts(const char* str, PlatformSpecificFile file)
00181 {
00182    fputs(str, (FILE*)file);
00183 }
00184 
00185 void PlatformSpecificFClose(PlatformSpecificFile file)
00186 {
00187    fclose((FILE*)file);
00188 }
00189 
00190 void PlatformSpecificFlush()
00191 {
00192   fflush(stdout);
00193 }
00194 
00195 #include "Serial.h"
00196 using namespace mbed;
00197 
00198 int PlatformSpecificPutchar(int c)
00199 {
00200     /* Please modify this block for test results to be reported as
00201      * console output. The following is a sample implementation using a
00202      * Serial object connected to the console. */
00203 #define NEED_TEST_REPORT_AS_CONSOLE_OUTPUT 1
00204 #if NEED_TEST_REPORT_AS_CONSOLE_OUTPUT
00205     extern Serial mbed_cpputest_console;
00206 
00207     #define NEED_LINE_FEED_IN_ADDITION_TO_NEWLINE 1
00208     #if NEED_LINE_FEED_IN_ADDITION_TO_NEWLINE
00209     /* CppUTest emits \n line terminators in its reports; some terminals
00210      * need the linefeed (\r) in addition. */
00211     if (c == '\n') {
00212         mbed_cpputest_console.putc('\r');
00213     }
00214     #endif /* #if NEED_LINE_FEED_IN_ADDITION_TO_NEWLINE */
00215 
00216     return (mbed_cpputest_console.putc(c));
00217 #else /* NEED_TEST_REPORT_AS_CONSOLE_OUTPUT */
00218     return (0);
00219 #endif /* NEED_TEST_REPORT_AS_CONSOLE_OUTPUT */
00220 }
00221 
00222 void* PlatformSpecificMalloc(size_t size)
00223 {
00224    return malloc(size);
00225 }
00226 
00227 void* PlatformSpecificRealloc (void* memory, size_t size)
00228 {
00229    return realloc(memory, size);
00230 }
00231 
00232 void PlatformSpecificFree(void* memory)
00233 {
00234    free(memory);
00235 }
00236 
00237 void* PlatformSpecificMemCpy(void* s1, const void* s2, size_t size)
00238 {
00239    return memcpy(s1, s2, size);
00240 }
00241 
00242 void* PlatformSpecificMemset(void* mem, int c, size_t size)
00243 {
00244     return memset(mem, c, size);
00245 }
00246 
00247 double PlatformSpecificFabs(double d)
00248 {
00249    return fabs(d);
00250 }
00251 
00252 int PlatformSpecificIsNan(double d)
00253 {
00254     return isnan(d);
00255 }