From 2ab2fef60a2ad8fbc21b2d7c1415ea6a5f9974b4 Mon Sep 17 00:00:00 2001 From: Ross Ryles Date: Wed, 31 Oct 2012 12:34:30 +0000 Subject: [PATCH] Array comparisons of floating point types fail if any values are NaN or infinite. --- src/unity.c | 8 +++++-- test/testunity.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/src/unity.c b/src/unity.c index 317609c..8273b46 100644 --- a/src/unity.c +++ b/src/unity.c @@ -539,7 +539,9 @@ void UnityAssertEqualFloatArray(const _UF* expected, tol = UNITY_FLOAT_PRECISION * *ptr_expected; if (tol < 0.0) tol = 0.0 - tol; - if (diff > tol) + + //This first part of this condition will catch any NaN or Infinite values + if ((diff * 0.0f != 0.0f) || (diff > tol)) { UnityTestResultsFailBegin(lineNumber); UnityPrint(UnityStrElement); @@ -703,7 +705,9 @@ void UnityAssertEqualDoubleArray(const _UD* expected, tol = UNITY_DOUBLE_PRECISION * *ptr_expected; if (tol < 0.0) tol = 0.0 - tol; - if (diff > tol) + + //This first part of this condition will catch any NaN or Infinite values + if ((diff * 0.0f != 0.0f) || (diff > tol)) { UnityTestResultsFailBegin(lineNumber); UnityPrint(UnityStrElement); diff --git a/test/testunity.c b/test/testunity.c index dedd9ef..3345959 100755 --- a/test/testunity.c +++ b/test/testunity.c @@ -2529,6 +2529,34 @@ void testNotEqualFloatArraysNegative3(void) #endif } +void testNotEqualFloatArraysNaN(void) +{ +#ifdef UNITY_EXCLUDE_FLOAT + TEST_IGNORE(); +#else + float p0[] = {1.0, 0.0 / 0.0, 25.4, 0.253}; + float p1[] = {1.0, 0.0 / 0.0, 25.4, 0.253}; + + EXPECT_ABORT_BEGIN + TEST_ASSERT_EQUAL_FLOAT_ARRAY(p0, p1, 4); + VERIFY_FAILS_END +#endif +} + +void testNotEqualFloatArraysInf(void) +{ +#ifdef UNITY_EXCLUDE_FLOAT + TEST_IGNORE(); +#else + float p0[] = {1.0, 1.0 / 0.0, 25.4, 0.253}; + float p1[] = {1.0, 1.0 / 0.0, 25.4, 0.253}; + + EXPECT_ABORT_BEGIN + TEST_ASSERT_EQUAL_FLOAT_ARRAY(p0, p1, 4); + VERIFY_FAILS_END +#endif +} + // ===================== THESE TEST WILL RUN IF YOUR CONFIG INCLUDES DOUBLE SUPPORT ================== void testDoublesWithinDelta(void) @@ -2903,3 +2931,30 @@ void testNotEqualDoubleArraysNegative3(void) #endif } +void testNotEqualDoubleArraysNaN(void) +{ +#ifdef UNITY_EXCLUDE_DOUBLE + TEST_IGNORE(); +#else + double p0[] = {1.0, 0.0 / 0.0, 25.4, 0.253}; + double p1[] = {1.0, 0.0 / 0.0, 25.4, 0.253}; + + EXPECT_ABORT_BEGIN + TEST_ASSERT_EQUAL_DOUBLE_ARRAY(p0, p1, 4); + VERIFY_FAILS_END +#endif +} + +void testNotEqualDoubleArraysInf(void) +{ +#ifdef UNITY_EXCLUDE_DOUBLE + TEST_IGNORE(); +#else + double p0[] = {1.0, 1.0 / 0.0, 25.4, 0.253}; + double p1[] = {1.0, 1.0 / 0.0, 25.4, 0.253}; + + EXPECT_ABORT_BEGIN + TEST_ASSERT_EQUAL_DOUBLE_ARRAY(p0, p1, 4); + VERIFY_FAILS_END +#endif +} \ No newline at end of file