-
Notifications
You must be signed in to change notification settings - Fork 1
/
map_test.go
168 lines (156 loc) · 3.83 KB
/
map_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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package goneric
import (
"fmt"
"github.com/stretchr/testify/assert"
"sort"
"strconv"
"testing"
)
func TestMap(t *testing.T) {
mappedData := Map(func(v string) int {
i, _ := strconv.Atoi(v)
return i
}, "1", "3", "2")
assert.Equal(t, []int{1, 3, 2}, mappedData)
}
func TestMapErr(t *testing.T) {
mappedData, err := MapErr(func(v string) (int, error) {
i, err := strconv.Atoi(v)
return i, err
}, "1", "2", "z", "4")
assert.Error(t, err)
assert.Equal(t, []int{1, 2}, mappedData)
mappedData2, err := MapErr(func(v string) (int, error) {
i, err := strconv.Atoi(v)
return i, err
}, "1", "2", "3", "4")
assert.NoError(t, err)
assert.Equal(t, []int{1, 2, 3, 4}, mappedData2)
}
func TestMapSlice(t *testing.T) {
mappedData := MapSlice(func(v string) int {
i, _ := strconv.Atoi(v)
return i
}, []string{"1", "3", "2"})
assert.Equal(t, []int{1, 3, 2}, mappedData)
}
func ExampleMapSlice() {
out := MapSlice(
func(i int) string { return fmt.Sprintf("-=0x%02x=-", i) },
MapSlice(
func(i int) int { return i + 1 },
MapSlice(
func(i int) int { return i * i },
GenSlice(10, func(idx int) int { return idx }),
),
),
)
fmt.Printf("%+v", out)
// output: [-=0x01=- -=0x02=- -=0x05=- -=0x0a=- -=0x11=- -=0x1a=- -=0x25=- -=0x32=- -=0x41=- -=0x52=-]
}
func TestMapSliceErr(t *testing.T) {
mappedData, err := MapSliceErr(func(v string) (int, error) {
i, err := strconv.Atoi(v)
return i, err
}, []string{"1", "2", "z", "4"})
assert.Error(t, err)
assert.Equal(t, []int{1, 2}, mappedData)
mappedData2, err := MapSliceErr(func(v string) (int, error) {
i, err := strconv.Atoi(v)
return i, err
}, []string{"1", "2", "3", "4"})
assert.NoError(t, err)
assert.Equal(t, []int{1, 2, 3, 4}, mappedData2)
}
func TestMapSliceSkip(t *testing.T) {
mappedData := MapSliceSkip(func(v string) (int, bool) {
i, err := strconv.Atoi(v)
return i, err != nil
}, []string{"1", "2", "z", "4"})
assert.Equal(t, []int{1, 2, 4}, mappedData)
mappedData2 := MapSliceSkip(func(v string) (int, bool) {
i, err := strconv.Atoi(v)
return i, err != nil
}, []string{"1", "2", "3", "4"})
assert.Equal(t, []int{1, 2, 3, 4}, mappedData2)
}
func TestMapSliceErrSkip(t *testing.T) {
mappedData, err := MapSliceErrSkip(func(v string) (int, error) {
i, err := strconv.Atoi(v)
if v == "j" {
return i, err
}
if err != nil {
return i, ErrSkip{}
}
return i, nil
}, []string{"1", "2", "z", "4", "j", "6"})
assert.Error(t, err)
assert.Equal(t, []int{1, 2, 4}, mappedData)
mappedData2, err := MapSliceErrSkip(func(v string) (int, error) {
i, err := strconv.Atoi(v)
return i, err
}, []string{"1", "2", "3", "4"})
assert.NoError(t, err)
assert.Equal(t, []int{1, 2, 3, 4}, mappedData2)
}
func TestMapSliceKey(t *testing.T) {
data := map[string]int{
"a": 1,
"b": 2,
"c": 3,
}
dataSlice := MapSliceKey(data)
sort.Strings(dataSlice)
assert.EqualValues(t, []string{"a", "b", "c"}, dataSlice)
}
func TestMapSliceValue(t *testing.T) {
data := map[string]int{
"a": 1,
"b": 2,
"c": 3,
}
dataSlice := MapSliceValue(data)
sort.Ints(dataSlice)
assert.EqualValues(t, []int{1, 2, 3}, dataSlice)
}
func TestMapToSlice(t *testing.T) {
assert.True(t,
CompareSliceSet(
[]string{"a-1", "b-2"},
MapToSlice(
func(k string, v int) string {
return fmt.Sprintf("%s-%d", k, v)
}, map[string]int{
"a": 1,
"b": 2,
},
)))
}
func TestMapMap(t *testing.T) {
data := map[string]int{
"a": 1,
"b": 2,
"c": 3,
}
out := MapMap(func(a string, b int) (int, string) { return b, a }, data)
assert.Equal(t, map[int]string{
1: "a",
2: "b",
3: "c",
}, out)
}
func TestMapMapInplace(t *testing.T) {
data := map[string]int{
"a": 1,
"b": 2,
"c": 3,
}
out := map[int]string{}
MapMapInplace(func(a string, b int) (int, string) { return b, a }, data, out)
assert.Equal(t, map[int]string{
1: "a",
2: "b",
3: "c",
}, out)
}