-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_test.go
241 lines (207 loc) · 7.25 KB
/
list_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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package steams
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestListOf(t *testing.T) {
list := Of(1, 2, 3, 4, 5)
assert.Equal(t, List[int]{1, 2, 3, 4, 5}, list)
}
func TestFilter(t *testing.T) {
list := Of(1, 2, 3, 4, 5)
filtered := list.Filter(func(i int) bool { return i%2 == 0 })
assert.Equal(t, List[int]{2, 4}, filtered)
}
func TestMapToAny(t *testing.T) {
list := Of(1, 2, 3, 4, 5)
mapped := list.MapToAny(func(i int) any { return struct{ v int }{i * 2} })
assert.Equal(t, List[any]{struct{ v int }{2}, struct{ v int }{4}, struct{ v int }{6}, struct{ v int }{8}, struct{ v int }{10}}, mapped)
}
func TestMapToString(t *testing.T) {
list := Of(1, 2, 3, 4, 5)
mapped := list.MapToString(func(i int) string { return fmt.Sprintf("value: %d", i) })
assert.Equal(t, List[string]{"value: 1", "value: 2", "value: 3", "value: 4", "value: 5"}, mapped)
}
func TestMapToInt(t *testing.T) {
list := Of(1, 2, 3, 4, 5)
mapped := list.MapToInt(func(i int) int { return i * 2 })
assert.Equal(t, List[int]{2, 4, 6, 8, 10}, mapped)
}
func TestFilterMapToAny(t *testing.T) {
list := Of(1, 2, 3, 4, 5)
filtered := list.FilterMapToAny(func(i int) bool { return i%2 == 0 }, func(i int) any { return i * 2 })
assert.Equal(t, List[any]{4, 8}, filtered)
}
func TestFilterMapToInt(t *testing.T) {
list := Of(1, 2, 3, 4, 5)
filtered := list.FilterMapToInt(func(i int) bool { return i%2 == 0 }, func(i int) int { return i * 2 })
assert.Equal(t, List[int]{4, 8}, filtered)
}
func TestFilterMapToString(t *testing.T) {
list := Of(1, 2, 3, 4, 5)
filtered := list.FilterMapToString(func(i int) bool { return i%2 == 0 }, func(i int) string { return fmt.Sprintf("Res: %d", i*2) })
assert.Equal(t, List[string]{"Res: 4", "Res: 8"}, filtered)
}
func TestFlatMapToAny(t *testing.T) {
list := List[List[int]]{{1, 2}, {2, 4}, {3, 6}}
flattened := list.FlatMapToAny(func(s List[int]) Steam[any] {
results := make(List[any], s.Count())
for i, v := range s.Collect() {
results[i] = fmt.Sprintf("v%v", v)
}
return results
})
assert.Equal(t, List[any]{"v1", "v2", "v2", "v4", "v3", "v6"}, flattened)
}
func TestFlatMapToInt(t *testing.T) {
list := List[List[int]]{{1, 2}, {2, 4}, {3, 6}}
flattened := list.FlatMapToInt(func(s List[int]) Steam[int] {
results := make(List[int], s.Count())
for i, v := range s.Collect() {
results[i] = v
}
return results
})
assert.Equal(t, List[int]{1, 2, 2, 4, 3, 6}, flattened)
}
func TestFlatMapToString(t *testing.T) {
list := List[List[int]]{{1, 2}, {2, 4}, {3, 6}}
flattened := list.FlatMapToString(func(s List[int]) Steam[string] {
results := make(List[string], s.Count())
for i, v := range s.Collect() {
results[i] = fmt.Sprintf("v%v", v)
}
return results
})
assert.Equal(t, List[string]{"v1", "v2", "v2", "v4", "v3", "v6"}, flattened)
}
func TestLimit(t *testing.T) {
limited := Of(1, 2, 3, 4, 5).Limit(3)
assert.Equal(t, List[int]{1, 2, 3}, limited)
}
func TestCount(t *testing.T) {
count := Of(1, 2, 3, 4, 5).Count()
assert.Equal(t, 5, count)
}
func TestForEach(t *testing.T) {
list := List[int]{1, 2, 3, 4, 5}
var sum int
list.ForEach(func(x int) {
sum += x
})
assert.Equal(t, 15, sum, "Expected sum to be 15")
}
func TestPeek(t *testing.T) {
list := List[int]{1, 2, 3, 4, 5}
var sum int
peekedList := list.Peek(func(x int) {
sum += x
})
assert.Equal(t, 15, sum, "Expected sum to be 15")
assert.Equal(t, 5, peekedList.Count(), "Expected peekedList to have 5 elements")
}
func TestAllMatch(t *testing.T) {
list := List[int]{2, 4, 6, 8, 10}
assert.True(t, list.AllMatch(func(x int) bool {
return x%2 == 0
}), "Expected all elements to match the predicate")
}
func TestAnyMatch(t *testing.T) {
list := List[int]{1, 2, 3, 4, 5}
assert.True(t, list.AnyMatch(func(x int) bool {
return x%2 == 0
}), "Expected at least one element to match the predicate")
}
func TestNoneMatch(t *testing.T) {
list := List[int]{1, 3, 5, 7, 9}
assert.True(t, list.NoneMatch(func(x int) bool {
return x%2 == 0
}), "Expected no elements to match the predicate")
}
func TestFindFirst(t *testing.T) {
list := List[int]{1, 2, 3, 4, 5}
first := list.FindFirst()
assert.True(t, first.IsPresent(), "Expected to find the first element")
assert.Equal(t, 1, first.Get(), "Expected the first element to be 1")
}
func TestTakeWhile(t *testing.T) {
list := List[int]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
result := list.TakeWhile(func(x int) bool {
return x < 6
})
assert.Equal(t, 5, result.Count(), "Expected result to have 5 elements")
for i, v := range result.Collect() {
assert.Equal(t, i+1, v, "Expected element %d to be %d", i, i+1)
}
}
func TestDropWhile(t *testing.T) {
list := List[int]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
result := list.DropWhile(func(x int) bool {
return x < 6
})
assert.Equal(t, 5, result.Count(), "Expected result to have 5 elements")
for i, v := range result.Collect() {
assert.Equal(t, i+6, v, "Expected element %d to be %d", i, i+6)
}
}
func TestReduce(t *testing.T) {
list := List[int]{1, 2, 3, 4, 5}
result := list.Reduce(0, func(acc, x int) int {
return acc + x
})
assert.Equal(t, 15, result, "Expected result to be 15")
}
func TestReverse(t *testing.T) {
list := List[int]{1, 2, 3, 4, 5}
reversed := list.Reverse()
assert.Equal(t, List[int]{5, 4, 3, 2, 1}, reversed, "Expected reversed list to be [5, 4, 3, 2, 1]")
}
func TestPosition(t *testing.T) {
list := List[int]{1, 2, 3, 4, 5}
index := list.Position(func(x int) bool {
return x == 3
})
assert.True(t, index.IsPresent(), "Expected to find the element")
assert.Equal(t, 2, index.Get(), "Expected the index to be 2")
index = list.Position(FindPosition(6))
assert.False(t, index.IsPresent(), "Expected not to find the element")
assert.Equal(t, -1, index.OrElse(-1), "Expected the index to be nil")
}
func TestLast(t *testing.T) {
list := List[int]{1, 2, 3, 4, 5}
last := list.Last()
assert.True(t, last.IsPresent(), "Expected to find the last element")
assert.Equal(t, 5, last.Get(), "Expected the last element to be 5")
emptyList := List[int]{}
last = emptyList.Last()
assert.True(t, last.IsEmpty(), "Expected not to find the last element")
assert.Equal(t, 0, last.OrElse(0), "Expected the last element to be nil")
}
func TestSkip(t *testing.T) {
list := List[int]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
skipped := list.Skip(3)
assert.Equal(t, List[int]{4, 5, 6, 7, 8, 9, 10}, skipped, "Expected the skipped list to be [4, 5, 6, 7, 8, 9, 10]")
emptyList := List[int]{}
skipped = emptyList.Skip(3)
assert.Equal(t, emptyList, skipped, "Expected the skipped list to be empty")
}
func TestSorted(t *testing.T) {
list := List[int]{5, 2, 8, 1, 9}
sorted := list.Sorted(OrderDesc)
assert.Equal(t, List[int]{1, 2, 5, 8, 9}, sorted, "Expected the sorted list to be [1, 2, 5, 8, 9]")
}
func TestGetCompared(t *testing.T) {
list := List[int]{5, 2, 8, 1, 9}
max := list.GetCompared(func(a, b int) bool {
return a > b
})
assert.True(t, max.IsPresent(), "Expected to find the maximum element")
assert.Equal(t, 9, max.Get(), "Expected the maximum element to be 9")
min := list.GetCompared(Max)
assert.False(t, min.IsEmpty(), "Expected to find the minimum element")
assert.Equal(t, 1, min.Get(), "Expected the minimum element to be 1")
emptyList := List[int]{}
min = emptyList.GetCompared(Min)
assert.False(t, min.IsPresent(), "Expected not to find any element")
}