From 6901c8eb045f97572a5ed82a9fb446056c14b37d Mon Sep 17 00:00:00 2001 From: mvandervoord Date: Tue, 3 Nov 2009 01:15:54 +0000 Subject: [PATCH] - 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 --- src/unity.c | 87 +++++++++++++++++++++++++++++++++++++++++--- src/unity.h | 15 ++++++++ test/testunity.c | 94 +++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 187 insertions(+), 9 deletions(-) diff --git a/src/unity.c b/src/unity.c index 28c4831..380fc91 100644 --- a/src/unity.c +++ b/src/unity.c @@ -321,7 +321,7 @@ void UnityAssertEqualUnsignedIntArray(const unsigned int* expected, Unity.CurrentTestFailed = 1; 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) { UnityPrintChar(' '); @@ -363,13 +363,18 @@ void UnityAssertFloatsWithin(const float delta, const unsigned short lineNumber) { float diff = actual - expected; - + float pos_delta = delta; + if (diff < 0) { - diff = -diff; + diff = 0.0f - diff; } - - if (delta < diff) + if (pos_delta < 0) + { + pos_delta = 0.0f - pos_delta; + } + + if (pos_delta < diff) { Unity.CurrentTestFailed = 1; UnityTestResultsBegin(lineNumber); @@ -484,7 +489,19 @@ void UnityAssertEqualMemory(const void* expected, const unsigned short lineNumber) { 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; + } // if both pointers not null compare the memory 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) { Unity.CurrentTestFailed = 1; diff --git a/src/unity.h b/src/unity.h index f69d680..f8c69fe 100644 --- a/src/unity.h +++ b/src/unity.h @@ -140,6 +140,13 @@ void UnityAssertEqualMemory(const void* expected, const char* msg, 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, const float expected, const float actual, @@ -306,6 +313,8 @@ void UnityIgnore(const char* message, const long line); 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) \ Unity.TestFile=__FILE__; \ @@ -319,6 +328,12 @@ void UnityIgnore(const char* message, const long line); 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_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_IGNORE_MESSAGE(message) { Unity.TestFile=__FILE__; UnityIgnore((message), (unsigned short)__LINE__); TEST_ABORT(); } #define TEST_IGNORE() TEST_IGNORE_MESSAGE(NULL) diff --git a/test/testunity.c b/test/testunity.c index 00579f5..9fe5869 100644 --- a/test/testunity.c +++ b/test/testunity.c @@ -507,6 +507,28 @@ void testFloatsNotWithinDelta(void) 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) { TEST_ASSERT_INT_WITHIN(1, 5000, 5001); @@ -614,14 +636,13 @@ void testNotEqualString_ActualStringIsNull(void) void testEqualMemory(void) { const char *testString = "whatever"; - + TEST_ASSERT_EQUAL_MEMORY(testString, testString, 8); TEST_ASSERT_EQUAL_MEMORY("whatever", "whatever", 8); TEST_ASSERT_EQUAL_MEMORY("whatever", testString, 8); TEST_ASSERT_EQUAL_MEMORY(testString, "whatever", 8); - TEST_ASSERT_EQUAL_MEMORY(testString, "whatever", 0); - TEST_ASSERT_EQUAL_MEMORY(NULL, NULL, 0); - TEST_ASSERT_EQUAL_INT(0U, Unity.TestFailures); + TEST_ASSERT_EQUAL_MEMORY(testString, "whatever", 2); + TEST_ASSERT_EQUAL_MEMORY(NULL, NULL, 1); } void testNotEqualMemory1(void) @@ -810,6 +831,71 @@ void testNotEqualUIntArrays3(void) 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) { volatile int mask = 0;