Skip to content

Commit dce6d32

Browse files
committed
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.
1 parent 0f07adf commit dce6d32

File tree

3 files changed

+20
-21
lines changed

3 files changed

+20
-21
lines changed

src/unity.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -617,11 +617,12 @@ void UnityAssertEqualIntArray(UNITY_INTERNAL_PTR expected,
617617

618618
/*-----------------------------------------------*/
619619
/* Wrap this define in a function with variable types as float or double */
620-
#define UNITY_FLOAT_OR_DOUBLE_WITHIN(delta, expected, actual, diff) \
621-
if (expected == actual) return 1; \
622-
diff = actual - expected; \
623-
if (diff < 0.0f) diff = 0.0f - diff; \
624-
if (delta < 0.0f) delta = 0.0f - delta; \
620+
#define UNITY_FLOAT_OR_DOUBLE_WITHIN(delta, expected, actual, diff) \
621+
if (isinf(expected) && isinf(actual) && (isneg(expected) == isneg(actual))) return 1; \
622+
if (isnan(expected) && isnan(actual)) return 1; \
623+
diff = actual - expected; \
624+
if (diff < 0.0f) diff = 0.0f - diff; \
625+
if (delta < 0.0f) delta = 0.0f - delta; \
625626
return !(isnan(diff) || isinf(diff) || (delta < diff));
626627
/* This first part of this condition will catch any NaN or Infinite values */
627628

test/tests/testparameterized.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,26 @@ void putcharSpy(int c) { (void)putchar(c);} // include passthrough for linking t
1818

1919
#define VERIFY_FAILS_END \
2020
} \
21-
Unity.CurrentTestFailed = (Unity.CurrentTestFailed == 1) ? 0 : 1; \
21+
Unity.CurrentTestFailed = (Unity.CurrentTestFailed != 0) ? 0 : 1; \
2222
if (Unity.CurrentTestFailed == 1) { \
2323
SetToOneMeanWeAlreadyCheckedThisGuy = 1; \
24-
UnityPrint("[[[[ Previous Test Should Have Failed But Did Not ]]]]"); \
24+
UnityPrintNumberUnsigned(Unity.CurrentTestLineNumber); \
25+
UNITY_OUTPUT_CHAR(':'); \
26+
UnityPrint(Unity.CurrentTestName); \
27+
UnityPrint(":FAIL: [[[[ Test Should Have Failed But Did Not ]]]]"); \
2528
UNITY_OUTPUT_CHAR('\n'); \
2629
}
2730

2831
#define VERIFY_IGNORES_END \
2932
} \
30-
Unity.CurrentTestFailed = (Unity.CurrentTestIgnored == 1) ? 0 : 1; \
33+
Unity.CurrentTestFailed = (Unity.CurrentTestIgnored != 0) ? 0 : 1; \
3134
Unity.CurrentTestIgnored = 0; \
3235
if (Unity.CurrentTestFailed == 1) { \
3336
SetToOneMeanWeAlreadyCheckedThisGuy = 1; \
34-
UnityPrint("[[[[ Previous Test Should Have Ignored But Did Not ]]]]"); \
37+
UnityPrintNumberUnsigned(Unity.CurrentTestLineNumber); \
38+
UNITY_OUTPUT_CHAR(':'); \
39+
UnityPrint(Unity.CurrentTestName); \
40+
UnityPrint(":FAIL: [[[[ Test Should Have Ignored But Did Not ]]]]"); \
3541
UNITY_OUTPUT_CHAR('\n'); \
3642
}
3743

@@ -50,7 +56,7 @@ void tearDown(void)
5056
TEST_FAIL_MESSAGE("<= Failed in tearDown");
5157
if ((SetToOneMeanWeAlreadyCheckedThisGuy == 0) && (Unity.CurrentTestFailed > 0))
5258
{
53-
UnityPrint("[[[[ Previous Test Should Have Passed But Did Not ]]]]");
59+
UnityPrint(": [[[[ Test Should Have Passed But Did Not ]]]]");
5460
UNITY_OUTPUT_CHAR('\n');
5561
}
5662
}

test/tests/testunity.c

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2797,14 +2797,12 @@ void testFloatsNotEqualExpectedNaN(void)
27972797
#endif
27982798
}
27992799

2800-
void testFloatsNotEqualBothNaN(void)
2800+
void testFloatsEqualBothNaN(void)
28012801
{
28022802
#ifdef UNITY_EXCLUDE_FLOAT
28032803
TEST_IGNORE();
28042804
#else
2805-
EXPECT_ABORT_BEGIN
28062805
TEST_ASSERT_EQUAL_FLOAT(0.0f / f_zero, 0.0f / f_zero);
2807-
VERIFY_FAILS_END
28082806
#endif
28092807
}
28102808

@@ -3192,17 +3190,15 @@ void testNotEqualFloatArraysNegative3(void)
31923190
#endif
31933191
}
31943192

3195-
void testNotEqualFloatArraysNaN(void)
3193+
void testEqualFloatArraysNaN(void)
31963194
{
31973195
#ifdef UNITY_EXCLUDE_FLOAT
31983196
TEST_IGNORE();
31993197
#else
32003198
float p0[] = {1.0f, 0.0f / f_zero, 25.4f, 0.253f};
32013199
float p1[] = {1.0f, 0.0f / f_zero, 25.4f, 0.253f};
32023200

3203-
EXPECT_ABORT_BEGIN
32043201
TEST_ASSERT_EQUAL_FLOAT_ARRAY(p0, p1, 4);
3205-
VERIFY_FAILS_END
32063202
#endif
32073203
}
32083204

@@ -3325,14 +3321,12 @@ void testDoublesNotEqualExpectedNaN(void)
33253321
#endif
33263322
}
33273323

3328-
void testDoublesNotEqualBothNaN(void)
3324+
void testDoublesEqualBothNaN(void)
33293325
{
33303326
#ifdef UNITY_EXCLUDE_DOUBLE
33313327
TEST_IGNORE();
33323328
#else
3333-
EXPECT_ABORT_BEGIN
33343329
TEST_ASSERT_EQUAL_DOUBLE(0.0 / d_zero, 0.0 / d_zero);
3335-
VERIFY_FAILS_END
33363330
#endif
33373331
}
33383332

@@ -3727,9 +3721,7 @@ void testNotEqualDoubleArraysNaN(void)
37273721
double p0[] = {1.0, 0.0 / d_zero, 25.4, 0.253};
37283722
double p1[] = {1.0, 0.0 / d_zero, 25.4, 0.253};
37293723

3730-
EXPECT_ABORT_BEGIN
37313724
TEST_ASSERT_EQUAL_DOUBLE_ARRAY(p0, p1, 4);
3732-
VERIFY_FAILS_END
37333725
#endif
37343726
}
37353727

0 commit comments

Comments
 (0)