gonum.org/v1/gonum@v0.14.0/graph/graphs/gen/small_world_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 gen
     6  
     7  import (
     8  	"testing"
     9  
    10  	"gonum.org/v1/gonum/graph/simple"
    11  )
    12  
    13  var smallWorldDimensionParameters = [][]int{
    14  	{50},
    15  	{10, 10},
    16  	{6, 5, 4},
    17  }
    18  
    19  func TestNavigableSmallWorldUndirected(t *testing.T) {
    20  	t.Parallel()
    21  	for p := 1; p < 5; p++ {
    22  		for q := 0; q < 10; q++ {
    23  			for r := 0.5; r < 10; r++ {
    24  				for _, dims := range smallWorldDimensionParameters {
    25  					g := &gnUndirected{UndirectedBuilder: simple.NewUndirectedGraph()}
    26  					orig := g.NewNode()
    27  					g.AddNode(orig)
    28  					err := NavigableSmallWorld(g, dims, p, q, r, nil)
    29  					n := 1
    30  					for _, d := range dims {
    31  						n *= d
    32  					}
    33  					if err != nil {
    34  						t.Fatalf("unexpected error: dims=%v n=%d, p=%d, q=%d, r=%v: %v", dims, n, p, q, r, err)
    35  					}
    36  					if g.From(orig.ID()).Len() != 0 {
    37  						t.Errorf("edge added from already existing node: dims=%v n=%d, p=%d, q=%d, r=%v", dims, n, p, q, r)
    38  					}
    39  					if g.addBackwards {
    40  						t.Errorf("edge added with From.ID > To.ID: dims=%v n=%d, p=%d, q=%d, r=%v", dims, n, p, q, r)
    41  					}
    42  					if g.addSelfLoop {
    43  						t.Errorf("unexpected self edge: dims=%v n=%d, p=%d, q=%d, r=%v", dims, n, p, q, r)
    44  					}
    45  					if g.addMultipleEdge {
    46  						t.Errorf("unexpected multiple edge: dims=%v n=%d, p=%d, q=%d, r=%v", dims, n, p, q, r)
    47  					}
    48  				}
    49  			}
    50  		}
    51  	}
    52  }
    53  
    54  func TestNavigableSmallWorldDirected(t *testing.T) {
    55  	t.Parallel()
    56  	for p := 1; p < 5; p++ {
    57  		for q := 0; q < 10; q++ {
    58  			for r := 0.5; r < 10; r++ {
    59  				for _, dims := range smallWorldDimensionParameters {
    60  					g := &gnDirected{DirectedBuilder: simple.NewDirectedGraph()}
    61  					err := NavigableSmallWorld(g, dims, p, q, r, nil)
    62  					n := 1
    63  					for _, d := range dims {
    64  						n *= d
    65  					}
    66  					if err != nil {
    67  						t.Fatalf("unexpected error: dims=%v n=%d, p=%d, q=%d, r=%v: %v", dims, n, p, q, r, err)
    68  					}
    69  					if g.addSelfLoop {
    70  						t.Errorf("unexpected self edge: dims=%v n=%d, p=%d, q=%d, r=%v", dims, n, p, q, r)
    71  					}
    72  					if g.addMultipleEdge {
    73  						t.Errorf("unexpected multiple edge: dims=%v n=%d, p=%d, q=%d, r=%v", dims, n, p, q, r)
    74  					}
    75  				}
    76  			}
    77  		}
    78  	}
    79  }