From bf953322fbf21db8932133e850e98aa2b04f8217 Mon Sep 17 00:00:00 2001 From: hectorj Date: Wed, 1 Apr 2020 12:17:59 +0200 Subject: [PATCH] Make InEpsilon fail when given a NaN fixes #918 --- assert/assertions.go | 9 +++++++++ assert/assertions_test.go | 3 +++ 2 files changed, 12 insertions(+) diff --git a/assert/assertions.go b/assert/assertions.go index 044da8b01..023042375 100644 --- a/assert/assertions.go +++ b/assert/assertions.go @@ -1128,6 +1128,9 @@ func calcRelativeError(expected, actual interface{}) (float64, error) { if !aok { return 0, fmt.Errorf("expected value %q cannot be converted to float", expected) } + if math.IsNaN(af) { + return 0, errors.New("expected value must not be NaN") + } if af == 0 { return 0, fmt.Errorf("expected value must have a value other than zero to calculate the relative error") } @@ -1135,6 +1138,9 @@ func calcRelativeError(expected, actual interface{}) (float64, error) { if !bok { return 0, fmt.Errorf("actual value %q cannot be converted to float", actual) } + if math.IsNaN(bf) { + return 0, errors.New("actual value must not be NaN") + } return math.Abs(af-bf) / math.Abs(af), nil } @@ -1144,6 +1150,9 @@ func InEpsilon(t TestingT, expected, actual interface{}, epsilon float64, msgAnd if h, ok := t.(tHelper); ok { h.Helper() } + if math.IsNaN(epsilon) { + return Fail(t, "epsilon must not be NaN") + } actualEpsilon, err := calcRelativeError(expected, actual) if err != nil { return Fail(t, err.Error(), msgAndArgs...) diff --git a/assert/assertions_test.go b/assert/assertions_test.go index a4a95858b..9fdf6b2db 100644 --- a/assert/assertions_test.go +++ b/assert/assertions_test.go @@ -1267,6 +1267,9 @@ func TestInEpsilon(t *testing.T) { {0.1, -0.1, 1.99}, {0, 0.1, 2}, // expected must be different to zero {time.Second, time.Second + 10*time.Millisecond, 0.002}, + {math.NaN(), 0, 1}, + {0, math.NaN(), 1}, + {0, 0, math.NaN()}, } for _, tc := range cases {