-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlisp_tests.js
More file actions
162 lines (156 loc) · 6.78 KB
/
lisp_tests.js
File metadata and controls
162 lines (156 loc) · 6.78 KB
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
159
160
161
162
// Copyright(C) 2010 synapticfailure.com, except where otherwise noted
var node_lisp = require('./node_lisp.js');
function validate(){
var tests = [['"hello world"', '"hello world"'],
['(quote "hello world")', '"hello world"'],
['(quote a)', 'a'],
['\'a', 'a'],
['(quote (a b c))', '(a b c)'],
['\'(a b c)', '(a b c)'],
['\'(a (b c) d)', '(a (b c) d)'],
['nil', 'nil'],
['\'nil', 'nil'],
['()', 'nil'],
['\'()', 'nil'],
['(atom \'x)', 't'],
['(atom \'())', 't'],
['(atom nil)', 't'],
['(atom \'(a b c))', 'nil'],
['(atom "hello world")', 't'],
['(numberp 3)', 't'],
['(numberp \'a)', 'nil'],
['(numberp \'(a b c))', 'nil'],
['(numberp (+ 1 3))', 't'],
['3', '3'],
['(+)', '0'],
['(+ 1)', '1'],
['(+ 2 3)', '5'],
['(+ 4 5 6)', '15'],
['(- 1)', '-1'],
['(- 2 1)', '1'],
['(- 3 2 1)', '0'],
['(*)', '1'],
['(* 3)', '3'],
['(* 6 10)', '60'],
['(* 1 2 3)', '6'],
['(/ 1)', '1'],
['(/ 6 2)', '3'],
['(/ 12 2 2)', '3'],
['(< 1)', 't'],
['(< 1 3)', 't'],
['(< 3 1)', 'nil'],
['(< 1 2 3)', 't'],
['(< 4 1 3)', 'nil'],
['(> 1)', 't'],
['(> 1 3)', 'nil'],
['(> 3 1)', 't'],
['(> 4 2 1)', 't'],
['(> 2 1 4)', 'nil'],
['(= 1)', 't'],
['(= 1 3)', 'nil'],
['(= 3 3)', 't'],
['(eq 1 3)', 'nil'],
['(eq 3 3)', 't'],
['(eq 3 nil)', 'nil'],
['(eq nil nil)', 't'],
['(eq \'a \'b)', 'nil'],
['(eq \'a \'a)', 't'],
['(eq \'3 3)', 't'],
['(and)', 't'],
['(and t)', 't'],
['(and t nil)', 'nil'],
['(and t t nil)', 'nil'],
['(and t t t)', 't'],
['(and (atom \'a) (eq \'a \'a))', 't'],
['(and (atom \'a) (eq \'a \'b))', 'nil'],
['(or)', 'nil'],
['(or t)', 't'],
['(or t nil)', 't'],
['(or t t nil)', 't'],
['(or t t t)', 't'],
['(or nil)', 'nil'],
['(or nil t)', 't'],
['(or nil nil t)', 't'],
['(or nil nil nil)', 'nil'],
['(car nil)', 'nil'],
['(car \'())', 'nil'],
['(car \'(a))', 'a'],
['(car \'(a b c d))', 'a'],
['(car \'(a (b c) d))', 'a'],
['(cdr nil)', 'nil'],
['(cdr \'())', 'nil'],
['(cdr \'(a))', 'nil'],
['(cdr \'(a b c d))', '(b c d)'],
['(cdr \'(a (b c) d))', '((b c) d)'],
['(cadr \'((a b) (c d) e))', '(c d)'],
['(caddr \'((a b) (c d) e))', 'e'],
['(cdar \'((a b) (c d) e))', '(b)'],
['(cons \'a \'(b c))', '(a b c)'],
['(cons \'a (cons \'b (cons \'c \'())))', '(a b c)'],
['(car (cons \'a \'(b c)))', 'a'],
['(cdr (cons \'a \'(b c)))', '(b c)'],
['(cons nil \'(c d))', '(nil c d)'],
['(cons \'(a b) \'(c d))', '((a b) c d)'],
['(cons \'a nil)', '(a)'],
['(list)', 'nil'],
['(list \'a)', '(a)'],
['(list \'a \'b \'c)', '(a b c)'],
['(list nil)', '(nil)'],
['(car (cons 1 nil))', '1'],
['(cond)', 'nil'],
['(cond (\'a))', 'a'],
['(cond (\'(a b c)))', '(a b c)'],
['(cond (\'a nil 1))', '1'],
['(cond ((eq \'a \'b) \'first) ((atom \'a) \'second))', 'second'],
['(cond ((eq \'a \'b) \'first) ((atom \'(a b c)) \'second))', 'nil'],
['(cond ((eq \'a \'a) \'first) ((atom \'(a b c)) \'second))', 'first'],
['(cond ((eq \'a \'b) \'first) ((atom \'(a b c)) \'second) (t \'third))', 'third'],
['((lambda (x) (cons x \'(b))) \'a)', '(a b)'],
['((lambda (x y) (cons x (cdr y))) \'z \'(a b c))', '(z b c)'],
['((lambda (f) (f \'(b c))) \'(lambda (x) (cons \'a x)))', '(a b c)'],
['(null \'a)', 'nil'],
['(null \'())', 't'],
['(null nil)', 't'],
['(not (eq \'a \'a))', 'nil'],
['(not (eq \'a \'b))', 't'],
['(append \'(a b) \'(c d))', '(a b c d)'],
['(append \'() \'(c d))', '(c d)'],
['(pair \'(x y z) \'(a b c))', '((x a) (y b) (z c))'],
['(assoc \'x \'((x a) (y b)))', 'a'],
['(assoc \'x \'((x new) (x a) (y b)))', 'new'],
['(reverse \'(a b c))', '(c b a)'],
['(let ((a 3) (b 4) (c 5)) (* (+ a b) c))', '35'],
['(let ((a 1)) (let ((a (+ a 1))) (+ a 1)) (+ a 1))', '2'],
['(let () (+ 1 3) (+ 3 4))', '7'],
['(set a 3)', '3'],
['a', '3'],
['(set a nil b 4 c \'a)', 'a'],
['a', 'nil'],
['b', '4'],
['c', 'a'],
['(cond ((null nil) (set a 5)) (t (set a 7)))', '5'],
['(if (null nil) (set a 5) (set a 7))', '5'],
['a', '5'],
['(eval (+ 2 3))', '5'],
['(((lambda (f) ((lambda (proc) (f (lambda (arg) ((proc proc) arg)))) (lambda (proc) (f (lambda (arg) ((proc proc) arg)))))) (lambda (self) (lambda (ls) (cond ((null ls) 0) (t (+ 1 (self (cdr ls)))))))) \'(1 2 3 4 5))', '5'],
['(((lambda (f) ((lambda (proc) (f (lambda (arg) ((proc proc) arg)))) (lambda (proc) (f (lambda (arg) ((proc proc) arg)))))) (lambda (self) (lambda (ls) (if (null ls) 0 (+ 1 (self (cdr ls))))))) \'(1 2 3 4 5))', '5']
];
var passed = true;
var count = tests.length;
for(var i = 0; i < count; i++){
var input = tests[i][0];
var expected = tests[i][1];
var output = node_lisp.report(input, true);
if(output != expected){
console.log('Test failed: input = "' + input + '", output = "' + output + '", expected = "' + expected + '"');
passed = false;
break;
}
}
if(passed){
console.log('All tests passed');
//reset();
}
return passed;
}
exports.validate = validate;