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{})
| 1393 | // |
| 1394 | // assert.InDelta(t, math.Pi, 22/7.0, 0.01) |
| 1395 | func 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. |
| 1428 | func InDeltaSlice(t TestingT, expected, actual interface{}, delta float64, msgAndArgs ...interface{}) bool { |