-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib_test.go
101 lines (88 loc) · 1.71 KB
/
lib_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
package libprisma_test
import (
"github.com/xadaemon/libprisma"
"testing"
)
func TestMap(t *testing.T) {
cases := []struct {
name string
in []int
out []libprisma.Result[int]
}{
{
name: "success",
in: []int{1, 2, 3},
out: []libprisma.Result[int]{libprisma.Ok(2), libprisma.Ok(4), libprisma.Ok(6)},
},
{
name: "empty",
in: []int{},
out: []libprisma.Result[int]{},
},
}
for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
got := libprisma.Map(tt.in, func(i int) libprisma.Result[int] {
return libprisma.Ok(i * 2)
})
if len(got) != len(tt.out) {
t.Errorf("got %v, want %v", got, tt.out)
}
for i := range got {
if got[i].ValueOrPanic() != tt.out[i].ValueOrPanic() {
t.Errorf("got %v, want %v", got, tt.out)
}
}
})
}
}
func TestSwitch(t *testing.T) {
nums := make([]int, 1000)
for i := 0; i < 1000; i++ {
nums[i] = i
}
even, odd := libprisma.Switch(nums, func(i int) bool {
return i%2 == 0
})
for _, n := range even {
if n%2 != 0 {
t.Errorf("got %v, want even", n)
}
}
for _, n := range odd {
if n%2 == 0 {
t.Errorf("got %v, want odd", n)
}
}
}
func TestStreamingSwitch(t *testing.T) {
nums := make([]int, 1000)
for i := 0; i < 1000; i++ {
nums[i] = i
}
stream := make(chan int)
even := make(chan int)
odd := make(chan int)
go libprisma.Stream(stream, nums)
go libprisma.StreamingSwitch(stream, even, odd, func(i int) bool {
return i%2 == 0
})
for {
select {
case n := <-even:
if n%2 != 0 {
t.Errorf("got %v, want even", n)
t.Fail()
}
case n := <-odd:
if n%2 == 0 {
t.Errorf("got %v, want odd", n)
t.Fail()
}
case _, ok := <-stream:
if !ok {
return
}
}
}
}