Fix reporting of memory faults on Windows.
[sbcl.git] / tests / interface.pure.lisp
blobbd566e9110369e09913ad62f71e94da310688d79
1 ;;;; tests for problems in the interface presented to the user/programmer
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; While most of SBCL is derived from the CMU CL system, the test
7 ;;;; files (like this one) were written from scratch after the fork
8 ;;;; from CMU CL.
9 ;;;;
10 ;;;; This software is in the public domain and is provided with
11 ;;;; absolutely no warranty. See the COPYING and CREDITS files for
12 ;;;; more information.
14 (in-package :cl-user)
16 (load "test-util.lisp")
17 (load "compiler-test-util.lisp")
18 (use-package :test-util)
20 ;;;; properties of symbols, e.g. presence of doc strings for public symbols
22 (with-test (:name (documentation :cl) :skipped-on '(:not :sb-doc))
23 (let ((n 0))
24 (do-symbols (s 'cl)
25 (if (fboundp s)
26 (when (documentation s 'function)
27 (incf n))))
28 (assert (= n 594))))
30 ;;;; tests of interface machinery
32 ;;; APROPOS should accept a package designator, not just a package, and
33 ;;; furthermore do the right thing when it gets a package designator.
34 ;;; (bug reported and fixed by Alexey Dejneka sbcl-devel 2001-10-17)
35 (with-test (:name (apropos-list :package-designator))
36 (assert (< 0
37 (length (apropos-list "PRINT" :cl))
38 (length (apropos-list "PRINT")))))
39 ;;; Further, it should correctly deal with the external-only flag (bug
40 ;;; reported by cliini on #lisp IRC 2003-05-30, fixed in sbcl-0.8.0.1x
41 ;;; by CSR)
42 (with-test (:name (apropos-list :external-only))
43 (assert (= (length (apropos-list "" "CL"))
44 (length (apropos-list "" "CL" t))))
45 (assert (< 0
46 (length (apropos-list "" "SB-VM" t))
47 (length (apropos-list "" "SB-VM")))))
49 ;;; DESCRIBE shouldn't fail on rank-0 arrays (bug reported and fixed
50 ;;; by Lutz Euler sbcl-devel 2002-12-03)
51 (with-test (:name (describe array :rank 0))
52 (flet ((test (array)
53 (assert (plusp (length (with-output-to-string (stream)
54 (describe array stream)))))))
55 (test #0a0)
56 (test #(1 2 3))
57 (test #2a((1 2) (3 4)))))
59 ;;; TYPEP, SUBTYPEP, UPGRADED-ARRAY-ELEMENT-TYPE and
60 ;;; UPGRADED-COMPLEX-PART-TYPE should be able to deal with NIL as an
61 ;;; environment argument
62 (with-test (:name (typep :environment nil))
63 (typep 1 'fixnum nil))
65 (with-test (:name (subtypep :environment nil))
66 (subtypep 'fixnum 'integer nil))
68 (with-test (:name (upgraded-array-element-type :environment nil))
69 (upgraded-array-element-type '(mod 5) nil))
71 (with-test (:name (upgraded-complex-part-type :environment nil))
72 (upgraded-complex-part-type '(single-float 0.0 1.0) nil))
74 #+sb-doc
75 (with-test (:name (documentation :sb-ext))
76 ;; We should have documentation for our extension package:
77 (assert (documentation (find-package "SB-EXT") t)))
79 ;;; DECLARE should not be a special operator
80 (with-test (:name (declare :not special-operator-p))
81 (assert (not (special-operator-p 'declare))))
83 ;;; WITH-TIMEOUT should accept more than one form in its body.
84 (with-test (:name (sb-ext:with-timeout :forms))
85 (handler-bind ((sb-ext:timeout #'continue))
86 (sb-ext:with-timeout 3
87 (sleep 2)
88 (sleep 2))))
90 ;;; SLEEP should not cons except on 32-bit platforms when
91 ;;; (> (mod seconds 1) (* most-positive-fixnum 1e-9))
92 (with-test (:name (sleep :non-consing) :fails-on :win32
93 :skipped-on :interpreter)
94 (handler-case (sb-ext:with-timeout 5
95 (ctu:assert-no-consing (sleep 0.00001s0))
96 (locally (declare (notinline sleep))
97 (ctu:assert-no-consing (sleep 0.00001s0))
98 (ctu:assert-no-consing (sleep 0.00001d0))
99 (ctu:assert-no-consing (sleep 1/100000003))))
100 (timeout ())))
102 ;;; Changes to make SLEEP cons less led to SLEEP
103 ;;; not sleeping at all on 32-bit platforms when
104 ;;; (> (mod seconds 1) (* most-positive-fixnum 1e-9)).
105 (with-test (:name (sleep :bug-1194673))
106 (assert (eq :timeout
107 (handler-case
108 (with-timeout 0.01
109 (sleep 0.6))
110 (timeout ()
111 :timeout)))))
113 ;;; SLEEP should work with large integers as well
114 (with-test (:name (sleep :pretty-much-forever))
115 (assert (eq :timeout
116 (handler-case
117 (sb-ext:with-timeout 1
118 (sleep (ash 1 (* 2 sb-vm:n-word-bits))))
119 (sb-ext:timeout ()
120 :timeout)))))
122 ;;; DOCUMENTATION should return nil, not signal slot-unbound
123 (with-test (:name (documentation :return nil))
124 (flet ((test (thing doc-type)
125 (assert (eq nil (documentation thing doc-type)))))
126 (test 'fixnum 'type)
127 (test 'class 'type)
128 (test (find-class 'class) 'type)
129 (test 'foo 'structure)))
131 ;;; DECODE-UNIVERSAL-TIME should accept second-resolution time-zones.
132 (with-test (:name (decode-universal-time :second-resolution :time-zone))
133 (macrolet ((test (ut time-zone list)
134 (destructuring-bind (sec min hr date mon yr day tz)
135 list
136 `(multiple-value-bind (sec min hr date mon yr day dst tz)
137 (decode-universal-time ,ut ,time-zone)
138 (declare (ignore dst))
139 (assert (= sec ,sec))
140 (assert (= min ,min))
141 (assert (= hr ,hr))
142 (assert (= date ,date))
143 (assert (= mon ,mon))
144 (assert (= yr ,yr))
145 (assert (= day ,day))
146 (assert (= tz ,tz))))))
147 (test (* 86400 365) -1/3600 (1 0 0 1 1 1901 1 -1/3600))
148 (test (* 86400 365) 0 (0 0 0 1 1 1901 1 0))
149 (test (* 86400 365) 1/3600 (59 59 23 31 12 1900 0 1/3600))))
151 ;;; DECODE-UNIVERSAL-TIME shouldn't fail when the time is outside UNIX
152 ;;; 32-bit time_t and a timezone wasn't passed
153 (with-test (:name (decode-universal-time :decode 0))
154 (decode-universal-time 0 nil))
156 ;;; ENCODE-UNIVERSAL-TIME should be able to encode the universal time
157 ;;; 0 when passed a representation in a timezone where the
158 ;;; representation of 0 as a decoded time is in 1899.
159 (with-test (:name (encode-universal-time :encode 0))
160 (encode-universal-time 0 0 23 31 12 1899 1))
162 ;;; DISASSEMBLE shouldn't fail on purified functions
163 (with-test (:name (disassemble :purified))
164 (disassemble 'cl:+ :stream (make-broadcast-stream))
165 (disassemble 'sb-ext:run-program :stream (make-broadcast-stream)))
167 ;;; minimal test of GC: see stress-gc.{sh,lisp} for a more
168 ;;; comprehensive test.
169 (with-test (:name (sb-ext:gc :minimal :stress))
170 (loop repeat 2
171 do (compile nil '(lambda (x) x))
172 do (sb-ext:gc :full t)))
174 ;;; On x86-64, the instruction definitions for CMP*[PS][SD] were broken
175 ;;; so that the disassembler threw an error when they were used with
176 ;;; one operand in memory.
177 (with-test (:name (disassemble :bug-814702))
178 ;; Quote the lambdas, because WITH-TEST produces a hairy lexical environment
179 ;; which make an interpreted lambda uncompilable.
180 (disassemble '(lambda (x)
181 (= #C(2.0f0 3.0f0)
182 (the (complex single-float) x)))
183 :stream (make-broadcast-stream))
184 (disassemble '(lambda (x y)
185 (= (the (complex single-float) x)
186 (the (complex single-float) y)))
187 :stream (make-broadcast-stream)))
189 #+x86-64
190 ;; The labeler for LEA would choke on an illegal encoding
191 ;; instead of showing what it illegally encodes, such as LEA RAX, RSP
192 (with-test (:name (disassemble :x86-lea :illegal-op))
193 (let ((a (coerce '(#x48 #x8D #xC4) '(array (unsigned-byte 8) (3)))))
194 (sb-sys:with-pinned-objects (a)
195 (sb-disassem::disassemble-memory (sb-sys:sap-int (sb-sys:vector-sap a)) 3
196 :stream (make-broadcast-stream)))))
198 ;; Assert that disassemblies of identically-acting functions are identical
199 ;; if address printing is turned off. Should work on any backend, I think.
200 (with-test (:name (disassemble :without-addresses))
201 (flet ((disassembly-text (lambda-expr)
202 (let ((string
203 (let ((sb-disassem::*disassem-location-column-width* 0)
204 (*print-pretty* nil)) ; prevent function name wraparound
205 (with-output-to-string (s)
206 (disassemble lambda-expr :stream s)))))
207 ;; Return all except the first two lines. This is subject to change
208 ;; any time we muck with the layout unfortunately.
209 (subseq string
210 (1+ (position #\Newline string
211 :start (1+ (position #\Newline string))))))))
212 (let ((string1 (disassembly-text '(lambda (x) (car x))))
213 (string2 (disassembly-text '(lambda (y) (car y)))))
214 (assert (string= string1 string2)))))
216 (with-test (:name :disassemble-assembly-routine)
217 (let ((code
218 (block nil
219 (sb-vm::map-allocated-objects
220 (lambda (obj type size)
221 (declare (ignore size))
222 (when (= type sb-vm:code-header-widetag)
223 (return obj)))
224 :read-only))))
225 (assert code) ; found something to disassemble
226 (sb-disassem:disassemble-code-component code
227 :stream (make-broadcast-stream))))
229 ;;; This tests that the x86-64 disasembler does not crash
230 ;;; on LEA with a rip-relative operand and no label.
231 (with-test (:name :disassemble-no-labels
232 :skipped-on '(not :x86-64))
233 (let* ((lines
234 (split-string
235 (with-output-to-string (stream)
236 ;; A smallish function whose code happens to contain
237 ;; the thing under test.
238 (disassemble 'sb-impl::inspector :stream stream))
239 #\Newline))
240 (line (find "; = L0" lines :test 'search)))
241 (assert (search "LEA " line)) ; verify our test precondition
242 ;; Now just disassemble without labels and see that we don't crash
243 (disassemble 'sb-impl::inspector
244 :use-labels nil
245 :stream (make-broadcast-stream))))
247 ;;; Check that SLEEP called with ratios (with no common factors with
248 ;;; 1000000000, and smaller than 1/1000000000) works more or less as
249 ;;; expected.
250 (with-test (:name (sleep ratio))
251 (let ((fun0a (checked-compile '(lambda () (sleep 1/7))))
252 (fun0b (checked-compile '(lambda () (sleep 1/100000000000000000000000000))))
253 (fun1 (checked-compile '(lambda (x) (sleep x))))
254 (start-time (get-universal-time)))
255 (sleep 1/7)
256 (sleep 1/100000000000000000000000000)
257 (funcall fun0a)
258 (funcall fun0b)
259 (funcall fun1 1/7)
260 (funcall fun1 1/100000000000000000000000000)
261 (assert (< (- (get-universal-time) start-time) 2))))
263 (with-test (:name (sb-ext:assert-version->= :ok))
264 (sb-ext:assert-version->= 1 1 13))
266 (with-test (:name (sb-ext:assert-version->= :fails))
267 (assert-error
268 (sb-ext:assert-version->= most-positive-fixnum)))
270 (with-test (:name :bug-1095483)
271 (assert-error (fboundp '(cas "foo"))))
273 (with-test (:name :sleep-return-value)
274 (assert (equal (multiple-value-list
275 (funcall
276 (checked-compile `(lambda ()
277 (sleep 0.1)))))
278 '(nil))))