Make InEpsilon fail when given a NaN

fixes #918
pull/601/merge
hectorj 2020-04-01 12:17:59 +02:00 committed by Boyan Soubachov
parent dfba5a4e3a
commit 012967472b
2 changed files with 12 additions and 0 deletions

View File

@ -1245,6 +1245,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")
}
@ -1252,6 +1255,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
}
@ -1261,6 +1267,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...)

View File

@ -1458,6 +1458,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 {