github.com/jingcheng-WU/gonum@v0.9.1-0.20210323123734-f1a2a11a8f7b/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  	"testing"
    10  
    11  	"github.com/jingcheng-WU/gonum/graph"
    12  	"github.com/jingcheng-WU/gonum/graph/iterator"
    13  	"github.com/jingcheng-WU/gonum/graph/simple"
    14  )
    15  
    16  type line struct{ f, t int }
    17  
    18  func (l line) From() graph.Node         { return simple.Node(l.f) }
    19  func (l line) To() graph.Node           { return simple.Node(l.t) }
    20  func (l line) ReversedLine() graph.Line { return line{f: l.t, t: l.f} }
    21  func (l line) ID() int64                { return 1 }
    22  
    23  var orderedLinesTests = []struct {
    24  	lines []graph.Line
    25  }{
    26  	{lines: nil},
    27  	{lines: []graph.Line{line{f: 1, t: 2}}},
    28  	{lines: []graph.Line{line{f: 1, t: 2}, line{f: 2, t: 3}, line{f: 3, t: 4}, line{f: 4, t: 5}}},
    29  	{lines: []graph.Line{line{f: 5, t: 4}, line{f: 4, t: 3}, line{f: 3, t: 2}, line{f: 2, t: 1}}},
    30  }
    31  
    32  func TestOrderedLinesIterate(t *testing.T) {
    33  	for _, test := range orderedLinesTests {
    34  		it := iterator.NewOrderedLines(test.lines)
    35  		for i := 0; i < 2; i++ {
    36  			if it.Len() != len(test.lines) {
    37  				t.Errorf("unexpected iterator length for round %d: got:%d want:%d", i, it.Len(), len(test.lines))
    38  			}
    39  			var got []graph.Line
    40  			for it.Next() {
    41  				got = append(got, it.Line())
    42  			}
    43  			want := test.lines
    44  			if !reflect.DeepEqual(got, want) {
    45  				t.Errorf("unexpected iterator output for round %d: got:%#v want:%#v", i, got, want)
    46  			}
    47  			it.Reset()
    48  		}
    49  	}
    50  }
    51  
    52  func TestOrderedLinesSlice(t *testing.T) {
    53  	for _, test := range orderedLinesTests {
    54  		it := iterator.NewOrderedLines(test.lines)
    55  		for i := 0; i < 2; i++ {
    56  			got := it.LineSlice()
    57  			want := test.lines
    58  			if !reflect.DeepEqual(got, want) {
    59  				t.Errorf("unexpected iterator output for round %d: got:%#v want:%#v", i, got, want)
    60  			}
    61  			it.Reset()
    62  		}
    63  	}
    64  }
    65  
    66  type weightedLine struct {
    67  	f, t int
    68  	w    float64
    69  }
    70  
    71  func (l weightedLine) From() graph.Node         { return simple.Node(l.f) }
    72  func (l weightedLine) To() graph.Node           { return simple.Node(l.t) }
    73  func (l weightedLine) ReversedLine() graph.Line { l.f, l.t = l.t, l.f; return l }
    74  func (l weightedLine) Weight() float64          { return l.w }
    75  func (l weightedLine) ID() int64                { return 1 }
    76  
    77  var orderedWeightedLinesTests = []struct {
    78  	lines []graph.WeightedLine
    79  }{
    80  	{lines: nil},
    81  	{lines: []graph.WeightedLine{weightedLine{f: 1, t: 2, w: 1}}},
    82  	{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}}},
    83  	{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}}},
    84  }
    85  
    86  func TestOrderedWeightedLinesIterate(t *testing.T) {
    87  	for _, test := range orderedWeightedLinesTests {
    88  		it := iterator.NewOrderedWeightedLines(test.lines)
    89  		for i := 0; i < 2; i++ {
    90  			if it.Len() != len(test.lines) {
    91  				t.Errorf("unexpected iterator length for round %d: got:%d want:%d", i, it.Len(), len(test.lines))
    92  			}
    93  			var got []graph.WeightedLine
    94  			for it.Next() {
    95  				got = append(got, it.WeightedLine())
    96  			}
    97  			want := test.lines
    98  			if !reflect.DeepEqual(got, want) {
    99  				t.Errorf("unexpected iterator output for round %d: got:%#v want:%#v", i, got, want)
   100  			}
   101  			it.Reset()
   102  		}
   103  	}
   104  }
   105  
   106  func TestOrderedWeightedLinesSlice(t *testing.T) {
   107  	for _, test := range orderedWeightedLinesTests {
   108  		it := iterator.NewOrderedWeightedLines(test.lines)
   109  		for i := 0; i < 2; i++ {
   110  			got := it.WeightedLineSlice()
   111  			want := test.lines
   112  			if !reflect.DeepEqual(got, want) {
   113  				t.Errorf("unexpected iterator output for round %d: got:%#v want:%#v", i, got, want)
   114  			}
   115  			it.Reset()
   116  		}
   117  	}
   118  }