1
0
mirror of https://github.com/ThrowTheSwitch/Unity.git synced 2026-01-26 18:01:35 +01:00

Initial project population

git-svn-id: http://unity.svn.sourceforge.net/svnroot/unity/trunk@1 e7d17a6e-8845-0410-bbbc-c8efb4fdad7e
This commit is contained in:
greg-williams
2008-02-07 03:00:54 +00:00
commit 720acfbb95
11 changed files with 1905 additions and 0 deletions

336
src/unity.c Normal file
View File

@@ -0,0 +1,336 @@
#include "unity.h"
#include <stdio.h>
struct _Unity Unity =
{
NULL,
0,
0,
0,
0,
0,
NULL,
1e-4f,
};
void UnityPrintChar(char ch)
{
putchar(ch);
}
void UnityPrint(const char *string)
{
unsigned char * pch = (unsigned char *)string;
if (pch != NULL)
{
while (*pch)
{
UnityPrintChar(*pch);
pch++;
}
}
}
void UnityPrintNumberByStyle(long number, UNITY_DISPLAY_STYLE_T style)
{
switch (style)
{
case UNITY_DISPLAY_STYLE_HEX8: UnityPrintNumberHex(number,2); break;
case UNITY_DISPLAY_STYLE_HEX16: UnityPrintNumberHex(number,4); break;
case UNITY_DISPLAY_STYLE_HEX32: UnityPrintNumberHex(number,8); break;
case UNITY_DISPLAY_STYLE_UINT: UnityPrintNumberUnsigned(number); break;
default: UnityPrintNumber(number); break;
}
}
/// basically do an itoa using as little ram as possible
void UnityPrintNumber(long number)
{
unsigned long divisor = 10;
if (number < 0)
{
UnityPrintChar('-');
number = -number;
}
// figure out initial divisor
while (number / divisor)
{
divisor *= 10;
}
// now divide number by divisor, mod and print, then divide divisor
do
{
divisor /= 10;
UnityPrintChar((char)('0' + (number / divisor % 10)));
}
while (divisor > 1U);
}
/// basically do an itoa using as little ram as possible
void UnityPrintNumberUnsigned(unsigned long number)
{
unsigned long divisor = 10;
// figure out initial divisor
while (number / divisor)
{
divisor *= 10;
}
// now divide number by divisor, mod and print, then divide divisor
do
{
divisor /= 10;
UnityPrintChar((char)('0' + (number / divisor % 10)));
}
while (divisor > 1U);
}
void UnityPrintNumberHex(unsigned long number, char nibbles)
{
unsigned long nibble;
UnityPrint("0x");
while (nibbles > 0)
{
nibble = (number >> (--nibbles << 2)) & 0x0000000F;
if (nibble <= 9)
{
UnityPrintChar((char)('0' + nibble));
}
else
{
UnityPrintChar((char)('A' - 10 + nibble));
}
}
}
void UnityPrintMask(unsigned long mask, unsigned long number)
{
unsigned long bit = 0x00000001;
int i;
for (i = 0; i < 32; i++)
{
if (bit & mask)
{
if (bit & number)
{
UnityPrintChar('1');
}
else
{
UnityPrintChar('0');
}
}
else
{
UnityPrintChar('X');
}
bit = bit << 1;
}
}
void UnityTestResultsBegin(int line)
{
UnityPrint(Unity.TestFile);
UnityPrintChar(':');
UnityPrintNumber(line);
UnityPrintChar(':');
UnityPrint(Unity.CurrentTestName);
UnityPrintChar(':');
}
void UnityConcludeTest()
{
if (Unity.CurrentTestIgnored)
{
Unity.TestIgnores++;
}
else if (!Unity.CurrentTestFailed)
{
UnityPrint(Unity.CurrentTestName);
UnityPrint("::: PASS\n");
}
else
{
Unity.TestFailures++;
}
Unity.CurrentTestFailed = 0;
Unity.CurrentTestIgnored = 0;
}
void UnityAssertBits(int mask, int expected, int actual, const char *msg, unsigned short lineNumber)
{
if ((mask & expected) != (mask & actual))
{
Unity.CurrentTestFailed = 1;
UnityTestResultsBegin(lineNumber);
UnityPrint("Expected ");
UnityPrintMask(mask, expected);
UnityPrint(" was ");
UnityPrintMask(mask, actual);
UnityPrintChar('.');
if (msg)
{
UnityPrintChar(' ');
UnityPrint(msg);
}
UnityPrintChar('\n');
}
}
void UnityAssertEqualInt(int expected, int actual, const char *msg, unsigned short lineNumber, UNITY_DISPLAY_STYLE_T style)
{
if (expected != actual)
{
Unity.CurrentTestFailed = 1;
UnityTestResultsBegin(lineNumber);
UnityPrint("Expected ");
UnityPrintNumberByStyle(expected, style);
UnityPrint(" was ");
UnityPrintNumberByStyle(actual, style);
UnityPrintChar('.');
if (msg)
{
UnityPrintChar(' ');
UnityPrint(msg);
}
UnityPrintChar('\n');
}
}
void UnityAssertFloatsWithin(float delta, float expected, float actual, const char *msg, unsigned short lineNumber)
{
float diff = actual - expected;
if (diff < 0)
{
diff = -diff;
}
if (delta < diff)
{
Unity.CurrentTestFailed = 1;
UnityTestResultsBegin(lineNumber);
UnityPrint("Floats not within delta.");
if (msg)
{
UnityPrintChar(' ');
UnityPrint(msg);
}
UnityPrintChar('\n');
}
}
void UnityAssertIntsWithin(int delta, int expected, int actual, const char *msg, unsigned short lineNumber)
{
int diff = actual - expected;
if (diff < 0)
{
diff = -diff;
}
if (delta < diff)
{
Unity.CurrentTestFailed = 1;
UnityTestResultsBegin(lineNumber);
UnityPrint("Ints not within delta.");
if (msg)
{
UnityPrintChar(' ');
UnityPrint(msg);
}
UnityPrintChar('\n');
}
}
void UnityAssertEqualString(const char *expected, const char *actual, const char *msg, unsigned short lineNumber)
{
unsigned int i;
// if both pointers not null compare the strings
if (expected && actual)
{
for (i = 0; expected[i] || actual[i]; i++)
{
if (expected[i] != actual[i])
{
Unity.CurrentTestFailed = 1;
}
}
}
else
{ // handle case of one pointers being null (if both null, test should pass)
if (expected != actual)
{
Unity.CurrentTestFailed = 1;
}
}
if (Unity.CurrentTestFailed)
{
UnityTestResultsBegin(lineNumber);
UnityPrint("Expected '");
UnityPrint(expected);
UnityPrint("' was '");
UnityPrint(actual);
UnityPrintChar('\'');
UnityPrintChar('.');
if (msg)
{
UnityPrintChar(' ');
UnityPrint(msg);
}
UnityPrintChar('\n');
}
}
void UnityFail(const char *message, int line)
{
Unity.CurrentTestFailed = 1;
UnityTestResultsBegin(line);
UnityPrint(message);
UnityPrintChar('\n');
}
void UnityIgnore(const char *message, int line)
{
Unity.CurrentTestIgnored = 1;
UnityTestResultsBegin(line);
UnityPrint(message);
UnityPrint(" IGNORED\n");
}
void UnityBegin()
{
Unity.NumberOfTests = 0;
}
void UnityEnd(void)
{
UnityPrintNumber(Unity.NumberOfTests);
UnityPrint(" Tests ");
UnityPrintNumber(Unity.TestFailures);
UnityPrint(" Failures ");
UnityPrintNumber(Unity.TestIgnores);
UnityPrint(" Ignored\n");
if (Unity.TestFailures == 0U)
{
UnityPrint("OK\n");
}
else
{
UnityPrint("FAIL\n");
}
}

206
src/unity.h Normal file
View File

@@ -0,0 +1,206 @@
#ifndef UNITY_FRAMEWORK_H
#define UNITY_FRAMEWORK_H
#define UNITY
#include <stdio.h>
typedef void (*UnityTestFunction)(void);
typedef enum
{
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;
};
extern struct _Unity Unity;
void CreateResults();
void UnityBegin();
void UnityEnd(void);
void UnityPrintChar(char ch);
void UnityPrint(const char *string);
void UnityPrintMask(unsigned long mask, unsigned long number);
void UnityPrintNumberByStyle(long number, UNITY_DISPLAY_STYLE_T style);
void UnityPrintNumber(long number);
void UnityPrintNumberUnsigned(unsigned long number);
void UnityPrintNumberHex(unsigned long number, char nibbles);
void UnityConcludeTest();
void UnityAssertEqualInt(int expected, int actual,
const char *msg, unsigned short lineNumber, UNITY_DISPLAY_STYLE_T style);
void UnityAssertBits(int mask, int expected, int actual,
const char *msg, unsigned short lineNumber);
void UnityAssertEqualString(const char *expected, const char *actual,
const char *msg, unsigned short lineNumber );
void UnityAssertFloatsWithin(float delta, float expected, float actual,
const char *msg, unsigned short lineNumber);
void UnityAssertIntsWithin(int delta, int expected, int actual,
const char *msg, unsigned short lineNumber);
void UnityFail(const char *message, int line);
void UnityIgnore(const char *message, int line);
#define EXIT_WRAPPED_TEST(exprString) \
if( Unity.CurrentTestFailed ) {\
UnityPrint(__FILE__); \
UnityPrint(":"); \
UnityPrintNumber(__LINE__); \
UnityPrint(":REDIRECTED:"); \
UnityPrint(exprString); \
UnityPrintChar('\n'); \
}
#define RETURN_IF_NECESSARY() \
if( Unity.CurrentTestFailed || Unity.CurrentTestIgnored ) {return;}
#define TEST_WRAP_NO_RETURN(function) \
{\
function; \
EXIT_WRAPPED_TEST(#function); \
}
#define RUN_TEST(func) \
Unity.CurrentTestName = #func; \
Unity.NumberOfTests ++; \
runTest(func); \
UnityConcludeTest();
#define TEST_WRAP(function) \
{\
TEST_WRAP_NO_RETURN(function); \
Unity.TestFile=__FILE__; \
RETURN_IF_NECESSARY(); \
}
#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); \
RETURN_IF_NECESSARY();
#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__); \
RETURN_IF_NECESSARY();
#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); \
RETURN_IF_NECESSARY();
#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); \
RETURN_IF_NECESSARY();
#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); \
RETURN_IF_NECESSARY();
#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); \
RETURN_IF_NECESSARY();
#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)
#define TEST_ASSERT_EQUAL_HEX(expected, actual) TEST_ASSERT_EQUAL_HEX32(expected, message)
#define TEST_ASSERT_BITS_MESSAGE(mask, expected, actual, message) \
Unity.TestFile=__FILE__; \
UnityAssertBits((mask), (expected), (actual), (message), (unsigned short)__LINE__); \
RETURN_IF_NECESSARY();
#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__); \
RETURN_IF_NECESSARY();
#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__); \
RETURN_IF_NECESSARY();
#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__); \
RETURN_IF_NECESSARY();
#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__); \
RETURN_IF_NECESSARY();
#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__); \
RETURN_IF_NECESSARY();
#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__); \
RETURN_IF_NECESSARY();
#define TEST_ASSERT_EQUAL_STRING(expected, actual) TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, actual, NULL)
#define TEST_FAIL(message) { Unity.TestFile=__FILE__; UnityFail((message), (unsigned short)__LINE__); return; }
#define TEST_IGNORE_MESSAGE(message) { Unity.TestFile=__FILE__; UnityIgnore((message), (unsigned short)__LINE__); return; }
#define TEST_IGNORE() TEST_IGNORE_MESSAGE("")
#define TEST_PROTECT() (setjmp(AbortFrame) == 0)
#define TEST_THROW(message) { Unity.TestFile=__FILE__; UnityFail((message), (unsigned short)__LINE__); longjmp(AbortFrame, 1); }
#endif