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

     1  // Copyright ©2015 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 topo
     6  
     7  import (
     8  	"reflect"
     9  	"testing"
    10  
    11  	"gonum.org/v1/gonum/graph/internal/ordered"
    12  	"gonum.org/v1/gonum/graph/simple"
    13  )
    14  
    15  var cyclesInTests = []struct {
    16  	g    []intset
    17  	want [][]int64
    18  }{
    19  	{
    20  		g: []intset{
    21  			0: linksTo(1),
    22  			1: linksTo(2, 7),
    23  			2: linksTo(3, 6),
    24  			3: linksTo(4),
    25  			4: linksTo(2, 5),
    26  			6: linksTo(3, 5),
    27  			7: linksTo(0, 6),
    28  		},
    29  		want: [][]int64{
    30  			{0, 1, 7, 0},
    31  			{2, 3, 4, 2},
    32  			{2, 6, 3, 4, 2},
    33  		},
    34  	},
    35  	{
    36  		g: []intset{
    37  			0: linksTo(1, 2, 3),
    38  			1: linksTo(2),
    39  			2: linksTo(3),
    40  			3: linksTo(1),
    41  		},
    42  		want: [][]int64{
    43  			{1, 2, 3, 1},
    44  		},
    45  	},
    46  	{
    47  		g: []intset{
    48  			0: linksTo(1),
    49  			1: linksTo(0, 2),
    50  			2: linksTo(1),
    51  		},
    52  		want: [][]int64{
    53  			{0, 1, 0},
    54  			{1, 2, 1},
    55  		},
    56  	},
    57  	{
    58  		g: []intset{
    59  			0: linksTo(1),
    60  			1: linksTo(2, 3),
    61  			2: linksTo(4, 5),
    62  			3: linksTo(4, 5),
    63  			4: linksTo(6),
    64  			5: nil,
    65  			6: nil,
    66  		},
    67  		want: nil,
    68  	},
    69  	{
    70  		g: []intset{
    71  			0: linksTo(1),
    72  			1: linksTo(2, 3, 4),
    73  			2: linksTo(0, 3),
    74  			3: linksTo(4),
    75  			4: linksTo(3),
    76  		},
    77  		want: [][]int64{
    78  			{0, 1, 2, 0},
    79  			{3, 4, 3},
    80  		},
    81  	},
    82  }
    83  
    84  func TestDirectedCyclesIn(t *testing.T) {
    85  	for i, test := range cyclesInTests {
    86  		g := simple.NewDirectedGraph()
    87  		g.AddNode(simple.Node(-10)) // Make sure we test graphs with sparse IDs.
    88  		for u, e := range test.g {
    89  			// Add nodes that are not defined by an edge.
    90  			if g.Node(int64(u)) == nil {
    91  				g.AddNode(simple.Node(u))
    92  			}
    93  			for v := range e {
    94  				g.SetEdge(simple.Edge{F: simple.Node(u), T: simple.Node(v)})
    95  			}
    96  		}
    97  		cycles := DirectedCyclesIn(g)
    98  		var got [][]int64
    99  		if cycles != nil {
   100  			got = make([][]int64, len(cycles))
   101  		}
   102  		// johnson.circuit does range iteration over maps,
   103  		// so sort to ensure consistent ordering.
   104  		for j, c := range cycles {
   105  			ids := make([]int64, len(c))
   106  			for k, n := range c {
   107  				ids[k] = n.ID()
   108  			}
   109  			got[j] = ids
   110  		}
   111  		ordered.BySliceValues(got)
   112  		if !reflect.DeepEqual(got, test.want) {
   113  			t.Errorf("unexpected johnson result for %d:\n\tgot:%#v\n\twant:%#v", i, got, test.want)
   114  		}
   115  	}
   116  }