Repository navigation
Expand file tree
/
Copy pathortho_test.go
More file actions
113 lines (105 loc) · 3.75 KB
/
Copy pathortho_test.go
File metadata and controls
113 lines (105 loc) · 3.75 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package layout
import (
"slices"
"testing"
)
// TestReversedEdgeRunsOnItsOwnChain checks that an edge reversed to
// break a cycle keeps a chain of its own instead of merging into the
// edge the other way, so that it can leave and enter at its own ends.
func TestReversedEdgeRunsOnItsOwnChain(t *testing.T) {
def := NewDigraph()
def.Edge("a", "b").Label = "down"
def.Edge("b", "a").Label = "up"
c := newHierComponent(newWorkGraph(def), Options{})
c.build()
c.rank()
down, _ := c.hierEdge(c.graphdef.Edges[0])
up, reversed := c.hierEdge(c.graphdef.Edges[1])
if !reversed || down == up || len(up.Chain) == 0 || up.Chain[0] == down.Chain[0] {
t.Errorf("reversed edge shares the chain of the other: %v %v", down.Chain, up.Chain)
}
}
func TestSeparate(t *testing.T) {
for _, tc := range []struct {
want []Length
weight []float64
out []Length
}{
{[]Length{0, 10, 20}, []float64{1, 1, 1}, []Length{0, 10, 20}},
{[]Length{0, 0}, []float64{1, 1}, []Length{-5, 5}},
{[]Length{0, 0}, []float64{1, 3}, []Length{-7.5, 2.5}},
{[]Length{-5, 0, 5}, []float64{1, 1, 1}, []Length{-10, 0, 10}},
{[]Length{90, 100}, []float64{1, 1}, []Length{85, 95}}, // clamped to hi
} {
got := separate(nil, tc.want, tc.weight, 10, -100, 95)
if !slices.Equal(got, tc.out) {
t.Errorf("separate(%v, %v) = %v, want %v", tc.want, tc.weight, got, tc.out)
}
}
}
// TestOrthoTrackCycle routes three edges between two ranks that each enter
// the channel where another leaves it, around a cycle, so that no order of
// single tracks keeps their vertical runs apart: one of them must step
// over on a second track.
func TestOrthoTrackCycle(t *testing.T) {
def := NewDigraph()
at := map[string]Vector{"a": {216, 0}, "b": {408, 0}, "c": {344, 0}, "x": {408, 100}, "y": {344, 100}, "z": {216, 100}}
for _, e := range [][2]string{{"a", "x"}, {"b", "y"}, {"c", "z"}} {
def.Edge(e[0], e[1])
}
graph := newWorkGraph(def)
for _, node := range graph.Nodes {
node.Center, node.Radius = at[node.ID], Vector{10, 10}
node.Shape = Box
}
for _, edge := range graph.Edges {
edge.Path = []Vector{edge.From.BottomCenter(), edge.To.TopCenter()}
}
rows := [][2]Length{{-10, 10}, {90, 110}}
orthoEdges(graph, rows, 10, false)
// vertical runs of different edges at one x must not overlap
type run struct {
edge *ledge
x Length
y0, y1 Length
}
var runs []run
for _, edge := range graph.Edges {
for i := 0; i+1 < len(edge.Path); i++ {
a, b := edge.Path[i], edge.Path[i+1]
if a.X == b.X && a.Y != b.Y {
runs = append(runs, run{edge, a.X, min(a.Y, b.Y), max(a.Y, b.Y)})
}
}
}
for i, a := range runs {
for _, b := range runs[i+1:] {
if a.edge != b.edge && (a.x-b.x).Abs() < 5 && a.y0 < b.y1-0.5 && b.y0 < a.y1-0.5 {
t.Errorf("%v and %v run down x=%v together, %v-%v and %v-%v", a.edge, b.edge, a.x, a.y0, a.y1, b.y0, b.y1)
}
}
}
}
func TestDetour(t *testing.T) {
for _, tc := range []struct {
path Path
want Length
}{
{Path{{0, 0}, {0, 50}, {100, 50}, {100, 100}}, 0}, // jog towards the end
{Path{{0, 0}, {0, -20}, {100, -20}, {100, 0}}, 2}, // arc over to a neighbor
{Path{{0, 0}, {0, 50}, {-30, 50}, {-30, 80}, {0, 80}, {0, 100}}, 8}, // out and back
{Path{{0, 0}, {0, 150}, {0, 100}}, 50}, // past the end and back
} {
if got := detour(tc.path); got < tc.want-1 || got > tc.want+1 {
t.Errorf("detour(%v) = %v, want about %v", tc.path, got, tc.want)
}
}
}
func TestSteeperCrossingPastPeak(t *testing.T) {
// segments heading the same way cross steepest at a moderate gap and
// flatten again beyond it, so hi alone says nothing about reaching it
got := steeperCrossing(-150, 40, -42, 40, 0, 128)
if got <= 0 || got >= 128 {
t.Errorf("got %v, want a move between 0 and 128", got)
}
}