mirror of
https://github.com/ThrowTheSwitch/Unity.git
synced 2026-01-28 02:34:27 +01:00
Finish checking for equal floats in Arrays functions
Refactor to use the same code everywhere for FloatWithin check Rename and fix a few tests for new behavior, infinity == infinity
This commit is contained in:
76
src/unity.c
76
src/unity.c
@@ -618,7 +618,22 @@ 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; \
|
||||||
|
return !(isnan(diff) || isinf(diff) || (delta < diff));
|
||||||
|
/* This first part of this condition will catch any NaN or Infinite values */
|
||||||
|
|
||||||
#ifndef UNITY_EXCLUDE_FLOAT
|
#ifndef UNITY_EXCLUDE_FLOAT
|
||||||
|
static int UnityFloatsWithin(_UF delta, _UF expected, _UF actual)
|
||||||
|
{
|
||||||
|
_UF diff;
|
||||||
|
UNITY_FLOAT_OR_DOUBLE_WITHIN(delta, expected, actual, diff);
|
||||||
|
}
|
||||||
|
|
||||||
void UnityAssertEqualFloatArray(UNITY_PTR_ATTRIBUTE const _UF* expected,
|
void UnityAssertEqualFloatArray(UNITY_PTR_ATTRIBUTE const _UF* expected,
|
||||||
UNITY_PTR_ATTRIBUTE const _UF* actual,
|
UNITY_PTR_ATTRIBUTE const _UF* actual,
|
||||||
const _UU32 num_elements,
|
const _UU32 num_elements,
|
||||||
@@ -628,7 +643,6 @@ void UnityAssertEqualFloatArray(UNITY_PTR_ATTRIBUTE const _UF* expected,
|
|||||||
_UU32 elements = num_elements;
|
_UU32 elements = num_elements;
|
||||||
UNITY_PTR_ATTRIBUTE const _UF* ptr_expected = expected;
|
UNITY_PTR_ATTRIBUTE const _UF* ptr_expected = expected;
|
||||||
UNITY_PTR_ATTRIBUTE const _UF* ptr_actual = actual;
|
UNITY_PTR_ATTRIBUTE const _UF* ptr_actual = actual;
|
||||||
_UF diff, tol;
|
|
||||||
|
|
||||||
UNITY_SKIP_EXECUTION;
|
UNITY_SKIP_EXECUTION;
|
||||||
|
|
||||||
@@ -642,15 +656,7 @@ void UnityAssertEqualFloatArray(UNITY_PTR_ATTRIBUTE const _UF* expected,
|
|||||||
|
|
||||||
while (elements--)
|
while (elements--)
|
||||||
{
|
{
|
||||||
diff = *ptr_expected - *ptr_actual;
|
if (!UnityFloatsWithin(*ptr_expected * UNITY_FLOAT_PRECISION, *ptr_expected, *ptr_actual))
|
||||||
if (diff < 0.0f)
|
|
||||||
diff = 0.0f - diff;
|
|
||||||
tol = UNITY_FLOAT_PRECISION * *ptr_expected;
|
|
||||||
if (tol < 0.0f)
|
|
||||||
tol = 0.0f - tol;
|
|
||||||
|
|
||||||
/* This first part of this condition will catch any NaN or Infinite values */
|
|
||||||
if (isnan(diff) || isinf(diff) || (diff > tol))
|
|
||||||
{
|
{
|
||||||
UnityTestResultsFailBegin(lineNumber);
|
UnityTestResultsFailBegin(lineNumber);
|
||||||
UnityPrint(UnityStrElement);
|
UnityPrint(UnityStrElement);
|
||||||
@@ -678,24 +684,10 @@ void UnityAssertFloatsWithin(const _UF delta,
|
|||||||
const char* msg,
|
const char* msg,
|
||||||
const UNITY_LINE_TYPE lineNumber)
|
const UNITY_LINE_TYPE lineNumber)
|
||||||
{
|
{
|
||||||
_UF diff = actual - expected;
|
|
||||||
_UF pos_delta = delta;
|
|
||||||
|
|
||||||
UNITY_SKIP_EXECUTION;
|
UNITY_SKIP_EXECUTION;
|
||||||
|
|
||||||
if (expected == actual) return;
|
|
||||||
|
|
||||||
if (diff < 0.0f)
|
if (!UnityFloatsWithin(delta, expected, actual))
|
||||||
{
|
|
||||||
diff = 0.0f - diff;
|
|
||||||
}
|
|
||||||
if (pos_delta < 0.0f)
|
|
||||||
{
|
|
||||||
pos_delta = 0.0f - pos_delta;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* This first part of this condition will catch any NaN or Infinite values */
|
|
||||||
if (isnan(diff) || isinf(diff) || (pos_delta < diff))
|
|
||||||
{
|
{
|
||||||
UnityTestResultsFailBegin(lineNumber);
|
UnityTestResultsFailBegin(lineNumber);
|
||||||
#ifdef UNITY_FLOAT_VERBOSE
|
#ifdef UNITY_FLOAT_VERBOSE
|
||||||
@@ -782,6 +774,12 @@ void UnityAssertFloatSpecial(const _UF actual,
|
|||||||
|
|
||||||
/*-----------------------------------------------*/
|
/*-----------------------------------------------*/
|
||||||
#ifndef UNITY_EXCLUDE_DOUBLE
|
#ifndef UNITY_EXCLUDE_DOUBLE
|
||||||
|
static int UnityDoublesWithin(_UD delta, _UD expected, _UD actual)
|
||||||
|
{
|
||||||
|
_UD diff;
|
||||||
|
UNITY_FLOAT_OR_DOUBLE_WITHIN(delta, expected, actual, diff);
|
||||||
|
}
|
||||||
|
|
||||||
void UnityAssertEqualDoubleArray(UNITY_PTR_ATTRIBUTE const _UD* expected,
|
void UnityAssertEqualDoubleArray(UNITY_PTR_ATTRIBUTE const _UD* expected,
|
||||||
UNITY_PTR_ATTRIBUTE const _UD* actual,
|
UNITY_PTR_ATTRIBUTE const _UD* actual,
|
||||||
const _UU32 num_elements,
|
const _UU32 num_elements,
|
||||||
@@ -791,7 +789,6 @@ void UnityAssertEqualDoubleArray(UNITY_PTR_ATTRIBUTE const _UD* expected,
|
|||||||
_UU32 elements = num_elements;
|
_UU32 elements = num_elements;
|
||||||
UNITY_PTR_ATTRIBUTE const _UD* ptr_expected = expected;
|
UNITY_PTR_ATTRIBUTE const _UD* ptr_expected = expected;
|
||||||
UNITY_PTR_ATTRIBUTE const _UD* ptr_actual = actual;
|
UNITY_PTR_ATTRIBUTE const _UD* ptr_actual = actual;
|
||||||
_UD diff, tol;
|
|
||||||
|
|
||||||
UNITY_SKIP_EXECUTION;
|
UNITY_SKIP_EXECUTION;
|
||||||
|
|
||||||
@@ -805,15 +802,7 @@ void UnityAssertEqualDoubleArray(UNITY_PTR_ATTRIBUTE const _UD* expected,
|
|||||||
|
|
||||||
while (elements--)
|
while (elements--)
|
||||||
{
|
{
|
||||||
diff = *ptr_expected - *ptr_actual;
|
if (!UnityDoublesWithin(*ptr_expected * UNITY_DOUBLE_PRECISION, *ptr_expected, *ptr_actual))
|
||||||
if (diff < 0.0)
|
|
||||||
diff = 0.0 - diff;
|
|
||||||
tol = UNITY_DOUBLE_PRECISION * *ptr_expected;
|
|
||||||
if (tol < 0.0)
|
|
||||||
tol = 0.0 - tol;
|
|
||||||
|
|
||||||
/* This first part of this condition will catch any NaN or Infinite values */
|
|
||||||
if (isnan(diff) || isinf(diff) || (diff > tol))
|
|
||||||
{
|
{
|
||||||
UnityTestResultsFailBegin(lineNumber);
|
UnityTestResultsFailBegin(lineNumber);
|
||||||
UnityPrint(UnityStrElement);
|
UnityPrint(UnityStrElement);
|
||||||
@@ -841,24 +830,9 @@ void UnityAssertDoublesWithin(const _UD delta,
|
|||||||
const char* msg,
|
const char* msg,
|
||||||
const UNITY_LINE_TYPE lineNumber)
|
const UNITY_LINE_TYPE lineNumber)
|
||||||
{
|
{
|
||||||
_UD diff = actual - expected;
|
|
||||||
_UD pos_delta = delta;
|
|
||||||
|
|
||||||
UNITY_SKIP_EXECUTION;
|
UNITY_SKIP_EXECUTION;
|
||||||
|
|
||||||
if (expected == actual) return;
|
if (!UnityDoublesWithin(delta, expected, actual))
|
||||||
|
|
||||||
if (diff < 0.0)
|
|
||||||
{
|
|
||||||
diff = 0.0 - diff;
|
|
||||||
}
|
|
||||||
if (pos_delta < 0.0)
|
|
||||||
{
|
|
||||||
pos_delta = 0.0 - pos_delta;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* This first part of this condition will catch any NaN or Infinite values */
|
|
||||||
if (isnan(diff) || isinf(diff) || (pos_delta < diff))
|
|
||||||
{
|
{
|
||||||
UnityTestResultsFailBegin(lineNumber);
|
UnityTestResultsFailBegin(lineNumber);
|
||||||
#ifdef UNITY_DOUBLE_VERBOSE
|
#ifdef UNITY_DOUBLE_VERBOSE
|
||||||
|
|||||||
@@ -4,8 +4,8 @@
|
|||||||
[Released under MIT License. Please refer to license.txt for details]
|
[Released under MIT License. Please refer to license.txt for details]
|
||||||
========================================== */
|
========================================== */
|
||||||
|
|
||||||
#include <setjmp.h>
|
|
||||||
#include "unity.h"
|
#include "unity.h"
|
||||||
|
#include <setjmp.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
// Dividing by these constants produces +/- infinity.
|
// Dividing by these constants produces +/- infinity.
|
||||||
@@ -2833,7 +2833,7 @@ void testFloatsNotEqualExpectedInf(void)
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void testFloatsNotEqualBothInf(void)
|
void testFloatsEqualBothInf(void)
|
||||||
{
|
{
|
||||||
#ifdef UNITY_EXCLUDE_FLOAT
|
#ifdef UNITY_EXCLUDE_FLOAT
|
||||||
TEST_IGNORE();
|
TEST_IGNORE();
|
||||||
@@ -3187,7 +3187,7 @@ void testNotEqualFloatArraysNaN(void)
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void testNotEqualFloatArraysInf(void)
|
void testEqualFloatArraysInf(void)
|
||||||
{
|
{
|
||||||
#ifdef UNITY_EXCLUDE_FLOAT
|
#ifdef UNITY_EXCLUDE_FLOAT
|
||||||
TEST_IGNORE();
|
TEST_IGNORE();
|
||||||
@@ -3195,9 +3195,7 @@ void testNotEqualFloatArraysInf(void)
|
|||||||
float p0[] = {1.0f, 1.0f / f_zero, 25.4f, 0.253f};
|
float p0[] = {1.0f, 1.0f / f_zero, 25.4f, 0.253f};
|
||||||
float p1[] = {1.0f, 1.0f / f_zero, 25.4f, 0.253f};
|
float p1[] = {1.0f, 1.0f / f_zero, 25.4f, 0.253f};
|
||||||
|
|
||||||
EXPECT_ABORT_BEGIN
|
|
||||||
TEST_ASSERT_EQUAL_FLOAT_ARRAY(p0, p1, 4);
|
TEST_ASSERT_EQUAL_FLOAT_ARRAY(p0, p1, 4);
|
||||||
VERIFY_FAILS_END
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3363,7 +3361,7 @@ void testDoublesNotEqualExpectedInf(void)
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void testDoublesNotEqualBothInf(void)
|
void testDoublesEqualBothInf(void)
|
||||||
{
|
{
|
||||||
#ifdef UNITY_EXCLUDE_DOUBLE
|
#ifdef UNITY_EXCLUDE_DOUBLE
|
||||||
TEST_IGNORE();
|
TEST_IGNORE();
|
||||||
@@ -3716,7 +3714,7 @@ void testNotEqualDoubleArraysNaN(void)
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void testNotEqualDoubleArraysInf(void)
|
void testEqualDoubleArraysInf(void)
|
||||||
{
|
{
|
||||||
#ifdef UNITY_EXCLUDE_DOUBLE
|
#ifdef UNITY_EXCLUDE_DOUBLE
|
||||||
TEST_IGNORE();
|
TEST_IGNORE();
|
||||||
@@ -3724,9 +3722,7 @@ void testNotEqualDoubleArraysInf(void)
|
|||||||
double p0[] = {1.0, 1.0 / d_zero, 25.4, 0.253};
|
double p0[] = {1.0, 1.0 / d_zero, 25.4, 0.253};
|
||||||
double p1[] = {1.0, 1.0 / d_zero, 25.4, 0.253};
|
double p1[] = {1.0, 1.0 / d_zero, 25.4, 0.253};
|
||||||
|
|
||||||
EXPECT_ABORT_BEGIN
|
|
||||||
TEST_ASSERT_EQUAL_DOUBLE_ARRAY(p0, p1, 4);
|
TEST_ASSERT_EQUAL_DOUBLE_ARRAY(p0, p1, 4);
|
||||||
VERIFY_FAILS_END
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user