1.0.6.58: better heap exhaustion information
[sbcl/lichteblau.git] / tests / stream.impure.lisp
blob8ecee3a6c5a5b5c5c9bbca74b9e7a138bddc6ac0
1 ;;;; tests related to Lisp streams
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 (load "assertoid.lisp")
15 (use-package "ASSERTOID")
17 ;;; type errors for inappropriate stream arguments, fixed in
18 ;;; sbcl-0.7.8.19
19 (locally
20 (declare (optimize (safety 3)))
21 (assert (raises-error? (make-two-way-stream (make-string-output-stream)
22 (make-string-output-stream))
23 type-error))
24 (assert (raises-error? (make-two-way-stream (make-string-input-stream "foo")
25 (make-string-input-stream "bar"))
26 type-error))
27 ;; the following two aren't actually guaranteed, because ANSI, as it
28 ;; happens, doesn't say "should signal an error" for
29 ;; MAKE-ECHO-STREAM. It's still good to have, but if future
30 ;; maintenance work causes this test to fail because of these
31 ;; MAKE-ECHO-STREAM clauses, consider simply removing these clauses
32 ;; from the test. -- CSR, 2002-10-06
33 (assert (raises-error? (make-echo-stream (make-string-output-stream)
34 (make-string-output-stream))
35 type-error))
36 (assert (raises-error? (make-echo-stream (make-string-input-stream "foo")
37 (make-string-input-stream "bar"))
38 type-error))
39 (assert (raises-error? (make-concatenated-stream
40 (make-string-output-stream)
41 (make-string-input-stream "foo"))
42 type-error)))
44 ;;; bug 225: STRING-STREAM was not a class
45 (eval `(defgeneric bug225 (s)
46 ,@(mapcar (lambda (class)
47 `(:method :around ((s ,class)) (cons ',class (call-next-method))))
48 '(stream string-stream sb-impl::string-input-stream
49 sb-impl::string-output-stream))
50 (:method (class) nil)))
52 (assert (equal (bug225 (make-string-input-stream "hello"))
53 '(sb-impl::string-input-stream string-stream stream)))
54 (assert (equal (bug225 (make-string-output-stream))
55 '(sb-impl::string-output-stream string-stream stream)))
58 ;;; improper buffering on (SIGNED-BYTE 8) streams (fixed by David Lichteblau):
59 (let ((p "signed-byte-8-test.data"))
60 (with-open-file (s p
61 :direction :output
62 :element-type '(unsigned-byte 8)
63 :if-exists :supersede)
64 (write-byte 255 s))
65 (with-open-file (s p :element-type '(signed-byte 8))
66 (assert (= (read-byte s) -1)))
67 (delete-file p))
69 ;;; :IF-EXISTS got :ERROR and NIL the wrong way round (reported by
70 ;;; Milan Zamazal)
71 (let* ((p "this-file-will-exist")
72 (stream (open p :direction :output :if-exists :error)))
73 (assert (null (with-open-file (s p :direction :output :if-exists nil) s)))
74 (assert (raises-error?
75 (with-open-file (s p :direction :output :if-exists :error))))
76 (close stream)
77 (delete-file p))
79 (assert (raises-error? (read-byte (make-string-input-stream "abc"))
80 type-error))
81 (assert (raises-error? (with-open-file (s "/dev/zero")
82 (read-byte s))
83 type-error))
84 ;;; bidirectional streams getting confused about their position
85 (let ((p "bidirectional-stream-test"))
86 (with-open-file (s p :direction :output :if-exists :supersede)
87 (with-standard-io-syntax
88 (format s "~S ~S ~S~%" 'these 'are 'symbols)))
89 (with-open-file (s p :direction :io :if-exists :overwrite)
90 (read s)
91 (with-standard-io-syntax
92 (prin1 'insert s)))
93 (with-open-file (s p)
94 (assert (string= (read-line s) "THESE INSERTMBOLS")))
95 (delete-file p))
97 ;;; :DIRECTION :IO didn't work on non-existent pathnames
98 (let ((p "direction-io-test"))
99 (ignore-errors (delete-file p))
100 (with-open-file (s p :direction :io)
101 (format s "1")
102 (finish-output s)
103 (file-position s :start)
104 (assert (char= (read-char s) #\1)))
105 (delete-file p))
107 ;;; FILE-POSITION on broadcast-streams is mostly uncontroversial
108 (assert (= 0 (file-position (make-broadcast-stream))))
109 (assert (file-position (make-broadcast-stream) :start))
110 (assert (file-position (make-broadcast-stream) 0))
111 (assert (not (file-position (make-broadcast-stream) 1)))
112 (let ((s (make-broadcast-stream)))
113 (write-char #\a s)
114 (assert (not (file-position s 1)))
115 (assert (= 0 (file-position s))))
117 (let ((p "broadcast-stream-test"))
118 (ignore-errors (delete-file p))
119 (with-open-file (f p :direction :output)
120 (let ((s (make-broadcast-stream f)))
121 (assert (= 0 (file-position s)))
122 (assert (file-position s :start))
123 (assert (file-position s 0))
124 (write-char #\a s)
125 (assert (= 1 (file-position s))) ; unicode...
126 (assert (file-position s 0))))
127 (delete-file p))
129 ;;; CLOSING a non-new streams should not delete them, and superseded
130 ;;; files should be restored.
131 (let ((test "test-file-for-close-should-not-delete"))
132 (macrolet ((test-mode (mode)
133 `(progn
134 (catch :close-test-exit
135 (with-open-file (f test :direction :output :if-exists ,mode)
136 (write-line "test" f)
137 (throw :close-test-exit t)))
138 (assert (and (probe-file test) ,mode)))))
139 (unwind-protect
140 (progn
141 (with-open-file (f test :direction :output)
142 (write-line "test" f))
143 (test-mode :append)
144 (test-mode :overwrite)
145 ;; FIXME: We really should recover supersede files as well, according to
146 ;; CLOSE in CLHS, but at the moment we don't.
147 ;; (test-mode :supersede)
148 (test-mode :rename)
149 (test-mode :rename-and-delete))
150 (when (probe-file test)
151 (delete-file test)))))
153 ;;; test for read-write invariance of signed bytes, from Bruno Haible
154 ;;; cmucl-imp 2004-09-06
155 (defun bin-stream-test (&key (size (integer-length most-positive-fixnum))
156 (type 'unsigned-byte) (file-name "stream-impure.tmp")
157 (num-bytes 10)
158 (bytes (if (eq type 'signed-byte)
159 (loop :repeat num-bytes :collect
160 (- (random (ash 1 size))
161 (ash 1 (1- size))))
162 (loop :repeat num-bytes :collect
163 (random (ash 1 size))))))
164 (with-open-file (foo file-name :direction :output :if-exists :supersede
165 :element-type (list type size))
166 (dolist (byte bytes)
167 (write-byte byte foo)))
168 (unwind-protect
169 (with-open-file (foo file-name :direction :input
170 :element-type (list type size))
171 (list (stream-element-type foo) (file-length foo) bytes
172 (loop :for byte :in bytes :for nb = (read-byte foo) :collect nb
173 :unless (= nb byte) :do
174 (flet ((by-out (sz by)
175 (format nil "~v,'0,' ,4:b"
176 (+ sz (floor sz 4)) by)))
177 (error "~& * [(~s ~s)] ~a != ~a~%" type size
178 (by-out size byte) (by-out size nb))))))
179 (delete-file file-name)))
180 (loop for size from 2 to 40 do (bin-stream-test :size size :type 'signed-byte))
182 ;;; Check READ-SEQUENCE signals a TYPE-ERROR when the sequence can't
183 ;;; contain a stream element.
185 ;;; These tests check READ-SEQUENCE correctness, not whether the fast
186 ;;; or slow paths are being taken for each element type. To check the
187 ;;; fast or slow paths, trace ANSI-STREAM-READ-BYTE (slow path) and/or
188 ;;; READ-N-BYTES:
190 ;;; (trace sb-impl::ansi-stream-read-byte sb-impl::read-n-bytes)
192 ;;; The order should be ANSI-STREAM-READ-BYTE, READ-N-BYTES,
193 ;;; READ-N-BYTES, ANSI-STREAM-READ-BYTE, ANSI-STREAM-READ-BYTE.
195 (let ((pathname "read-sequence.data"))
197 ;; Create the binary data.
198 (with-open-file (stream pathname
199 :direction :output
200 :if-exists :supersede
201 :element-type '(unsigned-byte 8))
202 (write-byte 255 stream))
204 ;; Check the slow path for generic vectors.
205 (let ((sequence (make-array 1)))
206 (with-open-file (stream pathname
207 :direction :input
208 :element-type '(unsigned-byte 8))
209 (read-sequence sequence stream)
210 (assert (equalp sequence #(255)))))
212 (let ((sequence (make-array 1)))
213 (with-open-file (stream pathname
214 :direction :input
215 :external-format :latin-1
216 :element-type 'character)
217 (read-sequence sequence stream)
218 (assert (equalp sequence #(#.(code-char 255))))))
220 ;; Check the fast path works for (UNSIGNED-BYTE 8) and (SIGNED-BYTE
221 ;; 8) vectors.
222 (let ((sequence (make-array 1 :element-type '(unsigned-byte 8))))
223 (with-open-file (stream pathname
224 :direction :input
225 :element-type '(unsigned-byte 8))
226 (read-sequence sequence stream)
227 (assert (equalp sequence #(255)))))
229 (let ((sequence (make-array 1 :element-type '(signed-byte 8))))
230 (with-open-file (stream pathname
231 :direction :input
232 :element-type '(signed-byte 8))
233 (read-sequence sequence stream)
234 (assert (equalp sequence #(-1)))))
236 ;; A bivalent stream can be read to a unsigned-byte vector, a
237 ;; string, or a generic vector
239 (let ((sequence (make-array 1 :element-type '(unsigned-byte 8))))
240 (with-open-file (stream pathname
241 :direction :input
242 :element-type :default)
243 (read-sequence sequence stream)
244 (assert (equalp sequence #(255)))))
246 (let ((sequence (make-array 1 :element-type 'character)))
247 (with-open-file (stream pathname
248 :direction :input
249 :external-format :latin-1
250 :element-type :default)
251 (read-sequence sequence stream)
252 (assert (equalp sequence #(#.(code-char 255))))))
254 (let ((sequence (make-array 1)))
255 (with-open-file (stream pathname
256 :direction :input
257 :external-format :latin-1
258 :element-type :default)
259 (read-sequence sequence stream)
260 (assert (equalp sequence #(#.(code-char 255))))))
262 ;; Check that a TYPE-ERROR is signalled for incompatible (sequence,
263 ;; stream) pairs.
265 (let ((sequence (make-array 1 :element-type '(signed-byte 8))))
266 (with-open-file (stream pathname
267 :direction :input
268 :element-type '(unsigned-byte 8))
269 (handler-case (progn
270 (read-sequence sequence stream)
271 (error "READ-SEQUENCE didn't signal an error"))
272 (type-error (condition)
273 (assert (= (type-error-datum condition) 255))
274 (assert (subtypep (type-error-expected-type condition)
275 '(signed-byte 8)))))))
277 (let ((sequence (make-array 1 :element-type '(unsigned-byte 8))))
278 (with-open-file (stream pathname
279 :direction :input
280 :element-type '(signed-byte 8))
281 (handler-case (progn
282 (read-sequence sequence stream)
283 (error "READ-SEQUENCE didn't signal an error"))
284 (type-error (condition)
285 (assert (= (type-error-datum condition) -1))
286 (assert (subtypep (type-error-expected-type condition)
287 '(unsigned-byte 8)))))))
289 ;; Can't read a signed-byte from a bivalent stream
291 (let ((sequence (make-array 1 :element-type '(signed-byte 8))))
292 (with-open-file (stream pathname
293 :direction :input
294 :external-format :latin1
295 :element-type :default)
296 (handler-case (progn
297 (read-sequence sequence stream)
298 (error "READ-SEQUENCE didn't signal an error"))
299 (type-error (condition)
300 (assert (eql (type-error-datum condition) (code-char 255)))
301 (assert (subtypep (type-error-expected-type condition)
302 '(signed-byte 8))))))))
304 ;;; Check WRITE-SEQUENCE signals a TYPE-ERROR when the stream can't
305 ;;; write a sequence element.
307 ;;; These tests check WRITE-SEQUENCE correctness, not whether the fast
308 ;;; or slow paths are being taken for each element type. See the
309 ;;; READ-SEQUENCE tests above for more information.
311 ;;; (trace sb-impl::output-unsigned-byte-full-buffered sb-impl::output-signed-byte-full-buffered sb-impl::output-raw-bytes)
313 (let ((pathname "write-sequence.data")
314 (generic-sequence (make-array 1 :initial-contents '(255)))
315 (generic-character-sequence (make-array 1 :initial-element #\a))
316 (generic-mixed-sequence (make-array 2 :initial-element #\a))
317 (string (make-array 1 :element-type 'character
318 :initial-element (code-char 255)))
319 (unsigned-sequence (make-array 1
320 :element-type '(unsigned-byte 8)
321 :initial-contents '(255)))
322 (signed-sequence (make-array 1
323 :element-type '(signed-byte 8)
324 :initial-contents '(-1))))
326 (setf (aref generic-mixed-sequence 1) 255)
328 ;; Check the slow path for generic vectors.
329 (with-open-file (stream pathname
330 :direction :output
331 :if-exists :supersede
332 :element-type '(unsigned-byte 8))
333 (write-sequence generic-sequence stream))
335 (with-open-file (stream pathname
336 :direction :output
337 :if-exists :supersede
338 :element-type 'character)
339 (write-sequence generic-character-sequence stream))
341 ;; Check the fast path for unsigned and signed vectors.
342 (with-open-file (stream pathname
343 :direction :output
344 :if-exists :supersede
345 :element-type '(unsigned-byte 8))
346 (write-sequence unsigned-sequence stream))
348 (with-open-file (stream pathname
349 :direction :output
350 :if-exists :supersede
351 :element-type '(signed-byte 8))
352 (write-sequence signed-sequence stream))
354 ;; Bivalent streams on unsigned-byte vectors, strings, and a simple
355 ;; vector with mixed characters and bytes
357 (with-open-file (stream pathname
358 :direction :output
359 :if-exists :supersede
360 :element-type :default)
361 (write-sequence unsigned-sequence stream))
363 (with-open-file (stream pathname
364 :direction :output
365 :external-format :latin-1
366 :if-exists :supersede
367 :element-type :default)
368 (write-sequence string stream))
370 (with-open-file (stream pathname
371 :direction :output
372 :external-format :latin-1
373 :if-exists :supersede
374 :element-type :default)
375 (write-sequence generic-mixed-sequence stream))
377 ;; Check a TYPE-ERROR is signalled for unsigned and signed vectors
378 ;; which are incompatible with the stream element type.
379 (with-open-file (stream pathname
380 :direction :output
381 :if-exists :supersede
382 :element-type '(signed-byte 8))
383 (handler-case (progn
384 (write-sequence unsigned-sequence stream)
385 (error "WRITE-SEQUENCE didn't signal an error"))
386 (type-error (condition)
387 (assert (= (type-error-datum condition) 255))
388 (assert (subtypep (type-error-expected-type condition)
389 '(signed-byte 8))))))
391 (with-open-file (stream pathname
392 :direction :output
393 :if-exists :supersede
394 :element-type '(unsigned-byte 8))
395 (handler-case (progn
396 (write-sequence signed-sequence stream)
397 (error "WRITE-SEQUENCE didn't signal an error"))
398 (type-error (condition)
399 (assert (= (type-error-datum condition) -1))
400 (assert (subtypep (type-error-expected-type condition)
401 '(unsigned-byte 8))))))
403 (with-open-file (stream pathname
404 :direction :output
405 :if-exists :supersede
406 :element-type :default)
407 (handler-case (progn
408 (write-sequence signed-sequence stream)
409 (error "WRITE-SEQUENCE didn't signal an error"))
410 (type-error (condition)
411 (assert (= (type-error-datum condition) -1))
412 (assert (subtypep (type-error-expected-type condition)
413 '(unsigned-byte 8)))))))
415 ;;; writing looong lines. takes way too long and way too much space
416 ;;; to test on 64 bit platforms
417 #-#.(cl:if (cl:= sb-vm:n-word-bits 64) '(and) '(or))
418 (progn
419 (defun write-n-chars (n stream)
420 (format t "~&/writing ~D chars on a single line~%" n)
421 (finish-output t)
422 (loop repeat n
423 do (write-char #\x stream))
424 (terpri stream)
427 (let ((test "long-lines-write-test.tmp"))
428 (unwind-protect
429 (with-open-file (f test
430 :direction :output
431 :external-format :ascii
432 :element-type 'character
433 :if-does-not-exist :create
434 :if-exists :supersede)
435 (write-n-chars (+ most-positive-fixnum 7) f))
436 (when (probe-file test)
437 (delete-file test)))))
439 ;;; success