- make floating point support optional and configurable

git-svn-id: http://unity.svn.sourceforge.net/svnroot/unity/trunk@48 e7d17a6e-8845-0410-bbbc-c8efb4fdad7e
This commit is contained in:
mvandervoord
2009-12-06 18:56:35 +00:00
parent 302e786453
commit b6817999c1
2 changed files with 117 additions and 108 deletions
+58 -72
View File
@@ -2,23 +2,7 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
struct _Unity Unity = struct _Unity Unity = { 0 };
{
NULL,
NULL,
0,
0,
0,
0,
0,
1e-4f,
{0},
};
void UnityPrintChar(const char ch)
{
putchar(ch);
}
void UnityPrint(const char* string) void UnityPrint(const char* string)
{ {
@@ -28,7 +12,7 @@ void UnityPrint(const char* string)
{ {
while (*pch) while (*pch)
{ {
UnityPrintChar(*pch); UNITY_OUTPUT_CHAR(*pch);
pch++; pch++;
} }
} }
@@ -55,7 +39,7 @@ void UnityPrintNumber(const long number_to_print)
if (number < 0) if (number < 0)
{ {
UnityPrintChar('-'); UNITY_OUTPUT_CHAR('-');
number = -number; number = -number;
} }
@@ -72,7 +56,7 @@ void UnityPrintNumber(const long number_to_print)
// now mod and print, then divide divisor // now mod and print, then divide divisor
do do
{ {
UnityPrintChar((char)('0' + (number / divisor % 10))); UNITY_OUTPUT_CHAR((char)('0' + (number / divisor % 10)));
divisor /= 10; divisor /= 10;
} }
while (divisor > 0); while (divisor > 0);
@@ -97,7 +81,7 @@ void UnityPrintNumberUnsigned(const unsigned long number)
// now mod and print, then divide divisor // now mod and print, then divide divisor
do do
{ {
UnityPrintChar((char)('0' + (number / divisor % 10))); UNITY_OUTPUT_CHAR((char)('0' + (number / divisor % 10)));
divisor /= 10; divisor /= 10;
} }
while (divisor > 0); while (divisor > 0);
@@ -114,11 +98,11 @@ void UnityPrintNumberHex(const unsigned long number, const char nibbles_to_print
nibble = (number >> (--nibbles << 2)) & 0x0000000F; nibble = (number >> (--nibbles << 2)) & 0x0000000F;
if (nibble <= 9) if (nibble <= 9)
{ {
UnityPrintChar((char)('0' + nibble)); UNITY_OUTPUT_CHAR((char)('0' + nibble));
} }
else else
{ {
UnityPrintChar((char)('A' - 10 + nibble)); UNITY_OUTPUT_CHAR((char)('A' - 10 + nibble));
} }
} }
} }
@@ -134,16 +118,16 @@ void UnityPrintMask(const unsigned long mask, const unsigned long number)
{ {
if (bit & number) if (bit & number)
{ {
UnityPrintChar('1'); UNITY_OUTPUT_CHAR('1');
} }
else else
{ {
UnityPrintChar('0'); UNITY_OUTPUT_CHAR('0');
} }
} }
else else
{ {
UnityPrintChar('X'); UNITY_OUTPUT_CHAR('X');
} }
bit = bit << 1; bit = bit << 1;
} }
@@ -152,11 +136,11 @@ void UnityPrintMask(const unsigned long mask, const unsigned long number)
void UnityTestResultsBegin(const long line) void UnityTestResultsBegin(const long line)
{ {
UnityPrint(Unity.TestFile); UnityPrint(Unity.TestFile);
UnityPrintChar(':'); UNITY_OUTPUT_CHAR(':');
UnityPrintNumber(line); UnityPrintNumber(line);
UnityPrintChar(':'); UNITY_OUTPUT_CHAR(':');
UnityPrint(Unity.CurrentTestName); UnityPrint(Unity.CurrentTestName);
UnityPrintChar(':'); UNITY_OUTPUT_CHAR(':');
} }
void UnityConcludeTest() void UnityConcludeTest()
@@ -194,13 +178,13 @@ void UnityAssertBits(const long mask,
UnityPrintMask(mask, expected); UnityPrintMask(mask, expected);
UnityPrint(" was "); UnityPrint(" was ");
UnityPrintMask(mask, actual); UnityPrintMask(mask, actual);
UnityPrintChar('.'); UNITY_OUTPUT_CHAR('.');
if (msg) if (msg)
{ {
UnityPrintChar(' '); UNITY_OUTPUT_CHAR(' ');
UnityPrint(msg); UnityPrint(msg);
} }
UnityPrintChar('\n'); UNITY_OUTPUT_CHAR('\n');
} }
} }
@@ -219,13 +203,13 @@ void UnityAssertEqualNumber(const long expected,
UnityPrintNumberByStyle(expected, style); UnityPrintNumberByStyle(expected, style);
UnityPrint(" was "); UnityPrint(" was ");
UnityPrintNumberByStyle(actual, style); UnityPrintNumberByStyle(actual, style);
UnityPrintChar('.'); UNITY_OUTPUT_CHAR('.');
if (msg) if (msg)
{ {
UnityPrintChar(' '); UNITY_OUTPUT_CHAR(' ');
UnityPrint(msg); UnityPrint(msg);
} }
UnityPrintChar('\n'); UNITY_OUTPUT_CHAR('\n');
} }
} }
@@ -244,13 +228,13 @@ void UnityAssertEqualNumberUnsigned(const unsigned long expected,
UnityPrintNumberByStyle(expected, style); UnityPrintNumberByStyle(expected, style);
UnityPrint(" was "); UnityPrint(" was ");
UnityPrintNumberByStyle(actual, style); UnityPrintNumberByStyle(actual, style);
UnityPrintChar('.'); UNITY_OUTPUT_CHAR('.');
if (msg) if (msg)
{ {
UnityPrintChar(' '); UNITY_OUTPUT_CHAR(' ');
UnityPrint(msg); UnityPrint(msg);
} }
UnityPrintChar('\n'); UNITY_OUTPUT_CHAR('\n');
} }
} }
@@ -273,10 +257,10 @@ void UnityAssertEqualIntArray(const int* expected,
UnityPrint("You asked me to compare 0 elements of an array, which was pointless."); UnityPrint("You asked me to compare 0 elements of an array, which was pointless.");
if (msg) if (msg)
{ {
UnityPrintChar(' '); UNITY_OUTPUT_CHAR(' ');
UnityPrint(msg); UnityPrint(msg);
} }
UnityPrintChar('\n'); UNITY_OUTPUT_CHAR('\n');
return; return;
} }
@@ -293,13 +277,13 @@ void UnityAssertEqualIntArray(const int* expected,
UnityPrintNumberByStyle(*--ptr_expected, style); UnityPrintNumberByStyle(*--ptr_expected, style);
UnityPrint(" was "); UnityPrint(" was ");
UnityPrintNumberByStyle(*--ptr_actual, style); UnityPrintNumberByStyle(*--ptr_actual, style);
UnityPrintChar('.'); UNITY_OUTPUT_CHAR('.');
if (msg) if (msg)
{ {
UnityPrintChar(' '); UNITY_OUTPUT_CHAR(' ');
UnityPrint(msg); UnityPrint(msg);
} }
UnityPrintChar('\n'); UNITY_OUTPUT_CHAR('\n');
return; return;
} }
} }
@@ -324,10 +308,10 @@ void UnityAssertEqualUnsignedIntArray(const unsigned int* expected,
UnityPrint("You asked me to compare nothing, which was pointless."); UnityPrint("You asked me to compare nothing, which was pointless.");
if (msg) if (msg)
{ {
UnityPrintChar(' '); UNITY_OUTPUT_CHAR(' ');
UnityPrint(msg); UnityPrint(msg);
} }
UnityPrintChar('\n'); UNITY_OUTPUT_CHAR('\n');
return; return;
} }
@@ -344,26 +328,27 @@ void UnityAssertEqualUnsignedIntArray(const unsigned int* expected,
UnityPrintNumberByStyle(*--ptr_expected, style); UnityPrintNumberByStyle(*--ptr_expected, style);
UnityPrint(" was "); UnityPrint(" was ");
UnityPrintNumberByStyle(*--ptr_actual, style); UnityPrintNumberByStyle(*--ptr_actual, style);
UnityPrintChar('.'); UNITY_OUTPUT_CHAR('.');
if (msg) if (msg)
{ {
UnityPrintChar(' '); UNITY_OUTPUT_CHAR(' ');
UnityPrint(msg); UnityPrint(msg);
} }
UnityPrintChar('\n'); UNITY_OUTPUT_CHAR('\n');
return; return;
} }
} }
} }
void UnityAssertFloatsWithin(const float delta, #ifndef UNITY_EXCLUDE_UFOAT
const float expected, void UnityAssertFloatsWithin(const _UF delta,
const float actual, const _UF expected,
const _UF actual,
const char* msg, const char* msg,
const unsigned short lineNumber) const unsigned short lineNumber)
{ {
float diff = actual - expected; _UF diff = actual - expected;
float pos_delta = delta; _UF pos_delta = delta;
if (diff < 0) if (diff < 0)
{ {
@@ -381,12 +366,13 @@ void UnityAssertFloatsWithin(const float delta,
UnityPrint("Floats not within delta."); UnityPrint("Floats not within delta.");
if (msg) if (msg)
{ {
UnityPrintChar(' '); UNITY_OUTPUT_CHAR(' ');
UnityPrint(msg); UnityPrint(msg);
} }
UnityPrintChar('\n'); UNITY_OUTPUT_CHAR('\n');
} }
} }
#endif
void UnityAssertNumbersWithin(const long delta, void UnityAssertNumbersWithin(const long delta,
const long expected, const long expected,
@@ -408,10 +394,10 @@ void UnityAssertNumbersWithin(const long delta,
UnityPrint("Values not within delta."); UnityPrint("Values not within delta.");
if (msg) if (msg)
{ {
UnityPrintChar(' '); UNITY_OUTPUT_CHAR(' ');
UnityPrint(msg); UnityPrint(msg);
} }
UnityPrintChar('\n'); UNITY_OUTPUT_CHAR('\n');
} }
} }
@@ -430,10 +416,10 @@ void UnityAssertNumbersUnsignedWithin(const unsigned long delta,
UnityPrint("Values not within delta."); UnityPrint("Values not within delta.");
if (msg) if (msg)
{ {
UnityPrintChar(' '); UNITY_OUTPUT_CHAR(' ');
UnityPrint(msg); UnityPrint(msg);
} }
UnityPrintChar('\n'); UNITY_OUTPUT_CHAR('\n');
} }
} }
@@ -470,14 +456,14 @@ void UnityAssertEqualString(const char* expected,
UnityPrint(expected); UnityPrint(expected);
UnityPrint("' was '"); UnityPrint("' was '");
UnityPrint(actual); UnityPrint(actual);
UnityPrintChar('\''); UNITY_OUTPUT_CHAR('\'');
UnityPrintChar('.'); UNITY_OUTPUT_CHAR('.');
if (msg) if (msg)
{ {
UnityPrintChar(' '); UNITY_OUTPUT_CHAR(' ');
UnityPrint(msg); UnityPrint(msg);
} }
UnityPrintChar('\n'); UNITY_OUTPUT_CHAR('\n');
} }
} }
@@ -496,10 +482,10 @@ void UnityAssertEqualMemory(const void* expected,
UnityPrint("You asked me to compare nothing, which was pointless."); UnityPrint("You asked me to compare nothing, which was pointless.");
if (msg) if (msg)
{ {
UnityPrintChar(' '); UNITY_OUTPUT_CHAR(' ');
UnityPrint(msg); UnityPrint(msg);
} }
UnityPrintChar('\n'); UNITY_OUTPUT_CHAR('\n');
return; return;
} }
@@ -525,10 +511,10 @@ void UnityAssertEqualMemory(const void* expected,
UnityPrint("Memory Mismatch."); UnityPrint("Memory Mismatch.");
if (msg) if (msg)
{ {
UnityPrintChar(' '); UNITY_OUTPUT_CHAR(' ');
UnityPrint(msg); UnityPrint(msg);
} }
UnityPrintChar('\n'); UNITY_OUTPUT_CHAR('\n');
} }
} }
@@ -550,10 +536,10 @@ void UnityAssertEqualMemoryArray(const void* expected,
UnityPrint("You asked me to compare nothing, which was pointless."); UnityPrint("You asked me to compare nothing, which was pointless.");
if (msg) if (msg)
{ {
UnityPrintChar(' '); UNITY_OUTPUT_CHAR(' ');
UnityPrint(msg); UnityPrint(msg);
} }
UnityPrintChar('\n'); UNITY_OUTPUT_CHAR('\n');
return; return;
} }
@@ -587,10 +573,10 @@ void UnityAssertEqualMemoryArray(const void* expected,
UnityPrint(" Memory Mismatch."); UnityPrint(" Memory Mismatch.");
if (msg) if (msg)
{ {
UnityPrintChar(' '); UNITY_OUTPUT_CHAR(' ');
UnityPrint(msg); UnityPrint(msg);
} }
UnityPrintChar('\n'); UNITY_OUTPUT_CHAR('\n');
} }
} }
@@ -599,7 +585,7 @@ void UnityFail(const char* message, const long line)
Unity.CurrentTestFailed = 1; Unity.CurrentTestFailed = 1;
UnityTestResultsBegin(line); UnityTestResultsBegin(line);
UnityPrint(message); UnityPrint(message);
UnityPrintChar('\n'); UNITY_OUTPUT_CHAR('\n');
} }
void UnityIgnore(const char* message, const long line) void UnityIgnore(const char* message, const long line)
+59 -36
View File
@@ -6,23 +6,40 @@
#include <stdio.h> #include <stdio.h>
#include <setjmp.h> #include <setjmp.h>
//------------------------------------------------------- //-------------------------------------------------------
// Bus Width Management // Float Support
//------------------------------------------------------- //-------------------------------------------------------
// 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
#ifndef INT_WIDTH #ifndef UNITY_EXCLUDE_FLOAT
#define INT_WIDTH (32) #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;
#endif #endif
#if (INT_WIDTH == 32) //-------------------------------------------------------
// 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)
typedef unsigned char _UU8; typedef unsigned char _UU8;
typedef unsigned short _UU16; typedef unsigned short _UU16;
typedef unsigned int _UU32; typedef unsigned int _UU32;
typedef signed char _US8; typedef signed char _US8;
typedef signed short _US16; typedef signed short _US16;
typedef signed int _US32; typedef signed int _US32;
#elif (INT_WIDTH == 16) #elif (UNITY_INT_WIDTH == 16)
typedef unsigned char _UU8; typedef unsigned char _UU8;
typedef unsigned int _UU16; typedef unsigned int _UU16;
typedef unsigned long _UU32; typedef unsigned long _UU32;
@@ -30,18 +47,19 @@
typedef signed int _US16; typedef signed int _US16;
typedef signed long _US32; typedef signed long _US32;
#else #else
#error Invalid INT_WIDTH specified! (32 or 16 only are currently supported) #error Invalid UNITY_INT_WIDTH specified! (32 or 16 only are currently supported)
#error Defaults to INT_WIDTH=32 if unspecified
#endif #endif
//-------------------------------------------------------
// Output Method
//-------------------------------------------------------
#ifndef UNITY_OUTPUT_CHAR
#define UNITY_OUTPUT_CHAR(a) putchar(a)
#endif
//------------------------------------------------------- //-------------------------------------------------------
// Internal Functions and Structs Needed For Unity To Run // Internal Structs Needed
//
// (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.)
//------------------------------------------------------- //-------------------------------------------------------
typedef void (*UnityTestFunction)(void); typedef void (*UnityTestFunction)(void);
@@ -64,13 +82,11 @@ struct _Unity
unsigned char TestIgnores; unsigned char TestIgnores;
unsigned char CurrentTestFailed; unsigned char CurrentTestFailed;
unsigned char CurrentTestIgnored; unsigned char CurrentTestIgnored;
float DefaultDelta;
jmp_buf AbortFrame; jmp_buf AbortFrame;
}; };
extern struct _Unity Unity; extern struct _Unity Unity;
//------------------------------------------------------- //-------------------------------------------------------
// Test Suite Management // Test Suite Management
//------------------------------------------------------- //-------------------------------------------------------
@@ -79,12 +95,10 @@ void UnityBegin(void);
void UnityEnd(void); void UnityEnd(void);
void UnityConcludeTest(void); void UnityConcludeTest(void);
//------------------------------------------------------- //-------------------------------------------------------
// Test Output // Test Output
//------------------------------------------------------- //-------------------------------------------------------
void UnityPrintChar(const char ch);
void UnityPrint(const char* string); void UnityPrint(const char* string);
void UnityPrintMask(const unsigned long mask, const unsigned long number); void UnityPrintMask(const unsigned long mask, const unsigned long number);
void UnityPrintNumberByStyle(const long number, const UNITY_DISPLAY_STYLE_T style); void UnityPrintNumberByStyle(const long number, const UNITY_DISPLAY_STYLE_T style);
@@ -92,10 +106,13 @@ void UnityPrintNumber(const long number);
void UnityPrintNumberUnsigned(const unsigned long number); void UnityPrintNumberUnsigned(const unsigned long number);
void UnityPrintNumberHex(const unsigned long number, const char nibbles); void UnityPrintNumberHex(const unsigned long number, const char nibbles);
//------------------------------------------------------- //-------------------------------------------------------
// Test Assertion Fuctions // Test Assertion Fuctions
//------------------------------------------------------- //-------------------------------------------------------
// 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.
void UnityAssertEqualNumber(const long expected, void UnityAssertEqualNumber(const long expected,
const long actual, const long actual,
@@ -147,12 +164,6 @@ void UnityAssertEqualMemoryArray(const void* expected,
const char* msg, const char* msg,
const unsigned short lineNumber ); const unsigned short lineNumber );
void UnityAssertFloatsWithin(const float delta,
const float expected,
const float actual,
const char* msg,
const unsigned short lineNumber);
void UnityAssertNumbersWithin(const long delta, void UnityAssertNumbersWithin(const long delta,
const long expected, const long expected,
const long actual, const long actual,
@@ -169,6 +180,13 @@ void UnityFail(const char* message, const long line);
void UnityIgnore(const char* message, const long line); void UnityIgnore(const char* message, const long line);
#ifndef UNITY_EXCLUDE_FLOAT
void UnityAssertFloatsWithin(const _UF delta,
const _UF expected,
const _UF actual,
const char* msg,
const unsigned short lineNumber);
#endif
//------------------------------------------------------- //-------------------------------------------------------
// Test Running Macros // Test Running Macros
@@ -186,13 +204,11 @@ void UnityIgnore(const char* message, const long line);
Unity.NumberOfTests ++; \ Unity.NumberOfTests ++; \
runTest(func); \ runTest(func); \
UnityConcludeTest(); UnityConcludeTest();
//------------------------------------------------------- //-------------------------------------------------------
// Test Asserts // Test Asserts
//
// (these are the macros you are looking for)
//------------------------------------------------------- //-------------------------------------------------------
// these are the macros you are looking for
#define TEST_ASSERT_MESSAGE(condition, message) if (condition) {} else {TEST_FAIL(message);} #define TEST_ASSERT_MESSAGE(condition, message) if (condition) {} else {TEST_FAIL(message);}
#define TEST_ASSERT(condition) TEST_ASSERT_MESSAGE(condition, NULL) #define TEST_ASSERT(condition) TEST_ASSERT_MESSAGE(condition, NULL)
@@ -308,14 +324,6 @@ void UnityIgnore(const char* message, const long line);
ABORT_IF_NECESSARY(); ABORT_IF_NECESSARY();
#define TEST_ASSERT_BIT_LOW(bit, actual) TEST_ASSERT_BIT_LOW_MESSAGE(bit, actual, NULL) #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__); \
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 / 10000.0f, expected, actual, message)
#define TEST_ASSERT_EQUAL_FLOAT(expected, actual) TEST_ASSERT_FLOAT_WITHIN_MESSAGE((expected) / 10000.0f, expected, actual, NULL)
#define TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, actual, message) \ #define TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, actual, message) \
Unity.TestFile=__FILE__; \ Unity.TestFile=__FILE__; \
UnityAssertEqualString((expected), (actual), (message), (unsigned short)__LINE__); \ UnityAssertEqualString((expected), (actual), (message), (unsigned short)__LINE__); \
@@ -338,4 +346,19 @@ void UnityIgnore(const char* message, const long line);
#define TEST_IGNORE_MESSAGE(message) { Unity.TestFile=__FILE__; UnityIgnore((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) #define TEST_IGNORE() TEST_IGNORE_MESSAGE(NULL)
#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) \
Unity.TestFile=__FILE__; \
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
#endif #endif