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 file. 4 5// Package cmpopts provides common options for the cmp package. 6package cmpopts 7 8import ( 9 "errors" 10 "math" 11 "reflect" 12 "time" 13 14 "github.com/google/go-cmp/cmp" 15) 16 17func 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. 23func EquateEmpty() cmp.Option { 24 return cmp.FilterValues(isEmpty, cmp.Comparer(equateAlways)) 25} 26 27func 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// 46// |x-y| ≤ max(fraction*min(|x|, |y|), margin) 47// 48// EquateApprox can be used in conjunction with EquateNaNs. 49func EquateApprox(fraction, margin float64) cmp.Option { 50 if margin < 0 || fraction < 0 || math.IsNaN(margin) || math.IsNaN(fraction) { 51 panic("margin or fraction must be a non-negative number") 52 } 53 a := approximator{fraction, margin} 54 return cmp.Options{ 55 cmp.FilterValues(areRealF64s, cmp.Comparer(a.compareF64)), 56 cmp.FilterValues(areRealF32s, cmp.Comparer(a.compareF32)), 57 } 58} 59 60type approximator struct{ frac, marg float64 } 61 62func areRealF64s(x, y float64) bool { 63 return !math.IsNaN(x) && !math.IsNaN(y) && !math.IsInf(x, 0) && !math.IsInf(y, 0) 64} 65func areRealF32s(x, y float32) bool { 66 return areRealF64s(float64(x), float64(y)) 67} 68func (a approximator) compareF64(x, y float64) bool { 69 relMarg := a.frac * math.Min(math.Abs(x), math.Abs(y)) 70 return math.Abs(x-y) <= math.Max(a.marg, relMarg) 71} 72func (a approximator) compareF32(x, y float32) bool { 73 return a.compareF64(float64(x), float64(y)) 74} 75 76// EquateNaNs returns a Comparer option that determines float32 and float64 77// NaN values to be equal. 78// 79// EquateNaNs can be used in conjunction with EquateApprox. 80func EquateNaNs() cmp.Option { 81 return cmp.Options{ 82 cmp.FilterValues(areNaNsF64s, cmp.Comparer(equateAlways)), 83 cmp.FilterValues(areNaNsF32s, cmp.Comparer(equateAlways)), 84 } 85} 86 87func areNaNsF64s(x, y float64) bool { 88 return math.IsNaN(x) && math.IsNaN(y) 89} 90func areNaNsF32s(x, y float32) bool { 91 return areNaNsF64s(float64(x), float64(y)) 92} 93 94// EquateApproxTime returns a Comparer option that determines two non-zero 95// time.Time values to be equal if they are within some margin of one another. 96// If both times have a monotonic clock reading, then the monotonic time 97// difference will be used. The margin must be non-negative. 98func EquateApproxTime(margin time.Duration) cmp.Option { 99 if margin < 0 { 100 panic("margin must be a non-negative number") 101 } 102 a := timeApproximator{margin} 103 return cmp.FilterValues(areNonZeroTimes, cmp.Comparer(a.compare)) 104} 105 106func areNonZeroTimes(x, y time.Time) bool { 107 return !x.IsZero() && !y.IsZero() 108} 109 110type timeApproximator struct { 111 margin time.Duration 112} 113 114func (a timeApproximator) compare(x, y time.Time) bool { 115 // Avoid subtracting times to avoid overflow when the 116 // difference is larger than the largest representable duration. 117 if x.After(y) { 118 // Ensure x is always before y 119 x, y = y, x 120 } 121 // We're within the margin if x+margin >= y. 122 // Note: time.Time doesn't have AfterOrEqual method hence the negation. 123 return !x.Add(a.margin).Before(y) 124} 125 126// AnyError is an error that matches any non-nil error. 127var AnyError anyError 128 129type anyError struct{} 130 131func (anyError) Error() string { return "any error" } 132func (anyError) Is(err error) bool { return err != nil } 133 134// EquateErrors returns a Comparer option that determines errors to be equal 135// if errors.Is reports them to match. The AnyError error can be used to 136// match any non-nil error. 137func EquateErrors() cmp.Option { 138 return cmp.FilterValues(areConcreteErrors, cmp.Comparer(compareErrors)) 139} 140 141// areConcreteErrors reports whether x and y are types that implement error. 142// The input types are deliberately of the interface{} type rather than the 143// error type so that we can handle situations where the current type is an 144// interface{}, but the underlying concrete types both happen to implement 145// the error interface. 146func areConcreteErrors(x, y interface{}) bool { 147 _, ok1 := x.(error) 148 _, ok2 := y.(error) 149 return ok1 && ok2 150} 151 152func compareErrors(x, y interface{}) bool { 153 xe := x.(error) 154 ye := y.(error) 155 return errors.Is(xe, ye) || errors.Is(ye, xe) 156} 157