mirror of
https://github.com/gonum/gonum.git
synced 2025-10-07 16:11:03 +08:00
all: make tests pass when -count is greater than 1
Tests run repeatedly do not reinitialise state, meaning that a second run of the tests will have leftover state from the previous run. This ensures that repeated runs are identical with the exception of map iteration order.
This commit is contained in:
@@ -17,7 +17,8 @@ type graphBuilder interface {
|
|||||||
graph.Builder
|
graph.Builder
|
||||||
}
|
}
|
||||||
|
|
||||||
var copyTests = []struct {
|
func TestCopy(t *testing.T) {
|
||||||
|
copyTests := []struct {
|
||||||
desc string
|
desc string
|
||||||
|
|
||||||
src graph.Graph
|
src graph.Graph
|
||||||
@@ -123,7 +124,6 @@ var copyTests = []struct {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCopy(t *testing.T) {
|
|
||||||
for _, test := range copyTests {
|
for _, test := range copyTests {
|
||||||
graph.Copy(test.dst, test.src)
|
graph.Copy(test.dst, test.src)
|
||||||
want := test.want
|
want := test.want
|
||||||
@@ -141,7 +141,8 @@ type graphWeightedBuilder interface {
|
|||||||
graph.WeightedBuilder
|
graph.WeightedBuilder
|
||||||
}
|
}
|
||||||
|
|
||||||
var copyWeightedTests = []struct {
|
func TestCopyWeighted(t *testing.T) {
|
||||||
|
copyWeightedTests := []struct {
|
||||||
desc string
|
desc string
|
||||||
|
|
||||||
src graph.Weighted
|
src graph.Weighted
|
||||||
@@ -247,7 +248,6 @@ var copyWeightedTests = []struct {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCopyWeighted(t *testing.T) {
|
|
||||||
for _, test := range copyWeightedTests {
|
for _, test := range copyWeightedTests {
|
||||||
graph.CopyWeighted(test.dst, test.src)
|
graph.CopyWeighted(test.dst, test.src)
|
||||||
want := test.want
|
want := test.want
|
||||||
|
@@ -19,7 +19,8 @@ import (
|
|||||||
. "gonum.org/v1/gonum/graph/layout"
|
. "gonum.org/v1/gonum/graph/layout"
|
||||||
)
|
)
|
||||||
|
|
||||||
var eadesR2Tests = []struct {
|
func TestEadesR2(t *testing.T) {
|
||||||
|
eadesR2Tests := []struct {
|
||||||
name string
|
name string
|
||||||
g graph.Graph
|
g graph.Graph
|
||||||
param EadesR2
|
param EadesR2
|
||||||
@@ -196,7 +197,6 @@ var eadesR2Tests = []struct {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEadesR2(t *testing.T) {
|
|
||||||
for _, test := range eadesR2Tests {
|
for _, test := range eadesR2Tests {
|
||||||
eades := test.param
|
eades := test.param
|
||||||
o := NewOptimizerR2(test.g, eades.Update)
|
o := NewOptimizerR2(test.g, eades.Update)
|
||||||
|
@@ -28,7 +28,8 @@ var (
|
|||||||
arch string
|
arch string
|
||||||
)
|
)
|
||||||
|
|
||||||
var isomapR2Tests = []struct {
|
func TestIsomapR2(t *testing.T) {
|
||||||
|
isomapR2Tests := []struct {
|
||||||
name string
|
name string
|
||||||
g graph.Graph
|
g graph.Graph
|
||||||
}{
|
}{
|
||||||
@@ -159,7 +160,6 @@ var isomapR2Tests = []struct {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIsomapR2(t *testing.T) {
|
|
||||||
for _, test := range isomapR2Tests {
|
for _, test := range isomapR2Tests {
|
||||||
o := NewOptimizerR2(test.g, IsomapR2{}.Update)
|
o := NewOptimizerR2(test.g, IsomapR2{}.Update)
|
||||||
var n int
|
var n int
|
||||||
|
@@ -21,7 +21,12 @@ import (
|
|||||||
// weightedlines
|
// weightedlines
|
||||||
// empty
|
// empty
|
||||||
|
|
||||||
var nodesOfTests = []struct {
|
type basicNodes struct {
|
||||||
|
graph.Nodes
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNodesOf(t *testing.T) {
|
||||||
|
nodesOfTests := []struct {
|
||||||
name string
|
name string
|
||||||
nodes graph.Nodes
|
nodes graph.Nodes
|
||||||
want []graph.Node
|
want []graph.Node
|
||||||
@@ -58,11 +63,6 @@ var nodesOfTests = []struct {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
type basicNodes struct {
|
|
||||||
graph.Nodes
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestNodesOf(t *testing.T) {
|
|
||||||
for _, test := range nodesOfTests {
|
for _, test := range nodesOfTests {
|
||||||
got := graph.NodesOf(test.nodes)
|
got := graph.NodesOf(test.nodes)
|
||||||
if !reflect.DeepEqual(got, test.want) {
|
if !reflect.DeepEqual(got, test.want) {
|
||||||
@@ -71,7 +71,12 @@ func TestNodesOf(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var edgesOfTests = []struct {
|
type basicEdges struct {
|
||||||
|
graph.Edges
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEdgesOf(t *testing.T) {
|
||||||
|
edgesOfTests := []struct {
|
||||||
name string
|
name string
|
||||||
edges graph.Edges
|
edges graph.Edges
|
||||||
want []graph.Edge
|
want []graph.Edge
|
||||||
@@ -119,11 +124,6 @@ var edgesOfTests = []struct {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
type basicEdges struct {
|
|
||||||
graph.Edges
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestEdgesOf(t *testing.T) {
|
|
||||||
for _, test := range edgesOfTests {
|
for _, test := range edgesOfTests {
|
||||||
got := graph.EdgesOf(test.edges)
|
got := graph.EdgesOf(test.edges)
|
||||||
if !reflect.DeepEqual(got, test.want) {
|
if !reflect.DeepEqual(got, test.want) {
|
||||||
@@ -132,7 +132,12 @@ func TestEdgesOf(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var weightedEdgesOfTests = []struct {
|
type basicWeightedEdges struct {
|
||||||
|
graph.WeightedEdges
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWeightedEdgesOf(t *testing.T) {
|
||||||
|
weightedEdgesOfTests := []struct {
|
||||||
name string
|
name string
|
||||||
edges graph.WeightedEdges
|
edges graph.WeightedEdges
|
||||||
want []graph.WeightedEdge
|
want []graph.WeightedEdge
|
||||||
@@ -180,11 +185,6 @@ var weightedEdgesOfTests = []struct {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
type basicWeightedEdges struct {
|
|
||||||
graph.WeightedEdges
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestWeightedEdgesOf(t *testing.T) {
|
|
||||||
for _, test := range weightedEdgesOfTests {
|
for _, test := range weightedEdgesOfTests {
|
||||||
got := graph.WeightedEdgesOf(test.edges)
|
got := graph.WeightedEdgesOf(test.edges)
|
||||||
if !reflect.DeepEqual(got, test.want) {
|
if !reflect.DeepEqual(got, test.want) {
|
||||||
@@ -193,7 +193,12 @@ func TestWeightedEdgesOf(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var linesOfTests = []struct {
|
type basicLines struct {
|
||||||
|
graph.Lines
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLinesOf(t *testing.T) {
|
||||||
|
linesOfTests := []struct {
|
||||||
name string
|
name string
|
||||||
lines graph.Lines
|
lines graph.Lines
|
||||||
want []graph.Line
|
want []graph.Line
|
||||||
@@ -241,11 +246,6 @@ var linesOfTests = []struct {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
type basicLines struct {
|
|
||||||
graph.Lines
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestLinesOf(t *testing.T) {
|
|
||||||
for _, test := range linesOfTests {
|
for _, test := range linesOfTests {
|
||||||
got := graph.LinesOf(test.lines)
|
got := graph.LinesOf(test.lines)
|
||||||
if !reflect.DeepEqual(got, test.want) {
|
if !reflect.DeepEqual(got, test.want) {
|
||||||
@@ -254,7 +254,12 @@ func TestLinesOf(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var weightedLinesOfTests = []struct {
|
type basicWeightedLines struct {
|
||||||
|
graph.WeightedLines
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWeightedLinesOf(t *testing.T) {
|
||||||
|
weightedLinesOfTests := []struct {
|
||||||
name string
|
name string
|
||||||
lines graph.WeightedLines
|
lines graph.WeightedLines
|
||||||
want []graph.WeightedLine
|
want []graph.WeightedLine
|
||||||
@@ -302,11 +307,6 @@ var weightedLinesOfTests = []struct {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
type basicWeightedLines struct {
|
|
||||||
graph.WeightedLines
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestWeightedLinesOf(t *testing.T) {
|
|
||||||
for _, test := range weightedLinesOfTests {
|
for _, test := range weightedLinesOfTests {
|
||||||
got := graph.WeightedLinesOf(test.lines)
|
got := graph.WeightedLinesOf(test.lines)
|
||||||
if !reflect.DeepEqual(got, test.want) {
|
if !reflect.DeepEqual(got, test.want) {
|
||||||
|
@@ -163,11 +163,12 @@ func TestMetropolisHastings(t *testing.T) {
|
|||||||
compareNormal(t, target, batch, nil, 5e-1, 5e-1)
|
compareNormal(t, target, batch, nil, 5e-1, 5e-1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// randomNormal constructs a random Normal distribution.
|
// randomNormal constructs a random Normal distribution using the provided
|
||||||
|
// random source.
|
||||||
func randomNormal(dim int, src *rand.Rand) (*distmv.Normal, bool) {
|
func randomNormal(dim int, src *rand.Rand) (*distmv.Normal, bool) {
|
||||||
data := make([]float64, dim*dim)
|
data := make([]float64, dim*dim)
|
||||||
for i := range data {
|
for i := range data {
|
||||||
data[i] = rand.Float64()
|
data[i] = src.Float64()
|
||||||
}
|
}
|
||||||
a := mat.NewDense(dim, dim, data)
|
a := mat.NewDense(dim, dim, data)
|
||||||
var sigma mat.SymDense
|
var sigma mat.SymDense
|
||||||
@@ -180,6 +181,8 @@ func randomNormal(dim int, src *rand.Rand) (*distmv.Normal, bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func compareNormal(t *testing.T, want *distmv.Normal, batch *mat.Dense, weights []float64, meanTol, covTol float64) {
|
func compareNormal(t *testing.T, want *distmv.Normal, batch *mat.Dense, weights []float64, meanTol, covTol float64) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
dim := want.Dim()
|
dim := want.Dim()
|
||||||
mu := want.Mean(nil)
|
mu := want.Mean(nil)
|
||||||
var sigma mat.SymDense
|
var sigma mat.SymDense
|
||||||
@@ -224,10 +227,11 @@ func TestMetropolisHastingser(t *testing.T) {
|
|||||||
{3, 103, 11, 51},
|
{3, 103, 11, 51},
|
||||||
{3, 103, 51, 11},
|
{3, 103, 51, 11},
|
||||||
} {
|
} {
|
||||||
|
src := rand.New(rand.NewSource(1))
|
||||||
dim := test.dim
|
dim := test.dim
|
||||||
|
|
||||||
initial := make([]float64, dim)
|
initial := make([]float64, dim)
|
||||||
target, ok := randomNormal(dim, nil)
|
target, ok := randomNormal(dim, src)
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Fatal("bad test, sigma not pos def")
|
t.Fatal("bad test, sigma not pos def")
|
||||||
}
|
}
|
||||||
@@ -239,7 +243,7 @@ func TestMetropolisHastingser(t *testing.T) {
|
|||||||
|
|
||||||
// Test the Metropolis Hastingser by generating all the samples, then generating
|
// Test the Metropolis Hastingser by generating all the samples, then generating
|
||||||
// the same samples with a burnin and rate.
|
// the same samples with a burnin and rate.
|
||||||
src := rand.New(rand.NewSource(1))
|
src = rand.New(rand.NewSource(1))
|
||||||
proposal, ok := NewProposalNormal(sigmaImp, src)
|
proposal, ok := NewProposalNormal(sigmaImp, src)
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Fatal("bad test, sigma not pos def")
|
t.Fatal("bad test, sigma not pos def")
|
||||||
|
@@ -185,7 +185,8 @@ func TestDimensionEquality(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var operationTests = []struct {
|
func TestOperations(t *testing.T) {
|
||||||
|
operationTests := []struct {
|
||||||
recvOp func(Uniter) *Unit
|
recvOp func(Uniter) *Unit
|
||||||
param Uniter
|
param Uniter
|
||||||
want Uniter
|
want Uniter
|
||||||
@@ -197,8 +198,6 @@ var operationTests = []struct {
|
|||||||
{Dimless(1).Unit().Div, Length(2), New(0.5, Dimensions{LengthDim: -1})},
|
{Dimless(1).Unit().Div, Length(2), New(0.5, Dimensions{LengthDim: -1})},
|
||||||
{Length(1).Unit().Div, Dimless(2), Length(0.5)},
|
{Length(1).Unit().Div, Dimless(2), Length(0.5)},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestOperations(t *testing.T) {
|
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
for i, test := range operationTests {
|
for i, test := range operationTests {
|
||||||
var got Uniter
|
var got Uniter
|
||||||
|
Reference in New Issue
Block a user