-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlnotation_test.go
113 lines (102 loc) · 2.18 KB
/
lnotation_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
package lnotation
import (
"context"
"log"
"testing"
"github.com/jig/lisp"
. "github.com/jig/lisp/env"
"github.com/jig/lisp/lib/assert/nsassert"
"github.com/jig/lisp/lib/concurrent/nsconcurrent"
"github.com/jig/lisp/lib/core/nscore"
"github.com/jig/lisp/lib/coreextented/nscoreextended"
. "github.com/jig/lisp/types"
)
// (range 0 4)
func TestLNotationMinimalExample(t *testing.T) {
sampleCode := LS("range", 0, 4)
lr, err := lisp.EVAL(context.TODO(), sampleCode, NewTestEnv())
if err != nil {
t.Fatal(err)
}
vec, ok := lr.(Vector)
if !ok {
t.Fatal("not a vector")
}
for i := 0; i < 4; i++ {
if vec.Val[i] != i {
t.Fatalf("not %d", i)
}
}
}
// (reduce + 0 (range 0 10000))
func TestLNotation(t *testing.T) {
sampleCode := LS("reduce", S("+"), 0, LS("range", 0, 10000))
lr, err := lisp.EVAL(context.TODO(), sampleCode, NewTestEnv())
if err != nil {
t.Fatal(err)
}
switch lr := lr.(type) {
case int:
if lr != 49995000 {
t.Fatal("incorrect result")
}
default:
t.Fatal("incorrect type")
}
}
// (do
// (def fib (fn [n]
// (if (= n 0)
// 1
// (if (= n 1)
// 1
// (+ (fib (- n 1))
// (fib (- n 2)))))))
// (fib 50))
func TestLNotationFibonacci(t *testing.T) {
// use of a mix of L() and LS()
do := S("do")
def := S("def")
fib := S("fib")
fn := S("fn")
n := S("n")
iF := S("if")
env := NewTestEnv()
lr, err := lisp.EVAL(context.TODO(),
L(do,
L(def, fib, L(fn, V([]Symbol{n}),
L(iF, LS("=", n, 0),
1,
L(iF, LS("=", n, 1),
1,
LS("+", L(fib, LS("-", n, 1)),
L(fib, LS("-", n, 2))))))),
L(fib, 15)),
env,
)
if err != nil {
t.Fatal(err)
}
if lr.(int) != 987 {
t.Fatal("wrong result for fibonacci")
}
}
func NewTestEnv() EnvType {
repl_env := NewEnv()
for _, library := range []struct {
name string
load func(repl_env EnvType) error
}{
{"core mal", nscore.Load},
{"core mal with input", nscore.LoadInput},
{"command line args", nscore.LoadCmdLineArgs},
{"concurrent", nsconcurrent.Load},
{"core mal extended", nscoreextended.Load},
{"assert", nsassert.Load},
} {
if err := library.load(repl_env); err != nil {
log.Fatalf("Library Load Error: %v\n", err)
}
}
return repl_env
}