-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconcurrent_map_test.go
66 lines (54 loc) · 1.35 KB
/
concurrent_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
package cstream_test
import (
"context"
"fmt"
"testing"
cstream "github.com/planxnx/concurrent-stream"
)
func TestMap(t *testing.T) {
ctx := context.Background()
goroutines := []int{-8, -4, -2, -1, 0, 1, 2, 4, 8}
n := []int{1, 10, 100, 1000, 10000}
basicStream := func(t *testing.T, n []int, goroutine int) {
mapper := cstream.NewMap(ctx, goroutine, n, func(item int, _ int) int {
return factorial(item)
})
result, err := mapper.Result(ctx)
if err != nil {
t.Error("unexpected error:", err)
t.FailNow()
}
if mapper.IsRunning() {
t.Error("expected not running")
}
if !mapper.IsDone() {
t.Error("expected done")
}
if len(result) != len(n) {
t.Errorf("expected %d items, got %d", len(n), len(result))
}
for i, item := range result {
if expected := factorial(i); item != expected {
t.Errorf("expected %d, got %d", expected, item)
}
}
// Multiple calls to Close should not panic.
mapper.Close()
mapper.Close()
}
inputs := make([][]int, 0, len(n))
for _, n := range n {
input := make([]int, 0, n)
for i := 0; i < n; i++ {
input = append(input, i)
}
inputs = append(inputs, input)
}
for _, goroutine := range goroutines {
for _, input := range inputs {
t.Run(fmt.Sprintf("%dgouroutines,%dn", goroutine, len(input)), func(t *testing.T) {
basicStream(t, input, goroutine)
})
}
}
}