This repository has been archived by the owner on Aug 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathitertools_test.go
225 lines (201 loc) · 4.42 KB
/
itertools_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
package itertools
import (
"fmt"
"math/rand"
"testing"
)
func ExampleIterInt() {
arr := []int{1, 2, 3, 4}
ch := Iter(arr)
for element := range ch {
fmt.Printf("%v:", element)
}
// Output: 1:2:3:4:
}
func TestIterLower(t *testing.T) {
expected, counter := 1, 0
arr := []int{1}
ch := Iter(arr)
for range ch {
counter++
}
if counter != expected {
t.Log("counter not correct number")
t.Fail()
}
}
func ExampleNext() {
arr := []int{1, 2, 3, 4}
ch := Iter(arr)
element := Next(ch)
fmt.Printf("%v", element)
element = Next(ch)
fmt.Printf("%v", element)
for element := range ch {
fmt.Printf("%v", element)
}
// Output: 1234
}
func ExampleRepeat() {
ch := Repeat("example_string", 5)
for element := range ch {
fmt.Printf("%v", element)
}
// Output: example_stringexample_stringexample_stringexample_stringexample_string
}
func ExampleZip() {
first := []int{1, 2, 3}
second := []int{4, 5, 6}
third := []int{7, 8, 9}
ch := Zip(first, second, third)
for element := range ch {
fmt.Printf("%v", element)
}
// Output: [1 4 7][2 5 8][3 6 9]
}
func ExampleZipFailure() {
first := []int{1, 2, 3}
second := []int{4, 5, 6}
third := []int{7, 8, 9, 11}
ch := Zip(first, second, third)
for element := range ch {
fmt.Printf("%v", element)
}
// Output: all parameters must be of the same length
}
func ExampleChain() {
first := []int{1, 2, 3}
second := []int{4, 5, 6}
third := []int{7, 8, 9}
ch := Chain(first, second, third)
for element := range ch {
fmt.Printf("%v", element)
}
// Output: 123456789
}
func ExampleCount() {
ch := Count(1, 2)
fmt.Printf("%v", Next(ch))
fmt.Printf("%v", Next(ch))
fmt.Printf("%v", Next(ch))
// Output: 135
}
func ExampleCountDec() {
ch := Count(1.5, 0.5)
for i := 0; i < 4; i++ {
fmt.Printf("%v:", Next(ch))
}
// Output:1.5:2:2.5:3:
}
func ExampleCycle() {
ch := Cycle("teststring")
for i := 0; i < 14; i++ {
fmt.Printf("%v", Next(ch))
}
// Output: teststringtest
}
func ExampleAccumulate() {
arr := []int{1, 2, 3, 4, 5}
ch := Accumulate(arr, "", 0)
for element := range ch {
fmt.Printf("%v", element)
}
// Output: 1361015
}
func ExampleAccumulateWithStart() {
arr := []int{1, 2, 3, 4, 5}
ch := Accumulate(arr, "", 100)
for element := range ch {
fmt.Printf("%v:", element)
}
// Output: 100:101:103:106:110:115:
}
func ExampleAccumulateMultiply() {
arr := []int{1, 2, 3, 4, 5}
ch := Accumulate(arr, "multiply", 0)
for element := range ch {
fmt.Printf("%v:", element)
}
// Output: 1:2:6:24:120:
}
func ExampleAccumulateMultiplyWithStart() {
arr := []int{1, 2, 3, 4, 5}
ch := Accumulate(arr, "multiply", 100)
for element := range ch {
fmt.Printf("%v:", element)
}
// Output: 100:101:102:106:124:220:
}
func ExampleTee() {
param := "ABCDEFGHIJKLMNOPQ"
ch := Tee(param, 2)
for element := range ch {
fmt.Printf("%v:", element)
}
// Output: AB:CD:EF:GH:IJ:KL:MN:OP:Q:
}
func ExampleTeeArray() {
param := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
ch := Tee(param, 2)
for element := range ch {
fmt.Printf("%v:", element)
}
// Output: [1 2]:[3 4]:[5 6]:[7 8]:[9]:
}
func ExamplePairwise() {
param := "ABCDEFGHIJKLP"
ch := Pairwise(param)
for element := range ch {
fmt.Printf("%v:", element)
}
// Output: AB:CD:EF:GH:IJ:KL:P:
}
func ExampleDropwhile() {
param := []int{1, 4, 6, 4, 1}
predicate := Predicate(func(i interface{}) bool {
return i.(int) < 5
})
ch := Dropwhile(predicate, param)
for element := range ch {
fmt.Printf("%v:", element)
}
// Output: 6:4:1:
}
func ExampleFilterfalse() {
param := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
isOdd := Predicate(func(i interface{}) bool {
result := i.(int) % 2
if result == 1 {
return true
}
return false
})
ch := Filterfalse(isOdd, param)
for element := range ch {
fmt.Printf("%v:", element)
}
// Output: 2:4:6:8:10:
}
func generateRandomString(stringLength int) (result string) {
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
b := make([]rune, stringLength)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
result = string(b)
return
}
func TestCompressString(t *testing.T) {
expected, counter := 3, 0
data := []string{"A", "B", "C", "D", "E", "F"}
selector := []bool{true, false, true, false, false, true}
ch := Compress(data, selector)
for value := range ch {
fmt.Printf("%v:", value)
counter++
}
if counter != expected {
t.Error("counter not expected number, got: ", counter, "expected :", expected)
}
// Output: A:C:F:
}