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:
Dan Kortschak
2022-08-06 10:16:11 +09:30
parent aedb59a6f6
commit a4dda6a99c
6 changed files with 769 additions and 766 deletions

View File

@@ -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
@@ -25,7 +26,7 @@ var copyTests = []struct {
// If want is nil, compare to src. // If want is nil, compare to src.
want graph.Graph want graph.Graph
}{ }{
{ {
desc: "undirected to undirected", desc: "undirected to undirected",
src: func() graph.Graph { src: func() graph.Graph {
@@ -121,9 +122,8 @@ var copyTests = []struct {
}(), }(),
dst: simple.NewDirectedGraph(), dst: simple.NewDirectedGraph(),
}, },
} }
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
@@ -149,7 +150,7 @@ var copyWeightedTests = []struct {
// If want is nil, compare to src. // If want is nil, compare to src.
want graph.Graph want graph.Graph
}{ }{
{ {
desc: "undirected to undirected", desc: "undirected to undirected",
src: func() graph.Weighted { src: func() graph.Weighted {
@@ -245,9 +246,8 @@ var copyWeightedTests = []struct {
}(), }(),
dst: simple.NewWeightedDirectedGraph(0, 0), dst: simple.NewWeightedDirectedGraph(0, 0),
}, },
} }
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

View File

@@ -19,12 +19,13 @@ 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
wantIters int wantIters int
}{ }{
{ {
name: "line", name: "line",
g: func() graph.Graph { g: func() graph.Graph {
@@ -194,9 +195,8 @@ var eadesR2Tests = []struct {
param: EadesR2{Repulsion: 1, Rate: 0.1, Updates: 100, Theta: 0.1, Src: rand.NewSource(1)}, param: EadesR2{Repulsion: 1, Rate: 0.1, Updates: 100, Theta: 0.1, Src: rand.NewSource(1)},
wantIters: 100, wantIters: 100,
}, },
} }
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)

View File

@@ -28,10 +28,11 @@ 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
}{ }{
{ {
name: "line_isomap", name: "line_isomap",
g: func() graph.Graph { g: func() graph.Graph {
@@ -157,9 +158,8 @@ var isomapR2Tests = []struct {
return orderedGraph{g} return orderedGraph{g}
}(), }(),
}, },
} }
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

View File

@@ -21,11 +21,16 @@ 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
}{ }{
{ {
name: "nil", name: "nil",
nodes: nil, nodes: nil,
@@ -56,13 +61,8 @@ var nodesOfTests = []struct {
nodes: iterator.NewOrderedNodes([]graph.Node{simple.Node(-1), simple.Node(0), simple.Node(1), simple.Node(2), simple.Node(3)}), nodes: iterator.NewOrderedNodes([]graph.Node{simple.Node(-1), simple.Node(0), simple.Node(1), simple.Node(2), simple.Node(3)}),
want: []graph.Node{simple.Node(-1), simple.Node(0), simple.Node(1), simple.Node(2), simple.Node(3)}, want: []graph.Node{simple.Node(-1), simple.Node(0), simple.Node(1), simple.Node(2), simple.Node(3)},
}, },
} }
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,11 +71,16 @@ 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
}{ }{
{ {
name: "nil", name: "nil",
edges: nil, edges: nil,
@@ -117,13 +122,8 @@ var edgesOfTests = []struct {
simple.Edge{F: simple.Node(3), T: simple.Node(4)}, simple.Edge{F: simple.Node(3), T: simple.Node(4)},
}, },
}, },
} }
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,11 +132,16 @@ 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
}{ }{
{ {
name: "nil", name: "nil",
edges: nil, edges: nil,
@@ -178,13 +183,8 @@ var weightedEdgesOfTests = []struct {
simple.WeightedEdge{F: simple.Node(3), T: simple.Node(4), W: 3}, simple.WeightedEdge{F: simple.Node(3), T: simple.Node(4), W: 3},
}, },
}, },
} }
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,11 +193,16 @@ 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
}{ }{
{ {
name: "nil", name: "nil",
lines: nil, lines: nil,
@@ -239,13 +244,8 @@ var linesOfTests = []struct {
multi.Line{F: multi.Node(3), T: multi.Node(4), UID: 1}, multi.Line{F: multi.Node(3), T: multi.Node(4), UID: 1},
}, },
}, },
} }
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,11 +254,16 @@ 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
}{ }{
{ {
name: "nil", name: "nil",
lines: nil, lines: nil,
@@ -300,13 +305,8 @@ var weightedLinesOfTests = []struct {
multi.WeightedLine{F: multi.Node(3), T: multi.Node(4), W: 3, UID: 1}, multi.WeightedLine{F: multi.Node(3), T: multi.Node(4), W: 3, UID: 1},
}, },
}, },
} }
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) {

View File

@@ -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")

View File

@@ -185,20 +185,19 @@ 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
}{ }{
{Dimless(1).Unit().Add, Dimless(2), Dimless(3)}, {Dimless(1).Unit().Add, Dimless(2), Dimless(3)},
{Dimless(1).Unit().Mul, Dimless(2), Dimless(2)}, {Dimless(1).Unit().Mul, Dimless(2), Dimless(2)},
{Dimless(1).Unit().Mul, Length(2), Length(2)}, {Dimless(1).Unit().Mul, Length(2), Length(2)},
{Length(1).Unit().Mul, Dimless(2), Length(2)}, {Length(1).Unit().Mul, Dimless(2), Length(2)},
{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