Unit Testing framework based on http://cpputest.github.io/
CppUTest
Where to find more information
- The CppUTest manual can be found at CppUTest.org
- If you have any question, check out the Google Groups
- The sources from which this library was derived may be found at the main github page
Getting test reports on the console
You may need to tailor the file src/Platforms/mbed/UtestPlatform.cpp
to your needs. In particular, if you want console output, you might want to look at the function PlatformSpecificPutchar()
.
Quick introduction (some code!)
To write your first test, all you need is a new cpp file with a TEST_GROUP and a TEST, like:
#include "CppUTest/TestHarness.h" TEST_GROUP(FirstTestGroup) { }; TEST(FirstTestGroup, FirstTest) { FAIL("Fail me!"); }
This test will fail.
You can add new tests to the test group by just writing more tests in the file, like this:
TEST(FirstTestGroup, SecondTest) { STRCMP_EQUAL("hello", "world"); LONGS_EQUAL(1, 2); CHECK(false); }
You do need to trigger the tests from somewhere in your program. It could look something like:
#include "CppUTest/TestRegistry.h" #include "CppUTest/CommandLineTestRunner.h" int main(int ac, char** av) { .... unsigned failureCount = 0; { ConsoleTestOutput output; CommandLineTestRunner runner(ac, av, &output, TestRegistry::getCurrentRegistry()); failureCount = runner.runAllTestsMain(); } if (failureCount == 0) { console.printf("PASSED\r\n"); } ... }
For more information, We’d recommend to read the manual or, even better, check some existing tests such as SimpleStringTest or (a bit more complicated) MemoryLeakDetectorTest or the mocking tests or just check out the Cheat Sheet.
src/Platforms/armcc/UtestPlatform.cpp@1:4769360130ed, 2014-06-17 (annotated)
- Committer:
- Rohit Grover
- Date:
- Tue Jun 17 15:52:54 2014 +0100
- Revision:
- 1:4769360130ed
updating to the latest version of cppUtest; tested against nordic's m-kit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Rohit Grover |
1:4769360130ed | 1 | /* |
Rohit Grover |
1:4769360130ed | 2 | * Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde |
Rohit Grover |
1:4769360130ed | 3 | * All rights reserved. |
Rohit Grover |
1:4769360130ed | 4 | * |
Rohit Grover |
1:4769360130ed | 5 | * Redistribution and use in source and binary forms, with or without |
Rohit Grover |
1:4769360130ed | 6 | * modification, are permitted provided that the following conditions are met: |
Rohit Grover |
1:4769360130ed | 7 | * * Redistributions of source code must retain the above copyright |
Rohit Grover |
1:4769360130ed | 8 | * notice, this list of conditions and the following disclaimer. |
Rohit Grover |
1:4769360130ed | 9 | * * Redistributions in binary form must reproduce the above copyright |
Rohit Grover |
1:4769360130ed | 10 | * notice, this list of conditions and the following disclaimer in the |
Rohit Grover |
1:4769360130ed | 11 | * documentation and/or other materials provided with the distribution. |
Rohit Grover |
1:4769360130ed | 12 | * * Neither the name of the <organization> nor the |
Rohit Grover |
1:4769360130ed | 13 | * names of its contributors may be used to endorse or promote products |
Rohit Grover |
1:4769360130ed | 14 | * derived from this software without specific prior written permission. |
Rohit Grover |
1:4769360130ed | 15 | * |
Rohit Grover |
1:4769360130ed | 16 | * THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY |
Rohit Grover |
1:4769360130ed | 17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
Rohit Grover |
1:4769360130ed | 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
Rohit Grover |
1:4769360130ed | 19 | * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY |
Rohit Grover |
1:4769360130ed | 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
Rohit Grover |
1:4769360130ed | 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
Rohit Grover |
1:4769360130ed | 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
Rohit Grover |
1:4769360130ed | 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
Rohit Grover |
1:4769360130ed | 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
Rohit Grover |
1:4769360130ed | 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Rohit Grover |
1:4769360130ed | 26 | */ |
Rohit Grover |
1:4769360130ed | 27 | |
Rohit Grover |
1:4769360130ed | 28 | #include <cstdlib> |
Rohit Grover |
1:4769360130ed | 29 | #include "CppUTest/TestHarness.h" |
Rohit Grover |
1:4769360130ed | 30 | #undef malloc |
Rohit Grover |
1:4769360130ed | 31 | #undef free |
Rohit Grover |
1:4769360130ed | 32 | #undef calloc |
Rohit Grover |
1:4769360130ed | 33 | #undef realloc |
Rohit Grover |
1:4769360130ed | 34 | |
Rohit Grover |
1:4769360130ed | 35 | #define far // eliminate "meaningless type qualifier" warning |
Rohit Grover |
1:4769360130ed | 36 | #include "CppUTest/TestRegistry.h" |
Rohit Grover |
1:4769360130ed | 37 | #include <time.h> |
Rohit Grover |
1:4769360130ed | 38 | #include <stdio.h> |
Rohit Grover |
1:4769360130ed | 39 | #include <stdarg.h> |
Rohit Grover |
1:4769360130ed | 40 | #include <setjmp.h> |
Rohit Grover |
1:4769360130ed | 41 | #include <string.h> |
Rohit Grover |
1:4769360130ed | 42 | #include <math.h> |
Rohit Grover |
1:4769360130ed | 43 | #include <ctype.h> |
Rohit Grover |
1:4769360130ed | 44 | |
Rohit Grover |
1:4769360130ed | 45 | #include "CppUTest/PlatformSpecificFunctions.h" |
Rohit Grover |
1:4769360130ed | 46 | |
Rohit Grover |
1:4769360130ed | 47 | static jmp_buf test_exit_jmp_buf[2]; |
Rohit Grover |
1:4769360130ed | 48 | static int jmp_buf_index = 0; |
Rohit Grover |
1:4769360130ed | 49 | |
Rohit Grover |
1:4769360130ed | 50 | /* The ARMCC compiler will compile this function with C++ linkage, unless |
Rohit Grover |
1:4769360130ed | 51 | * we specifically tell it to use C linkage again, in the function definiton. |
Rohit Grover |
1:4769360130ed | 52 | */ |
Rohit Grover |
1:4769360130ed | 53 | extern "C" int PlatformSpecificSetJmp(void (*function) (void* data), void* data) |
Rohit Grover |
1:4769360130ed | 54 | { |
Rohit Grover |
1:4769360130ed | 55 | if (0 == setjmp(test_exit_jmp_buf[jmp_buf_index])) { |
Rohit Grover |
1:4769360130ed | 56 | jmp_buf_index++; |
Rohit Grover |
1:4769360130ed | 57 | function(data); |
Rohit Grover |
1:4769360130ed | 58 | jmp_buf_index--; |
Rohit Grover |
1:4769360130ed | 59 | return 1; |
Rohit Grover |
1:4769360130ed | 60 | } |
Rohit Grover |
1:4769360130ed | 61 | return 0; |
Rohit Grover |
1:4769360130ed | 62 | } |
Rohit Grover |
1:4769360130ed | 63 | |
Rohit Grover |
1:4769360130ed | 64 | void PlatformSpecificLongJmp() |
Rohit Grover |
1:4769360130ed | 65 | { |
Rohit Grover |
1:4769360130ed | 66 | jmp_buf_index--; |
Rohit Grover |
1:4769360130ed | 67 | longjmp(test_exit_jmp_buf[jmp_buf_index], 1); |
Rohit Grover |
1:4769360130ed | 68 | } |
Rohit Grover |
1:4769360130ed | 69 | |
Rohit Grover |
1:4769360130ed | 70 | void PlatformSpecificRestoreJumpBuffer() |
Rohit Grover |
1:4769360130ed | 71 | { |
Rohit Grover |
1:4769360130ed | 72 | jmp_buf_index--; |
Rohit Grover |
1:4769360130ed | 73 | } |
Rohit Grover |
1:4769360130ed | 74 | |
Rohit Grover |
1:4769360130ed | 75 | void PlatformSpecificRunTestInASeperateProcess(UtestShell* shell, TestPlugin* plugin, TestResult* result) |
Rohit Grover |
1:4769360130ed | 76 | { |
Rohit Grover |
1:4769360130ed | 77 | printf("-p isn' implemented for armcc. Running inside the process\b"); |
Rohit Grover |
1:4769360130ed | 78 | shell->runOneTest(plugin, *result); |
Rohit Grover |
1:4769360130ed | 79 | } |
Rohit Grover |
1:4769360130ed | 80 | |
Rohit Grover |
1:4769360130ed | 81 | TestOutput::WorkingEnvironment PlatformSpecificGetWorkingEnvironment() |
Rohit Grover |
1:4769360130ed | 82 | { |
Rohit Grover |
1:4769360130ed | 83 | return TestOutput::eclipse; |
Rohit Grover |
1:4769360130ed | 84 | } |
Rohit Grover |
1:4769360130ed | 85 | |
Rohit Grover |
1:4769360130ed | 86 | ///////////// Time in millis |
Rohit Grover |
1:4769360130ed | 87 | /* |
Rohit Grover |
1:4769360130ed | 88 | * In Keil MDK-ARM, clock() default implementation used semihosting. |
Rohit Grover |
1:4769360130ed | 89 | * Resolutions is user adjustable (1 ms for now) |
Rohit Grover |
1:4769360130ed | 90 | */ |
Rohit Grover |
1:4769360130ed | 91 | static long TimeInMillisImplementation() |
Rohit Grover |
1:4769360130ed | 92 | { |
Rohit Grover |
1:4769360130ed | 93 | clock_t t = clock(); |
Rohit Grover |
1:4769360130ed | 94 | return t; |
Rohit Grover |
1:4769360130ed | 95 | } |
Rohit Grover |
1:4769360130ed | 96 | |
Rohit Grover |
1:4769360130ed | 97 | static long (*timeInMillisFp) () = TimeInMillisImplementation; |
Rohit Grover |
1:4769360130ed | 98 | |
Rohit Grover |
1:4769360130ed | 99 | long GetPlatformSpecificTimeInMillis() |
Rohit Grover |
1:4769360130ed | 100 | { |
Rohit Grover |
1:4769360130ed | 101 | return timeInMillisFp(); |
Rohit Grover |
1:4769360130ed | 102 | } |
Rohit Grover |
1:4769360130ed | 103 | |
Rohit Grover |
1:4769360130ed | 104 | /* The ARMCC compiler will compile this function with C++ linkage, unless |
Rohit Grover |
1:4769360130ed | 105 | * we specifically tell it to use C linkage again, in the function definiton. |
Rohit Grover |
1:4769360130ed | 106 | */ |
Rohit Grover |
1:4769360130ed | 107 | extern "C" void SetPlatformSpecificTimeInMillisMethod(long (*platformSpecific) ()) |
Rohit Grover |
1:4769360130ed | 108 | { |
Rohit Grover |
1:4769360130ed | 109 | timeInMillisFp = (platformSpecific == 0) ? TimeInMillisImplementation : platformSpecific; |
Rohit Grover |
1:4769360130ed | 110 | } |
Rohit Grover |
1:4769360130ed | 111 | |
Rohit Grover |
1:4769360130ed | 112 | ///////////// Time in String |
Rohit Grover |
1:4769360130ed | 113 | |
Rohit Grover |
1:4769360130ed | 114 | static const char* TimeStringImplementation() |
Rohit Grover |
1:4769360130ed | 115 | { |
Rohit Grover |
1:4769360130ed | 116 | time_t tm = time(NULL); |
Rohit Grover |
1:4769360130ed | 117 | return ctime(&tm); |
Rohit Grover |
1:4769360130ed | 118 | } |
Rohit Grover |
1:4769360130ed | 119 | |
Rohit Grover |
1:4769360130ed | 120 | static const char* (*timeStringFp) () = TimeStringImplementation; |
Rohit Grover |
1:4769360130ed | 121 | |
Rohit Grover |
1:4769360130ed | 122 | const char* GetPlatformSpecificTimeString() |
Rohit Grover |
1:4769360130ed | 123 | { |
Rohit Grover |
1:4769360130ed | 124 | return timeStringFp(); |
Rohit Grover |
1:4769360130ed | 125 | } |
Rohit Grover |
1:4769360130ed | 126 | |
Rohit Grover |
1:4769360130ed | 127 | /* The ARMCC compiler will compile this function with C++ linkage, unless |
Rohit Grover |
1:4769360130ed | 128 | * we specifically tell it to use C linkage again, in the function definiton. |
Rohit Grover |
1:4769360130ed | 129 | */ |
Rohit Grover |
1:4769360130ed | 130 | extern "C" void SetPlatformSpecificTimeStringMethod(const char* (*platformMethod) ()) |
Rohit Grover |
1:4769360130ed | 131 | { |
Rohit Grover |
1:4769360130ed | 132 | timeStringFp = (platformMethod == 0) ? TimeStringImplementation : platformMethod; |
Rohit Grover |
1:4769360130ed | 133 | } |
Rohit Grover |
1:4769360130ed | 134 | |
Rohit Grover |
1:4769360130ed | 135 | int PlatformSpecificAtoI(const char* str) |
Rohit Grover |
1:4769360130ed | 136 | { |
Rohit Grover |
1:4769360130ed | 137 | return atoi(str); |
Rohit Grover |
1:4769360130ed | 138 | } |
Rohit Grover |
1:4769360130ed | 139 | |
Rohit Grover |
1:4769360130ed | 140 | size_t PlatformSpecificStrLen(const char* str) |
Rohit Grover |
1:4769360130ed | 141 | { |
Rohit Grover |
1:4769360130ed | 142 | return strlen(str); |
Rohit Grover |
1:4769360130ed | 143 | } |
Rohit Grover |
1:4769360130ed | 144 | |
Rohit Grover |
1:4769360130ed | 145 | int PlatformSpecificStrCmp(const char* s1, const char* s2) |
Rohit Grover |
1:4769360130ed | 146 | { |
Rohit Grover |
1:4769360130ed | 147 | return strcmp(s1, s2); |
Rohit Grover |
1:4769360130ed | 148 | } |
Rohit Grover |
1:4769360130ed | 149 | |
Rohit Grover |
1:4769360130ed | 150 | int PlatformSpecificStrNCmp(const char* s1, const char* s2, size_t size) |
Rohit Grover |
1:4769360130ed | 151 | { |
Rohit Grover |
1:4769360130ed | 152 | return strncmp(s1, s2, size); |
Rohit Grover |
1:4769360130ed | 153 | } |
Rohit Grover |
1:4769360130ed | 154 | |
Rohit Grover |
1:4769360130ed | 155 | char* PlatformSpecificStrStr(const char* s1, const char* s2) |
Rohit Grover |
1:4769360130ed | 156 | { |
Rohit Grover |
1:4769360130ed | 157 | return strstr((char*)s1, (char*)s2); |
Rohit Grover |
1:4769360130ed | 158 | } |
Rohit Grover |
1:4769360130ed | 159 | |
Rohit Grover |
1:4769360130ed | 160 | /* The ARMCC compiler will compile this function with C++ linkage, unless |
Rohit Grover |
1:4769360130ed | 161 | * we specifically tell it to use C linkage again, in the function definiton. |
Rohit Grover |
1:4769360130ed | 162 | */ |
Rohit Grover |
1:4769360130ed | 163 | // extern "C" int PlatformSpecificVSNprintf(char *str, size_t size, const char* format, va_list args) |
Rohit Grover |
1:4769360130ed | 164 | int PlatformSpecificVSNprintf(char *str, size_t size, const char* format, va_list args) |
Rohit Grover |
1:4769360130ed | 165 | { |
Rohit Grover |
1:4769360130ed | 166 | return vsnprintf( str, size, format, args); |
Rohit Grover |
1:4769360130ed | 167 | } |
Rohit Grover |
1:4769360130ed | 168 | |
Rohit Grover |
1:4769360130ed | 169 | char PlatformSpecificToLower(char c) |
Rohit Grover |
1:4769360130ed | 170 | { |
Rohit Grover |
1:4769360130ed | 171 | return tolower(c); |
Rohit Grover |
1:4769360130ed | 172 | } |
Rohit Grover |
1:4769360130ed | 173 | |
Rohit Grover |
1:4769360130ed | 174 | PlatformSpecificFile PlatformSpecificFOpen(const char* filename, const char* flag) |
Rohit Grover |
1:4769360130ed | 175 | { |
Rohit Grover |
1:4769360130ed | 176 | return fopen(filename, flag); |
Rohit Grover |
1:4769360130ed | 177 | } |
Rohit Grover |
1:4769360130ed | 178 | |
Rohit Grover |
1:4769360130ed | 179 | |
Rohit Grover |
1:4769360130ed | 180 | void PlatformSpecificFPuts(const char* str, PlatformSpecificFile file) |
Rohit Grover |
1:4769360130ed | 181 | { |
Rohit Grover |
1:4769360130ed | 182 | fputs(str, (FILE*)file); |
Rohit Grover |
1:4769360130ed | 183 | } |
Rohit Grover |
1:4769360130ed | 184 | |
Rohit Grover |
1:4769360130ed | 185 | void PlatformSpecificFClose(PlatformSpecificFile file) |
Rohit Grover |
1:4769360130ed | 186 | { |
Rohit Grover |
1:4769360130ed | 187 | fclose((FILE*)file); |
Rohit Grover |
1:4769360130ed | 188 | } |
Rohit Grover |
1:4769360130ed | 189 | |
Rohit Grover |
1:4769360130ed | 190 | void PlatformSpecificFlush() |
Rohit Grover |
1:4769360130ed | 191 | { |
Rohit Grover |
1:4769360130ed | 192 | fflush(stdout); |
Rohit Grover |
1:4769360130ed | 193 | } |
Rohit Grover |
1:4769360130ed | 194 | |
Rohit Grover |
1:4769360130ed | 195 | #include "Serial.h" |
Rohit Grover |
1:4769360130ed | 196 | using namespace mbed; |
Rohit Grover |
1:4769360130ed | 197 | |
Rohit Grover |
1:4769360130ed | 198 | int PlatformSpecificPutchar(int c) |
Rohit Grover |
1:4769360130ed | 199 | { |
Rohit Grover |
1:4769360130ed | 200 | /* Please modify this block for test results to be reported as |
Rohit Grover |
1:4769360130ed | 201 | * console output. The following is a sample implementation using a |
Rohit Grover |
1:4769360130ed | 202 | * Serial object connected to the console. */ |
Rohit Grover |
1:4769360130ed | 203 | #define NEED_TEST_REPORT_AS_CONSOLE_OUTPUT 1 |
Rohit Grover |
1:4769360130ed | 204 | #if NEED_TEST_REPORT_AS_CONSOLE_OUTPUT |
Rohit Grover |
1:4769360130ed | 205 | extern Serial console; |
Rohit Grover |
1:4769360130ed | 206 | |
Rohit Grover |
1:4769360130ed | 207 | #define NEED_LINE_FEED_IN_ADDITION_TO_NEWLINE 1 |
Rohit Grover |
1:4769360130ed | 208 | #if NEED_LINE_FEED_IN_ADDITION_TO_NEWLINE |
Rohit Grover |
1:4769360130ed | 209 | /* CppUTest emits \n line terminators in its reports; some terminals |
Rohit Grover |
1:4769360130ed | 210 | * need the linefeed (\r) in addition. */ |
Rohit Grover |
1:4769360130ed | 211 | if (c == '\n') { |
Rohit Grover |
1:4769360130ed | 212 | console.putc('\r'); |
Rohit Grover |
1:4769360130ed | 213 | } |
Rohit Grover |
1:4769360130ed | 214 | #endif /* #if NEED_LINE_FEED_IN_ADDITION_TO_NEWLINE */ |
Rohit Grover |
1:4769360130ed | 215 | |
Rohit Grover |
1:4769360130ed | 216 | return (console.putc(c)); |
Rohit Grover |
1:4769360130ed | 217 | #else /* NEED_TEST_REPORT_AS_CONSOLE_OUTPUT */ |
Rohit Grover |
1:4769360130ed | 218 | return (0); |
Rohit Grover |
1:4769360130ed | 219 | #endif /* NEED_TEST_REPORT_AS_CONSOLE_OUTPUT */ |
Rohit Grover |
1:4769360130ed | 220 | } |
Rohit Grover |
1:4769360130ed | 221 | |
Rohit Grover |
1:4769360130ed | 222 | void* PlatformSpecificMalloc(size_t size) |
Rohit Grover |
1:4769360130ed | 223 | { |
Rohit Grover |
1:4769360130ed | 224 | return malloc(size); |
Rohit Grover |
1:4769360130ed | 225 | } |
Rohit Grover |
1:4769360130ed | 226 | |
Rohit Grover |
1:4769360130ed | 227 | void* PlatformSpecificRealloc (void* memory, size_t size) |
Rohit Grover |
1:4769360130ed | 228 | { |
Rohit Grover |
1:4769360130ed | 229 | return realloc(memory, size); |
Rohit Grover |
1:4769360130ed | 230 | } |
Rohit Grover |
1:4769360130ed | 231 | |
Rohit Grover |
1:4769360130ed | 232 | void PlatformSpecificFree(void* memory) |
Rohit Grover |
1:4769360130ed | 233 | { |
Rohit Grover |
1:4769360130ed | 234 | free(memory); |
Rohit Grover |
1:4769360130ed | 235 | } |
Rohit Grover |
1:4769360130ed | 236 | |
Rohit Grover |
1:4769360130ed | 237 | void* PlatformSpecificMemCpy(void* s1, const void* s2, size_t size) |
Rohit Grover |
1:4769360130ed | 238 | { |
Rohit Grover |
1:4769360130ed | 239 | return memcpy(s1, s2, size); |
Rohit Grover |
1:4769360130ed | 240 | } |
Rohit Grover |
1:4769360130ed | 241 | |
Rohit Grover |
1:4769360130ed | 242 | void* PlatformSpecificMemset(void* mem, int c, size_t size) |
Rohit Grover |
1:4769360130ed | 243 | { |
Rohit Grover |
1:4769360130ed | 244 | return memset(mem, c, size); |
Rohit Grover |
1:4769360130ed | 245 | } |
Rohit Grover |
1:4769360130ed | 246 | |
Rohit Grover |
1:4769360130ed | 247 | double PlatformSpecificFabs(double d) |
Rohit Grover |
1:4769360130ed | 248 | { |
Rohit Grover |
1:4769360130ed | 249 | return fabs(d); |
Rohit Grover |
1:4769360130ed | 250 | } |
Rohit Grover |
1:4769360130ed | 251 | |
Rohit Grover |
1:4769360130ed | 252 | int PlatformSpecificIsNan(double d) |
Rohit Grover |
1:4769360130ed | 253 | { |
Rohit Grover |
1:4769360130ed | 254 | return isnan(d); |
Rohit Grover |
1:4769360130ed | 255 | } |