gonum.org/v1/gonum@v0.14.0/graph/iterator/edges_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 "gonum.org/v1/gonum/graph" 12 "gonum.org/v1/gonum/graph/iterator" 13 "gonum.org/v1/gonum/graph/simple" 14 ) 15 16 type edge struct{ f, t int } 17 18 func (e edge) From() graph.Node { return simple.Node(e.f) } 19 func (e edge) To() graph.Node { return simple.Node(e.t) } 20 func (e edge) ReversedEdge() graph.Edge { return edge{f: e.t, t: e.f} } 21 22 var orderedEdgesTests = []struct { 23 edges []graph.Edge 24 }{ 25 {edges: nil}, 26 {edges: []graph.Edge{edge{f: 1, t: 2}}}, 27 {edges: []graph.Edge{edge{f: 1, t: 2}, edge{f: 2, t: 3}, edge{f: 3, t: 4}, edge{f: 4, t: 5}}}, 28 {edges: []graph.Edge{edge{f: 5, t: 4}, edge{f: 4, t: 3}, edge{f: 3, t: 2}, edge{f: 2, t: 1}}}, 29 } 30 31 func TestOrderedEdgesIterate(t *testing.T) { 32 for _, test := range orderedEdgesTests { 33 it := iterator.NewOrderedEdges(test.edges) 34 for i := 0; i < 2; i++ { 35 if it.Len() != len(test.edges) { 36 t.Errorf("unexpected iterator length for round %d: got:%d want:%d", i, it.Len(), len(test.edges)) 37 } 38 var got []graph.Edge 39 for it.Next() { 40 got = append(got, it.Edge()) 41 } 42 want := test.edges 43 if !reflect.DeepEqual(got, want) { 44 t.Errorf("unexpected iterator output for round %d: got:%#v want:%#v", i, got, want) 45 } 46 it.Reset() 47 } 48 } 49 } 50 51 func TestOrderedEdgesSlice(t *testing.T) { 52 for _, test := range orderedEdgesTests { 53 it := iterator.NewOrderedEdges(test.edges) 54 for i := 0; i < 2; i++ { 55 got := it.EdgeSlice() 56 want := test.edges 57 if !reflect.DeepEqual(got, want) { 58 t.Errorf("unexpected iterator output for round %d: got:%#v want:%#v", i, got, want) 59 } 60 it.Reset() 61 } 62 } 63 } 64 65 type weightedEdge struct { 66 f, t int 67 w float64 68 } 69 70 func (e weightedEdge) From() graph.Node { return simple.Node(e.f) } 71 func (e weightedEdge) To() graph.Node { return simple.Node(e.t) } 72 func (e weightedEdge) ReversedEdge() graph.Edge { e.f, e.t = e.t, e.f; return e } 73 func (e weightedEdge) Weight() float64 { return e.w } 74 75 var orderedWeightedEdgesTests = []struct { 76 edges []graph.WeightedEdge 77 }{ 78 {edges: nil}, 79 {edges: []graph.WeightedEdge{weightedEdge{f: 1, t: 2, w: 1}}}, 80 {edges: []graph.WeightedEdge{weightedEdge{f: 1, t: 2, w: 1}, weightedEdge{f: 2, t: 3, w: 2}, weightedEdge{f: 3, t: 4, w: 3}, weightedEdge{f: 4, t: 5, w: 4}}}, 81 {edges: []graph.WeightedEdge{weightedEdge{f: 5, t: 4, w: 4}, weightedEdge{f: 4, t: 3, w: 3}, weightedEdge{f: 3, t: 2, w: 2}, weightedEdge{f: 2, t: 1, w: 1}}}, 82 } 83 84 func TestOrderedWeightedEdgesIterate(t *testing.T) { 85 for _, test := range orderedWeightedEdgesTests { 86 it := iterator.NewOrderedWeightedEdges(test.edges) 87 for i := 0; i < 2; i++ { 88 if it.Len() != len(test.edges) { 89 t.Errorf("unexpected iterator length for round %d: got:%d want:%d", i, it.Len(), len(test.edges)) 90 } 91 var got []graph.WeightedEdge 92 for it.Next() { 93 got = append(got, it.WeightedEdge()) 94 } 95 want := test.edges 96 if !reflect.DeepEqual(got, want) { 97 t.Errorf("unexpected iterator output for round %d: got:%#v want:%#v", i, got, want) 98 } 99 it.Reset() 100 } 101 } 102 } 103 104 func TestOrderedWeightedEdgesSlice(t *testing.T) { 105 for _, test := range orderedWeightedEdgesTests { 106 it := iterator.NewOrderedWeightedEdges(test.edges) 107 for i := 0; i < 2; i++ { 108 got := it.WeightedEdgeSlice() 109 want := test.edges 110 if !reflect.DeepEqual(got, want) { 111 t.Errorf("unexpected iterator output for round %d: got:%#v want:%#v", i, got, want) 112 } 113 it.Reset() 114 } 115 } 116 }