-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheuler_test.go
89 lines (72 loc) · 1.66 KB
/
euler_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
package main
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestIntGen(t *testing.T) {
out := intGen(2)
assert.Equal(t, 1, <-out)
assert.Equal(t, 2, <-out)
_, more := <-out
assert.False(t, more)
}
func TestFilterInt(t *testing.T) {
in := intGen(4)
out := filterInt(in, func(n int) bool { return n%2 == 0 })
assert.Equal(t, 2, <-out)
assert.Equal(t, 4, <-out)
assert.Equal(t, 0, <-out)
}
func TestFibGen(t *testing.T) {
done := make(chan bool)
defer close(done)
expected := []int{1, 1, 2, 3, 5, 8, 13}
fib := fibGen(done)
for _, n := range expected {
assert.Equal(t, n, <-fib)
}
}
func TestTakeUntil(t *testing.T) {
seq := func(done <-chan bool) chan int {
out := make(chan int)
i := 0
go func() {
for {
select {
case <-done:
close(out)
return
default:
i++
out <- i
}
}
}()
return out
}
done := make(chan bool)
expected := []int{1, 2, 3, 4, 5}
untilFive := func(n int) bool { return n > 5 }
integers := seq(done)
takeUntil5 := takeUntil(integers, done, untilFive)
i := 0
for n := range takeUntil5 {
assert.Equal(t, expected[i], n)
i++
}
}
func TestSum(t *testing.T) {
assert.Equal(t, 15, sumIntC(intGen(5)))
}
func TestPrimesUpto(t *testing.T) {
assert.Equal(t, []int{}, primesUpto(1))
assert.Equal(t, []int{2}, primesUpto(2))
assert.Equal(t, []int{2, 3}, primesUpto(3))
assert.Equal(t, []int{2, 3, 5, 7}, primesUpto(10))
assert.Equal(t, []int{2, 3, 5, 7, 11, 13, 17, 19}, primesUpto(20))
}
func TestNPrimes(t *testing.T) {
assert.Equal(t, []int{2}, nPrimes(1))
assert.Equal(t, []int{2, 3, 5, 7, 11}, nPrimes(5))
assert.Equal(t, []int{2, 3, 5, 7, 11, 13}, nPrimes(6))
}