-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlispInterpreter.test.js
More file actions
80 lines (65 loc) · 1.62 KB
/
lispInterpreter.test.js
File metadata and controls
80 lines (65 loc) · 1.62 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
import lisp from './lispInterpreter'
test('Evaluate add', () => {
expect(lisp(`(+ 1 2 3)`))
.toEqual(6)
})
test('Evaluate nested', () => {
expect(lisp(`(* (+ 1 2) (+ 2 2))`))
.toEqual(12)
})
test('Evaluate define & refrence', () => {
expect(lisp(`(begin (define r 10) (* r r))`))
.toEqual(100)
})
test('Evaluate begin', () => {
expect(lisp(`(begin 10 20 30)`))
.toEqual(30)
})
test('Evaluate max', () => {
expect(lisp(`(max 2 4 9 2 1)`))
.toEqual(9)
})
test('Evaluate multiple args', () => {
expect(lisp(`(< 2 1 2)`))
.toEqual('#f')
})
test('Evaluate comparision with multiple args', () => {
expect(lisp(`(> 2 1 2)`))
.toEqual('#f')
})
test('Evaluate conditional', () => {
expect(lisp(`(if (> 1 2) 1 2)`))
.toEqual(2)
expect(lisp(`(if (> 2 1) 1 2)`))
.toEqual(1)
})
test('Evaluate not', () => {
expect(lisp(`(not #f)`))
.toEqual('#t')
expect(lisp(`(not #t)`))
.toEqual('#f')
})
test('Evaluate constant(num)', () => {
expect(lisp(`(+ 99 )`))
.toEqual(99)
})
test('Evaluate invalid input', () => {
expect(() => lisp(`(begin (+ 1 2)(* 1 2)`))
.toThrow()
})
test('Evaluate define expression', () => {
expect(lisp(`(begin (define r 10) (define sqr (* r r)) sqr)`))
.toEqual(100)
})
test('Evaluate factorial', () => {
expect(lisp(`(begin (define fact (lambda (n) (if (<= n 1) 1 (* n (fact (- n 1))))))(fact 10))`))
.toEqual(3628800)
})
test('Evaluate fib', () => {
expect(lisp(`(begin (define fib (lambda (n) (if (< n 2) 1 (+ (fib (- n 1)) (fib (- n 2))))))(fib 10))`))
.toEqual(89)
})
test('Evaluate invalid expression', () => {
expect(() => lisp(`(+ 1 2 * 1 2)`))
.toThrow()
})