From dce6d329ffa3a7317e52b2e0b7466886815f2297 Mon Sep 17 00:00:00 2001 From: Mark VanderVoord Date: Thu, 22 Sep 2016 08:35:22 -0400 Subject: [PATCH] Finished fixing floating point comparisons. We have streamlined how floats and doubles are checked, but we still can't compare them for equality directly. So we're directly testing for infinite and NaN before checking diffs. Also, we've officially decided that for testing purposes NaN shall equal NaN, +Inf shall equal +Inf, and -Inf shall equal -Inf. It's what most people expect during a test. --- src/unity.c | 11 ++++++----- test/tests/testparameterized.c | 16 +++++++++++----- test/tests/testunity.c | 14 +++----------- 3 files changed, 20 insertions(+), 21 deletions(-) diff --git a/src/unity.c b/src/unity.c index 8cca41b..58e93db 100644 --- a/src/unity.c +++ b/src/unity.c @@ -617,11 +617,12 @@ void UnityAssertEqualIntArray(UNITY_INTERNAL_PTR expected, /*-----------------------------------------------*/ /* Wrap this define in a function with variable types as float or double */ -#define UNITY_FLOAT_OR_DOUBLE_WITHIN(delta, expected, actual, diff) \ - if (expected == actual) return 1; \ - diff = actual - expected; \ - if (diff < 0.0f) diff = 0.0f - diff; \ - if (delta < 0.0f) delta = 0.0f - delta; \ +#define UNITY_FLOAT_OR_DOUBLE_WITHIN(delta, expected, actual, diff) \ + if (isinf(expected) && isinf(actual) && (isneg(expected) == isneg(actual))) return 1; \ + if (isnan(expected) && isnan(actual)) return 1; \ + diff = actual - expected; \ + if (diff < 0.0f) diff = 0.0f - diff; \ + if (delta < 0.0f) delta = 0.0f - delta; \ return !(isnan(diff) || isinf(diff) || (delta < diff)); /* This first part of this condition will catch any NaN or Infinite values */ diff --git a/test/tests/testparameterized.c b/test/tests/testparameterized.c index 95216dd..aa6d173 100644 --- a/test/tests/testparameterized.c +++ b/test/tests/testparameterized.c @@ -18,20 +18,26 @@ void putcharSpy(int c) { (void)putchar(c);} // include passthrough for linking t #define VERIFY_FAILS_END \ } \ - Unity.CurrentTestFailed = (Unity.CurrentTestFailed == 1) ? 0 : 1; \ + Unity.CurrentTestFailed = (Unity.CurrentTestFailed != 0) ? 0 : 1; \ if (Unity.CurrentTestFailed == 1) { \ SetToOneMeanWeAlreadyCheckedThisGuy = 1; \ - UnityPrint("[[[[ Previous Test Should Have Failed But Did Not ]]]]"); \ + UnityPrintNumberUnsigned(Unity.CurrentTestLineNumber); \ + UNITY_OUTPUT_CHAR(':'); \ + UnityPrint(Unity.CurrentTestName); \ + UnityPrint(":FAIL: [[[[ Test Should Have Failed But Did Not ]]]]"); \ UNITY_OUTPUT_CHAR('\n'); \ } #define VERIFY_IGNORES_END \ } \ - Unity.CurrentTestFailed = (Unity.CurrentTestIgnored == 1) ? 0 : 1; \ + Unity.CurrentTestFailed = (Unity.CurrentTestIgnored != 0) ? 0 : 1; \ Unity.CurrentTestIgnored = 0; \ if (Unity.CurrentTestFailed == 1) { \ SetToOneMeanWeAlreadyCheckedThisGuy = 1; \ - UnityPrint("[[[[ Previous Test Should Have Ignored But Did Not ]]]]"); \ + UnityPrintNumberUnsigned(Unity.CurrentTestLineNumber); \ + UNITY_OUTPUT_CHAR(':'); \ + UnityPrint(Unity.CurrentTestName); \ + UnityPrint(":FAIL: [[[[ Test Should Have Ignored But Did Not ]]]]"); \ UNITY_OUTPUT_CHAR('\n'); \ } @@ -50,7 +56,7 @@ void tearDown(void) TEST_FAIL_MESSAGE("<= Failed in tearDown"); if ((SetToOneMeanWeAlreadyCheckedThisGuy == 0) && (Unity.CurrentTestFailed > 0)) { - UnityPrint("[[[[ Previous Test Should Have Passed But Did Not ]]]]"); + UnityPrint(": [[[[ Test Should Have Passed But Did Not ]]]]"); UNITY_OUTPUT_CHAR('\n'); } } diff --git a/test/tests/testunity.c b/test/tests/testunity.c index a8a0d32..8230163 100644 --- a/test/tests/testunity.c +++ b/test/tests/testunity.c @@ -2797,14 +2797,12 @@ void testFloatsNotEqualExpectedNaN(void) #endif } -void testFloatsNotEqualBothNaN(void) +void testFloatsEqualBothNaN(void) { #ifdef UNITY_EXCLUDE_FLOAT TEST_IGNORE(); #else - EXPECT_ABORT_BEGIN TEST_ASSERT_EQUAL_FLOAT(0.0f / f_zero, 0.0f / f_zero); - VERIFY_FAILS_END #endif } @@ -3192,7 +3190,7 @@ void testNotEqualFloatArraysNegative3(void) #endif } -void testNotEqualFloatArraysNaN(void) +void testEqualFloatArraysNaN(void) { #ifdef UNITY_EXCLUDE_FLOAT TEST_IGNORE(); @@ -3200,9 +3198,7 @@ void testNotEqualFloatArraysNaN(void) float p0[] = {1.0f, 0.0f / f_zero, 25.4f, 0.253f}; float p1[] = {1.0f, 0.0f / f_zero, 25.4f, 0.253f}; - EXPECT_ABORT_BEGIN TEST_ASSERT_EQUAL_FLOAT_ARRAY(p0, p1, 4); - VERIFY_FAILS_END #endif } @@ -3325,14 +3321,12 @@ void testDoublesNotEqualExpectedNaN(void) #endif } -void testDoublesNotEqualBothNaN(void) +void testDoublesEqualBothNaN(void) { #ifdef UNITY_EXCLUDE_DOUBLE TEST_IGNORE(); #else - EXPECT_ABORT_BEGIN TEST_ASSERT_EQUAL_DOUBLE(0.0 / d_zero, 0.0 / d_zero); - VERIFY_FAILS_END #endif } @@ -3727,9 +3721,7 @@ void testNotEqualDoubleArraysNaN(void) double p0[] = {1.0, 0.0 / d_zero, 25.4, 0.253}; double p1[] = {1.0, 0.0 / d_zero, 25.4, 0.253}; - EXPECT_ABORT_BEGIN TEST_ASSERT_EQUAL_DOUBLE_ARRAY(p0, p1, 4); - VERIFY_FAILS_END #endif }