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