gonum.org/v1/gonum@v0.14.0/graph/nodes_edges_test.go (about)

     1  // Copyright ©2019 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 graph_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/multi"
    14  	"gonum.org/v1/gonum/graph/simple"
    15  )
    16  
    17  // nodes
    18  // edges
    19  // weightededges
    20  // lines
    21  // weightedlines
    22  // empty
    23  
    24  type basicNodes struct {
    25  	graph.Nodes
    26  }
    27  
    28  func TestNodesOf(t *testing.T) {
    29  	nodesOfTests := []struct {
    30  		name  string
    31  		nodes graph.Nodes
    32  		want  []graph.Node
    33  	}{
    34  		{
    35  			name:  "nil",
    36  			nodes: nil,
    37  			want:  nil,
    38  		},
    39  		{
    40  			name:  "empty",
    41  			nodes: graph.Empty,
    42  			want:  nil,
    43  		},
    44  		{
    45  			name:  "no nodes",
    46  			nodes: iterator.NewOrderedNodes(nil),
    47  			want:  nil,
    48  		},
    49  		{
    50  			name:  "implicit nodes",
    51  			nodes: iterator.NewImplicitNodes(-1, 4, func(id int) graph.Node { return simple.Node(id) }),
    52  			want:  []graph.Node{simple.Node(-1), simple.Node(0), simple.Node(1), simple.Node(2), simple.Node(3)},
    53  		},
    54  		{
    55  			name:  "no slice method",
    56  			nodes: basicNodes{iterator.NewOrderedNodes([]graph.Node{simple.Node(-1), simple.Node(0), simple.Node(1), simple.Node(2), simple.Node(3)})},
    57  			want:  []graph.Node{simple.Node(-1), simple.Node(0), simple.Node(1), simple.Node(2), simple.Node(3)},
    58  		},
    59  		{
    60  			name:  "explicit nodes",
    61  			nodes: iterator.NewOrderedNodes([]graph.Node{simple.Node(-1), simple.Node(0), simple.Node(1), simple.Node(2), simple.Node(3)}),
    62  			want:  []graph.Node{simple.Node(-1), simple.Node(0), simple.Node(1), simple.Node(2), simple.Node(3)},
    63  		},
    64  	}
    65  
    66  	for _, test := range nodesOfTests {
    67  		got := graph.NodesOf(test.nodes)
    68  		if !reflect.DeepEqual(got, test.want) {
    69  			t.Errorf("unexpected result for %q: got:%v want:%v", test.name, got, test.want)
    70  		}
    71  	}
    72  }
    73  
    74  type basicEdges struct {
    75  	graph.Edges
    76  }
    77  
    78  func TestEdgesOf(t *testing.T) {
    79  	edgesOfTests := []struct {
    80  		name  string
    81  		edges graph.Edges
    82  		want  []graph.Edge
    83  	}{
    84  		{
    85  			name:  "nil",
    86  			edges: nil,
    87  			want:  nil,
    88  		},
    89  		{
    90  			name:  "empty",
    91  			edges: graph.Empty,
    92  			want:  nil,
    93  		},
    94  		{
    95  			name:  "no edges",
    96  			edges: iterator.NewOrderedEdges(nil),
    97  			want:  nil,
    98  		},
    99  		{
   100  			name: "no slice method",
   101  			edges: basicEdges{iterator.NewOrderedEdges([]graph.Edge{
   102  				simple.Edge{F: simple.Node(-1), T: simple.Node(0)},
   103  				simple.Edge{F: simple.Node(1), T: simple.Node(2)},
   104  				simple.Edge{F: simple.Node(3), T: simple.Node(4)},
   105  			})},
   106  			want: []graph.Edge{
   107  				simple.Edge{F: simple.Node(-1), T: simple.Node(0)},
   108  				simple.Edge{F: simple.Node(1), T: simple.Node(2)},
   109  				simple.Edge{F: simple.Node(3), T: simple.Node(4)},
   110  			},
   111  		},
   112  		{
   113  			name: "explicit edges",
   114  			edges: iterator.NewOrderedEdges([]graph.Edge{
   115  				simple.Edge{F: simple.Node(-1), T: simple.Node(0)},
   116  				simple.Edge{F: simple.Node(1), T: simple.Node(2)},
   117  				simple.Edge{F: simple.Node(3), T: simple.Node(4)},
   118  			}),
   119  			want: []graph.Edge{
   120  				simple.Edge{F: simple.Node(-1), T: simple.Node(0)},
   121  				simple.Edge{F: simple.Node(1), T: simple.Node(2)},
   122  				simple.Edge{F: simple.Node(3), T: simple.Node(4)},
   123  			},
   124  		},
   125  	}
   126  
   127  	for _, test := range edgesOfTests {
   128  		got := graph.EdgesOf(test.edges)
   129  		if !reflect.DeepEqual(got, test.want) {
   130  			t.Errorf("unexpected result for %q: got:%v want:%v", test.name, got, test.want)
   131  		}
   132  	}
   133  }
   134  
   135  type basicWeightedEdges struct {
   136  	graph.WeightedEdges
   137  }
   138  
   139  func TestWeightedEdgesOf(t *testing.T) {
   140  	weightedEdgesOfTests := []struct {
   141  		name  string
   142  		edges graph.WeightedEdges
   143  		want  []graph.WeightedEdge
   144  	}{
   145  		{
   146  			name:  "nil",
   147  			edges: nil,
   148  			want:  nil,
   149  		},
   150  		{
   151  			name:  "empty",
   152  			edges: graph.Empty,
   153  			want:  nil,
   154  		},
   155  		{
   156  			name:  "no edges",
   157  			edges: iterator.NewOrderedWeightedEdges(nil),
   158  			want:  nil,
   159  		},
   160  		{
   161  			name: "no slice method",
   162  			edges: basicWeightedEdges{iterator.NewOrderedWeightedEdges([]graph.WeightedEdge{
   163  				simple.WeightedEdge{F: simple.Node(-1), T: simple.Node(0), W: 1},
   164  				simple.WeightedEdge{F: simple.Node(1), T: simple.Node(2), W: 2},
   165  				simple.WeightedEdge{F: simple.Node(3), T: simple.Node(4), W: 3},
   166  			})},
   167  			want: []graph.WeightedEdge{
   168  				simple.WeightedEdge{F: simple.Node(-1), T: simple.Node(0), W: 1},
   169  				simple.WeightedEdge{F: simple.Node(1), T: simple.Node(2), W: 2},
   170  				simple.WeightedEdge{F: simple.Node(3), T: simple.Node(4), W: 3},
   171  			},
   172  		},
   173  		{
   174  			name: "explicit edges",
   175  			edges: iterator.NewOrderedWeightedEdges([]graph.WeightedEdge{
   176  				simple.WeightedEdge{F: simple.Node(-1), T: simple.Node(0), W: 1},
   177  				simple.WeightedEdge{F: simple.Node(1), T: simple.Node(2), W: 2},
   178  				simple.WeightedEdge{F: simple.Node(3), T: simple.Node(4), W: 3},
   179  			}),
   180  			want: []graph.WeightedEdge{
   181  				simple.WeightedEdge{F: simple.Node(-1), T: simple.Node(0), W: 1},
   182  				simple.WeightedEdge{F: simple.Node(1), T: simple.Node(2), W: 2},
   183  				simple.WeightedEdge{F: simple.Node(3), T: simple.Node(4), W: 3},
   184  			},
   185  		},
   186  	}
   187  
   188  	for _, test := range weightedEdgesOfTests {
   189  		got := graph.WeightedEdgesOf(test.edges)
   190  		if !reflect.DeepEqual(got, test.want) {
   191  			t.Errorf("unexpected result for %q: got:%v want:%v", test.name, got, test.want)
   192  		}
   193  	}
   194  }
   195  
   196  type basicLines struct {
   197  	graph.Lines
   198  }
   199  
   200  func TestLinesOf(t *testing.T) {
   201  	linesOfTests := []struct {
   202  		name  string
   203  		lines graph.Lines
   204  		want  []graph.Line
   205  	}{
   206  		{
   207  			name:  "nil",
   208  			lines: nil,
   209  			want:  nil,
   210  		},
   211  		{
   212  			name:  "empty",
   213  			lines: graph.Empty,
   214  			want:  nil,
   215  		},
   216  		{
   217  			name:  "no edges",
   218  			lines: iterator.NewOrderedLines(nil),
   219  			want:  nil,
   220  		},
   221  		{
   222  			name: "no slice method",
   223  			lines: basicLines{iterator.NewOrderedLines([]graph.Line{
   224  				multi.Line{F: multi.Node(-1), T: multi.Node(0), UID: -1},
   225  				multi.Line{F: multi.Node(1), T: multi.Node(2), UID: 0},
   226  				multi.Line{F: multi.Node(3), T: multi.Node(4), UID: 1},
   227  			})},
   228  			want: []graph.Line{
   229  				multi.Line{F: multi.Node(-1), T: multi.Node(0), UID: -1},
   230  				multi.Line{F: multi.Node(1), T: multi.Node(2), UID: 0},
   231  				multi.Line{F: multi.Node(3), T: multi.Node(4), UID: 1},
   232  			},
   233  		},
   234  		{
   235  			name: "explicit edges",
   236  			lines: iterator.NewOrderedLines([]graph.Line{
   237  				multi.Line{F: multi.Node(-1), T: multi.Node(0), UID: -1},
   238  				multi.Line{F: multi.Node(1), T: multi.Node(2), UID: 0},
   239  				multi.Line{F: multi.Node(3), T: multi.Node(4), UID: 1},
   240  			}),
   241  			want: []graph.Line{
   242  				multi.Line{F: multi.Node(-1), T: multi.Node(0), UID: -1},
   243  				multi.Line{F: multi.Node(1), T: multi.Node(2), UID: 0},
   244  				multi.Line{F: multi.Node(3), T: multi.Node(4), UID: 1},
   245  			},
   246  		},
   247  	}
   248  
   249  	for _, test := range linesOfTests {
   250  		got := graph.LinesOf(test.lines)
   251  		if !reflect.DeepEqual(got, test.want) {
   252  			t.Errorf("unexpected result for %q: got:%v want:%v", test.name, got, test.want)
   253  		}
   254  	}
   255  }
   256  
   257  type basicWeightedLines struct {
   258  	graph.WeightedLines
   259  }
   260  
   261  func TestWeightedLinesOf(t *testing.T) {
   262  	weightedLinesOfTests := []struct {
   263  		name  string
   264  		lines graph.WeightedLines
   265  		want  []graph.WeightedLine
   266  	}{
   267  		{
   268  			name:  "nil",
   269  			lines: nil,
   270  			want:  nil,
   271  		},
   272  		{
   273  			name:  "empty",
   274  			lines: graph.Empty,
   275  			want:  nil,
   276  		},
   277  		{
   278  			name:  "no edges",
   279  			lines: iterator.NewOrderedWeightedLines(nil),
   280  			want:  nil,
   281  		},
   282  		{
   283  			name: "no slice method",
   284  			lines: basicWeightedLines{iterator.NewOrderedWeightedLines([]graph.WeightedLine{
   285  				multi.WeightedLine{F: multi.Node(-1), T: multi.Node(0), W: 1, UID: -1},
   286  				multi.WeightedLine{F: multi.Node(1), T: multi.Node(2), W: 2, UID: 0},
   287  				multi.WeightedLine{F: multi.Node(3), T: multi.Node(4), W: 3, UID: 1},
   288  			})},
   289  			want: []graph.WeightedLine{
   290  				multi.WeightedLine{F: multi.Node(-1), T: multi.Node(0), W: 1, UID: -1},
   291  				multi.WeightedLine{F: multi.Node(1), T: multi.Node(2), W: 2, UID: 0},
   292  				multi.WeightedLine{F: multi.Node(3), T: multi.Node(4), W: 3, UID: 1},
   293  			},
   294  		},
   295  		{
   296  			name: "explicit edges",
   297  			lines: iterator.NewOrderedWeightedLines([]graph.WeightedLine{
   298  				multi.WeightedLine{F: multi.Node(-1), T: multi.Node(0), W: 1, UID: -1},
   299  				multi.WeightedLine{F: multi.Node(1), T: multi.Node(2), W: 2, UID: 0},
   300  				multi.WeightedLine{F: multi.Node(3), T: multi.Node(4), W: 3, UID: 1},
   301  			}),
   302  			want: []graph.WeightedLine{
   303  				multi.WeightedLine{F: multi.Node(-1), T: multi.Node(0), W: 1, UID: -1},
   304  				multi.WeightedLine{F: multi.Node(1), T: multi.Node(2), W: 2, UID: 0},
   305  				multi.WeightedLine{F: multi.Node(3), T: multi.Node(4), W: 3, UID: 1},
   306  			},
   307  		},
   308  	}
   309  
   310  	for _, test := range weightedLinesOfTests {
   311  		got := graph.WeightedLinesOf(test.lines)
   312  		if !reflect.DeepEqual(got, test.want) {
   313  			t.Errorf("unexpected result for %q: got:%v want:%v", test.name, got, test.want)
   314  		}
   315  	}
   316  }