/
func_test.go
223 lines (193 loc) · 4.33 KB
/
func_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
213
214
215
216
217
218
219
220
221
222
223
package funcs
import (
"testing"
)
func double(a int) int {
return a * 2
}
func TestMapInt(t *testing.T) {
a := []int{1, 2, 3}
b := Map(a, double).(chan int)
x := <- b
y := <- b
z := <- b
if x != 2 || y != 4 || z != 6 {
t.Errorf("does not equal [2, 4, 6]")
}
}
func TestPMapInt(t *testing.T) {
a := []int{1, 2, 3}
b := PMap(a, double).(chan int)
x := <- b
y := <- b
z := <- b
if x != 2 || y != 4 || z != 6 {
t.Errorf("does not equal [2, 4, 6]")
}
}
func stringLength(s string) int {
return len(s)
}
func TestMapString(t *testing.T) {
a := []string{"a", "bee", "sea", "dee", "eff", "gee", "aitch"}
b := Maps(a, stringLength).([]int)
if len(b) != 7 {
t.Error("Expecting seven values")
}
if b[0] != 1 || b[1] != 3 || b[6] != 5 {
t.Errorf("%v does not equal [1, 3, 3, 3, 3, 3, 5]", b)
}
}
func TestPMapString(t *testing.T) {
a := []string{"a", "bee", "sea", "dee", "eff", "gee", "aitch"}
b := PMaps(a, stringLength).([]int)
if len(b) != 7 {
t.Error("Expecting seven values")
}
if b[0] != 1 || b[1] != 3 || b[6] != 5 {
t.Errorf("%v does not equal [1, 3, 3, 3, 3, 3, 5]", b)
}
}
type TestStruct struct {
a int
b int
}
func doToTest(ts *TestStruct) (*TestStruct) {
ts.b = ts.a + 1
return ts
}
func TestMapStruct(t *testing.T) {
ts1 := &TestStruct{100, 0}
ts2 := &TestStruct{200, 0}
a := []*TestStruct{ts1, ts2}
b := Maps(a, doToTest).([]*TestStruct)
if b[0].b != 101 || b[1].b != 201 {
t.Errorf("%v does not equal the expected result")
}
}
func TestMapChan(t *testing.T) {
in := make(chan int)
out := Map(in, double).(chan int)
in <- 1
result := <- out
if result != 2 {
t.Errorf("%v does not equal 2", result)
}
}
func isEven(a int) bool {
return a % 2 == 0
}
func TestFilters(t *testing.T) {
a := []int{1, 2, 3, 4, 5}
b := Filters(a, isEven).([]int)
if len(b) != 2 {
t.Error("Expecting two results")
}
if b[0] != 2 || b[1] != 4 {
t.Error("Unexpected result:", b)
}
}
func add(a, b int) int {
return a + b
}
func TestReduce(t *testing.T) {
a := []int{1, 2, 3, 4}
b := Reduce(a, add).(int)
if b != 10 {
t.Error("Expecting 10, got:", b)
}
}
func appendSlice(l []int, v int) []int {
return append(l, v)
}
func TestReduce2(t *testing.T) {
a := []int{1, 2, 3, 4, 5}
b := Filter(a, isEven)
c := Reduce(b, appendSlice).([]int)
if len(c) != 2 {
t.Error("Should have length 2, has: ", len(c))
}
if c[0] != 2 && c[1] != 4 {
t.Error("Results not [2, 4]:", c)
}
}
func TestCombine(t *testing.T) {
a := []int{1, 2, 3, 4, 5, 6}
b := PMap(a, func(x int) int { return x * 3})
c := Filter(b, isEven)
d := Reduce(c, add).(int)
if d != 36 {
t.Error("Expecting 36 was:", d)
}
}
func TestGroupBy(t *testing.T) {
a := []string{"a", "bee", "sea", "aitch", "doubleyou", "zed"}
b := GroupBy(a, stringLength).(map[int][]string)
if len(b) != 4 {
t.Error("Should have length 4, has: ", len(b), " b: ", b)
}
if len(b[3]) != 3 {
t.Error("Should have three items of length 3, has: ", b[3])
}
if b[9][0] != "doubleyou" {
t.Error("Expecting 'doubleyou' in position 9: ", b)
}
}
type IndexableStruct struct {
id int
data string
}
func TestIndexBy(t *testing.T) {
metadata := map[string]string{"ABC": "A12", "XYZ": "911", "BBB": "12X"}
a := []*IndexableStruct{&IndexableStruct{1, "ABC"}, &IndexableStruct{2, "BBB"}, &IndexableStruct{3, "XYZ"}}
b := IndexBy(a, func(s *IndexableStruct) string { return metadata[s.data] }).(map[string]*IndexableStruct)
if len(b) != 3 {
t.Error("Expecting on three items, actually: ", b)
}
if b["911"].data != "XYZ" {
t.Error("Not expected: ", b)
}
if b["12X"].id != 2 {
t.Error("Not expected: ", b)
}
}
func fib(idx int) int {
if idx == 0 {
return 1
} else if idx == 1 {
return 1
} else {
return fib(idx - 2) + fib(idx - 1)
}
}
const testDataSize = 20
func sourceData() (source []int) {
source = make([]int, testDataSize)
for i := 0; i < testDataSize; i++ {
source[i] = 20
}
return
}
func BenchmarkNone(b *testing.B) {
source := sourceData()
for i := 0; i < b.N; i++ {
dest := make([]int, testDataSize)
for idx, val := range source {
dest[idx] = fib(val)
}
}
}
func BenchmarkMap(b *testing.B) {
source := sourceData()
for i := 0; i < b.N; i++ {
fibs := Maps(source, fib).([]int)
b.Log(len(fibs))
}
}
func BenchmarkPMap(b *testing.B) {
source := sourceData()
for i := 0; i < b.N; i++ {
fibs := PMaps(source, fib).([]int)
b.Log(len(fibs))
}
}