github.com/gopherd/gonum@v0.0.4/graph/iterator/lines_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/gopherd/gonum/graph" 13 "github.com/gopherd/gonum/graph/iterator" 14 "github.com/gopherd/gonum/graph/simple" 15 ) 16 17 type line struct{ f, t, id int64 } 18 19 func (l line) From() graph.Node { return simple.Node(l.f) } 20 func (l line) To() graph.Node { return simple.Node(l.t) } 21 func (l line) ReversedLine() graph.Line { l.f, l.t = l.t, l.f; return l } 22 func (l line) ID() int64 { return l.id } 23 24 var linesTests = []struct { 25 lines map[int64]graph.Line 26 }{ 27 {lines: nil}, 28 {lines: map[int64]graph.Line{1: line{f: 1, t: 2, id: 1}}}, 29 {lines: map[int64]graph.Line{1: line{f: 1, t: 2, id: 1}, 2: line{f: 2, t: 3, id: 2}, 3: line{f: 3, t: 4, id: 3}, 4: line{f: 4, t: 5, id: 4}}}, 30 {lines: map[int64]graph.Line{4: line{f: 5, t: 4, id: 4}, 3: line{f: 4, t: 3, id: 3}, 2: line{f: 3, t: 2, id: 2}, 1: line{f: 2, t: 1, id: 1}}}, 31 } 32 33 func TestLinesIterate(t *testing.T) { 34 for _, test := range linesTests { 35 it := iterator.NewLines(test.lines) 36 for i := 0; i < 2; i++ { 37 if it.Len() != len(test.lines) { 38 t.Errorf("unexpected iterator length for round %d: got:%d want:%d", i, it.Len(), len(test.lines)) 39 } 40 var got map[int64]graph.Line 41 if it.Len() != 0 { 42 got = make(map[int64]graph.Line) 43 } 44 for it.Next() { 45 got[it.Line().ID()] = it.Line() 46 } 47 want := test.lines 48 if !reflect.DeepEqual(got, want) { 49 t.Errorf("unexpected iterator output for round %d: got:%#v want:%#v", i, got, want) 50 } 51 it.Reset() 52 } 53 } 54 } 55 56 func TestLinesSlice(t *testing.T) { 57 for _, test := range linesTests { 58 it := iterator.NewLines(test.lines) 59 for i := 0; i < 2; i++ { 60 got := it.LineSlice() 61 var want []graph.Line 62 for _, l := range test.lines { 63 want = append(want, l) 64 } 65 sort.Slice(got, func(i, j int) bool { return got[i].ID() < got[j].ID() }) 66 sort.Slice(want, func(i, j int) bool { return want[i].ID() < want[j].ID() }) 67 if !reflect.DeepEqual(got, want) { 68 t.Errorf("unexpected iterator output for round %d: got:%#v want:%#v", i, got, want) 69 } 70 it.Reset() 71 } 72 } 73 } 74 75 var orderedLinesTests = []struct { 76 lines []graph.Line 77 }{ 78 {lines: nil}, 79 {lines: []graph.Line{line{f: 1, t: 2}}}, 80 {lines: []graph.Line{line{f: 1, t: 2}, line{f: 2, t: 3}, line{f: 3, t: 4}, line{f: 4, t: 5}}}, 81 {lines: []graph.Line{line{f: 5, t: 4}, line{f: 4, t: 3}, line{f: 3, t: 2}, line{f: 2, t: 1}}}, 82 } 83 84 func TestOrderedLinesIterate(t *testing.T) { 85 for _, test := range orderedLinesTests { 86 it := iterator.NewOrderedLines(test.lines) 87 for i := 0; i < 2; i++ { 88 if it.Len() != len(test.lines) { 89 t.Errorf("unexpected iterator length for round %d: got:%d want:%d", i, it.Len(), len(test.lines)) 90 } 91 var got []graph.Line 92 for it.Next() { 93 got = append(got, it.Line()) 94 } 95 want := test.lines 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 TestOrderedLinesSlice(t *testing.T) { 105 for _, test := range orderedLinesTests { 106 it := iterator.NewOrderedLines(test.lines) 107 for i := 0; i < 2; i++ { 108 got := it.LineSlice() 109 want := test.lines 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 } 117 118 type weightedLine struct { 119 f, t, id int64 120 w float64 121 } 122 123 func (l weightedLine) From() graph.Node { return simple.Node(l.f) } 124 func (l weightedLine) To() graph.Node { return simple.Node(l.t) } 125 func (l weightedLine) ReversedLine() graph.Line { l.f, l.t = l.t, l.f; return l } 126 func (l weightedLine) Weight() float64 { return l.w } 127 func (l weightedLine) ID() int64 { return l.id } 128 129 var weightedLinesTests = []struct { 130 lines map[int64]graph.WeightedLine 131 }{ 132 {lines: nil}, 133 {lines: map[int64]graph.WeightedLine{2: weightedLine{f: 1, t: 2, w: 1, id: 2}}}, 134 {lines: map[int64]graph.WeightedLine{2: weightedLine{f: 1, t: 2, w: 1, id: 2}, 4: weightedLine{f: 2, t: 3, w: 2, id: 4}, 6: weightedLine{f: 3, t: 4, w: 3, id: 6}, 8: weightedLine{f: 4, t: 5, w: 4, id: 8}}}, 135 {lines: map[int64]graph.WeightedLine{8: weightedLine{f: 5, t: 4, w: 4, id: 8}, 6: weightedLine{f: 4, t: 3, w: 3, id: 6}, 4: weightedLine{f: 3, t: 2, w: 2, id: 4}, 2: weightedLine{f: 2, t: 1, w: 1, id: 2}}}, 136 } 137 138 func TestWeightedLinesIterate(t *testing.T) { 139 for _, test := range weightedLinesTests { 140 it := iterator.NewWeightedLines(test.lines) 141 for i := 0; i < 2; i++ { 142 if it.Len() != len(test.lines) { 143 t.Errorf("unexpected iterator length for round %d: got:%d want:%d", i, it.Len(), len(test.lines)) 144 } 145 var got map[int64]graph.WeightedLine 146 if it.Len() != 0 { 147 got = make(map[int64]graph.WeightedLine) 148 } 149 for it.Next() { 150 got[it.WeightedLine().ID()] = it.WeightedLine() 151 } 152 want := test.lines 153 if !reflect.DeepEqual(got, want) { 154 t.Errorf("unexpected iterator output for round %d: got:%#v want:%#v", i, got, want) 155 } 156 it.Reset() 157 } 158 } 159 } 160 161 func TestWeightedLinesSlice(t *testing.T) { 162 for _, test := range weightedLinesTests { 163 it := iterator.NewWeightedLines(test.lines) 164 for i := 0; i < 2; i++ { 165 got := it.WeightedLineSlice() 166 var want []graph.WeightedLine 167 for _, l := range test.lines { 168 want = append(want, l) 169 } 170 sort.Slice(got, func(i, j int) bool { return got[i].ID() < got[j].ID() }) 171 sort.Slice(want, func(i, j int) bool { return want[i].ID() < want[j].ID() }) 172 if !reflect.DeepEqual(got, want) { 173 t.Errorf("unexpected iterator output for round %d: got:%#v want:%#v", i, got, want) 174 } 175 it.Reset() 176 } 177 } 178 } 179 180 var orderedWeightedLinesTests = []struct { 181 lines []graph.WeightedLine 182 }{ 183 {lines: nil}, 184 {lines: []graph.WeightedLine{weightedLine{f: 1, t: 2, w: 1}}}, 185 {lines: []graph.WeightedLine{weightedLine{f: 1, t: 2, w: 1}, weightedLine{f: 2, t: 3, w: 2}, weightedLine{f: 3, t: 4, w: 3}, weightedLine{f: 4, t: 5, w: 4}}}, 186 {lines: []graph.WeightedLine{weightedLine{f: 5, t: 4, w: 4}, weightedLine{f: 4, t: 3, w: 3}, weightedLine{f: 3, t: 2, w: 2}, weightedLine{f: 2, t: 1, w: 1}}}, 187 } 188 189 func TestOrderedWeightedLinesIterate(t *testing.T) { 190 for _, test := range orderedWeightedLinesTests { 191 it := iterator.NewOrderedWeightedLines(test.lines) 192 for i := 0; i < 2; i++ { 193 if it.Len() != len(test.lines) { 194 t.Errorf("unexpected iterator length for round %d: got:%d want:%d", i, it.Len(), len(test.lines)) 195 } 196 var got []graph.WeightedLine 197 for it.Next() { 198 got = append(got, it.WeightedLine()) 199 } 200 want := test.lines 201 if !reflect.DeepEqual(got, want) { 202 t.Errorf("unexpected iterator output for round %d: got:%#v want:%#v", i, got, want) 203 } 204 it.Reset() 205 } 206 } 207 } 208 209 func TestOrderedWeightedLinesSlice(t *testing.T) { 210 for _, test := range orderedWeightedLinesTests { 211 it := iterator.NewOrderedWeightedLines(test.lines) 212 for i := 0; i < 2; i++ { 213 got := it.WeightedLineSlice() 214 want := test.lines 215 if !reflect.DeepEqual(got, want) { 216 t.Errorf("unexpected iterator output for round %d: got:%#v want:%#v", i, got, want) 217 } 218 it.Reset() 219 } 220 } 221 }