github.com/gopherd/gonum@v0.0.4/graph/iterator/nodes_test.go (about) 1 // Copyright ©2018 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 iterator_test 6 7 import ( 8 "reflect" 9 "testing" 10 11 "github.com/gopherd/gonum/graph" 12 "github.com/gopherd/gonum/graph/internal/ordered" 13 "github.com/gopherd/gonum/graph/iterator" 14 "github.com/gopherd/gonum/graph/simple" 15 ) 16 17 var orderedNodesTests = []struct { 18 nodes []graph.Node 19 }{ 20 {nodes: nil}, 21 {nodes: []graph.Node{simple.Node(1)}}, 22 {nodes: []graph.Node{simple.Node(1), simple.Node(2), simple.Node(3), simple.Node(5)}}, 23 {nodes: []graph.Node{simple.Node(5), simple.Node(3), simple.Node(2), simple.Node(1)}}, 24 } 25 26 func TestOrderedNodesIterate(t *testing.T) { 27 for _, test := range orderedNodesTests { 28 it := iterator.NewOrderedNodes(test.nodes) 29 for i := 0; i < 2; i++ { 30 if it.Len() != len(test.nodes) { 31 t.Errorf("unexpected iterator length for round %d: got:%d want:%d", i, it.Len(), len(test.nodes)) 32 } 33 var got []graph.Node 34 for it.Next() { 35 got = append(got, it.Node()) 36 if len(got)+it.Len() != len(test.nodes) { 37 t.Errorf("unexpected iterator length during iteration for round %d: got:%d want:%d", i, it.Len(), len(test.nodes)-len(got)) 38 } 39 } 40 want := test.nodes 41 if !reflect.DeepEqual(got, want) { 42 t.Errorf("unexpected iterator output for round %d: got:%#v want:%#v", i, got, want) 43 } 44 it.Reset() 45 } 46 } 47 } 48 49 func TestOrderedNodesSlice(t *testing.T) { 50 for _, test := range orderedNodesTests { 51 it := iterator.NewOrderedNodes(test.nodes) 52 for i := 0; i < 2; i++ { 53 got := it.NodeSlice() 54 want := test.nodes 55 if !reflect.DeepEqual(got, want) { 56 t.Errorf("unexpected iterator output for round %d: got:%#v want:%#v", i, got, want) 57 } 58 it.Reset() 59 } 60 } 61 } 62 63 var implicitNodesTests = []struct { 64 beg, end int 65 new func(int) graph.Node 66 want []graph.Node 67 }{ 68 { 69 beg: 1, end: 1, 70 want: nil, 71 }, 72 { 73 beg: 1, end: 2, 74 new: newSimpleNode, 75 want: []graph.Node{simple.Node(1)}, 76 }, 77 { 78 beg: 1, end: 5, 79 new: newSimpleNode, 80 want: []graph.Node{simple.Node(1), simple.Node(2), simple.Node(3), simple.Node(4)}, 81 }, 82 } 83 84 func newSimpleNode(id int) graph.Node { return simple.Node(id) } 85 86 func TestImplicitNodesIterate(t *testing.T) { 87 for _, test := range implicitNodesTests { 88 it := iterator.NewImplicitNodes(test.beg, test.end, test.new) 89 for i := 0; i < 2; i++ { 90 if it.Len() != len(test.want) { 91 t.Errorf("unexpected iterator length for round %d: got:%d want:%d", i, it.Len(), len(test.want)) 92 } 93 var got []graph.Node 94 for it.Next() { 95 got = append(got, it.Node()) 96 if len(got)+it.Len() != test.end-test.beg { 97 t.Errorf("unexpected iterator length during iteration for round %d: got:%d want:%d", i, it.Len(), (test.end-test.beg)-len(got)) 98 } 99 } 100 if it.Len() != 0 { 101 t.Errorf("unexpected depleted iterator length for round %d: got:%d want:0", i, it.Len()) 102 } 103 if !reflect.DeepEqual(got, test.want) { 104 t.Errorf("unexpected iterator output for round %d: got:%#v want:%#v", i, got, test.want) 105 } 106 it.Reset() 107 } 108 } 109 } 110 111 var nodesTests = []struct { 112 nodes map[int64]graph.Node 113 }{ 114 {nodes: nil}, 115 {nodes: make(map[int64]graph.Node)}, 116 {nodes: map[int64]graph.Node{1: simple.Node(1)}}, 117 {nodes: map[int64]graph.Node{1: simple.Node(1), 2: simple.Node(2), 3: simple.Node(3), 5: simple.Node(5)}}, 118 {nodes: map[int64]graph.Node{5: simple.Node(5), 3: simple.Node(3), 2: simple.Node(2), 1: simple.Node(1)}}, 119 } 120 121 func TestIterateNodes(t *testing.T) { 122 for _, typ := range []struct { 123 name string 124 new func(map[int64]graph.Node) graph.Nodes 125 }{ 126 {name: "Nodes", new: func(n map[int64]graph.Node) graph.Nodes { return iterator.NewNodes(n) }}, 127 {name: "LazyOrderedNodes", new: func(n map[int64]graph.Node) graph.Nodes { return iterator.NewLazyOrderedNodes(n) }}, 128 } { 129 t.Run(typ.name, func(t *testing.T) { 130 for _, test := range nodesTests { 131 it := typ.new(test.nodes) 132 for i := 0; i < 2; i++ { 133 if it.Len() != len(test.nodes) { 134 t.Errorf("unexpected iterator length for round %d: got:%d want:%d", i, it.Len(), len(test.nodes)) 135 } 136 var got map[int64]graph.Node 137 if test.nodes != nil { 138 got = make(map[int64]graph.Node) 139 } 140 for it.Next() { 141 n := it.Node() 142 got[n.ID()] = n 143 if len(got)+it.Len() != len(test.nodes) { 144 t.Errorf("unexpected iterator length during iteration for round %d: got:%d want:%d", i, it.Len(), len(test.nodes)) 145 } 146 } 147 want := test.nodes 148 if !reflect.DeepEqual(got, want) { 149 t.Errorf("unexpected iterator output for round %d: got:%#v want:%#v", i, got, want) 150 } 151 func() { 152 defer func() { 153 r := recover() 154 if r != nil { 155 t.Errorf("unexpected panic: %v", r) 156 } 157 }() 158 it.Next() 159 }() 160 it.Reset() 161 } 162 } 163 }) 164 } 165 } 166 167 var nodesByEdgeTests = []struct { 168 n int64 169 edges map[int64]graph.Edge 170 want map[int64]graph.Node 171 }{ 172 // The actual values of the edge stored in the edge 173 // map leading to each node are not used, so they are 174 // filled with nil values. 175 { 176 n: 6, 177 edges: nil, 178 want: nil, 179 }, 180 { 181 n: 6, 182 edges: make(map[int64]graph.Edge), 183 want: make(map[int64]graph.Node), 184 }, 185 { 186 n: 6, 187 edges: map[int64]graph.Edge{1: nil}, 188 want: map[int64]graph.Node{1: simple.Node(1)}, 189 }, 190 { 191 n: 6, 192 edges: map[int64]graph.Edge{1: nil, 2: nil, 3: nil, 5: nil}, 193 want: map[int64]graph.Node{1: simple.Node(1), 2: simple.Node(2), 3: simple.Node(3), 5: simple.Node(5)}, 194 }, 195 { 196 n: 6, 197 edges: map[int64]graph.Edge{5: nil, 3: nil, 2: nil, 1: nil}, 198 want: map[int64]graph.Node{5: simple.Node(5), 3: simple.Node(3), 2: simple.Node(2), 1: simple.Node(1)}, 199 }, 200 } 201 202 func TestNodesByEdgeIterate(t *testing.T) { 203 for _, typ := range []struct { 204 name string 205 new func(map[int64]graph.Node, map[int64]graph.Edge) graph.Nodes 206 }{ 207 { 208 name: "NodesByEdge", 209 new: func(n map[int64]graph.Node, e map[int64]graph.Edge) graph.Nodes { 210 return iterator.NewNodesByEdge(n, e) 211 }}, 212 { 213 name: "LazyOrderedNodesByEdge", 214 new: func(n map[int64]graph.Node, e map[int64]graph.Edge) graph.Nodes { 215 return iterator.NewLazyOrderedNodesByEdge(n, e) 216 }, 217 }, 218 } { 219 t.Run(typ.name, func(t *testing.T) { 220 for _, test := range nodesByEdgeTests { 221 nodes := make(map[int64]graph.Node) 222 for i := int64(0); i < test.n; i++ { 223 nodes[i] = simple.Node(i) 224 } 225 226 it := typ.new(nodes, test.edges) 227 for i := 0; i < 2; i++ { 228 if it.Len() != len(test.edges) { 229 t.Errorf("unexpected iterator length for round %d: got:%d want:%d", i, it.Len(), len(nodes)) 230 } 231 var got map[int64]graph.Node 232 if test.edges != nil { 233 got = make(map[int64]graph.Node) 234 } 235 for it.Next() { 236 n := it.Node() 237 got[n.ID()] = n 238 if len(got)+it.Len() != len(test.edges) { 239 t.Errorf("unexpected iterator length during iteration for round %d: got:%d want:%d", i, it.Len(), len(nodes)) 240 } 241 } 242 if !reflect.DeepEqual(got, test.want) { 243 t.Errorf("unexpected iterator output for round %d: got:%#v want:%#v", i, got, test.want) 244 } 245 func() { 246 defer func() { 247 r := recover() 248 if r != nil { 249 t.Errorf("unexpected panic: %v", r) 250 } 251 }() 252 it.Next() 253 }() 254 it.Reset() 255 } 256 } 257 }) 258 } 259 } 260 261 var nodesByWeightedEdgeTests = []struct { 262 n int64 263 edges map[int64]graph.WeightedEdge 264 want map[int64]graph.Node 265 }{ 266 // The actual values of the edges stored in the edge 267 // map leading to each node are not used, so they are 268 // filled with nil values. 269 { 270 n: 6, 271 edges: nil, 272 want: nil, 273 }, 274 { 275 n: 6, 276 edges: make(map[int64]graph.WeightedEdge), 277 want: make(map[int64]graph.Node), 278 }, 279 { 280 n: 6, 281 edges: map[int64]graph.WeightedEdge{1: nil}, 282 want: map[int64]graph.Node{1: simple.Node(1)}, 283 }, 284 { 285 n: 6, 286 edges: map[int64]graph.WeightedEdge{1: nil, 2: nil, 3: nil, 5: nil}, 287 want: map[int64]graph.Node{1: simple.Node(1), 2: simple.Node(2), 3: simple.Node(3), 5: simple.Node(5)}, 288 }, 289 { 290 n: 6, 291 edges: map[int64]graph.WeightedEdge{5: nil, 3: nil, 2: nil, 1: nil}, 292 want: map[int64]graph.Node{5: simple.Node(5), 3: simple.Node(3), 2: simple.Node(2), 1: simple.Node(1)}, 293 }, 294 } 295 296 func TestNodesByWeightedEdgeIterate(t *testing.T) { 297 for _, typ := range []struct { 298 name string 299 new func(map[int64]graph.Node, map[int64]graph.WeightedEdge) graph.Nodes 300 }{ 301 { 302 name: "NodesByWeightedEdge", 303 new: func(n map[int64]graph.Node, e map[int64]graph.WeightedEdge) graph.Nodes { 304 return iterator.NewNodesByWeightedEdge(n, e) 305 }}, 306 { 307 name: "LazyOrderedNodesByWeightedEdge", 308 new: func(n map[int64]graph.Node, e map[int64]graph.WeightedEdge) graph.Nodes { 309 return iterator.NewLazyOrderedNodesByWeightedEdge(n, e) 310 }, 311 }, 312 } { 313 t.Run(typ.name, func(t *testing.T) { 314 for _, test := range nodesByWeightedEdgeTests { 315 nodes := make(map[int64]graph.Node) 316 for i := int64(0); i < test.n; i++ { 317 nodes[i] = simple.Node(i) 318 } 319 320 it := typ.new(nodes, test.edges) 321 for i := 0; i < 2; i++ { 322 if it.Len() != len(test.edges) { 323 t.Errorf("unexpected iterator length for round %d: got:%d want:%d", i, it.Len(), len(nodes)) 324 } 325 var got map[int64]graph.Node 326 if test.edges != nil { 327 got = make(map[int64]graph.Node) 328 } 329 for it.Next() { 330 n := it.Node() 331 got[n.ID()] = n 332 if len(got)+it.Len() != len(test.edges) { 333 t.Errorf("unexpected iterator length during iteration for round %d: got:%d want:%d", i, it.Len(), len(nodes)) 334 } 335 } 336 if !reflect.DeepEqual(got, test.want) { 337 t.Errorf("unexpected iterator output for round %d: got:%#v want:%#v", i, got, test.want) 338 } 339 func() { 340 defer func() { 341 r := recover() 342 if r != nil { 343 t.Errorf("unexpected panic: %v", r) 344 } 345 }() 346 it.Next() 347 }() 348 it.Reset() 349 } 350 } 351 }) 352 } 353 } 354 355 var nodesByLinesTests = []struct { 356 n int64 357 lines map[int64]map[int64]graph.Line 358 want map[int64]graph.Node 359 }{ 360 // The actual values of the lines stored in the line 361 // collection leading to each node are not used, so 362 // they are filled with nil. 363 { 364 n: 6, 365 lines: nil, 366 want: nil, 367 }, 368 { 369 n: 6, 370 lines: make(map[int64]map[int64]graph.Line), 371 want: make(map[int64]graph.Node), 372 }, 373 { 374 n: 6, 375 lines: map[int64]map[int64]graph.Line{1: nil}, 376 want: map[int64]graph.Node{1: simple.Node(1)}, 377 }, 378 { 379 n: 6, 380 lines: map[int64]map[int64]graph.Line{1: nil, 2: nil, 3: nil, 5: nil}, 381 want: map[int64]graph.Node{1: simple.Node(1), 2: simple.Node(2), 3: simple.Node(3), 5: simple.Node(5)}, 382 }, 383 { 384 n: 6, 385 lines: map[int64]map[int64]graph.Line{5: nil, 3: nil, 2: nil, 1: nil}, 386 want: map[int64]graph.Node{5: simple.Node(5), 3: simple.Node(3), 2: simple.Node(2), 1: simple.Node(1)}, 387 }, 388 } 389 390 func TestNodesByLinesIterate(t *testing.T) { 391 for _, typ := range []struct { 392 name string 393 new func(map[int64]graph.Node, map[int64]map[int64]graph.Line) graph.Nodes 394 }{ 395 { 396 name: "NodesByLines", 397 new: func(n map[int64]graph.Node, e map[int64]map[int64]graph.Line) graph.Nodes { 398 return iterator.NewNodesByLines(n, e) 399 }}, 400 { 401 name: "LazyOrderedNodesByLines", 402 new: func(n map[int64]graph.Node, e map[int64]map[int64]graph.Line) graph.Nodes { 403 return iterator.NewLazyOrderedNodesByLines(n, e) 404 }, 405 }, 406 } { 407 t.Run(typ.name, func(t *testing.T) { 408 for _, test := range nodesByLinesTests { 409 nodes := make(map[int64]graph.Node) 410 for i := int64(0); i < test.n; i++ { 411 nodes[i] = simple.Node(i) 412 } 413 414 it := typ.new(nodes, test.lines) 415 for i := 0; i < 2; i++ { 416 if it.Len() != len(test.lines) { 417 t.Errorf("unexpected iterator length for round %d: got:%d want:%d", i, it.Len(), len(nodes)) 418 } 419 var got map[int64]graph.Node 420 if test.lines != nil { 421 got = make(map[int64]graph.Node) 422 } 423 for it.Next() { 424 n := it.Node() 425 got[n.ID()] = n 426 if len(got)+it.Len() != len(test.lines) { 427 t.Errorf("unexpected iterator length during iteration for round %d: got:%d want:%d", i, it.Len(), len(nodes)) 428 } 429 } 430 if !reflect.DeepEqual(got, test.want) { 431 t.Errorf("unexpected iterator output for round %d: got:%#v want:%#v", i, got, test.want) 432 } 433 func() { 434 defer func() { 435 r := recover() 436 if r != nil { 437 t.Errorf("unexpected panic: %v", r) 438 } 439 }() 440 it.Next() 441 }() 442 it.Reset() 443 } 444 } 445 }) 446 } 447 } 448 449 var nodesByWeightedLinesTests = []struct { 450 n int64 451 lines map[int64]map[int64]graph.WeightedLine 452 want map[int64]graph.Node 453 }{ 454 // The actual values of the lines stored in the line 455 // collection leading to each node are not used, so 456 // they are filled with nil. 457 { 458 n: 6, 459 lines: nil, 460 want: nil, 461 }, 462 { 463 n: 6, 464 lines: make(map[int64]map[int64]graph.WeightedLine), 465 want: make(map[int64]graph.Node), 466 }, 467 { 468 n: 6, 469 lines: map[int64]map[int64]graph.WeightedLine{1: nil}, 470 want: map[int64]graph.Node{1: simple.Node(1)}, 471 }, 472 { 473 n: 6, 474 lines: map[int64]map[int64]graph.WeightedLine{1: nil, 2: nil, 3: nil, 5: nil}, 475 want: map[int64]graph.Node{1: simple.Node(1), 2: simple.Node(2), 3: simple.Node(3), 5: simple.Node(5)}, 476 }, 477 { 478 n: 6, 479 lines: map[int64]map[int64]graph.WeightedLine{5: nil, 3: nil, 2: nil, 1: nil}, 480 want: map[int64]graph.Node{5: simple.Node(5), 3: simple.Node(3), 2: simple.Node(2), 1: simple.Node(1)}, 481 }, 482 } 483 484 func TestNodesByWeightedLinesIterate(t *testing.T) { 485 for _, typ := range []struct { 486 name string 487 new func(map[int64]graph.Node, map[int64]map[int64]graph.WeightedLine) graph.Nodes 488 }{ 489 { 490 name: "NodesByWeightedLines", 491 new: func(n map[int64]graph.Node, e map[int64]map[int64]graph.WeightedLine) graph.Nodes { 492 return iterator.NewNodesByWeightedLines(n, e) 493 }}, 494 { 495 name: "LazyOrderedNodesByWeightedLines", 496 new: func(n map[int64]graph.Node, e map[int64]map[int64]graph.WeightedLine) graph.Nodes { 497 return iterator.NewLazyOrderedNodesByWeightedLines(n, e) 498 }, 499 }, 500 } { 501 t.Run(typ.name, func(t *testing.T) { 502 for _, test := range nodesByWeightedLinesTests { 503 nodes := make(map[int64]graph.Node) 504 for i := int64(0); i < test.n; i++ { 505 nodes[i] = simple.Node(i) 506 } 507 508 it := typ.new(nodes, test.lines) 509 for i := 0; i < 2; i++ { 510 if it.Len() != len(test.lines) { 511 t.Errorf("unexpected iterator length for round %d: got:%d want:%d", i, it.Len(), len(nodes)) 512 } 513 var got map[int64]graph.Node 514 if test.lines != nil { 515 got = make(map[int64]graph.Node) 516 } 517 for it.Next() { 518 n := it.Node() 519 got[n.ID()] = n 520 if len(got)+it.Len() != len(test.lines) { 521 t.Errorf("unexpected iterator length during iteration for round %d: got:%d want:%d", i, it.Len(), len(nodes)) 522 } 523 } 524 if !reflect.DeepEqual(got, test.want) { 525 t.Errorf("unexpected iterator output for round %d: got:%#v want:%#v", i, got, test.want) 526 } 527 func() { 528 defer func() { 529 r := recover() 530 if r != nil { 531 t.Errorf("unexpected panic: %v", r) 532 } 533 }() 534 it.Next() 535 }() 536 it.Reset() 537 } 538 } 539 }) 540 } 541 } 542 543 type nodeSlicer interface { 544 graph.Nodes 545 graph.NodeSlicer 546 } 547 548 var nodeSlicerTests = []struct { 549 nodes nodeSlicer 550 want []graph.Node 551 }{ 552 { 553 nodes: iterator.NewOrderedNodes([]graph.Node{simple.Node(1)}), 554 want: []graph.Node{simple.Node(1)}, 555 }, 556 { 557 nodes: iterator.NewOrderedNodes([]graph.Node{simple.Node(1), simple.Node(2), simple.Node(3)}), 558 want: []graph.Node{simple.Node(1), simple.Node(2), simple.Node(3)}, 559 }, 560 561 { 562 nodes: iterator.NewImplicitNodes(1, 2, func(id int) graph.Node { return simple.Node(id) }), 563 want: []graph.Node{simple.Node(1)}, 564 }, 565 { 566 nodes: iterator.NewImplicitNodes(1, 4, func(id int) graph.Node { return simple.Node(id) }), 567 want: []graph.Node{simple.Node(1), simple.Node(2), simple.Node(3)}, 568 }, 569 570 { 571 nodes: iterator.NewNodes(map[int64]graph.Node{1: simple.Node(1)}), 572 want: []graph.Node{simple.Node(1)}, 573 }, 574 { 575 nodes: iterator.NewNodes(map[int64]graph.Node{1: simple.Node(1), 2: simple.Node(2), 3: simple.Node(3)}), 576 want: []graph.Node{simple.Node(1), simple.Node(2), simple.Node(3)}, 577 }, 578 { 579 nodes: iterator.NewNodes(map[int64]graph.Node{1: simple.Node(1), 2: simple.Node(2), 3: simple.Node(3), 4: simple.Node(4), 5: simple.Node(5)}), 580 want: []graph.Node{simple.Node(1), simple.Node(2), simple.Node(3), simple.Node(4), simple.Node(5)}, 581 }, 582 { 583 nodes: iterator.NewNodes(map[int64]graph.Node{5: simple.Node(5), 3: simple.Node(3), 2: simple.Node(2), 1: simple.Node(1)}), 584 want: []graph.Node{simple.Node(1), simple.Node(2), simple.Node(3), simple.Node(5)}, 585 }, 586 587 { 588 nodes: iterator.NewLazyOrderedNodes(map[int64]graph.Node{1: simple.Node(1)}), 589 want: []graph.Node{simple.Node(1)}, 590 }, 591 { 592 nodes: iterator.NewLazyOrderedNodes(map[int64]graph.Node{1: simple.Node(1), 2: simple.Node(2), 3: simple.Node(3)}), 593 want: []graph.Node{simple.Node(1), simple.Node(2), simple.Node(3)}, 594 }, 595 { 596 nodes: iterator.NewLazyOrderedNodes(map[int64]graph.Node{1: simple.Node(1), 2: simple.Node(2), 3: simple.Node(3), 4: simple.Node(4), 5: simple.Node(5)}), 597 want: []graph.Node{simple.Node(1), simple.Node(2), simple.Node(3), simple.Node(4), simple.Node(5)}, 598 }, 599 { 600 nodes: iterator.NewLazyOrderedNodes(map[int64]graph.Node{5: simple.Node(5), 3: simple.Node(3), 2: simple.Node(2), 1: simple.Node(1)}), 601 want: []graph.Node{simple.Node(1), simple.Node(2), simple.Node(3), simple.Node(5)}, 602 }, 603 604 // The actual values of the edges stored in the edge 605 // map leading to each node are not used, so they are 606 // filled with nil values. 607 // 608 // The three other constructors for NodesByEdge are not 609 // tested for this behaviour since they have already 610 // been tested above. 611 { 612 nodes: iterator.NewNodesByEdge( 613 map[int64]graph.Node{1: simple.Node(1), 2: simple.Node(2), 3: simple.Node(3), 4: simple.Node(4), 5: simple.Node(5)}, 614 map[int64]graph.Edge{1: nil}, 615 ), 616 want: []graph.Node{simple.Node(1)}, 617 }, 618 { 619 nodes: iterator.NewNodesByEdge( 620 map[int64]graph.Node{1: simple.Node(1), 2: simple.Node(2), 3: simple.Node(3), 4: simple.Node(4), 5: simple.Node(5)}, 621 map[int64]graph.Edge{1: nil, 2: nil, 3: nil, 5: nil}, 622 ), 623 want: []graph.Node{simple.Node(1), simple.Node(2), simple.Node(3), simple.Node(5)}, 624 }, 625 { 626 nodes: iterator.NewNodesByEdge( 627 map[int64]graph.Node{1: simple.Node(1), 2: simple.Node(2), 3: simple.Node(3), 4: simple.Node(4), 5: simple.Node(5)}, 628 map[int64]graph.Edge{5: nil, 3: nil, 2: nil, 1: nil}, 629 ), 630 want: []graph.Node{simple.Node(1), simple.Node(2), simple.Node(3), simple.Node(5)}, 631 }, 632 633 { 634 nodes: iterator.NewLazyOrderedNodesByEdge( 635 map[int64]graph.Node{1: simple.Node(1), 2: simple.Node(2), 3: simple.Node(3), 4: simple.Node(4), 5: simple.Node(5)}, 636 map[int64]graph.Edge{1: nil}, 637 ), 638 want: []graph.Node{simple.Node(1)}, 639 }, 640 { 641 nodes: iterator.NewLazyOrderedNodesByEdge( 642 map[int64]graph.Node{1: simple.Node(1), 2: simple.Node(2), 3: simple.Node(3), 4: simple.Node(4), 5: simple.Node(5)}, 643 map[int64]graph.Edge{1: nil, 2: nil, 3: nil, 5: nil}, 644 ), 645 want: []graph.Node{simple.Node(1), simple.Node(2), simple.Node(3), simple.Node(5)}, 646 }, 647 { 648 nodes: iterator.NewLazyOrderedNodesByEdge( 649 map[int64]graph.Node{1: simple.Node(1), 2: simple.Node(2), 3: simple.Node(3), 4: simple.Node(4), 5: simple.Node(5)}, 650 map[int64]graph.Edge{5: nil, 3: nil, 2: nil, 1: nil}, 651 ), 652 want: []graph.Node{simple.Node(1), simple.Node(2), simple.Node(3), simple.Node(5)}, 653 }, 654 655 { 656 nodes: iterator.NewLazyOrderedNodesByWeightedEdge( 657 map[int64]graph.Node{1: simple.Node(1), 2: simple.Node(2), 3: simple.Node(3), 4: simple.Node(4), 5: simple.Node(5)}, 658 map[int64]graph.WeightedEdge{1: nil}, 659 ), 660 want: []graph.Node{simple.Node(1)}, 661 }, 662 { 663 nodes: iterator.NewLazyOrderedNodesByWeightedEdge( 664 map[int64]graph.Node{1: simple.Node(1), 2: simple.Node(2), 3: simple.Node(3), 4: simple.Node(4), 5: simple.Node(5)}, 665 map[int64]graph.WeightedEdge{1: nil, 2: nil, 3: nil, 5: nil}, 666 ), 667 want: []graph.Node{simple.Node(1), simple.Node(2), simple.Node(3), simple.Node(5)}, 668 }, 669 { 670 nodes: iterator.NewLazyOrderedNodesByWeightedEdge( 671 map[int64]graph.Node{1: simple.Node(1), 2: simple.Node(2), 3: simple.Node(3), 4: simple.Node(4), 5: simple.Node(5)}, 672 map[int64]graph.WeightedEdge{5: nil, 3: nil, 2: nil, 1: nil}, 673 ), 674 want: []graph.Node{simple.Node(1), simple.Node(2), simple.Node(3), simple.Node(5)}, 675 }, 676 677 { 678 nodes: iterator.NewLazyOrderedNodesByLines( 679 map[int64]graph.Node{1: simple.Node(1), 2: simple.Node(2), 3: simple.Node(3), 4: simple.Node(4), 5: simple.Node(5)}, 680 map[int64]map[int64]graph.Line{1: nil}, 681 ), 682 want: []graph.Node{simple.Node(1)}, 683 }, 684 { 685 nodes: iterator.NewLazyOrderedNodesByLines( 686 map[int64]graph.Node{1: simple.Node(1), 2: simple.Node(2), 3: simple.Node(3), 4: simple.Node(4), 5: simple.Node(5)}, 687 map[int64]map[int64]graph.Line{1: nil, 2: nil, 3: nil, 5: nil}, 688 ), 689 want: []graph.Node{simple.Node(1), simple.Node(2), simple.Node(3), simple.Node(5)}, 690 }, 691 { 692 nodes: iterator.NewLazyOrderedNodesByLines( 693 map[int64]graph.Node{1: simple.Node(1), 2: simple.Node(2), 3: simple.Node(3), 4: simple.Node(4), 5: simple.Node(5)}, 694 map[int64]map[int64]graph.Line{5: nil, 3: nil, 2: nil, 1: nil}, 695 ), 696 want: []graph.Node{simple.Node(1), simple.Node(2), simple.Node(3), simple.Node(5)}, 697 }, 698 699 { 700 nodes: iterator.NewLazyOrderedNodesByWeightedLines( 701 map[int64]graph.Node{1: simple.Node(1), 2: simple.Node(2), 3: simple.Node(3), 4: simple.Node(4), 5: simple.Node(5)}, 702 map[int64]map[int64]graph.WeightedLine{1: nil}, 703 ), 704 want: []graph.Node{simple.Node(1)}, 705 }, 706 { 707 nodes: iterator.NewLazyOrderedNodesByWeightedLines( 708 map[int64]graph.Node{1: simple.Node(1), 2: simple.Node(2), 3: simple.Node(3), 4: simple.Node(4), 5: simple.Node(5)}, 709 map[int64]map[int64]graph.WeightedLine{1: nil, 2: nil, 3: nil, 5: nil}, 710 ), 711 want: []graph.Node{simple.Node(1), simple.Node(2), simple.Node(3), simple.Node(5)}, 712 }, 713 { 714 nodes: iterator.NewLazyOrderedNodesByWeightedLines( 715 map[int64]graph.Node{1: simple.Node(1), 2: simple.Node(2), 3: simple.Node(3), 4: simple.Node(4), 5: simple.Node(5)}, 716 map[int64]map[int64]graph.WeightedLine{5: nil, 3: nil, 2: nil, 1: nil}, 717 ), 718 want: []graph.Node{simple.Node(1), simple.Node(2), simple.Node(3), simple.Node(5)}, 719 }, 720 } 721 722 func TestNodeSlicers(t *testing.T) { 723 for k, test := range nodeSlicerTests { 724 wantLen := test.nodes.Len() 725 for i := 0; i < wantLen; i++ { 726 var gotIter []graph.Node 727 for n := 0; n < i; n++ { 728 ok := test.nodes.Next() 729 if !ok { 730 t.Errorf("test %d: unexpected failed Next call at position %d of len %d", k, n, wantLen) 731 } 732 gotIter = append(gotIter, test.nodes.Node()) 733 } 734 gotSlice := test.nodes.NodeSlice() 735 if test.nodes.Next() { 736 t.Errorf("test %d: expected no further iteration possible after NodeSlice with %d pre-iterations of %d", k, i, wantLen) 737 } 738 739 if gotLen := len(gotIter) + len(gotSlice); gotLen != wantLen { 740 t.Errorf("test %d: unexpected total node count: got:%d want:%d", k, gotLen, wantLen) 741 } 742 got := append(gotIter, gotSlice...) 743 ordered.ByID(got) 744 if !reflect.DeepEqual(got, test.want) { 745 t.Errorf("test %d: unexpected node slice:\ngot: %v\nwant:%v", k, got, test.want) 746 } 747 748 test.nodes.Reset() 749 } 750 } 751 }