-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparallel_test.go
100 lines (87 loc) · 1.99 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
96
97
98
99
100
package parallel_test
import (
"reflect"
"sort"
"testing"
"github.com/JigneshSatam/parallel"
)
type customUserType int
// execute -> Overridden `execute()` method to make customUserType as an executor
func (c customUserType) Execute() interface{} {
return c * c
}
var testCases = []struct {
name string
ip interface{}
op []int
}{
{
name: "Valid Tasks",
ip: []customUserType{1, 2, 3, 4, 5},
op: []int{1, 4, 9, 16, 25},
},
{
name: "Invalid Tasks - where Execute method not overridden by input type",
ip: []int{1, 5},
op: []int{},
},
{
name: "Valid Single Task",
ip: customUserType(5),
op: []int{25},
},
{
name: "Inalid Single Task - where Execute method not overridden by input type",
ip: 5,
op: []int{},
},
{
name: "Valid Empty Tasks",
ip: []customUserType{},
op: []int{},
},
{
name: "Invalid Empty Tasks - where Execute method not overridden by input type",
ip: []int{},
op: []int{},
},
}
func TestRun(t *testing.T) {
t.Parallel()
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
outputArray := []customUserType{}
for ele := range parallel.Run(tc.ip) {
outputArray = append(outputArray, ele.(customUserType))
}
intArr := []int{}
for _, ele := range outputArray {
intArr = append(intArr, int(ele))
}
sort.Ints(intArr)
if !reflect.DeepEqual(tc.op, intArr) {
t.Errorf("Run Failed for test %v \n want ===> %v \n got ===> %v", tc.name, tc.op, intArr)
}
})
}
}
func BenchmarkRun(b *testing.B) {
for i := 0; i < b.N; i++ {
arr := []customUserType{1, 2, 3, 4, 5}
op := []int{1, 4, 9, 16, 25}
output := []customUserType{}
for ele := range parallel.Run(arr) {
output = append(output, ele.(customUserType))
}
intArr := []int{}
for _, ele := range output {
intArr = append(intArr, int(ele))
}
sort.Ints(intArr)
if !reflect.DeepEqual(op, intArr) {
b.Errorf("Run Failed for test \n want ===> %v \n got ===> %v", op, intArr)
}
}
}