unity.h 12.1 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

M
mvandervoord 已提交
9 10 11 12 13 14 15 16 17
//-------------------------------------------------------
// Internal Functions and Structs Needed For Unity To Run
//
// (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.)
//-------------------------------------------------------

G
greg-williams 已提交
18 19
typedef void (*UnityTestFunction)(void);

20
typedef enum
G
greg-williams 已提交
21 22 23 24 25 26 27 28 29 30
{
    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
{
31 32
    const char* TestFile;
    const char* CurrentTestName;
G
greg-williams 已提交
33 34 35 36 37 38
    unsigned char NumberOfTests;
    unsigned char TestFailures;
    unsigned char TestIgnores;
    unsigned char CurrentTestFailed;
    unsigned char CurrentTestIgnored;
    float DefaultDelta;
39
    jmp_buf AbortFrame;
G
greg-williams 已提交
40 41 42 43
};

extern struct _Unity Unity;

44
void CreateResults(void);
G
greg-williams 已提交
45

46
void UnityBegin(void);
G
greg-williams 已提交
47
void UnityEnd(void);
48
long UnityGetNumFailures(void);
G
greg-williams 已提交
49

50
void UnityPrintChar(const char ch);
51
void UnityPrint(const char* string);
52 53 54 55 56
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);
57
void UnityConcludeTest(void);
G
greg-williams 已提交
58

M
mvandervoord 已提交
59 60
void UnityAssertEqualInt(const long expected,
                         const long actual,
61
                         const char* msg,
62 63 64
                         const unsigned short lineNumber,
                         const UNITY_DISPLAY_STYLE_T style);

M
mvandervoord 已提交
65 66 67 68 69 70 71 72 73 74
void UnityAssertEqualIntArray(const long* expected,
                              const long* actual,
                              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,
75
                     const char* msg,
76 77
                     const unsigned short lineNumber);

78 79 80
void UnityAssertEqualString(const char* expected,
                            const char* actual,
                            const char* msg,
81 82
                            const unsigned short lineNumber );

M
mvandervoord 已提交
83 84 85
void UnityAssertEqualMemory(const void* expected,
                            const void* actual,
                            unsigned long length,
86
                            const char* msg,
87 88
                            const unsigned short lineNumber );

89 90 91
void UnityAssertFloatsWithin(const float delta,
                             const float expected,
                             const float actual,
92
                             const char* msg,
93 94
                             const unsigned short lineNumber);

M
mvandervoord 已提交
95 96 97
void UnityAssertIntsWithin(const long delta,
                           const long expected,
                           const long actual,
98
                           const char* msg,
99 100
                           const unsigned short lineNumber);

M
mvandervoord 已提交
101 102 103
void UnityFail(const char* message, const long line);

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

M
mvandervoord 已提交
105 106 107
//-------------------------------------------------------
// Test Running Macros
//-------------------------------------------------------
G
greg-williams 已提交
108

109
#define TEST_PROTECT() (setjmp(Unity.AbortFrame) == 0)
110

111
#define TEST_ABORT() {longjmp(Unity.AbortFrame, 1);}
112 113 114

#define ABORT_IF_NECESSARY() \
    if( Unity.CurrentTestFailed || Unity.CurrentTestIgnored ) {TEST_ABORT();}
G
greg-williams 已提交
115 116 117 118 119 120 121

#define RUN_TEST(func) \
    Unity.CurrentTestName = #func; \
    Unity.NumberOfTests ++; \
    runTest(func); \
    UnityConcludeTest();

M
mvandervoord 已提交
122 123 124 125 126 127
//-------------------------------------------------------
// Test Asserts
//
// (these are the macros you are looking for)
//-------------------------------------------------------

G
greg-williams 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
#define TEST_ASSERT_MESSAGE(condition, message) if (condition) {} else {TEST_FAIL(message);}
#define TEST_ASSERT(condition) TEST_ASSERT_MESSAGE(condition, NULL)

#define TEST_ASSERT_TRUE_MESSAGE(condition) TEST_ASSERT_MESSAGE(condition, message)
#define TEST_ASSERT_TRUE(condition) TEST_ASSERT(condition)

#define TEST_ASSERT_UNLESS_MESSAGE(condition) TEST_ASSERT_MESSAGE(!(condition), message)
#define TEST_ASSERT_UNLESS(condition) TEST_ASSERT(!(condition))

#define TEST_ASSERT_FALSE_MESSAGE(condition) TEST_ASSERT_MESSAGE(!(condition), message)
#define TEST_ASSERT_FALSE(condition) TEST_ASSERT(!(condition))

#define TEST_ASSERT_NULL(pointer) TEST_ASSERT_MESSAGE(pointer == NULL, #pointer " was not null.")
#define TEST_ASSERT_NOT_NULL(pointer) TEST_ASSERT_MESSAGE(pointer != NULL, #pointer " was null.")

#define TEST_ASSERT_EQUAL_INT_MESSAGE(expected, actual, message) \
    Unity.TestFile=__FILE__; \
M
mvandervoord 已提交
145
    UnityAssertEqualInt((long)(expected), (long)(actual), (message), (unsigned short)__LINE__, UNITY_DISPLAY_STYLE_INT); \
146
    ABORT_IF_NECESSARY();
G
greg-williams 已提交
147 148
#define TEST_ASSERT_EQUAL_INT(expected, actual)	TEST_ASSERT_EQUAL_INT_MESSAGE(expected, actual, NULL)

M
mvandervoord 已提交
149 150 151 152 153 154
#define TEST_ASSERT_EQUAL_INT_ARRAY_MESSAGE(expected, actual, num_elements, message) \
    Unity.TestFile=__FILE__; \
    UnityAssertEqualIntArray((long*)(expected), (long*)(actual), (unsigned long)(num_elements), (message), (unsigned short)__LINE__, UNITY_DISPLAY_STYLE_INT); \
    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 已提交
155 156 157 158 159 160
#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)

#define TEST_ASSERT_INT_WITHIN_MESSAGE(delta, expected, actual, message) \
    Unity.TestFile=__FILE__; \
    UnityAssertIntsWithin((delta), (expected), (actual), NULL, (unsigned short)__LINE__); \
161
    ABORT_IF_NECESSARY();
G
greg-williams 已提交
162 163 164 165
#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) \
    Unity.TestFile=__FILE__; \
M
mvandervoord 已提交
166
    UnityAssertEqualInt((long)(expected), (long)(actual), (message), (unsigned short)__LINE__, UNITY_DISPLAY_STYLE_UINT); \
167
    ABORT_IF_NECESSARY();
G
greg-williams 已提交
168 169
#define TEST_ASSERT_EQUAL_UINT(expected, actual) TEST_ASSERT_EQUAL_UINT_MESSAGE(expected, actual, NULL)

M
mvandervoord 已提交
170 171 172 173 174 175
#define TEST_ASSERT_EQUAL_UINT_ARRAY_MESSAGE(expected, actual, num_elements, message) \
    Unity.TestFile=__FILE__; \
    UnityAssertEqualIntArray((long*)(expected), (long*)(actual), (unsigned long)(num_elements),  (message), (unsigned short)__LINE__, UNITY_DISPLAY_STYLE_UINT); \
    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 已提交
176 177
#define TEST_ASSERT_EQUAL_HEX8_MESSAGE(expected, actual, message) \
    Unity.TestFile=__FILE__; \
M
mvandervoord 已提交
178
    UnityAssertEqualInt((long)(expected), (long)(actual), (message), (unsigned short)__LINE__, UNITY_DISPLAY_STYLE_HEX8); \
179
    ABORT_IF_NECESSARY();
G
greg-williams 已提交
180 181 182 183
#define TEST_ASSERT_EQUAL_HEX8(expected, actual) TEST_ASSERT_EQUAL_HEX8_MESSAGE(expected, actual, NULL)

#define TEST_ASSERT_EQUAL_HEX16_MESSAGE(expected, actual, message) \
    Unity.TestFile=__FILE__; \
M
mvandervoord 已提交
184
    UnityAssertEqualInt((long)(expected), (long)(actual), (message), (unsigned short)__LINE__, UNITY_DISPLAY_STYLE_HEX16); \
185
    ABORT_IF_NECESSARY();
G
greg-williams 已提交
186 187 188 189
#define TEST_ASSERT_EQUAL_HEX16(expected, actual)	TEST_ASSERT_EQUAL_HEX16_MESSAGE(expected, actual, NULL)

#define TEST_ASSERT_EQUAL_HEX32_MESSAGE(expected, actual, message) \
    Unity.TestFile=__FILE__; \
M
mvandervoord 已提交
190
    UnityAssertEqualInt((long)(expected), (long)(actual), (message), (unsigned short)__LINE__, UNITY_DISPLAY_STYLE_HEX32); \
191
    ABORT_IF_NECESSARY();
G
greg-williams 已提交
192 193
#define TEST_ASSERT_EQUAL_HEX32(expected, actual)	TEST_ASSERT_EQUAL_HEX32_MESSAGE(expected, actual, NULL)

M
mvandervoord 已提交
194 195 196 197 198 199
#define TEST_ASSERT_EQUAL_HEX32_ARRAY_MESSAGE(expected, actual, num_elements, message) \
    Unity.TestFile=__FILE__; \
    UnityAssertEqualIntArray((long*)(expected), (long*)(actual), (unsigned long)(num_elements), (message), (unsigned short)__LINE__, UNITY_DISPLAY_STYLE_HEX32); \
    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 已提交
200
#define TEST_ASSERT_EQUAL_HEX_MESSAGE(expected, actual, message) TEST_ASSERT_EQUAL_HEX32_MESSAGE(expected, actual, message)
201
#define TEST_ASSERT_EQUAL_HEX(expected, actual) TEST_ASSERT_EQUAL_HEX32(expected, actual)
G
greg-williams 已提交
202

M
mvandervoord 已提交
203 204 205 206 207 208
#define TEST_ASSERT_EQUAL_HEX_ARRAY_MESSAGE(expected, actual, num_elements, message) \
    Unity.TestFile=__FILE__; \
    UnityAssertEqualIntArray((long*)(expected), (long*)(actual), (unsigned long)(num_elements), (message), (unsigned short)__LINE__, UNITY_DISPLAY_STYLE_HEX32); \
    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 已提交
209 210 211
#define TEST_ASSERT_BITS_MESSAGE(mask, expected, actual, message) \
    Unity.TestFile=__FILE__; \
    UnityAssertBits((mask), (expected), (actual), (message), (unsigned short)__LINE__); \
212
    ABORT_IF_NECESSARY();
G
greg-williams 已提交
213 214 215 216 217
#define TEST_ASSERT_BITS(mask, expected, actual) TEST_ASSERT_BITS_MESSAGE(mask, expected, actual, NULL)

#define TEST_ASSERT_BITS_HIGH_MESSAGE(mask, actual, message) \
    Unity.TestFile=__FILE__; \
    UnityAssertBits((mask), (-1), (actual), (message), (unsigned short)__LINE__); \
218
    ABORT_IF_NECESSARY();
G
greg-williams 已提交
219 220 221 222 223
#define TEST_ASSERT_BITS_HIGH(mask, actual) TEST_ASSERT_BITS_HIGH_MESSAGE(mask, actual, NULL)

#define TEST_ASSERT_BITS_LOW_MESSAGE(mask, actual, message) \
    Unity.TestFile=__FILE__; \
    UnityAssertBits((mask), (0), (actual), (message), (unsigned short)__LINE__); \
224
    ABORT_IF_NECESSARY();
G
greg-williams 已提交
225 226 227 228 229
#define TEST_ASSERT_BITS_LOW(mask, actual) TEST_ASSERT_BITS_LOW_MESSAGE(mask, actual, NULL)

#define TEST_ASSERT_BIT_HIGH_MESSAGE(bit, actual, message) \
    Unity.TestFile=__FILE__; \
    UnityAssertBits((1 << bit), (-1), (actual), (message), (unsigned short)__LINE__); \
230
    ABORT_IF_NECESSARY();
G
greg-williams 已提交
231 232 233 234 235
#define TEST_ASSERT_BIT_HIGH(bit, actual) TEST_ASSERT_BIT_HIGH_MESSAGE(bit, actual, NULL)

#define TEST_ASSERT_BIT_LOW_MESSAGE(bit, actual, message) \
    Unity.TestFile=__FILE__; \
    UnityAssertBits((1 << bit), (0), (actual), (message), (unsigned short)__LINE__); \
236
    ABORT_IF_NECESSARY();
G
greg-williams 已提交
237 238 239 240 241
#define TEST_ASSERT_BIT_LOW(bit, actual) TEST_ASSERT_BIT_LOW_MESSAGE(bit, actual, NULL)

#define TEST_ASSERT_FLOAT_WITHIN_MESSAGE(delta, expected, actual, message) \
    Unity.TestFile=__FILE__; \
    UnityAssertFloatsWithin((delta), (expected), (actual), (message), (unsigned short)__LINE__); \
242
    ABORT_IF_NECESSARY();
G
greg-williams 已提交
243 244 245 246 247
#define TEST_ASSERT_FLOAT_WITHIN(delta, expected, actual) TEST_ASSERT_FLOAT_WITHIN_MESSAGE(delta, expected, actual, NULL)

#define TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, actual, message) \
    Unity.TestFile=__FILE__; \
    UnityAssertEqualString((expected), (actual), (message), (unsigned short)__LINE__); \
248
    ABORT_IF_NECESSARY();
G
greg-williams 已提交
249 250
#define TEST_ASSERT_EQUAL_STRING(expected, actual) TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, actual, NULL)

251 252 253 254 255 256
#define TEST_ASSERT_EQUAL_MEMORY_MESSAGE(expected, actual, len, message) \
    Unity.TestFile=__FILE__; \
    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)

257 258 259
#define TEST_FAIL(message) { Unity.TestFile=__FILE__; UnityFail((message), (unsigned short)__LINE__); TEST_ABORT(); }
#define TEST_IGNORE_MESSAGE(message) { Unity.TestFile=__FILE__; UnityIgnore((message), (unsigned short)__LINE__); TEST_ABORT(); }
#define TEST_IGNORE() TEST_IGNORE_MESSAGE(NULL)
G
greg-williams 已提交
260 261 262

#endif