This repository has been archived by the owner on Oct 1, 2020. It is now read-only.
/
map_test.go
78 lines (61 loc) · 1.41 KB
/
map_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
package par
import (
"fmt"
"math"
"math/rand"
"testing"
)
const maxsize = 1025
func expensive(v float64) float64 {
for i := 0; i < 100; i++ {
v = math.J0(v)
}
return v
}
func cheap(v float64) float64 {
return math.Sin(v)
}
func makeData(n uint) []float64 {
data := make([]float64, n)
for i := uint(0); i < n; i++ {
data[i] = rand.Float64()
}
return data
}
func mapSerial(f func(float64) float64, l []float64) []float64 {
result := make([]float64, len(l))
for idx, val := range l {
result[idx] = f(val)
}
return result
}
func TestParMapChunked(t *testing.T) {
for i := uint(1); i < maxsize; i *= 2 {
data := makeData(i)
r := MapFloat64Chunked(cheap, data)
check := mapSerial(cheap, data)
compareVectors(t, r, check, fmt.Sprintf("ParMapChunked(%d)", i))
}
}
func TestParMapInterleaved(t *testing.T) {
for i := uint(1); i < maxsize; i *= 2 {
data := makeData(i)
r := MapFloat64Interleaved(cheap, data)
check := mapSerial(cheap, data)
compareVectors(t, r, check, fmt.Sprintf("ParMapInterleaved(%d)", i))
}
}
func compareVectors(t *testing.T, candidate, check []float64, candidateName string) {
// Sanity check
if len(candidate) != len(check) {
t.Errorf("len(%s) = %d, want %d", candidateName, len(candidate),
len(check))
return
}
for idx, val := range check {
if candidate[idx] != val {
t.Errorf("%s[%d] = %f, want %f", candidateName, idx,
candidate[idx], val)
}
}
}