This repository has been archived by the owner on Apr 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
/
undirect_test.go
126 lines (118 loc) · 3.33 KB
/
undirect_test.go
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
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright ©2015 The gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package graph_test
import (
"math"
"testing"
"github.com/gonum/graph"
"github.com/gonum/graph/simple"
"github.com/gonum/matrix/mat64"
)
var directedGraphs = []struct {
g func() graph.DirectedBuilder
edges []simple.Edge
absent float64
merge func(x, y float64, xe, ye graph.Edge) float64
want mat64.Matrix
}{
{
g: func() graph.DirectedBuilder { return simple.NewDirectedGraph(0, 0) },
edges: []simple.Edge{
{F: simple.Node(0), T: simple.Node(1), W: 2},
{F: simple.Node(1), T: simple.Node(0), W: 1},
{F: simple.Node(1), T: simple.Node(2), W: 1},
},
want: mat64.NewSymDense(3, []float64{
0, (1. + 2.) / 2., 0,
(1. + 2.) / 2., 0, 1. / 2.,
0, 1. / 2., 0,
}),
},
{
g: func() graph.DirectedBuilder { return simple.NewDirectedGraph(0, 0) },
edges: []simple.Edge{
{F: simple.Node(0), T: simple.Node(1), W: 2},
{F: simple.Node(1), T: simple.Node(0), W: 1},
{F: simple.Node(1), T: simple.Node(2), W: 1},
},
absent: 1,
merge: func(x, y float64, _, _ graph.Edge) float64 { return math.Sqrt(x * y) },
want: mat64.NewSymDense(3, []float64{
0, math.Sqrt(1 * 2), 0,
math.Sqrt(1 * 2), 0, math.Sqrt(1 * 1),
0, math.Sqrt(1 * 1), 0,
}),
},
{
g: func() graph.DirectedBuilder { return simple.NewDirectedGraph(0, 0) },
edges: []simple.Edge{
{F: simple.Node(0), T: simple.Node(1), W: 2},
{F: simple.Node(1), T: simple.Node(0), W: 1},
{F: simple.Node(1), T: simple.Node(2), W: 1},
},
merge: func(x, y float64, _, _ graph.Edge) float64 { return math.Min(x, y) },
want: mat64.NewSymDense(3, []float64{
0, math.Min(1, 2), 0,
math.Min(1, 2), 0, math.Min(1, 0),
0, math.Min(1, 0), 0,
}),
},
{
g: func() graph.DirectedBuilder { return simple.NewDirectedGraph(0, 0) },
edges: []simple.Edge{
{F: simple.Node(0), T: simple.Node(1), W: 2},
{F: simple.Node(1), T: simple.Node(0), W: 1},
{F: simple.Node(1), T: simple.Node(2), W: 1},
},
merge: func(x, y float64, xe, ye graph.Edge) float64 {
if xe == nil {
return y
}
if ye == nil {
return x
}
return math.Min(x, y)
},
want: mat64.NewSymDense(3, []float64{
0, math.Min(1, 2), 0,
math.Min(1, 2), 0, 1,
0, 1, 0,
}),
},
{
g: func() graph.DirectedBuilder { return simple.NewDirectedGraph(0, 0) },
edges: []simple.Edge{
{F: simple.Node(0), T: simple.Node(1), W: 2},
{F: simple.Node(1), T: simple.Node(0), W: 1},
{F: simple.Node(1), T: simple.Node(2), W: 1},
},
merge: func(x, y float64, _, _ graph.Edge) float64 { return math.Max(x, y) },
want: mat64.NewSymDense(3, []float64{
0, math.Max(1, 2), 0,
math.Max(1, 2), 0, math.Max(1, 0),
0, math.Max(1, 0), 0,
}),
},
}
func TestUndirect(t *testing.T) {
for _, test := range directedGraphs {
g := test.g()
for _, e := range test.edges {
g.SetEdge(e)
}
src := graph.Undirect{G: g, Absent: test.absent, Merge: test.merge}
dst := simple.NewUndirectedMatrixFrom(src.Nodes(), 0, 0, 0)
for _, u := range src.Nodes() {
for _, v := range src.From(u) {
dst.SetEdge(src.Edge(u, v))
}
}
if !mat64.Equal(dst.Matrix(), test.want) {
t.Errorf("unexpected result:\ngot:\n%.4v\nwant:\n%.4v",
mat64.Formatted(dst.Matrix()),
mat64.Formatted(test.want),
)
}
}
}