gonum.org/v1/gonum@v0.14.0/graph/path/floydwarshall_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 path
     6  
     7  import (
     8  	"math"
     9  	"reflect"
    10  	"testing"
    11  
    12  	"gonum.org/v1/gonum/graph"
    13  	"gonum.org/v1/gonum/graph/internal/ordered"
    14  	"gonum.org/v1/gonum/graph/path/internal/testgraphs"
    15  )
    16  
    17  func TestFloydWarshall(t *testing.T) {
    18  	t.Parallel()
    19  	for _, test := range testgraphs.ShortestPathTests {
    20  		g := test.Graph()
    21  		for _, e := range test.Edges {
    22  			g.SetWeightedEdge(e)
    23  		}
    24  
    25  		pt, ok := FloydWarshall(g.(graph.Graph))
    26  		if test.HasNegativeCycle {
    27  			if ok {
    28  				t.Errorf("%q: expected negative cycle", test.Name)
    29  			}
    30  			continue
    31  		}
    32  		if !ok {
    33  			t.Fatalf("%q: unexpected negative cycle", test.Name)
    34  		}
    35  
    36  		// Check all random paths returned are OK.
    37  		for i := 0; i < 10; i++ {
    38  			p, weight, unique := pt.Between(test.Query.From().ID(), test.Query.To().ID())
    39  			if weight != test.Weight {
    40  				t.Errorf("%q: unexpected weight from Between: got:%f want:%f",
    41  					test.Name, weight, test.Weight)
    42  			}
    43  			if weight := pt.Weight(test.Query.From().ID(), test.Query.To().ID()); weight != test.Weight {
    44  				t.Errorf("%q: unexpected weight from Weight: got:%f want:%f",
    45  					test.Name, weight, test.Weight)
    46  			}
    47  			if unique != test.HasUniquePath {
    48  				t.Errorf("%q: unexpected number of paths: got: unique=%t want: unique=%t",
    49  					test.Name, unique, test.HasUniquePath)
    50  			}
    51  
    52  			var got []int64
    53  			for _, n := range p {
    54  				got = append(got, n.ID())
    55  			}
    56  			ok := len(got) == 0 && len(test.WantPaths) == 0
    57  			for _, sp := range test.WantPaths {
    58  				if reflect.DeepEqual(got, sp) {
    59  					ok = true
    60  					break
    61  				}
    62  			}
    63  			if !ok {
    64  				t.Errorf("%q: unexpected shortest path:\ngot: %v\nwant from:%v",
    65  					test.Name, p, test.WantPaths)
    66  			}
    67  		}
    68  
    69  		np, weight, unique := pt.Between(test.NoPathFor.From().ID(), test.NoPathFor.To().ID())
    70  		if np != nil || !math.IsInf(weight, 1) || unique {
    71  			t.Errorf("%q: unexpected path:\ngot: path=%v weight=%f unique=%t\nwant:path=<nil> weight=+Inf unique=false",
    72  				test.Name, np, weight, unique)
    73  		}
    74  
    75  		paths, weight := pt.AllBetween(test.Query.From().ID(), test.Query.To().ID())
    76  		if weight != test.Weight {
    77  			t.Errorf("%q: unexpected weight from Between: got:%f want:%f",
    78  				test.Name, weight, test.Weight)
    79  		}
    80  
    81  		var got [][]int64
    82  		if len(paths) != 0 {
    83  			got = make([][]int64, len(paths))
    84  		}
    85  		for i, p := range paths {
    86  			for _, v := range p {
    87  				got[i] = append(got[i], v.ID())
    88  			}
    89  		}
    90  		ordered.BySliceValues(got)
    91  		if !reflect.DeepEqual(got, test.WantPaths) {
    92  			t.Errorf("testing %q: unexpected shortest paths:\ngot: %v\nwant:%v",
    93  				test.Name, got, test.WantPaths)
    94  		}
    95  
    96  		paths = paths[:0]
    97  		pt.AllBetweenFunc(test.Query.From().ID(), test.Query.To().ID(), func(path []graph.Node) {
    98  			paths = append(paths, append([]graph.Node(nil), path...))
    99  		})
   100  		got = nil
   101  		if len(paths) != 0 {
   102  			got = make([][]int64, len(paths))
   103  		}
   104  		for i, p := range paths {
   105  			for _, v := range p {
   106  				got[i] = append(got[i], v.ID())
   107  			}
   108  		}
   109  		ordered.BySliceValues(got)
   110  		if !reflect.DeepEqual(got, test.WantPaths) {
   111  			t.Errorf("testing %q: unexpected shortest paths:\ngot: %v\nwant:%v",
   112  				test.Name, got, test.WantPaths)
   113  		}
   114  
   115  		nps, weight := pt.AllBetween(test.NoPathFor.From().ID(), test.NoPathFor.To().ID())
   116  		if nps != nil || !math.IsInf(weight, 1) {
   117  			t.Errorf("%q: unexpected path:\ngot: paths=%v weight=%f\nwant:path=<nil> weight=+Inf",
   118  				test.Name, nps, weight)
   119  		}
   120  	}
   121  }