1 ;;;-*- Mode: Lisp; Syntax: ANSI-Common-Lisp -*-
4 Copyright
(c) 2004-
2005 Christopher K. Riesbeck
6 Permission is hereby granted
, free of charge
, to any person obtaining
7 a copy of this software and associated documentation files
(the "Software"),
8 to deal in the Software without restriction
, including without limitation
9 the rights to use
, copy
, modify
, merge
, publish
, distribute
, sublicense
,
10 and
/or sell copies of the Software
, and to permit persons to whom the
11 Software is furnished to do so
, subject to the following conditions
:
13 The above copyright notice and this permission notice shall be included
14 in all copies or substantial portions of the Software.
16 THE SOFTWARE IS PROVIDED
"AS IS", WITHOUT WARRANTY OF ANY KIND
, EXPRESS
17 OR IMPLIED
, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY
,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM
, DAMAGES OR
20 OTHER LIABILITY
, WHETHER IN AN ACTION OF CONTRACT
, TORT OR OTHERWISE
,
21 ARISING FROM
, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 OTHER DEALINGS IN THE SOFTWARE.
26 ;;; A test suite package, modelled after JUnit.
27 ;;; Author: Chris Riesbeck
31 ;;; 04/07/06 added ~<...~> to remaining error output forms [CKR]
32 ;;; 04/06/06 added ~<...~> to compact error output better [CKR]
33 ;;; 04/06/06 fixed RUN-TESTS to get tests dynamically (bug reported
34 ;;; by Daniel Edward Burke) [CKR]
35 ;;; 02/08/06 added newlines to error output [CKR]
36 ;;; 12/30/05 renamed ASSERT-PREDICATE to ASSERT-EQUALITY [CKR]
37 ;;; 12/29/05 added ASSERT-EQ, ASSERT-EQL, ASSERT-EQUALP [CKR]
38 ;;; 12/22/05 recoded use-debugger to use handler-bind, added option to prompt for debugger,
39 ;;; 11/07/05 added *use-debugger* and assert-predicate [DFB]
40 ;;; 09/18/05 replaced Academic Free License with MIT Licence [CKR]
41 ;;; 08/30/05 added license notice [CKR]
42 ;;; 06/28/05 changed RUN-TESTS to compile code at run time, not expand time [CKR]
43 ;;; 02/21/05 removed length check from SET-EQUAL [CKR]
44 ;;; 02/17/05 added RUN-ALL-TESTS [CKR]
45 ;;; 01/18/05 added ASSERT-EQUAL back in [CKR]
46 ;;; 01/17/05 much clean up, added WITH-TEST-LISTENER [CKR]
47 ;;; 01/15/05 replaced ASSERT-EQUAL etc. with ASSERT-TRUE and ASSERT-FALSE [CKR]
48 ;;; 01/04/05 changed COLLECT-RESULTS to echo output on *STANDARD-OUTPuT* [CKR]
49 ;;; 01/04/05 added optional package argument to REMOVE-ALL-TESTS [CKR]
50 ;;; 01/04/05 changed OUTPUT-OK-P to trim spaces and returns [CKR]
51 ;;; 01/04/05 changed OUTPUT-OK-P to not check output except when asked to [CKR]
52 ;;; 12/03/04 merged REMOVE-TEST into REMOVE-TESTS [CKR]
53 ;;; 12/03/04 removed ability to pass forms to RUN-TESTS [CKR]
54 ;;; 12/03/04 refactored RUN-TESTS expansion into RUN-TEST-THUNKS [CKR]
55 ;;; 12/02/04 changed to group tests under packages [CKR]
56 ;;; 11/30/04 changed assertions to put expected value first, like JUnit [CKR]
57 ;;; 11/30/04 improved error handling and summarization [CKR]
58 ;;; 11/30/04 generalized RUN-TESTS, removed RUN-TEST [CKR]
59 ;;; 02/27/04 fixed ASSERT-PRINTS not ignoring value [CKR]
60 ;;; 02/07/04 fixed ASSERT-EXPANDS failure message [CKR]
61 ;;; 02/07/04 added ASSERT-NULL, ASSERT-NOT-NULL [CKR]
62 ;;; 01/31/04 added error handling and totalling to RUN-TESTS [CKR]
63 ;;; 01/31/04 made RUN-TEST/RUN-TESTS macros [CKR]
64 ;;; 01/29/04 fixed ASSERT-EXPANDS quote bug [CKR]
65 ;;; 01/28/04 major changes from BUG-FINDER to be more like JUnit [CKR]
72 1. Read the documentation in lisp-unit.html.
74 2. Make a file of DEFINE-TEST
's. See exercise-tests.lisp for many
75 examples. If you want
, start your test file with
(REMOVE-TESTS) to
76 clear any previously defined tests.
80 2.
(use-package :lisp-unit
)
82 3. Load your code file and your file of tests.
84 4. Test your code with
(RUN-TESTS test-name1 test-name2 ...
) -- no quotes
! --
85 or simply
(RUN-TESTS) to run all defined tests.
87 A summary of how many tests passed and failed will be printed
,
88 with details on the failures.
90 Note
: Nothing is compiled until RUN-TESTS is expanded. Redefining
91 functions or even macros does not require reloading any tests.
93 For more information
, see lisp-unit.html.
97 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
99 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
101 (cl:defpackage
#:lisp-unit
103 (:export
#:define-test
#:run-all-tests
#:run-tests
104 #:assert-eq
#:assert-eql
#:assert-equal
#:assert-equalp
105 #:assert-error
#:assert-expands
#:assert-false
106 #:assert-equality
#:assert-prints
#:assert-true
107 #:get-test-code
#:get-tests
108 #:remove-all-tests
#:remove-tests
109 #:logically-equal
#:set-equal
110 #:float-equal
#:complex-equal
#:number-equal
113 #:with-test-listener
)
116 (in-package #:lisp-unit
)
118 (pushnew :lisp-unit
*features
*)
120 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
122 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
124 (defparameter *test-listener
* nil
)
126 (defparameter *tests
* (make-hash-table))
128 ;;; Used by RUN-TESTS to collect summary statistics
129 (defvar *test-count
* 0)
130 (defvar *pass-count
* 0)
132 ;;; Set by RUN-TESTS for use by SHOW-FAILURE
133 (defvar *test-name
* nil
)
135 ;;; If nil, errors in tests are caught and counted.
136 ;;; If :ask, user is given option of entering debugger or not.
137 ;;; If true and not :ask, debugger is entered.
138 (defparameter *use-debugger
* nil
)
140 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
142 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
146 (defmacro define-test
(name &body body
)
148 (store-test-code ',name
',body
)
153 (defmacro assert-eq
(expected form
&rest extras
)
154 (expand-assert :equal form form expected extras
:test
#'eq
))
156 (defmacro assert-eql
(expected form
&rest extras
)
157 (expand-assert :equal form form expected extras
:test
#'eql
))
159 (defmacro assert-equal
(expected form
&rest extras
)
160 (expand-assert :equal form form expected extras
:test
#'equal
))
162 (defmacro assert-equalp
(expected form
&rest extras
)
163 (expand-assert :equal form form expected extras
:test
#'equalp
))
165 (defmacro assert-error
(condition form
&rest extras
)
166 (expand-assert :error form
(expand-error-form form
)
169 (defmacro assert-expands
(&environment env expansion form
&rest extras
)
170 (expand-assert :macro form
171 (expand-macro-form form
#+lispworks nil
#-lispworks env
)
174 (defmacro assert-false
(form &rest extras
)
175 (expand-assert :result form form nil extras
))
177 (defmacro assert-equality
(test expected form
&rest extras
)
178 (expand-assert :equal form form expected extras
:test test
))
180 (defmacro assert-prints
(output form
&rest extras
)
181 (expand-assert :output form
(expand-output-form form
)
184 (defmacro assert-true
(form &rest extras
)
185 (expand-assert :result form form t extras
))
188 (defun expand-assert (type form body expected extras
&key
(test #'eql
))
190 ,type
',form
#'(lambda () ,body
) #'(lambda () ,expected
) ,(expand-extras extras
), test
))
192 (defun expand-error-form (form)
194 (condition (error) error
)))
196 (defun expand-output-form (form)
197 (let ((out (gensym)))
198 `(let* ((,out
(make-string-output-stream))
199 (*standard-output
* (make-broadcast-stream *standard-output
* ,out
)))
201 (get-output-stream-string ,out
))))
203 (defun expand-macro-form (form env
)
204 `(macroexpand-1 ',form
,env
))
206 (defun expand-extras (extras)
208 (list ,@(mapcan #'(lambda (form) (list `',form form
)) extras
))))
213 (defmacro run-all-tests
(package &rest tests
)
214 `(let ((*package
* (find-package ',package
)))
216 ,@(mapcar #'(lambda (test) (find-symbol (symbol-name test
) package
))
219 (defmacro run-tests
(&rest names
)
220 `(run-test-thunks (get-test-thunks ,(if (null names
) '(get-tests *package
*) `',names
))))
222 (defun get-test-thunks (names &optional
(package *package
*))
223 (mapcar #'(lambda (name) (get-test-thunk name package
))
226 (defun get-test-thunk (name package
)
227 (assert (get-test-code name package
) (name package
)
228 "No test defined for ~S in package ~S" name package
)
229 (list name
(coerce `(lambda () ,@(get-test-code name
)) 'function
)))
231 (defun use-debugger (&optional
(flag t
))
232 (setq *use-debugger
* flag
))
234 ;;; WITH-TEST-LISTENER
235 (defmacro with-test-listener
(listener &body body
)
236 `(let ((*test-listener
* #',listener
)) ,@body
))
238 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
240 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
242 (defun get-test-code (name &optional
(package *package
*))
243 (let ((table (get-package-table package
)))
245 (gethash name table
))))
247 (defun get-tests (&optional
(package *package
*))
249 (table (get-package-table package
)))
250 (cond ((null table
) nil
)
252 (maphash #'(lambda (key val
)
253 (declare (ignore val
))
256 (sort l
#'string
< :key
#'string
)))))
259 (defun remove-tests (names &optional
(package *package
*))
260 (let ((table (get-package-table package
)))
265 (remhash name table
))))))
267 (defun remove-all-tests (&optional
(package *package
*))
270 (remhash (find-package package
) *tests
*)))
273 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
274 ;;; Private functions
275 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
278 ;;; DEFINE-TEST support
280 (defun get-package-table (package &key create
)
281 (let ((table (gethash (find-package package
) *tests
*)))
284 (setf (gethash package
*tests
*)
285 (make-hash-table))))))
287 (defun get-test-name (form)
288 (if (atom form
) form
(cadr form
)))
290 (defun store-test-code (name code
&optional
(package *package
*))
292 (get-package-table package
:create t
))
296 ;;; ASSERTION support
298 (defun internal-assert (type form code-thunk expected-thunk extras test
)
299 (let* ((expected (multiple-value-list (funcall expected-thunk
)))
300 (actual (multiple-value-list (funcall code-thunk
)))
301 (passed (test-passed-p type expected actual test
)))
307 (record-result passed type form expected actual extras
)
311 (defun record-result (passed type form expected actual extras
)
312 (funcall (or *test-listener
* 'default-listener
)
313 passed type
*test-name
* form expected actual
314 (and extras
(funcall extras
))
315 *test-count
* *pass-count
*))
317 (defun default-listener
318 (passed type name form expected actual extras test-count pass-count
)
319 (declare (ignore test-count pass-count
))
321 (show-failure type
(get-failure-message type
)
322 name form expected actual extras
)))
324 (defun test-passed-p (type expected actual test
)
327 (or (eql (car actual
) (car expected
))
328 (typep (car actual
) (car expected
))))
330 (and (<= (length expected
) (length actual
))
331 (every test expected actual
)))
333 (equal (car actual
) (car expected
)))
335 (string= (string-trim '(#\newline
#\return
#\space
)
339 (logically-equal (car actual
) (car expected
)))
343 ;;; RUN-TESTS support
345 (defun run-test-thunks (test-thunks)
346 (unless (null test-thunks
)
347 (let ((total-test-count 0)
349 (total-error-count 0))
350 (dolist (test-thunk test-thunks
)
351 (multiple-value-bind (test-count pass-count error-count
)
352 (run-test-thunk (car test-thunk
) (cadr test-thunk
))
353 (incf total-test-count test-count
)
354 (incf total-pass-count pass-count
)
355 (incf total-error-count error-count
)))
356 (unless (null (cdr test-thunks
))
357 (show-summary 'total total-test-count total-pass-count total-error-count
))
360 (defun run-test-thunk (*test-name
* thunk
)
362 (format t
"~& Test ~S not found" *test-name
*)
363 (prog ((*test-count
* 0)
367 ((error #'(lambda (e)
368 (let ((*print-escape
* nil
))
370 (format t
"~& ~S: ~W" *test-name
* e
))
371 (if (use-debugger-p e
) e
(go exit
)))))
373 (show-summary *test-name
* *test-count
* *pass-count
*))
375 (return (values *test-count
* *pass-count
* error-count
)))))
377 (defun use-debugger-p (e)
379 (or (not (eql *use-debugger
* :ask
))
380 (y-or-n-p "~A -- debug?" e
))))
384 (defun get-failure-message (type)
386 (:error
"~&~@[Should have signalled ~{~S~^; ~} but saw~] ~{~S~^; ~}")
387 (:macro
"~&Should have expanded to ~{~S~^; ~} ~<~%~:;but saw ~{~S~^; ~}~>")
388 (:output
"~&Should have printed ~{~S~^; ~} ~<~%~:;but saw ~{~S~^; ~}~>")
389 (t "~&Expected ~{~S~^; ~} ~<~%~:;but saw ~{~S~^; ~}~>")
392 (defun show-failure (type msg name form expected actual extras
)
393 (format t
"~&~@[~S: ~]~S failed: " name form
)
394 (format t msg expected actual
)
395 (format t
"~{~& ~S => ~S~}~%" extras
)
398 (defun show-summary (name test-count pass-count
&optional error-count
)
399 (format t
"~&~A: ~S assertions passed, ~S failed~@[, ~S execution errors~]."
400 name pass-count
(- test-count pass-count
) error-count
))
402 (defun collect-form-values (form values
)
403 (mapcan #'(lambda (form-arg value
)
404 (if (constantp form-arg
)
406 (list form-arg value
)))
411 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
412 ;;; Useful equality predicates for tests
413 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
415 ;;; (LOGICALLY-EQUAL x y) => true or false
416 ;;; Return true if x and y both false or both true
418 (defun logically-equal (x y
)
419 (eql (not x
) (not y
)))
421 ;;; (SET-EQUAL l1 l2 :test) => true or false
422 ;;; Return true if every element of l1 is an element of l2
425 (defun set-equal (l1 l2
&key
(test #'equal
))
428 (subsetp l1 l2
:test test
)
429 (subsetp l2 l1
:test test
)))
431 ;;; (ROUNDOFF-ERROR x y) => number
432 ;;; Return the error delta between the exact and approximate floating
434 ;;; Equation 1.1 in NumLinAlg
435 (defun roundoff-error (exact approximate
)
436 "Returned the error delta between the exact and approximate floating
438 (abs (if (or (= 0.0 exact
) (= 0.0 approximate
))
439 (+ exact approximate
)
440 (- (/ approximate exact
) 1.0))))
442 ;;; (FLOAT-EQUAL float1 float2 &optional epsilon) => true or false
443 ;;; Return true if the absolute difference between float1 and float2
444 ;;; is less than epsilon. If an epsilon is not specified and either
445 ;;; float1 or float2 is single precision, the single-float-epsilon is
447 (defun float-equal (float1 float2
&optional epsilon
)
448 "Return true if the absolute difference between float1 and float2 is
449 less than some epsilon."
454 ((and (zerop float1
) (zerop float2
)))
456 (> epsilon
(roundoff-error float1 float2
)))
457 ((and (typep float1
'double-float
) (typep float2
'double-float
))
458 (> (* 2.0 double-float-epsilon
) (roundoff-error float1 float2
)))
459 ((or (typep float1
'single-float
) (typep float2
'single-float
))
460 (> (* 2.0 single-float-epsilon
) (roundoff-error float1 float2
)))
463 ;;; (COMPLEX-EQUAL complex1 complex2 &optional epsilon) => true or false
464 ;;; Return true if the absolute difference of the real components and
465 ;;; the absolute difference of the imaginary components is less then
466 ;;; epsilon. If an epsilon is not specified and either complex1 or
467 ;;; complex2 is (complex single-float), the single-float-epsilon is
469 (defun complex-equal (complex1 complex2
&optional epsilon
)
470 "Return true if the absolute difference between Re(complex1),
471 Re(complex2) and the absolute difference between Im(complex1),
472 Im(complex2) is less than epsilon."
474 (typep complex1
'(complex float
))
475 (typep complex2
'(complex float
))
476 (float-equal (realpart complex1
) (realpart complex2
) epsilon
)
477 (float-equal (imagpart complex1
) (imagpart complex2
) epsilon
)))
479 ;;; (NUMBER-EQUAL number1 number2) => true or false
480 ;;; Return true if the numbers are equal using the appropriate
482 (defun number-equal (number1 number2
&optional epsilon
)
483 "Return true if the numbers are equal using the appropriate
486 ((and (floatp number1
) (floatp number2
))
487 (float-equal number1 number2 epsilon
))
488 ((and (typep number1
'(complex float
)) (typep number2
'(complex float
)))
489 (complex-equal number1 number2 epsilon
))
490 ((and (numberp number1
) (numberp number2
))
492 (t (error "~A and ~A are not numbers." number1 number2
))))
494 ;;; (ELEMENT-EQUAL array1 array2 indice dimensions) => true or false
495 ;;; A utility function for ARRAY-EQUAL.
496 (defun element-equal (array1 array2 indices dimensions
&optional epsilon
)
497 "Return true if the index of array1 equals array2."
498 (let* ((rank (first dimensions
))
499 (remaining (rest dimensions
))
503 (element-equal array1 array2
504 (cons index indices
) remaining epsilon
))
506 (number-equal (apply #'aref array1 index
(reverse indices
))
507 (apply #'aref array2 index
(reverse indices
))
509 (do ((index 0 (1+ index
))
510 (result t
(funcall update-result index
)))
511 ((or (not result
) (>= index rank
)) result
))))
513 ;;; (DIMENSIONS-EQUAL array1 array2) => true or false
514 ;;; A utility function for ARRAY-EQUAL
515 (defun dimensions-equal (array1 array2
)
516 "Return trun if ARRAY1 and ARRAY2 are equal dimensions."
518 (= (array-rank array1
) (array-rank array2
))
519 (equal (array-dimensions array1
) (array-dimensions array2
))))
521 ;;; (ARRAY-EQUAL array1 array2) => true or false
522 ;;; Return true of the elements of the array are equal.
523 (defun array-equal (array1 array2
&optional epsilon
)
524 "Return true if the elements of the array are equal."
525 (when (dimensions-equal array1 array2
)
526 (element-equal array1 array2 nil
(array-dimensions array1
) epsilon
)))
529 ;;;; [NumLinAlg] James W. Demmel "Applied Numerical Linear Algebra",
530 ;;;; Society for Industrial and Applied Mathematics, 1997
531 ;;;; ISBN: 0-89871-389-7
533 (provide "lisp-unit")