unity.h 17.8 KB
Newer Older
G
greg-williams 已提交
1 2 3 4 5 6
#ifndef UNITY_FRAMEWORK_H
#define UNITY_FRAMEWORK_H

#define UNITY

#include <stdio.h>
7
#include <setjmp.h>
G
greg-williams 已提交
8

9
//-------------------------------------------------------
10
// Float Support
11
//-------------------------------------------------------
12 13 14
// define UNITY_EXCLUDE_FLOAT to disallow floating point comparisons
// define UNITY_FLOAT_DELTA to specify the precision to use when doing TEST_ASSERT_EQUAL_FLOAT
// define UNITY_FLOAT_TYPE to specify doubles instead of single precision floats
15

16 17 18 19 20 21 22 23
#ifndef UNITY_EXCLUDE_FLOAT
#ifndef UNITY_FLOAT_PRECISION
#define UNITY_FLOAT_PRECISION (0.00001f)
#endif
#ifndef UNITY_FLOAT_TYPE
#define UNITY_FLOAT_TYPE float
#endif
    typedef UNITY_FLOAT_TYPE _UF;
24 25
#endif

26 27 28 29 30 31 32 33 34 35
//-------------------------------------------------------
// Int Support
//-------------------------------------------------------
// Unity assumes 32 bit integers by default
// If your compiler treats ints of a different size, define UNITY_INT_WIDTH

#ifndef UNITY_INT_WIDTH
#define UNITY_INT_WIDTH (32)
#endif
#if (UNITY_INT_WIDTH == 32)
36 37 38 39 40 41
    typedef unsigned char   _UU8;
    typedef unsigned short  _UU16;
    typedef unsigned int    _UU32;
    typedef signed char     _US8;
    typedef signed short    _US16;
    typedef signed int      _US32;
42
#elif (UNITY_INT_WIDTH == 16)
43 44 45 46 47 48 49
    typedef unsigned char   _UU8;
    typedef unsigned int    _UU16;
    typedef unsigned long   _UU32;
    typedef signed char     _US8;
    typedef signed int      _US16;
    typedef signed long     _US32;               
#else
50
    #error Invalid UNITY_INT_WIDTH specified! (32 or 16 only are currently supported)
51 52
#endif

53 54 55 56 57 58 59
//-------------------------------------------------------
// Output Method
//-------------------------------------------------------

#ifndef UNITY_OUTPUT_CHAR
#define UNITY_OUTPUT_CHAR(a) putchar(a)
#endif
60

M
mvandervoord 已提交
61
//-------------------------------------------------------
62
// Internal Structs Needed
M
mvandervoord 已提交
63 64
//-------------------------------------------------------

G
greg-williams 已提交
65 66
typedef void (*UnityTestFunction)(void);

67
typedef enum
G
greg-williams 已提交
68 69 70 71 72 73 74 75 76 77
{
    UNITY_DISPLAY_STYLE_INT,
    UNITY_DISPLAY_STYLE_UINT,
    UNITY_DISPLAY_STYLE_HEX8,
    UNITY_DISPLAY_STYLE_HEX16,
    UNITY_DISPLAY_STYLE_HEX32
} UNITY_DISPLAY_STYLE_T;

struct _Unity
{
78
    const char* TestFile;
79
		const char* AssertContainerFile;
80
    const char* CurrentTestName;
81
		unsigned long CurrentTestLineNumber;
G
greg-williams 已提交
82 83 84 85 86
    unsigned char NumberOfTests;
    unsigned char TestFailures;
    unsigned char TestIgnores;
    unsigned char CurrentTestFailed;
    unsigned char CurrentTestIgnored;
87
    jmp_buf AbortFrame;
G
greg-williams 已提交
88 89 90 91
};

extern struct _Unity Unity;

92 93 94
//-------------------------------------------------------
// Test Suite Management
//-------------------------------------------------------
G
greg-williams 已提交
95

96
void UnityBegin(void);
G
greg-williams 已提交
97
void UnityEnd(void);
98 99 100 101 102
void UnityConcludeTest(void);

//-------------------------------------------------------
// Test Output
//-------------------------------------------------------
G
greg-williams 已提交
103

104
void UnityPrint(const char* string);
105 106 107 108 109
void UnityPrintMask(const unsigned long mask, const unsigned long number);
void UnityPrintNumberByStyle(const long number, const UNITY_DISPLAY_STYLE_T style);
void UnityPrintNumber(const long number);
void UnityPrintNumberUnsigned(const unsigned long number);
void UnityPrintNumberHex(const unsigned long number, const char nibbles);
G
greg-williams 已提交
110

111 112 113
//-------------------------------------------------------
// Test Assertion Fuctions
//-------------------------------------------------------
114 115 116 117
//  Use the macros below this section instead of calling
//  these directly. The macros have a consistent naming
//  convention and will pull in file and line information
//  for you.
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139

void UnityAssertEqualNumber(const long expected,
                            const long actual,
                            const char* msg,
                            const unsigned short lineNumber,
                            const UNITY_DISPLAY_STYLE_T style);
                         
void UnityAssertEqualNumberUnsigned(const unsigned long expected,
                                    const unsigned long actual,
                                    const char* msg,
                                    const unsigned short lineNumber,
                                    const UNITY_DISPLAY_STYLE_T style);

void UnityAssertEqualIntArray(const int* expected,
                              const int* actual,
                              const unsigned long num_elements,
                              const char* msg,
                              const unsigned short lineNumber,
                              const UNITY_DISPLAY_STYLE_T style);

void UnityAssertEqualUnsignedIntArray(const unsigned int* expected,
                              const unsigned int* actual,
M
mvandervoord 已提交
140 141 142 143 144 145 146 147
                              const unsigned long num_elements,
                              const char* msg,
                              const unsigned short lineNumber,
                              const UNITY_DISPLAY_STYLE_T style);

void UnityAssertBits(const long mask,
                     const long expected,
                     const long actual,
148
                     const char* msg,
149 150
                     const unsigned short lineNumber);

151 152 153
void UnityAssertEqualString(const char* expected,
                            const char* actual,
                            const char* msg,
154 155
                            const unsigned short lineNumber );

M
mvandervoord 已提交
156 157 158
void UnityAssertEqualMemory(const void* expected,
                            const void* actual,
                            unsigned long length,
159
                            const char* msg,
160 161
                            const unsigned short lineNumber );

162
void UnityAssertEqualMemoryArray(const void* expected,
163 164 165 166 167
                                 const void* actual,
                                 unsigned long length,
                                 unsigned long num_elements,
                                 const char* msg,
                                 const unsigned short lineNumber );
168

169 170 171 172 173 174 175 176 177 178 179
void UnityAssertNumbersWithin(const long delta,
                              const long expected,
                              const long actual,
                              const char* msg,
                              const unsigned short lineNumber);

void UnityAssertNumbersUnsignedWithin(const unsigned long delta,
                                      const unsigned long expected,
                                      const unsigned long actual,
                                      const char* msg,
                                      const unsigned short lineNumber);
180

M
mvandervoord 已提交
181 182 183
void UnityFail(const char* message, const long line);

void UnityIgnore(const char* message, const long line);
184

185 186 187 188 189 190 191
#ifndef UNITY_EXCLUDE_FLOAT
void UnityAssertFloatsWithin(const _UF delta,
                             const _UF expected,
                             const _UF actual,
                             const char* msg,
                             const unsigned short lineNumber);
#endif
192

M
mvandervoord 已提交
193 194 195
//-------------------------------------------------------
// Test Running Macros
//-------------------------------------------------------
G
greg-williams 已提交
196

197
#define TEST_PROTECT() (setjmp(Unity.AbortFrame) == 0)
198

199
#define TEST_ABORT() {longjmp(Unity.AbortFrame, 1);}
200 201 202

#define ABORT_IF_NECESSARY() \
    if( Unity.CurrentTestFailed || Unity.CurrentTestIgnored ) {TEST_ABORT();}
G
greg-williams 已提交
203

204
#define RUN_TEST(func, line_num) \
G
greg-williams 已提交
205
    Unity.CurrentTestName = #func; \
206
		Unity.CurrentTestLineNumber = line_num; \
G
greg-williams 已提交
207 208 209
    Unity.NumberOfTests ++; \
    runTest(func); \
    UnityConcludeTest();
210
    
M
mvandervoord 已提交
211 212 213
//-------------------------------------------------------
// Test Asserts
//-------------------------------------------------------
214
// these are the macros you are looking for
M
mvandervoord 已提交
215

G
greg-williams 已提交
216
#define TEST_ASSERT_MESSAGE(condition, message) if (condition) {} else {TEST_FAIL(message);}
217
#define TEST_ASSERT(condition) TEST_ASSERT_MESSAGE(condition, "Expectation Failed.")
G
greg-williams 已提交
218

219
#define TEST_ASSERT_TRUE_MESSAGE(condition, message) TEST_ASSERT_MESSAGE(condition, message)
220
#define TEST_ASSERT_TRUE(condition) TEST_ASSERT_MESSAGE(condition, "Expected TRUE was FALSE.")
G
greg-williams 已提交
221

222
#define TEST_ASSERT_UNLESS_MESSAGE(condition, message) TEST_ASSERT_MESSAGE(!(condition), message)
G
greg-williams 已提交
223 224
#define TEST_ASSERT_UNLESS(condition) TEST_ASSERT(!(condition))

225
#define TEST_ASSERT_FALSE_MESSAGE(condition, message) TEST_ASSERT_MESSAGE(!(condition), message)
226
#define TEST_ASSERT_FALSE(condition) TEST_ASSERT_MESSAGE(!(condition), "Expected FALSE was TRUE.")
G
greg-williams 已提交
227 228

#define TEST_ASSERT_NULL(pointer) TEST_ASSERT_MESSAGE(pointer == NULL, #pointer " was not null.")
229
#define TEST_ASSERT_NULL_MESSAGE(pointer, message) TEST_ASSERT_MESSAGE(pointer == NULL, message)
G
greg-williams 已提交
230
#define TEST_ASSERT_NOT_NULL(pointer) TEST_ASSERT_MESSAGE(pointer != NULL, #pointer " was null.")
231
#define TEST_ASSERT_NOT_NULL_MESSAGE(pointer, message) TEST_ASSERT_MESSAGE(pointer != NULL, message)
G
greg-williams 已提交
232 233

#define TEST_ASSERT_EQUAL_INT_MESSAGE(expected, actual, message) \
234
    Unity.AssertContainerFile=__FILE__; \
235
    UnityAssertEqualNumber((long)(expected), (long)(actual), (message), (unsigned short)__LINE__, UNITY_DISPLAY_STYLE_INT); \
236
    ABORT_IF_NECESSARY();
G
greg-williams 已提交
237 238
#define TEST_ASSERT_EQUAL_INT(expected, actual)	TEST_ASSERT_EQUAL_INT_MESSAGE(expected, actual, NULL)

M
mvandervoord 已提交
239
#define TEST_ASSERT_EQUAL_INT_ARRAY_MESSAGE(expected, actual, num_elements, message) \
240
    Unity.AssertContainerFile=__FILE__; \
241
    UnityAssertEqualIntArray((const int*)(expected), (const int*)(actual), (unsigned long)(num_elements), (message), (unsigned short)__LINE__, UNITY_DISPLAY_STYLE_INT); \
M
mvandervoord 已提交
242 243 244
    ABORT_IF_NECESSARY();
#define TEST_ASSERT_EQUAL_INT_ARRAY(expected, actual, num_elements) TEST_ASSERT_EQUAL_INT_ARRAY_MESSAGE(expected, actual, num_elements, NULL)

G
greg-williams 已提交
245 246 247
#define TEST_ASSERT_EQUAL_MESSAGE(expected, actual, message) TEST_ASSERT_EQUAL_INT_MESSAGE((expected), (actual), (message))
#define TEST_ASSERT_EQUAL(expected, actual) TEST_ASSERT_EQUAL_INT(expected, actual)

248 249 250
#define TEST_ASSERT_NOT_EQUAL_MESSAGE(expected, actual, message) TEST_ASSERT_FALSE_MESSAGE(((expected) == (actual)), (message))
#define TEST_ASSERT_NOT_EQUAL(expected, actual) TEST_ASSERT_FALSE((expected) == (actual))

G
greg-williams 已提交
251
#define TEST_ASSERT_INT_WITHIN_MESSAGE(delta, expected, actual, message) \
252
    Unity.AssertContainerFile=__FILE__; \
253
    UnityAssertNumbersWithin((long)(delta), (long)(expected), (long)(actual), NULL, (unsigned short)__LINE__); \
254
    ABORT_IF_NECESSARY();
G
greg-williams 已提交
255 256 257
#define TEST_ASSERT_INT_WITHIN(delta, expected, actual) TEST_ASSERT_INT_WITHIN_MESSAGE(delta, expected, actual, NULL)

#define TEST_ASSERT_EQUAL_UINT_MESSAGE(expected, actual, message) \
258
    Unity.AssertContainerFile=__FILE__; \
259
    UnityAssertEqualNumberUnsigned((unsigned long)(expected), (unsigned long)(actual), (message), (unsigned short)__LINE__, UNITY_DISPLAY_STYLE_UINT); \
260
    ABORT_IF_NECESSARY();
G
greg-williams 已提交
261 262
#define TEST_ASSERT_EQUAL_UINT(expected, actual) TEST_ASSERT_EQUAL_UINT_MESSAGE(expected, actual, NULL)

M
mvandervoord 已提交
263
#define TEST_ASSERT_EQUAL_UINT_ARRAY_MESSAGE(expected, actual, num_elements, message) \
264
    Unity.AssertContainerFile=__FILE__; \
265
    UnityAssertEqualUnsignedIntArray((const unsigned int*)(expected), (const unsigned int*)(actual), (unsigned long)(num_elements), (message), (unsigned short)__LINE__, UNITY_DISPLAY_STYLE_UINT); \
M
mvandervoord 已提交
266 267 268
    ABORT_IF_NECESSARY();
#define TEST_ASSERT_EQUAL_UINT_ARRAY(expected, actual, num_elements) TEST_ASSERT_EQUAL_UINT_ARRAY_MESSAGE(expected, actual, num_elements, NULL)

G
greg-williams 已提交
269
#define TEST_ASSERT_EQUAL_HEX8_MESSAGE(expected, actual, message) \
270
    Unity.AssertContainerFile=__FILE__; \
271
    UnityAssertEqualNumberUnsigned((unsigned long)(expected), (unsigned long)(actual), (message), (unsigned short)__LINE__, UNITY_DISPLAY_STYLE_HEX8); \
272
    ABORT_IF_NECESSARY();
G
greg-williams 已提交
273 274 275
#define TEST_ASSERT_EQUAL_HEX8(expected, actual) TEST_ASSERT_EQUAL_HEX8_MESSAGE(expected, actual, NULL)

#define TEST_ASSERT_EQUAL_HEX16_MESSAGE(expected, actual, message) \
276
    Unity.AssertContainerFile=__FILE__; \
277
    UnityAssertEqualNumberUnsigned((unsigned long)(expected), (unsigned long)(actual), (message), (unsigned short)__LINE__, UNITY_DISPLAY_STYLE_HEX16); \
278
    ABORT_IF_NECESSARY();
G
greg-williams 已提交
279 280 281
#define TEST_ASSERT_EQUAL_HEX16(expected, actual)	TEST_ASSERT_EQUAL_HEX16_MESSAGE(expected, actual, NULL)

#define TEST_ASSERT_EQUAL_HEX32_MESSAGE(expected, actual, message) \
282
    Unity.AssertContainerFile=__FILE__; \
283
    UnityAssertEqualNumber((long)(expected), (long)(actual), (message), (unsigned short)__LINE__, UNITY_DISPLAY_STYLE_HEX32); \
284
    ABORT_IF_NECESSARY();
G
greg-williams 已提交
285 286
#define TEST_ASSERT_EQUAL_HEX32(expected, actual)	TEST_ASSERT_EQUAL_HEX32_MESSAGE(expected, actual, NULL)

M
mvandervoord 已提交
287
#define TEST_ASSERT_EQUAL_HEX32_ARRAY_MESSAGE(expected, actual, num_elements, message) \
288
    Unity.AssertContainerFile=__FILE__; \
289
    UnityAssertEqualIntArray((const int*)(expected), (const int*)(actual), (unsigned long)(num_elements), (message), (unsigned short)__LINE__, UNITY_DISPLAY_STYLE_HEX32); \
M
mvandervoord 已提交
290 291 292
    ABORT_IF_NECESSARY();
#define TEST_ASSERT_EQUAL_HEX32_ARRAY(expected, actual, num_elements) TEST_ASSERT_EQUAL_HEX32_ARRAY_MESSAGE(expected, actual, num_elements, NULL)

G
greg-williams 已提交
293
#define TEST_ASSERT_EQUAL_HEX_MESSAGE(expected, actual, message) TEST_ASSERT_EQUAL_HEX32_MESSAGE(expected, actual, message)
294
#define TEST_ASSERT_EQUAL_HEX(expected, actual) TEST_ASSERT_EQUAL_HEX32(expected, actual)
G
greg-williams 已提交
295

M
mvandervoord 已提交
296
#define TEST_ASSERT_EQUAL_HEX_ARRAY_MESSAGE(expected, actual, num_elements, message) \
297
    Unity.AssertContainerFile=__FILE__; \
298
    UnityAssertEqualIntArray((const int*)(expected), (const int*)(actual), (unsigned long)(num_elements), (message), (unsigned short)__LINE__, UNITY_DISPLAY_STYLE_HEX32); \
M
mvandervoord 已提交
299 300 301
    ABORT_IF_NECESSARY();
#define TEST_ASSERT_EQUAL_HEX_ARRAY(expected, actual, num_elements) TEST_ASSERT_EQUAL_HEX32_ARRAY_MESSAGE(expected, actual, num_elements, NULL)

G
greg-williams 已提交
302
#define TEST_ASSERT_BITS_MESSAGE(mask, expected, actual, message) \
303
    Unity.AssertContainerFile=__FILE__; \
G
greg-williams 已提交
304
    UnityAssertBits((mask), (expected), (actual), (message), (unsigned short)__LINE__); \
305
    ABORT_IF_NECESSARY();
G
greg-williams 已提交
306 307 308
#define TEST_ASSERT_BITS(mask, expected, actual) TEST_ASSERT_BITS_MESSAGE(mask, expected, actual, NULL)

#define TEST_ASSERT_BITS_HIGH_MESSAGE(mask, actual, message) \
309
    Unity.AssertContainerFile=__FILE__; \
G
greg-williams 已提交
310
    UnityAssertBits((mask), (-1), (actual), (message), (unsigned short)__LINE__); \
311
    ABORT_IF_NECESSARY();
G
greg-williams 已提交
312 313 314
#define TEST_ASSERT_BITS_HIGH(mask, actual) TEST_ASSERT_BITS_HIGH_MESSAGE(mask, actual, NULL)

#define TEST_ASSERT_BITS_LOW_MESSAGE(mask, actual, message) \
315
    Unity.AssertContainerFile=__FILE__; \
G
greg-williams 已提交
316
    UnityAssertBits((mask), (0), (actual), (message), (unsigned short)__LINE__); \
317
    ABORT_IF_NECESSARY();
G
greg-williams 已提交
318 319 320
#define TEST_ASSERT_BITS_LOW(mask, actual) TEST_ASSERT_BITS_LOW_MESSAGE(mask, actual, NULL)

#define TEST_ASSERT_BIT_HIGH_MESSAGE(bit, actual, message) \
321
    Unity.AssertContainerFile=__FILE__; \
322
    UnityAssertBits(((_UU32)1 << bit), (-1), (actual), (message), (unsigned short)__LINE__); \
323
    ABORT_IF_NECESSARY();
G
greg-williams 已提交
324 325 326
#define TEST_ASSERT_BIT_HIGH(bit, actual) TEST_ASSERT_BIT_HIGH_MESSAGE(bit, actual, NULL)

#define TEST_ASSERT_BIT_LOW_MESSAGE(bit, actual, message) \
327
    Unity.AssertContainerFile=__FILE__; \
328
    UnityAssertBits(((_UU32)1 << bit), (0), (actual), (message), (unsigned short)__LINE__); \
329
    ABORT_IF_NECESSARY();
G
greg-williams 已提交
330 331 332
#define TEST_ASSERT_BIT_LOW(bit, actual) TEST_ASSERT_BIT_LOW_MESSAGE(bit, actual, NULL)

#define TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, actual, message) \
333
    Unity.AssertContainerFile=__FILE__; \
G
greg-williams 已提交
334
    UnityAssertEqualString((expected), (actual), (message), (unsigned short)__LINE__); \
335
    ABORT_IF_NECESSARY();
G
greg-williams 已提交
336 337
#define TEST_ASSERT_EQUAL_STRING(expected, actual) TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, actual, NULL)

338
#define TEST_ASSERT_EQUAL_MEMORY_MESSAGE(expected, actual, len, message) \
339
    Unity.AssertContainerFile=__FILE__; \
340 341 342 343
    UnityAssertEqualMemory((expected), (actual), (len), (message), (unsigned short)__LINE__); \
    ABORT_IF_NECESSARY();
#define TEST_ASSERT_EQUAL_MEMORY(expected, actual, len) TEST_ASSERT_EQUAL_MEMORY_MESSAGE(expected, actual, len, NULL)

344
#define TEST_ASSERT_EQUAL_MEMORY_ARRAY_MESSAGE(expected, actual, len, num_elements, message) \
345
    Unity.AssertContainerFile=__FILE__; \
346 347 348 349
    UnityAssertEqualMemoryArray((expected), (actual), (len), (num_elements), (message), (unsigned short)__LINE__); \
    ABORT_IF_NECESSARY();
#define TEST_ASSERT_EQUAL_MEMORY_ARRAY(expected, actual, len, num_elements) TEST_ASSERT_EQUAL_MEMORY_ARRAY_MESSAGE(expected, actual, len, num_elements, NULL)

350 351
#define TEST_FAIL(message) { Unity.AssertContainerFile=__FILE__; UnityFail((message), (unsigned short)__LINE__); TEST_ABORT(); }
#define TEST_IGNORE_MESSAGE(message) { Unity.AssertContainerFile=__FILE__; UnityIgnore((message), (unsigned short)__LINE__); TEST_ABORT(); }
352
#define TEST_IGNORE() TEST_IGNORE_MESSAGE(NULL)
353
#define TEST_ONLY() 
G
greg-williams 已提交
354

355 356 357 358 359 360 361
#ifdef UNITY_EXCLUDE_FLOAT
#define TEST_ASSERT_FLOAT_WITHIN_MESSAGE(delta, expected, actual, message)  TEST_FAIL("Unity Floating Point Disabled");
#define TEST_ASSERT_FLOAT_WITHIN(delta, expected, actual)                   TEST_FAIL("Unity Floating Point Disabled");
#define TEST_ASSERT_EQUAL_FLOAT_MESSAGE(expected, actual, message)          TEST_FAIL("Unity Floating Point Disabled");
#define TEST_ASSERT_EQUAL_FLOAT(expected, actual)                           TEST_FAIL("Unity Floating Point Disabled");
#else
#define TEST_ASSERT_FLOAT_WITHIN_MESSAGE(delta, expected, actual, message) \
362
    Unity.AssertContainerFile=__FILE__; \
363 364 365 366 367 368 369
    UnityAssertFloatsWithin((delta), (expected), (actual), (message), (unsigned short)__LINE__); \
    ABORT_IF_NECESSARY();
#define TEST_ASSERT_FLOAT_WITHIN(delta, expected, actual) TEST_ASSERT_FLOAT_WITHIN_MESSAGE(delta, expected, actual, NULL)
#define TEST_ASSERT_EQUAL_FLOAT_MESSAGE(expected, actual, message) TEST_ASSERT_FLOAT_WITHIN_MESSAGE((expected) * UNITY_FLOAT_PRECISION, expected, actual, message)
#define TEST_ASSERT_EQUAL_FLOAT(expected, actual) TEST_ASSERT_FLOAT_WITHIN_MESSAGE((expected) * UNITY_FLOAT_PRECISION, expected, actual, NULL)
#endif

G
greg-williams 已提交
370
#endif