-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathxiter_test.go
More file actions
89 lines (79 loc) · 1.11 KB
/
Copy pathxiter_test.go
File metadata and controls
89 lines (79 loc) · 1.11 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
package xiter
import (
"fmt"
"testing"
)
func TestCoroutine(t *testing.T) {
yield, stop := Coroutine(func(first int, yield func(int) (int, bool)) int {
if first != 0 {
t.Fatal(first)
}
prev := first
for {
v, ok := yield(prev + 1)
if !ok {
if prev != 10 {
t.Fatal(v)
}
return -1
}
if v != prev+2 {
t.Fatal(v)
}
prev = v
}
})
prev, ok := yield(0)
if !ok {
t.Fatal(prev)
}
for prev < 10 {
v, ok := yield(prev + 1)
if !ok {
t.Fatal(v)
}
if v != prev+2 {
t.Fatal(v)
}
prev = v
}
r := stop()
if r != -1 {
t.Fatal(r)
}
}
func ExampleCoroutine() {
next, stop := Coroutine(func(val int, next func(int) (int, bool)) int {
for {
v, ok := next(val * 2)
if !ok {
return v
}
val = v
}
})
defer stop()
var val int
for range 10 {
v, ok := next(val + 1)
if !ok {
break
}
val = v
}
fmt.Printf("result: %v\n", stop())
// Output: result: 1023
}
func TestPush(t *testing.T) {
next, stop := Push(Sum[int])
defer stop()
for n := range 10 {
if !next(n) {
t.Fatal(n)
}
}
r := stop()
if r != 45 {
t.Fatal(r)
}
}