From 24a56b0c38496a20735b5015414c6ad4b910f03f Mon Sep 17 00:00:00 2001 From: mvandervoord Date: Thu, 19 Feb 2009 03:30:45 +0000 Subject: [PATCH] - happier with const (and more optimized on some compilers) - better helper examples - general purpose memory compare git-svn-id: http://unity.svn.sourceforge.net/svnroot/unity/trunk@16 e7d17a6e-8845-0410-bbbc-c8efb4fdad7e --- auto/generate_test_runner.rb | 2 +- src/UnityHelper.c | 6 +- src/UnityHelper.h | 11 ++- src/unity.c | 45 ++++++++++++ src/unity.h | 12 ++++ test/testunity.c | 129 +++++++++++++++++++++++++++-------- test/testunity_Runner.c | 12 +++- 7 files changed, 174 insertions(+), 43 deletions(-) diff --git a/auto/generate_test_runner.rb b/auto/generate_test_runner.rb index be94d3e..8d23d93 100644 --- a/auto/generate_test_runner.rb +++ b/auto/generate_test_runner.rb @@ -59,7 +59,7 @@ class UnityTestRunnerGenerator def find_mocks(includes) mock_headers = [] includes.each do |include_file| - mock_headers << include_file if include_file.include? "Mock" + mock_headers << include_file if (include_file =~ /^mock/i) end return mock_headers end diff --git a/src/UnityHelper.c b/src/UnityHelper.c index 7c07f41..5a3d08a 100644 --- a/src/UnityHelper.c +++ b/src/UnityHelper.c @@ -40,8 +40,6 @@ void AssertFloatArrayWithin(const float tolerance, const float* expected, const void AssertEqualExampleStruct(EXAMPLE_STRUCT_T expected, EXAMPLE_STRUCT_T actual) { - sprintf(message, "Example Struct Failed For Field x", i); - TEST_ASSERT_EQUAL_INT_MESSAGE(expected.x, actual.x, message); - sprintf(message, "Example Struct Failed For Field y", i); - TEST_ASSERT_EQUAL_INT_MESSAGE(expected.y, actual.y, message); + TEST_ASSERT_EQUAL_INT_MESSAGE(expected.x, actual.x, "Example Struct Failed For Field x"); + TEST_ASSERT_EQUAL_INT_MESSAGE(expected.y, actual.y, "Example Struct Failed For Field y"); } diff --git a/src/UnityHelper.h b/src/UnityHelper.h index c444625..b0090a6 100644 --- a/src/UnityHelper.h +++ b/src/UnityHelper.h @@ -1,20 +1,17 @@ #ifndef _TESTHELPER_H #define _TESTHELPER_H -typedef struct _EXAMPLE_STRUCT_T -{ - int x; - int y; -} EXAMPLE_STRUCT_T; +#include "Types.h" void AssertEqualUintArray(const unsigned int* expected, const unsigned int* actual, const unsigned int length); void AssertEqualIntArray(const int* expected, const int* actual, const unsigned int length); -void AssertEqualCharArray(const char[] expected, const char actual[], const int length); +void AssertEqualCharArray(const char expected[], const char actual[], const int length); void AssertFloatArrayWithin(const float tolerance, const float* expected, const float* actual, const unsigned int length); void AssertEqualExampleStruct(const EXAMPLE_STRUCT_T expected, const EXAMPLE_STRUCT_T actual); #define TEST_ASSERT_EQUAL_UINT_ARRAY(expected, actual, length) {AssertEqualUintArray(expected, actual, length);} #define TEST_ASSERT_EQUAL_INT_ARRAY(expected, actual, length) {AssertEqualIntArray(expected, actual, length);} #define TEST_ASSERT_FLOAT_ARRAY_WITHIN(tolerance, expected, actual, length) {AssertFloatArrayWithin(tolerance, expected, actual, length);} -#define TEST_ASSERT_EQUAL_EXAMPLE_STRUCT(expected, actual) {AssertEqualExampleStruct(expected, actual);} +#define TEST_ASSERT_EQUAL_EXAMPLE_STRUCT_T(expected, actual) {AssertEqualExampleStruct(expected, actual);} + #endif // _TESTHELPER_H diff --git a/src/unity.c b/src/unity.c index 77b991a..c555d21 100644 --- a/src/unity.c +++ b/src/unity.c @@ -316,6 +316,51 @@ void UnityAssertEqualString(const char *expected, } } + +void UnityAssertEqualMemory(void *expected, + void *actual, + unsigned int length, + const char *msg, + unsigned short lineNumber) +{ + unsigned int i; + if (length == 0) + return; + + // if both pointers not null compare the memory + if (expected && actual) + { + if (memcmp(expected, actual, length) != 0) + { + 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, const int line) { Unity.CurrentTestFailed = 1; diff --git a/src/unity.h b/src/unity.h index 0cb6ffd..b4b35e9 100644 --- a/src/unity.h +++ b/src/unity.h @@ -64,6 +64,12 @@ void UnityAssertEqualString(const char *expected, const char *msg, const unsigned short lineNumber ); +void UnityAssertEqualMemory(void *expected, + void *actual, + unsigned int length, + const char *msg, + const unsigned short lineNumber ); + void UnityAssertFloatsWithin(const float delta, const float expected, const float actual, @@ -193,6 +199,12 @@ void UnityIgnore(const char *message, const int line); ABORT_IF_NECESSARY(); #define TEST_ASSERT_EQUAL_STRING(expected, actual) TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, actual, NULL) +#define TEST_ASSERT_EQUAL_MEMORY_MESSAGE(expected, actual, len, message) \ + Unity.TestFile=__FILE__; \ + UnityAssertEqualMemory((expected), (actual), (len), (message), (unsigned short)__LINE__); \ + ABORT_IF_NECESSARY(); +#define TEST_ASSERT_EQUAL_MEMORY(expected, actual, len) TEST_ASSERT_EQUAL_MEMORY_MESSAGE(expected, actual, len, 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 a1a8d42..63a55d1 100644 --- a/test/testunity.c +++ b/test/testunity.c @@ -23,14 +23,14 @@ void tearDown(void) void testTrue(void) { TEST_ASSERT(1); - + TEST_ASSERT_TRUE(1); } void testFalse(void) { TEST_ASSERT_FALSE(0); - + TEST_ASSERT_UNLESS(0); } @@ -42,11 +42,11 @@ void testPreviousPass(void) void testNotVanilla(void) { int failed; - + EXPECT_ABORT_BEGIN TEST_ASSERT(0); EXPECT_ABORT_END - + failed = Unity.CurrentTestFailed; Unity.CurrentTestFailed = 0; @@ -57,7 +57,7 @@ void testNotVanilla(void) void testNotTrue(void) { int failed; - + EXPECT_ABORT_BEGIN TEST_ASSERT_TRUE(0); EXPECT_ABORT_END @@ -72,7 +72,7 @@ void testNotTrue(void) void testNotFalse(void) { int failed; - + EXPECT_ABORT_BEGIN TEST_ASSERT_FALSE(1); EXPECT_ABORT_END @@ -87,7 +87,7 @@ void testNotFalse(void) void testNotUnless(void) { int failed; - + EXPECT_ABORT_BEGIN TEST_ASSERT_UNLESS(1); EXPECT_ABORT_END @@ -102,7 +102,7 @@ void testNotUnless(void) void testFail(void) { int failed; - + EXPECT_ABORT_BEGIN TEST_FAIL("Expected for testing"); EXPECT_ABORT_END @@ -117,7 +117,7 @@ void testFail(void) void testIgnore(void) { int ignored; - + EXPECT_ABORT_BEGIN TEST_IGNORE(); TEST_FAIL("This should not be reached"); @@ -132,7 +132,7 @@ void testIgnore(void) void testIgnoreMessage(void) { int ignored; - + EXPECT_ABORT_BEGIN TEST_IGNORE_MESSAGE("This is an expected TEST_IGNORE_MESSAGE string!"); TEST_FAIL("This should not be reached"); @@ -147,7 +147,7 @@ void testIgnoreMessage(void) void testNotEqualInts(void) { int failed; - + EXPECT_ABORT_BEGIN TEST_ASSERT_EQUAL_INT(3982, 3983); EXPECT_ABORT_END @@ -161,7 +161,7 @@ void testNotEqualInts(void) void testNotEqualBits(void) { int failed; - + EXPECT_ABORT_BEGIN TEST_ASSERT_BITS(0xFF00, 0x5555, 0x5A55); EXPECT_ABORT_END @@ -176,7 +176,7 @@ void testNotEqualBits(void) void testNotEqualUInts(void) { int failed; - + EXPECT_ABORT_BEGIN TEST_ASSERT_EQUAL_UINT(900000, 900001); EXPECT_ABORT_END @@ -190,7 +190,7 @@ void testNotEqualUInts(void) void testNotEqualHex8s(void) { int failed; - + EXPECT_ABORT_BEGIN TEST_ASSERT_EQUAL_HEX8(0x23,0x22); EXPECT_ABORT_END @@ -204,7 +204,7 @@ void testNotEqualHex8s(void) void testNotEqualHex16s(void) { int failed; - + EXPECT_ABORT_BEGIN TEST_ASSERT_EQUAL_HEX16(0x1234, 0x1235); EXPECT_ABORT_END @@ -218,7 +218,7 @@ void testNotEqualHex16s(void) void testNotEqualHex32s(void) { int failed; - + EXPECT_ABORT_BEGIN TEST_ASSERT_EQUAL_HEX32(900000, 900001); EXPECT_ABORT_END @@ -438,7 +438,7 @@ void testFloatsWithinDelta(void) void testFloatsNotWithinDelta(void) { int failed; - + EXPECT_ABORT_BEGIN TEST_ASSERT_FLOAT_WITHIN(0.05f, 9273.2649f, 9273.2049f); EXPECT_ABORT_END @@ -460,7 +460,7 @@ void testIntsWithinDelta(void) void testIntsNotWithinDelta(void) { int failed; - + EXPECT_ABORT_BEGIN TEST_ASSERT_INT_WITHIN(5, 5000, 5006); EXPECT_ABORT_END @@ -486,11 +486,11 @@ void testEqualStrings(void) void testNotEqualString1(void) { int failed; - + EXPECT_ABORT_BEGIN TEST_ASSERT_EQUAL_STRING("foo", "bar"); EXPECT_ABORT_END - + failed = Unity.CurrentTestFailed; Unity.CurrentTestFailed = 0; @@ -500,11 +500,11 @@ void testNotEqualString1(void) void testNotEqualString2(void) { int failed; - + EXPECT_ABORT_BEGIN TEST_ASSERT_EQUAL_STRING("foo", ""); EXPECT_ABORT_END - + failed = Unity.CurrentTestFailed; Unity.CurrentTestFailed = 0; @@ -514,11 +514,11 @@ void testNotEqualString2(void) void testNotEqualString3(void) { int failed; - + EXPECT_ABORT_BEGIN TEST_ASSERT_EQUAL_STRING("", "bar"); EXPECT_ABORT_END - + failed = Unity.CurrentTestFailed; Unity.CurrentTestFailed = 0; @@ -528,11 +528,11 @@ void testNotEqualString3(void) void testNotEqualString_ExpectedStringIsNull(void) { int failed; - + EXPECT_ABORT_BEGIN TEST_ASSERT_EQUAL_STRING(NULL, "bar"); EXPECT_ABORT_END - + failed = Unity.CurrentTestFailed; Unity.CurrentTestFailed = 0; @@ -542,11 +542,80 @@ void testNotEqualString_ExpectedStringIsNull(void) void testNotEqualString_ActualStringIsNull(void) { int failed; - + EXPECT_ABORT_BEGIN TEST_ASSERT_EQUAL_STRING("foo", NULL); EXPECT_ABORT_END - + + failed = Unity.CurrentTestFailed; + Unity.CurrentTestFailed = 0; + + TEST_ASSERT_MESSAGE(1U == failed, "This is also expected"); +} + +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); +} + +void testNotEqualMemory1(void) +{ + int failed; + + EXPECT_ABORT_BEGIN + TEST_ASSERT_EQUAL_MEMORY("foo", "bar", 3); + EXPECT_ABORT_END + + failed = Unity.CurrentTestFailed; + Unity.CurrentTestFailed = 0; + + TEST_ASSERT_MESSAGE(1U == failed, "This is also expected"); +} + +void testNotEqualMemory2(void) +{ + int failed; + + EXPECT_ABORT_BEGIN + TEST_ASSERT_EQUAL_MEMORY("fool", "food", 4); + EXPECT_ABORT_END + + failed = Unity.CurrentTestFailed; + Unity.CurrentTestFailed = 0; + + TEST_ASSERT_MESSAGE(1U == failed, "This is also expected"); +} + +void testNotEqualMemory3(void) +{ + int failed; + + EXPECT_ABORT_BEGIN + TEST_ASSERT_EQUAL_MEMORY(NULL, "food", 4); + EXPECT_ABORT_END + + failed = Unity.CurrentTestFailed; + Unity.CurrentTestFailed = 0; + + TEST_ASSERT_MESSAGE(1U == failed, "This is also expected"); +} + +void testNotEqualMemory4(void) +{ + int failed; + + EXPECT_ABORT_BEGIN + TEST_ASSERT_EQUAL_MEMORY("fool", NULL, 4); + EXPECT_ABORT_END + failed = Unity.CurrentTestFailed; Unity.CurrentTestFailed = 0; @@ -556,7 +625,7 @@ void testNotEqualString_ActualStringIsNull(void) void testProtection(void) { volatile int mask = 0; - + if (TEST_PROTECT()) { mask |= 1; @@ -567,7 +636,7 @@ void testProtection(void) Unity.CurrentTestFailed = 0; mask |= 2; } - + TEST_ASSERT_EQUAL(3, mask); } diff --git a/test/testunity_Runner.c b/test/testunity_Runner.c index 3ee0397..6c09a9f 100644 --- a/test/testunity_Runner.c +++ b/test/testunity_Runner.c @@ -43,6 +43,11 @@ void testNotEqualString2(void); void testNotEqualString3(void); void testNotEqualString_ExpectedStringIsNull(void); void testNotEqualString_ActualStringIsNull(void); +void testEqualMemory(void); +void testNotEqualMemory1(void); +void testNotEqualMemory2(void); +void testNotEqualMemory3(void); +void testNotEqualMemory4(void); void testProtection(void); @@ -53,7 +58,7 @@ static void runTest(UnityTestFunction test) setUp(); test(); } - + if (TEST_PROTECT()) { tearDown(); @@ -103,6 +108,11 @@ int main(void) RUN_TEST(testNotEqualString3); RUN_TEST(testNotEqualString_ExpectedStringIsNull); RUN_TEST(testNotEqualString_ActualStringIsNull); + RUN_TEST(testEqualMemory); + RUN_TEST(testNotEqualMemory1); + RUN_TEST(testNotEqualMemory2); + RUN_TEST(testNotEqualMemory3); + RUN_TEST(testNotEqualMemory4); RUN_TEST(testProtection); UnityEnd(); -- GitLab