1
- package iter
1
+ package iter_test
2
2
3
3
import (
4
4
"errors"
@@ -8,12 +8,14 @@ import (
8
8
"sync/atomic"
9
9
"testing"
10
10
11
+ "github.com/sourcegraph/conc/iter"
12
+
11
13
"github.com/stretchr/testify/require"
12
14
)
13
15
14
16
func ExampleIterator () {
15
17
input := []int {1 , 2 , 3 , 4 }
16
- iterator := Iterator [int ]{
18
+ iterator := iter. Iterator [int ]{
17
19
MaxGoroutines : len (input ) / 2 ,
18
20
}
19
21
@@ -34,7 +36,7 @@ func TestIterator(t *testing.T) {
34
36
t .Run ("safe for reuse" , func (t * testing.T ) {
35
37
t .Parallel ()
36
38
37
- iterator := Iterator [int ]{MaxGoroutines : 999 }
39
+ iterator := iter. Iterator [int ]{MaxGoroutines : 999 }
38
40
39
41
// iter.Concurrency > numInput case that updates iter.Concurrency
40
42
iterator .ForEachIdx ([]int {1 , 2 , 3 }, func (i int , t * int ) {})
@@ -45,12 +47,12 @@ func TestIterator(t *testing.T) {
45
47
t .Run ("allows more than defaultMaxGoroutines() concurrent tasks" , func (t * testing.T ) {
46
48
t .Parallel ()
47
49
48
- wantConcurrency := 2 * defaultMaxGoroutines ()
50
+ wantConcurrency := 2 * iter . DefaultMaxGoroutines ()
49
51
50
52
maxConcurrencyHit := make (chan struct {})
51
53
52
54
tasks := make ([]int , wantConcurrency )
53
- iterator := Iterator [int ]{MaxGoroutines : wantConcurrency }
55
+ iterator := iter. Iterator [int ]{MaxGoroutines : wantConcurrency }
54
56
55
57
var concurrentTasks atomic.Int64
56
58
iterator .ForEach (tasks , func (t * int ) {
@@ -79,7 +81,7 @@ func TestForEachIdx(t *testing.T) {
79
81
t .Parallel ()
80
82
f := func () {
81
83
ints := []int {}
82
- ForEachIdx (ints , func (i int , val * int ) {
84
+ iter . ForEachIdx (ints , func (i int , val * int ) {
83
85
panic ("this should never be called" )
84
86
})
85
87
}
@@ -90,7 +92,7 @@ func TestForEachIdx(t *testing.T) {
90
92
t .Parallel ()
91
93
f := func () {
92
94
ints := []int {1 }
93
- ForEachIdx (ints , func (i int , val * int ) {
95
+ iter . ForEachIdx (ints , func (i int , val * int ) {
94
96
panic ("super bad thing happened" )
95
97
})
96
98
}
@@ -100,7 +102,7 @@ func TestForEachIdx(t *testing.T) {
100
102
t .Run ("mutating inputs is fine" , func (t * testing.T ) {
101
103
t .Parallel ()
102
104
ints := []int {1 , 2 , 3 , 4 , 5 }
103
- ForEachIdx (ints , func (i int , val * int ) {
105
+ iter . ForEachIdx (ints , func (i int , val * int ) {
104
106
* val += 1
105
107
})
106
108
require .Equal (t , []int {2 , 3 , 4 , 5 , 6 }, ints )
@@ -109,7 +111,7 @@ func TestForEachIdx(t *testing.T) {
109
111
t .Run ("huge inputs" , func (t * testing.T ) {
110
112
t .Parallel ()
111
113
ints := make ([]int , 10000 )
112
- ForEachIdx (ints , func (i int , val * int ) {
114
+ iter . ForEachIdx (ints , func (i int , val * int ) {
113
115
* val = i
114
116
})
115
117
expected := make ([]int , 10000 )
@@ -127,7 +129,7 @@ func TestForEach(t *testing.T) {
127
129
t .Parallel ()
128
130
f := func () {
129
131
ints := []int {}
130
- ForEach (ints , func (val * int ) {
132
+ iter . ForEach (ints , func (val * int ) {
131
133
panic ("this should never be called" )
132
134
})
133
135
}
@@ -138,7 +140,7 @@ func TestForEach(t *testing.T) {
138
140
t .Parallel ()
139
141
f := func () {
140
142
ints := []int {1 }
141
- ForEach (ints , func (val * int ) {
143
+ iter . ForEach (ints , func (val * int ) {
142
144
panic ("super bad thing happened" )
143
145
})
144
146
}
@@ -148,7 +150,7 @@ func TestForEach(t *testing.T) {
148
150
t .Run ("mutating inputs is fine" , func (t * testing.T ) {
149
151
t .Parallel ()
150
152
ints := []int {1 , 2 , 3 , 4 , 5 }
151
- ForEach (ints , func (val * int ) {
153
+ iter . ForEach (ints , func (val * int ) {
152
154
* val += 1
153
155
})
154
156
require .Equal (t , []int {2 , 3 , 4 , 5 , 6 }, ints )
@@ -157,7 +159,7 @@ func TestForEach(t *testing.T) {
157
159
t .Run ("huge inputs" , func (t * testing.T ) {
158
160
t .Parallel ()
159
161
ints := make ([]int , 10000 )
160
- ForEach (ints , func (val * int ) {
162
+ iter . ForEach (ints , func (val * int ) {
161
163
* val = 1
162
164
})
163
165
expected := make ([]int , 10000 )
@@ -479,7 +481,7 @@ func BenchmarkForEach(b *testing.B) {
479
481
b .Run (strconv .Itoa (count ), func (b * testing.B ) {
480
482
ints := make ([]int , count )
481
483
for i := 0 ; i < b .N ; i ++ {
482
- ForEach (ints , func (i * int ) {
484
+ iter . ForEach (ints , func (i * int ) {
483
485
* i = 0
484
486
})
485
487
}
0 commit comments