InDeltaMapValues is the same as InDelta, but it compares all values between two maps. Both maps must have exactly the same keys.
(t TestingT, expected, actual interface{}, delta float64, msgAndArgs ...interface{})
| 1502 | |
| 1503 | // InDeltaMapValues is the same as InDelta, but it compares all values between two maps. Both maps must have exactly the same keys. |
| 1504 | func InDeltaMapValues(t TestingT, expected, actual interface{}, delta float64, msgAndArgs ...interface{}) bool { |
| 1505 | if h, ok := t.(tHelper); ok { |
| 1506 | h.Helper() |
| 1507 | } |
| 1508 | if expected == nil || actual == nil || |
| 1509 | reflect.TypeOf(actual).Kind() != reflect.Map || |
| 1510 | reflect.TypeOf(expected).Kind() != reflect.Map { |
| 1511 | return Fail(t, "Arguments must be maps", msgAndArgs...) |
| 1512 | } |
| 1513 | |
| 1514 | expectedMap := reflect.ValueOf(expected) |
| 1515 | actualMap := reflect.ValueOf(actual) |
| 1516 | |
| 1517 | if expectedMap.Len() != actualMap.Len() { |
| 1518 | return Fail(t, "Arguments must have the same number of keys", msgAndArgs...) |
| 1519 | } |
| 1520 | |
| 1521 | for _, k := range expectedMap.MapKeys() { |
| 1522 | ev := expectedMap.MapIndex(k) |
| 1523 | av := actualMap.MapIndex(k) |
| 1524 | |
| 1525 | if !ev.IsValid() { |
| 1526 | return Fail(t, fmt.Sprintf("missing key %q in expected map", k), msgAndArgs...) |
| 1527 | } |
| 1528 | |
| 1529 | if !av.IsValid() { |
| 1530 | return Fail(t, fmt.Sprintf("missing key %q in actual map", k), msgAndArgs...) |
| 1531 | } |
| 1532 | |
| 1533 | if !InDelta( |
| 1534 | t, |
| 1535 | ev.Interface(), |
| 1536 | av.Interface(), |
| 1537 | delta, |
| 1538 | msgAndArgs..., |
| 1539 | ) { |
| 1540 | return false |
| 1541 | } |
| 1542 | } |
| 1543 | |
| 1544 | return true |
| 1545 | } |
| 1546 | |
| 1547 | func calcRelativeError(expected, actual interface{}) (float64, error) { |
| 1548 | af, aok := toFloat(expected) |