mirror of
https://github.com/ThrowTheSwitch/Unity.git
synced 2026-01-25 09:21:36 +01:00
- support arrays of integers
- fixed bug when reporting problems with large integers - fixed bug in test suite when checking for expected failures. git-svn-id: http://unity.svn.sourceforge.net/svnroot/unity/trunk@22 e7d17a6e-8845-0410-bbbc-c8efb4fdad7e
This commit is contained in:
131
src/unity.c
131
src/unity.c
@@ -38,18 +38,19 @@ void UnityPrintNumberByStyle(const long number, const UNITY_DISPLAY_STYLE_T styl
|
||||
{
|
||||
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;
|
||||
case UNITY_DISPLAY_STYLE_HEX8: UnityPrintNumberHex((unsigned long)number, 2); break;
|
||||
case UNITY_DISPLAY_STYLE_HEX16: UnityPrintNumberHex((unsigned long)number, 4); break;
|
||||
case UNITY_DISPLAY_STYLE_HEX32: UnityPrintNumberHex((unsigned long)number, 8); break;
|
||||
case UNITY_DISPLAY_STYLE_UINT: UnityPrintNumberUnsigned((unsigned long)number); break;
|
||||
default: UnityPrintNumber(number); break;
|
||||
}
|
||||
}
|
||||
|
||||
/// basically do an itoa using as little ram as possible
|
||||
void UnityPrintNumber(const long number_to_print)
|
||||
{
|
||||
unsigned long divisor = 10;
|
||||
long divisor = 1;
|
||||
long next_divisor;
|
||||
long number = number_to_print;
|
||||
|
||||
if (number < 0)
|
||||
@@ -59,38 +60,47 @@ void UnityPrintNumber(const long number_to_print)
|
||||
}
|
||||
|
||||
// figure out initial divisor
|
||||
while (number / divisor)
|
||||
while (number / divisor > 9)
|
||||
{
|
||||
divisor *= 10;
|
||||
next_divisor = divisor * 10;
|
||||
if (next_divisor > divisor)
|
||||
divisor = next_divisor;
|
||||
else
|
||||
break;
|
||||
}
|
||||
|
||||
// now divide number by divisor, mod and print, then divide divisor
|
||||
// now mod and print, then divide divisor
|
||||
do
|
||||
{
|
||||
divisor /= 10;
|
||||
UnityPrintChar((char)('0' + (number / divisor % 10)));
|
||||
divisor /= 10;
|
||||
}
|
||||
while (divisor > 1U);
|
||||
while (divisor > 0);
|
||||
}
|
||||
|
||||
/// basically do an itoa using as little ram as possible
|
||||
void UnityPrintNumberUnsigned(const unsigned long number)
|
||||
{
|
||||
unsigned long divisor = 10;
|
||||
unsigned long divisor = 1;
|
||||
unsigned long next_divisor;
|
||||
|
||||
// figure out initial divisor
|
||||
while (number / divisor)
|
||||
while (number / divisor > 9)
|
||||
{
|
||||
divisor *= 10;
|
||||
next_divisor = divisor * 10;
|
||||
if (next_divisor > divisor)
|
||||
divisor = next_divisor;
|
||||
else
|
||||
break;
|
||||
}
|
||||
|
||||
// now divide number by divisor, mod and print, then divide divisor
|
||||
// now mod and print, then divide divisor
|
||||
do
|
||||
{
|
||||
divisor /= 10;
|
||||
UnityPrintChar((char)('0' + (number / divisor % 10)));
|
||||
divisor /= 10;
|
||||
}
|
||||
while (divisor > 1U);
|
||||
while (divisor > 0);
|
||||
}
|
||||
|
||||
void UnityPrintNumberHex(const unsigned long number, const char nibbles_to_print)
|
||||
@@ -116,7 +126,7 @@ void UnityPrintNumberHex(const unsigned long number, const char nibbles_to_print
|
||||
void UnityPrintMask(const unsigned long mask, const unsigned long number)
|
||||
{
|
||||
unsigned long bit = 0x00000001;
|
||||
int i;
|
||||
long i;
|
||||
|
||||
for (i = 0; i < 32; i++)
|
||||
{
|
||||
@@ -139,7 +149,7 @@ void UnityPrintMask(const unsigned long mask, const unsigned long number)
|
||||
}
|
||||
}
|
||||
|
||||
void UnityTestResultsBegin(const int line)
|
||||
void UnityTestResultsBegin(const long line)
|
||||
{
|
||||
UnityPrint(Unity.TestFile);
|
||||
UnityPrintChar(':');
|
||||
@@ -169,9 +179,9 @@ void UnityConcludeTest()
|
||||
Unity.CurrentTestIgnored = 0;
|
||||
}
|
||||
|
||||
void UnityAssertBits(const int mask,
|
||||
const int expected,
|
||||
const int actual,
|
||||
void UnityAssertBits(const long mask,
|
||||
const long expected,
|
||||
const long actual,
|
||||
const char* msg,
|
||||
const unsigned short lineNumber)
|
||||
{
|
||||
@@ -194,8 +204,8 @@ void UnityAssertBits(const int mask,
|
||||
}
|
||||
}
|
||||
|
||||
void UnityAssertEqualInt(const int expected,
|
||||
const int actual,
|
||||
void UnityAssertEqualInt(const long expected,
|
||||
const long actual,
|
||||
const char* msg,
|
||||
const unsigned short lineNumber,
|
||||
const UNITY_DISPLAY_STYLE_T style)
|
||||
@@ -219,6 +229,57 @@ void UnityAssertEqualInt(const int expected,
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
unsigned long elements = num_elements;
|
||||
const long* ptr_expected = expected;
|
||||
const long* ptr_actual = actual;
|
||||
|
||||
if (elements == 0)
|
||||
{
|
||||
Unity.CurrentTestFailed = 1;
|
||||
|
||||
UnityTestResultsBegin(lineNumber);
|
||||
UnityPrint("You asked me to compare 0 elements of an array, which was pointless.");
|
||||
if (msg)
|
||||
{
|
||||
UnityPrintChar(' ');
|
||||
UnityPrint(msg);
|
||||
}
|
||||
UnityPrintChar('\n');
|
||||
return;
|
||||
}
|
||||
|
||||
while (elements--)
|
||||
{
|
||||
if (*ptr_expected++ != *ptr_actual++)
|
||||
{
|
||||
Unity.CurrentTestFailed = 1;
|
||||
|
||||
UnityTestResultsBegin(lineNumber);
|
||||
UnityPrint("Element ");
|
||||
UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
|
||||
UnityPrint(" Expected ");
|
||||
UnityPrintNumberByStyle(*--ptr_expected, style);
|
||||
UnityPrint(" was ");
|
||||
UnityPrintNumberByStyle(*--ptr_actual, style);
|
||||
UnityPrintChar('.');
|
||||
if (msg)
|
||||
{
|
||||
UnityPrintChar(' ');
|
||||
UnityPrint(msg);
|
||||
}
|
||||
UnityPrintChar('\n');
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void UnityAssertFloatsWithin(const float delta,
|
||||
const float expected,
|
||||
const float actual,
|
||||
@@ -246,13 +307,13 @@ void UnityAssertFloatsWithin(const float delta,
|
||||
}
|
||||
}
|
||||
|
||||
void UnityAssertIntsWithin(const int delta,
|
||||
const int expected,
|
||||
const int actual,
|
||||
void UnityAssertIntsWithin(const long delta,
|
||||
const long expected,
|
||||
const long actual,
|
||||
const char* msg,
|
||||
const unsigned short lineNumber)
|
||||
{
|
||||
int diff = actual - expected;
|
||||
long diff = actual - expected;
|
||||
|
||||
if (diff < 0)
|
||||
{
|
||||
@@ -278,7 +339,7 @@ void UnityAssertEqualString(const char* expected,
|
||||
const char* msg,
|
||||
unsigned short lineNumber)
|
||||
{
|
||||
unsigned int i;
|
||||
unsigned long i;
|
||||
|
||||
// if both pointers not null compare the strings
|
||||
if (expected && actual)
|
||||
@@ -318,9 +379,9 @@ void UnityAssertEqualString(const char* expected,
|
||||
}
|
||||
|
||||
|
||||
void UnityAssertEqualMemory(void* expected,
|
||||
void* actual,
|
||||
unsigned int length,
|
||||
void UnityAssertEqualMemory(const void* expected,
|
||||
const void* actual,
|
||||
unsigned long length,
|
||||
const char* msg,
|
||||
unsigned short lineNumber)
|
||||
{
|
||||
@@ -356,7 +417,7 @@ void UnityAssertEqualMemory(void* expected,
|
||||
}
|
||||
}
|
||||
|
||||
void UnityFail(const char* message, const int line)
|
||||
void UnityFail(const char* message, const long line)
|
||||
{
|
||||
Unity.CurrentTestFailed = 1;
|
||||
UnityTestResultsBegin(line);
|
||||
@@ -364,7 +425,7 @@ void UnityFail(const char* message, const int line)
|
||||
UnityPrintChar('\n');
|
||||
}
|
||||
|
||||
void UnityIgnore(const char* message, const int line)
|
||||
void UnityIgnore(const char* message, const long line)
|
||||
{
|
||||
Unity.CurrentTestIgnored = 1;
|
||||
UnityTestResultsBegin(line);
|
||||
@@ -395,7 +456,7 @@ void UnityEnd(void)
|
||||
}
|
||||
}
|
||||
|
||||
int UnityGetNumFailures(void)
|
||||
long UnityGetNumFailures(void)
|
||||
{
|
||||
return Unity.TestFailures;
|
||||
}
|
||||
|
||||
89
src/unity.h
89
src/unity.h
@@ -6,6 +6,15 @@
|
||||
#include <stdio.h>
|
||||
#include <setjmp.h>
|
||||
|
||||
//-------------------------------------------------------
|
||||
// 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.)
|
||||
//-------------------------------------------------------
|
||||
|
||||
typedef void (*UnityTestFunction)(void);
|
||||
|
||||
typedef enum
|
||||
@@ -36,7 +45,7 @@ void CreateResults();
|
||||
|
||||
void UnityBegin();
|
||||
void UnityEnd(void);
|
||||
int UnityGetNumFailures(void);
|
||||
long UnityGetNumFailures(void);
|
||||
|
||||
void UnityPrintChar(const char ch);
|
||||
void UnityPrint(const char* string);
|
||||
@@ -47,15 +56,22 @@ void UnityPrintNumberUnsigned(const unsigned long number);
|
||||
void UnityPrintNumberHex(const unsigned long number, const char nibbles);
|
||||
void UnityConcludeTest();
|
||||
|
||||
void UnityAssertEqualInt(const int expected,
|
||||
const int actual,
|
||||
void UnityAssertEqualInt(const long expected,
|
||||
const long 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,
|
||||
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,
|
||||
const char* msg,
|
||||
const unsigned short lineNumber);
|
||||
|
||||
@@ -64,9 +80,9 @@ void UnityAssertEqualString(const char* expected,
|
||||
const char* msg,
|
||||
const unsigned short lineNumber );
|
||||
|
||||
void UnityAssertEqualMemory(void* expected,
|
||||
void* actual,
|
||||
unsigned int length,
|
||||
void UnityAssertEqualMemory(const void* expected,
|
||||
const void* actual,
|
||||
unsigned long length,
|
||||
const char* msg,
|
||||
const unsigned short lineNumber );
|
||||
|
||||
@@ -76,15 +92,19 @@ void UnityAssertFloatsWithin(const float delta,
|
||||
const char* msg,
|
||||
const unsigned short lineNumber);
|
||||
|
||||
void UnityAssertIntsWithin(const int delta,
|
||||
const int expected,
|
||||
const int actual,
|
||||
void UnityAssertIntsWithin(const long delta,
|
||||
const long expected,
|
||||
const long actual,
|
||||
const char* msg,
|
||||
const unsigned short lineNumber);
|
||||
|
||||
void UnityFail(const char* message, const int line);
|
||||
void UnityFail(const char* message, const long line);
|
||||
|
||||
void UnityIgnore(const char* message, const int line);
|
||||
void UnityIgnore(const char* message, const long line);
|
||||
|
||||
//-------------------------------------------------------
|
||||
// Test Running Macros
|
||||
//-------------------------------------------------------
|
||||
|
||||
#define TEST_PROTECT() (setjmp(Unity.AbortFrame) == 0)
|
||||
|
||||
@@ -99,6 +119,12 @@ void UnityIgnore(const char* message, const int line);
|
||||
runTest(func); \
|
||||
UnityConcludeTest();
|
||||
|
||||
//-------------------------------------------------------
|
||||
// Test Asserts
|
||||
//
|
||||
// (these are the macros you are looking for)
|
||||
//-------------------------------------------------------
|
||||
|
||||
#define TEST_ASSERT_MESSAGE(condition, message) if (condition) {} else {TEST_FAIL(message);}
|
||||
#define TEST_ASSERT(condition) TEST_ASSERT_MESSAGE(condition, NULL)
|
||||
|
||||
@@ -112,15 +138,20 @@ void UnityIgnore(const char* message, const int line);
|
||||
#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); \
|
||||
UnityAssertEqualInt((long)(expected), (long)(actual), (message), (unsigned short)__LINE__, UNITY_DISPLAY_STYLE_INT); \
|
||||
ABORT_IF_NECESSARY();
|
||||
#define TEST_ASSERT_EQUAL_INT(expected, actual) TEST_ASSERT_EQUAL_INT_MESSAGE(expected, actual, NULL)
|
||||
|
||||
#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)
|
||||
|
||||
#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)
|
||||
|
||||
@@ -132,31 +163,49 @@ void UnityIgnore(const char* message, const int line);
|
||||
|
||||
#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); \
|
||||
UnityAssertEqualInt((long)(expected), (long)(actual), (message), (unsigned short)__LINE__, UNITY_DISPLAY_STYLE_UINT); \
|
||||
ABORT_IF_NECESSARY();
|
||||
#define TEST_ASSERT_EQUAL_UINT(expected, actual) TEST_ASSERT_EQUAL_UINT_MESSAGE(expected, actual, NULL)
|
||||
|
||||
#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)
|
||||
|
||||
#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); \
|
||||
UnityAssertEqualInt((long)(expected), (long)(actual), (message), (unsigned short)__LINE__, UNITY_DISPLAY_STYLE_HEX8); \
|
||||
ABORT_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); \
|
||||
UnityAssertEqualInt((long)(expected), (long)(actual), (message), (unsigned short)__LINE__, UNITY_DISPLAY_STYLE_HEX16); \
|
||||
ABORT_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); \
|
||||
UnityAssertEqualInt((long)(expected), (long)(actual), (message), (unsigned short)__LINE__, UNITY_DISPLAY_STYLE_HEX32); \
|
||||
ABORT_IF_NECESSARY();
|
||||
#define TEST_ASSERT_EQUAL_HEX32(expected, actual) TEST_ASSERT_EQUAL_HEX32_MESSAGE(expected, actual, NULL)
|
||||
|
||||
#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)
|
||||
|
||||
#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, actual)
|
||||
|
||||
#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)
|
||||
|
||||
#define TEST_ASSERT_BITS_MESSAGE(mask, expected, actual, message) \
|
||||
Unity.TestFile=__FILE__; \
|
||||
UnityAssertBits((mask), (expected), (actual), (message), (unsigned short)__LINE__); \
|
||||
|
||||
Reference in New Issue
Block a user