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