From 67a37a4db7ed6b97500be4c9f6a842de05de8568 Mon Sep 17 00:00:00 2001 From: jsalling Date: Mon, 2 May 2016 23:50:28 -0500 Subject: [PATCH 1/9] Refactor UnityPrintNumber(), remove unreachable code Remove what looks like an overflow check. 'divisor' cannot overflow since it is the same type as 'number' and cannot be greater. --- src/unity.c | 36 +++--------------------------------- 1 file changed, 3 insertions(+), 33 deletions(-) diff --git a/src/unity.c b/src/unity.c index f46600d..090d5b8 100644 --- a/src/unity.c +++ b/src/unity.c @@ -164,12 +164,9 @@ void UnityPrintNumberByStyle(const _U_SINT number, const UNITY_DISPLAY_STYLE_T s } //----------------------------------------------- -/// basically do an itoa using as little ram as possible void UnityPrintNumber(const _U_SINT number_to_print) { - _U_UINT divisor = 1; - _U_UINT next_divisor; - _U_UINT number; + _U_UINT number = (_U_UINT)number_to_print; if (number_to_print < 0) { @@ -177,29 +174,7 @@ void UnityPrintNumber(const _U_SINT number_to_print) UNITY_OUTPUT_CHAR('-'); number = (_U_UINT)(-number_to_print); } - else - { - //Non-negative number - number = (_U_UINT)number_to_print; - } - - // figure out initial divisor - while (number / divisor > 9) - { - next_divisor = divisor * 10; - if (next_divisor > divisor) - divisor = next_divisor; - else - break; - } - - // now mod and print, then divide divisor - do - { - UNITY_OUTPUT_CHAR((char)('0' + (number / divisor % 10))); - divisor /= 10; - } - while (divisor > 0); + UnityPrintNumberUnsigned(number); } //----------------------------------------------- @@ -207,16 +182,11 @@ void UnityPrintNumber(const _U_SINT number_to_print) void UnityPrintNumberUnsigned(const _U_UINT number) { _U_UINT divisor = 1; - _U_UINT next_divisor; // figure out initial divisor while (number / divisor > 9) { - next_divisor = divisor * 10; - if (next_divisor > divisor) - divisor = next_divisor; - else - break; + divisor *= 10; } // now mod and print, then divide divisor From b971ec921fe72977fe1ff55b2af497419b2e9912 Mon Sep 17 00:00:00 2001 From: jsalling Date: Tue, 3 May 2016 19:52:32 -0500 Subject: [PATCH 2/9] Simplify printing StrPointless into a single macro, remove repeated code --- src/unity.c | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/src/unity.c b/src/unity.c index 090d5b8..d8fb7aa 100644 --- a/src/unity.c +++ b/src/unity.c @@ -491,6 +491,13 @@ void UnityAssertEqualNumber(const _U_SINT expected, } } +#define UnityPrintPointlessAndBail() \ +{ \ + UnityTestResultsFailBegin(lineNumber); \ + UnityPrint(UnityStrPointless); \ + UnityAddMsgIfSpecified(msg); \ + UNITY_FAIL_AND_BAIL; } + //----------------------------------------------- void UnityAssertEqualIntArray(UNITY_INTERNAL_PTR expected, UNITY_INTERNAL_PTR actual, @@ -507,10 +514,7 @@ void UnityAssertEqualIntArray(UNITY_INTERNAL_PTR expected, if (elements == 0) { - UnityTestResultsFailBegin(lineNumber); - UnityPrint(UnityStrPointless); - UnityAddMsgIfSpecified(msg); - UNITY_FAIL_AND_BAIL; + UnityPrintPointlessAndBail(); } if (UnityCheckArraysForNull((UNITY_INTERNAL_PTR)expected, (UNITY_INTERNAL_PTR)actual, lineNumber, msg) == 1) @@ -625,10 +629,7 @@ void UnityAssertEqualFloatArray(UNITY_PTR_ATTRIBUTE const _UF* expected, if (elements == 0) { - UnityTestResultsFailBegin(lineNumber); - UnityPrint(UnityStrPointless); - UnityAddMsgIfSpecified(msg); - UNITY_FAIL_AND_BAIL; + UnityPrintPointlessAndBail(); } if (UnityCheckArraysForNull((UNITY_INTERNAL_PTR)expected, (UNITY_INTERNAL_PTR)actual, lineNumber, msg) == 1) @@ -789,10 +790,7 @@ void UnityAssertEqualDoubleArray(UNITY_PTR_ATTRIBUTE const _UD* expected, if (elements == 0) { - UnityTestResultsFailBegin(lineNumber); - UnityPrint(UnityStrPointless); - UnityAddMsgIfSpecified(msg); - UNITY_FAIL_AND_BAIL; + UnityPrintPointlessAndBail(); } if (UnityCheckArraysForNull((UNITY_INTERNAL_PTR)expected, (UNITY_INTERNAL_PTR)actual, lineNumber, msg) == 1) @@ -1071,10 +1069,7 @@ void UnityAssertEqualStringArray( const char** expected, // if no elements, it's an error if (num_elements == 0) { - UnityTestResultsFailBegin(lineNumber); - UnityPrint(UnityStrPointless); - UnityAddMsgIfSpecified(msg); - UNITY_FAIL_AND_BAIL; + UnityPrintPointlessAndBail(); } if (UnityCheckArraysForNull((UNITY_INTERNAL_PTR)expected, (UNITY_INTERNAL_PTR)actual, lineNumber, msg) == 1) @@ -1134,10 +1129,7 @@ void UnityAssertEqualMemory( UNITY_INTERNAL_PTR expected, if ((elements == 0) || (length == 0)) { - UnityTestResultsFailBegin(lineNumber); - UnityPrint(UnityStrPointless); - UnityAddMsgIfSpecified(msg); - UNITY_FAIL_AND_BAIL; + UnityPrintPointlessAndBail(); } if (UnityCheckArraysForNull((UNITY_INTERNAL_PTR)expected, (UNITY_INTERNAL_PTR)actual, lineNumber, msg) == 1) From 07513dd331bf0acc9ad70783af7eb79acf316da0 Mon Sep 17 00:00:00 2001 From: jsalling Date: Tue, 3 May 2016 20:02:01 -0500 Subject: [PATCH 3/9] Remove weak linked setup/teardown from coverage build, unreachable & empty Add config flag for 64-bit, max covered lines even with 32-bit compilers Always print uncovered lines, delete separate make target --- test/Makefile | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/test/Makefile b/test/Makefile index 9d30e0d..b0c4d2c 100644 --- a/test/Makefile +++ b/test/Makefile @@ -5,7 +5,8 @@ endif #DEBUG = -O0 -g CFLAGS += -std=c99 -pedantic -Wall -Wextra -Werror -Wredundant-decls CFLAGS += $(DEBUG) -DEFINES = -D UNITY_OUTPUT_CHAR=putcharSpy -D UNITY_INCLUDE_DOUBLE +DEFINES = -D UNITY_OUTPUT_CHAR=putcharSpy +DEFINES += -D UNITY_SUPPORT_64 -D UNITY_INCLUDE_DOUBLE -D UNITY_NO_WEAK SRC = ../src/unity.c tests/testunity.c build/testunityRunner.c INC_DIR = -I ../src COV_FLAGS = -fprofile-arcs -ftest-coverage -I ../../src @@ -24,8 +25,6 @@ coverage: $(BUILD_DIR)/testunityRunner.c ./$(TARGET) | grep Tests -A1 cd $(BUILD_DIR) && \ gcov unity.c | head -3 - -uncovered: grep '###' $(BUILD_DIR)/unity.c.gcov -C2 || true test: CFLAGS += -Wbad-function-cast -Wcast-qual -Wconversion -Wformat=2 -Wold-style-definition \ From a6748f39cd8326a24aa80702e27cf9402ba1708c Mon Sep 17 00:00:00 2001 From: jsalling Date: Tue, 3 May 2016 20:14:30 -0500 Subject: [PATCH 4/9] Test number arrays comparison with length 0, it should fail Get test coverage on this feature for int, float, & double --- test/tests/testunity.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/test/tests/testunity.c b/test/tests/testunity.c index 32c486c..bd612db 100644 --- a/test/tests/testunity.c +++ b/test/tests/testunity.c @@ -1580,6 +1580,16 @@ void testNotEqualIntArrays3(void) VERIFY_FAILS_END } +void testNotEqualIntArraysLengthZero(void) +{ + _UU32 p0[1] = {1}; + _UU32 p1[1] = {1}; + + EXPECT_ABORT_BEGIN + TEST_ASSERT_EQUAL_INT_ARRAY(p0, p1, 0); + VERIFY_FAILS_END +} + void testEqualPtrArrays(void) { char A = 1; @@ -3148,6 +3158,20 @@ void testNotEqualFloatArraysInf(void) #endif } +void testNotEqualFloatArraysLengthZero(void) +{ +#ifdef UNITY_EXCLUDE_FLOAT + TEST_IGNORE(); +#else + float p0[1] = {0.0f}; + float p1[1] = {0.0f}; + + EXPECT_ABORT_BEGIN + TEST_ASSERT_EQUAL_FLOAT_ARRAY(p0, p1, 0); + VERIFY_FAILS_END +#endif +} + // ===================== THESE TEST WILL RUN IF YOUR CONFIG INCLUDES DOUBLE SUPPORT ================== void testDoublesWithinDelta(void) @@ -3664,6 +3688,20 @@ void testNotEqualDoubleArraysInf(void) #endif } +void testNotEqualDoubleArraysLengthZero(void) +{ +#ifdef UNITY_EXCLUDE_DOUBLE + TEST_IGNORE(); +#else + double p0[1] = {0.0}; + double p1[1] = {0.0}; + + EXPECT_ABORT_BEGIN + TEST_ASSERT_EQUAL_DOUBLE_ARRAY(p0, p1, 0); + VERIFY_FAILS_END +#endif +} + void testThatDetailsCanBeHandleOneDetail(void) { #ifdef UNITY_EXCLUDE_DETAILS From 213e47178ca9292d398db58a2b4842b525ad143a Mon Sep 17 00:00:00 2001 From: jsalling Date: Tue, 3 May 2016 20:17:38 -0500 Subject: [PATCH 5/9] Test string arrays and memory comparison with length 0, it should fail --- test/tests/testunity.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/test/tests/testunity.c b/test/tests/testunity.c index bd612db..90b842f 100644 --- a/test/tests/testunity.c +++ b/test/tests/testunity.c @@ -1476,6 +1476,16 @@ void testEqualStringArrayIfBothNulls(void) TEST_ASSERT_EQUAL_STRING_ARRAY(expStrings, testStrings, 4); } +void testNotEqualStringArrayLengthZero(void) +{ + const char *testStrings[] = {NULL}; + const char **expStrings = NULL; + + EXPECT_ABORT_BEGIN + TEST_ASSERT_EQUAL_STRING_ARRAY(expStrings, testStrings, 0); + VERIFY_FAILS_END +} + void testEqualMemory(void) { const char *testString = "whatever"; @@ -1516,6 +1526,13 @@ void testNotEqualMemory4(void) VERIFY_FAILS_END } +void testNotEqualMemoryLengthZero(void) +{ + EXPECT_ABORT_BEGIN + TEST_ASSERT_EQUAL_MEMORY(NULL, NULL, 0); + VERIFY_FAILS_END +} + void testEqualIntArrays(void) { int p0[] = {1, 8, 987, -2}; From 4164540333b23195f21a598fd6af982c8974245c Mon Sep 17 00:00:00 2001 From: jsalling Date: Tue, 3 May 2016 20:26:42 -0500 Subject: [PATCH 6/9] Test number arrays for two NULL inputs, it should pass More statement coverage on NULL input feature --- test/tests/testunity.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/tests/testunity.c b/test/tests/testunity.c index 90b842f..850472b 100644 --- a/test/tests/testunity.c +++ b/test/tests/testunity.c @@ -1545,6 +1545,7 @@ void testEqualIntArrays(void) TEST_ASSERT_EQUAL_INT_ARRAY(p0, p1, 4); TEST_ASSERT_EQUAL_INT_ARRAY(p0, p2, 3); TEST_ASSERT_EQUAL_INT_ARRAY(p0, p3, 1); + TEST_ASSERT_EQUAL_INT_ARRAY(NULL, NULL, 1); } void testNotEqualIntArraysNullExpected(void) @@ -3032,6 +3033,7 @@ void testEqualFloatArrays(void) TEST_ASSERT_EQUAL_FLOAT_ARRAY(p0, p1, 4); TEST_ASSERT_EQUAL_FLOAT_ARRAY(p0, p2, 3); TEST_ASSERT_EQUAL_FLOAT_ARRAY(p0, p3, 1); + TEST_ASSERT_EQUAL_FLOAT_ARRAY(NULL, NULL, 1); #endif } @@ -3562,6 +3564,7 @@ void testEqualDoubleArrays(void) TEST_ASSERT_EQUAL_DOUBLE_ARRAY(p0, p1, 4); TEST_ASSERT_EQUAL_DOUBLE_ARRAY(p0, p2, 3); TEST_ASSERT_EQUAL_DOUBLE_ARRAY(p0, p3, 1); + TEST_ASSERT_EQUAL_DOUBLE_ARRAY(NULL, NULL, 1); #endif } From 7c270e05417e0a50e6ed530cb0304dfa427ffd74 Mon Sep 17 00:00:00 2001 From: jsalling Date: Tue, 3 May 2016 20:31:37 -0500 Subject: [PATCH 7/9] Test that details are printed in a direct TEST_FAIL call --- test/tests/testunity.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test/tests/testunity.c b/test/tests/testunity.c index 850472b..eb6601a 100644 --- a/test/tests/testunity.c +++ b/test/tests/testunity.c @@ -3722,6 +3722,8 @@ void testNotEqualDoubleArraysLengthZero(void) #endif } +// ===================== THESE TEST WILL RUN IF YOUR CONFIG INCLUDES DETAIL SUPPORT ================== + void testThatDetailsCanBeHandleOneDetail(void) { #ifdef UNITY_EXCLUDE_DETAILS @@ -3735,6 +3737,19 @@ void testThatDetailsCanBeHandleOneDetail(void) #endif } +void testThatDetailsCanHandleTestFail(void) +{ +#ifdef UNITY_EXCLUDE_DETAILS + TEST_IGNORE(); +#else + UNITY_SET_DETAILS("Detail1","Detail2"); + + EXPECT_ABORT_BEGIN + TEST_FAIL_MESSAGE("Should Fail And Say Detail1 and Detail2"); + VERIFY_FAILS_END +#endif +} + void testThatDetailsCanBeHandleTwoDetails(void) { #ifdef UNITY_EXCLUDE_DETAILS From bff491c73c1c0765edb2164dee313e7b3619a4cc Mon Sep 17 00:00:00 2001 From: jsalling Date: Tue, 3 May 2016 20:47:00 -0500 Subject: [PATCH 8/9] Test strings not equal to NULL when using length parameter --- test/tests/testunity.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/tests/testunity.c b/test/tests/testunity.c index eb6601a..f1316b9 100644 --- a/test/tests/testunity.c +++ b/test/tests/testunity.c @@ -1390,6 +1390,13 @@ void testNotEqualString_ExpectedStringIsNull(void) VERIFY_FAILS_END } +void testNotEqualStringLen_ExpectedStringIsNull(void) +{ + EXPECT_ABORT_BEGIN + TEST_ASSERT_EQUAL_STRING_LEN(NULL, "bar", 1); + VERIFY_FAILS_END +} + void testNotEqualString_ActualStringIsNull(void) { EXPECT_ABORT_BEGIN @@ -1397,6 +1404,13 @@ void testNotEqualString_ActualStringIsNull(void) VERIFY_FAILS_END } +void testNotEqualStringLen_ActualStringIsNull(void) +{ + EXPECT_ABORT_BEGIN + TEST_ASSERT_EQUAL_STRING_LEN("foo", NULL, 1); + VERIFY_FAILS_END +} + void testEqualStringArrays(void) { const char *testStrings[] = { "foo", "boo", "woo", "moo" }; From c5238adab25ea40c5fe81753ecb66d12419843aa Mon Sep 17 00:00:00 2001 From: jsalling Date: Tue, 3 May 2016 21:58:29 -0500 Subject: [PATCH 9/9] 100% code coverage for Unity project Add test for failure count and UnityEnd return value Cover printing escape codes with length parameter Full statement coverage --- src/unity.h | 4 ++-- test/tests/testunity.c | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/unity.h b/src/unity.h index 9b3d97b..c74c073 100644 --- a/src/unity.h +++ b/src/unity.h @@ -235,8 +235,8 @@ void tearDown(void); //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)) +#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 #define TEST_ASSERT_EQUAL_INT_ARRAY_MESSAGE(expected, actual, num_elements, message) UNITY_TEST_ASSERT_EQUAL_INT_ARRAY((expected), (actual), (num_elements), __LINE__, (message)) diff --git a/test/tests/testunity.c b/test/tests/testunity.c index f1316b9..e0872cc 100644 --- a/test/tests/testunity.c +++ b/test/tests/testunity.c @@ -1370,7 +1370,7 @@ void testNotEqualString4(void) void testNotEqualStringLen4(void) { EXPECT_ABORT_BEGIN - TEST_ASSERT_EQUAL_STRING_LEN("bar\r", "bar\n", 4); + TEST_ASSERT_EQUAL_STRING_LEN("\r\x16", "bar\n", 4); VERIFY_FAILS_END } @@ -2260,6 +2260,19 @@ int putcharSpy(int c) return c; } +void testFailureCountIncrementsAndIsReturnedAtEnd(void) +{ + Unity.CurrentTestFailed = 1; + startPutcharSpy(); // Suppress output + UnityConcludeTest(); + TEST_ASSERT_EQUAL(1, Unity.TestFailures); + + int failures = UnityEnd(); + Unity.TestFailures--; + endPutcharSpy(); + TEST_ASSERT_EQUAL(1, failures); +} + #define TEST_ASSERT_EQUAL_PRINT_NUMBERS(expected, actual) { \ startPutcharSpy(); UnityPrintNumber((actual)); endPutcharSpy(); \ TEST_ASSERT_EQUAL_STRING((expected), getBufferPutcharSpy()); \