diff options
author | Alexander Kiryukhin <a.kiryukhin@mail.ru> | 2022-05-01 21:50:12 +0300 |
---|---|---|
committer | Alexander Kiryukhin <a.kiryukhin@mail.ru> | 2022-05-01 21:50:12 +0300 |
commit | 9fcf8e29214210612d545bed50d7f889800ac639 (patch) | |
tree | 3a99d2cd37fb8158d49abc1de6298758d205c9dd /channels/fanin_test.go |
Initial
Diffstat (limited to 'channels/fanin_test.go')
-rw-r--r-- | channels/fanin_test.go | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/channels/fanin_test.go b/channels/fanin_test.go new file mode 100644 index 0000000..dd0c889 --- /dev/null +++ b/channels/fanin_test.go @@ -0,0 +1,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) + } + }) + } +} |