dag: Replace with the same values is a no-op

This commit is contained in:
Mitchell Hashimoto 2015-05-07 11:22:07 -07:00
parent ddad945717
commit caef7769ae
2 changed files with 29 additions and 0 deletions

View File

@ -82,6 +82,11 @@ func (g *Graph) Replace(original, replacement Vertex) bool {
return false
}
// If they're the same, then don't do anything
if original == replacement {
return true
}
// Add our new vertex, then copy all the edges
g.Add(replacement)
for _, target := range g.DownEdges(original).List() {

View File

@ -63,6 +63,22 @@ func TestGraph_replace(t *testing.T) {
}
}
func TestGraph_replaceSelf(t *testing.T) {
var g Graph
g.Add(1)
g.Add(2)
g.Add(3)
g.Connect(BasicEdge(1, 2))
g.Connect(BasicEdge(2, 3))
g.Replace(2, 2)
actual := strings.TrimSpace(g.String())
expected := strings.TrimSpace(testGraphReplaceSelfStr)
if actual != expected {
t.Fatalf("bad: %s", actual)
}
}
const testGraphBasicStr = `
1
3
@ -88,3 +104,11 @@ const testGraphReplaceStr = `
42
3
`
const testGraphReplaceSelfStr = `
1
2
2
3
3
`