diff --git a/src/unity.c b/src/unity.c index f4fe99b96e41c52da4c4803709ba9e6ad39c428f..f7cae0a7a8ca6dab336a4dd1c083b64548c2f87f 100644 --- a/src/unity.c +++ b/src/unity.c @@ -1016,13 +1016,17 @@ void UnityAssertEqualStringLen(const char* expected, } /*-----------------------------------------------*/ -void UnityAssertEqualStringArray(const char** expected, +void UnityAssertEqualStringArray(UNITY_INTERNAL_PTR expected, const char** actual, const UNITY_UINT32 num_elements, const char* msg, - const UNITY_LINE_TYPE lineNumber) + const UNITY_LINE_TYPE lineNumber, + const UNITY_FLAGS_T flags) { - UNITY_UINT32 i, j = 0; + UNITY_UINT32 i = 0; + UNITY_UINT32 j = 0; + const char* exp; + const char* act; RETURN_IF_FAIL_OR_IGNORE; @@ -1032,18 +1036,35 @@ void UnityAssertEqualStringArray(const char** expected, UnityPrintPointlessAndBail(); } - if (expected == actual) return; /* Both are NULL or same pointer */ + if ((const char**)expected == actual) + { + return; /* Both are NULL or same pointer */ + } + if (UnityIsOneArrayNull((UNITY_INTERNAL_PTR)expected, (UNITY_INTERNAL_PTR)actual, lineNumber, msg)) + { UNITY_FAIL_AND_BAIL; + } + + if (flags != UNITY_ARRAY_TO_ARRAY) + { + exp = (const char*)expected; + } do { + act = actual[j]; + if (flags == UNITY_ARRAY_TO_ARRAY) + { + exp = ((const char**)expected)[j]; + } + /* if both pointers not null compare the strings */ - if (expected[j] && actual[j]) + if (exp && act) { - for (i = 0; expected[j][i] || actual[j][i]; i++) + for (i = 0; exp[i] || act[i]; i++) { - if (expected[j][i] != actual[j][i]) + if (exp[i] != act[i]) { Unity.CurrentTestFailed = 1; break; @@ -1052,7 +1073,7 @@ void UnityAssertEqualStringArray(const char** expected, } else { /* handle case of one pointers being null (if both null, test should pass) */ - if (expected[j] != actual[j]) + if (exp != act) { Unity.CurrentTestFailed = 1; } @@ -1066,7 +1087,7 @@ void UnityAssertEqualStringArray(const char** expected, UnityPrint(UnityStrElement); UnityPrintNumberUnsigned(j); } - UnityPrintExpectedAndActualStrings((const char*)(expected[j]), (const char*)(actual[j])); + UnityPrintExpectedAndActualStrings(exp, act); UnityAddMsgIfSpecified(msg); UNITY_FAIL_AND_BAIL; } diff --git a/src/unity.h b/src/unity.h index 19e32edbbfc44cb877cd9ad0af3d62277c5a8516..643423839ff11e1a5251449eaec5fbabce7395db 100644 --- a/src/unity.h +++ b/src/unity.h @@ -174,7 +174,7 @@ void tearDown(void); #define TEST_ASSERT_EACH_EQUAL_HEX32(expected, actual, num_elements) UNITY_TEST_ASSERT_EACH_EQUAL_HEX32((expected), (actual), (num_elements), __LINE__, NULL) #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_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) /* Floating Point (If Enabled) */ @@ -301,7 +301,7 @@ void tearDown(void); #define TEST_ASSERT_EACH_EQUAL_HEX32_MESSAGE(expected, actual, num_elements, message) UNITY_TEST_ASSERT_EACH_EQUAL_HEX32((expected), (actual), (num_elements), __LINE__, (message)) #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_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)) /* Floating Point (If Enabled) */ diff --git a/src/unity_internals.h b/src/unity_internals.h index 93bbe98cfd0cb4d132d8b794b84d98a64fd044e4..b213ee971129793128754fc23e5268e2bfcc432f 100644 --- a/src/unity_internals.h +++ b/src/unity_internals.h @@ -480,11 +480,12 @@ void UnityAssertEqualStringLen(const char* expected, const char* msg, const UNITY_LINE_TYPE lineNumber); -void UnityAssertEqualStringArray( const char** expected, +void UnityAssertEqualStringArray( UNITY_INTERNAL_PTR expected, const char** actual, const UNITY_UINT32 num_elements, const char* msg, - const UNITY_LINE_TYPE lineNumber); + const UNITY_LINE_TYPE lineNumber, + const UNITY_FLAGS_T flags); void UnityAssertEqualMemory( UNITY_INTERNAL_PTR expected, UNITY_INTERNAL_PTR actual, @@ -680,7 +681,7 @@ int UnityTestMatches(void); #define UNITY_TEST_ASSERT_EQUAL_HEX16_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_HEX16, UNITY_ARRAY_TO_ARRAY) #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_STRING_ARRAY(expected, actual, num_elements, line, message) UnityAssertEqualStringArray((UNITY_INTERNAL_PTR)(expected), (const char**)(actual), (UNITY_UINT32)(num_elements), (message), (UNITY_LINE_TYPE)(line), UNITY_ARRAY_TO_ARRAY) #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) @@ -695,7 +696,7 @@ int UnityTestMatches(void); #define UNITY_TEST_ASSERT_EACH_EQUAL_HEX16(expected, actual, num_elements, line, message) UnityAssertEqualIntArray(UnityNumToPtr((UNITY_INT)(UNITY_INT16 )expected, 2), (UNITY_INTERNAL_PTR)(actual), (UNITY_UINT32)(num_elements), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_HEX16, UNITY_ARRAY_TO_VAL) #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_STRING(expected, actual, num_elements, line, message) UnityAssertEqualStringArray((UNITY_INTERNAL_PTR)(expected), (const char**)(actual), (UNITY_UINT32)(num_elements), (message), (UNITY_LINE_TYPE)(line), UNITY_ARRAY_TO_VAL) #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 diff --git a/test/tests/testunity.c b/test/tests/testunity.c index b5d593aad0ca8a535b8f99ea09d6af8831588031..3555fb2d679aced1883dfa79685eb53e29e39019 100644 --- a/test/tests/testunity.c +++ b/test/tests/testunity.c @@ -1519,6 +1519,61 @@ void testNotEqualStringArrayLengthZero(void) VERIFY_FAILS_END } +void testEqualStringEachEqual(void) +{ + const char *testStrings1[] = { "foo", "foo", "foo", "foo" }; + const char *testStrings2[] = { "boo", "boo", "boo", "zoo" }; + const char *testStrings3[] = { "", "", "", "" }; + + TEST_ASSERT_EACH_EQUAL_STRING("foo", testStrings1, 4); + TEST_ASSERT_EACH_EQUAL_STRING("foo", testStrings1, 1); + TEST_ASSERT_EACH_EQUAL_STRING("boo", testStrings2, 3); + TEST_ASSERT_EACH_EQUAL_STRING("", testStrings3, 4); +} + +void testNotEqualStringEachEqual1(void) +{ + const char *testStrings[] = { "foo", "foo", "foo", "moo" }; + + EXPECT_ABORT_BEGIN + TEST_ASSERT_EACH_EQUAL_STRING("foo", testStrings, 4); + VERIFY_FAILS_END +} + +void testNotEqualStringEachEqual2(void) +{ + const char *testStrings[] = { "boo", "foo", "foo", "foo" }; + + EXPECT_ABORT_BEGIN + TEST_ASSERT_EACH_EQUAL_STRING("foo", testStrings, 4); + VERIFY_FAILS_END +} + +void testNotEqualStringEachEqual3(void) +{ + const char *testStrings[] = { "foo", "foo", "foo", NULL }; + + EXPECT_ABORT_BEGIN + TEST_ASSERT_EACH_EQUAL_STRING("foo", testStrings, 4); + VERIFY_FAILS_END +} + +void testNotEqualStringEachEqual4(void) +{ + const char *testStrings[] = { "foo", "foo", "woo", "foo" }; + + EXPECT_ABORT_BEGIN + TEST_ASSERT_EACH_EQUAL_STRING("foo", testStrings, 4); + VERIFY_FAILS_END +} + +void testNotEqualStringEachEqual5(void) +{ + EXPECT_ABORT_BEGIN + TEST_ASSERT_EACH_EQUAL_STRING("foo", NULL, 1); + VERIFY_FAILS_END +} + void testEqualMemory(void) { const char *testString = "whatever";