-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiterator_test.go
114 lines (94 loc) · 2.8 KB
/
iterator_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
package native
import (
"testing"
)
func TestMapContainer_FilterTake(t *testing.T) {
data := map[string]bool{
"hello": false,
"привет": true,
"morning": false,
"утро": true,
"winter": false,
"зима": true,
"movie": false,
"фильм": true,
"TV": false,
"ТВ": true,
"snowdrift": false,
"сугроб": true,
"courtyard": false,
"двор": true,
}
expectedLen := 7
existLen := MapIterator(data).
Filter(func(word string, isRussian bool) bool { return isRussian }).
Len()
if existLen != expectedLen {
t.Error("failed to filter russian word. Got ", existLen, " words, but expected is ", expectedLen, " words")
}
expectedLen = 3
existLen = MapIterator(data).
Filter(func(word string, isRussian bool) bool { return isRussian }).
Take(expectedLen).
Len()
if existLen != expectedLen {
t.Error("failed to filter russian word and take just ", expectedLen, " words. Got ", existLen, " words, but expected is ", expectedLen, " words")
}
}
func TestUintContainer_FilterTakeMul(t *testing.T) {
data := []uint{0, 3, 5, 7, 18, 30, 36, 46, 48, 52, 60}
expectedLen := 7
existLen := UintIterator(data).
Filter(func(index int, number uint) bool { return number%3 == 0 }).
Len()
if existLen != expectedLen {
t.Error("failed to filter number divided by 3. Got ", existLen, " words, but expected is ", expectedLen, " words")
}
expectedLen = 3
existLen = UintIterator(data).
Filter(func(index int, number uint) bool { return number%3 == 0 }).
Take(expectedLen).
Len()
if existLen != expectedLen {
t.Error("failed to filter number divided by 3 and take just ", expectedLen, " words. Got ", existLen, " words, but expected is ", expectedLen, " words")
}
var expectedMul uint = 167961600
existMul := UintIterator(data).
Filter(func(index int, number uint) bool { return number > 0 && number%3 == 0 }).
Mul()
if existMul != expectedMul {
t.Error("failed to filter number divided by 3 and multiply it. Got ", existMul, ", but expected is ", expectedMul)
}
}
func BenchmarkUintContainer_Len(b *testing.B) {
dataLen := 1000
data := make([]uint, 0, dataLen)
for i := 0; i < cap(data); i++ {
data = append(data, uint(i)*3+1)
}
b.Run("native iterator", func(b *testing.B) {
count := 0
for i := 0; i < 100; i++ {
for _, number := range data {
if number > 0 && number%2 == 0 {
count++
}
}
}
if count < 10 {
b.Error("failed to native calc length", count)
}
})
iterator := UintIterator(data)
b.Run("native iterator", func(b *testing.B) {
count := 0
for i := 0; i < 100; i++ {
count += iterator.
Filter(func(index int, number uint) bool { return number > 0 && number%2 == 0 }).
Len()
}
if count < 10 {
b.Error("failed to iterative calc length", count)
}
})
}