From a11a13750d10b70653f40878558fa44e1533db7a Mon Sep 17 00:00:00 2001 From: Mark VanderVoord Date: Tue, 21 Mar 2017 18:28:49 -0400 Subject: [PATCH] Added memory each equal assertion --- src/unity.c | 7 ++++- src/unity.h | 4 +-- src/unity_internals.h | 9 +++--- test/tests/testunity.c | 65 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 78 insertions(+), 7 deletions(-) diff --git a/src/unity.c b/src/unity.c index 779b43b..f4fe99b 100644 --- a/src/unity.c +++ b/src/unity.c @@ -1079,7 +1079,8 @@ void UnityAssertEqualMemory(UNITY_INTERNAL_PTR expected, const UNITY_UINT32 length, const UNITY_UINT32 num_elements, const char* msg, - const UNITY_LINE_TYPE lineNumber) + const UNITY_LINE_TYPE lineNumber, + const UNITY_FLAGS_T flags) { UNITY_PTR_ATTRIBUTE const unsigned char* ptr_exp = (UNITY_PTR_ATTRIBUTE const unsigned char*)expected; UNITY_PTR_ATTRIBUTE const unsigned char* ptr_act = (UNITY_PTR_ATTRIBUTE const unsigned char*)actual; @@ -1123,6 +1124,10 @@ void UnityAssertEqualMemory(UNITY_INTERNAL_PTR expected, ptr_exp++; ptr_act++; } + if (flags == UNITY_ARRAY_TO_VAL) + { + ptr_exp = (UNITY_PTR_ATTRIBUTE const unsigned char*)expected; + } } } diff --git a/src/unity.h b/src/unity.h index 42c9e6f..19e32ed 100644 --- a/src/unity.h +++ b/src/unity.h @@ -175,7 +175,7 @@ void tearDown(void); #define TEST_ASSERT_EACH_EQUAL_HEX64(expected, actual, num_elements) UNITY_TEST_ASSERT_EACH_EQUAL_HEX64((expected), (actual), (num_elements), __LINE__, NULL) #define TEST_ASSERT_EACH_EQUAL_PTR(expected, actual, num_elements) UNITY_TEST_ASSERT_EACH_EQUAL_PTR((expected), (actual), (num_elements), __LINE__, NULL) //#define TEST_ASSERT_EACH_EQUAL_STRING(expected, actual, num_elements) UNITY_TEST_ASSERT_EACH_EQUAL_STRING((expected), (actual), (num_elements), __LINE__, NULL) -//#define TEST_ASSERT_EACH_EQUAL_MEMORY(expected, actual, len, num_elements) UNITY_TEST_ASSERT_EACH_EQUAL_MEMORY((expected), (actual), (len), (num_elements), __LINE__, NULL) +#define TEST_ASSERT_EACH_EQUAL_MEMORY(expected, actual, len, num_elements) UNITY_TEST_ASSERT_EACH_EQUAL_MEMORY((expected), (actual), (len), (num_elements), __LINE__, NULL) /* Floating Point (If Enabled) */ #define TEST_ASSERT_FLOAT_WITHIN(delta, expected, actual) UNITY_TEST_ASSERT_FLOAT_WITHIN((delta), (expected), (actual), __LINE__, NULL) @@ -302,7 +302,7 @@ void tearDown(void); #define TEST_ASSERT_EACH_EQUAL_HEX64_MESSAGE(expected, actual, num_elements, message) UNITY_TEST_ASSERT_EACH_EQUAL_HEX64((expected), (actual), (num_elements), __LINE__, (message)) #define TEST_ASSERT_EACH_EQUAL_PTR_MESSAGE(expected, actual, num_elements, message) UNITY_TEST_ASSERT_EACH_EQUAL_PTR((expected), (actual), (num_elements), __LINE__, (message)) //#define TEST_ASSERT_EACH_EQUAL_STRING_MESSAGE(expected, actual, num_elements, message) UNITY_TEST_ASSERT_EACH_EQUAL_STRING((expected), (actual), (num_elements), __LINE__, (message)) -//#define TEST_ASSERT_EACH_EQUAL_MEMORY_MESSAGE(expected, actual, len, num_elements, message) UNITY_TEST_ASSERT_EACH_EQUAL_MEMORY((expected), (actual), (len), (num_elements), __LINE__, (message)) +#define TEST_ASSERT_EACH_EQUAL_MEMORY_MESSAGE(expected, actual, len, num_elements, message) UNITY_TEST_ASSERT_EACH_EQUAL_MEMORY((expected), (actual), (len), (num_elements), __LINE__, (message)) /* Floating Point (If Enabled) */ #define TEST_ASSERT_FLOAT_WITHIN_MESSAGE(delta, expected, actual, message) UNITY_TEST_ASSERT_FLOAT_WITHIN((delta), (expected), (actual), __LINE__, (message)) diff --git a/src/unity_internals.h b/src/unity_internals.h index 7662935..93bbe98 100644 --- a/src/unity_internals.h +++ b/src/unity_internals.h @@ -491,7 +491,8 @@ void UnityAssertEqualMemory( UNITY_INTERNAL_PTR expected, const UNITY_UINT32 length, const UNITY_UINT32 num_elements, const char* msg, - const UNITY_LINE_TYPE lineNumber); + const UNITY_LINE_TYPE lineNumber, + const UNITY_FLAGS_T flags); void UnityAssertNumbersWithin(const UNITY_UINT delta, const UNITY_INT expected, @@ -665,7 +666,7 @@ int UnityTestMatches(void); #define UNITY_TEST_ASSERT_EQUAL_PTR(expected, actual, line, message) UnityAssertEqualNumber((UNITY_PTR_TO_INT)(expected), (UNITY_PTR_TO_INT)(actual), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_POINTER) #define UNITY_TEST_ASSERT_EQUAL_STRING(expected, actual, line, message) UnityAssertEqualString((const char*)(expected), (const char*)(actual), (message), (UNITY_LINE_TYPE)(line)) #define UNITY_TEST_ASSERT_EQUAL_STRING_LEN(expected, actual, len, line, message) UnityAssertEqualStringLen((const char*)(expected), (const char*)(actual), (UNITY_UINT32)(len), (message), (UNITY_LINE_TYPE)(line)) -#define UNITY_TEST_ASSERT_EQUAL_MEMORY(expected, actual, len, line, message) UnityAssertEqualMemory((UNITY_INTERNAL_PTR)(expected), (UNITY_INTERNAL_PTR)(actual), (UNITY_UINT32)(len), 1, (message), (UNITY_LINE_TYPE)(line)) +#define UNITY_TEST_ASSERT_EQUAL_MEMORY(expected, actual, len, line, message) UnityAssertEqualMemory((UNITY_INTERNAL_PTR)(expected), (UNITY_INTERNAL_PTR)(actual), (UNITY_UINT32)(len), 1, (message), (UNITY_LINE_TYPE)(line), UNITY_ARRAY_TO_ARRAY) #define UNITY_TEST_ASSERT_EQUAL_INT_ARRAY(expected, actual, num_elements, line, message) UnityAssertEqualIntArray((UNITY_INTERNAL_PTR)(expected), (UNITY_INTERNAL_PTR)(actual), (UNITY_UINT32)(num_elements), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_INT, UNITY_ARRAY_TO_ARRAY) #define UNITY_TEST_ASSERT_EQUAL_INT8_ARRAY(expected, actual, num_elements, line, message) UnityAssertEqualIntArray((UNITY_INTERNAL_PTR)(expected), (UNITY_INTERNAL_PTR)(actual), (UNITY_UINT32)(num_elements), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_INT8, UNITY_ARRAY_TO_ARRAY) @@ -680,7 +681,7 @@ int UnityTestMatches(void); #define UNITY_TEST_ASSERT_EQUAL_HEX32_ARRAY(expected, actual, num_elements, line, message) UnityAssertEqualIntArray((UNITY_INTERNAL_PTR)(expected), (UNITY_INTERNAL_PTR)(actual), (UNITY_UINT32)(num_elements), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_HEX32, UNITY_ARRAY_TO_ARRAY) #define UNITY_TEST_ASSERT_EQUAL_PTR_ARRAY(expected, actual, num_elements, line, message) UnityAssertEqualIntArray((UNITY_INTERNAL_PTR)(expected), (UNITY_INTERNAL_PTR)(actual), (UNITY_UINT32)(num_elements), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_POINTER, UNITY_ARRAY_TO_ARRAY) #define UNITY_TEST_ASSERT_EQUAL_STRING_ARRAY(expected, actual, num_elements, line, message) UnityAssertEqualStringArray((const char**)(expected), (const char**)(actual), (UNITY_UINT32)(num_elements), (message), (UNITY_LINE_TYPE)(line)) -#define UNITY_TEST_ASSERT_EQUAL_MEMORY_ARRAY(expected, actual, len, num_elements, line, message) UnityAssertEqualMemory((UNITY_INTERNAL_PTR)(expected), (UNITY_INTERNAL_PTR)(actual), (UNITY_UINT32)(len), (UNITY_UINT32)(num_elements), (message), (UNITY_LINE_TYPE)(line)) +#define UNITY_TEST_ASSERT_EQUAL_MEMORY_ARRAY(expected, actual, len, num_elements, line, message) UnityAssertEqualMemory((UNITY_INTERNAL_PTR)(expected), (UNITY_INTERNAL_PTR)(actual), (UNITY_UINT32)(len), (UNITY_UINT32)(num_elements), (message), (UNITY_LINE_TYPE)(line), UNITY_ARRAY_TO_ARRAY) #define UNITY_TEST_ASSERT_EACH_EQUAL_INT(expected, actual, num_elements, line, message) UnityAssertEqualIntArray(UnityNumToPtr((UNITY_INT) expected, sizeof(int)), (UNITY_INTERNAL_PTR)(actual), (UNITY_UINT32)(num_elements), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_INT, UNITY_ARRAY_TO_VAL) #define UNITY_TEST_ASSERT_EACH_EQUAL_INT8(expected, actual, num_elements, line, message) UnityAssertEqualIntArray(UnityNumToPtr((UNITY_INT)(UNITY_INT8 )expected, 1), (UNITY_INTERNAL_PTR)(actual), (UNITY_UINT32)(num_elements), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_INT8, UNITY_ARRAY_TO_VAL) @@ -695,7 +696,7 @@ int UnityTestMatches(void); #define UNITY_TEST_ASSERT_EACH_EQUAL_HEX32(expected, actual, num_elements, line, message) UnityAssertEqualIntArray(UnityNumToPtr((UNITY_INT)(UNITY_INT32 )expected, 4), (UNITY_INTERNAL_PTR)(actual), (UNITY_UINT32)(num_elements), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_HEX32, UNITY_ARRAY_TO_VAL) #define UNITY_TEST_ASSERT_EACH_EQUAL_PTR(expected, actual, num_elements, line, message) UnityAssertEqualIntArray(UnityNumToPtr((UNITY_INT) expected, sizeof(int*)), (UNITY_INTERNAL_PTR)(actual), (UNITY_UINT32)(num_elements), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_POINTER, UNITY_ARRAY_TO_VAL) #define UNITY_TEST_ASSERT_EACH_EQUAL_STRING(expected, actual, num_elements, line, message) UnityAssertEqualStringArray((const char**)(expected), (const char**)(actual), (UNITY_UINT32)(num_elements), (message), (UNITY_LINE_TYPE)(line)) -#define UNITY_TEST_ASSERT_EACH_EQUAL_MEMORY(expected, actual, len, num_elements, line, message) UnityAssertEqualMemory((UNITY_INTERNAL_PTR)(expected), (UNITY_INTERNAL_PTR)(actual), (UNITY_UINT32)(len), (UNITY_UINT32)(num_elements), (message), (UNITY_LINE_TYPE)(line)) +#define UNITY_TEST_ASSERT_EACH_EQUAL_MEMORY(expected, actual, len, num_elements, line, message) UnityAssertEqualMemory((UNITY_INTERNAL_PTR)(expected), (UNITY_INTERNAL_PTR)(actual), (UNITY_UINT32)(len), (UNITY_UINT32)(num_elements), (message), (UNITY_LINE_TYPE)(line), UNITY_ARRAY_TO_VAL) #ifdef UNITY_SUPPORT_64 #define UNITY_TEST_ASSERT_EQUAL_INT64(expected, actual, line, message) UnityAssertEqualNumber((UNITY_INT)(expected), (UNITY_INT)(actual), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_INT64) diff --git a/test/tests/testunity.c b/test/tests/testunity.c index 7f8f1e5..b5d593a 100644 --- a/test/tests/testunity.c +++ b/test/tests/testunity.c @@ -2737,6 +2737,71 @@ void testNotEqualMemoryArrays3(void) VERIFY_FAILS_END } +void testEqualMemoryEachEqual(void) +{ + int p0[] = {1, 8, 987, -2}; + int p1[] = {1, 8, 987, -2, 1, 8, 987, -2}; + int p2[] = {8, 8, 8, 2}; + int p3[] = {8, 500, 600, 700}; + int v = 8; + + TEST_ASSERT_EACH_EQUAL_MEMORY(p0, p0, sizeof(int)*4, 1); + TEST_ASSERT_EACH_EQUAL_MEMORY(p0, p1, sizeof(int)*4, 2); + TEST_ASSERT_EACH_EQUAL_MEMORY(p0, p1, sizeof(int)*4, 1); + TEST_ASSERT_EACH_EQUAL_MEMORY(&v, p2, sizeof(int), 3); + TEST_ASSERT_EACH_EQUAL_MEMORY(&v, p3, sizeof(int), 1); +} + +void testNotEqualMemoryEachEqualExpectedNull(void) +{ + int* p0 = NULL; + int p1[] = {1, 8, 987, 2}; + + EXPECT_ABORT_BEGIN + TEST_ASSERT_EACH_EQUAL_MEMORY(p0, p1, sizeof(int), 4); + VERIFY_FAILS_END +} + +void testNotEqualMemoryEachEqualActualNull(void) +{ + int p0[] = {1, 8, 987, -2}; + int* p1 = NULL; + + EXPECT_ABORT_BEGIN + TEST_ASSERT_EACH_EQUAL_MEMORY(p0, p1, sizeof(int), 4); + VERIFY_FAILS_END +} + +void testNotEqualMemoryEachEqual1(void) +{ + int p0[] = {1, 8, 987, -2}; + int p1[] = {9, 8, 987, -2, 1, 8, 987, -2, 1, 8, 987, -2}; + + EXPECT_ABORT_BEGIN + TEST_ASSERT_EACH_EQUAL_MEMORY(p0, p1, sizeof(int)*4, 3); + VERIFY_FAILS_END +} + +void testNotEqualMemoryEachEqual2(void) +{ + int p0[] = {1, 8, 987, -2}; + int p1[] = {1, 8, 987, -2, 1, 8, 987, -2, 1, 8, 987, 9}; + + EXPECT_ABORT_BEGIN + TEST_ASSERT_EACH_EQUAL_MEMORY(p0, p1, sizeof(int)*4, 3); + VERIFY_FAILS_END +} + +void testNotEqualMemoryEachEqual3(void) +{ + int p0[] = {1, 8, 987, -2}; + int p1[] = {1, 8, 987, -2, 1, 9, 987, -2, 1, 8, 987, -2}; + + EXPECT_ABORT_BEGIN + TEST_ASSERT_EACH_EQUAL_MEMORY(p0, p1, sizeof(int)*4, 3); + VERIFY_FAILS_END +} + void testProtection(void) { volatile int mask = 0; -- GitLab