From bd4ac584862ea78f917a14e4f6f89d1d11279cce Mon Sep 17 00:00:00 2001 From: jsalling Date: Thu, 29 Dec 2016 20:47:03 -0600 Subject: [PATCH] Inline the isneg/ispos macros, remove promotion warnings on constants Originated from commit c6dc96f3 but are rarely used and not in math.h Removes float to double promotion warning and simplifies the code 'gcc -std=gnu99 -Wdouble-promotion' spits out these warnings --- src/unity.c | 30 +++++++++++------------------- src/unity_internals.h | 11 ++--------- test/tests/testunity.c | 5 +++-- 3 files changed, 16 insertions(+), 30 deletions(-) diff --git a/src/unity.c b/src/unity.c index c755ebb..af5f838 100644 --- a/src/unity.c +++ b/src/unity.c @@ -688,11 +688,11 @@ 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 (isinf(expected) && isinf(actual) && (isneg(expected) == isneg(actual))) return 1; \ + if (isinf(expected) && isinf(actual) && ((expected < 0) == (actual < 0))) return 1; \ if (UNITY_NAN_CHECK) return 1; \ diff = actual - expected; \ - if (diff < 0.0f) diff = 0.0f - diff; \ - if (delta < 0.0f) delta = 0.0f - delta; \ + if (diff < 0) diff = -diff; \ + if (delta < 0) delta = -delta; \ return !(isnan(diff) || isinf(diff) || (diff > delta)) /* This first part of this condition will catch any NaN or Infinite values */ #ifndef UNITY_NAN_NOT_EQUAL_NAN @@ -793,11 +793,11 @@ void UnityAssertFloatSpecial(const UNITY_FLOAT actual, { case UNITY_FLOAT_IS_INF: case UNITY_FLOAT_IS_NOT_INF: - is_trait = isinf(actual) & ispos(actual); + is_trait = isinf(actual) && (actual > 0); break; case UNITY_FLOAT_IS_NEG_INF: case UNITY_FLOAT_IS_NOT_NEG_INF: - is_trait = isinf(actual) & isneg(actual); + is_trait = isinf(actual) && (actual < 0); break; case UNITY_FLOAT_IS_NAN: @@ -805,13 +805,9 @@ void UnityAssertFloatSpecial(const UNITY_FLOAT actual, is_trait = isnan(actual) ? 1 : 0; break; - /* A determinate number is non infinite and not NaN. (therefore the opposite of the two above) */ - case UNITY_FLOAT_IS_DET: + case UNITY_FLOAT_IS_DET: /* A determinate number is non infinite and not NaN. */ case UNITY_FLOAT_IS_NOT_DET: - if (isinf(actual) || isnan(actual)) - is_trait = 0; - else - is_trait = 1; + is_trait = !isinf(actual) && !isnan(actual); break; default: @@ -922,11 +918,11 @@ void UnityAssertDoubleSpecial(const UNITY_DOUBLE actual, { case UNITY_FLOAT_IS_INF: case UNITY_FLOAT_IS_NOT_INF: - is_trait = isinf(actual) & ispos(actual); + is_trait = isinf(actual) && (actual > 0); break; case UNITY_FLOAT_IS_NEG_INF: case UNITY_FLOAT_IS_NOT_NEG_INF: - is_trait = isinf(actual) & isneg(actual); + is_trait = isinf(actual) && (actual < 0); break; case UNITY_FLOAT_IS_NAN: @@ -934,13 +930,9 @@ void UnityAssertDoubleSpecial(const UNITY_DOUBLE actual, is_trait = isnan(actual) ? 1 : 0; break; - /* A determinate number is non infinite and not NaN. (therefore the opposite of the two above) */ - case UNITY_FLOAT_IS_DET: + case UNITY_FLOAT_IS_DET: /* A determinate number is non infinite and not NaN. */ case UNITY_FLOAT_IS_NOT_DET: - if (isinf(actual) || isnan(actual)) - is_trait = 0; - else - is_trait = 1; + is_trait = !isinf(actual) && !isnan(actual); break; default: diff --git a/src/unity_internals.h b/src/unity_internals.h index 34f0bd3..b0311dc 100644 --- a/src/unity_internals.h +++ b/src/unity_internals.h @@ -187,6 +187,7 @@ #endif typedef UNITY_FLOAT_TYPE UNITY_FLOAT; +/* isinf & isnan macros should be provided by math.h */ #ifndef isinf /* The value of Inf - Inf is NaN */ #define isinf(n) (isnan((n) - (n)) && !isnan(n)) @@ -198,14 +199,6 @@ typedef UNITY_FLOAT_TYPE UNITY_FLOAT; #define isnan(n) ((n != n) ? 1 : 0) #endif -#ifndef isneg -#define isneg(n) ((n < 0.0f) ? 1 : 0) -#endif - -#ifndef ispos -#define ispos(n) ((n > 0.0f) ? 1 : 0) -#endif - #endif /*------------------------------------------------------- @@ -243,7 +236,7 @@ typedef UNITY_FLOAT_TYPE UNITY_FLOAT; /* Double Floating Point Support */ #ifndef UNITY_DOUBLE_PRECISION - #define UNITY_DOUBLE_PRECISION (1e-12f) + #define UNITY_DOUBLE_PRECISION (1e-12) #endif #ifndef UNITY_DOUBLE_TYPE diff --git a/test/tests/testunity.c b/test/tests/testunity.c index 08c621a..e81e57a 100644 --- a/test/tests/testunity.c +++ b/test/tests/testunity.c @@ -3274,8 +3274,9 @@ void testFloatPrinting(void) TEST_ASSERT_EQUAL_PRINT_FLOATING("8.3099991e+09", 8309999104.0f); TEST_ASSERT_EQUAL_PRINT_FLOATING("1.0e+10", 1.0e+10f); TEST_ASSERT_EQUAL_PRINT_FLOATING("1.0e+10", 10000000000.0f); - TEST_ASSERT_EQUAL_PRINT_FLOATING("1.00005499e+10", 1.000055e+10f); - TEST_ASSERT_EQUAL_PRINT_FLOATING("1.10000006e+38", 1.10000005e+38f); + /* Some compilers have trouble with inexact float constants, a float cast works generally */ + TEST_ASSERT_EQUAL_PRINT_FLOATING("1.00005499e+10", (float)1.000055e+10f); + TEST_ASSERT_EQUAL_PRINT_FLOATING("1.10000006e+38", (float)1.10000005e+38f); TEST_ASSERT_EQUAL_PRINT_FLOATING("1.63529943e+10", 1.63529943e+10f); TEST_ASSERT_EQUAL_PRINT_FLOATING("3.40282347e+38", 3.40282346638e38f); -- GitLab