-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_border_segments_test.go
More file actions
82 lines (76 loc) · 2.85 KB
/
Copy pathadd_border_segments_test.go
File metadata and controls
82 lines (76 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package dagro
import (
"reflect"
"testing"
)
func TestAddBorderSegmentsNoClusters(t *testing.T) {
for _, g := range []*Graph{
NewGraph().SetNode("a", Attrs{"rank": float64(0)}),
NewGraph(GraphOptions{Compound: true}).SetNode("a", Attrs{"rank": float64(0)}),
} {
addBorderSegments(g)
if g.NodeCount() != 1 || !reflect.DeepEqual(g.Node("a"), Attrs{"rank": float64(0)}) {
t.Fatalf("border nodes added without cluster: nodes=%v a=%#v", g.Nodes(), g.Node("a"))
}
}
}
func checkSubgraphBorder(t *testing.T, g *Graph, subgraph, id, borderType string, rank float64) {
t.Helper()
want := Attrs{
"dummy": "border", "borderType": borderType,
"rank": rank, "width": float64(0), "height": float64(0),
}
if got := asAttrs(g.Node(id)); !reflect.DeepEqual(got, want) {
t.Fatalf("border %s = %#v, want %#v", id, got, want)
}
if parent, ok := g.Parent(id); !ok || parent != subgraph {
t.Fatalf("Parent(%s) = %q, %v, want %q", id, parent, ok, subgraph)
}
}
func TestAddBorderSegmentsSingleAndMultipleRanks(t *testing.T) {
t.Run("single rank", func(t *testing.T) {
g := NewGraph(GraphOptions{Compound: true}).
SetNode("sg", Attrs{"minRank": float64(1), "maxRank": float64(1)})
addBorderSegments(g)
sg := asAttrs(g.Node("sg"))
left := sg["borderLeft"].(map[int]string)[1]
right := sg["borderRight"].(map[int]string)[1]
checkSubgraphBorder(t, g, "sg", left, "borderLeft", 1)
checkSubgraphBorder(t, g, "sg", right, "borderRight", 1)
})
t.Run("multiple ranks", func(t *testing.T) {
g := NewGraph(GraphOptions{Compound: true}).
SetNode("sg", Attrs{"minRank": float64(1), "maxRank": float64(2)})
addBorderSegments(g)
sg := asAttrs(g.Node("sg"))
left := sg["borderLeft"].(map[int]string)
right := sg["borderRight"].(map[int]string)
for rank := 1; rank <= 2; rank++ {
checkSubgraphBorder(t, g, "sg", left[rank], "borderLeft", float64(rank))
checkSubgraphBorder(t, g, "sg", right[rank], "borderRight", float64(rank))
}
if !g.HasEdge(left[1], left[2]) || !g.HasEdge(right[1], right[2]) {
t.Fatalf("border chains missing: edges=%#v", g.Edges())
}
if num(asAttrs(g.EdgeByArgs(left[1], left[2])), "weight") != 1 ||
num(asAttrs(g.EdgeByArgs(right[1], right[2])), "weight") != 1 {
t.Fatalf("border chain weights wrong")
}
})
}
func TestAddBorderSegmentsNestedSubgraphs(t *testing.T) {
g := NewGraph(GraphOptions{Compound: true}).
SetNode("sg1", Attrs{"minRank": float64(1), "maxRank": float64(1)}).
SetNode("sg2", Attrs{"minRank": float64(1), "maxRank": float64(1)})
if err := g.SetParent("sg2", "sg1"); err != nil {
t.Fatal(err)
}
addBorderSegments(g)
for _, sgID := range []string{"sg1", "sg2"} {
sg := asAttrs(g.Node(sgID))
left := sg["borderLeft"].(map[int]string)[1]
right := sg["borderRight"].(map[int]string)[1]
checkSubgraphBorder(t, g, sgID, left, "borderLeft", 1)
checkSubgraphBorder(t, g, sgID, right, "borderRight", 1)
}
}