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.
5 // Package cmpopts provides common options for the cmp package.
13 "github.com/google/go-cmp/cmp"
14 "golang.org/x/xerrors"
17 func equateAlways(_, _ interface{}) bool { return true }
19 // EquateEmpty returns a Comparer option that determines all maps and slices
20 // with a length of zero to be equal, regardless of whether they are nil.
22 // EquateEmpty can be used in conjunction with SortSlices and SortMaps.
23 func EquateEmpty() cmp.Option {
24 return cmp.FilterValues(isEmpty, cmp.Comparer(equateAlways))
27 func isEmpty(x, y interface{}) bool {
28 vx, vy := reflect.ValueOf(x), reflect.ValueOf(y)
29 return (x != nil && y != nil && vx.Type() == vy.Type()) &&
30 (vx.Kind() == reflect.Slice || vx.Kind() == reflect.Map) &&
31 (vx.Len() == 0 && vy.Len() == 0)
34 // EquateApprox returns a Comparer option that determines float32 or float64
35 // values to be equal if they are within a relative fraction or absolute margin.
36 // This option is not used when either x or y is NaN or infinite.
38 // The fraction determines that the difference of two values must be within the
39 // smaller fraction of the two values, while the margin determines that the two
40 // values must be within some absolute margin.
41 // To express only a fraction or only a margin, use 0 for the other parameter.
42 // The fraction and margin must be non-negative.
44 // The mathematical expression used is equivalent to:
45 // |x-y| ≤ max(fraction*min(|x|, |y|), margin)
47 // EquateApprox can be used in conjunction with EquateNaNs.
48 func EquateApprox(fraction, margin float64) cmp.Option {
49 if margin < 0 || fraction < 0 || math.IsNaN(margin) || math.IsNaN(fraction) {
50 panic("margin or fraction must be a non-negative number")
52 a := approximator{fraction, margin}
54 cmp.FilterValues(areRealF64s, cmp.Comparer(a.compareF64)),
55 cmp.FilterValues(areRealF32s, cmp.Comparer(a.compareF32)),
59 type approximator struct{ frac, marg float64 }
61 func areRealF64s(x, y float64) bool {
62 return !math.IsNaN(x) && !math.IsNaN(y) && !math.IsInf(x, 0) && !math.IsInf(y, 0)
64 func areRealF32s(x, y float32) bool {
65 return areRealF64s(float64(x), float64(y))
67 func (a approximator) compareF64(x, y float64) bool {
68 relMarg := a.frac * math.Min(math.Abs(x), math.Abs(y))
69 return math.Abs(x-y) <= math.Max(a.marg, relMarg)
71 func (a approximator) compareF32(x, y float32) bool {
72 return a.compareF64(float64(x), float64(y))
75 // EquateNaNs returns a Comparer option that determines float32 and float64
76 // NaN values to be equal.
78 // EquateNaNs can be used in conjunction with EquateApprox.
79 func EquateNaNs() cmp.Option {
81 cmp.FilterValues(areNaNsF64s, cmp.Comparer(equateAlways)),
82 cmp.FilterValues(areNaNsF32s, cmp.Comparer(equateAlways)),
86 func areNaNsF64s(x, y float64) bool {
87 return math.IsNaN(x) && math.IsNaN(y)
89 func areNaNsF32s(x, y float32) bool {
90 return areNaNsF64s(float64(x), float64(y))
93 // EquateApproxTime returns a Comparer option that determines two non-zero
94 // time.Time values to be equal if they are within some margin of one another.
95 // If both times have a monotonic clock reading, then the monotonic time
96 // difference will be used. The margin must be non-negative.
97 func EquateApproxTime(margin time.Duration) cmp.Option {
99 panic("margin must be a non-negative number")
101 a := timeApproximator{margin}
102 return cmp.FilterValues(areNonZeroTimes, cmp.Comparer(a.compare))
105 func areNonZeroTimes(x, y time.Time) bool {
106 return !x.IsZero() && !y.IsZero()
109 type timeApproximator struct {
113 func (a timeApproximator) compare(x, y time.Time) bool {
114 // Avoid subtracting times to avoid overflow when the
115 // difference is larger than the largest representible duration.
117 // Ensure x is always before y
120 // We're within the margin if x+margin >= y.
121 // Note: time.Time doesn't have AfterOrEqual method hence the negation.
122 return !x.Add(a.margin).Before(y)
125 // AnyError is an error that matches any non-nil error.
126 var AnyError anyError
128 type anyError struct{}
130 func (anyError) Error() string { return "any error" }
131 func (anyError) Is(err error) bool { return err != nil }
133 // EquateErrors returns a Comparer option that determines errors to be equal
134 // if errors.Is reports them to match. The AnyError error can be used to
135 // match any non-nil error.
136 func EquateErrors() cmp.Option {
137 return cmp.FilterValues(areConcreteErrors, cmp.Comparer(compareErrors))
140 // areConcreteErrors reports whether x and y are types that implement error.
141 // The input types are deliberately of the interface{} type rather than the
142 // error type so that we can handle situations where the current type is an
143 // interface{}, but the underlying concrete types both happen to implement
144 // the error interface.
145 func areConcreteErrors(x, y interface{}) bool {
151 func compareErrors(x, y interface{}) bool {
154 // TODO(≥go1.13): Use standard definition of errors.Is.
155 return xerrors.Is(xe, ye) || xerrors.Is(ye, xe)