-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprefetch_test.go
60 lines (56 loc) · 1.14 KB
/
prefetch_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
package lazy
import (
"go4ml.xyz/errstr"
"gotest.tools/v3/assert"
"reflect"
"testing"
)
func linlist(list interface{}) Source {
return func(xs ...interface{}) Stream {
worker := 0
open := NoPrefetch
for _, x := range xs {
if f, ok := x.(func() (int, int, Prefetch)); ok {
worker, _, open = f()
} else {
return Error(errstr.Errorf("unsupported source option: %v", x))
}
}
v := reflect.ValueOf(list)
return open(worker, func() Stream {
index := 0
return func(next bool) (r interface{}, i int) {
if next && index < v.Len() {
i = index
r = v.Index(i).Interface()
index++
return r, i
}
return EoS, index
}
})
}
}
func Test_Prefetch_1(t *testing.T) {
i := 0
linlist(colors).MustDrain(Sink(func(v interface{}, _ error) (_ error) {
if v != nil {
x := v.(Color)
y := colors[i]
assert.Assert(t, y.Color == x.Color)
i++
} else {
assert.Assert(t, i == len(colors))
}
return
}), 2)
}
func Test_Prefetch_2(t *testing.T) {
a := Sequence(func(i int) interface{} {
if i < 100 {
return i
}
return EoS
}).MustCollectAny(2).([]int)
assert.Assert(t, len(a) == 100)
}