-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumbers_test.go
157 lines (120 loc) · 3.6 KB
/
numbers_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
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
package λ
import (
qt "github.com/frankban/quicktest"
"testing"
)
// makeCounter creates a closure around an integer that is utilised by
// the 3 functions it returns: an incrementor, a getter and a
// resetter. The incrementor is a λ function, and thus can be used as
// a parameter to, or return value from any other λ type.
// Specifically if you pass it to a church numeral, it will be called
// the number of times that numeral represents, and thus can be used
// for its side-effect of generating an Integer equivalent of any
// given Church numeral.
func makeCounter() (λ, func() int, func()) {
var i int = 0
inc := func(f λ) λ {
i = i + 1
return f
}
get := func() int {
return i
}
reset := func() {
i = 0
}
return inc, get, reset
}
// intResult is a convenience method used in testing. It wraps up the
// steps required to convert a Church numeral to a Go integer.
func intResult(l λ) int {
counter, count, _ := makeCounter()
_ = l(counter)(nil)
return count()
}
func TestZero(t *testing.T) {
c := qt.New(t)
c.Assert(intResult(zero), qt.Equals, 0)
}
func TestOne(t *testing.T) {
c := qt.New(t)
c.Assert(intResult(one), qt.Equals, 1)
}
func TestTwo(t *testing.T) {
c := qt.New(t)
c.Assert(intResult(two), qt.Equals, 2)
}
func TestSucc(t *testing.T) {
c := qt.New(t)
// The succesor of zero is 1
c.Assert(intResult(succ(zero)), qt.Equals, 1)
c.Assert(intResult(zero(succ)), qt.Equals, 1)
// The successor of one is 2
c.Assert(intResult(succ(one)), qt.Equals, 2)
// The 0th successor of 0 is 0
c.Assert(intResult(zero(succ)(zero)), qt.Equals, 0)
// The 0th succesor of one is 0
c.Assert(intResult(zero(succ)(one)), qt.Equals, 1)
// The 1st succesor of 0 is 1
c.Assert(intResult(one(succ)(zero)), qt.Equals, 1)
// The 1st succesor of 1 is 2
c.Assert(intResult(one(succ)(one)), qt.Equals, 2)
// The 3rd succesor of 3 is 6
three := succ(two)
c.Assert(intResult(three), qt.Equals, 3)
c.Assert(intResult(three(succ)(three)), qt.Equals, 6)
}
func TestPred(t *testing.T) {
c := qt.New(t)
three := succ(two)
// The predecessor of 1 is 0
c.Assert(intResult(pred(one)), qt.Equals, 0)
// The predecessor of 2 is 1
c.Assert(intResult(pred(two)), qt.Equals, 1)
// The predecessor of 3 is 2
c.Assert(intResult(pred(three)), qt.Equals, 2)
// The first predecessor of 1 is 0
c.Assert(intResult(one(pred)(one)), qt.Equals, 0)
// The first predecessor of 2 is 1
c.Assert(intResult(one(pred)(two)), qt.Equals, 1)
// The first predecessor of 3 is 2
c.Assert(intResult(one(pred)(three)), qt.Equals, 2)
// The second predecessor of 3 is 1
c.Assert(intResult(two(pred)(three)), qt.Equals, 1)
// The third predecessor of 3 is 0
c.Assert(intResult(three(pred)(three)), qt.Equals, 0)
}
func TestPlus(t *testing.T) {
c := qt.New(t)
c.Assert(intResult(one(plus)(one)), qt.Equals, 2)
c.Assert(intResult(two(plus)(two)), qt.Equals, 4)
}
func TestPowers(t *testing.T) {
c := qt.New(t)
// 0**1
c.Assert(intResult(one(zero)), qt.Equals, 0)
// 1**0
c.Assert(intResult(zero(one)), qt.Equals, 1)
// 1**1
c.Assert(intResult(one(one)), qt.Equals, 1)
// 1**2
c.Assert(intResult(two(one)), qt.Equals, 1)
// 1**2
c.Assert(intResult(one(two)), qt.Equals, 2)
// 2**2
c.Assert(intResult(two(two)), qt.Equals, 4)
}
func TestMul(t *testing.T) {
c := qt.New(t)
four := mul(two)(two)
c.Assert(intResult(four), qt.Equals, 4)
eight := mul(four)(two)
c.Assert(intResult(eight), qt.Equals, 8)
}
func TestIsZero(t *testing.T) {
c := qt.New(t)
b := IsZero(zero)
c.Assert(intResult(IfThenElse(b)(one)(two)), qt.Equals, 1)
b = IsZero(one)
c.Assert(intResult(IfThenElse(b)(one)(two)), qt.Equals, 2)
}