Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / github.com / google / go-cmp@v0.5.1 / cmp / cmpopts / equate.go
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
5 // Package cmpopts provides common options for the cmp package.
6 package cmpopts
7
8 import (
9         "math"
10         "reflect"
11         "time"
12
13         "github.com/google/go-cmp/cmp"
14         "golang.org/x/xerrors"
15 )
16
17 func equateAlways(_, _ interface{}) bool { return true }
18
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.
21 //
22 // EquateEmpty can be used in conjunction with SortSlices and SortMaps.
23 func EquateEmpty() cmp.Option {
24         return cmp.FilterValues(isEmpty, cmp.Comparer(equateAlways))
25 }
26
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)
32 }
33
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.
37 //
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.
43 //
44 // The mathematical expression used is equivalent to:
45 //      |x-y| ≤ max(fraction*min(|x|, |y|), margin)
46 //
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")
51         }
52         a := approximator{fraction, margin}
53         return cmp.Options{
54                 cmp.FilterValues(areRealF64s, cmp.Comparer(a.compareF64)),
55                 cmp.FilterValues(areRealF32s, cmp.Comparer(a.compareF32)),
56         }
57 }
58
59 type approximator struct{ frac, marg float64 }
60
61 func areRealF64s(x, y float64) bool {
62         return !math.IsNaN(x) && !math.IsNaN(y) && !math.IsInf(x, 0) && !math.IsInf(y, 0)
63 }
64 func areRealF32s(x, y float32) bool {
65         return areRealF64s(float64(x), float64(y))
66 }
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)
70 }
71 func (a approximator) compareF32(x, y float32) bool {
72         return a.compareF64(float64(x), float64(y))
73 }
74
75 // EquateNaNs returns a Comparer option that determines float32 and float64
76 // NaN values to be equal.
77 //
78 // EquateNaNs can be used in conjunction with EquateApprox.
79 func EquateNaNs() cmp.Option {
80         return cmp.Options{
81                 cmp.FilterValues(areNaNsF64s, cmp.Comparer(equateAlways)),
82                 cmp.FilterValues(areNaNsF32s, cmp.Comparer(equateAlways)),
83         }
84 }
85
86 func areNaNsF64s(x, y float64) bool {
87         return math.IsNaN(x) && math.IsNaN(y)
88 }
89 func areNaNsF32s(x, y float32) bool {
90         return areNaNsF64s(float64(x), float64(y))
91 }
92
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 {
98         if margin < 0 {
99                 panic("margin must be a non-negative number")
100         }
101         a := timeApproximator{margin}
102         return cmp.FilterValues(areNonZeroTimes, cmp.Comparer(a.compare))
103 }
104
105 func areNonZeroTimes(x, y time.Time) bool {
106         return !x.IsZero() && !y.IsZero()
107 }
108
109 type timeApproximator struct {
110         margin time.Duration
111 }
112
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.
116         if x.After(y) {
117                 // Ensure x is always before y
118                 x, y = y, x
119         }
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)
123 }
124
125 // AnyError is an error that matches any non-nil error.
126 var AnyError anyError
127
128 type anyError struct{}
129
130 func (anyError) Error() string     { return "any error" }
131 func (anyError) Is(err error) bool { return err != nil }
132
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))
138 }
139
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 {
146         _, ok1 := x.(error)
147         _, ok2 := y.(error)
148         return ok1 && ok2
149 }
150
151 func compareErrors(x, y interface{}) bool {
152         xe := x.(error)
153         ye := y.(error)
154         // TODO(≥go1.13): Use standard definition of errors.Is.
155         return xerrors.Is(xe, ye) || xerrors.Is(ye, xe)
156 }