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

typedef void (*UnityTestFunction)(void);

11
typedef enum
G
greg-williams 已提交
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
{
    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
{
    char* CurrentTestName;
    unsigned char NumberOfTests;
    unsigned char TestFailures;
    unsigned char TestIgnores;
    unsigned char CurrentTestFailed;
    unsigned char CurrentTestIgnored;
    const char *TestFile;
    float DefaultDelta;
30
    jmp_buf AbortFrame;
G
greg-williams 已提交
31 32 33 34 35 36 37 38
};

extern struct _Unity Unity;

void CreateResults();

void UnityBegin();
void UnityEnd(void);
39
int  UnityGetNumFailures(void);
G
greg-williams 已提交
40

41
void UnityPrintChar(const char ch);
G
greg-williams 已提交
42
void UnityPrint(const char *string);
43 44 45 46 47
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 已提交
48 49
void UnityConcludeTest();

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
void UnityAssertEqualInt(const int expected,
                         const int actual,
                         const char *msg,
                         const unsigned short lineNumber,
                         const UNITY_DISPLAY_STYLE_T style);

void UnityAssertBits(const int mask,
                     const int expected,
                     const int actual,
                     const char *msg,
                     const unsigned short lineNumber);

void UnityAssertEqualString(const char *expected,
                            const char *actual,
                            const char *msg,
                            const unsigned short lineNumber );

67 68 69 70 71 72
void UnityAssertEqualMemory(void *expected,
                            void *actual,
                            unsigned int length,
                            const char *msg,
                            const unsigned short lineNumber );

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
void UnityAssertFloatsWithin(const float delta,
                             const float expected,
                             const float actual,
                             const char *msg,
                             const unsigned short lineNumber);

void UnityAssertIntsWithin(const int delta,
                           const int expected,
                           const int actual,
                           const char *msg,
                           const unsigned short lineNumber);

void UnityFail(const char *message, const int line);

void UnityIgnore(const char *message, const int line);
G
greg-williams 已提交
88

89
#define TEST_PROTECT() (setjmp(Unity.AbortFrame) == 0)
90

91
#define TEST_ABORT() {longjmp(Unity.AbortFrame, 1);}
92 93 94

#define ABORT_IF_NECESSARY() \
    if( Unity.CurrentTestFailed || Unity.CurrentTestIgnored ) {TEST_ABORT();}
G
greg-williams 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120

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

#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__; \
    UnityAssertEqualInt((int)(expected), (int)(actual), (message), (unsigned short)__LINE__, UNITY_DISPLAY_STYLE_INT); \
121
    ABORT_IF_NECESSARY();
G
greg-williams 已提交
122 123 124 125 126 127 128 129
#define TEST_ASSERT_EQUAL_INT(expected, actual)	TEST_ASSERT_EQUAL_INT_MESSAGE(expected, actual, NULL)

#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__); \
130
    ABORT_IF_NECESSARY();
G
greg-williams 已提交
131 132 133 134 135
#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__; \
    UnityAssertEqualInt((int)(expected), (int)(actual), (message), (unsigned short)__LINE__, UNITY_DISPLAY_STYLE_UINT); \
136
    ABORT_IF_NECESSARY();
G
greg-williams 已提交
137 138 139 140 141
#define TEST_ASSERT_EQUAL_UINT(expected, actual) TEST_ASSERT_EQUAL_UINT_MESSAGE(expected, actual, NULL)

#define TEST_ASSERT_EQUAL_HEX8_MESSAGE(expected, actual, message) \
    Unity.TestFile=__FILE__; \
    UnityAssertEqualInt((int)(expected), (int)(actual), (message), (unsigned short)__LINE__, UNITY_DISPLAY_STYLE_HEX8); \
142
    ABORT_IF_NECESSARY();
G
greg-williams 已提交
143 144 145 146 147
#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__; \
    UnityAssertEqualInt((int)(expected), (int)(actual), (message), (unsigned short)__LINE__, UNITY_DISPLAY_STYLE_HEX16); \
148
    ABORT_IF_NECESSARY();
G
greg-williams 已提交
149 150 151 152 153
#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__; \
    UnityAssertEqualInt((int)(expected), (int)(actual), (message), (unsigned short)__LINE__, UNITY_DISPLAY_STYLE_HEX32); \
154
    ABORT_IF_NECESSARY();
G
greg-williams 已提交
155 156 157
#define TEST_ASSERT_EQUAL_HEX32(expected, actual)	TEST_ASSERT_EQUAL_HEX32_MESSAGE(expected, actual, NULL)

#define TEST_ASSERT_EQUAL_HEX_MESSAGE(expected, actual, message) TEST_ASSERT_EQUAL_HEX32_MESSAGE(expected, actual, message)
158
#define TEST_ASSERT_EQUAL_HEX(expected, actual) TEST_ASSERT_EQUAL_HEX32(expected, actual)
G
greg-williams 已提交
159 160 161 162

#define TEST_ASSERT_BITS_MESSAGE(mask, expected, actual, message) \
    Unity.TestFile=__FILE__; \
    UnityAssertBits((mask), (expected), (actual), (message), (unsigned short)__LINE__); \
163
    ABORT_IF_NECESSARY();
G
greg-williams 已提交
164 165 166 167 168
#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__); \
169
    ABORT_IF_NECESSARY();
G
greg-williams 已提交
170 171 172 173 174
#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__); \
175
    ABORT_IF_NECESSARY();
G
greg-williams 已提交
176 177 178 179 180
#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__); \
181
    ABORT_IF_NECESSARY();
G
greg-williams 已提交
182 183 184 185 186
#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__); \
187
    ABORT_IF_NECESSARY();
G
greg-williams 已提交
188 189 190 191 192
#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__); \
193
    ABORT_IF_NECESSARY();
G
greg-williams 已提交
194 195 196 197 198
#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__); \
199
    ABORT_IF_NECESSARY();
G
greg-williams 已提交
200 201
#define TEST_ASSERT_EQUAL_STRING(expected, actual) TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, actual, NULL)

202 203 204 205 206 207
#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)

208 209 210
#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 已提交
211 212 213

#endif