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.
MODIFY
graph/graph_test.go
graph/graph_test.go
@@ -17,7 +17,8 @@ type graphBuilder interface {
graph.Builder
}
-var copyTests = []struct {
+func TestCopy(t *testing.T) {
+ copyTests := []struct {
desc string
src graph.Graph
@@ -123,7 +124,6 @@ var copyTests = []struct {
},
}
-func TestCopy(t *testing.T) {
for _, test := range copyTests {
graph.Copy(test.dst, test.src)
want := test.want
@@ -141,7 +141,8 @@ type graphWeightedBuilder interface {
graph.WeightedBuilder
}
-var copyWeightedTests = []struct {
+func TestCopyWeighted(t *testing.T) {
+ copyWeightedTests := []struct {
desc string
src graph.Weighted
@@ -247,7 +248,6 @@ var copyWeightedTests = []struct {
},
}
-func TestCopyWeighted(t *testing.T) {
for _, test := range copyWeightedTests {
graph.CopyWeighted(test.dst, test.src)
want := test.want

MODIFY
graph/layout/eades_test.go
graph/layout/eades_test.go
@@ -19,7 +19,8 @@ import (
. "gonum.org/v1/gonum/graph/layout"
)
-var eadesR2Tests = []struct {
+func TestEadesR2(t *testing.T) {
+ eadesR2Tests := []struct {
name string
g graph.Graph
param EadesR2
@@ -196,7 +197,6 @@ var eadesR2Tests = []struct {
},
}
-func TestEadesR2(t *testing.T) {
for _, test := range eadesR2Tests {
eades := test.param
o := NewOptimizerR2(test.g, eades.Update)

MODIFY
graph/layout/isomap_test.go
graph/layout/isomap_test.go
@@ -28,7 +28,8 @@ var (
arch string
)
-var isomapR2Tests = []struct {
+func TestIsomapR2(t *testing.T) {
+ isomapR2Tests := []struct {
name string
g graph.Graph
}{
@@ -159,7 +160,6 @@ var isomapR2Tests = []struct {
},
}
-func TestIsomapR2(t *testing.T) {
for _, test := range isomapR2Tests {
o := NewOptimizerR2(test.g, IsomapR2{}.Update)
var n int

MODIFY
graph/nodes_edges_test.go
graph/nodes_edges_test.go
@@ -21,7 +21,12 @@ import (
// weightedlines
// empty
-var nodesOfTests = []struct {
+type basicNodes struct {
+ graph.Nodes
+}
+
+func TestNodesOf(t *testing.T) {
+ nodesOfTests := []struct {
name string
nodes graph.Nodes
want []graph.Node
@@ -58,11 +63,6 @@ var nodesOfTests = []struct {
},
}
-type basicNodes struct {
- graph.Nodes
-}
-
-func TestNodesOf(t *testing.T) {
for _, test := range nodesOfTests {
got := graph.NodesOf(test.nodes)
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
edges graph.Edges
want []graph.Edge
@@ -119,11 +124,6 @@ var edgesOfTests = []struct {
},
}
-type basicEdges struct {
- graph.Edges
-}
-
-func TestEdgesOf(t *testing.T) {
for _, test := range edgesOfTests {
got := graph.EdgesOf(test.edges)
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
edges graph.WeightedEdges
want []graph.WeightedEdge
@@ -180,11 +185,6 @@ var weightedEdgesOfTests = []struct {
},
}
-type basicWeightedEdges struct {
- graph.WeightedEdges
-}
-
-func TestWeightedEdgesOf(t *testing.T) {
for _, test := range weightedEdgesOfTests {
got := graph.WeightedEdgesOf(test.edges)
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
lines graph.Lines
want []graph.Line
@@ -241,11 +246,6 @@ var linesOfTests = []struct {
},
}
-type basicLines struct {
- graph.Lines
-}
-
-func TestLinesOf(t *testing.T) {
for _, test := range linesOfTests {
got := graph.LinesOf(test.lines)
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
lines graph.WeightedLines
want []graph.WeightedLine
@@ -302,11 +307,6 @@ var weightedLinesOfTests = []struct {
},
}
-type basicWeightedLines struct {
- graph.WeightedLines
-}
-
-func TestWeightedLinesOf(t *testing.T) {
for _, test := range weightedLinesOfTests {
got := graph.WeightedLinesOf(test.lines)
if !reflect.DeepEqual(got, test.want) {

MODIFY
stat/samplemv/sample_test.go
stat/samplemv/sample_test.go
@@ -163,11 +163,12 @@ func TestMetropolisHastings(t *testing.T) {
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) {
data := make([]float64, dim*dim)
for i := range data {
- data[i] = rand.Float64()
+ data[i] = src.Float64()
}
a := mat.NewDense(dim, dim, data)
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) {
+ t.Helper()
+
dim := want.Dim()
mu := want.Mean(nil)
var sigma mat.SymDense
@@ -224,10 +227,11 @@ func TestMetropolisHastingser(t *testing.T) {
{3, 103, 11, 51},
{3, 103, 51, 11},
} {
+ src := rand.New(rand.NewSource(1))
dim := test.dim
initial := make([]float64, dim)
- target, ok := randomNormal(dim, nil)
+ target, ok := randomNormal(dim, src)
if !ok {
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
// 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)
if !ok {
t.Fatal("bad test, sigma not pos def")

MODIFY
unit/unit_test.go
unit/unit_test.go
@@ -185,7 +185,8 @@ func TestDimensionEquality(t *testing.T) {
}
}
-var operationTests = []struct {
+func TestOperations(t *testing.T) {
+ operationTests := []struct {
recvOp func(Uniter) *Unit
param Uniter
want Uniter
@@ -197,8 +198,6 @@ var operationTests = []struct {
{Dimless(1).Unit().Div, Length(2), New(0.5, Dimensions{LengthDim: -1})},
{Length(1).Unit().Div, Dimless(2), Length(0.5)},
}
-
-func TestOperations(t *testing.T) {
t.Parallel()
for i, test := range operationTests {
var got Uniter

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.