diff options
author | Joe Tsai <joetsai@digital-static.net> | 2017-09-28 10:41:56 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-09-28 10:41:56 -0700 |
commit | e25c8746f136c5d3731dba1f807b1e50106b3b55 (patch) | |
tree | 4da5833c5a97bb767e4adf2a9ecdd367cb2e5e7e /cmp/compare.go | |
parent | f46009a0a1e3526b07f548b2f80f73a4d2d32716 (diff) | |
download | go-cmp-e25c8746f136c5d3731dba1f807b1e50106b3b55.tar.gz |
Change diff.Difference to always return an edit-script (#45)
Rather than performing the heuristic for whether two slices are
"too different" in the diff package, just return the full edit-script
and move the heuristic logic into the cmp package.
The main adjustment to the heuristic is that we only print the
full slice if it is composed of a slice of primitive types (e.g., []byte)
and the difference between the two slices is sufficiently great enough.
Fixes #44
Diffstat (limited to 'cmp/compare.go')
-rw-r--r-- | cmp/compare.go | 16 |
1 files changed, 12 insertions, 4 deletions
diff --git a/cmp/compare.go b/cmp/compare.go index 2980d74..eb84d01 100644 --- a/cmp/compare.go +++ b/cmp/compare.go @@ -377,15 +377,23 @@ func (s *state) compareArray(vx, vy reflect.Value, t reflect.Type) { s.curPath.push(step) // Compute an edit-script for slices vx and vy. - eq, es := diff.Difference(vx.Len(), vy.Len(), func(ix, iy int) diff.Result { + es := diff.Difference(vx.Len(), vy.Len(), func(ix, iy int) diff.Result { step.xkey, step.ykey = ix, iy return s.statelessCompare(vx.Index(ix), vy.Index(iy)) }) - // Equal or no edit-script, so report entire slices as is. - if eq || es == nil { + // Report the entire slice as is if the arrays are of primitive kind, + // and the arrays are different enough. + isPrimitive := false + switch t.Elem().Kind() { + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, + reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr, + reflect.Bool, reflect.Float32, reflect.Float64, reflect.Complex64, reflect.Complex128: + isPrimitive = true + } + if isPrimitive && es.Dist() > (vx.Len()+vy.Len())/4 { s.curPath.pop() // Pop first since we are reporting the whole slice - s.report(eq, vx, vy) + s.report(false, vx, vy) return } |