-
Notifications
You must be signed in to change notification settings - Fork 0
/
flow_test.go
145 lines (117 loc) · 2.94 KB
/
flow_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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package dataflow
import (
"fmt"
"testing"
)
// Emulated flow network:
//
// +----------- B:( -1 ) -------------+
// | |
// Input:(x) -+-> C:( +2 ) -> E:( * ) -> Final:( + ) -> end.
// | |
// +----> D:( +5 ) ----+
//
func testExecution() (*ExecutionGraph, error) {
stages := []Stage{
NewStage("b", func(args ...interface{}) (i interface{}, err error) {
if len(args) != 1 {
return nil, fmt.Errorf("unexpected arguments: %v", args)
}
a, ok := args[0].(int)
if !ok {
return nil, fmt.Errorf("bad argument: %v", args[0])
}
return a - 1, nil
}, Input),
NewStage("c", func(args ...interface{}) (i interface{}, err error) {
if len(args) != 1 {
return nil, fmt.Errorf("unexpected arguments: %v", args)
}
a, ok := args[0].(int)
if !ok {
return nil, fmt.Errorf("bad argument: %v", args[0])
}
return a + 2, nil
}, Input),
NewStage("d", func(args ...interface{}) (i interface{}, err error) {
if len(args) != 1 {
return nil, fmt.Errorf("unexpected arguments: %v", args)
}
a, ok := args[0].(int)
if !ok {
return nil, fmt.Errorf("bad argument: %v", args[0])
}
return a + 5, nil
}, Input),
NewStage("e", func(args ...interface{}) (i interface{}, err error) {
if len(args) != 2 {
return nil, fmt.Errorf("unexpected arguments: %v", args)
}
c, ok := args[0].(int)
if !ok {
return nil, fmt.Errorf("bad first argument: %v", args[0])
}
d, ok := args[1].(int)
if !ok {
return nil, fmt.Errorf("bad second argument: %v", args[1])
}
return c * d, nil
}, "c", "d"),
}
fin := NewFinalStage(func(args ...interface{}) (i interface{}, err error) {
if len(args) != 2 {
return nil, fmt.Errorf("unexpected arguments: %v", args)
}
b, ok := args[0].(int)
if !ok {
return nil, fmt.Errorf("bad first argument: %v", args[0])
}
e, ok := args[1].(int)
if !ok {
return nil, fmt.Errorf("bad second argument: %v", args[1])
}
return b + e, nil
}, "b", "e")
// construct execution network
return NewExecutionGraph(fin, stages...)
}
func TestComputation(t *testing.T) {
graph, err := testExecution()
if err != nil {
t.Errorf("constructing execution graph: %v", err)
}
exec, collapse := graph.Run()
tests := []struct {
name string
arg int
want int
}{
{"1", 1, 18},
{"2", 2, 29},
{"-4", -4, -7},
{"0", 0, 9},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := exec(tt.arg)
if err != nil {
t.Errorf("unexpected execution error: %v", err)
}
if tt.want != result {
t.Errorf("unexpected result: want %v, but get %v, ", tt.want, result)
}
})
}
collapse()
}
func BenchmarkExecutionGraph(b *testing.B) {
graph, err := testExecution()
if err != nil {
b.Errorf("constructing execution graph: %v", err)
}
exec, _ := graph.Run()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = exec(i)
}
}