-
Notifications
You must be signed in to change notification settings - Fork 7
/
more.stt
105 lines (90 loc) · 2.87 KB
/
more.stt
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
(define report-result
(lambda (result form)
(prn (if result "pass" "FAIL") " ... " form)))
(defmacro check (form)
`(report-result ~form '~form))
(define test-not
(lambda ()
(do
(check (= true (not false)))
(check (= true (not nil)))
(check (= false (not true)))
(check (= false (not "a")))
(check (= false (not 0))))))
(define test-variadic-args
(lambda ()
(do
(check (= true ((lambda (a & more) (list? more)) 1 2 3)))
(check (= true ((lambda (a & more) (list? more)) 1)))
(check (= true ((lambda (& more) (list? more)))))
(check (= 0 ((lambda (& more) (count more)))))
(check (= 0 ((lambda (a b c & more) (count more)) 1 2 3)))
(check (= 3 ((lambda (& more) (count more)) 1 2 3))))))
(define test-equality
(lambda ()
(do
(check (= (= 0 0) true))
(check (= (= 1 1) true))
(check (= (= 0 1) false))
(check (= (= true true) true))
(check (= (= false false) true))
(check (= (= false nil) false))
(check (= (= nil nil) true))
(check (= (= (list) (list)) true))
(check (= (= (list 0 1 2) (list 0 1 2)) true))
(check (= (= (list 0 2 1) (list 0 1 2)) false))
(check (= (= (list) (list 0 1 2)) false)))))
(define test-user-fns
(lambda ()
(do
(check (= ((lambda (a b) (+ b a)) 3 4) 7))
(check (= ((lambda () 2)) 2))
(check (= ((lambda (f x) (f x)) (lambda (a) (+ 1 a)) 7) 8)))))
(define test-closures
(lambda ()
(do
(check (= (((lambda (a) (lambda (b) (+ a b))) 5) 7) 12)))))
(define sum2 (lambda (n acc) (if (= n 0) acc (sum2 (- n 1) (+ n acc)))))
(define foo (lambda (n) (if (= n 0) 0 (bar (- n 1)))))
(define bar (lambda (n) (if (= n 0) 0 (foo (- n 1)))))
(define test-tco
(lambda ()
(do
(check (= (foo 10) 0))
(check (= (sum2 10 0) 55))
(check (= 2 (do (do 1 2)))))))
(define test-builtins
(lambda ()
(do
(check (= true (symbol? (symbol "asdf"))))
(check (= true (= 'asdf (symbol "asdf")))))))
(define test-exceptions
(lambda ()
(do
(check (= "c2" (try (try (throw "e1") (catch e (throw "e2"))) (catch e "c2"))))
(check (= "c2" (try (do (try "t1" (catch e "c1")) (throw "e1")) (catch e "c2")))))))
(define test-seq-fns
(lambda ()
(do
(check (= 1 (nth (list 1) 0)))
(check (= 2 (nth (list 1 2) 1)))
(check (= nil (nth (list 1 2 nil) 2)))
(check (= nil (first '())))
(check (= nil (first nil)))
(check (= nil (first (list))))
(check (= 6 (first (list 6))))
(check (= 7 (first (list 7 8 9))))
(check (= '() (rest '())))
(check (= '() (rest nil)))
(check (= '() (rest (list))))
(check (= '() (rest (list 6))))
(check (= '(8 9) (rest (list 7 8 9)))))))
;; (test-not)
(test-variadic-args)
(test-equality)
(test-user-fns)
(test-closures)
(test-tco)
(test-builtins)
(test-exceptions)
(test-seq-fns)