Skip to content

Commit

Permalink
We're going to use the C99 isinf() and isnan() macros wherever possib…
Browse files Browse the repository at this point in the history
…le now. If your compiler doesn't support this, define UNITY_EXCLUDE_MATH_H and it will go back to the old method
  • Loading branch information
mvandervoord committed Nov 13, 2015
1 parent e4a99b5 commit c6dc96f
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 17 deletions.
Binary file modified docs/Unity Summary.odt
Binary file not shown.
Binary file modified docs/Unity Summary.pdf
Binary file not shown.
33 changes: 16 additions & 17 deletions src/unity.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,10 @@ const char UnityStrResultsTests[] = " Tests ";
const char UnityStrResultsFailures[] = " Failures ";
const char UnityStrResultsIgnored[] = " Ignored ";

#ifndef UNITY_EXCLUDE_FLOAT
#ifdef UNITY_FLOAT_NEEDS_ZERO
// Dividing by these constants produces +/- infinity.
// The rationale is given in UnityAssertFloatIsInf's body.
static const _UF f_zero = 0.0f;
#ifndef UNITY_EXCLUDE_DOUBLE
static const _UD d_zero = 0.0;
#endif
#endif

// compiler-generic print formatting masks
Expand Down Expand Up @@ -738,29 +735,30 @@ void UnityAssertFloatSpecial(const _UF actual,
//We are using a variable to hold the zero value because some compilers complain about dividing by zero otherwise
case UNITY_FLOAT_IS_INF:
case UNITY_FLOAT_IS_NOT_INF:
is_trait = ((1.0f / f_zero) == actual) ? 1 : 0;
is_trait = isinf(actual) & ispos(actual);
break;
case UNITY_FLOAT_IS_NEG_INF:
case UNITY_FLOAT_IS_NOT_NEG_INF:
is_trait = ((-1.0f / f_zero) == actual) ? 1 : 0;
is_trait = isinf(actual) & isneg(actual);
break;

//NaN is the only floating point value that does NOT equal itself. Therefore if Actual == Actual, then it is NOT NaN.
case UNITY_FLOAT_IS_NAN:
case UNITY_FLOAT_IS_NOT_NAN:
is_trait = (actual == actual) ? 0 : 1;
is_trait = isnan(actual);
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_NOT_DET:
if ( (actual != actual) || ((1.0f / f_zero) == actual) || ((-1.0f / f_zero) == actual) )
if (isinf(actual) | isnan(actual))
is_trait = 0;
else
is_trait = 1;
break;
default:
;

default:
break;
}

if (is_trait != should_be_trait)
Expand Down Expand Up @@ -894,35 +892,36 @@ void UnityAssertDoubleSpecial(const _UD actual,

UNITY_SKIP_EXECUTION;

switch(style)
switch(style)
{
//To determine Inf / Neg Inf, we compare to an Inf / Neg Inf value we create on the fly
//We are using a variable to hold the zero value because some compilers complain about dividing by zero otherwise
case UNITY_FLOAT_IS_INF:
case UNITY_FLOAT_IS_NOT_INF:
is_trait = ((1.0 / d_zero) == actual) ? 1 : 0;
is_trait = isinf(actual) & ispos(actual);
break;
case UNITY_FLOAT_IS_NEG_INF:
case UNITY_FLOAT_IS_NOT_NEG_INF:
is_trait = ((-1.0 / d_zero) == actual) ? 1 : 0;
is_trait = isinf(actual) & isneg(actual);
break;

//NaN is the only floating point value that does NOT equal itself. Therefore if Actual == Actual, then it is NOT NaN.
case UNITY_FLOAT_IS_NAN:
case UNITY_FLOAT_IS_NOT_NAN:
is_trait = (actual == actual) ? 0 : 1;
is_trait = isnan(actual);
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_NOT_DET:
if ( (actual != actual) || ((1.0 / d_zero) == actual) || ((-1.0 / d_zero) == actual) )
if (isinf(actual) | isnan(actual))
is_trait = 0;
else
is_trait = 1;
break;
default:
;

default:
break;
}

if (is_trait != should_be_trait)
Expand Down
22 changes: 22 additions & 0 deletions src/unity_internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
//apparently this is not a constant expression: (sizeof(unsigned int *) * 256 - 1) so we have to just let this fall through
#endif
#endif

#ifndef UNITY_EXCLUDE_MATH_H
#include <math.h>
#endif

//-------------------------------------------------------
// Guess Widths If Not Specified
//-------------------------------------------------------
Expand Down Expand Up @@ -214,6 +219,23 @@ typedef _US64 _U_SINT;
#endif
typedef UNITY_FLOAT_TYPE _UF;

#ifndef isinf
#define isinf(n) (((1.0f / f_zero) == n) ? 1 : 0) || (((-1.0f / f_zero) == n) ? 1 : 0)
#define UNITY_FLOAT_NEEDS_ZERO
#endif

#ifndef isnan
#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

//-------------------------------------------------------
Expand Down
83 changes: 83 additions & 0 deletions test/targets/clang_file.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
---
compiler:
path: clang
source_path: '../src/'
unit_tests_path: &unit_tests_path 'tests/'
build_path: &build_path 'build/'
options:
- '-c'
- '-Wall'
- '-Wextra'
- '-Werror'
- '-Wcast-qual'
- '-Wconversion'
- '-Wdisabled-optimization'
- '-Wformat=2'
- '-Winit-self'
- '-Winline'
- '-Winvalid-pch'
- '-Wmissing-declarations'
- '-Wmissing-include-dirs'
- '-Wmissing-prototypes'
- '-Wnonnull'
- '-Wpacked'
- '-Wpointer-arith'
- '-Wredundant-decls'
- '-Wswitch-default'
- '-Wstrict-aliasing'
- '-Wstrict-overflow=5'
- '-Wuninitialized'
- '-Wunused'
- '-Wunreachable-code'
- '-Wreturn-type'
- '-Wshadow'
- '-Wundef'
- '-Wwrite-strings'
- '-Wno-missing-declarations'
- '-Wno-missing-prototypes'
- '-Wno-nested-externs'
- '-Wno-redundant-decls'
- '-Wno-unused-parameter'
- '-Wno-variadic-macros'
- '-Wbad-function-cast'
- '-fms-extensions'
- '-fno-omit-frame-pointer'
- '-ffloat-store'
- '-fno-common'
- '-fstrict-aliasing'
- '-std=gnu99'
- '-pedantic'
- '-O0'
includes:
prefix: '-I'
items:
- 'src/'
- '../src/'
- *unit_tests_path
defines:
prefix: '-D'
items:
- UNITY_INCLUDE_DOUBLE
- UNITY_SUPPORT_64
- UNITY_OUTPUT_RESULTS_FILE
object_files:
prefix: '-o'
extension: '.o'
destination: *build_path
linker:
path: gcc
options:
- -lm
- '-m64'
includes:
prefix: '-I'
object_files:
path: *build_path
extension: '.o'
bin_files:
prefix: '-o'
extension: '.exe'
destination: *build_path
colour: true
:unity:
:plugins: []
46 changes: 46 additions & 0 deletions test/targets/gcc_manual_math.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
compiler:
path: gcc
source_path: '../src/'
unit_tests_path: &unit_tests_path 'tests/'
build_path: &build_path 'build/'
options:
- '-c'
- '-m64'
- '-Wall'
- '-Wno-address'
- '-std=c99'
- '-pedantic'
includes:
prefix: '-I'
items:
- 'src/'
- '../src/'
- *unit_tests_path
defines:
prefix: '-D'
items:
- UNITY_EXCLUDE_MATH_H
- UNITY_INCLUDE_DOUBLE
- UNITY_SUPPORT_TEST_CASES
- UNITY_SUPPORT_64
object_files:
prefix: '-o'
extension: '.o'
destination: *build_path
linker:
path: gcc
options:
- -lm
- '-m64'
includes:
prefix: '-I'
object_files:
path: *build_path
extension: '.o'
bin_files:
prefix: '-o'
extension: '.exe'
destination: *build_path
colour: true
:unity:
:plugins: []

0 comments on commit c6dc96f

Please sign in to comment.