-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparallel_test.go
95 lines (88 loc) · 2.2 KB
/
parallel_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
package goneric
import (
"fmt"
"github.com/stretchr/testify/assert"
"math/rand"
"strconv"
"testing"
"time"
)
func TestParallelMap(t *testing.T) {
mappedData := ParallelMap(func(v string) int {
time.Sleep(time.Millisecond * time.Duration(rand.Int31n(10)))
i, _ := strconv.Atoi(v)
return i
},
3,
"9", "1", "2", "4", "5", "11")
assert.Equal(t, []int{9, 1, 2, 4, 5, 11}, mappedData)
}
func TestParallelMapSlice(t *testing.T) {
mappedData := ParallelMapSlice(func(v string) int {
time.Sleep(time.Millisecond * time.Duration(rand.Int31n(10)))
i, _ := strconv.Atoi(v)
return i
},
3,
[]string{"1", "3", "2", "7", "9", "12"})
assert.Equal(t, []int{1, 3, 2, 7, 9, 12}, mappedData)
}
func ExampleParallelMapSlice() {
data := []string{"1", "3", "2", "7", "9", "12"}
mappedData := ParallelMapSlice(
// function used to map
func(v string) int {
time.Sleep(time.Millisecond * time.Duration(rand.Int31n(10)))
i, _ := strconv.Atoi(v)
return i
},
3, // run it at least this many times
data)
// order stays
fmt.Printf("%T%+v\n%T%+v", data, data, mappedData, mappedData)
//Output: []string[1 3 2 7 9 12]
//[]int[1 3 2 7 9 12]
}
func TestParallelSliceMapChannel(t *testing.T) {
data := []string{"1", "3", "2", "7", "9", "12"}
ch := ParallelMapSliceChan(func(s string) int {
i, _ := strconv.Atoi(s)
return i
}, 3, data)
out := []int{}
for o := range ch {
out = append(out, o)
}
assert.True(t, CompareSliceSet([]int{1, 2, 3, 7, 9, 12}, out), out)
}
func TestParallelSliceMapChannelFinisher(t *testing.T) {
data := []string{"1", "3", "2", "7", "9", "12"}
ch, finish := ParallelMapSliceChanFinisher(func(s string) int {
i, _ := strconv.Atoi(s)
return i
}, 3, data)
out := []int{}
for o := range ch {
out = append(out, o)
}
assert.True(t, CompareSliceSet([]int{1, 2, 3, 7, 9, 12}, out), out)
assert.True(t, <-finish)
}
func TestParallelMapMap(t *testing.T) {
data := map[string]int{
"a": 3,
"b": 1,
"c": 8,
"d": 7,
}
mappedData := ParallelMapMap(func(k string, v int) (string, string) {
time.Sleep(time.Millisecond * time.Duration(v))
return k, strconv.Itoa(v)
}, 4, data)
assert.Equal(t, map[string]string{
"a": "3",
"b": "1",
"c": "8",
"d": "7",
}, mappedData)
}