blob: 3d8d0cd3ae37b7b5db6755a0f247fd9435dcb294 [file] [log] [blame]
Abhay Kumara61c5222025-11-10 07:32:50 +00001// 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 file.
4
5// Package cmpopts provides common options for the cmp package.
6package cmpopts
7
8import (
9 "errors"
10 "fmt"
11 "math"
12 "reflect"
13 "time"
14
15 "github.com/google/go-cmp/cmp"
16)
17
18func equateAlways(_, _ interface{}) bool { return true }
19
20// EquateEmpty returns a [cmp.Comparer] option that determines all maps and slices
21// with a length of zero to be equal, regardless of whether they are nil.
22//
23// EquateEmpty can be used in conjunction with [SortSlices] and [SortMaps].
24func EquateEmpty() cmp.Option {
25 return cmp.FilterValues(isEmpty, cmp.Comparer(equateAlways))
26}
27
28func isEmpty(x, y interface{}) bool {
29 vx, vy := reflect.ValueOf(x), reflect.ValueOf(y)
30 return (x != nil && y != nil && vx.Type() == vy.Type()) &&
31 (vx.Kind() == reflect.Slice || vx.Kind() == reflect.Map) &&
32 (vx.Len() == 0 && vy.Len() == 0)
33}
34
35// EquateApprox returns a [cmp.Comparer] option that determines float32 or float64
36// values to be equal if they are within a relative fraction or absolute margin.
37// This option is not used when either x or y is NaN or infinite.
38//
39// The fraction determines that the difference of two values must be within the
40// smaller fraction of the two values, while the margin determines that the two
41// values must be within some absolute margin.
42// To express only a fraction or only a margin, use 0 for the other parameter.
43// The fraction and margin must be non-negative.
44//
45// The mathematical expression used is equivalent to:
46//
47// |x-y| ≤ max(fraction*min(|x|, |y|), margin)
48//
49// EquateApprox can be used in conjunction with [EquateNaNs].
50func EquateApprox(fraction, margin float64) cmp.Option {
51 if margin < 0 || fraction < 0 || math.IsNaN(margin) || math.IsNaN(fraction) {
52 panic("margin or fraction must be a non-negative number")
53 }
54 a := approximator{fraction, margin}
55 return cmp.Options{
56 cmp.FilterValues(areRealF64s, cmp.Comparer(a.compareF64)),
57 cmp.FilterValues(areRealF32s, cmp.Comparer(a.compareF32)),
58 }
59}
60
61type approximator struct{ frac, marg float64 }
62
63func areRealF64s(x, y float64) bool {
64 return !math.IsNaN(x) && !math.IsNaN(y) && !math.IsInf(x, 0) && !math.IsInf(y, 0)
65}
66func areRealF32s(x, y float32) bool {
67 return areRealF64s(float64(x), float64(y))
68}
69func (a approximator) compareF64(x, y float64) bool {
70 relMarg := a.frac * math.Min(math.Abs(x), math.Abs(y))
71 return math.Abs(x-y) <= math.Max(a.marg, relMarg)
72}
73func (a approximator) compareF32(x, y float32) bool {
74 return a.compareF64(float64(x), float64(y))
75}
76
77// EquateNaNs returns a [cmp.Comparer] option that determines float32 and float64
78// NaN values to be equal.
79//
80// EquateNaNs can be used in conjunction with [EquateApprox].
81func EquateNaNs() cmp.Option {
82 return cmp.Options{
83 cmp.FilterValues(areNaNsF64s, cmp.Comparer(equateAlways)),
84 cmp.FilterValues(areNaNsF32s, cmp.Comparer(equateAlways)),
85 }
86}
87
88func areNaNsF64s(x, y float64) bool {
89 return math.IsNaN(x) && math.IsNaN(y)
90}
91func areNaNsF32s(x, y float32) bool {
92 return areNaNsF64s(float64(x), float64(y))
93}
94
95// EquateApproxTime returns a [cmp.Comparer] option that determines two non-zero
96// [time.Time] values to be equal if they are within some margin of one another.
97// If both times have a monotonic clock reading, then the monotonic time
98// difference will be used. The margin must be non-negative.
99func EquateApproxTime(margin time.Duration) cmp.Option {
100 if margin < 0 {
101 panic("margin must be a non-negative number")
102 }
103 a := timeApproximator{margin}
104 return cmp.FilterValues(areNonZeroTimes, cmp.Comparer(a.compare))
105}
106
107func areNonZeroTimes(x, y time.Time) bool {
108 return !x.IsZero() && !y.IsZero()
109}
110
111type timeApproximator struct {
112 margin time.Duration
113}
114
115func (a timeApproximator) compare(x, y time.Time) bool {
116 // Avoid subtracting times to avoid overflow when the
117 // difference is larger than the largest representable duration.
118 if x.After(y) {
119 // Ensure x is always before y
120 x, y = y, x
121 }
122 // We're within the margin if x+margin >= y.
123 // Note: time.Time doesn't have AfterOrEqual method hence the negation.
124 return !x.Add(a.margin).Before(y)
125}
126
127// AnyError is an error that matches any non-nil error.
128var AnyError anyError
129
130type anyError struct{}
131
132func (anyError) Error() string { return "any error" }
133func (anyError) Is(err error) bool { return err != nil }
134
135// EquateErrors returns a [cmp.Comparer] option that determines errors to be equal
136// if [errors.Is] reports them to match. The [AnyError] error can be used to
137// match any non-nil error.
138func EquateErrors() cmp.Option {
139 return cmp.FilterValues(areConcreteErrors, cmp.Comparer(compareErrors))
140}
141
142// areConcreteErrors reports whether x and y are types that implement error.
143// The input types are deliberately of the interface{} type rather than the
144// error type so that we can handle situations where the current type is an
145// interface{}, but the underlying concrete types both happen to implement
146// the error interface.
147func areConcreteErrors(x, y interface{}) bool {
148 _, ok1 := x.(error)
149 _, ok2 := y.(error)
150 return ok1 && ok2
151}
152
153func compareErrors(x, y interface{}) bool {
154 xe := x.(error)
155 ye := y.(error)
156 return errors.Is(xe, ye) || errors.Is(ye, xe)
157}
158
159// EquateComparable returns a [cmp.Option] that determines equality
160// of comparable types by directly comparing them using the == operator in Go.
161// The types to compare are specified by passing a value of that type.
162// This option should only be used on types that are documented as being
163// safe for direct == comparison. For example, [net/netip.Addr] is documented
164// as being semantically safe to use with ==, while [time.Time] is documented
165// to discourage the use of == on time values.
166func EquateComparable(typs ...interface{}) cmp.Option {
167 types := make(typesFilter)
168 for _, typ := range typs {
169 switch t := reflect.TypeOf(typ); {
170 case !t.Comparable():
171 panic(fmt.Sprintf("%T is not a comparable Go type", typ))
172 case types[t]:
173 panic(fmt.Sprintf("%T is already specified", typ))
174 default:
175 types[t] = true
176 }
177 }
178 return cmp.FilterPath(types.filter, cmp.Comparer(equateAny))
179}
180
181type typesFilter map[reflect.Type]bool
182
183func (tf typesFilter) filter(p cmp.Path) bool { return tf[p.Last().Type()] }
184
185func equateAny(x, y interface{}) bool { return x == y }