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