-
Notifications
You must be signed in to change notification settings - Fork 0
/
kcomb_test.go
171 lines (159 loc) · 3.85 KB
/
kcomb_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
package kcomb
import (
"log"
"reflect"
"testing"
)
func TestCombineGenerator(t *testing.T) {
cases := []struct {
columns []Set
expected []Set
}{
{
columns: []Set{
Set{Datum{"apple"}, Datum{"orange"}},
Set{Datum{"celery"}, Datum{"broccoli"}},
},
expected: []Set{
Set{Datum{"apple"}, Datum{"celery"}},
Set{Datum{"apple"}, Datum{"broccoli"}},
Set{Datum{"orange"}, Datum{"celery"}},
Set{Datum{"orange"}, Datum{"broccoli"}},
},
},
{
columns: []Set{
Set{Datum{1}, Datum{2}},
Set{Datum{3}, Datum{4}},
},
expected: []Set{
Set{Datum{1}, Datum{3}},
Set{Datum{1}, Datum{4}},
Set{Datum{2}, Datum{3}},
Set{Datum{2}, Datum{4}},
},
},
{
columns: []Set{
Set{Datum{1}, Datum{2}, Datum{3}},
Set{Datum{4}, Datum{5}, Datum{6}, Datum{7}},
Set{Datum{8}},
},
expected: []Set{
Set{Datum{1}, Datum{4}, Datum{8}},
Set{Datum{1}, Datum{5}, Datum{8}},
Set{Datum{1}, Datum{6}, Datum{8}},
Set{Datum{1}, Datum{7}, Datum{8}},
Set{Datum{2}, Datum{4}, Datum{8}},
Set{Datum{2}, Datum{5}, Datum{8}},
Set{Datum{2}, Datum{6}, Datum{8}},
Set{Datum{2}, Datum{7}, Datum{8}},
Set{Datum{3}, Datum{4}, Datum{8}},
Set{Datum{3}, Datum{5}, Datum{8}},
Set{Datum{3}, Datum{6}, Datum{8}},
Set{Datum{3}, Datum{7}, Datum{8}},
},
},
}
for idx, tc := range cases {
done := make(chan struct{})
stream := CombineGenerator(done, tc.columns)
streamIdx := 0
for v := range stream {
if reflect.DeepEqual(v, tc.expected[streamIdx]) == false {
t.Fatalf("Case Index: %d, Stream Index: %d - Expected '%v' to equal '%v'", idx, streamIdx, v, tc.expected[streamIdx])
}
streamIdx++
}
}
}
func TestCombine(t *testing.T) {
cases := []struct {
columns []Set
expected []Set
}{
{
columns: []Set{
Set{Datum{"apple"}, Datum{"orange"}},
Set{Datum{"celery"}, Datum{"broccoli"}},
},
expected: []Set{
Set{Datum{"apple"}, Datum{"celery"}},
Set{Datum{"apple"}, Datum{"broccoli"}},
Set{Datum{"orange"}, Datum{"celery"}},
Set{Datum{"orange"}, Datum{"broccoli"}},
},
},
{
columns: []Set{
Set{Datum{1}, Datum{2}},
Set{Datum{3}, Datum{4}},
},
expected: []Set{
Set{Datum{1}, Datum{3}},
Set{Datum{1}, Datum{4}},
Set{Datum{2}, Datum{3}},
Set{Datum{2}, Datum{4}},
},
},
{
columns: []Set{
Set{Datum{1}, Datum{2}, Datum{3}},
Set{Datum{4}, Datum{5}, Datum{6}, Datum{7}},
Set{Datum{8}},
},
expected: []Set{
Set{Datum{1}, Datum{4}, Datum{8}},
Set{Datum{1}, Datum{5}, Datum{8}},
Set{Datum{1}, Datum{6}, Datum{8}},
Set{Datum{1}, Datum{7}, Datum{8}},
Set{Datum{2}, Datum{4}, Datum{8}},
Set{Datum{2}, Datum{5}, Datum{8}},
Set{Datum{2}, Datum{6}, Datum{8}},
Set{Datum{2}, Datum{7}, Datum{8}},
Set{Datum{3}, Datum{4}, Datum{8}},
Set{Datum{3}, Datum{5}, Datum{8}},
Set{Datum{3}, Datum{6}, Datum{8}},
Set{Datum{3}, Datum{7}, Datum{8}},
},
},
}
for idx, tc := range cases {
r := Combine(tc.columns)
t.Logf("Index: %d - Created %d combinations", idx, len(r))
if reflect.DeepEqual(r, tc.expected) == false {
t.Fatalf("Index: %d - Expected '%v' to equal '%v'", idx, r, tc.expected)
}
}
}
func BenchmarkCombine2(b *testing.B) {
for n := 0; n < b.N; n++ {
benchmarkCombine(2, b)
}
}
func BenchmarkCombine10(b *testing.B) {
for n := 0; n < b.N; n++ {
benchmarkCombine(10, b)
}
}
func BenchmarkCombine50(b *testing.B) {
for n := 0; n < b.N; n++ {
benchmarkCombine(50, b)
}
}
func BenchmarkCombine1000(b *testing.B) {
for n := 0; n < b.N; n++ {
benchmarkCombine(1000, b)
}
}
func benchmarkCombine(n int, b *testing.B) {
col1 := make(Set, n)
col2 := make(Set, n)
col3 := make(Set, n)
for i := 1; i < n; i++ {
col1[i] = Datum{i}
col2[i] = Datum{i}
col3[i] = Datum{i}
}
log.Printf("created %d combinations", len(Combine([]Set{col1, col2, col3})))
}