-
Notifications
You must be signed in to change notification settings - Fork 2
/
map_chan_test.go
53 lines (45 loc) · 874 Bytes
/
map_chan_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
package pipe
import (
"fmt"
"testing"
)
func TestMapChan(t *testing.T) {
count := 0
counter := func(item int) string {
count++
return fmt.Sprint(count)
}
in := make(chan int, 5)
out := MapChan(counter, in).(chan string)
go func() {
in <- 7
in <- 4
in <- 5
}()
for i := 1; i <= 3; i++ {
if result := <-out; result != fmt.Sprint(i) {
t.Fatal("MapChan received ", i, " items but output ", result)
}
}
close(in)
}
func TestMapChanTypeCoercion(t *testing.T) {
count := 0
counter := func(item fmt.Stringer) string {
count++
return fmt.Sprint(count)
}
in := make(chan testStringer, 5)
out := MapChan(counter, in).(chan string)
go func() {
in <- 7
in <- 4
in <- 5
}()
for i := 1; i <= 3; i++ {
if result := <-out; result != fmt.Sprint(i) {
t.Fatal("MapChan received ", i, " items but output ", result)
}
}
close(in)
}