github.com/gopherd/gonum@v0.0.4/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  	"github.com/gopherd/gonum/graph"
    13  	"github.com/gopherd/gonum/graph/internal/ordered"
    14  	"github.com/gopherd/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  		nps, weight := pt.AllBetween(test.NoPathFor.From().ID(), test.NoPathFor.To().ID())
    97  		if nps != nil || !math.IsInf(weight, 1) {
    98  			t.Errorf("%q: unexpected path:\ngot: paths=%v weight=%f\nwant:path=<nil> weight=+Inf",
    99  				test.Name, nps, weight)
   100  		}
   101  	}
   102  }