diff options
author | Joe Tsai <joetsai@digital-static.net> | 2019-12-16 13:18:14 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-12-16 13:18:14 -0800 |
commit | 5a6f75716e1203a923a78c9efb94089d857df0f6 (patch) | |
tree | 19a1b334d759761879443b465761a2337075f3ad /cmp/options.go | |
parent | 340f1ebe299ef6712c79da23ad5bc6e3efad8250 (diff) | |
download | go-cmp-5a6f75716e1203a923a78c9efb94089d857df0f6.tar.gz |
Add support for comparing graphs (#85)
Previously, trying to call Equal on a graph would result in a stack-overflow
due to infinite recursion traversing cycles on a graph.
While a vast majority of Go values are trees or acyclic graphs, there exist
a small number of cases where graph equality is required.
As such, we add cycle detection to Equal and define what it means for two graphs
to be equal. Contrary to reflect.DeepEqual, which declares two graphs to be
equal so long any cycle were encountered, we require two graphs to have
equivalent graph structures.
Mathematically speaking, a graph G is a tuple (V, E) consisting of the set of
vertices and edges in that graph. Graphs G1 and G2 are equal if V1 == V2,
E1 == E2, and both have the same root vertex (entry point into the graph).
When traversing G1 and G2, we remember a stack of previously visited edges
ES1 and ES2. If the current edge e1 is in ES1 or e2 is in ES2, then we know that
a cycle exists. The graphs have the same structure when the previously
encountered edge ep1 and ep2 were encountered together.
Note that edges and vertices unreachable from the root vertex are ignored.
Appreciation goes to Eyal Posener (@posener), who proposed a different
(but semantically equivalent) approach in #79, which served as inspiration.
Fixes #74
Diffstat (limited to 'cmp/options.go')
-rw-r--r-- | cmp/options.go | 6 |
1 files changed, 6 insertions, 0 deletions
diff --git a/cmp/options.go b/cmp/options.go index 409e803..abbd2a6 100644 --- a/cmp/options.go +++ b/cmp/options.go @@ -455,6 +455,11 @@ func (r Result) ByFunc() bool { return r.flags&reportByFunc != 0 } +// ByCycle reports whether a reference cycle was detected. +func (r Result) ByCycle() bool { + return r.flags&reportByCycle != 0 +} + type resultFlags uint const ( @@ -465,6 +470,7 @@ const ( reportByIgnore reportByMethod reportByFunc + reportByCycle ) // Reporter is an Option that can be passed to Equal. When Equal traverses |