stats/combin: add CartesianGenerator (#1111)

* stats/combin: add CartesianGenerator
This commit is contained in:
Nils Wogatzky
2019-10-13 11:54:04 +02:00
committed by Brendan Tracey
parent f714310635
commit c0d3984e72
3 changed files with 127 additions and 8 deletions

View File

@@ -298,6 +298,38 @@ func TestCartesian(t *testing.T) {
}
}
func TestNumCartesianProducts(t *testing.T) {
want := 6
got := Card([]int{1, 2, 3})
if want != got {
t.Errorf("number of cartesian products mismatch.\nwant:\n%v\ngot:\n%v", want, got)
}
}
func TestCartesianGenerator(t *testing.T) {
want := [][]int{
{0, 0, 0},
{0, 0, 1},
{0, 0, 2},
{0, 1, 0},
{0, 1, 1},
{0, 1, 2},
}
gen := NewCartesianGenerator([]int{1, 2, 3})
iterations := 0
for gen.Next() {
got := gen.Product()
if !reflect.DeepEqual(got, want[iterations]) {
t.Errorf("Cartesian product does not match. want: %v got: %v", want[iterations], got)
}
iterations++
}
if iterations != len(want) {
t.Errorf("Number of products does not match. want: %v got: %v", len(want), iterations)
}
}
func TestPermutationIndex(t *testing.T) {
for cas, s := range []struct {
n, k int