-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproperty_test.go
212 lines (182 loc) · 6.05 KB
/
property_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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// property_test.go offers a very simple property test on the available combinatorics
// functionality.
// If we want all possible length 2 combinations of 10 items, then we can expect to get
// n choose k = n! / (k! * (n-k)!) = 10! / (2! * (10-2)!) = 45. In those 45 cases, we
// should expect to see each number exactly 45 - (9 choose 2) = 45 - 36 = 9 times.
// The above is an example with combinations. We can possibly also do them for other
// functionality as well
package gocombinatorics
import (
"fmt"
"math/big"
"math/rand"
"testing"
)
// Multiple types all adhere to this interface
type combinationLike interface {
Next() bool
LenInds() int
Indices() []int
}
// combinationLikeValueCounter will iterate through something combination like and count
// each unique value, returning it in a map.
// It assumes that the combinationLike has already been created
func combinationLikeValueCounter(c combinationLike) map[int]int {
// Make the result map
result := make(map[int]int, c.LenInds())
// Iterate through the object
for c.Next() {
// Add each item in c.Inds to the result map
for _, num := range c.Indices() {
result[num]++
}
}
return result
}
func TestCombinationsProperties(t *testing.T) {
testCases := []struct {
desc string
n int
k int
num_want_to_see int
}{
{
desc: "n=3, k=2",
n: 3,
k: 2,
num_want_to_see: 2,
},
{
desc: "n=10, k=2",
n: 10,
k: 2,
num_want_to_see: 9,
},
{
desc: "n=10, k=7",
n: 10,
k: 7,
num_want_to_see: 120 - 36,
},
{
desc: "n=50, k=5",
n: 50,
k: 5,
num_want_to_see: 211876,
},
}
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
// Create the combination
data := stepped_range(0, tC.n, 1)
c, err := NewCombinations(data, tC.k)
if err != nil {
t.Errorf("Error creating combinations: %v", err)
}
// Count the number of times each item appears
counts := combinationLikeValueCounter(c)
// Check that each value in counts appears t.num_want_to_see times
for num, count := range counts {
if count != tC.num_want_to_see {
t.Errorf("Expected %v to appear %v times, but it appeared %v times", num, tC.num_want_to_see, count)
}
}
})
}
}
func Test100RandomCombinations(t *testing.T) {
for i := 0; i < 100; i++ {
// Generate two numbers, 1 <= n <= 50, 1 <= k <= n
n := rand.Int63n(50) + 1
k := rand.Int63n(n) + 1
// If any index should appear more than 10_000_000 times, skip this iteration
total_times := nchoosek(uint64(n), uint64(k))
one_less := nchoosek(uint64(n-1), uint64(k))
times_we_see_each_index := big.NewInt(0).Sub(total_times, one_less)
if times_we_see_each_index.Cmp(big.NewInt(10000000)) > 0 {
t.Logf("Skipping test because we see each index more than 10_000_000 times")
continue
}
run_name := fmt.Sprintf("n=%v, k=%v", n, k)
t.Run(run_name, func(t *testing.T) {
// Create the combination
data := stepped_range(0, int(n), 1)
c, err := NewCombinations(data, int(k))
if err != nil {
t.Errorf("Error creating combinations: %v", err)
}
// Count the number of times each item appears
counts := combinationLikeValueCounter(c)
// Check that each value in counts appears t.num_want_to_see times
for num, count := range counts {
count_big := big.NewInt(int64(count))
if count_big.Cmp(times_we_see_each_index) != 0 {
t.Errorf("Expected %v to appear %v times, but it appeared %v times", num, times_we_see_each_index, count)
}
}
})
}
}
func Test100RandomCombinationsWithReplacement(t *testing.T) {
for i := 0; i < 100; i++ {
// Generate two numbers, 1 <= n <= 50, 1 <= k <= n
n := rand.Int63n(15) + 1
k := rand.Int63n(n) + 1
// If any index should appear more than 10_000_000 times, skip this iteration
times_we_see_each_index := elts_in_combo_w_replacement(int(n), int(k))
if times_we_see_each_index.Cmp(big.NewInt(10000000)) > 0 {
t.Logf("Skipping test because we see each index more than 10_000_000 times")
continue
}
run_name := fmt.Sprintf("n=%v, k=%v", n, k)
t.Run(run_name, func(t *testing.T) {
// Create the combination
data := stepped_range(0, int(n), 1)
c, err := NewCombinationsWithReplacement(data, int(k))
if err != nil {
t.Errorf("Error creating CombinationsWithReplacement: %v", err)
}
// Count the number of times each item appears
counts := combinationLikeValueCounter(c)
// Check that each value in counts appears t.num_want_to_see times
for num, count := range counts {
count_big := big.NewInt(int64(count))
if count_big.Cmp(times_we_see_each_index) != 0 {
t.Errorf("Expected %v to appear %v times, but it appeared %v times", num, times_we_see_each_index, count)
}
}
})
}
}
func Test100RandomPermutations(t *testing.T) {
// Do 100 iterations
for i := 0; i < 100; i++ {
// Generate two numbers, 1 <= n <= 50, 1 <= k <= n
n := rand.Int63n(15) + 1
k := rand.Int63n(n) + 1
data := stepped_range(0, int(n), 1)
// If any index should appear more than 10_000_000 times, skip this iteration
times_we_see_each_index := elts_in_permutations(int(n), int(k))
if times_we_see_each_index.Cmp(big.NewInt(10000000)) > 0 {
t.Logf("Skipping test because we see each index more than 10_000_000 times")
continue
}
run_name := fmt.Sprintf("n=%v, k=%v", n, k)
t.Run(run_name, func(t *testing.T) {
// Create the permutation
p, err := NewPermutations(data, int(k))
if err != nil {
t.Errorf("Error creating Permutations: %v", err)
}
// Count the number of times each item appears
var counts map[int]int = combinationLikeValueCounter(p)
// Check that each value in counts appears t.num_want_to_see times
for num, count := range counts {
count_big := big.NewInt(int64(count))
if count_big.Cmp(times_we_see_each_index) != 0 {
t.Errorf("Expected %v to appear %v times, but it appeared %v times", num, times_we_see_each_index, count)
}
}
})
}
}