source: trunk/source/tests/ansi-tests/make-echo-stream.lsp @ 8991

Last change on this file since 8991 was 8991, checked in by gz, 11 years ago

Check in the gcl ansi test suite (original, in preparation for making local changes)

File size: 9.0 KB
Line 
1;-*- Mode:     Lisp -*-
2;;;; Author:   Paul Dietz
3;;;; Created:  Thu Feb 12 04:34:42 2004
4;;;; Contains: Tests of MAKE-ECHO-STREAM
5
6(in-package :cl-test)
7
8(deftest make-echo-stream.1
9  (let* ((is (make-string-input-stream "foo"))
10         (os (make-string-output-stream))
11         (s (make-echo-stream is os)))
12    (values
13     (read-char s)
14     (get-output-stream-string os)))
15  #\f "f")
16
17(deftest make-echo-stream.2
18  (let* ((is (make-string-input-stream "foo"))
19         (os (make-string-output-stream))
20         (s (make-echo-stream is os)))
21    (get-output-stream-string os))
22  "")
23
24(deftest make-echo-stream.3
25  (let* ((is (make-string-input-stream "foo"))
26         (os (make-string-output-stream))
27         (s (make-echo-stream is os)))
28    (values (read-line s nil)
29            (get-output-stream-string os)))
30  "foo" "foo")
31
32;;; Tests of READ-BYTE on echo streams
33
34(deftest make-echo-stream.4
35  (let ((pn #p"tmp.dat")
36        (pn2 #p"tmp2.dat")
37        (element-type '(unsigned-byte 8)))
38    (with-open-file (os pn
39                        :direction :output
40                        :element-type element-type
41                        :if-exists :supersede)
42                    (loop for x in '(2 3 5 7 11)
43                          do (write-byte x os)))
44    (with-open-file
45     (is pn :direction :input :element-type element-type)
46     (values
47      (with-open-file
48       (os pn2 :direction :output :if-exists :supersede
49           :element-type element-type)
50       (let ((s (make-echo-stream is os)))
51         (loop repeat 6 collect (read-byte s nil :eof1))))
52      (with-open-file
53       (s pn2 :direction :input :element-type element-type)
54       (loop repeat 6 collect (read-byte s nil :eof2))))))
55  (2 3 5 7 11 :eof1)
56  (2 3 5 7 11 :eof2))
57
58(deftest make-echo-stream.5
59  (let ((pn #p"tmp.dat")
60        (pn2 #p"tmp2.dat")
61        (element-type '(unsigned-byte 8)))
62    (with-open-file (os pn
63                        :direction :output
64                        :element-type element-type
65                        :if-exists :supersede)
66                    (loop for x in '(2 3 5 7 11)
67                          do (write-byte x os)))
68    (with-open-file
69     (is pn :direction :input :element-type element-type)
70     (values
71      (with-open-file
72       (os pn2 :direction :output :if-exists :supersede
73           :element-type element-type)
74       (let ((s (make-echo-stream is os)))
75         (loop repeat 6 collect (read-byte s nil 100))))
76      (with-open-file
77       (s pn2 :direction :input :element-type element-type)
78       (loop repeat 6 collect (read-byte s nil 200))))))
79  (2 3 5 7 11 100)
80  (2 3 5 7 11 200))
81
82(deftest make-echo-stream.6
83  (let* ((is (make-string-input-stream "foo"))
84         (os (make-string-output-stream))
85         (s (make-echo-stream is os)))
86    (values (coerce (loop repeat 3 collect (read-char-no-hang s)) 'string)
87            (get-output-stream-string os)))
88  "foo" "foo")
89
90(deftest make-echo-stream.7
91  (let* ((is (make-string-input-stream "foo"))
92         (os (make-string-output-stream))
93         (s (make-echo-stream is os)))
94    (values (coerce (loop repeat 4 collect (read-char-no-hang s nil '#\z))
95                    'string)
96            (get-output-stream-string os)))
97  "fooz" "foo")
98
99;;; peek-char + echo streams is tested in peek-char.lsp
100;;; unread-char + echo streams is tested in unread-char.lsp
101
102(deftest make-echo-stream.8
103  (let* ((is (make-string-input-stream "foo"))
104         (os (make-string-output-stream))
105         (s (make-echo-stream is os))
106         (x (copy-seq "xxxxxx")))
107    (values
108     (read-sequence x s)
109     x
110     (get-output-stream-string os)))
111  3
112  "fooxxx"
113  "foo")
114
115(deftest make-echo-stream.9
116  (let ((pn #p"tmp.dat")
117        (pn2 #p"tmp2.dat")
118        (element-type '(unsigned-byte 8)))
119    (with-open-file (os pn
120                        :direction :output
121                        :element-type element-type
122                        :if-exists :supersede)
123                    (loop for x in '(2 3 5 7 11)
124                          do (write-byte x os)))
125    (with-open-file
126     (is pn :direction :input :element-type element-type)
127     (values
128      (with-open-file
129       (os pn2 :direction :output :if-exists :supersede
130           :element-type element-type)
131       (let ((s (make-echo-stream is os))
132             (x (vector 0 0 0 0 0 0 0 0)))
133         (list (read-sequence x s)
134               x)))
135      (with-open-file
136       (s pn2 :direction :input :element-type element-type)
137       (loop repeat 8 collect (read-byte s nil nil))))))
138  (5 #(2 3 5 7 11 0 0 0))
139  (2 3 5 7 11 nil nil nil))
140
141(deftest make-echo-stream.10
142  (let* ((is (make-string-input-stream "foo"))
143         (os (make-string-output-stream))
144         (s (make-echo-stream is os)))
145    (values
146     (notnot (open-stream-p s))
147     (close s)
148     (open-stream-p s)
149     (notnot (open-stream-p is))
150     (notnot (open-stream-p os))))
151  t t nil t t)
152
153(deftest make-echo-stream.11
154  (let* ((is (make-string-input-stream "foo"))
155         (os (make-string-output-stream))
156         (s (make-echo-stream is os)))
157    (values
158     (notnot (listen s))
159     (read-char s)
160     (notnot (listen s))
161     (read-char s)
162     (notnot (listen s))
163     (read-char s)
164     (listen s)))
165  t #\f t #\o t #\o nil)
166
167(deftest make-echo-stream.12
168  (let* ((is (make-string-input-stream "foo"))
169         (os (make-string-output-stream))
170         (s (make-echo-stream is os)))
171    (values
172     (notnot (streamp s))
173     (notnot (typep s 'stream))
174     (notnot (typep s 'echo-stream))
175     (notnot (input-stream-p s))
176     (notnot (output-stream-p s))
177     (notnot (stream-element-type s))))
178  t t t t t t)
179
180;;; FIXME
181;;; Add tests for clear-input, file-position(?)
182;;;  Also, add tests for output operations (since echo-streams are
183;;;   bidirectional)
184
185(deftest make-echo-stream.13
186  (let* ((is (make-string-input-stream "foo"))
187         (os (make-string-output-stream))
188         (s (make-echo-stream is os)))
189    (values
190     (write-char #\0 s)
191     (close s)
192     (get-output-stream-string os)))
193  #\0 t "0")
194
195(deftest make-echo-stream.14
196  (let* ((is (make-string-input-stream "foo"))
197         (os (make-string-output-stream))
198         (s (make-echo-stream is os)))
199    (values
200     (terpri s)
201     (close s)
202     (get-output-stream-string os)))
203  nil t #.(string #\Newline))
204
205(deftest make-echo-stream.15
206  (let ((pn #p"tmp.dat")
207        (pn2 #p"tmp2.dat")
208        (element-type '(unsigned-byte 8)))
209    (with-open-file (os pn
210                        :direction :output
211                        :element-type element-type
212                        :if-exists :supersede))
213    (with-open-file
214     (is pn :direction :input :element-type element-type)
215     (values
216      (with-open-file
217       (os pn2 :direction :output :if-exists :supersede
218           :element-type element-type)
219       (let ((s (make-echo-stream is os))
220             (x (mapcar #'char-code (coerce "abcdefg" 'list))))
221         (loop for b in x do
222               (assert (equal (list b)
223                              (multiple-value-list (write-byte b s)))))
224         (close s)))))
225    (with-open-file
226     (is pn2 :direction :input :element-type element-type)
227     (let ((x (vector 0 0 0 0 0 0 0)))
228       (read-sequence x is)
229       (values
230        (read-byte is nil :done)
231        (map 'string #'code-char x)))))
232  :done
233  "abcdefg")
234
235(deftest make-echo-stream.16
236  (let ((pn #p"tmp.dat")
237        (pn2 #p"tmp2.dat")
238        (element-type '(unsigned-byte 8)))
239    (with-open-file (os pn
240                        :direction :output
241                        :element-type element-type
242                        :if-exists :supersede))
243    (with-open-file
244     (is pn :direction :input :element-type element-type)
245     (values
246      (with-open-file
247       (os pn2 :direction :output :if-exists :supersede
248           :element-type element-type)
249       (let ((s (make-echo-stream is os))
250             (x (map 'vector #'char-code "abcdefg")))
251         (assert (equal (multiple-value-list (write-sequence x s)) (list x)))
252         (close s)))))
253    (with-open-file
254     (is pn2 :direction :input :element-type element-type)
255     (let ((x (vector 0 0 0 0 0 0 0)))
256       (read-sequence x is)
257       (values
258        (read-byte is nil :done)
259        (map 'string #'code-char x)))))
260  :done
261  "abcdefg")
262
263(deftest make-echo-stream.17
264  (let* ((is (make-string-input-stream "foo"))
265         (os (make-string-output-stream))
266         (s (make-echo-stream is os)))
267    (values
268     (write-char #\X s)
269     (notnot (fresh-line s))
270     (finish-output s)
271     (force-output s)
272     (close s)
273     (get-output-stream-string os)))
274 #\X t nil nil t #.(coerce '(#\X #\Newline) 'string))
275
276(deftest make-echo-stream.18
277  (let* ((is (make-string-input-stream "foo"))
278         (os (make-string-output-stream))
279         (s (make-echo-stream is os)))
280    (values
281     (write-string "159" s)
282     (close s)
283     (get-output-stream-string os)))
284  "159" t "159")
285
286(deftest make-echo-stream.20
287  (let* ((is (make-string-input-stream "foo"))
288         (os (make-string-output-stream))
289         (s (make-echo-stream is os)))
290    (values
291     (write-string "0159X" s :start 1 :end 4)
292     (close s)
293     (get-output-stream-string os)))
294  "0159X" t "159")
295
296(deftest make-echo-stream.21
297  (let* ((is (make-string-input-stream "foo"))
298         (os (make-string-output-stream))
299         (s (make-echo-stream is os)))
300    (values
301     (write-line "159" s)
302     (close s)
303     (get-output-stream-string os)))
304  "159" t #.(concatenate 'string "159" (string #\Newline)))
305
306(deftest make-echo-stream.22
307  (let* ((is (make-string-input-stream "foo"))
308         (os (make-string-output-stream))
309         (s (make-echo-stream is os)))
310    (values
311     (write-char #\0 s)
312     (clear-output s)))
313  #\0 nil)
314
315;;; Error tests
316
317(deftest make-echo-stream.error.1
318  (signals-error (make-echo-stream) program-error)
319  t)
320
321(deftest make-echo-stream.error.2
322  (signals-error (make-echo-stream *standard-input*) program-error)
323  t)
324
325(deftest make-echo-stream.error.3
326  (signals-error (make-echo-stream *standard-input* *standard-output* nil)
327                 program-error)
328  t)
329
330
331
332
Note: See TracBrowser for help on using the repository browser.