make-two-way-stream.lsp 7.44 KB
Newer Older
1 2 3 4 5 6 7 8 9
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Fri Jan 30 05:39:56 2004
;;;; Contains: Tests for MAKE-TWO-WAY-STREAM

(in-package :cl-test)

(deftest make-two-way-stream.1
  (let* ((is (make-string-input-stream "foo"))
sds's avatar
sds committed
10 11
         (os (make-string-output-stream))
         (s (make-two-way-stream is os)))
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
    (assert (typep s 'stream))
    (assert (typep s 'two-way-stream))
    (assert (streamp s))
    (assert (open-stream-p s))
    (assert (input-stream-p s))
    (assert (output-stream-p s))
    (assert (stream-element-type s))
    (values
     (read-char s)
     (write-char #\b s)
     (read-char s)
     (write-char #\a s)
     (read-char s)
     (write-char #\r s)
     (get-output-stream-string os)))
  #\f #\b #\o #\a #\o #\r "bar")

29 30
(deftest make-two-way-stream.2
   (let* ((is (make-string-input-stream "foo"))
sds's avatar
sds committed
31 32
          (os (make-string-output-stream))
          (s (make-two-way-stream is os)))
33 34 35 36 37 38 39 40 41
     (values
      (close s)
      (open-stream-p s)
      (notnot (open-stream-p is))
      (notnot (open-stream-p os))
      (write-char #\8 os)
      (get-output-stream-string os)))
   t nil t t #\8 "8")

42 43
(deftest make-two-way-stream.3
   (let* ((is (make-string-input-stream "foo"))
sds's avatar
sds committed
44 45
          (os (make-string-output-stream))
          (s (make-two-way-stream is os)))
46 47 48 49 50 51 52 53
     (values
      (peek-char nil s)
      (read-char s)
      (get-output-stream-string os)))
   #\f #\f "")

(deftest make-two-way-stream.4
   (let* ((is (make-string-input-stream "foo"))
sds's avatar
sds committed
54 55
          (os (make-string-output-stream))
          (s (make-two-way-stream is os)))
56 57 58 59 60 61 62 63 64 65
     (values
      (read-char-no-hang s)
      (read-char-no-hang s nil)
      (read-char-no-hang s t :eof)
      (read-char-no-hang s nil :eof)
      (get-output-stream-string os)))
   #\f #\o #\o :eof "")

(deftest make-two-way-stream.5
   (let* ((is (make-string-input-stream "foo"))
sds's avatar
sds committed
66 67
          (os (make-string-output-stream))
          (s (make-two-way-stream is os)))
68 69 70 71 72 73 74
     (values
      (terpri s)
      (get-output-stream-string os)))
   nil #.(string #\Newline))

(deftest make-two-way-stream.6
   (let* ((is (make-string-input-stream "foo"))
sds's avatar
sds committed
75 76
          (os (make-string-output-stream))
          (s (make-two-way-stream is os)))
77 78 79 80 81 82 83 84 85
     (values
      (write-char #\+ s)
      (notnot (fresh-line s))
      (read-char s)
      (get-output-stream-string os)))
   #\+ t #\f #.(coerce (list #\+ #\Newline) 'string))

(deftest make-two-way-stream.7
   (let* ((is (make-string-input-stream "foo"))
sds's avatar
sds committed
86 87
          (os (make-string-output-stream))
          (s (make-two-way-stream is os)))
88 89 90 91 92 93 94 95 96 97 98
     (values
      (read-char s)
      (unread-char #\f s)
      (read-char s)
      (read-char s)
      (unread-char #\o s)
      (get-output-stream-string os)))
   #\f nil #\f #\o nil "")

(deftest make-two-way-stream.8
   (let* ((is (make-string-input-stream "foo"))
sds's avatar
sds committed
99 100
          (os (make-string-output-stream))
          (s (make-two-way-stream is os)))
101 102 103 104 105 106 107
     (values
      (read-line s)
      (get-output-stream-string os)))
   "foo" "")

(deftest make-two-way-stream.9
   (let* ((is (make-string-input-stream "foo"))
sds's avatar
sds committed
108 109
          (os (make-string-output-stream))
          (s (make-two-way-stream is os)))
110 111 112 113 114 115 116
     (values
      (write-string "bar" s)
      (get-output-stream-string os)))
   "bar" "bar")

(deftest make-two-way-stream.10
   (let* ((is (make-string-input-stream "foo"))
sds's avatar
sds committed
117 118
          (os (make-string-output-stream))
          (s (make-two-way-stream is os)))
119 120 121 122 123 124 125
     (values
      (write-line "bar" s)
      (get-output-stream-string os)))
   "bar" #.(concatenate 'string "bar" '(#\Newline)))

(deftest make-two-way-stream.11
  (let* ((is (make-string-input-stream "foo"))
sds's avatar
sds committed
126 127
          (os (make-string-output-stream))
          (s (make-two-way-stream is os)))
128 129 130 131 132 133 134 135 136
    (let ((x (vector nil nil nil)))
     (values
      (read-sequence x s)
      x
      (get-output-stream-string os))))
  3 #(#\f #\o #\o) "")

(deftest make-two-way-stream.12
  (let ((pn1 #p"tmp.dat")
sds's avatar
sds committed
137 138
        (pn2 #p"tmp2.dat")
        (element-type '(unsigned-byte 8)))
139
    (with-open-file (s pn1 :direction :output :if-exists :supersede
sds's avatar
sds committed
140 141
                       :element-type element-type)
                    (dolist (b '(3 8 19 41)) (write-byte b s)))
142 143 144 145
    (with-open-file
     (is pn1 :direction :input :element-type element-type)
     (with-open-file
      (os pn2 :direction :output :element-type element-type
sds's avatar
sds committed
146
          :if-exists :supersede)
147
      (let ((s (make-two-way-stream is os))
sds's avatar
sds committed
148 149 150 151 152 153
            (x (vector nil nil nil nil)))
        (assert (eql (read-sequence x s) 4))
        (assert (equalp x #(3 8 19 41)))
        (let ((y #(100 5 18 211 0 178)))
          (assert (eql (write-sequence y s) y))
          (close s)))))
154 155 156 157
    (with-open-file
     (s pn2 :direction :input :element-type element-type)
     (let ((x (vector nil nil nil nil nil nil nil)))
       (values
sds's avatar
sds committed
158 159
        (read-sequence x s)
        x))))
160 161 162 163 164
  6
  #(100 5 18 211 0 178 nil))

(deftest make-two-way-stream.13
  (let ((pn1 #p"tmp.dat")
sds's avatar
sds committed
165 166
        (pn2 #p"tmp2.dat")
        (element-type '(unsigned-byte 32)))
167
    (with-open-file (s pn1 :direction :output :if-exists :supersede
sds's avatar
sds committed
168 169
                       :element-type element-type)
                    (dolist (b '(3 8 19 41)) (write-byte b s)))
170 171 172 173
    (with-open-file
     (is pn1 :direction :input :element-type element-type)
     (with-open-file
      (os pn2 :direction :output :element-type element-type
sds's avatar
sds committed
174
          :if-exists :supersede)
175
      (let ((s (make-two-way-stream is os))
sds's avatar
sds committed
176 177 178 179 180 181
            (x (vector nil nil nil nil)))
        (assert (eql (read-sequence x s) 4))
        (assert (equalp x #(3 8 19 41)))
        (let ((y #(100 5 18 211 0 178)))
          (assert (eql (write-sequence y s) y))
          (close s)))))
182 183 184 185
    (with-open-file
     (s pn2 :direction :input :element-type element-type)
     (let ((x (vector nil nil nil nil nil nil nil)))
       (values
sds's avatar
sds committed
186 187
        (read-sequence x s)
        x))))
188 189 190 191 192
  6
  #(100 5 18 211 0 178 nil))

(deftest make-two-way-stream.14
  (let* ((is (make-string-input-stream "foo"))
sds's avatar
sds committed
193 194
         (os (make-string-output-stream))
         (s (make-two-way-stream is os)))
195 196 197 198 199 200
    (values
     (write-string "abc" s)
     (clear-input s)
     (write-string "def" s)
     (get-output-stream-string os)))
  "abc" nil "def" "abcdef")
201 202 203 204 205 206 207 208 209

;;; Error tests

(deftest make-two-way-stream.error.1
  (signals-error (make-two-way-stream) program-error)
  t)

(deftest make-two-way-stream.error.2
  (signals-error (make-two-way-stream (make-string-input-stream "foo"))
sds's avatar
sds committed
210
                 program-error)
211 212 213 214
  t)

(deftest make-two-way-stream.error.3
  (signals-error (let ((os (make-string-output-stream)))
sds's avatar
sds committed
215 216 217
                   (make-two-way-stream (make-string-input-stream "foo")
                                        os nil))
                 program-error)
218 219 220
  t)

(deftest make-two-way-stream.error.4
pfdietz's avatar
pfdietz committed
221
  (check-type-error #'(lambda (x) (make-two-way-stream x (make-string-output-stream)))
sds's avatar
sds committed
222
                    #'(lambda (x) (and (streamp x) (input-stream-p x))))
223 224 225
  nil)

(deftest make-two-way-stream.error.5
pfdietz's avatar
pfdietz committed
226
  (check-type-error #'(lambda (x) (make-two-way-stream x (make-string-output-stream)))
sds's avatar
sds committed
227 228
                    #'(lambda (x) (and (streamp x) (input-stream-p x)))
                    *streams*)
229 230 231
  nil)

(deftest make-two-way-stream.error.6
pfdietz's avatar
pfdietz committed
232
  (check-type-error #'(lambda (x) (make-two-way-stream (make-string-input-stream "foo") x))
sds's avatar
sds committed
233
                    #'(lambda (x) (and (streamp x) (output-stream-p x))))
234 235 236
  nil)

(deftest make-two-way-stream.error.7
pfdietz's avatar
pfdietz committed
237
  (check-type-error #'(lambda (x) (make-two-way-stream (make-string-input-stream "foo") x))
sds's avatar
sds committed
238 239
                    #'(lambda (x) (and (streamp x) (output-stream-p x)))
                    *streams*)
240 241 242 243
  nil)



sds's avatar
sds committed
244