| 1 |
#|----------------------------------------------------------------------------| |
| 2 |
| Copyright 1990 by the Massachusetts Institute of Technology, Cambridge MA. | |
| 3 |
| | |
| 4 |
| Permission to use, copy, modify, and distribute this software and its | |
| 5 |
| documentation for any purpose and without fee is hereby granted, provided | |
| 6 |
| that this copyright and permission notice appear in all copies and | |
| 7 |
| supporting documentation, and that the name of M.I.T. not be used in | |
| 8 |
| advertising or publicity pertaining to distribution of the software | |
| 9 |
| without specific, written prior permission. M.I.T. makes no | |
| 10 |
| representations about the suitability of this software for any purpose. | |
| 11 |
| It is provided "as is" without express or implied warranty. | |
| 12 |
| | |
| 13 |
| M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING | |
| 14 |
| ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL | |
| 15 |
| M.I.T. BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR | |
| 16 |
| ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, | |
| 17 |
| WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, | |
| 18 |
| ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | |
| 19 |
| SOFTWARE. | |
| 20 |
|----------------------------------------------------------------------------|# |
| 21 |
|
| 22 |
(defpackage :sb-rt |
| 23 |
(:nicknames :rt :regression-test :rtest) |
| 24 |
(:use #:cl) |
| 25 |
(:export #:*do-tests-when-defined* #:*test* #:continue-testing |
| 26 |
#:deftest #:deftest* #:do-test #:do-tests #:get-test |
| 27 |
#:pending-tests #:rem-all-tests #:rem-test) |
| 28 |
(:documentation "The MIT regression tester")) |
| 29 |
|
| 30 |
(in-package :sb-rt) |
| 31 |
|
| 32 |
(defvar *test* nil "Current test name") |
| 33 |
(defvar *do-tests-when-defined* nil) |
| 34 |
(defvar *entries* '(nil) "Test database") |
| 35 |
(defvar *in-test* nil "Used by TEST") |
| 36 |
(defvar *debug* nil "For debugging") |
| 37 |
(defvar *catch-errors* t |
| 38 |
"When true, causes errors in a test to be caught.") |
| 39 |
(defvar *print-circle-on-failure* nil |
| 40 |
"Failure reports are printed with *PRINT-CIRCLE* bound to this value.") |
| 41 |
(defvar *compile-tests* nil |
| 42 |
"When true, compile the tests before running them.") |
| 43 |
(defvar *optimization-settings* '((safety 3))) |
| 44 |
(defvar *expected-failures* nil |
| 45 |
"A list of test names that are expected to fail.") |
| 46 |
|
| 47 |
(defstruct (entry (:conc-name nil) |
| 48 |
(:type list)) |
| 49 |
pend name form) |
| 50 |
|
| 51 |
(defmacro vals (entry) `(cdddr ,entry)) |
| 52 |
|
| 53 |
(defmacro defn (entry) `(cdr ,entry)) |
| 54 |
|
| 55 |
(defun pending-tests () |
| 56 |
(do ((l (cdr *entries*) (cdr l)) |
| 57 |
(r nil)) |
| 58 |
((null l) (nreverse r)) |
| 59 |
(when (pend (car l)) |
| 60 |
(push (name (car l)) r)))) |
| 61 |
|
| 62 |
(defun rem-all-tests () |
| 63 |
(setq *entries* (list nil)) |
| 64 |
nil) |
| 65 |
|
| 66 |
(defun rem-test (&optional (name *test*)) |
| 67 |
(do ((l *entries* (cdr l))) |
| 68 |
((null (cdr l)) nil) |
| 69 |
(when (equal (name (cadr l)) name) |
| 70 |
(setf (cdr l) (cddr l)) |
| 71 |
(return name)))) |
| 72 |
|
| 73 |
(defun get-test (&optional (name *test*)) |
| 74 |
(defn (get-entry name))) |
| 75 |
|
| 76 |
(defun get-entry (name) |
| 77 |
(let ((entry (find name (cdr *entries*) |
| 78 |
:key #'name |
| 79 |
:test #'equal))) |
| 80 |
(when (null entry) |
| 81 |
(report-error t |
| 82 |
"~%No test with name ~:@(~S~)." |
| 83 |
name)) |
| 84 |
entry)) |
| 85 |
|
| 86 |
(defmacro deftest (name form &rest values) |
| 87 |
`(add-entry '(t ,name ,form .,values))) |
| 88 |
|
| 89 |
(defmacro deftest* (name form result) |
| 90 |
`(deftest ,name ,form ,@(eval result))) |
| 91 |
|
| 92 |
(defun add-entry (entry) |
| 93 |
(setq entry (copy-list entry)) |
| 94 |
(do ((l *entries* (cdr l))) (nil) |
| 95 |
(when (null (cdr l)) |
| 96 |
(setf (cdr l) (list entry)) |
| 97 |
(return nil)) |
| 98 |
(when (equal (name (cadr l)) |
| 99 |
(name entry)) |
| 100 |
(setf (cadr l) entry) |
| 101 |
(report-error nil |
| 102 |
"Redefining test ~:@(~S~)" |
| 103 |
(name entry)) |
| 104 |
(return nil))) |
| 105 |
(when *do-tests-when-defined* |
| 106 |
(do-entry entry)) |
| 107 |
(setq *test* (name entry))) |
| 108 |
|
| 109 |
(defun report-error (error? &rest args) |
| 110 |
(cond (*debug* |
| 111 |
(apply #'format t args) |
| 112 |
(if error? (throw '*debug* nil))) |
| 113 |
(error? (apply #'error args)) |
| 114 |
(t (apply #'format t args)))) |
| 115 |
|
| 116 |
(defun do-test (&optional (name *test*)) |
| 117 |
(do-entry (get-entry name))) |
| 118 |
|
| 119 |
(defun equalp-with-case (x y) |
| 120 |
"Like EQUALP, but doesn't do case conversion of characters." |
| 121 |
(cond |
| 122 |
((eq x y) t) |
| 123 |
((consp x) |
| 124 |
(and (consp y) |
| 125 |
(equalp-with-case (car x) (car y)) |
| 126 |
(equalp-with-case (cdr x) (cdr y)))) |
| 127 |
((and (typep x 'array) |
| 128 |
(= (array-rank x) 0)) |
| 129 |
(equalp-with-case (aref x) (aref y))) |
| 130 |
((typep x 'vector) |
| 131 |
(and (typep y 'vector) |
| 132 |
(let ((x-len (length x)) |
| 133 |
(y-len (length y))) |
| 134 |
(and (eql x-len y-len) |
| 135 |
(loop |
| 136 |
for e1 across x |
| 137 |
for e2 across y |
| 138 |
always (equalp-with-case e1 e2)))))) |
| 139 |
((and (typep x 'array) |
| 140 |
(typep y 'array) |
| 141 |
(not (equal (array-dimensions x) |
| 142 |
(array-dimensions y)))) |
| 143 |
nil) |
| 144 |
((typep x 'array) |
| 145 |
(and (typep y 'array) |
| 146 |
(let ((size (array-total-size x))) |
| 147 |
(loop for i from 0 below size |
| 148 |
always (equalp-with-case (row-major-aref x i) |
| 149 |
(row-major-aref y i)))))) |
| 150 |
(t (eql x y)))) |
| 151 |
|
| 152 |
(defun do-entry (entry &optional |
| 153 |
(s *standard-output*)) |
| 154 |
(catch '*in-test* |
| 155 |
(setq *test* (name entry)) |
| 156 |
(setf (pend entry) t) |
| 157 |
(let* ((*in-test* t) |
| 158 |
;; (*break-on-warnings* t) |
| 159 |
(aborted nil) |
| 160 |
r) |
| 161 |
;; (declare (special *break-on-warnings*)) |
| 162 |
|
| 163 |
(block aborted |
| 164 |
(setf r |
| 165 |
(flet ((%do |
| 166 |
() |
| 167 |
(if *compile-tests* |
| 168 |
(multiple-value-list |
| 169 |
(funcall (compile |
| 170 |
nil |
| 171 |
`(lambda () |
| 172 |
(declare |
| 173 |
(optimize ,@*optimization-settings*)) |
| 174 |
,(form entry))))) |
| 175 |
(multiple-value-list |
| 176 |
(eval (form entry)))))) |
| 177 |
(if *catch-errors* |
| 178 |
(handler-bind |
| 179 |
((style-warning #'muffle-warning) |
| 180 |
(error #'(lambda (c) |
| 181 |
(setf aborted t) |
| 182 |
(setf r (list c)) |
| 183 |
(return-from aborted nil)))) |
| 184 |
(%do)) |
| 185 |
(%do))))) |
| 186 |
|
| 187 |
(setf (pend entry) |
| 188 |
(or aborted |
| 189 |
(not (equalp-with-case r (vals entry))))) |
| 190 |
|
| 191 |
(when (pend entry) |
| 192 |
(let ((*print-circle* *print-circle-on-failure*)) |
| 193 |
(format s "~&Test ~:@(~S~) failed~ |
| 194 |
~%Form: ~S~ |
| 195 |
~%Expected value~P: ~ |
| 196 |
~{~S~^~%~17t~}~%" |
| 197 |
*test* (form entry) |
| 198 |
(length (vals entry)) |
| 199 |
(vals entry)) |
| 200 |
(format s "Actual value~P: ~ |
| 201 |
~{~S~^~%~15t~}.~%" |
| 202 |
(length r) r) |
| 203 |
(let ((x (first r))) |
| 204 |
(when (typep x 'condition) |
| 205 |
(format s "~&Condition: ~A" x))) |
| 206 |
)))) |
| 207 |
(when (not (pend entry)) *test*)) |
| 208 |
|
| 209 |
(defun continue-testing () |
| 210 |
(if *in-test* |
| 211 |
(throw '*in-test* nil) |
| 212 |
(do-entries *standard-output*))) |
| 213 |
|
| 214 |
(defun do-tests (&optional |
| 215 |
(out *standard-output*)) |
| 216 |
(dolist (entry (cdr *entries*)) |
| 217 |
(setf (pend entry) t)) |
| 218 |
(if (streamp out) |
| 219 |
(do-entries out) |
| 220 |
(with-open-file |
| 221 |
(stream out :direction :output) |
| 222 |
(do-entries stream)))) |
| 223 |
|
| 224 |
(defun do-entries (s) |
| 225 |
(format s "~&Doing ~A pending test~:P ~ |
| 226 |
of ~A tests total.~%" |
| 227 |
(count t (cdr *entries*) |
| 228 |
:key #'pend) |
| 229 |
(length (cdr *entries*))) |
| 230 |
(dolist (entry (cdr *entries*)) |
| 231 |
(when (pend entry) |
| 232 |
(format s "~@[~<~%~:; ~:@(~S~)~>~]" |
| 233 |
(do-entry entry s)))) |
| 234 |
(let ((pending (pending-tests)) |
| 235 |
(expected-table (make-hash-table :test #'equal))) |
| 236 |
(dolist (ex *expected-failures*) |
| 237 |
(setf (gethash ex expected-table) t)) |
| 238 |
(let ((new-failures |
| 239 |
(loop for pend in pending |
| 240 |
unless (gethash pend expected-table) |
| 241 |
collect pend))) |
| 242 |
(if (null pending) |
| 243 |
(format s "~&No tests failed.") |
| 244 |
(progn |
| 245 |
(format s "~&~A out of ~A ~ |
| 246 |
total tests failed: ~ |
| 247 |
~:@(~{~<~% ~1:;~S~>~ |
| 248 |
~^, ~}~)." |
| 249 |
(length pending) |
| 250 |
(length (cdr *entries*)) |
| 251 |
pending) |
| 252 |
(if (null new-failures) |
| 253 |
(format s "~&No unexpected failures.") |
| 254 |
(when *expected-failures* |
| 255 |
(format s "~&~A unexpected failures: ~ |
| 256 |
~:@(~{~<~% ~1:;~S~>~ |
| 257 |
~^, ~}~)." |
| 258 |
(length new-failures) |
| 259 |
new-failures))) |
| 260 |
)) |
| 261 |
(finish-output s) |
| 262 |
(null pending)))) |
| 263 |
|
| 264 |
(provide '#:rt) |