Created
February 18, 2017 19:13
-
-
Save dgellow/91c2062157430f20bfb4a12102419cda to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Compare two hierarchy graphs, useful for tests. Traverse both | |
// graphs and calculate an md5 based on the employee ID. If | |
// compareName is true, use the employee name instead | |
func CompareGraphs(g1 *Node, g2 *Node, compareName ...bool) bool { | |
compName := false | |
if len(compareName) == 1 { | |
compName = compareName[0] | |
} | |
hash1 := md5.New() | |
hash2 := md5.New() | |
// Traverse first graph | |
traverseGraph(g1, nil, func(n *Node, parent *Node) bool { | |
s := n.Employee.ID | |
if compName { | |
s = n.Employee.Name | |
} | |
io.WriteString(hash1, s) | |
return true | |
}) | |
// Traverse second graph | |
traverseGraph(g2, nil, func(n *Node, parent *Node) bool { | |
s := n.Employee.ID | |
if compName { | |
s = n.Employee.Name | |
} | |
io.WriteString(hash2, s) | |
return true | |
}) | |
res1 := fmt.Sprintf("%x", hash1.Sum(nil)) | |
res2 := fmt.Sprintf("%x", hash2.Sum(nil)) | |
return res1 == res2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment