-
Notifications
You must be signed in to change notification settings - Fork 273
/
scan_test.go
120 lines (84 loc) · 2.07 KB
/
scan_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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package funk
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestForEach(t *testing.T) {
is := assert.New(t)
results := []int{}
ForEach([]int{1, 2, 3, 4}, func(x int) {
if x%2 == 0 {
results = append(results, x)
}
})
is.Equal(results, []int{2, 4})
toModify := []int{1, 2, 3}
ForEach(toModify, func(x *int) { *x = *x * 2 })
is.Equal(toModify, []int{2, 4, 6})
toModify = []int{}
ForEach(toModify, func(x *int) {})
is.Equal(toModify, []int{})
strModify := []string{"a", "b"}
ForEach(strModify, func(s *string) {
*s = *s + *s
})
is.Equal(strModify, []string{"aa", "bb"})
mapping := map[int]string{
1: "Florent",
2: "Gilles",
}
ForEach(mapping, func(k int, v string) {
is.Equal(v, mapping[k])
})
}
func TestForEachRight(t *testing.T) {
is := assert.New(t)
results := []int{}
ForEachRight([]int{1, 2, 3, 4}, func(x int) {
results = append(results, x*2)
})
is.Equal(results, []int{8, 6, 4, 2})
toModify := []int{1, 2, 3}
ForEach(toModify, func(x *int) { *x = *x * 2 })
is.Equal(toModify, []int{2, 4, 6})
toModify = []int{}
ForEach(toModify, func(x *int) {})
is.Equal(toModify, []int{})
strModify := []string{"a", "b"}
ForEach(strModify, func(s *string) {
*s = *s + *s
})
is.Equal(strModify, []string{"aa", "bb"})
mapping := map[int]string{
1: "Florent",
2: "Gilles",
}
mapKeys := []int{}
ForEachRight(mapping, func(k int, v string) {
is.Equal(v, mapping[k])
mapKeys = append(mapKeys, k)
})
is.Equal(len(mapKeys), 2)
is.Contains(mapKeys, 1)
is.Contains(mapKeys, 2)
}
func TestHead(t *testing.T) {
is := assert.New(t)
is.Equal(Head([]int{1, 2, 3, 4}), 1)
}
func TestLast(t *testing.T) {
is := assert.New(t)
is.Equal(Last([]int{1, 2, 3, 4}), 4)
}
func TestTail(t *testing.T) {
is := assert.New(t)
is.Equal(Tail([]int{}), []int{})
is.Equal(Tail([]int{1}), []int{1})
is.Equal(Tail([]int{1, 2, 3, 4}), []int{2, 3, 4})
}
func TestInitial(t *testing.T) {
is := assert.New(t)
is.Equal(Initial([]int{}), []int{})
is.Equal(Initial([]int{1}), []int{1})
is.Equal(Initial([]int{1, 2, 3, 4}), []int{1, 2, 3})
}