-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrangers_test.go
More file actions
196 lines (149 loc) · 4.72 KB
/
rangers_test.go
File metadata and controls
196 lines (149 loc) · 4.72 KB
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
package arrayr
import (
"fmt"
"strings"
"testing"
"time"
"github.com/eenti-utils/typr"
)
func TestRange_1(t *testing.T) {
var testResult int
assrtEqual(t, 0, testResult)
sum := func(_ int, e int) (r typr.Op) {
testResult += e
return
}
Range(sum, From(1, 2, 3))
assrtEqual(t, 6, testResult)
}
func TestRange_2(t *testing.T) {
var testResult []string
assrtEqual(t, 0, len(testResult))
addToList := func(_ int, e string) (r typr.Op) {
testResult = append(testResult, e)
return
}
Range(addToList, From("apple", "banana", "coconut"))
assrtEqual(t, 3, len(testResult))
assrtEqual(t, []string{"apple", "banana", "coconut"}, testResult)
}
func TestRange_3(t *testing.T) {
var testResult []string
assrtEqual(t, 0, len(testResult))
addToList := func(_ int, e string) (r typr.Op) {
testResult = append(testResult, e)
return
}
Range(addToList, From("apple", "banana", "coconut"), RangeOpts[string]{Step: -1}) // negative step means range in reverse order
assrtEqual(t, 3, len(testResult))
assrtEqual(t, []string{"coconut", "banana", "apple"}, testResult)
}
func TestRange_4(t *testing.T) {
var testResult []string
assrtEqual(t, 0, len(testResult))
addToList := func(_ int, e string) (r typr.Op) {
testResult = append(testResult, e)
return
}
testFilter := func(s string) bool {
return strings.Contains(s, "a")
}
testValidator := func(s string) (e error) {
if strings.HasPrefix(s, "c") {
return
}
e = fmt.Errorf("invalid element [ %s ]", s)
return
}
Range(addToList, From("apple", "banana", "coconut"), RangeOpts[string]{Step: -1, FilterElements: testFilter, ValidateElements: testValidator}) // nothing should get through...
assrtEqual(t, 0, len(testResult))
}
func TestRange_5(t *testing.T) {
var testResult []string
assrtEqual(t, 0, len(testResult))
addToList := func(_ int, e string) (r typr.Op) {
testResult = append(testResult, e)
return
}
testFilter := func(s string) bool {
return strings.Contains(s, "a")
}
Range(addToList, From("apple", "banana", "coconut"), RangeOpts[string]{Step: -1, FilterElements: testFilter})
assrtEqual(t, 2, len(testResult))
assrtEqual(t, []string{"banana", "apple"}, testResult)
}
func TestRange_6(t *testing.T) {
var testResult []string
assrtEqual(t, 0, len(testResult))
addToList := func(_ int, e string) (r typr.Op) {
testResult = append(testResult, e)
return
}
testValidator := func(s string) (e error) {
if strings.HasPrefix(s, "c") {
return
}
e = fmt.Errorf("invalid element [ %s ]", s)
return
}
Range(addToList, From("apple", "banana", "coconut"), RangeOpts[string]{Step: -1, ValidateElements: testValidator})
assrtEqual(t, 1, len(testResult))
assrtEqual(t, []string{"coconut"}, testResult)
}
func TestRange_7(t *testing.T) {
var testResult []string
assrtEqual(t, 0, len(testResult))
addToList := func(_ int, e string) (r typr.Op) {
testResult = append(testResult, e)
return
}
Range(addToList, []string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}, RangeOpts[string]{Step: -2})
assrtEqual(t, 5, len(testResult))
assrtEqual(t, []string{"10", "8", "6", "4", "2"}, testResult)
}
func TestRange_Concurrently(t *testing.T) {
var testResult []string
assrtEqual(t, 0, len(testResult))
ch := make(chan string)
go func() {
/***********************************************
* pop items off of the channel one at a time, *
* and add them to testResult *
* the items will show up, at some point ... *
***********************************************/
var item string
itemOK := true
for itemOK {
if item, itemOK = <-ch; itemOK {
testResult = append(testResult, item)
}
}
}()
addToListSafely := func(_ int, e string) (r typr.Op) {
// just send whatever's received to the channel
// it's the safest bet ...
ch <- e
return
}
options := RangeOpts[string]{
Step: -2, // submit every other element to the Ranger function in "reverse order", starting with the last element
Concurrently: true, // process elements concurrently
}
Range(
addToListSafely, // the Ranger function
[]string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}, // the array
options,
)
close(ch) // finished with the channel
time.Sleep(1 * time.Second) // tic toc tic
// *hopefully* the last elements have been added to testResult, by now
// with time, and concurrency, it's always a coin-toss
assrtEqual(t, 5, len(testResult))
testResultStr := strings.Join(testResult, " ") + " "
assrtTrue(t, strings.Contains(testResultStr, "2 "))
assrtTrue(t, strings.Contains(testResultStr, "4 "))
assrtTrue(t, strings.Contains(testResultStr, "6 "))
assrtTrue(t, strings.Contains(testResultStr, "8 "))
assrtTrue(t, strings.Contains(testResultStr, "10 "))
t.Logf("testResult == %v", testResult)
}