go-pattern-examples/gomore/04_fan_in/fan_in_test.go

35 lines
633 B
Go
Raw Normal View History

2020-05-06 12:12:28 +03:00
package fanin
import (
"fmt"
"testing"
)
2020-05-07 11:52:03 +03:00
func TestFanInNumbersSeq(t *testing.T) {
2020-05-07 06:55:36 +03:00
//第一路输入源
dataStreams1 := []int{13, 44, 56, 99, 9, 45, 67, 90, 78, 23}
2020-05-06 12:12:28 +03:00
// generate the common channel with inputs
2020-05-07 06:55:36 +03:00
inputChan1 := generateNumbersPipeline(dataStreams1)
//第二路输入源
dataStreams2 := []int{2, 4, 6, 9, 1, 1, 2, 3, 7, 8}
inputChan2 := generateNumbersPipeline(dataStreams2)
2020-05-06 12:12:28 +03:00
2020-05-07 06:55:36 +03:00
c1 := squareNumber(inputChan1)
c2 := squareNumber(inputChan2)
//fanIn data for the squared numbers
out := Merge(c1, c2)
2020-05-06 12:12:28 +03:00
sum := 0
2020-05-07 06:55:36 +03:00
for c := range out {
sum += c
2020-05-06 12:12:28 +03:00
}
2020-05-07 06:55:36 +03:00
fmt.Printf("Total Sum of Squares by FanIn : %d\n", sum)
2020-05-06 12:12:28 +03:00
}