Name formatter lambdas nicely
[sbcl.git] / tests / interface.pure.lisp
blobc3d127c477d774a861b0bcd97780542a264e6d2c
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 ;;; Data in the high bits of a fun header caused CODE-N-UNBOXED-DATA-WORDS
190 ;;; to return a ridiculously huge value.
191 (with-test (:name (disassemble :unboxed-data))
192 (assert (< (sb-kernel:code-n-unboxed-data-words
193 (sb-kernel:fun-code-header #'expt))
194 100))) ; The exact value is irrelevant.
196 #+x86-64
197 ;; The labeler for LEA would choke on an illegal encoding
198 ;; instead of showing what it illegally encodes, such as LEA RAX, RSP
199 (with-test (:name (disassemble :x86-lea :illegal-op))
200 (let ((a (coerce '(#x48 #x8D #xC4) '(array (unsigned-byte 8) (3)))))
201 (sb-sys:with-pinned-objects (a)
202 (sb-disassem::disassemble-memory (sb-sys:sap-int (sb-sys:vector-sap a)) 3
203 :stream (make-broadcast-stream)))))
205 ;; Assert that disassemblies of identically-acting functions are identical
206 ;; if address printing is turned off. Should work on any backend, I think.
207 (with-test (:name (disassemble :without-addresses))
208 (flet ((disassembly-text (lambda-expr)
209 (let ((string
210 (let ((sb-disassem::*disassem-location-column-width* 0)
211 (*print-pretty* nil)) ; prevent function name wraparound
212 (with-output-to-string (s)
213 (disassemble lambda-expr :stream s)))))
214 ;; Return all except the first two lines. This is subject to change
215 ;; any time we muck with the layout unfortunately.
216 (subseq string
217 (1+ (position #\Newline string
218 :start (1+ (position #\Newline string))))))))
219 (let ((string1 (disassembly-text '(lambda (x) (car x))))
220 (string2 (disassembly-text '(lambda (y) (car y)))))
221 (assert (string= string1 string2)))))
223 (with-test (:name :disassemble-assembly-routine)
224 (let ((code
225 (block nil
226 (sb-vm::map-allocated-objects
227 (lambda (obj type size)
228 (declare (ignore size))
229 (when (= type sb-vm:code-header-widetag)
230 (return obj)))
231 :read-only))))
232 (assert code) ; found something to disassemble
233 (sb-disassem:disassemble-code-component code
234 :stream (make-broadcast-stream))))
236 ;;; This tests that the x86-64 disasembler does not crash
237 ;;; on LEA with a rip-relative operand and no label.
238 (with-test (:name :disassemble-no-labels
239 :skipped-on '(not :x86-64))
240 (let* ((lines
241 (split-string
242 (with-output-to-string (stream)
243 ;; A smallish function whose code happens to contain
244 ;; the thing under test.
245 (disassemble 'sb-impl::inspector :stream stream))
246 #\Newline))
247 (line (find "; = L0" lines :test 'search)))
248 (assert (search "LEA " line)) ; verify our test precondition
249 ;; Now just disassemble without labels and see that we don't crash
250 (disassemble 'sb-impl::inspector
251 :use-labels nil
252 :stream (make-broadcast-stream))))
254 ;;; Check that SLEEP called with ratios (with no common factors with
255 ;;; 1000000000, and smaller than 1/1000000000) works more or less as
256 ;;; expected.
257 (with-test (:name (sleep ratio))
258 (let ((fun0a (checked-compile '(lambda () (sleep 1/7))))
259 (fun0b (checked-compile '(lambda () (sleep 1/100000000000000000000000000))))
260 (fun1 (checked-compile '(lambda (x) (sleep x))))
261 (start-time (get-universal-time)))
262 (sleep 1/7)
263 (sleep 1/100000000000000000000000000)
264 (funcall fun0a)
265 (funcall fun0b)
266 (funcall fun1 1/7)
267 (funcall fun1 1/100000000000000000000000000)
268 (assert (< (- (get-universal-time) start-time) 2))))
270 (with-test (:name (sb-ext:assert-version->= :ok))
271 (sb-ext:assert-version->= 1 1 13))
273 (with-test (:name (sb-ext:assert-version->= :fails))
274 (assert-error
275 (sb-ext:assert-version->= most-positive-fixnum)))
277 (with-test (:name :bug-1095483)
278 (assert-error (fboundp '(cas "foo"))))
280 (with-test (:name :sleep-return-value)
281 (assert (equal (multiple-value-list
282 (funcall
283 (checked-compile `(lambda ()
284 (sleep 0.1)))))
285 '(nil))))
287 (with-test (:name :time-no-print-length-abbreviation)
288 (let ((s (make-string-output-stream)))
289 (let ((*trace-output* s))
290 (time (progn)))
291 (let ((str (get-output-stream-string s)))
292 (assert (and (>= (count #\newline str) 4)
293 (search "bytes consed" str))))))
295 (with-test (:name :split-seconds-for-sleep)
296 (assert (< (nth-value 1 (sb-impl::split-seconds-for-sleep 7.2993028420866d7))
297 1000000000)))