-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpf_test.go
81 lines (71 loc) · 1.5 KB
/
pf_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
package pf
import (
"fmt"
"os"
"runtime"
"testing"
)
func TestMap(t *testing.T) {
// Define a 6x4 image
var pitch int32 = 6
pixels := make([]uint32, 4*pitch)
// Use 1 core for setting all pixels to 42
Map(1, func(x uint32) uint32 {
return 42
}, pixels)
// Check that all pixels are now 42
for i, p := range pixels {
if p != 42 {
fmt.Fprintf(os.Stderr, "Map fail at pixel %d, y %d. Has value: %d\n", i, int32(i)/pitch, p)
t.Fail()
}
}
}
func TestDivide(t *testing.T) {
pixels := make([]uint32, 100)
// 100 divided into 2 slices fits perfectly
slicesOfSlices := Divide(pixels, 2)
if len(slicesOfSlices) != 2 {
t.Fail()
}
if len(slicesOfSlices[0]) != 50 {
t.Fail()
}
if len(slicesOfSlices[1]) != 50 {
t.Fail()
}
// 100 divided into 3 slices + leftover pixels
slicesOfSlices = Divide(pixels, 3)
if len(slicesOfSlices) != 4 {
t.Fail()
}
if len(slicesOfSlices[0]) != 33 {
t.Fail()
}
if len(slicesOfSlices[1]) != 33 {
t.Fail()
}
if len(slicesOfSlices[2]) != 33 {
t.Fail()
}
if len(slicesOfSlices[3]) != 1 {
t.Fail()
}
}
func TestConcurrentMap(t *testing.T) {
// Define a 7x19 image
var pitch int32 = 7
pixels := make([]uint32, 19*pitch)
// Use N cores for setting all pixels to 1337
n := runtime.NumCPU()
Map(n, func(x uint32) uint32 {
return 1337
}, pixels)
// Check that all pixels are now 1337
for i, p := range pixels {
if p != 1337 {
fmt.Fprintf(os.Stderr, "Map fail at pixel %d, y %d. Has value: %d\n", i, int32(i)/pitch, p)
t.Fail()
}
}
}