Add InEpsilon().

This commit is contained in:
bhenderson 2014-07-10 08:58:49 -07:00
parent 3b602c6e81
commit 60a27ebea6

View File

@ -537,6 +537,39 @@ func InDelta(t TestingT, expected, actual interface{}, delta float64, msgAndArgs
return true
}
// InEpsilon asserts that expected and actual have a relative error less than epsilon
//
// 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)
bf, bok := toFloat(actual)
if !aok || !bok {
return Fail(t, fmt.Sprintf("Parameters must be numerical"), msgAndArgs...)
}
if af < 0 {
af = -af
}
if bf < 0 {
bf = -bf
}
var delta float64
if af < bf {
delta = af * epsilon
} else {
delta = bf * epsilon
}
// delta = min(|expected|, |actual|) * epsilon
dt := af - bf
if dt < -delta || dt > delta {
return Fail(t, fmt.Sprintf("Max difference between %v and %v allowed is %v, but difference was %v", expected, actual, delta, dt), msgAndArgs...)
}
return true
}
/*
Errors
*/