-
Notifications
You must be signed in to change notification settings - Fork 0
/
ch9.lisp
45 lines (35 loc) · 1007 Bytes
/
ch9.lisp
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
(defvar *test-name* nil)
(defun report-result (result form)
(format t "~:[FAIL~;pass~] ... ~a: ~a~%" result *test-name* form)
result)
(deftest test-+ ()
(check
(= (+ 1 2) 3)
(= (+ 1 2 3) 6)
(= (+ -1 -2) -3)))
(deftest test-* ()
(check
(= (* 1 2) 2)
(= (* 1 2 3) 6)
(= (* -1 -2) 2)))
(deftest test-arithmetic ()
(combine-results
(test-+)
(test-*)))
(defmacro with-gensyms ((&rest names) &body body)
`(let ,(loop for n in names collect `(,n (gensym)))
,@body))
(defmacro check (&body forms)
`(combine-results
,@(loop for f in forms collect `(report-result ,f ',f))))
(defmacro combine-results (&body forms)
(with-gensyms (result)
`(let ((,result t))
,@(loop for f in forms collect `(unless ,f (setf ,result nil)))
,result)))
(defmacro deftest (name parameters &body body)
`(defun ,name ,parameters
(let ((*test-name* (append *test-name* (list ',name))))
,@body)))
(deftest test-math ()
(test-arithmetic))