-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmacros_test.go
158 lines (130 loc) · 3.38 KB
/
macros_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
158
package main
import (
"fmt"
"math/big"
"testing"
)
func TestBasicMatch(t *testing.T) {
pparse := NewParser("(a ...)")
pval, _ := pparse.GetValue()
fparse := NewParser("(1 2 3)")
fval, _ := fparse.GetValue()
if !IsMatch(pval, fval, []Symbol{}) {
t.Errorf("(a ...) did not match (1 2 3)")
}
}
func TestDotMatch(t *testing.T) {
pparse := NewParser("(a . b)")
pval, _ := pparse.GetValue()
fparse := NewParser("(1 2 3)")
fval, _ := fparse.GetValue()
if !IsMatch(pval, fval, []Symbol{}) {
t.Errorf("(a . b) did not match (1 2 3)")
}
}
func TestLetMatch(t *testing.T) {
pparse := NewParser("(((a b) ...) body ...)")
pval, _ := pparse.GetValue()
fparse := NewParser("(((x 1) (y 2)) (+ 1 1) (+ 1 2))")
fval, _ := fparse.GetValue()
if !IsMatch(pval, fval, []Symbol{}) {
t.Errorf("Match failed on 'let'")
}
}
func TestBasicMap(t *testing.T) {
pparse := NewParser("(a ...)")
pval, _ := pparse.GetValue()
fparse := NewParser("(1 2 3)")
fval, _ := fparse.GetValue()
m := MacroMap{}
if err := m.parse(pval, fval, []Symbol{}, true); err != nil {
t.Errorf("Could not parse to map: %v", err)
}
a := Str2Sym("a")
if len(m[a].v) != 3 {
t.Errorf("Wrong length for map element: %d vs 3", len(m[a].v))
}
expected := []Integer{
Integer(*big.NewInt(1)),
Integer(*big.NewInt(2)),
Integer(*big.NewInt(3)),
}
for i := range m[a].v {
x := big.Int(m[a].v[i].(Integer))
y := big.Int(expected[i])
if x.Cmp(&y) != 0 {
t.Errorf("Expected first element to be 1")
}
}
}
func TestTranscribe(t *testing.T) {
pparse := NewParser("(a ...)")
pval, _ := pparse.GetValue()
fparse := NewParser("(1 2 3)")
fval, _ := fparse.GetValue()
tparse := NewParser("((a ...))")
tval, _ := tparse.GetValue()
m := MacroMap{}
m.parse(pval, fval, []Symbol{}, true)
res, err := m.transcribe(tval, false, Str2Sym("macro"))
if err != nil {
t.Error(err)
}
rparse := NewParser("((1 2 3))")
rval, err := rparse.GetValue()
if err != nil {
t.Error(err)
}
if !IsEqual(rval, res) {
PrintValue(rval)
fmt.Println()
PrintValue(res)
fmt.Println()
t.Errorf("Mismatch %T", res)
}
}
func TestParseSyntaxRules(t *testing.T) {
input := `(syntax-rules (a b) ((_ b) (cons b a)) ((_ a) (cons a b)))`
parse := NewParser(input)
val, err := parse.GetValue()
if err != nil {
t.Errorf("Error occurred while parsing input: %v", err)
}
result, err := ParseSyntaxRules(val)
if err != nil {
t.Errorf("Error occurred while parsing syntax rules: %v", err)
}
expectedLiterals := []Symbol{Str2Sym("a"), Str2Sym("b")}
for i, literal := range result.Literals {
if literal != expectedLiterals[i] {
t.Errorf("Expected literal %v, but got %v",
expectedLiterals[i], literal)
}
}
expectedPatterns := [][]Value{
{Str2Sym("_"), Str2Sym("b")},
{Str2Sym("_"), Str2Sym("a")},
}
for i, pattern := range result.Patterns {
vec, _ := list2vec(pattern)
for j, value := range vec {
if value != expectedPatterns[i][j] {
t.Errorf("Expected pattern %v, but got %v",
expectedPatterns[i][j], value)
}
}
}
expectedTemplates := [][]Value{
{Str2Sym("cons"), Str2Sym("b"), Str2Sym("a")},
{Str2Sym("cons"), Str2Sym("a"), Str2Sym("b")},
}
for i, template := range result.Templates {
vec, _ := list2vec(template.(*Pair))
for j, val := range vec {
if !IsEqual(val, expectedTemplates[i][j]) {
t.Errorf("Expected template %+v, but got %+v",
expectedTemplates[i][j], val)
}
}
}
}