github.com/decred/dcrlnd@v0.7.6/autopilot/centrality_testdata_test.go (about)

     1  package autopilot
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/decred/dcrd/dcrec/secp256k1/v4"
     7  	"github.com/decred/dcrd/dcrutil/v4"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  // testGraphDesc is a helper type to describe a test graph.
    12  type testGraphDesc struct {
    13  	nodes int
    14  	edges map[int][]int
    15  }
    16  
    17  var centralityTestGraph = testGraphDesc{
    18  	nodes: 9,
    19  	edges: map[int][]int{
    20  		0: {1, 2, 3},
    21  		1: {2},
    22  		2: {3},
    23  		3: {4, 5},
    24  		4: {5, 6, 7},
    25  		5: {6, 7},
    26  		6: {7, 8},
    27  	},
    28  }
    29  
    30  var testGraphCentrality = []float64{
    31  	3.0, 0.0, 3.0, 15.0, 6.0, 6.0, 7.0, 0.0, 0.0,
    32  }
    33  
    34  var normalizedTestGraphCentrality = []float64{
    35  	0.2, 0.0, 0.2, 1.0, 0.4, 0.4, 7.0 / 15.0, 0.0, 0.0,
    36  }
    37  
    38  // buildTestGraph builds a test graph from a passed graph desriptor.
    39  func buildTestGraph(t *testing.T,
    40  	graph testGraph, desc testGraphDesc) map[int]*secp256k1.PublicKey {
    41  
    42  	nodes := make(map[int]*secp256k1.PublicKey)
    43  
    44  	for i := 0; i < desc.nodes; i++ {
    45  		key, err := graph.addRandNode()
    46  		require.NoError(t, err, "cannot create random node")
    47  
    48  		nodes[i] = key
    49  	}
    50  
    51  	const chanCapacity = dcrutil.AtomsPerCoin
    52  	for u, neighbors := range desc.edges {
    53  		for _, v := range neighbors {
    54  			_, _, err := graph.addRandChannel(
    55  				nodes[u], nodes[v], chanCapacity,
    56  			)
    57  			require.NoError(t, err,
    58  				"unexpected error adding random channel",
    59  			)
    60  			if err != nil {
    61  				t.Fatalf("unexpected error adding"+
    62  					"random channel: %v", err)
    63  			}
    64  		}
    65  	}
    66  
    67  	return nodes
    68  }