Fix thread-alloca test on Windows.
[sbcl.git] / tests / test-util.lisp
1 (defpackage :test-util
2   (:use :cl :sb-ext)
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
7            #:runtime))
8
9 (in-package :test-util)
10
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)
16
17 (defvar *threads-to-kill*)
18 (defvar *threads-to-join*)
19
20 (eval-when (:compile-toplevel :load-toplevel :execute)
21   (require :sb-posix))
22
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)))
27
28 #+sb-thread
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*))
33     thread))
34
35 #+sb-thread
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*))
40     thread))
41
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*))
47
48 (defmacro with-test ((&key fails-on broken-on skipped-on name)
49                      &body body)
50   (let ((block-name (gensym))
51         #+sb-thread (threads (gensym "THREADS")))
52     `(progn
53        (start-test)
54        (cond
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"))
59          (t
60           (let (#+sb-thread (,threads (sb-thread:list-all-threads))
61                 (*threads-to-join* nil)
62                 (*threads-to-kill* nil))
63             (block ,block-name
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))))
69                 (progn
70                   (log-msg "Running ~S" ',name)
71                   ,@body
72                   #+sb-thread
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*
79                                                  *threads-to-join*)
80                                           ,threads))
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))))
92                     (when any-leftover
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)))))))))))
98
99 (defun report-test-status ()
100   (with-standard-io-syntax
101       (with-open-file (stream "test-status.lisp-expr"
102                               :direction :output
103                               :if-exists :supersede)
104         (format stream "~s~%" *failures*))))
105
106 (defun start-test ()
107   (unless (eq *test-file* *load-pathname*)
108     (setf *test-file* *load-pathname*)
109     (setf *test-count* 0))
110   (incf *test-count*))
111
112 (defun really-invoke-debugger (condition)
113   (with-simple-restart (continue "Continue")
114     (let ((*invoke-debugger-hook* *invoke-debugger-hook*))
115       (enable-debugger)
116       (invoke-debugger condition))))
117
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*))
125         *failures*)
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))))
131
132 (defun expected-failure-p (fails-on)
133   (sb-impl::featurep fails-on))
134
135 (defun broken-p (broken-on)
136   (sb-impl::featurep broken-on))
137
138 (defun skipped-p (skipped-on)
139   (sb-impl::featurep skipped-on))
140
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
157         minimize
158         (loop with start = (get-internal-run-time)
159               with duration = 0
160               for n = 1 then (* n 2)
161               for total-runs = n then (+ total-runs n)
162               do (dotimes (i n)
163                    (funcall thunk))
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)))))
170
171 (defmacro runtime (form &key (repetitions 3) (precision 10))
172   `(runtime* (lambda () ,form) ,repetitions ,precision))