File: stream.impure-cload.lisp

package info (click to toggle)
sbcl 1%3A0.9.16.0-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 19,960 kB
  • ctags: 16,537
  • sloc: lisp: 231,164; ansic: 19,558; asm: 2,539; sh: 1,925; makefile: 308
file content (75 lines) | stat: -rw-r--r-- 3,324 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
;;;; tests related to Lisp streams

;;;; This software is part of the SBCL system. See the README file for
;;;; more information.
;;;;
;;;; While most of SBCL is derived from the CMU CL system, the test
;;;; files (like this one) were written from scratch after the fork
;;;; from CMU CL.
;;;;
;;;; This software is in the public domain and is provided with
;;;; absolutely no warranty. See the COPYING and CREDITS files for
;;;; more information.

(in-package :cl-user)

;;; The unread and clear-input functions on input streams need to
;;; sneak past the old CMU CL encapsulation. As explained by DTC in
;;; the checkin message for his CMU CL patch ca. April 2001,
;;;   These streams encapsulate other input streams which may
;;;   have an input buffer so they need to call unread-char
;;;   and clear-input on the encapsulated stream rather than
;;;   directly calling the encapsulated streams misc method
;;;   as the misc methods are below the layer of the input buffer.
;;;
;;; The code below tests only UNREAD-CHAR. It would be nice to test
;;; CLEAR-INPUT too, but I'm not sure how to do it cleanly and
;;; portably in a noninteractive test. -- WHN 2001-05-05
(defparameter *scratch-file-name* "sbcl-wrapped-stream-test-data.tmp")
(defvar *scratch-file-stream*)
(dolist (scratch-file-length '(1 ; everyone's favorite corner case
                               200123)) ; hopefully much bigger than buffer
  (format t "/SCRATCH-FILE-LENGTH=~W~%" scratch-file-length)
  (with-open-file (s *scratch-file-name* :direction :output)
    (dotimes (i scratch-file-length)
      (write-char #\x s)))
  (dolist (wrap-named-stream-fn
           ;; All kinds of wrapped input streams have the same issue.
           (list (lambda (wrapped-stream-name)
                   (make-synonym-stream wrapped-stream-name))
                 (lambda (wrapped-stream-name)
                   (make-two-way-stream (symbol-value wrapped-stream-name)
                                        *standard-output*))
                 (lambda (wrapped-stream-name)
                   (make-concatenated-stream (symbol-value wrapped-stream-name)
                                             (make-string-input-stream "")))))
    (format t "/WRAP-NAMED-STREAM-FN=~S~%" wrap-named-stream-fn)
    (with-open-file (*scratch-file-stream* *scratch-file-name*
                                           :direction :input)
      (let ((ss (funcall wrap-named-stream-fn '*scratch-file-stream*)))
        (flet ((expect (thing-expected)
                 (let ((thing-found (read-char ss nil nil)))
                   (unless (eql thing-found thing-expected)
                     (error "expected ~S, found ~S"
                            thing-expected thing-found)))))
          (dotimes (i scratch-file-length)
            (expect #\x)
            (unread-char #\y ss)
            (expect #\y)
            (unread-char #\z ss)
            (expect #\z))
          (expect nil))))) ; i.e. end of file
  (delete-file *scratch-file-name*))

(with-open-file (s *scratch-file-name* :direction :output)
  (format s "1234~%"))
(assert
 (string=
  (with-open-file (s *scratch-file-name* :direction :input)
    (let* ((b (make-string 10)))
      (peek-char nil s)
      (read-sequence b s)
      b))
  (format nil "1234")
  :end1 4))
(delete-file *scratch-file-name*)