You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

90 lines
3.0 KiB

  1. // Copyright 2017, The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE.md file.
  4. // Package cmpopts provides common options for the cmp package.
  5. package cmpopts
  6. import (
  7. "math"
  8. "reflect"
  9. "github.com/google/go-cmp/cmp"
  10. )
  11. func equateAlways(_, _ interface{}) bool { return true }
  12. // EquateEmpty returns a Comparer option that determines all maps and slices
  13. // with a length of zero to be equal, regardless of whether they are nil.
  14. //
  15. // EquateEmpty can be used in conjunction with SortSlices and SortMaps.
  16. func EquateEmpty() cmp.Option {
  17. return cmp.FilterValues(isEmpty, cmp.Comparer(equateAlways))
  18. }
  19. func isEmpty(x, y interface{}) bool {
  20. vx, vy := reflect.ValueOf(x), reflect.ValueOf(y)
  21. return (x != nil && y != nil && vx.Type() == vy.Type()) &&
  22. (vx.Kind() == reflect.Slice || vx.Kind() == reflect.Map) &&
  23. (vx.Len() == 0 && vy.Len() == 0)
  24. }
  25. // EquateApprox returns a Comparer option that determines float32 or float64
  26. // values to be equal if they are within a relative fraction or absolute margin.
  27. // This option is not used when either x or y is NaN or infinite.
  28. //
  29. // The fraction determines that the difference of two values must be within the
  30. // smaller fraction of the two values, while the margin determines that the two
  31. // values must be within some absolute margin.
  32. // To express only a fraction or only a margin, use 0 for the other parameter.
  33. // The fraction and margin must be non-negative.
  34. //
  35. // The mathematical expression used is equivalent to:
  36. // |x-y| ≤ max(fraction*min(|x|, |y|), margin)
  37. //
  38. // EquateApprox can be used in conjunction with EquateNaNs.
  39. func EquateApprox(fraction, margin float64) cmp.Option {
  40. if margin < 0 || fraction < 0 || math.IsNaN(margin) || math.IsNaN(fraction) {
  41. panic("margin or fraction must be a non-negative number")
  42. }
  43. a := approximator{fraction, margin}
  44. return cmp.Options{
  45. cmp.FilterValues(areRealF64s, cmp.Comparer(a.compareF64)),
  46. cmp.FilterValues(areRealF32s, cmp.Comparer(a.compareF32)),
  47. }
  48. }
  49. type approximator struct{ frac, marg float64 }
  50. func areRealF64s(x, y float64) bool {
  51. return !math.IsNaN(x) && !math.IsNaN(y) && !math.IsInf(x, 0) && !math.IsInf(y, 0)
  52. }
  53. func areRealF32s(x, y float32) bool {
  54. return areRealF64s(float64(x), float64(y))
  55. }
  56. func (a approximator) compareF64(x, y float64) bool {
  57. relMarg := a.frac * math.Min(math.Abs(x), math.Abs(y))
  58. return math.Abs(x-y) <= math.Max(a.marg, relMarg)
  59. }
  60. func (a approximator) compareF32(x, y float32) bool {
  61. return a.compareF64(float64(x), float64(y))
  62. }
  63. // EquateNaNs returns a Comparer option that determines float32 and float64
  64. // NaN values to be equal.
  65. //
  66. // EquateNaNs can be used in conjunction with EquateApprox.
  67. func EquateNaNs() cmp.Option {
  68. return cmp.Options{
  69. cmp.FilterValues(areNaNsF64s, cmp.Comparer(equateAlways)),
  70. cmp.FilterValues(areNaNsF32s, cmp.Comparer(equateAlways)),
  71. }
  72. }
  73. func areNaNsF64s(x, y float64) bool {
  74. return math.IsNaN(x) && math.IsNaN(y)
  75. }
  76. func areNaNsF32s(x, y float32) bool {
  77. return areNaNsF64s(float64(x), float64(y))
  78. }