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