fd3f8df30315116c77afc92af6ce435a19b0daaf
[sbcl.git] / tests / stream.impure.lisp
1 ;;;; tests related to Lisp streams
2
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.
13
14 (load "assertoid.lisp")
15 (use-package "ASSERTOID")
16
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)))
43
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)))
51
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)))
56
57 \f
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))
68 \f
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))
78 \f
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     (let ((line (read-line s))
95           (want "THESE INSERTMBOLS"))
96       (unless (equal line want)
97         (error "wanted ~S, got ~S" want line))))
98   (delete-file p))
99 \f
100 ;;; :DIRECTION :IO didn't work on non-existent pathnames
101 (let ((p "direction-io-test"))
102   (ignore-errors (delete-file p))
103   (with-open-file (s p :direction :io)
104     (format s "1")
105     (finish-output s)
106     (file-position s :start)
107     (assert (char= (read-char s) #\1)))
108   (delete-file p))
109 \f
110 ;;; FILE-POSITION on broadcast-streams is mostly uncontroversial
111 (assert (= 0 (file-position (make-broadcast-stream))))
112 (assert (file-position (make-broadcast-stream) :start))
113 (assert (file-position (make-broadcast-stream) 0))
114 (assert (not (file-position (make-broadcast-stream) 1)))
115 (let ((s (make-broadcast-stream)))
116   (write-char #\a s)
117   (assert (not (file-position s 1)))
118   (assert (= 0 (file-position s))))
119
120 (let ((p "broadcast-stream-test"))
121   (ignore-errors (delete-file p))
122   (with-open-file (f p :direction :output)
123     (let ((s (make-broadcast-stream f)))
124       (assert (= 0 (file-position s)))
125       (assert (file-position s :start))
126       (assert (file-position s 0))
127       (write-char #\a s)
128       (assert (= 1 (file-position s))) ; unicode...
129       (assert (file-position s 0))))
130   (delete-file p))
131
132 ;;; CLOSING a non-new streams should not delete them, and superseded
133 ;;; files should be restored.
134 (let ((test "test-file-for-close-should-not-delete"))
135   (macrolet ((test-mode (mode)
136                `(progn
137                  (catch :close-test-exit
138                    (with-open-file (f test :direction :output :if-exists ,mode)
139                      (write-line "test" f)
140                      (throw :close-test-exit t)))
141                  (assert (and (probe-file test) ,mode)))))
142     (unwind-protect
143          (progn
144            (with-open-file (f test :direction :output)
145              (write-line "test" f))
146            (test-mode :append)
147            (test-mode :overwrite)
148            ;; FIXME: We really should recover supersede files as well, according to
149            ;; CLOSE in CLHS, but at the moment we don't.
150            ;; (test-mode :supersede)
151            (test-mode :rename)
152            (test-mode :rename-and-delete))
153       (when (probe-file test)
154         (delete-file test)))))
155
156 ;;; test for read-write invariance of signed bytes, from Bruno Haible
157 ;;; cmucl-imp 2004-09-06
158 (defun bin-stream-test (&key (size (integer-length most-positive-fixnum))
159                         (type 'unsigned-byte) (file-name "stream-impure.tmp")
160                         (num-bytes 10)
161                         (bytes (if (eq type 'signed-byte)
162                                    (loop :repeat num-bytes :collect
163                                          (- (random (ash 1 size))
164                                             (ash 1 (1- size))))
165                                    (loop :repeat num-bytes :collect
166                                          (random (ash 1 size))))))
167   (with-open-file (foo file-name :direction :output :if-exists :supersede
168                        :element-type (list type size))
169     (dolist (byte bytes)
170       (write-byte byte foo)))
171   (unwind-protect
172        (with-open-file (foo file-name :direction :input
173                             :element-type (list type size))
174          (list (stream-element-type foo) (file-length foo) bytes
175                (loop :for byte :in bytes :for nb = (read-byte foo) :collect nb
176                      :unless (= nb byte) :do
177                      (flet ((by-out (sz by)
178                               (format nil "~v,'0,' ,4:b"
179                                       (+ sz (floor sz 4)) by)))
180                        (error "~& * [(~s ~s)] ~a != ~a~%" type size
181                               (by-out size byte) (by-out size nb))))))
182     (delete-file file-name)))
183 (loop for size from 2 to 40 do (bin-stream-test :size size :type 'signed-byte))
184 \f
185 ;;; Check READ-SEQUENCE signals a TYPE-ERROR when the sequence can't
186 ;;; contain a stream element.
187 ;;;
188 ;;; These tests check READ-SEQUENCE correctness, not whether the fast
189 ;;; or slow paths are being taken for each element type.  To check the
190 ;;; fast or slow paths, trace ANSI-STREAM-READ-BYTE (slow path) and/or
191 ;;; READ-N-BYTES:
192 ;;;
193 ;;; (trace sb-impl::ansi-stream-read-byte sb-impl::read-n-bytes)
194 ;;;
195 ;;; The order should be ANSI-STREAM-READ-BYTE, READ-N-BYTES,
196 ;;; READ-N-BYTES, ANSI-STREAM-READ-BYTE, ANSI-STREAM-READ-BYTE.
197
198 (let ((pathname "read-sequence.data"))
199
200   ;; Create the binary data.
201   (with-open-file (stream pathname
202                           :direction :output
203                           :if-exists :supersede
204                           :element-type '(unsigned-byte 8))
205     (write-byte 255 stream))
206
207   ;; Check the slow path for generic vectors.
208   (let ((sequence (make-array 1)))
209     (with-open-file (stream pathname
210                             :direction :input
211                             :element-type '(unsigned-byte 8))
212     (read-sequence sequence stream)
213     (assert (equalp sequence #(255)))))
214
215   (let ((sequence (make-array 1)))
216     (with-open-file (stream pathname
217                             :direction :input
218                             :external-format :latin-1
219                             :element-type 'character)
220       (read-sequence sequence stream)
221       (assert (equalp sequence #(#.(code-char 255))))))
222
223   ;; Check the fast path works for (UNSIGNED-BYTE 8) and (SIGNED-BYTE
224   ;; 8) vectors.
225   (let ((sequence (make-array 1 :element-type '(unsigned-byte 8))))
226     (with-open-file (stream pathname
227                             :direction :input
228                             :element-type '(unsigned-byte 8))
229       (read-sequence sequence stream)
230       (assert (equalp sequence #(255)))))
231
232   (let ((sequence (make-array 1 :element-type '(signed-byte 8))))
233     (with-open-file (stream pathname
234                             :direction :input
235                             :element-type '(signed-byte 8))
236     (read-sequence sequence stream)
237     (assert (equalp sequence #(-1)))))
238
239   ;; A bivalent stream can be read to a unsigned-byte vector, a
240   ;; string, or a generic vector
241
242   (let ((sequence (make-array 1 :element-type '(unsigned-byte 8))))
243     (with-open-file (stream pathname
244                             :direction :input
245                             :element-type :default)
246       (read-sequence sequence stream)
247       (assert (equalp sequence #(255)))))
248
249   (let ((sequence (make-array 1 :element-type 'character)))
250     (with-open-file (stream pathname
251                             :direction :input
252                             :external-format :latin-1
253                             :element-type :default)
254       (read-sequence sequence stream)
255       (assert (equalp sequence #(#.(code-char 255))))))
256
257   (let ((sequence (make-array 1)))
258     (with-open-file (stream pathname
259                             :direction :input
260                             :external-format :latin-1
261                             :element-type :default)
262       (read-sequence sequence stream)
263       (assert (equalp sequence #(#.(code-char 255))))))
264
265   ;; Check that a TYPE-ERROR is signalled for incompatible (sequence,
266   ;; stream) pairs.
267
268   (let ((sequence (make-array 1 :element-type '(signed-byte 8))))
269     (with-open-file (stream pathname
270                             :direction :input
271                             :element-type '(unsigned-byte 8))
272       (handler-case (progn
273                       (read-sequence sequence stream)
274                       (error "READ-SEQUENCE didn't signal an error"))
275         (type-error (condition)
276           (assert (= (type-error-datum condition) 255))
277           (assert (subtypep (type-error-expected-type condition)
278                             '(signed-byte 8)))))))
279
280   (let ((sequence (make-array 1 :element-type '(unsigned-byte 8))))
281     (with-open-file (stream pathname
282                             :direction :input
283                             :element-type '(signed-byte 8))
284       (handler-case (progn
285                       (read-sequence sequence stream)
286                       (error "READ-SEQUENCE didn't signal an error"))
287         (type-error (condition)
288           (assert (= (type-error-datum condition) -1))
289           (assert (subtypep (type-error-expected-type condition)
290                             '(unsigned-byte 8)))))))
291
292   ;; Can't read a signed-byte from a bivalent stream
293
294   (let ((sequence (make-array 1 :element-type '(signed-byte 8))))
295     (with-open-file (stream pathname
296                             :direction :input
297                             :external-format :latin1
298                             :element-type :default)
299       (handler-case (progn
300                       (read-sequence sequence stream)
301                       (error "READ-SEQUENCE didn't signal an error"))
302         (type-error (condition)
303           (assert (eql (type-error-datum condition) (code-char 255)))
304           (assert (subtypep (type-error-expected-type condition)
305                             '(signed-byte 8)))))))
306   (delete-file pathname))
307 \f
308 ;;; Check WRITE-SEQUENCE signals a TYPE-ERROR when the stream can't
309 ;;; write a sequence element.
310 ;;;
311 ;;; These tests check WRITE-SEQUENCE correctness, not whether the fast
312 ;;; or slow paths are being taken for each element type.  See the
313 ;;; READ-SEQUENCE tests above for more information.
314 ;;;
315 ;;; (trace sb-impl::output-unsigned-byte-full-buffered sb-impl::output-signed-byte-full-buffered sb-impl::output-raw-bytes)
316
317 (let ((pathname "write-sequence.data")
318       (generic-sequence (make-array 1 :initial-contents '(255)))
319       (generic-character-sequence (make-array 1 :initial-element #\a))
320       (generic-mixed-sequence (make-array 2 :initial-element #\a))
321       (string (make-array 1 :element-type 'character
322                           :initial-element (code-char 255)))
323       (unsigned-sequence (make-array 1
324                                      :element-type '(unsigned-byte 8)
325                                      :initial-contents '(255)))
326       (signed-sequence (make-array 1
327                                    :element-type '(signed-byte 8)
328                                    :initial-contents '(-1))))
329
330   (setf (aref generic-mixed-sequence 1) 255)
331
332   ;; Check the slow path for generic vectors.
333   (with-open-file (stream pathname
334                            :direction :output
335                            :if-exists :supersede
336                            :element-type '(unsigned-byte 8))
337     (write-sequence generic-sequence stream))
338
339   (with-open-file (stream pathname
340                           :direction :output
341                           :if-exists :supersede
342                           :element-type 'character)
343     (write-sequence generic-character-sequence stream))
344
345   ;; Check the fast path for unsigned and signed vectors.
346   (with-open-file (stream pathname
347                           :direction :output
348                           :if-exists :supersede
349                           :element-type '(unsigned-byte 8))
350     (write-sequence unsigned-sequence stream))
351
352   (with-open-file (stream pathname
353                           :direction :output
354                           :if-exists :supersede
355                           :element-type '(signed-byte 8))
356     (write-sequence signed-sequence stream))
357
358   ;; Bivalent streams on unsigned-byte vectors, strings, and a simple
359   ;; vector with mixed characters and bytes
360
361   (with-open-file (stream pathname
362                           :direction :output
363                           :if-exists :supersede
364                           :element-type :default)
365     (write-sequence unsigned-sequence stream))
366
367   (with-open-file (stream pathname
368                           :direction :output
369                           :external-format :latin-1
370                           :if-exists :supersede
371                           :element-type :default)
372     (write-sequence string stream))
373
374   (with-open-file (stream pathname
375                           :direction :output
376                           :external-format :latin-1
377                           :if-exists :supersede
378                           :element-type :default)
379     (write-sequence generic-mixed-sequence stream))
380
381   ;; Check a TYPE-ERROR is signalled for unsigned and signed vectors
382   ;; which are incompatible with the stream element type.
383   (with-open-file (stream pathname
384                           :direction :output
385                           :if-exists :supersede
386                           :element-type '(signed-byte 8))
387     (handler-case (progn
388                     (write-sequence unsigned-sequence stream)
389                     (error "WRITE-SEQUENCE didn't signal an error"))
390       (type-error (condition)
391         (assert (= (type-error-datum condition) 255))
392         (assert (subtypep (type-error-expected-type condition)
393                           '(signed-byte 8))))))
394
395   (with-open-file (stream pathname
396                           :direction :output
397                           :if-exists :supersede
398                           :element-type '(unsigned-byte 8))
399     (handler-case (progn
400                     (write-sequence signed-sequence stream)
401                     (error "WRITE-SEQUENCE didn't signal an error"))
402       (type-error (condition)
403         (assert (= (type-error-datum condition) -1))
404         (assert (subtypep (type-error-expected-type condition)
405                           '(unsigned-byte 8))))))
406
407   (with-open-file (stream pathname
408                           :direction :output
409                           :if-exists :supersede
410                           :element-type :default)
411     (handler-case (progn
412                     (write-sequence signed-sequence stream)
413                     (error "WRITE-SEQUENCE didn't signal an error"))
414       (type-error (condition)
415         (assert (= (type-error-datum condition) -1))
416         (assert (subtypep (type-error-expected-type condition)
417                           '(unsigned-byte 8))))))
418
419   (delete-file pathname))
420
421 ;;; writing looong lines. takes way too long and way too much space
422 ;;; to test on 64 bit platforms
423 #-#.(cl:if (cl:= sb-vm:n-word-bits 64) '(and) '(or))
424 (let ((test "long-lines-write-test.tmp"))
425     (unwind-protect
426          (with-open-file (f test
427                             :direction :output
428                             :external-format :ascii
429                             :element-type 'character
430                             :if-does-not-exist :create
431                             :if-exists :supersede)
432            (let* ((n (truncate most-positive-fixnum 16))
433                   (m 18)
434                   (p (* n m))
435                   (buffer (make-string n)))
436              (dotimes (i m)
437                (write-char #\.)
438                (finish-output)
439                (write-sequence buffer f))
440              (assert (= p (sb-impl::fd-stream-char-pos f)))
441              (write-char #\! f)
442              (assert (= (+ 1 p) (sb-impl::fd-stream-char-pos f)))
443              (assert (typep p 'bignum))))
444       (when (probe-file test)
445         (delete-file test))))
446
447 ;;; read-sequence misreported the amount read and lost position
448 (let ((string (make-array (* 3 sb-impl::+ansi-stream-in-buffer-length+)
449                           :element-type 'character)))
450   (dotimes (i (length string))
451     (setf (char string i) (code-char (mod i char-code-limit))))
452   (with-open-file (f "read-sequence-character-test-data.tmp"
453                      :if-exists :supersede
454                      :direction :output
455                      :external-format :utf-8)
456     (write-sequence string f))
457   (let ((copy
458          (with-open-file (f "read-sequence-character-test-data.tmp"
459                             :if-does-not-exist :error
460                             :direction :input
461                             :external-format :utf-8)
462            (let ((buffer (make-array 128 :element-type 'character))
463                  (total 0))
464              (with-output-to-string (datum)
465                (loop for n-read = (read-sequence buffer f)
466                      do (write-sequence buffer datum :start 0 :end n-read)
467                         (assert (<= (incf total n-read) (length string)))
468                      while (and (= n-read 128))))))))
469     (assert (equal copy string)))
470   (delete-file "read-sequence-character-test-data.tmp"))
471
472 ;;; ANSI-STREAM-OUTPUT-STREAM-P used to assume that a SYNONYM-STREAM's
473 ;;; target was an ANSI stream, but it could be a user-defined stream,
474 ;;; e.g., a SLIME stream.
475 (defclass user-output-stream (fundamental-output-stream)
476   ())
477
478 (let ((*stream* (make-instance 'user-output-stream)))
479   (declare (special *stream*))
480   (with-open-stream (stream (make-synonym-stream '*stream*))
481     (assert (output-stream-p stream))))
482
483 (defclass user-input-stream (fundamental-input-stream)
484   ())
485
486 (let ((*stream* (make-instance 'user-input-stream)))
487   (declare (special *stream*))
488   (with-open-stream (stream (make-synonym-stream '*stream*))
489     (assert (input-stream-p stream))))
490
491 ;;; READ-LINE on ANSI-STREAM did not return T for the last line
492 ;;; (reported by Yoshinori Tahara)
493 (let ((pathname "test-read-line-eol"))
494   (with-open-file (out pathname :direction :output :if-exists :supersede)
495     (format out "a~%b"))
496   (let ((result (with-open-file (in pathname)
497                   (list (multiple-value-list (read-line in nil nil))
498                         (multiple-value-list (read-line in nil nil))
499                         (multiple-value-list (read-line in nil nil))))))
500     (delete-file pathname)
501     (assert (equal result '(("a" nil) ("b" t) (nil t))))))
502
503 ;;; READ-LINE used to work on closed streams because input buffers were left in place
504 (with-test (:name :bug-425)
505   ;; Normal close
506   (let ((f (open "stream.impure.lisp" :direction :input)))
507     (assert (stringp (read-line f)))
508     (close f)
509     (assert (eq :fii
510                 (handler-case
511                     (read-line f)
512                   (sb-int:closed-stream-error () :fii)))))
513   ;; Abort
514   (let ((f (open "stream.impure.lisp" :direction :input)))
515     (assert (stringp (read-line f nil nil)))
516     (close f :abort t)
517     (assert (eq :faa
518                 (handler-case
519                     (read-line f)
520                   (sb-int:closed-stream-error () :faa))))))
521
522 (with-test (:name :regression-1.0.12.22)
523   (with-open-file (s "stream.impure.lisp" :direction :input)
524     (let ((buffer (make-string 20)))
525       (assert (= 2 (read-sequence buffer s :start 0 :end 2)))
526       (assert (= 3 (read-sequence buffer s :start 2 :end 3)))
527       (file-position s :end)
528       (assert (= 3 (read-sequence buffer s :start 3))))))
529
530 ;;; In 1.0.27 (and also 0.9.16; presumably in between, too), binary
531 ;;; input operations on a bivalent stream did something bad after
532 ;;; unread-char: READ-BYTE would return the character, and
533 ;;; READ-SEQUENCE into a byte buffer would lose when attempting to
534 ;;; store the character in the vector.
535 (let ((pathname "bivalent-stream-unread-char-test.tmp"))
536   (with-open-file (s pathname
537                      :element-type :default
538                      :direction :io :if-exists :rename)
539     (write-char #\a s)
540     (file-position s :start)
541     (unread-char (read-char s) s)
542     (assert (integerp (read-byte s))))
543   (delete-file pathname))
544
545 (let ((pathname "bivalent-stream-unread-char-test.tmp"))
546   (with-open-file (s pathname
547                      :element-type :default
548                      :direction :io :if-exists :rename)
549     (write-char #\a s)
550     (file-position s :start)
551     (unread-char (read-char s) s)
552     (assert (let ((buffer (make-array 10 :element-type '(unsigned-byte 8))))
553               (read-sequence buffer s))))
554   (delete-file pathname))
555
556 #+sb-unicode
557 (let ((pathname "bivalent-stream-unread-char-test.tmp"))
558   (with-open-file (s pathname
559                      :element-type :default
560                      :direction :io :if-exists :rename
561                      :external-format :utf8)
562     (write-char (code-char 192) s)
563     (file-position s :start)
564     (unread-char (read-char s) s)
565     (assert (integerp (read-byte s))))
566   (delete-file pathname))
567
568 #+sb-unicode
569 (let ((pathname "bivalent-stream-unread-char-test.tmp"))
570   (with-open-file (s pathname
571                      :element-type :default
572                      :direction :io :if-exists :rename
573                      :external-format :utf8)
574     (write-char (code-char 192) s)
575     (file-position s :start)
576     (unread-char (read-char s) s)
577     (assert (let ((buffer (make-array 10 :element-type '(unsigned-byte 8))))
578               (read-sequence buffer s))))
579   (delete-file pathname))
580
581 (with-test (:name :delete-file-on-streams)
582   (with-open-file (f "delete-file-on-stream-test.tmp"
583                      :direction :io)
584     (delete-file f)
585     #-win32
586     (progn
587       (write-line "still open" f)
588       (file-position f :start)
589       (assert (equal "still open" (read-line f)))))
590   (assert (not (probe-file "delete-file-on-stream-test.tmp"))))
591 \f
592 ;;; READ-CHAR-NO-HANG on bivalent streams (as returned by RUN-PROGRAM)
593 ;;; was wrong.  CSR managed to promote the wrongness to all streams in
594 ;;; the 1.0.32.x series, breaking slime instantly.
595 (with-test (:name :read-char-no-hang-after-unread-char)
596   (let* ((process (run-program "/bin/sh" '("-c" "echo a && sleep 10")
597                                :output :stream :wait nil))
598          (stream (process-output process))
599          (char (read-char stream)))
600     (assert (char= char #\a))
601     (unread-char char stream)
602     (assert (char= (read-char stream) #\a))
603     (assert (char= (read-char stream) #\Newline))
604     (let ((time (get-universal-time)))
605       ;; no input, not yet known to be at EOF: should return
606       ;; immediately
607       (read-char-no-hang stream)
608       (assert (< (- (get-universal-time) time) 2)))))
609
610 #-win32
611 (require :sb-posix)
612
613 (with-test (:name :interrupt-open :skipped-on :win32)
614   (let ((fifo nil)
615         (to 0))
616     (unwind-protect
617          (progn
618            ;; Make a FIFO
619            (setf fifo (sb-posix:mktemp "SBCL-fifo.XXXXXXX"))
620            (sb-posix:mkfifo fifo (logior sb-posix:s-iwusr sb-posix:s-irusr))
621            ;; Try to open it (which hangs), and interrupt ourselves with a timer,
622            ;; continue (this used to result in an error due to open(2) returning with
623            ;; EINTR, then interupt again and unwind.
624            (handler-case
625                (with-timeout 2
626                  (handler-bind ((timeout (lambda (c)
627                                            (when (eql 1 (incf to))
628                                              (continue c)))))
629                    (with-timeout 1
630                      (with-open-file (f fifo :direction :input)
631                        :open))))
632              (timeout ()
633                (if (eql 2 to)
634                    :timeout
635                    :wtf))
636              (error (e)
637                e)))
638       (when fifo
639         (ignore-errors (delete-file fifo))))))
640
641 #-win32
642 (require :sb-posix)
643 (with-test (:name :overeager-character-buffering :skipped-on :win32)
644   (let ((fifo nil)
645         (proc nil))
646     (maphash
647      (lambda (format _)
648        (declare (ignore _))
649        (format t "trying ~A~%" format)
650        (finish-output t)
651        (unwind-protect
652             (progn
653               (setf fifo (sb-posix:mktemp "SBCL-fifo-XXXXXXX"))
654               (sb-posix:mkfifo fifo (logior sb-posix:s-iwusr sb-posix:s-irusr))
655               ;; KLUDGE: because we have both ends in the same process, we would
656               ;; need to use O_NONBLOCK, but this works too.
657               (setf proc
658                     (run-program "/bin/sh"
659                                  (list "-c"
660                                        (format nil "cat > ~A" (native-namestring fifo)))
661                                  :input :stream
662                                  :wait nil
663                                  :external-format format))
664               (write-line "foobar" (process-input proc))
665               (finish-output (process-input proc))
666               (with-open-file (f fifo :direction :input :external-format format)
667                 (assert (equal "foobar" (read-line f)))))
668          (when proc
669            (ignore-errors
670              (close (process-input proc) :abort t)
671              (process-wait proc))
672            (ignore-errors (process-close proc))
673            (setf proc nil))
674          (when fifo
675            (ignore-errors (delete-file fifo))
676            (setf fifo nil))))
677      sb-impl::*external-formats*)))
678
679 (with-test (:name :bug-657183)
680   (let ((name (merge-pathnames "stream-impure.temp-test"))
681         (text '(#\GREEK_SMALL_LETTER_LAMDA
682                 #\JAPANESE_BANK_SYMBOL
683                 #\Space
684                 #\HEAVY_BLACK_HEART))
685         (positions '(2 5 6 9))
686         (sb-impl::*default-external-format* :utf-8))
687     (unwind-protect
688          (progn
689            (with-open-file (f name :external-format :default :direction :output
690                               :if-exists :supersede)
691              (assert (eql 0 (file-position f)))
692              (mapc (lambda (char pos)
693                      (write-char char f)
694                      (assert (eql pos (file-position f))))
695                    text
696                    positions))
697            (with-open-file (f name :external-format :default :direction :input)
698              (assert (eql 0 (file-position f)))
699              (assert (eql (pop text) (read-char f)))
700              (assert (eql (file-position f) 2))
701              (assert (eql (pop text) (read-char f)))
702              (assert (eql (file-position f) 5))
703              (assert (eql (pop text) (read-char f)))
704              (assert (eql (file-position f) 6))
705              (assert (eql (pop text) (read-char f)))
706              (assert (eql (file-position f) 9))
707              (assert (eql (file-length f) 9))))
708       (ignore-errors (delete-file name)))))
709
710 (with-test (:name :bug-561642)
711   (let ((p "bug-561642-test.tmp"))
712     (unwind-protect
713          (progn
714            (with-open-file (f p
715                               :if-exists :supersede
716                               :if-does-not-exist :create
717                               :direction :output)
718              (write-line "FOOBAR" f))
719            (with-open-file (f p
720                               :if-exists :append
721                               :direction :output)
722              (let ((p0 (file-position f))
723                    (p1 (progn
724                          (write-char #\newline f)
725                          (file-position f)))
726                    (p2 (progn
727                          (write-char #\newline f)
728                          (finish-output f)
729                          (file-position f))))
730                (assert (eql 7 p0))
731                (assert (eql 8 p1))
732                (assert (eql 9 p2)))))
733       (ignore-errors (delete-file p)))))
734
735 ;;; success