MCPcopy
hub / github.com/stretchr/testify / InDelta

Function InDelta

assert/assertions.go:1447–1477  ·  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

1445//
1446// assert.InDelta(t, math.Pi, 22/7.0, 0.01)
1447func InDelta(t TestingT, expected, actual interface{}, delta float64, msgAndArgs ...interface{}) bool {
1448 if h, ok := t.(tHelper); ok {
1449 h.Helper()
1450 }
1451
1452 af, aok := toFloat(expected)
1453 bf, bok := toFloat(actual)
1454
1455 if !aok || !bok {
1456 return Fail(t, "Parameters must be numerical", msgAndArgs...)
1457 }
1458
1459 if math.IsNaN(af) && math.IsNaN(bf) {
1460 return true
1461 }
1462
1463 if math.IsNaN(af) {
1464 return Fail(t, "Expected must not be NaN", msgAndArgs...)
1465 }
1466
1467 if math.IsNaN(bf) {
1468 return Fail(t, fmt.Sprintf("Expected %v with delta %v, but was NaN", expected, delta), msgAndArgs...)
1469 }
1470
1471 dt := af - bf
1472 if dt < -delta || dt > delta {
1473 return Fail(t, fmt.Sprintf("Max difference between %v and %v allowed is %v, but difference was %v", expected, actual, delta, dt), msgAndArgs...)
1474 }
1475
1476 return true
1477}
1478
1479// InDeltaSlice is the same as InDelta, except it compares two slices.
1480func 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