-
Notifications
You must be signed in to change notification settings - Fork 0
/
combinator_test.go
123 lines (115 loc) · 3.38 KB
/
combinator_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
package combinator
import (
"context"
"fmt"
"testing"
"time"
)
func TestSchonfinkel(t *testing.T) {
tests := map[string]string{
"SKKx": "Ix",
"SKyx": "Ix",
"S(KS)Kxyz": "Zxyz",
"S(ZZS)(KK)xyz": "Txyz",
}
for statement1, statement2 := range tests {
t.Run(fmt.Sprintf("%s = %s", statement1, statement2), func(t *testing.T) {
result1, _ := Schonfinkel.Transform(context.Background(), statement1)
result2, _ := Schonfinkel.Transform(context.Background(), statement2)
if result1 != result2 {
t.Errorf("combinators %s and %s not equal, got %s and %s", statement1, statement2, result1, result2)
}
})
}
}
func TestSKI(t *testing.T) {
tests := map[string]string{
"Ix": "x",
"SKSK": "K",
"SKKx": "x",
"S(K(SI))Kab": "ba",
}
for statement, expectedResult := range tests {
t.Run(statement, func(t *testing.T) {
actualResult, _ := SKI.Transform(context.Background(), statement)
if expectedResult != actualResult {
t.Errorf("transformed SKI statement %s incorrectly, expected %s but got %s", statement, expectedResult, actualResult)
}
})
}
}
func TestIota(t *testing.T) {
tests := map[string]string{
"iix": "x",
"(i(i(ii)))": "K",
"(i(i(i(ii))))": "S",
}
for statement, expectedResult := range tests {
t.Run(statement, func(t *testing.T) {
actualResult, _ := Iota.Transform(context.Background(), statement)
if expectedResult != actualResult {
t.Errorf("transformed Iota statement %s incorrectly, expected %s but got %s", statement, expectedResult, actualResult)
}
})
}
}
func TestChurch(t *testing.T) {
tests := map[string]string{
"0fx": "x",
"S(0)fx": "fx",
"S(S(0))fx": "f(fx)",
"P(0)(0)fx": "x",
"P(S(S(S(0))))(S(S(0)))fx": "f(f(f(f(fx))))",
"M(S(S(S(0))))(S(S(0)))fx": "f(f(f(f(f(fx)))))",
"E(S(S(S(0))))(S(S(0)))fx": "f(f(f(f(f(f(f(f(fx))))))))",
}
for statement, expectedResult := range tests {
t.Run(statement, func(t *testing.T) {
actualResult, _ := Church.Transform(context.Background(), statement)
if expectedResult != actualResult {
t.Errorf("transformed Church statement %s incorrectly, expected %s but got %s", statement, expectedResult, actualResult)
}
})
}
}
func TestContext(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Millisecond)
_, err := BCKW.Transform(ctx, "WWW")
if err == nil {
t.Error("expected error")
}
cancel()
if err.Error() != context.DeadlineExceeded.Error() {
t.Errorf("expected error %s, got %s", context.DeadlineExceeded.Error(), err.Error())
}
}
func TestLoopDetection(t *testing.T) {
_, err := BCKW.Transform(context.Background(), "WWW")
if err == nil {
t.Error("expected error")
}
if err.Error() != "loop detected" {
t.Errorf("expected error loop detected, got %s", err.Error())
}
}
func TestFullyImproperCombinators(t *testing.T) {
tests := map[string]string{
//"Q": "S",
"QQ": "SQ",
}
for statement, expectedResult := range tests {
t.Run(statement, func(t *testing.T) {
actualResult, err := SK.With(Combinator{
Name: "Q",
Arguments: []string{},
Definition: "S",
}).Transform(context.Background(), statement)
if err != nil {
t.Error(err)
}
if actualResult != expectedResult {
t.Errorf("expected %s as a result, got %s", expectedResult, actualResult)
}
})
}
}