-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_test.go
149 lines (123 loc) · 3.33 KB
/
main_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
package main
import (
"math/big"
"os"
"testing"
)
func TestMain(m *testing.M) {
Top.Scope = TopScope // Put builtins into top-level scope
if int(SymLast) != len(SymbolNames) {
panic("Symbol table length mismatch")
}
Top.Run(Init, true)
Top.Run(CaseLambdaSRFI, true)
Top.Run(ListsSRFI, true)
for k, v := range TopScope.m { // Copy unmodified scope into basescope
BaseScope[k] = v
}
os.Exit(m.Run())
}
func TestLambdas(t *testing.T) {
Top.Run("(define add (lambda (x y) (+ x y)))", true)
result := stack.Top()
if _, ok := result.(*Procedure); !ok {
t.Errorf("Expected procedure, got %T", result)
}
Top.Run("(add 2 3)", true)
result, ok := stack.Top().(Integer)
if !ok {
t.Errorf("Expected integer, got %T", result)
}
if result := big.Int(result.(Integer)); result.Cmp(big.NewInt(5)) != 0 {
t.Errorf("Expected integer, got %v", result.String())
}
}
func TestAdder(t *testing.T) {
Top.Run("(define make-adder (lambda (x) (lambda (y) (+ x y))))", true)
result := stack.Top()
if _, ok := result.(*Procedure); !ok {
t.Errorf("Expected procedure, got %T", result)
}
Top.Run("(define add-2 (make-adder 2))", true)
result = stack.Top()
if _, ok := result.(*Procedure); !ok {
t.Errorf("Expected procedure, got %T", result)
}
Top.Run("(add-2 3)", true)
result, ok := stack.Top().(Integer)
if !ok {
t.Errorf("Expected integer, got %T", result)
}
if result := big.Int(result.(Integer)); result.Cmp(big.NewInt(5)) != 0 {
t.Errorf("Expected integer, got %v", result.String())
}
}
func TestCounter(t *testing.T) {
Top.Run("(define (make-ctr) (set! count 0)"+
"(lambda (ctr) (set! count (+ count 1)) count))", true)
result := stack.Top()
if _, ok := result.(*Procedure); !ok {
t.Errorf("Expected procedure, got %T", result)
}
Top.Run("(define ctr (make-ctr))", true)
result = stack.Top()
if _, ok := result.(*Procedure); !ok {
t.Errorf("Expected procedure, got %T", result)
}
Top.Run("(ctr)", true)
result, ok := stack.Top().(Integer)
if !ok {
t.Errorf("Expected integer, got %T", result)
}
if result := big.Int(result.(Integer)); result.Cmp(big.NewInt(1)) != 0 {
t.Errorf("Expected 1, got %v", result.String())
}
Top.Run("(ctr)", true)
result, ok = stack.Top().(Integer)
if !ok {
t.Errorf("Expected integer, got %T", result)
}
if result := big.Int(result.(Integer)); result.Cmp(big.NewInt(2)) != 0 {
t.Errorf("Expected 2, got %v", result.String())
}
}
func TestLet(t *testing.T) {
Top.Run("(let ((a 0) (b 1)) b)", true)
result, ok := stack.Top().(Integer)
if !ok {
t.Errorf("Expected integer, got %T", result)
}
if result := big.Int(result); result.Cmp(big.NewInt(1)) != 0 {
t.Errorf("Expected integer, got %v", result.String())
}
}
func TestAnd(t *testing.T) {
Top.Run("(and #t #t)", true)
result, ok := stack.Top().(Boolean)
if !ok {
t.Errorf("Expected boolean, got %T", result)
}
if result != true {
t.Errorf("Expected #t, got #f")
}
}
func TestOr(t *testing.T) {
Top.Run("(or #t #f)", true)
result, ok := stack.Top().(Boolean)
if !ok {
t.Errorf("Expected boolean, got %T", result)
}
if result != true {
t.Errorf("Expected true, got false")
}
}
func TestLetrec(t *testing.T) {
Top.Run("(letrec ((a #t)) #t)", true)
result, ok := stack.Top().(Boolean)
if !ok {
t.Errorf("Expected boolean, got %T", result)
}
if result != true {
t.Errorf("Expected true, got false")
}
}