-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmatrix_test.go
113 lines (103 loc) · 1.92 KB
/
matrix_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package dlx
import (
"fmt"
"sort"
"strings"
"testing"
)
var table = []struct {
nColumns int
rows [][]int
solutions [][][]int
}{
{ // 0
nColumns: 4,
rows: [][]int{
[]int{2, 3},
},
solutions: nil,
},
{ // 1
nColumns: 7,
rows: [][]int{
[]int{2, 4, 5},
[]int{0, 3, 6},
[]int{1, 2, 5},
[]int{0, 3},
[]int{1, 6},
[]int{3, 4, 6},
},
solutions: [][][]int{
[][]int{
[]int{2, 4, 5},
[]int{0, 3},
[]int{1, 6},
},
},
},
{ // 2
nColumns: 4,
rows: [][]int{
[]int{0, 1},
[]int{0, 2},
[]int{1, 2},
},
solutions: nil,
},
{ // 3
nColumns: 4,
rows: [][]int{
[]int{0, 1, 2},
[]int{0, 2},
[]int{1},
[]int{3},
},
solutions: [][][]int{
[][]int{
[]int{0, 1, 2},
[]int{3},
},
[][]int{
[]int{0, 2},
[]int{1},
[]int{3},
},
},
},
}
func TestSolve(t *testing.T) {
t.Parallel()
for i, v := range table {
t.Logf("Test-case %d", i)
m := NewMatrix(v.nColumns)
for _, r := range v.rows {
m.AddRow(r...)
}
var solutions [][][]int
m.Solve(SolutionAccepterFunc(func(cs [][]int) bool {
solutions = append(solutions, cs)
return false
}))
solutionsStr := toString(solutions)
expectedSolutionsStr := toString(v.solutions)
if solutionsStr != expectedSolutionsStr {
t.Errorf("Failed test-case %d:\nExpected solutions:\n%s\nActual solutions:\n%s\n", i, expectedSolutionsStr, solutionsStr)
}
}
}
func toString(solutions [][][]int) string {
var solutionsStrs []string
for _, solutions := range solutions {
var solutionStrs []string
for _, row := range solutions {
rowStr := fmt.Sprintf(" %v", row)
solutionStrs = append(solutionStrs, rowStr)
}
sort.Strings(solutionStrs)
solutionStr := "[\n" + strings.Join(solutionStrs, ",\n") + "\n]"
solutionsStrs = append(solutionsStrs, solutionStr)
}
sort.Strings(solutionsStrs)
solutionsStr := strings.Join(solutionsStrs, ",\n")
return solutionsStr
}