Fix NaN similarity checks

Plus minor alignments in code.
pull/1084/head^2
Menno 2021-05-30 21:11:30 +02:00 committed by Boyan Soubachov
parent ee42bbe4ab
commit fd9e1fb0e1
2 changed files with 32 additions and 24 deletions

View File

@ -1164,6 +1164,10 @@ func InDelta(t TestingT, expected, actual interface{}, delta float64, msgAndArgs
return Fail(t, fmt.Sprintf("Parameters must be numerical"), msgAndArgs...)
}
if math.IsNaN(af) && math.IsNaN(bf) {
return true
}
if math.IsNaN(af) {
return Fail(t, fmt.Sprintf("Expected must not be NaN"), msgAndArgs...)
}
@ -1250,8 +1254,12 @@ func InDeltaMapValues(t TestingT, expected, actual interface{}, delta float64, m
func calcRelativeError(expected, actual interface{}) (float64, error) {
af, aok := toFloat(expected)
if !aok {
return 0, fmt.Errorf("expected value %q cannot be converted to float", expected)
bf, bok := toFloat(actual)
if !aok || !bok {
return 0, fmt.Errorf("Parameters must be numerical")
}
if math.IsNaN(af) && math.IsNaN(bf) {
return 0, nil
}
if math.IsNaN(af) {
return 0, errors.New("expected value must not be NaN")
@ -1259,10 +1267,6 @@ func calcRelativeError(expected, actual interface{}) (float64, error) {
if af == 0 {
return 0, fmt.Errorf("expected value must have a value other than zero to calculate the relative error")
}
bf, bok := toFloat(actual)
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")
}

View File

@ -1324,6 +1324,7 @@ func TestInDelta(t *testing.T) {
False(t, InDelta(mockT, "", nil, 1), "Expected non numerals to fail")
False(t, InDelta(mockT, 42, math.NaN(), 0.01), "Expected NaN for actual to fail")
False(t, InDelta(mockT, math.NaN(), 42, 0.01), "Expected NaN for expected to fail")
True(t, InDelta(mockT, math.NaN(), math.NaN(), 0.01), "Expected NaN for both to pass")
cases := []struct {
a, b interface{}
@ -1354,19 +1355,19 @@ func TestInDeltaSlice(t *testing.T) {
mockT := new(testing.T)
True(t, InDeltaSlice(mockT,
[]float64{1.001, 0.999},
[]float64{1, 1},
0.1), "{1.001, 0.009} is element-wise close to {1, 1} in delta=0.1")
[]float64{1.001, math.NaN(), 0.999},
[]float64{1, math.NaN(), 1},
0.1), "{1.001, NaN, 0.009} is element-wise close to {1, NaN, 1} in delta=0.1")
True(t, InDeltaSlice(mockT,
[]float64{1, 2},
[]float64{0, 3},
1), "{1, 2} is element-wise close to {0, 3} in delta=1")
[]float64{1, math.NaN(), 2},
[]float64{0, math.NaN(), 3},
1), "{1, NaN, 2} is element-wise close to {0, NaN, 3} in delta=1")
False(t, InDeltaSlice(mockT,
[]float64{1, 2},
[]float64{0, 3},
0.1), "{1, 2} is not element-wise close to {0, 3} in delta=0.1")
[]float64{1, math.NaN(), 2},
[]float64{0, math.NaN(), 3},
0.1), "{1, NaN, 2} is not element-wise close to {0, NaN, 3} in delta=0.1")
False(t, InDeltaSlice(mockT, "", nil, 1), "Expected non numeral slices to fail")
}
@ -1386,10 +1387,12 @@ func TestInDeltaMapValues(t *testing.T) {
expect: map[string]float64{
"foo": 1.0,
"bar": 2.0,
"baz": math.NaN(),
},
actual: map[string]float64{
"foo": 1.01,
"bar": 1.99,
"baz": math.NaN(),
},
delta: 0.1,
f: True,
@ -1422,10 +1425,10 @@ func TestInDeltaMapValues(t *testing.T) {
{
title: "Within delta with zero value",
expect: map[string]float64{
"zero": 0.0,
"zero": 0,
},
actual: map[string]float64{
"zero": 0.0,
"zero": 0,
},
delta: 0.1,
f: True,
@ -1433,12 +1436,12 @@ func TestInDeltaMapValues(t *testing.T) {
{
title: "With missing key with zero value",
expect: map[string]float64{
"zero": 0.0,
"foo": 0.0,
"zero": 0,
"foo": 0,
},
actual: map[string]float64{
"zero": 0.0,
"bar": 0.0,
"zero": 0,
"bar": 0,
},
f: False,
},
@ -1462,6 +1465,7 @@ func TestInEpsilon(t *testing.T) {
{uint64(100), uint8(101), 0.01},
{0.1, -0.1, 2},
{0.1, 0, 2},
{math.NaN(), math.NaN(), 1},
{time.Second, time.Second + time.Millisecond, 0.002},
}
@ -1497,9 +1501,9 @@ func TestInEpsilonSlice(t *testing.T) {
mockT := new(testing.T)
True(t, InEpsilonSlice(mockT,
[]float64{2.2, 2.0},
[]float64{2.1, 2.1},
0.06), "{2.2, 2.0} is element-wise close to {2.1, 2.1} in espilon=0.06")
[]float64{2.2, math.NaN(), 2.0},
[]float64{2.1, math.NaN(), 2.1},
0.06), "{2.2, NaN, 2.0} is element-wise close to {2.1, NaN, 2.1} in espilon=0.06")
False(t, InEpsilonSlice(mockT,
[]float64{2.2, 2.0},