3 (:export #:with-test #:report-test-status #:*failures*
4 #:really-invoke-debugger
5 #:*break-on-failure* #:*break-on-expected-failure*
6 #:make-kill-thread #:make-join-thread
9 (in-package :test-util)
11 (defvar *test-count* 0)
12 (defvar *test-file* nil)
13 (defvar *failures* nil)
14 (defvar *break-on-failure* nil)
15 (defvar *break-on-expected-failure* nil)
17 (defvar *threads-to-kill*)
18 (defvar *threads-to-join*)
20 (eval-when (:compile-toplevel :load-toplevel :execute)
23 ;;; run-program on Windows doesn't have an :environment parameter,
24 ;;; set these globally
25 (sb-posix:putenv (format nil "SBCL_MACHINE_TYPE=~A" (machine-type)))
26 (sb-posix:putenv (format nil "SBCL_SOFTWARE_TYPE=~A" (software-type)))
29 (defun make-kill-thread (&rest args)
30 (let ((thread (apply #'sb-thread:make-thread args)))
31 (when (boundp '*threads-to-kill*)
32 (push thread *threads-to-kill*))
36 (defun make-join-thread (&rest args)
37 (let ((thread (apply #'sb-thread:make-thread args)))
38 (when (boundp '*threads-to-join*)
39 (push thread *threads-to-join*))
42 (defun log-msg (&rest args)
43 (format *trace-output* "~&::: ")
44 (apply #'format *trace-output* args)
45 (terpri *trace-output*)
46 (force-output *trace-output*))
48 (defmacro with-test ((&key fails-on broken-on skipped-on name)
50 (let ((block-name (gensym))
51 #+sb-thread (threads (gensym "THREADS")))
55 ((broken-p ,broken-on)
56 (fail-test :skipped-broken ',name "Test broken on this platform"))
57 ((skipped-p ,skipped-on)
58 (fail-test :skipped-disabled ',name "Test disabled for this combination of platform and features"))
60 (let (#+sb-thread (,threads (sb-thread:list-all-threads))
61 (*threads-to-join* nil)
62 (*threads-to-kill* nil))
64 (handler-bind ((error (lambda (error)
65 (if (expected-failure-p ,fails-on)
66 (fail-test :expected-failure ',name error)
67 (fail-test :unexpected-failure ',name error))
68 (return-from ,block-name))))
70 (log-msg "Running ~S" ',name)
73 (let ((any-leftover nil))
74 (dolist (thread *threads-to-join*)
75 (ignore-errors (sb-thread:join-thread thread)))
76 (dolist (thread *threads-to-kill*)
77 (ignore-errors (sb-thread:terminate-thread thread)))
78 (setf ,threads (union (union *threads-to-kill*
81 #+(and sb-safepoint-strictly (not win32))
82 (dolist (thread (sb-thread:list-all-threads))
83 (when (typep thread 'sb-thread:signal-handling-thread)
84 (ignore-errors (sb-thread:join-thread thread))))
85 (dolist (thread (sb-thread:list-all-threads))
86 (unless (or (not (sb-thread:thread-alive-p thread))
87 (eql thread sb-thread:*current-thread*)
88 (member thread ,threads)
89 (sb-thread:thread-emphemeral-p thread))
90 (setf any-leftover thread)
91 (ignore-errors (sb-thread:terminate-thread thread))))
93 (fail-test :leftover-thread ',name any-leftover)
94 (return-from ,block-name)))
95 (if (expected-failure-p ,fails-on)
96 (fail-test :unexpected-success ',name nil)
97 (log-msg "Success ~S" ',name)))))))))))
99 (defun report-test-status ()
100 (with-standard-io-syntax
101 (with-open-file (stream "test-status.lisp-expr"
103 :if-exists :supersede)
104 (format stream "~s~%" *failures*))))
107 (unless (eq *test-file* *load-pathname*)
108 (setf *test-file* *load-pathname*)
109 (setf *test-count* 0))
112 (defun really-invoke-debugger (condition)
113 (with-simple-restart (continue "Continue")
114 (let ((*invoke-debugger-hook* *invoke-debugger-hook*))
116 (invoke-debugger condition))))
118 (defun fail-test (type test-name condition)
119 (if (stringp condition)
120 (log-msg "~@<~A ~S ~:_~A~:>"
121 type test-name condition)
122 (log-msg "~@<~A ~S ~:_due to ~S: ~4I~:_\"~A\"~:>"
123 type test-name condition condition))
124 (push (list type *test-file* (or test-name *test-count*))
126 (unless (stringp condition)
127 (when (or (and *break-on-failure*
128 (not (eq type :expected-failure)))
129 *break-on-expected-failure*)
130 (really-invoke-debugger condition))))
132 (defun expected-failure-p (fails-on)
133 (sb-impl::featurep fails-on))
135 (defun broken-p (broken-on)
136 (sb-impl::featurep broken-on))
138 (defun skipped-p (skipped-on)
139 (sb-impl::featurep skipped-on))
141 ;;; Repeat calling THUNK until its cumulated runtime, measured using
142 ;;; GET-INTERNAL-RUN-TIME, is larger than PRECISION. Repeat this
143 ;;; REPETITIONS many times and return the time one call to THUNK took
144 ;;; in seconds as a float, according to the minimum of the cumulated
145 ;;; runtimes over the repetitions.
146 ;;; This allows to easily measure the runtime of expressions that take
147 ;;; much less time than one internal time unit. Also, the results are
148 ;;; unaffected, modulo quantization effects, by changes to
149 ;;; INTERNAL-TIME-UNITS-PER-SECOND.
150 ;;; Taking the minimum is intended to reduce the error introduced by
151 ;;; garbage collections occurring at unpredictable times. The inner
152 ;;; loop doubles the number of calls to THUNK each time before again
153 ;;; measuring the time spent, so that the time measurement overhead
154 ;;; doesn't distort the result if calling THUNK takes very little time.
155 (defun runtime* (thunk repetitions precision)
156 (loop repeat repetitions
158 (loop with start = (get-internal-run-time)
160 for n = 1 then (* n 2)
161 for total-runs = n then (+ total-runs n)
164 (setf duration (- (get-internal-run-time) start))
165 when (> duration precision)
166 return (/ (float duration) (float total-runs)))
167 into min-internal-time-units-per-call
168 finally (return (/ min-internal-time-units-per-call
169 (float internal-time-units-per-second)))))
171 (defmacro runtime (form &key (repetitions 3) (precision 10))
172 `(runtime* (lambda () ,form) ,repetitions ,precision))