pgraph: Add a GraphCmp method

This could probably be more efficient using a known algorithm, and it
could definitely require more tests, but is good enough for now.
This commit is contained in:
James Shubin
2017-05-30 17:28:22 -04:00
parent f65c5fb147
commit 51369adad1
3 changed files with 137 additions and 18 deletions

View File

@@ -18,6 +18,7 @@
package pgraph
import (
"fmt"
"reflect"
"testing"
)
@@ -657,3 +658,38 @@ func TestPgraphDelete1(t *testing.T) {
t.Errorf("should have 0 vertices instead of: %d", i)
}
}
func vertexCmpFn(v1, v2 Vertex) (bool, error) {
if v1.String() == "" || v2.String() == "" {
return false, fmt.Errorf("oops, empty vertex")
}
return v1.String() == v2.String(), nil
}
func edgeCmpFn(e1, e2 Edge) (bool, error) {
if e1.String() == "" || e2.String() == "" {
return false, fmt.Errorf("oops, empty edge")
}
return e1.String() == e2.String(), nil
}
func TestPgraphGraphCmp1(t *testing.T) {
g1 := &Graph{}
g2 := &Graph{}
g3 := &Graph{}
g3.AddVertex(NV("v1"))
g4 := &Graph{}
g4.AddVertex(NV("v2"))
if err := g1.GraphCmp(g2, vertexCmpFn, edgeCmpFn); err != nil {
t.Errorf("should have no error during GraphCmp, but got: %v", err)
}
if err := g1.GraphCmp(g3, vertexCmpFn, edgeCmpFn); err == nil {
t.Errorf("should have error during GraphCmp, but got nil")
}
if err := g3.GraphCmp(g4, vertexCmpFn, edgeCmpFn); err == nil {
t.Errorf("should have error during GraphCmp, but got nil")
}
}