mirror of
https://github.com/ThrowTheSwitch/Unity.git
synced 2025-12-17 22:08:07 +08:00
- added an "equal" check for floating point (where it checks that floats are within a significant digit of eachother)
- added array support for unknown types (memcompares) git-svn-id: http://unity.svn.sourceforge.net/svnroot/unity/trunk@45 e7d17a6e-8845-0410-bbbc-c8efb4fdad7e
This commit is contained in:
87
src/unity.c
87
src/unity.c
@@ -321,7 +321,7 @@ void UnityAssertEqualUnsignedIntArray(const unsigned int* expected,
|
|||||||
Unity.CurrentTestFailed = 1;
|
Unity.CurrentTestFailed = 1;
|
||||||
|
|
||||||
UnityTestResultsBegin(lineNumber);
|
UnityTestResultsBegin(lineNumber);
|
||||||
UnityPrint("You asked me to compare 0 elements of an array, which was pointless.");
|
UnityPrint("You asked me to compare nothing, which was pointless.");
|
||||||
if (msg)
|
if (msg)
|
||||||
{
|
{
|
||||||
UnityPrintChar(' ');
|
UnityPrintChar(' ');
|
||||||
@@ -363,13 +363,18 @@ void UnityAssertFloatsWithin(const float delta,
|
|||||||
const unsigned short lineNumber)
|
const unsigned short lineNumber)
|
||||||
{
|
{
|
||||||
float diff = actual - expected;
|
float diff = actual - expected;
|
||||||
|
float pos_delta = delta;
|
||||||
|
|
||||||
if (diff < 0)
|
if (diff < 0)
|
||||||
{
|
{
|
||||||
diff = -diff;
|
diff = 0.0f - diff;
|
||||||
}
|
}
|
||||||
|
if (pos_delta < 0)
|
||||||
if (delta < diff)
|
{
|
||||||
|
pos_delta = 0.0f - pos_delta;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pos_delta < diff)
|
||||||
{
|
{
|
||||||
Unity.CurrentTestFailed = 1;
|
Unity.CurrentTestFailed = 1;
|
||||||
UnityTestResultsBegin(lineNumber);
|
UnityTestResultsBegin(lineNumber);
|
||||||
@@ -484,7 +489,19 @@ void UnityAssertEqualMemory(const void* expected,
|
|||||||
const unsigned short lineNumber)
|
const unsigned short lineNumber)
|
||||||
{
|
{
|
||||||
if (length == 0)
|
if (length == 0)
|
||||||
|
{
|
||||||
|
Unity.CurrentTestFailed = 1;
|
||||||
|
|
||||||
|
UnityTestResultsBegin(lineNumber);
|
||||||
|
UnityPrint("You asked me to compare nothing, which was pointless.");
|
||||||
|
if (msg)
|
||||||
|
{
|
||||||
|
UnityPrintChar(' ');
|
||||||
|
UnityPrint(msg);
|
||||||
|
}
|
||||||
|
UnityPrintChar('\n');
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// if both pointers not null compare the memory
|
// if both pointers not null compare the memory
|
||||||
if (expected && actual)
|
if (expected && actual)
|
||||||
@@ -515,6 +532,66 @@ void UnityAssertEqualMemory(const void* expected,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void UnityAssertEqualMemoryArray(const void* expected,
|
||||||
|
const void* actual,
|
||||||
|
unsigned long length,
|
||||||
|
unsigned long num_elements,
|
||||||
|
const char* msg,
|
||||||
|
const unsigned short lineNumber)
|
||||||
|
{
|
||||||
|
unsigned long elements = num_elements;
|
||||||
|
if ((elements == 0) || (length == 0))
|
||||||
|
{
|
||||||
|
Unity.CurrentTestFailed = 1;
|
||||||
|
|
||||||
|
UnityTestResultsBegin(lineNumber);
|
||||||
|
UnityPrint("You asked me to compare nothing, which was pointless.");
|
||||||
|
if (msg)
|
||||||
|
{
|
||||||
|
UnityPrintChar(' ');
|
||||||
|
UnityPrint(msg);
|
||||||
|
}
|
||||||
|
UnityPrintChar('\n');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// if both pointers not null compare the memory
|
||||||
|
if (expected && actual)
|
||||||
|
{
|
||||||
|
while (elements--)
|
||||||
|
{
|
||||||
|
if (memcmp(expected, actual, length) != 0)
|
||||||
|
{
|
||||||
|
Unity.CurrentTestFailed = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
expected += length;
|
||||||
|
actual += length;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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("Element ");
|
||||||
|
UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
|
||||||
|
UnityPrint(" Memory Mismatch.");
|
||||||
|
if (msg)
|
||||||
|
{
|
||||||
|
UnityPrintChar(' ');
|
||||||
|
UnityPrint(msg);
|
||||||
|
}
|
||||||
|
UnityPrintChar('\n');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void UnityFail(const char* message, const long line)
|
void UnityFail(const char* message, const long line)
|
||||||
{
|
{
|
||||||
Unity.CurrentTestFailed = 1;
|
Unity.CurrentTestFailed = 1;
|
||||||
|
|||||||
15
src/unity.h
15
src/unity.h
@@ -140,6 +140,13 @@ void UnityAssertEqualMemory(const void* expected,
|
|||||||
const char* msg,
|
const char* msg,
|
||||||
const unsigned short lineNumber );
|
const unsigned short lineNumber );
|
||||||
|
|
||||||
|
void UnityAssertEqualMemoryArray(const void* expected,
|
||||||
|
const void* actual,
|
||||||
|
unsigned long length,
|
||||||
|
unsigned long num_elements,
|
||||||
|
const char* msg,
|
||||||
|
const unsigned short lineNumber );
|
||||||
|
|
||||||
void UnityAssertFloatsWithin(const float delta,
|
void UnityAssertFloatsWithin(const float delta,
|
||||||
const float expected,
|
const float expected,
|
||||||
const float actual,
|
const float actual,
|
||||||
@@ -306,6 +313,8 @@ void UnityIgnore(const char* message, const long line);
|
|||||||
UnityAssertFloatsWithin((delta), (expected), (actual), (message), (unsigned short)__LINE__); \
|
UnityAssertFloatsWithin((delta), (expected), (actual), (message), (unsigned short)__LINE__); \
|
||||||
ABORT_IF_NECESSARY();
|
ABORT_IF_NECESSARY();
|
||||||
#define TEST_ASSERT_FLOAT_WITHIN(delta, expected, actual) TEST_ASSERT_FLOAT_WITHIN_MESSAGE(delta, expected, actual, NULL)
|
#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__; \
|
||||||
@@ -319,6 +328,12 @@ void UnityIgnore(const char* message, const long line);
|
|||||||
ABORT_IF_NECESSARY();
|
ABORT_IF_NECESSARY();
|
||||||
#define TEST_ASSERT_EQUAL_MEMORY(expected, actual, len) TEST_ASSERT_EQUAL_MEMORY_MESSAGE(expected, actual, len, NULL)
|
#define TEST_ASSERT_EQUAL_MEMORY(expected, actual, len) TEST_ASSERT_EQUAL_MEMORY_MESSAGE(expected, actual, len, NULL)
|
||||||
|
|
||||||
|
#define TEST_ASSERT_EQUAL_MEMORY_ARRAY_MESSAGE(expected, actual, len, num_elements, message) \
|
||||||
|
Unity.TestFile=__FILE__; \
|
||||||
|
UnityAssertEqualMemoryArray((expected), (actual), (len), (num_elements), (message), (unsigned short)__LINE__); \
|
||||||
|
ABORT_IF_NECESSARY();
|
||||||
|
#define TEST_ASSERT_EQUAL_MEMORY_ARRAY(expected, actual, len, num_elements) TEST_ASSERT_EQUAL_MEMORY_ARRAY_MESSAGE(expected, actual, len, num_elements, NULL)
|
||||||
|
|
||||||
#define TEST_FAIL(message) { Unity.TestFile=__FILE__; UnityFail((message), (unsigned short)__LINE__); TEST_ABORT(); }
|
#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_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)
|
||||||
|
|||||||
@@ -507,6 +507,28 @@ void testFloatsNotWithinDelta(void)
|
|||||||
VERIFY_FAILURE_WAS_CAUGHT
|
VERIFY_FAILURE_WAS_CAUGHT
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void testFloatsEqual(void)
|
||||||
|
{
|
||||||
|
TEST_ASSERT_EQUAL_FLOAT(187245.0f, 187246.0f);
|
||||||
|
TEST_ASSERT_EQUAL_FLOAT(18724.5f, 18724.6f);
|
||||||
|
TEST_ASSERT_EQUAL_FLOAT(9273.2549f, 9273.2599f);
|
||||||
|
TEST_ASSERT_EQUAL_FLOAT(-726.93724f, -726.9374f);
|
||||||
|
}
|
||||||
|
|
||||||
|
void testFloatsNotEqual(void)
|
||||||
|
{
|
||||||
|
int failed;
|
||||||
|
|
||||||
|
EXPECT_ABORT_BEGIN
|
||||||
|
TEST_ASSERT_EQUAL_FLOAT(9273.9649f, 9273.0049f);
|
||||||
|
EXPECT_ABORT_END
|
||||||
|
|
||||||
|
failed = Unity.CurrentTestFailed;
|
||||||
|
Unity.CurrentTestFailed = 0;
|
||||||
|
|
||||||
|
VERIFY_FAILURE_WAS_CAUGHT
|
||||||
|
}
|
||||||
|
|
||||||
void testIntsWithinDelta(void)
|
void testIntsWithinDelta(void)
|
||||||
{
|
{
|
||||||
TEST_ASSERT_INT_WITHIN(1, 5000, 5001);
|
TEST_ASSERT_INT_WITHIN(1, 5000, 5001);
|
||||||
@@ -614,14 +636,13 @@ void testNotEqualString_ActualStringIsNull(void)
|
|||||||
void testEqualMemory(void)
|
void testEqualMemory(void)
|
||||||
{
|
{
|
||||||
const char *testString = "whatever";
|
const char *testString = "whatever";
|
||||||
|
|
||||||
TEST_ASSERT_EQUAL_MEMORY(testString, testString, 8);
|
TEST_ASSERT_EQUAL_MEMORY(testString, testString, 8);
|
||||||
TEST_ASSERT_EQUAL_MEMORY("whatever", "whatever", 8);
|
TEST_ASSERT_EQUAL_MEMORY("whatever", "whatever", 8);
|
||||||
TEST_ASSERT_EQUAL_MEMORY("whatever", testString, 8);
|
TEST_ASSERT_EQUAL_MEMORY("whatever", testString, 8);
|
||||||
TEST_ASSERT_EQUAL_MEMORY(testString, "whatever", 8);
|
TEST_ASSERT_EQUAL_MEMORY(testString, "whatever", 8);
|
||||||
TEST_ASSERT_EQUAL_MEMORY(testString, "whatever", 0);
|
TEST_ASSERT_EQUAL_MEMORY(testString, "whatever", 2);
|
||||||
TEST_ASSERT_EQUAL_MEMORY(NULL, NULL, 0);
|
TEST_ASSERT_EQUAL_MEMORY(NULL, NULL, 1);
|
||||||
TEST_ASSERT_EQUAL_INT(0U, Unity.TestFailures);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void testNotEqualMemory1(void)
|
void testNotEqualMemory1(void)
|
||||||
@@ -810,6 +831,71 @@ void testNotEqualUIntArrays3(void)
|
|||||||
VERIFY_FAILURE_WAS_CAUGHT
|
VERIFY_FAILURE_WAS_CAUGHT
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void testEqualMemoryArrays(void)
|
||||||
|
{
|
||||||
|
int p0[] = {1, 8, 987, -2};
|
||||||
|
int p1[] = {1, 8, 987, -2};
|
||||||
|
int p2[] = {1, 8, 987, 2};
|
||||||
|
int p3[] = {1, 500, 600, 700};
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL_MEMORY_ARRAY(p0, p0, 4, 1);
|
||||||
|
TEST_ASSERT_EQUAL_MEMORY_ARRAY(p0, p0, 4, 4);
|
||||||
|
TEST_ASSERT_EQUAL_MEMORY_ARRAY(p0, p1, 4, 4);
|
||||||
|
TEST_ASSERT_EQUAL_MEMORY_ARRAY(p0, p2, 4, 3);
|
||||||
|
TEST_ASSERT_EQUAL_MEMORY_ARRAY(p0, p3, 4, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void testNotEqualMemoryArrays1(void)
|
||||||
|
{
|
||||||
|
int p0[] = {1, 8, 987, -2};
|
||||||
|
int p1[] = {1, 8, 987, 2};
|
||||||
|
|
||||||
|
int failed;
|
||||||
|
|
||||||
|
EXPECT_ABORT_BEGIN
|
||||||
|
TEST_ASSERT_EQUAL_MEMORY_ARRAY(p0, p1, 4, 4);
|
||||||
|
EXPECT_ABORT_END
|
||||||
|
|
||||||
|
failed = Unity.CurrentTestFailed;
|
||||||
|
Unity.CurrentTestFailed = 0;
|
||||||
|
|
||||||
|
VERIFY_FAILURE_WAS_CAUGHT
|
||||||
|
}
|
||||||
|
|
||||||
|
void testNotEqualMemoryArrays2(void)
|
||||||
|
{
|
||||||
|
int p0[] = {1, 8, 987, -2};
|
||||||
|
int p1[] = {2, 8, 987, -2};
|
||||||
|
|
||||||
|
int failed;
|
||||||
|
|
||||||
|
EXPECT_ABORT_BEGIN
|
||||||
|
TEST_ASSERT_EQUAL_MEMORY_ARRAY(p0, p1, 4, 4);
|
||||||
|
EXPECT_ABORT_END
|
||||||
|
|
||||||
|
failed = Unity.CurrentTestFailed;
|
||||||
|
Unity.CurrentTestFailed = 0;
|
||||||
|
|
||||||
|
VERIFY_FAILURE_WAS_CAUGHT
|
||||||
|
}
|
||||||
|
|
||||||
|
void testNotEqualMemoryArrays3(void)
|
||||||
|
{
|
||||||
|
int p0[] = {1, 8, 987, -2};
|
||||||
|
int p1[] = {1, 8, 986, -2};
|
||||||
|
|
||||||
|
int failed;
|
||||||
|
|
||||||
|
EXPECT_ABORT_BEGIN
|
||||||
|
TEST_ASSERT_EQUAL_MEMORY_ARRAY(p0, p1, 4, 4);
|
||||||
|
EXPECT_ABORT_END
|
||||||
|
|
||||||
|
failed = Unity.CurrentTestFailed;
|
||||||
|
Unity.CurrentTestFailed = 0;
|
||||||
|
|
||||||
|
VERIFY_FAILURE_WAS_CAUGHT
|
||||||
|
}
|
||||||
|
|
||||||
void testProtection(void)
|
void testProtection(void)
|
||||||
{
|
{
|
||||||
volatile int mask = 0;
|
volatile int mask = 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user