|
@@ -20,7 +20,7 @@
|
20
|
20
|
|
21
|
21
|
(define-test test-call-a-function
|
22
|
22
|
"DEFUN defines global functions"
|
23
|
|
- (assert-equal ___ (some-named-function 7 11)))
|
|
23
|
+ (assert-equal 18 (some-named-function 7 11)))
|
24
|
24
|
|
25
|
25
|
|
26
|
26
|
(define-test test-shadow-a-function
|
|
@@ -30,8 +30,8 @@
|
30
|
30
|
(assert-eq 18 (some-named-function 7 11))
|
31
|
31
|
"flet binds a function to a name within a lexical environment"
|
32
|
32
|
(flet ((some-named-function (a b) (* a b)))
|
33
|
|
- (assert-equal ___ (some-named-function 7 11)))
|
34
|
|
- (assert-equal ___ (some-named-function 7 11)))
|
|
33
|
+ (assert-equal 77 (some-named-function 7 11)))
|
|
34
|
+ (assert-equal 18 (some-named-function 7 11)))
|
35
|
35
|
|
36
|
36
|
|
37
|
37
|
; borrowed from Common Lisp The Language chapter 5.2.2
|
|
@@ -41,9 +41,9 @@
|
41
|
41
|
|
42
|
42
|
(define-test test-optional-parameters
|
43
|
43
|
"Optional parameters are filled in with their default value."
|
44
|
|
- (assert-equal (func-with-opt-params :test-1 :test-2) ___)
|
45
|
|
- (assert-equal (func-with-opt-params :test-1) ___)
|
46
|
|
- (assert-equal (func-with-opt-params) ___))
|
|
44
|
+ (assert-equal (func-with-opt-params :test-1 :test-2) '(:test-1 :test-2))
|
|
45
|
+ (assert-equal (func-with-opt-params :test-1) '(:test-1 3))
|
|
46
|
+ (assert-equal (func-with-opt-params) '(2 3)))
|
47
|
47
|
|
48
|
48
|
|
49
|
49
|
;; ----
|
|
@@ -56,9 +56,9 @@
|
56
|
56
|
"Common Lisp optional params may bind a symbol which indicate whether the
|
57
|
57
|
value was provided or defaulted. Each optional parameter binding has the
|
58
|
58
|
form (var default-form supplied-p)."
|
59
|
|
- (assert-equal (func-with-opt-params-and-indication :test-1 :test-2) ___)
|
60
|
|
- (assert-equal (func-with-opt-params-and-indication :test-1) ___)
|
61
|
|
- (assert-equal (func-with-opt-params-and-indication) ___))
|
|
59
|
+ (assert-equal (func-with-opt-params-and-indication :test-1 :test-2) '(:test-1 t :test-2 t))
|
|
60
|
+ (assert-equal (func-with-opt-params-and-indication :test-1) '(:test-1 t 3 nil))
|
|
61
|
+ (assert-equal (func-with-opt-params-and-indication) '(2 nil 3 nil)))
|
62
|
62
|
|
63
|
63
|
|
64
|
64
|
;; ----
|
|
@@ -70,9 +70,9 @@
|
70
|
70
|
(define-test test-func-with-rest-params
|
71
|
71
|
"With &rest, the remaining params, are handed in as a list. Remaining
|
72
|
72
|
arguments (possibly none) are collected into a list."
|
73
|
|
- (assert-equal (func-with-rest-params) ___)
|
74
|
|
- (assert-equal (func-with-rest-params 1) ___)
|
75
|
|
- (assert-equal (func-with-rest-params 1 :two 333) ___))
|
|
73
|
+ (assert-equal (func-with-rest-params) nil)
|
|
74
|
+ (assert-equal (func-with-rest-params 1) '(1))
|
|
75
|
+ (assert-equal (func-with-rest-params 1 :two 333) '(1 :two 333)))
|
76
|
76
|
|
77
|
77
|
|
78
|
78
|
;; ----
|
|
@@ -83,24 +83,24 @@
|
83
|
83
|
|
84
|
84
|
(define-test test-key-params ()
|
85
|
85
|
"Key params allow the user to specify params in any order"
|
86
|
|
- (assert-equal (func-with-key-params) ___)
|
87
|
|
- (assert-equal (func-with-key-params :a 11 :b 22) ___)
|
|
86
|
+ (assert-equal (func-with-key-params) '(nil nil))
|
|
87
|
+ (assert-equal (func-with-key-params :a 11 :b 22) '(11 22))
|
88
|
88
|
; it is not necessary to specify all key parameters
|
89
|
|
- (assert-equal (func-with-key-params :b 22) ___)
|
|
89
|
+ (assert-equal (func-with-key-params :b 22) '(nil 22))
|
90
|
90
|
; order is not important
|
91
|
|
- (assert-equal (func-with-key-params :b 22 :a 0) ___))
|
|
91
|
+ (assert-equal (func-with-key-params :b 22 :a 0) '(0 22)))
|
92
|
92
|
|
93
|
93
|
(defun func-key-params-can-have-defaults (&key (a 3 a?) (b 4 b?))
|
94
|
94
|
(list a a? b b?))
|
95
|
95
|
|
96
|
96
|
(define-test test-key-params-can-have-defaults
|
97
|
97
|
"key parameters can have defaults also"
|
98
|
|
- (assert-equal (func-key-params-can-have-defaults) ____)
|
99
|
|
- (assert-equal (func-key-params-can-have-defaults :a 3 :b 4) ___)
|
100
|
|
- (assert-equal (func-key-params-can-have-defaults :a 11 :b 22) ___)
|
101
|
|
- (assert-equal (func-key-params-can-have-defaults :b 22) ___)
|
|
98
|
+ (assert-equal (func-key-params-can-have-defaults) '(3 nil 4 nil))
|
|
99
|
+ (assert-equal (func-key-params-can-have-defaults :a 3 :b 4) '(3 t 4 t))
|
|
100
|
+ (assert-equal (func-key-params-can-have-defaults :a 11 :b 22) '(11 t 22 t))
|
|
101
|
+ (assert-equal (func-key-params-can-have-defaults :b 22) '(3 nil 22 t))
|
102
|
102
|
; order is not important
|
103
|
|
- (assert-equal (func-key-params-can-have-defaults :b 22 :a 0) ___))
|
|
103
|
+ (assert-equal (func-key-params-can-have-defaults :b 22 :a 0) '(0 t 22 t)))
|
104
|
104
|
|
105
|
105
|
|
106
|
106
|
;; ----
|
|
@@ -112,10 +112,10 @@
|
112
|
112
|
|
113
|
113
|
(define-test test-many-kinds-params
|
114
|
114
|
"CL provides the programmer with more than enough rope to hang himself."
|
115
|
|
- (assert-equal (func-with-funky-parameters 1) ___)
|
116
|
|
- (assert-equal (func-with-funky-parameters 1 :b 2) ___)
|
117
|
|
- (assert-equal (func-with-funky-parameters 1 :b 2 :c 3) ___)
|
118
|
|
- (assert-equal (func-with-funky-parameters 1 :c 3 :b 2) ___))
|
|
115
|
+ (assert-equal (func-with-funky-parameters 1) '(1 nil 1 nil))
|
|
116
|
+ (assert-equal (func-with-funky-parameters 1 :b 2) '(1 2 1 (:b 2)))
|
|
117
|
+ (assert-equal (func-with-funky-parameters 1 :b 2 :c 3) '(1 2 3 (:b 2 :c 3)))
|
|
118
|
+ (assert-equal (func-with-funky-parameters 1 :c 3 :b 2) '(1 2 3 (:c 3 :b 2))))
|
119
|
119
|
|
120
|
120
|
|
121
|
121
|
;; Note that &rest parameters have to come before &key parameters.
|
|
@@ -129,16 +129,16 @@
|
129
|
129
|
(assert-equal 19 ((lambda (a b) (+ a b)) 10 9))
|
130
|
130
|
(let ((my-function))
|
131
|
131
|
(setf my-function (lambda (a b) (* a b)))
|
132
|
|
- (assert-equal ___ (funcall my-function 11 9)))
|
|
132
|
+ (assert-equal 99 (funcall my-function 11 9)))
|
133
|
133
|
(let ((list-of-functions nil))
|
134
|
134
|
(push (lambda (a b) (+ a b)) list-of-functions)
|
135
|
135
|
(push (lambda (a b) (* a b)) list-of-functions)
|
136
|
136
|
(push (lambda (a b) (- a b)) list-of-functions)
|
137
|
|
- (assert-equal ___ (funcall (second list-of-functions) 2 33))))
|
|
137
|
+ (assert-equal 66 (funcall (second list-of-functions) 2 33))))
|
138
|
138
|
|
139
|
139
|
(define-test test-lambdas-can-have-optional-params
|
140
|
|
- (assert-equal ___ ((lambda (a &optional (b 100)) (+ a b)) 10 9))
|
141
|
|
- (assert-equal ___ ((lambda (a &optional (b 100)) (+ a b)) 10)))
|
|
140
|
+ (assert-equal 19 ((lambda (a &optional (b 100)) (+ a b)) 10 9))
|
|
141
|
+ (assert-equal 110 ((lambda (a &optional (b 100)) (+ a b)) 10)))
|
142
|
142
|
|
143
|
143
|
|
144
|
144
|
; returns sign x
|
|
@@ -148,9 +148,9 @@
|
148
|
148
|
1)
|
149
|
149
|
|
150
|
150
|
(define-test test-return-from-function-early
|
151
|
|
- (assert-equal (sign-of -5.5) ___)
|
152
|
|
- (assert-equal (sign-of 0) ___)
|
153
|
|
- (assert-equal (sign-of ___) 1))
|
|
151
|
+ (assert-equal (sign-of -5.5) -1)
|
|
152
|
+ (assert-equal (sign-of 0) 0)
|
|
153
|
+ (assert-equal (sign-of 201) 1))
|
154
|
154
|
|
155
|
155
|
|
156
|
156
|
;; ----
|
|
@@ -170,8 +170,8 @@
|
170
|
170
|
(let ((add-100 (adder 100))
|
171
|
171
|
(add-500 (adder 500)))
|
172
|
172
|
"add-100 and add-500 now refer to different bindings to x"
|
173
|
|
- (assert-equal ___ (funcall add-100 3))
|
174
|
|
- (assert-equal ___ (funcall add-500 3))))
|
|
173
|
+ (assert-equal 103 (funcall add-100 3))
|
|
174
|
+ (assert-equal 503 (funcall add-500 3))))
|
175
|
175
|
|
176
|
176
|
|
177
|
177
|
;; ----
|
|
@@ -192,13 +192,13 @@
|
192
|
192
|
"An illustration of how lexical closures may interact."
|
193
|
193
|
(let ((tangled-funs-1 (two-funs 1))
|
194
|
194
|
(tangled-funs-2 (two-funs 2)))
|
195
|
|
- (assert-equal (funcall (first tangled-funs-1)) ___)
|
|
195
|
+ (assert-equal (funcall (first tangled-funs-1)) 1)
|
196
|
196
|
(funcall (second tangled-funs-1) 0)
|
197
|
|
- (assert-equal (funcall (first tangled-funs-1)) ___)
|
|
197
|
+ (assert-equal (funcall (first tangled-funs-1)) 0)
|
198
|
198
|
|
199
|
|
- (assert-equal (funcall (first tangled-funs-2)) ___)
|
|
199
|
+ (assert-equal (funcall (first tangled-funs-2)) 2)
|
200
|
200
|
(funcall (second tangled-funs-2) 100)
|
201
|
|
- (assert-equal (funcall (first tangled-funs-2)) ___)))
|
|
201
|
+ (assert-equal (funcall (first tangled-funs-2)) 100)))
|
202
|
202
|
|
203
|
203
|
|
204
|
204
|
(define-test test-apply-function-with-apply
|
|
@@ -209,13 +209,13 @@
|
209
|
209
|
(setq f2 '-)
|
210
|
210
|
(setq f3 'max)
|
211
|
211
|
|
212
|
|
- (assert-equal ___ (apply f1 '(1 2)))
|
213
|
|
- (assert-equal ___ (apply f2 '(1 2)))
|
|
212
|
+ (assert-equal 3 (apply f1 '(1 2)))
|
|
213
|
+ (assert-equal -1 (apply f2 '(1 2)))
|
214
|
214
|
|
215
|
215
|
; after the function name, the parameters are consed onto the front
|
216
|
216
|
; of the very last parameter
|
217
|
|
- (assert-equal ___ (apply f1 1 2 '(3)))
|
218
|
|
- (assert-equal ___ (apply f3 1 2 3 4 '()))))
|
|
217
|
+ (assert-equal 6 (apply f1 1 2 '(3)))
|
|
218
|
+ (assert-equal 4 (apply f3 1 2 3 4 '()))))
|
219
|
219
|
|
220
|
220
|
|
221
|
221
|
(define-test test-apply-function-with-funcall
|
|
@@ -225,7 +225,7 @@
|
225
|
225
|
(setq f1 '+)
|
226
|
226
|
(setq f2 '-)
|
227
|
227
|
(setq f3 'max)
|
228
|
|
- (assert-equal ___ (funcall f1 1 2))
|
229
|
|
- (assert-equal ___ (funcall f2 1 2))
|
230
|
|
- (assert-equal ___ (funcall f1 1 2 3))
|
231
|
|
- (assert-equal ___ (funcall f3 1 2 3 4))))
|
|
228
|
+ (assert-equal 3 (funcall f1 1 2))
|
|
229
|
+ (assert-equal -1 (funcall f2 1 2))
|
|
230
|
+ (assert-equal 6 (funcall f1 1 2 3))
|
|
231
|
+ (assert-equal 4 (funcall f3 1 2 3 4))))
|