-
Notifications
You must be signed in to change notification settings - Fork 0
/
map_test.go
130 lines (121 loc) · 3.86 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
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
package functional
import (
"reflect"
"strconv"
"testing"
"time"
)
func TestMapSerial(t *testing.T) {
result1 := MapSerial([]int{1, 2, 3, 4}, func(_ int, _ int) string {
return "Hello"
})
if len(result1) != 4 {
t.Errorf("expected length of result1 to be 4, got %d", len(result1))
}
if result1[0] != "Hello" || result1[1] != "Hello" || result1[2] != "Hello" || result1[3] != "Hello" {
t.Errorf("expected result1 to be [Hello, Hello, Hello, Hello], got %v", result1)
}
result2 := MapSerial([]int64{1, 2, 3, 4}, func(x int64, _ int) string {
return strconv.FormatInt(x, 10)
})
if len(result2) != 4 {
t.Errorf("expected length of result2 to be 4, got %d", len(result2))
}
if result2[0] != "1" || result2[1] != "2" || result2[2] != "3" || result2[3] != "4" {
t.Errorf("expected result2 to be [1, 2, 3, 4], got %v", result2)
}
}
func TestMapParallel(t *testing.T) {
result1 := MapParallel([]int{1, 2, 3, 4}, func(_ int, _ int) string {
return "Hello"
})
if len(result1) != 4 {
t.Errorf("expected length of result1 to be 4, got %d", len(result1))
}
if !reflect.DeepEqual(result1, []string{"Hello", "Hello", "Hello", "Hello"}) {
t.Errorf("expected result1 to be [Hello, Hello, Hello, Hello], got %v", result1)
}
result2 := MapParallel([]int64{1, 2, 3, 4}, func(x int64, _ int) string {
return strconv.FormatInt(x, 10)
})
if len(result2) != 4 {
t.Errorf("expected length of result2 to be 4, got %d", len(result2))
}
if !reflect.DeepEqual(result2, []string{"1", "2", "3", "4"}) {
t.Errorf("expected result2 to be [1, 2, 3, 4], got %v", result2)
}
}
func TestMapParallelWithGoroutineUpperLimit(t *testing.T) {
result1 := MapParallelWithGoroutineUpperLimit([]int{1, 2, 3, 4}, func(_ int, _ int) string {
return "Hello"
}, 2)
if len(result1) != 4 {
t.Errorf("expected length of result1 to be 4, got %d", len(result1))
}
if !reflect.DeepEqual(result1, []string{"Hello", "Hello", "Hello", "Hello"}) {
t.Errorf("expected result1 to be [Hello, Hello, Hello, Hello], got %v", result1)
}
result2 := MapParallelWithGoroutineUpperLimit([]int64{1, 2, 3, 4}, func(x int64, _ int) string {
return strconv.FormatInt(x, 10)
}, 2)
if len(result2) != 4 {
t.Errorf("expected length of result2 to be 4, got %d", len(result2))
}
if !reflect.DeepEqual(result2, []string{"1", "2", "3", "4"}) {
t.Errorf("expected result2 to be [1, 2, 3, 4], got %v", result2)
}
}
func TestMapParallelWithGoroutineUpperLimitConcurrentSecurity(t *testing.T) {
mapSlice := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
mapFunc := func(n int, _ int) string {
return strconv.FormatInt(int64(n), 10)
}
mapResult := make([]string, len(mapSlice))
for i, item := range mapSlice {
mapResult[i] = mapFunc(item, i)
}
checkMapResult := func(t *testing.T, r []string) {
if len(r) != len(mapSlice) {
t.Errorf("expected length of r to be %d, got %d", len(mapSlice), len(r))
}
if !reflect.DeepEqual(r, mapResult) {
t.Errorf("expected r to be %v, got %v", mapResult, r)
}
}
t.Run("param goroutineNum", func(t *testing.T) {
for i := 0; i <= len(mapSlice); i++ {
r := MapParallelWithGoroutineUpperLimit(mapSlice, mapFunc, i)
checkMapResult(t, r)
}
})
t.Run("panic", func(t *testing.T) {
mapSlice[3] = 0
defer func() {
mapSlice[3] = 3
}()
mapResult[3] = ""
defer func() {
mapResult[3] = "3"
}()
for i := 0; i <= len(mapSlice); i++ {
r := MapParallelWithGoroutineUpperLimit(mapSlice, func(item int, idx int) string {
if idx == 3 {
panic("panic") // nolint
}
return mapFunc(item, idx)
}, i)
checkMapResult(t, r)
}
})
t.Run("latency", func(t *testing.T) {
for i := 1; i <= len(mapSlice); i++ {
now := time.Now()
r := MapParallelWithGoroutineUpperLimit(mapSlice, func(item int, idx int) string {
time.Sleep(time.Second)
return mapFunc(item, idx)
}, i)
checkMapResult(t, r)
t.Logf("goroutineNum: %d, latency: %s", i, time.Since(now))
}
})
}