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
|
package channels
import (
"reflect"
"sort"
"testing"
)
func TestFanIn(t *testing.T) {
type args struct {
chans [][]int
}
tests := []struct {
name string
args args
want []int
}{
{
name: "test fanin",
args: args{
chans: [][]int{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
},
},
want: []int{1, 2, 3, 4, 5, 6, 7, 8, 9},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := []int{}
chans := make([]chan int, len(tt.args.chans))
for i, v := range tt.args.chans {
func(i int, v []int) {
chans[i] = make(chan int, len(v))
go func(i int, v []int) {
for _, v2 := range v {
chans[i] <- v2
}
close(chans[i])
}(i, v)
}(i, v)
}
ch := FanIn(chans...)
for o := range ch {
got = append(got, o)
}
sort.Ints(got)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("FanIn() = %v, want %v", got, tt.want)
}
})
}
}
|