github.com/gopherd/gonum@v0.0.4/graph/nodes_edges_test.go (about) 1 // Copyright ©2019 The Gonum Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package graph_test 6 7 import ( 8 "reflect" 9 "testing" 10 11 "github.com/gopherd/gonum/graph" 12 "github.com/gopherd/gonum/graph/iterator" 13 "github.com/gopherd/gonum/graph/multi" 14 "github.com/gopherd/gonum/graph/simple" 15 ) 16 17 // nodes 18 // edges 19 // weightededges 20 // lines 21 // weightedlines 22 // empty 23 24 var nodesOfTests = []struct { 25 name string 26 nodes graph.Nodes 27 want []graph.Node 28 }{ 29 { 30 name: "nil", 31 nodes: nil, 32 want: nil, 33 }, 34 { 35 name: "empty", 36 nodes: graph.Empty, 37 want: nil, 38 }, 39 { 40 name: "no nodes", 41 nodes: iterator.NewOrderedNodes(nil), 42 want: nil, 43 }, 44 { 45 name: "implicit nodes", 46 nodes: iterator.NewImplicitNodes(-1, 4, func(id int) graph.Node { return simple.Node(id) }), 47 want: []graph.Node{simple.Node(-1), simple.Node(0), simple.Node(1), simple.Node(2), simple.Node(3)}, 48 }, 49 { 50 name: "no slice method", 51 nodes: basicNodes{iterator.NewOrderedNodes([]graph.Node{simple.Node(-1), simple.Node(0), simple.Node(1), simple.Node(2), simple.Node(3)})}, 52 want: []graph.Node{simple.Node(-1), simple.Node(0), simple.Node(1), simple.Node(2), simple.Node(3)}, 53 }, 54 { 55 name: "explicit nodes", 56 nodes: iterator.NewOrderedNodes([]graph.Node{simple.Node(-1), simple.Node(0), simple.Node(1), simple.Node(2), simple.Node(3)}), 57 want: []graph.Node{simple.Node(-1), simple.Node(0), simple.Node(1), simple.Node(2), simple.Node(3)}, 58 }, 59 } 60 61 type basicNodes struct { 62 graph.Nodes 63 } 64 65 func TestNodesOf(t *testing.T) { 66 for _, test := range nodesOfTests { 67 got := graph.NodesOf(test.nodes) 68 if !reflect.DeepEqual(got, test.want) { 69 t.Errorf("unexpected result for %q: got:%v want:%v", test.name, got, test.want) 70 } 71 } 72 } 73 74 var edgesOfTests = []struct { 75 name string 76 edges graph.Edges 77 want []graph.Edge 78 }{ 79 { 80 name: "nil", 81 edges: nil, 82 want: nil, 83 }, 84 { 85 name: "empty", 86 edges: graph.Empty, 87 want: nil, 88 }, 89 { 90 name: "no edges", 91 edges: iterator.NewOrderedEdges(nil), 92 want: nil, 93 }, 94 { 95 name: "no slice method", 96 edges: basicEdges{iterator.NewOrderedEdges([]graph.Edge{ 97 simple.Edge{F: simple.Node(-1), T: simple.Node(0)}, 98 simple.Edge{F: simple.Node(1), T: simple.Node(2)}, 99 simple.Edge{F: simple.Node(3), T: simple.Node(4)}, 100 })}, 101 want: []graph.Edge{ 102 simple.Edge{F: simple.Node(-1), T: simple.Node(0)}, 103 simple.Edge{F: simple.Node(1), T: simple.Node(2)}, 104 simple.Edge{F: simple.Node(3), T: simple.Node(4)}, 105 }, 106 }, 107 { 108 name: "explicit edges", 109 edges: iterator.NewOrderedEdges([]graph.Edge{ 110 simple.Edge{F: simple.Node(-1), T: simple.Node(0)}, 111 simple.Edge{F: simple.Node(1), T: simple.Node(2)}, 112 simple.Edge{F: simple.Node(3), T: simple.Node(4)}, 113 }), 114 want: []graph.Edge{ 115 simple.Edge{F: simple.Node(-1), T: simple.Node(0)}, 116 simple.Edge{F: simple.Node(1), T: simple.Node(2)}, 117 simple.Edge{F: simple.Node(3), T: simple.Node(4)}, 118 }, 119 }, 120 } 121 122 type basicEdges struct { 123 graph.Edges 124 } 125 126 func TestEdgesOf(t *testing.T) { 127 for _, test := range edgesOfTests { 128 got := graph.EdgesOf(test.edges) 129 if !reflect.DeepEqual(got, test.want) { 130 t.Errorf("unexpected result for %q: got:%v want:%v", test.name, got, test.want) 131 } 132 } 133 } 134 135 var weightedEdgesOfTests = []struct { 136 name string 137 edges graph.WeightedEdges 138 want []graph.WeightedEdge 139 }{ 140 { 141 name: "nil", 142 edges: nil, 143 want: nil, 144 }, 145 { 146 name: "empty", 147 edges: graph.Empty, 148 want: nil, 149 }, 150 { 151 name: "no edges", 152 edges: iterator.NewOrderedWeightedEdges(nil), 153 want: nil, 154 }, 155 { 156 name: "no slice method", 157 edges: basicWeightedEdges{iterator.NewOrderedWeightedEdges([]graph.WeightedEdge{ 158 simple.WeightedEdge{F: simple.Node(-1), T: simple.Node(0), W: 1}, 159 simple.WeightedEdge{F: simple.Node(1), T: simple.Node(2), W: 2}, 160 simple.WeightedEdge{F: simple.Node(3), T: simple.Node(4), W: 3}, 161 })}, 162 want: []graph.WeightedEdge{ 163 simple.WeightedEdge{F: simple.Node(-1), T: simple.Node(0), W: 1}, 164 simple.WeightedEdge{F: simple.Node(1), T: simple.Node(2), W: 2}, 165 simple.WeightedEdge{F: simple.Node(3), T: simple.Node(4), W: 3}, 166 }, 167 }, 168 { 169 name: "explicit edges", 170 edges: iterator.NewOrderedWeightedEdges([]graph.WeightedEdge{ 171 simple.WeightedEdge{F: simple.Node(-1), T: simple.Node(0), W: 1}, 172 simple.WeightedEdge{F: simple.Node(1), T: simple.Node(2), W: 2}, 173 simple.WeightedEdge{F: simple.Node(3), T: simple.Node(4), W: 3}, 174 }), 175 want: []graph.WeightedEdge{ 176 simple.WeightedEdge{F: simple.Node(-1), T: simple.Node(0), W: 1}, 177 simple.WeightedEdge{F: simple.Node(1), T: simple.Node(2), W: 2}, 178 simple.WeightedEdge{F: simple.Node(3), T: simple.Node(4), W: 3}, 179 }, 180 }, 181 } 182 183 type basicWeightedEdges struct { 184 graph.WeightedEdges 185 } 186 187 func TestWeightedEdgesOf(t *testing.T) { 188 for _, test := range weightedEdgesOfTests { 189 got := graph.WeightedEdgesOf(test.edges) 190 if !reflect.DeepEqual(got, test.want) { 191 t.Errorf("unexpected result for %q: got:%v want:%v", test.name, got, test.want) 192 } 193 } 194 } 195 196 var linesOfTests = []struct { 197 name string 198 lines graph.Lines 199 want []graph.Line 200 }{ 201 { 202 name: "nil", 203 lines: nil, 204 want: nil, 205 }, 206 { 207 name: "empty", 208 lines: graph.Empty, 209 want: nil, 210 }, 211 { 212 name: "no edges", 213 lines: iterator.NewOrderedLines(nil), 214 want: nil, 215 }, 216 { 217 name: "no slice method", 218 lines: basicLines{iterator.NewOrderedLines([]graph.Line{ 219 multi.Line{F: multi.Node(-1), T: multi.Node(0), UID: -1}, 220 multi.Line{F: multi.Node(1), T: multi.Node(2), UID: 0}, 221 multi.Line{F: multi.Node(3), T: multi.Node(4), UID: 1}, 222 })}, 223 want: []graph.Line{ 224 multi.Line{F: multi.Node(-1), T: multi.Node(0), UID: -1}, 225 multi.Line{F: multi.Node(1), T: multi.Node(2), UID: 0}, 226 multi.Line{F: multi.Node(3), T: multi.Node(4), UID: 1}, 227 }, 228 }, 229 { 230 name: "explicit edges", 231 lines: iterator.NewOrderedLines([]graph.Line{ 232 multi.Line{F: multi.Node(-1), T: multi.Node(0), UID: -1}, 233 multi.Line{F: multi.Node(1), T: multi.Node(2), UID: 0}, 234 multi.Line{F: multi.Node(3), T: multi.Node(4), UID: 1}, 235 }), 236 want: []graph.Line{ 237 multi.Line{F: multi.Node(-1), T: multi.Node(0), UID: -1}, 238 multi.Line{F: multi.Node(1), T: multi.Node(2), UID: 0}, 239 multi.Line{F: multi.Node(3), T: multi.Node(4), UID: 1}, 240 }, 241 }, 242 } 243 244 type basicLines struct { 245 graph.Lines 246 } 247 248 func TestLinesOf(t *testing.T) { 249 for _, test := range linesOfTests { 250 got := graph.LinesOf(test.lines) 251 if !reflect.DeepEqual(got, test.want) { 252 t.Errorf("unexpected result for %q: got:%v want:%v", test.name, got, test.want) 253 } 254 } 255 } 256 257 var weightedLinesOfTests = []struct { 258 name string 259 lines graph.WeightedLines 260 want []graph.WeightedLine 261 }{ 262 { 263 name: "nil", 264 lines: nil, 265 want: nil, 266 }, 267 { 268 name: "empty", 269 lines: graph.Empty, 270 want: nil, 271 }, 272 { 273 name: "no edges", 274 lines: iterator.NewOrderedWeightedLines(nil), 275 want: nil, 276 }, 277 { 278 name: "no slice method", 279 lines: basicWeightedLines{iterator.NewOrderedWeightedLines([]graph.WeightedLine{ 280 multi.WeightedLine{F: multi.Node(-1), T: multi.Node(0), W: 1, UID: -1}, 281 multi.WeightedLine{F: multi.Node(1), T: multi.Node(2), W: 2, UID: 0}, 282 multi.WeightedLine{F: multi.Node(3), T: multi.Node(4), W: 3, UID: 1}, 283 })}, 284 want: []graph.WeightedLine{ 285 multi.WeightedLine{F: multi.Node(-1), T: multi.Node(0), W: 1, UID: -1}, 286 multi.WeightedLine{F: multi.Node(1), T: multi.Node(2), W: 2, UID: 0}, 287 multi.WeightedLine{F: multi.Node(3), T: multi.Node(4), W: 3, UID: 1}, 288 }, 289 }, 290 { 291 name: "explicit edges", 292 lines: iterator.NewOrderedWeightedLines([]graph.WeightedLine{ 293 multi.WeightedLine{F: multi.Node(-1), T: multi.Node(0), W: 1, UID: -1}, 294 multi.WeightedLine{F: multi.Node(1), T: multi.Node(2), W: 2, UID: 0}, 295 multi.WeightedLine{F: multi.Node(3), T: multi.Node(4), W: 3, UID: 1}, 296 }), 297 want: []graph.WeightedLine{ 298 multi.WeightedLine{F: multi.Node(-1), T: multi.Node(0), W: 1, UID: -1}, 299 multi.WeightedLine{F: multi.Node(1), T: multi.Node(2), W: 2, UID: 0}, 300 multi.WeightedLine{F: multi.Node(3), T: multi.Node(4), W: 3, UID: 1}, 301 }, 302 }, 303 } 304 305 type basicWeightedLines struct { 306 graph.WeightedLines 307 } 308 309 func TestWeightedLinesOf(t *testing.T) { 310 for _, test := range weightedLinesOfTests { 311 got := graph.WeightedLinesOf(test.lines) 312 if !reflect.DeepEqual(got, test.want) { 313 t.Errorf("unexpected result for %q: got:%v want:%v", test.name, got, test.want) 314 } 315 } 316 }