From 899f2f2fabb0e3f1a18c9223ab1fdc8d02986220 Mon Sep 17 00:00:00 2001 From: Ross Ryles Date: Tue, 30 Oct 2012 15:24:10 +0000 Subject: [PATCH] UnityAssertFloatsWithin now fails any test where either a NaN or Infinite value is passed as expected or actual. --- src/unity.c | 4 ++-- test/testunity.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/src/unity.c b/src/unity.c index 8188345..1a5acc9 100644 --- a/src/unity.c +++ b/src/unity.c @@ -578,8 +578,8 @@ void UnityAssertFloatsWithin(const _UF delta, pos_delta = 0.0f - pos_delta; } - // NOTE: This comparison is deliberately this way round so that NaNs fail. - if ( ! (pos_delta >= diff) ) + //This first part of this condition will catch any NaN or Infinite values + if ((diff * 0.0f != 0.0f) || (pos_delta < diff)) { UnityTestResultsFailBegin(lineNumber); #ifdef UNITY_FLOAT_VERBOSE diff --git a/test/testunity.c b/test/testunity.c index 2437f3f..5daf2ae 100755 --- a/test/testunity.c +++ b/test/testunity.c @@ -2281,6 +2281,50 @@ void testFloatsNotEqualNaNInf(void) #endif } +void testFloatsNotEqualActualInf(void) +{ +#ifdef UNITY_EXCLUDE_FLOAT + TEST_IGNORE(); +#else + EXPECT_ABORT_BEGIN + TEST_ASSERT_EQUAL_FLOAT(321.642f, 1.0f / 0.0f); + VERIFY_FAILS_END +#endif +} + +void testFloatsNotEqualExpectedInf(void) +{ +#ifdef UNITY_EXCLUDE_FLOAT + TEST_IGNORE(); +#else + EXPECT_ABORT_BEGIN + TEST_ASSERT_EQUAL_FLOAT(1.0f / 0.0f, 321.642f); + VERIFY_FAILS_END +#endif +} + +void testFloatsNotEqualBothInf(void) +{ +#ifdef UNITY_EXCLUDE_FLOAT + TEST_IGNORE(); +#else + EXPECT_ABORT_BEGIN + TEST_ASSERT_EQUAL_FLOAT(1.0f / 0.0f, 1.0f / 0.0f); + VERIFY_FAILS_END +#endif +} + +void testFloatsNotEqualPlusMinusInf(void) +{ +#ifdef UNITY_EXCLUDE_FLOAT + TEST_IGNORE(); +#else + EXPECT_ABORT_BEGIN + TEST_ASSERT_EQUAL_FLOAT(1.0f / 0.0f, -1.0f / 0.0f); + VERIFY_FAILS_END +#endif +} + void testEqualFloatArrays(void) { #ifdef UNITY_EXCLUDE_FLOAT