github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/dag/tarjan_test.go (about) 1 package dag 2 3 import ( 4 "sort" 5 "strings" 6 "testing" 7 ) 8 9 func TestGraphStronglyConnected(t *testing.T) { 10 var g Graph 11 g.Add(1) 12 g.Add(2) 13 g.Connect(BasicEdge(1, 2)) 14 g.Connect(BasicEdge(2, 1)) 15 16 actual := strings.TrimSpace(testSCCStr(StronglyConnected(&g))) 17 expected := strings.TrimSpace(testGraphStronglyConnectedStr) 18 if actual != expected { 19 t.Fatalf("bad: %s", actual) 20 } 21 } 22 23 func TestGraphStronglyConnected_two(t *testing.T) { 24 var g Graph 25 g.Add(1) 26 g.Add(2) 27 g.Connect(BasicEdge(1, 2)) 28 g.Connect(BasicEdge(2, 1)) 29 g.Add(3) 30 31 actual := strings.TrimSpace(testSCCStr(StronglyConnected(&g))) 32 expected := strings.TrimSpace(testGraphStronglyConnectedTwoStr) 33 if actual != expected { 34 t.Fatalf("bad: %s", actual) 35 } 36 } 37 38 func TestGraphStronglyConnected_three(t *testing.T) { 39 var g Graph 40 g.Add(1) 41 g.Add(2) 42 g.Connect(BasicEdge(1, 2)) 43 g.Connect(BasicEdge(2, 1)) 44 g.Add(3) 45 g.Add(4) 46 g.Add(5) 47 g.Add(6) 48 g.Connect(BasicEdge(4, 5)) 49 g.Connect(BasicEdge(5, 6)) 50 g.Connect(BasicEdge(6, 4)) 51 52 actual := strings.TrimSpace(testSCCStr(StronglyConnected(&g))) 53 expected := strings.TrimSpace(testGraphStronglyConnectedThreeStr) 54 if actual != expected { 55 t.Fatalf("bad: %s", actual) 56 } 57 } 58 59 func testSCCStr(list [][]Vertex) string { 60 var lines []string 61 for _, vs := range list { 62 result := make([]string, len(vs)) 63 for i, v := range vs { 64 result[i] = VertexName(v) 65 } 66 67 sort.Strings(result) 68 lines = append(lines, strings.Join(result, ",")) 69 } 70 71 sort.Strings(lines) 72 return strings.Join(lines, "\n") 73 } 74 75 const testGraphStronglyConnectedStr = `1,2` 76 77 const testGraphStronglyConnectedTwoStr = ` 78 1,2 79 3 80 ` 81 82 const testGraphStronglyConnectedThreeStr = ` 83 1,2 84 3 85 4,5,6 86 `