From 61c0b0b75ce95504db1175af8c3cefa6356d4d72 Mon Sep 17 00:00:00 2001 From: Eivind Tagseth Date: Thu, 23 Apr 2015 15:39:19 +0200 Subject: [PATCH] Added support for TEST_ASSERT_EQUAL_STRING_LEN* Compares two strings until maximum n bytes (i.e. strncmp()). --- README.md | 8 ++++ docs/Unity Summary.txt | 8 ++++ src/unity.c | 106 +++++++++++++++++++++++++++++++++++++++++ src/unity.h | 3 +- src/unity_internals.h | 7 +++ test/tests/testunity.c | 38 +++++++++++++++ 6 files changed, 169 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 61d0a62..5f98714 100644 --- a/README.md +++ b/README.md @@ -169,10 +169,18 @@ String Assertions Compare two null-terminate strings. Fail if any character is different or if the lengths are different. + TEST_ASSERT_EQUAL_STRING_LEN(expected, actual, len) + +Compare two strings. Fail if any character is different, stop comparing after len characters. + TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, actual, message) Compare two null-terminate strings. Fail if any character is different or if the lengths are different. Output a custom message on failure. + TEST_ASSERT_EQUAL_STRING_LEN_MESSAGE(expected, actual, len, message) + +Compare two strings. Fail if any character is different, stop comparing after len characters. Output a custom message on failure. + Pointer Assertions ------------------ diff --git a/docs/Unity Summary.txt b/docs/Unity Summary.txt index a157aac..fa83e51 100644 --- a/docs/Unity Summary.txt +++ b/docs/Unity Summary.txt @@ -178,10 +178,18 @@ TEST_ASSERT_EQUAL_STRING(expected, actual) Compare two null-terminate strings. Fail if any character is different or if the lengths are different. +TEST_ASSERT_EQUAL_STRING_LEN(expected, actual, len) + +Compare two strings. Fail if any character is different, stop comparing after len characters. + TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, actual, message) Compare two null-terminate strings. Fail if any character is different or if the lengths are different. Output a custom message on failure. +TEST_ASSERT_EQUAL_STRING_LEN_MESSAGE(expected, actual, len, message) + +Compare two strings. Fail if any character is different, stop comparing after len characters. Output a custom message on failure. + ------------------ Pointer Assertions ------------------ diff --git a/src/unity.c b/src/unity.c index d6fd0a1..842ca9c 100644 --- a/src/unity.c +++ b/src/unity.c @@ -110,6 +110,42 @@ void UnityPrint(const char* string) } } +void UnityPrintLen(const char* string, const _UU32 length) +{ + const char* pch = string; + + if (pch != NULL) + { + while (*pch && (pch - string) < length) + { + // printable characters plus CR & LF are printed + if ((*pch <= 126) && (*pch >= 32)) + { + UNITY_OUTPUT_CHAR(*pch); + } + //write escaped carriage returns + else if (*pch == 13) + { + UNITY_OUTPUT_CHAR('\\'); + UNITY_OUTPUT_CHAR('r'); + } + //write escaped line feeds + else if (*pch == 10) + { + UNITY_OUTPUT_CHAR('\\'); + UNITY_OUTPUT_CHAR('n'); + } + // unprintable characters are shown as codes + else + { + UNITY_OUTPUT_CHAR('\\'); + UnityPrintNumberHex((_U_UINT)*pch, 2); + } + pch++; + } + } +} + //----------------------------------------------- void UnityPrintNumberByStyle(const _U_SINT number, const UNITY_DISPLAY_STYLE_T style) { @@ -348,6 +384,35 @@ static void UnityPrintExpectedAndActualStrings(const char* expected, const char* } } +//----------------------------------------------- +static void UnityPrintExpectedAndActualStringsLen(const char* expected, const char* actual, const _UU32 length) +{ + UnityPrint(UnityStrExpected); + if (expected != NULL) + { + UNITY_OUTPUT_CHAR('\''); + UnityPrintLen(expected, length); + UNITY_OUTPUT_CHAR('\''); + } + else + { + UnityPrint(UnityStrNull); + } + UnityPrint(UnityStrWas); + if (actual != NULL) + { + UNITY_OUTPUT_CHAR('\''); + UnityPrintLen(actual, length); + UNITY_OUTPUT_CHAR('\''); + } + else + { + UnityPrint(UnityStrNull); + } +} + + + //----------------------------------------------- // Assertion & Control Helpers //----------------------------------------------- @@ -944,6 +1009,47 @@ void UnityAssertEqualString(const char* expected, } } +//----------------------------------------------- +void UnityAssertEqualStringLen(const char* expected, + const char* actual, + const _UU32 length, + const char* msg, + const UNITY_LINE_TYPE lineNumber) +{ + _UU32 i; + + UNITY_SKIP_EXECUTION; + + // if both pointers not null compare the strings + if (expected && actual) + { + for (i = 0; (expected[i] || actual[i]) && i < length; i++) + { + if (expected[i] != actual[i]) + { + Unity.CurrentTestFailed = 1; + break; + } + } + } + else + { // handle case of one pointers being null (if both null, test should pass) + if (expected != actual) + { + Unity.CurrentTestFailed = 1; + } + } + + if (Unity.CurrentTestFailed) + { + UnityTestResultsFailBegin(lineNumber); + UnityPrintExpectedAndActualStringsLen(expected, actual, length); + UnityAddMsgIfSpecified(msg); + UNITY_FAIL_AND_BAIL; + } +} + + //----------------------------------------------- void UnityAssertEqualStringArray( const char** expected, const char** actual, diff --git a/src/unity.h b/src/unity.h index 80e6126..c9b8ea5 100644 --- a/src/unity.h +++ b/src/unity.h @@ -116,6 +116,7 @@ //Structs and Strings #define TEST_ASSERT_EQUAL_PTR(expected, actual) UNITY_TEST_ASSERT_EQUAL_PTR((expected), (actual), __LINE__, NULL) #define TEST_ASSERT_EQUAL_STRING(expected, actual) UNITY_TEST_ASSERT_EQUAL_STRING(expected, actual, __LINE__, NULL) +#define TEST_ASSERT_EQUAL_STRING_LEN(expected, actual, len) UNITY_TEST_ASSERT_EQUAL_STRING_LEN(expected, actual, len, __LINE__, NULL) #define TEST_ASSERT_EQUAL_MEMORY(expected, actual, len) UNITY_TEST_ASSERT_EQUAL_MEMORY(expected, actual, len, __LINE__, NULL) //Arrays @@ -219,7 +220,7 @@ //Structs and Strings #define TEST_ASSERT_EQUAL_PTR_MESSAGE(expected, actual, message) UNITY_TEST_ASSERT_EQUAL_PTR(expected, actual, __LINE__, message) -#define TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, actual, message) UNITY_TEST_ASSERT_EQUAL_STRING(expected, actual, __LINE__, message) +#define TEST_ASSERT_EQUAL_STRING_LEN_MESSAGE(expected, actual, len, message) UNITY_TEST_ASSERT_EQUAL_STRING_LEN(expected, actual, len, __LINE__, message) #define TEST_ASSERT_EQUAL_MEMORY_MESSAGE(expected, actual, len, message) UNITY_TEST_ASSERT_EQUAL_MEMORY(expected, actual, len, __LINE__, message) //Arrays diff --git a/src/unity_internals.h b/src/unity_internals.h index 1552915..82e3381 100644 --- a/src/unity_internals.h +++ b/src/unity_internals.h @@ -440,6 +440,12 @@ void UnityAssertEqualString(const char* expected, const char* msg, const UNITY_LINE_TYPE lineNumber); +void UnityAssertEqualStringLen(const char* expected, + const char* actual, + const _UU32 length, + const char* msg, + const UNITY_LINE_TYPE lineNumber); + void UnityAssertEqualStringArray( const char** expected, const char** actual, const _UU32 num_elements, @@ -597,6 +603,7 @@ extern const char UnityStrErr64[]; #define UNITY_TEST_ASSERT_EQUAL_PTR(expected, actual, line, message) UnityAssertEqualNumber((_U_SINT)(_UP)(expected), (_U_SINT)(_UP)(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), (_UU32)(len), (message), (UNITY_LINE_TYPE)line) #define UNITY_TEST_ASSERT_EQUAL_MEMORY(expected, actual, len, line, message) UnityAssertEqualMemory((UNITY_PTR_ATTRIBUTE void*)(expected), (UNITY_PTR_ATTRIBUTE void*)(actual), (_UU32)(len), 1, (message), (UNITY_LINE_TYPE)line) #define UNITY_TEST_ASSERT_EQUAL_INT_ARRAY(expected, actual, num_elements, line, message) UnityAssertEqualIntArray((UNITY_PTR_ATTRIBUTE const void*)(expected), (UNITY_PTR_ATTRIBUTE const void*)(actual), (_UU32)(num_elements), (message), (UNITY_LINE_TYPE)line, UNITY_DISPLAY_STYLE_INT) diff --git a/test/tests/testunity.c b/test/tests/testunity.c index a701c92..f239056 100755 --- a/test/tests/testunity.c +++ b/test/tests/testunity.c @@ -1265,6 +1265,16 @@ void testEqualStrings(void) TEST_ASSERT_EQUAL_STRING("", ""); } +void testEqualStringsLen(void) +{ + const char *testString = "foobar"; + TEST_ASSERT_EQUAL_STRING_LEN(testString, testString, strlen(testString)); + TEST_ASSERT_EQUAL_STRING_LEN("foobar", "foobaz", 5); + TEST_ASSERT_EQUAL_STRING_LEN("foo", testString, 3); + TEST_ASSERT_EQUAL_STRING_LEN(testString, foo, 3); + TEST_ASSERT_EQUAL_STRING_LEN("", "", 3); +} + void testEqualStringsWithCarriageReturnsAndLineFeeds(void) { const char *testString = "foo\r\nbar"; @@ -1283,6 +1293,13 @@ void testNotEqualString1(void) VERIFY_FAILS_END } +void testNotEqualStringLen1(void) +{ + EXPECT_ABORT_BEGIN + TEST_ASSERT_EQUAL_STRING_LEN("foobar", "foobaz", 6); + VERIFY_FAILS_END +} + void testNotEqualString2(void) { EXPECT_ABORT_BEGIN @@ -1290,6 +1307,13 @@ void testNotEqualString2(void) VERIFY_FAILS_END } +void testNotEqualStringLen2(void) +{ + EXPECT_ABORT_BEGIN + TEST_ASSERT_EQUAL_STRING_LEN("foo", "", 3); + VERIFY_FAILS_END +} + void testNotEqualString3(void) { EXPECT_ABORT_BEGIN @@ -1297,6 +1321,13 @@ void testNotEqualString3(void) VERIFY_FAILS_END } +void testNotEqualStringLen3(void) +{ + EXPECT_ABORT_BEGIN + TEST_ASSERT_EQUAL_STRING_LEN("", "bar", 3); + VERIFY_FAILS_END +} + void testNotEqualString4(void) { EXPECT_ABORT_BEGIN @@ -1304,6 +1335,13 @@ void testNotEqualString4(void) VERIFY_FAILS_END } +void testNotEqualStringLen4(void) +{ + EXPECT_ABORT_BEGIN + TEST_ASSERT_EQUAL_STRING_LEN("bar\r", "bar\n", 4); + VERIFY_FAILS_END +} + void testNotEqualString5(void) { const char str1[] = { 0x41, 0x42, 0x03, 0x00 };