gonum.org/v1/gonum@v0.14.0/graph/graphs/gen/duplication_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"
    11  	"gonum.org/v1/gonum/graph/simple"
    12  )
    13  
    14  type duplication struct {
    15  	UndirectedMutator
    16  	addBackwards    bool
    17  	addSelfLoop     bool
    18  	addMultipleEdge bool
    19  }
    20  
    21  func (g *duplication) SetEdge(e graph.Edge) {
    22  	switch {
    23  	case e.From().ID() == e.To().ID():
    24  		g.addSelfLoop = true
    25  		return
    26  	case e.From().ID() > e.To().ID():
    27  		g.addBackwards = true
    28  	case g.UndirectedMutator.HasEdgeBetween(e.From().ID(), e.To().ID()):
    29  		g.addMultipleEdge = true
    30  	}
    31  
    32  	g.UndirectedMutator.SetEdge(e)
    33  }
    34  
    35  func TestDuplication(t *testing.T) {
    36  	t.Parallel()
    37  	for n := 2; n <= 50; n++ {
    38  		for alpha := 0.1; alpha <= 1; alpha += 0.1 {
    39  			for delta := 0.; delta <= 1; delta += 0.2 {
    40  				for sigma := 0.; sigma <= 1; sigma += 0.2 {
    41  					g := &duplication{UndirectedMutator: simple.NewUndirectedGraph()}
    42  					err := Duplication(g, n, delta, alpha, sigma, nil)
    43  					if err != nil {
    44  						t.Fatalf("unexpected error: n=%d, alpha=%v, delta=%v sigma=%v: %v", n, alpha, delta, sigma, err)
    45  					}
    46  					if g.addBackwards {
    47  						t.Errorf("edge added with From.ID > To.ID: n=%d, alpha=%v, delta=%v sigma=%v", n, alpha, delta, sigma)
    48  					}
    49  					if g.addSelfLoop {
    50  						t.Errorf("unexpected self edge: n=%d, alpha=%v, delta=%v sigma=%v", n, alpha, delta, sigma)
    51  					}
    52  					if g.addMultipleEdge {
    53  						t.Errorf("unexpected multiple edge: n=%d, alpha=%v, delta=%v sigma=%v", n, alpha, delta, sigma)
    54  					}
    55  				}
    56  			}
    57  		}
    58  	}
    59  }