Home | History | Annotate | Download | only in cmpopts
      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 
     12 	"github.com/google/go-cmp/cmp"
     13 )
     14 
     15 func equateAlways(_, _ interface{}) bool { return true }
     16 
     17 // EquateEmpty returns a Comparer option that determines all maps and slices
     18 // with a length of zero to be equal, regardless of whether they are nil.
     19 //
     20 // EquateEmpty can be used in conjuction with SortSlices and SortMaps.
     21 func EquateEmpty() cmp.Option {
     22 	return cmp.FilterValues(isEmpty, cmp.Comparer(equateAlways))
     23 }
     24 
     25 func isEmpty(x, y interface{}) bool {
     26 	vx, vy := reflect.ValueOf(x), reflect.ValueOf(y)
     27 	return (x != nil && y != nil && vx.Type() == vy.Type()) &&
     28 		(vx.Kind() == reflect.Slice || vx.Kind() == reflect.Map) &&
     29 		(vx.Len() == 0 && vy.Len() == 0)
     30 }
     31 
     32 // EquateApprox returns a Comparer option that determines float32 or float64
     33 // values to be equal if they are within a relative fraction or absolute margin.
     34 // This option is not used when either x or y is NaN or infinite.
     35 //
     36 // The fraction determines that the difference of two values must be within the
     37 // smaller fraction of the two values, while the margin determines that the two
     38 // values must be within some absolute margin.
     39 // To express only a fraction or only a margin, use 0 for the other parameter.
     40 // The fraction and margin must be non-negative.
     41 //
     42 // The mathematical expression used is equivalent to:
     43 //	|x-y|  max(fraction*min(|x|, |y|), margin)
     44 //
     45 // EquateApprox can be used in conjuction with EquateNaNs.
     46 func EquateApprox(fraction, margin float64) cmp.Option {
     47 	if margin < 0 || fraction < 0 || math.IsNaN(margin) || math.IsNaN(fraction) {
     48 		panic("margin or fraction must be a non-negative number")
     49 	}
     50 	a := approximator{fraction, margin}
     51 	return cmp.Options{
     52 		cmp.FilterValues(areRealF64s, cmp.Comparer(a.compareF64)),
     53 		cmp.FilterValues(areRealF32s, cmp.Comparer(a.compareF32)),
     54 	}
     55 }
     56 
     57 type approximator struct{ frac, marg float64 }
     58 
     59 func areRealF64s(x, y float64) bool {
     60 	return !math.IsNaN(x) && !math.IsNaN(y) && !math.IsInf(x, 0) && !math.IsInf(y, 0)
     61 }
     62 func areRealF32s(x, y float32) bool {
     63 	return areRealF64s(float64(x), float64(y))
     64 }
     65 func (a approximator) compareF64(x, y float64) bool {
     66 	relMarg := a.frac * math.Min(math.Abs(x), math.Abs(y))
     67 	return math.Abs(x-y) <= math.Max(a.marg, relMarg)
     68 }
     69 func (a approximator) compareF32(x, y float32) bool {
     70 	return a.compareF64(float64(x), float64(y))
     71 }
     72 
     73 // EquateNaNs returns a Comparer option that determines float32 and float64
     74 // NaN values to be equal.
     75 //
     76 // EquateNaNs can be used in conjuction with EquateApprox.
     77 func EquateNaNs() cmp.Option {
     78 	return cmp.Options{
     79 		cmp.FilterValues(areNaNsF64s, cmp.Comparer(equateAlways)),
     80 		cmp.FilterValues(areNaNsF32s, cmp.Comparer(equateAlways)),
     81 	}
     82 }
     83 
     84 func areNaNsF64s(x, y float64) bool {
     85 	return math.IsNaN(x) && math.IsNaN(y)
     86 }
     87 func areNaNsF32s(x, y float32) bool {
     88 	return areNaNsF64s(float64(x), float64(y))
     89 }
     90