-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombination_test.go
148 lines (140 loc) · 3.15 KB
/
combination_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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package math
import (
"sort"
"testing"
)
func TestCombinations(t *testing.T) {
cases := [][]uint{
[]uint{10, 0, 1},
[]uint{10, 10, 1},
[]uint{10, 1, 10},
[]uint{10, 9, 10},
[]uint{10, 3, 120},
[]uint{10, 5, 252},
[]uint{40, 10, 847660528},
[]uint{90, 10, 5720645481903},
}
for _, c := range cases {
expected := c[2]
actual := Combinations(c[0], c[1])
if actual != expected {
t.Errorf("Combinations(%d, %d): Expected %d, got %d", c[0], c[1], expected, actual)
}
}
}
/*
A function that treats a boolean vector as a binary number,
converting it to an integer value.
*/
func permToInt(perm []bool) int {
val := 0
for i, v := range perm {
if v {
bit := 1 << uint(i)
val = val | bit
}
}
return val
}
func TestPermToInt(t *testing.T) {
cases := [][]bool{
[]bool{},
[]bool{true},
[]bool{false, true},
[]bool{true, true},
[]bool{false, false, true},
[]bool{true, false, true},
[]bool{false, true, true},
[]bool{true, true, true},
}
for expected, perm := range cases {
actual := permToInt(perm)
if actual != expected {
t.Errorf("PermToInt incorrect. Expected %d, got %d", expected, actual)
}
}
}
func TestChooseZero(t *testing.T) {
// What happens if you choose zero elements?
calls := 0
WalkCombinations(0, 0, func(perm []bool) {
calls += 1
if len(perm) != 0 {
t.Errorf("Expected empty permutation, got %v", perm)
}
})
if calls != 1 {
t.Errorf("WalkCombinations(0, 0): expected 1 callback, got %d", calls)
}
calls = 0
WalkCombinations(100, 0, func(perm []bool) {
calls += 1
if len(perm) != 100 {
t.Errorf("Expected len(perm) == 100, got %d", len(perm))
}
for _, v := range perm {
if v {
t.Errorf("Expected all items in permutation to be false! %v", perm)
}
}
})
if calls != 1 {
t.Errorf("WalkCombinations(100, 0): expected 1 callback, got %d", calls)
}
}
func TestChooseOne(t *testing.T) {
calls := make([]int, 0, 10)
WalkCombinations(10, 1, func(perm []bool) {
calls = append(calls, permToInt(perm))
})
expected := []int{
1 << 0, // one bit at a time
1 << 1,
1 << 2,
1 << 3,
1 << 4,
1 << 5,
1 << 6,
1 << 7,
1 << 8,
1 << 9,
}
if len(calls) != len(expected) {
t.Errorf("WalkCombinations(10, 1): expected %v, got %v.", expected, calls)
} else {
sort.Sort(sort.IntSlice(calls))
for i, v := range calls {
if v != expected[i] {
t.Errorf("WalkCombinations(10, 1), call %d: expected %v, got %v.", i, expected[i], v)
}
}
}
}
func TestChooseAll(t *testing.T) {
calls := make([]int, 0, 10)
WalkCombinations(10, 10, func(perm []bool) {
calls = append(calls, permToInt(perm))
})
expected := []int{
1<<10 - 1, // all should be true
}
if len(calls) != len(expected) {
t.Errorf("Expected %v, got %v.", expected, calls)
} else {
for i, v := range calls {
if v != expected[i] {
t.Errorf("WalkCombinations(10, 10), call %d: expected %v, got %v.", i, expected[i], v)
}
}
}
}
func TestChooseSome(t *testing.T) {
var calls uint = 0
WalkCombinations(10, 4, func(perm []bool) {
calls += 1
})
expected := Combinations(10, 4)
if calls != expected {
t.Errorf("WalkCombinations(10, 4): expected %v callbacks, got %v.", expected, calls)
}
}