mirror of https://github.com/stretchr/testify.git
Refactor InEpsilon() to use InDelta().
parent
60a27ebea6
commit
256f07baef
|
@ -537,15 +537,14 @@ func InDelta(t TestingT, expected, actual interface{}, delta float64, msgAndArgs
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// InEpsilon asserts that expected and actual have a relative error less than epsilon
|
// min(|expected|, |actual|) * epsilon
|
||||||
//
|
func calcEpsilonDelta(expected, actual interface{}, epsilon float64) float64 {
|
||||||
// Returns whether the assertion was successful (true) or not (false).
|
|
||||||
func InEpsilon(t TestingT, expected, actual interface{}, epsilon float64, msgAndArgs ...interface{}) bool {
|
|
||||||
af, aok := toFloat(expected)
|
af, aok := toFloat(expected)
|
||||||
bf, bok := toFloat(actual)
|
bf, bok := toFloat(actual)
|
||||||
|
|
||||||
if !aok || !bok {
|
if !aok || !bok {
|
||||||
return Fail(t, fmt.Sprintf("Parameters must be numerical"), msgAndArgs...)
|
// invalid input
|
||||||
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
if af < 0 {
|
if af < 0 {
|
||||||
|
@ -560,14 +559,16 @@ func InEpsilon(t TestingT, expected, actual interface{}, epsilon float64, msgAnd
|
||||||
} else {
|
} else {
|
||||||
delta = bf * epsilon
|
delta = bf * epsilon
|
||||||
}
|
}
|
||||||
|
return delta
|
||||||
|
}
|
||||||
|
|
||||||
// delta = min(|expected|, |actual|) * epsilon
|
// InEpsilon asserts that expected and actual have a relative error less than epsilon
|
||||||
dt := af - bf
|
//
|
||||||
if dt < -delta || dt > delta {
|
// Returns whether the assertion was successful (true) or not (false).
|
||||||
return Fail(t, fmt.Sprintf("Max difference between %v and %v allowed is %v, but difference was %v", expected, actual, delta, dt), msgAndArgs...)
|
func InEpsilon(t TestingT, expected, actual interface{}, epsilon float64, msgAndArgs ...interface{}) bool {
|
||||||
}
|
delta := calcEpsilonDelta(expected, actual, epsilon)
|
||||||
|
|
||||||
return true
|
return InDelta(t, expected, actual, delta, msgAndArgs...)
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
Loading…
Reference in New Issue