forked from dreamsofcode-io/loop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparallel_test.go
93 lines (77 loc) · 1.4 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
package loop_test
import (
"fmt"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/dreamsofcode-io/loop"
)
var count uint64 = 1000
func TestParallelShouldNotPanic(t *testing.T) {
xs := []int{}
for range 10000 {
xs = append(xs, 0)
}
for i, _ := range loop.Parallel(xs) {
fmt.Println("hello")
if i > 300 {
break
}
}
}
// Parallel
func TestParallel(t *testing.T) {
testCases := []struct {
name string
input []int
wants int
}{
{
name: "",
input: []int{1, 2, 3, 4, 5},
wants: 15,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
sum := 0
for _, x := range loop.Parallel(tc.input) {
sum += x
}
assert.Equal(t, sum, tc.wants)
})
}
}
func TestParallelTimes(t *testing.T) {
sum := atomic.Int64{}
counter := atomic.Int64{}
for i := range loop.ParallelTimes(int64(5)) {
counter.Add(1)
sum.Add(i)
}
assert.Equal(t, counter.Load(), int64(5))
assert.Equal(t, sum.Load(), int64(10))
}
func BenchmarkParallel(b *testing.B) {
xs := make([]uint64, 0, count)
for i := range count {
xs = append(xs, i)
}
for range b.N {
for _, _ = range loop.Parallel(xs) {
time.Sleep(time.Microsecond)
}
}
}
func BenchmarkParallelExisting(b *testing.B) {
xs := make([]uint64, 0, count)
for i := range count {
xs = append(xs, i)
}
for range b.N {
for range xs {
time.Sleep(time.Microsecond)
}
}
}