-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathslice_iter_test.go
191 lines (161 loc) · 6.2 KB
/
slice_iter_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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package gofn
import (
"testing"
"github.com/stretchr/testify/assert"
)
func Test_ForEach(t *testing.T) {
copy1 := []int{}
ForEach[int]([]int(nil), func(i int, t int) { copy1 = append(copy1, t) })
assert.Equal(t, []int{}, copy1)
copy2 := []int64{}
ForEach([]int64{1, 2, 3}, func(i int, t int64) { copy2 = append(copy2, t) })
assert.Equal(t, []int64{1, 2, 3}, copy2)
}
func Test_ForEachPtr(t *testing.T) {
copy1 := []int{}
ForEachPtr[int]([]int(nil), func(i int, t *int) { copy1 = append(copy1, *t) })
assert.Equal(t, []int{}, copy1)
copy2 := []int64{}
ForEachPtr([]int64{1, 2, 3}, func(i int, t *int64) { copy2 = append(copy2, *t) })
assert.Equal(t, []int64{1, 2, 3}, copy2)
}
func Test_ForEachReverse(t *testing.T) {
copy1 := []int{}
ForEachReverse[int]([]int(nil), func(i int, t int) { copy1 = append(copy1, t) })
assert.Equal(t, []int{}, copy1)
copy2 := []int64{}
ForEachReverse([]int64{1, 2, 3}, func(i int, t int64) { copy2 = append(copy2, t) })
assert.Equal(t, []int64{3, 2, 1}, copy2)
}
func Test_ForEachPtrReverse(t *testing.T) {
copy1 := []int{}
ForEachPtrReverse[int]([]int(nil), func(i int, t *int) { copy1 = append(copy1, *t) })
assert.Equal(t, []int{}, copy1)
copy2 := []int64{}
ForEachPtrReverse([]int64{1, 2, 3}, func(i int, t *int64) { copy2 = append(copy2, *t) })
assert.Equal(t, []int64{3, 2, 1}, copy2)
}
func Test_Iter(t *testing.T) {
// No slice input
copy1 := []int{}
Iter[int, []int](func(i int, t int) bool { copy1 = append(copy1, t); return true })
assert.Equal(t, []int{}, copy1)
// Empty slice input
copy2 := []int{}
Iter(func(i int, t int) bool { copy2 = append(copy2, t); return true }, []int{})
assert.Equal(t, []int{}, copy2)
// Single slice
copy3 := []int{}
index3 := []int{}
Iter(func(i int, t int) bool { copy3 = append(copy3, t); index3 = append(index3, i); return true },
[]int{1, 2, 3})
assert.Equal(t, []int{1, 2, 3}, copy3)
assert.Equal(t, []int{0, 1, 2}, index3)
// Multiple slices
copy4 := []int32{}
index4 := []int{}
Iter(func(i int, t int32) bool { copy4 = append(copy4, t); index4 = append(index4, i); return true },
[]int32{1, 2, 3}, []int32{}, []int32{4, 5})
assert.Equal(t, []int32{1, 2, 3, 4, 5}, copy4)
assert.Equal(t, []int{0, 1, 2, 3, 4}, index4)
// With stop
copy5 := []string{}
index5 := []int{}
Iter(func(i int, t string) bool { copy5 = append(copy5, t); index5 = append(index5, i); return i < 3 },
[]string{"1", "2", "3"}, []string{}, []string{"4", "5"})
assert.Equal(t, []string{"1", "2", "3", "4"}, copy5)
assert.Equal(t, []int{0, 1, 2, 3}, index5)
}
func Test_IterPtr(t *testing.T) {
// No slice input
copy1 := []int{}
IterPtr[int, []int](func(i int, t *int) bool { copy1 = append(copy1, *t); return true })
assert.Equal(t, []int{}, copy1)
// Empty slice input
copy2 := []int{}
IterPtr(func(i int, t *int) bool { copy2 = append(copy2, *t); return true }, []int{})
assert.Equal(t, []int{}, copy2)
// Single slice
copy3 := []int{}
index3 := []int{}
IterPtr(func(i int, t *int) bool { copy3 = append(copy3, *t); index3 = append(index3, i); return true },
[]int{1, 2, 3})
assert.Equal(t, []int{1, 2, 3}, copy3)
assert.Equal(t, []int{0, 1, 2}, index3)
// Multiple slices
copy4 := []int32{}
index4 := []int{}
IterPtr(func(i int, t *int32) bool { copy4 = append(copy4, *t); index4 = append(index4, i); return true },
[]int32{1, 2, 3}, []int32{}, []int32{4, 5})
assert.Equal(t, []int32{1, 2, 3, 4, 5}, copy4)
assert.Equal(t, []int{0, 1, 2, 3, 4}, index4)
// With stop
copy5 := []string{}
index5 := []int{}
IterPtr(func(i int, t *string) bool { copy5 = append(copy5, *t); index5 = append(index5, i); return i < 3 },
[]string{"1", "2", "3"}, []string{}, []string{"4", "5"})
assert.Equal(t, []string{"1", "2", "3", "4"}, copy5)
assert.Equal(t, []int{0, 1, 2, 3}, index5)
}
func Test_IterReverse(t *testing.T) {
// No slice input
copy1 := []int{}
IterReverse[int, []int](func(i int, t int) bool { copy1 = append(copy1, t); return true })
assert.Equal(t, []int{}, copy1)
// Empty slice input
copy2 := []int{}
IterReverse(func(i int, t int) bool { copy2 = append(copy2, t); return true }, []int{})
assert.Equal(t, []int{}, copy2)
// Single slice
copy3 := []int{}
index3 := []int{}
IterReverse(func(i int, t int) bool { copy3 = append(copy3, t); index3 = append(index3, i); return true },
[]int{1, 2, 3})
assert.Equal(t, []int{3, 2, 1}, copy3)
assert.Equal(t, []int{2, 1, 0}, index3)
// Multiple slices
copy4 := []int32{}
index4 := []int{}
IterReverse(func(i int, t int32) bool { copy4 = append(copy4, t); index4 = append(index4, i); return true },
[]int32{1, 2, 3}, []int32{}, []int32{4, 5})
assert.Equal(t, []int32{5, 4, 3, 2, 1}, copy4)
assert.Equal(t, []int{4, 3, 2, 1, 0}, index4)
// With stop
copy5 := []string{}
index5 := []int{}
IterReverse(func(i int, t string) bool { copy5 = append(copy5, t); index5 = append(index5, i); return i > 3 },
[]string{"1", "2", "3"}, []string{}, []string{"4", "5"})
assert.Equal(t, []string{"5", "4"}, copy5)
assert.Equal(t, []int{4, 3}, index5)
}
func Test_IterPtrReverse(t *testing.T) {
// No slice input
copy1 := []int{}
IterPtrReverse[int, []int](func(i int, t *int) bool { copy1 = append(copy1, *t); return true })
assert.Equal(t, []int{}, copy1)
// Empty slice input
copy2 := []int{}
IterPtrReverse(func(i int, t *int) bool { copy2 = append(copy2, *t); return true }, []int{})
assert.Equal(t, []int{}, copy2)
// Single slice
copy3 := []int{}
index3 := []int{}
IterPtrReverse(func(i int, t *int) bool { copy3 = append(copy3, *t); index3 = append(index3, i); return true },
[]int{1, 2, 3})
assert.Equal(t, []int{3, 2, 1}, copy3)
assert.Equal(t, []int{2, 1, 0}, index3)
// Multiple slices
copy4 := []int32{}
index4 := []int{}
IterPtrReverse(func(i int, t *int32) bool { copy4 = append(copy4, *t); index4 = append(index4, i); return true },
[]int32{1, 2, 3}, []int32{}, []int32{4, 5})
assert.Equal(t, []int32{5, 4, 3, 2, 1}, copy4)
assert.Equal(t, []int{4, 3, 2, 1, 0}, index4)
// With stop
copy5 := []string{}
index5 := []int{}
IterPtrReverse(func(i int, t *string) bool { copy5 = append(copy5, *t); index5 = append(index5, i); return i > 3 },
[]string{"1", "2", "3"}, []string{}, []string{"4", "5"})
assert.Equal(t, []string{"5", "4"}, copy5)
assert.Equal(t, []int{4, 3}, index5)
}