MCPcopy Create free account
hub / github.com/stretchr/testify / InDelta

Function InDelta

assert/assertions.go:1395–1425  ·  view source on GitHub ↗

InDelta asserts that the two numerals are within delta of each other. assert.InDelta(t, math.Pi, 22/7.0, 0.01)

(t TestingT, expected, actual interface{}, delta float64, msgAndArgs ...interface{})

Source from the content-addressed store, hash-verified

1393//
1394// assert.InDelta(t, math.Pi, 22/7.0, 0.01)
1395func InDelta(t TestingT, expected, actual interface{}, delta float64, msgAndArgs ...interface{}) bool {
1396 if h, ok := t.(tHelper); ok {
1397 h.Helper()
1398 }
1399
1400 af, aok := toFloat(expected)
1401 bf, bok := toFloat(actual)
1402
1403 if !aok || !bok {
1404 return Fail(t, "Parameters must be numerical", msgAndArgs...)
1405 }
1406
1407 if math.IsNaN(af) && math.IsNaN(bf) {
1408 return true
1409 }
1410
1411 if math.IsNaN(af) {
1412 return Fail(t, "Expected must not be NaN", msgAndArgs...)
1413 }
1414
1415 if math.IsNaN(bf) {
1416 return Fail(t, fmt.Sprintf("Expected %v with delta %v, but was NaN", expected, delta), msgAndArgs...)
1417 }
1418
1419 dt := af - bf
1420 if dt < -delta || dt > delta {
1421 return Fail(t, fmt.Sprintf("Max difference between %v and %v allowed is %v, but difference was %v", expected, actual, delta, dt), msgAndArgs...)
1422 }
1423
1424 return true
1425}
1426
1427// InDeltaSlice is the same as InDelta, except it compares two slices.
1428func InDeltaSlice(t TestingT, expected, actual interface{}, delta float64, msgAndArgs ...interface{}) bool {

Callers 6

InDeltaFunction · 0.92
InDeltafFunction · 0.70
TestInDeltaFunction · 0.70
InDeltaSliceFunction · 0.70
InDeltaMapValuesFunction · 0.70
InDeltaMethod · 0.70

Calls 3

toFloatFunction · 0.85
FailFunction · 0.70
HelperMethod · 0.65

Tested by 1

TestInDeltaFunction · 0.56