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{})
| 1450 | |
| 1451 | // InDeltaMapValues is the same as InDelta, but it compares all values between two maps. Both maps must have exactly the same keys. |
| 1452 | func InDeltaMapValues(t TestingT, expected, actual interface{}, delta float64, msgAndArgs ...interface{}) bool { |
| 1453 | if h, ok := t.(tHelper); ok { |
| 1454 | h.Helper() |
| 1455 | } |
| 1456 | if expected == nil || actual == nil || |
| 1457 | reflect.TypeOf(actual).Kind() != reflect.Map || |
| 1458 | reflect.TypeOf(expected).Kind() != reflect.Map { |
| 1459 | return Fail(t, "Arguments must be maps", msgAndArgs...) |
| 1460 | } |
| 1461 | |
| 1462 | expectedMap := reflect.ValueOf(expected) |
| 1463 | actualMap := reflect.ValueOf(actual) |
| 1464 | |
| 1465 | if expectedMap.Len() != actualMap.Len() { |
| 1466 | return Fail(t, "Arguments must have the same number of keys", msgAndArgs...) |
| 1467 | } |
| 1468 | |
| 1469 | for _, k := range expectedMap.MapKeys() { |
| 1470 | ev := expectedMap.MapIndex(k) |
| 1471 | av := actualMap.MapIndex(k) |
| 1472 | |
| 1473 | if !ev.IsValid() { |
| 1474 | return Fail(t, fmt.Sprintf("missing key %q in expected map", k), msgAndArgs...) |
| 1475 | } |
| 1476 | |
| 1477 | if !av.IsValid() { |
| 1478 | return Fail(t, fmt.Sprintf("missing key %q in actual map", k), msgAndArgs...) |
| 1479 | } |
| 1480 | |
| 1481 | if !InDelta( |
| 1482 | t, |
| 1483 | ev.Interface(), |
| 1484 | av.Interface(), |
| 1485 | delta, |
| 1486 | msgAndArgs..., |
| 1487 | ) { |
| 1488 | return false |
| 1489 | } |
| 1490 | } |
| 1491 | |
| 1492 | return true |
| 1493 | } |
| 1494 | |
| 1495 | func calcRelativeError(expected, actual interface{}) (float64, error) { |
| 1496 | af, aok := toFloat(expected) |