1999-08-18 Gary Houston <ghouston@easynet.co.uk>
[bpt/guile.git] / test-suite / tests / ports.test
1 ;;;; ports.test --- test suite for Guile I/O ports -*- scheme -*-
2 ;;;; Jim Blandy <jimb@red-bean.com> --- October 1998
3 ;;;;
4 ;;;; Copyright (C) 1998, 1999 Free Software Foundation, Inc.
5 ;;;;
6 ;;;; This program is free software; you can redistribute it and/or modify
7 ;;;; it under the terms of the GNU General Public License as published by
8 ;;;; the Free Software Foundation; either version 2, or (at your option)
9 ;;;; any later version.
10 ;;;;
11 ;;;; This program is distributed in the hope that it will be useful,
12 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;;; GNU General Public License for more details.
15 ;;;;
16 ;;;; You should have received a copy of the GNU General Public License
17 ;;;; along with this software; see the file COPYING. If not, write to
18 ;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
19 ;;;; Boston, MA 02111-1307 USA
20
21 (use-modules (test-suite lib)
22 (ice-9 popen))
23
24 (define (display-line . args)
25 (for-each display args)
26 (newline))
27
28 (define (test-file)
29 (tmpnam))
30
31 \f
32 ;;;; Some general utilities for testing ports.
33
34 ;;; Read from PORT until EOF, and return the result as a string.
35 (define (read-all port)
36 (let loop ((chars '()))
37 (let ((char (read-char port)))
38 (if (eof-object? char)
39 (list->string (reverse! chars))
40 (loop (cons char chars))))))
41
42 (define (read-file filename)
43 (let* ((port (open-input-file filename))
44 (string (read-all port)))
45 (close-port port)
46 string))
47
48 \f
49 ;;;; Normal file ports.
50
51 ;;; Write out an s-expression, and read it back.
52 (catch-test-errors
53 (let ((string '("From fairest creatures we desire increase,"
54 "That thereby beauty's rose might never die,"))
55 (filename (test-file)))
56 (let ((port (open-output-file filename)))
57 (write string port)
58 (close-port port))
59 (let ((port (open-input-file filename)))
60 (let ((in-string (read port)))
61 (pass-if "file: write and read back list of strings"
62 (equal? string in-string)))
63 (close-port port))
64 (delete-file filename)))
65
66 ;;; Write out a string, and read it back a character at a time.
67 (catch-test-errors
68 (let ((string "This is a test string\nwith no newline at the end")
69 (filename (test-file)))
70 (let ((port (open-output-file filename)))
71 (display string port)
72 (close-port port))
73 (let ((in-string (read-file filename)))
74 (pass-if "file: write and read back characters"
75 (equal? string in-string)))
76 (delete-file filename)))
77
78 ;;; Buffered input/output port with seeking.
79 (catch-test-errors
80 (let* ((filename (test-file))
81 (port (open-file filename "w+")))
82 (display "J'Accuse" port)
83 (seek port -1 SEEK_CUR)
84 (pass-if "file: r/w 1"
85 (char=? (read-char port) #\e))
86 (pass-if "file: r/w 2"
87 (eof-object? (read-char port)))
88 (seek port -1 SEEK_CUR)
89 (write-char #\x port)
90 (seek port 7 SEEK_SET)
91 (pass-if "file: r/w 3"
92 (char=? (read-char port) #\x))
93 (seek port -2 SEEK_END)
94 (pass-if "file: r/w 4"
95 (char=? (read-char port) #\s))
96 (delete-file filename)))
97
98 ;;; Unbuffered input/output port with seeking.
99 (catch-test-errors
100 (let* ((filename (test-file))
101 (port (open-file filename "w+0")))
102 (display "J'Accuse" port)
103 (seek port -1 SEEK_CUR)
104 (pass-if "file: ub r/w 1"
105 (char=? (read-char port) #\e))
106 (pass-if "file: ub r/w 2"
107 (eof-object? (read-char port)))
108 (seek port -1 SEEK_CUR)
109 (write-char #\x port)
110 (seek port 7 SEEK_SET)
111 (pass-if "file: ub r/w 3"
112 (char=? (read-char port) #\x))
113 (seek port -2 SEEK_END)
114 (pass-if "file: ub r/w 4"
115 (char=? (read-char port) #\s))
116 (delete-file filename)))
117
118 ;;; unusual characters.
119 (catch-test-errors
120 (let* ((filename (test-file))
121 (port (open-output-file filename)))
122 (display (string #\nul (integer->char 255) (integer->char 128)
123 #\nul) port)
124 (close-port port)
125 (let* ((port (open-input-file filename))
126 (line (read-line port)))
127 (pass-if "file: read back NUL 1"
128 (char=? (string-ref line 0) #\nul))
129 (pass-if "file: read back 255"
130 (char=? (string-ref line 1) (integer->char 255)))
131 (pass-if "file: read back 128"
132 (char=? (string-ref line 2) (integer->char 128)))
133 (pass-if "file: read back NUL 2"
134 (char=? (string-ref line 3) #\nul))
135 (pass-if "file: EOF"
136 (eof-object? (read-char port))))
137 (delete-file filename)))
138
139 \f
140 ;;;; Pipe ports.
141
142 ;;; Run a command, and read its output.
143 (catch-test-errors
144 (let* ((pipe (open-pipe "echo 'Howdy there, partner!'" "r"))
145 (in-string (read-all pipe)))
146 (close-pipe pipe)
147 (pass-if "pipe: read"
148 (equal? in-string "Howdy there, partner!\n"))))
149
150 ;;; Run a command, send some output to it, and see if it worked.
151 (catch-test-errors
152 (let* ((filename (test-file))
153 (pipe (open-pipe (string-append "grep Mommy > " filename) "w")))
154 (display "Now Jimmy lives on a mushroom cloud\n" pipe)
155 (display "Mommy, why does everybody have a bomb?\n" pipe)
156 (close-pipe pipe)
157 (let ((in-string (read-file filename)))
158 (pass-if "pipe: write"
159 (equal? in-string "Mommy, why does everybody have a bomb?\n")))
160 (delete-file filename)))
161
162 \f
163 ;;;; Void ports. These are so trivial we don't test them.
164
165 \f
166 ;;;; String ports.
167
168 (with-test-prefix "string ports"
169
170 ;; Write text to a string port.
171 (catch-test-errors
172 (let* ((string "Howdy there, partner!")
173 (in-string (call-with-output-string
174 (lambda (port)
175 (display string port)
176 (newline port)))))
177 (pass-if "display text"
178 (equal? in-string (string-append string "\n")))))
179
180 ;; Write an s-expression to a string port.
181 (catch-test-errors
182 (let* ((sexpr '("more utterly random text" 1729 #(a vector) 3.1415926))
183 (in-sexpr
184 (call-with-input-string (call-with-output-string
185 (lambda (port)
186 (write sexpr port)))
187 read)))
188 (pass-if "write/read sexpr"
189 (equal? in-sexpr sexpr)))))
190
191 \f
192 ;;;; Soft ports. No tests implemented yet.
193
194 \f
195 ;;;; Generic operations across all port types.
196
197 (let ((port-loop-temp (test-file)))
198
199 ;; Return a list of input ports that all return the same text.
200 ;; We map tests over this list.
201 (define (input-port-list text)
202
203 ;; Create a text file some of the ports will use.
204 (let ((out-port (open-output-file port-loop-temp)))
205 (display text out-port)
206 (close-port out-port))
207
208 (list (open-input-file port-loop-temp)
209 (open-input-pipe (string-append "cat " port-loop-temp))
210 (call-with-input-string text (lambda (x) x))
211 ;; We don't test soft ports at the moment.
212 ))
213
214 (define port-list-names '("file" "pipe" "string"))
215
216 ;; Test the line counter.
217 (define (test-line-counter text second-line final-column)
218 (with-test-prefix "line counter"
219 (let ((ports (input-port-list text)))
220 (for-each
221 (lambda (port port-name)
222 (with-test-prefix port-name
223 (pass-if "at beginning of input"
224 (= (port-line port) 0))
225 (pass-if "read first character"
226 (eqv? (read-char port) #\x))
227 (pass-if "after reading one character"
228 (= (port-line port) 0))
229 (pass-if "read first newline"
230 (eqv? (read-char port) #\newline))
231 (pass-if "after reading first newline char"
232 (= (port-line port) 1))
233 (pass-if "second line read correctly"
234 (equal? (read-line port) second-line))
235 (pass-if "read-line increments line number"
236 (= (port-line port) 2))
237 (pass-if "read-line returns EOF"
238 (let loop ((i 0))
239 (cond
240 ((eof-object? (read-line port)) #t)
241 ((> i 20) #f)
242 (else (loop (+ i 1))))))
243 (pass-if "line count is 5 at EOF"
244 (= (port-line port) 5))
245 (pass-if "column is correct at EOF"
246 (= (port-column port) final-column))))
247 ports port-list-names)
248 (for-each close-port ports)
249 (delete-file port-loop-temp))))
250
251 (catch-test-errors
252 (with-test-prefix "newline"
253 (test-line-counter
254 (string-append "x\n"
255 "He who receives an idea from me, receives instruction\n"
256 "himself without lessening mine; as he who lights his\n"
257 "taper at mine, receives light without darkening me.\n"
258 " --- Thomas Jefferson\n")
259 "He who receives an idea from me, receives instruction"
260 0)))
261
262 (catch-test-errors
263 (with-test-prefix "no newline"
264 (test-line-counter
265 (string-append "x\n"
266 "He who receives an idea from me, receives instruction\n"
267 "himself without lessening mine; as he who lights his\n"
268 "taper at mine, receives light without darkening me.\n"
269 " --- Thomas Jefferson\n"
270 "no newline here")
271 "He who receives an idea from me, receives instruction"
272 15))))
273
274 \f
275 ;;;; testing read-delimited and friends
276
277 (with-test-prefix "read-delimited!"
278 (let ((c (make-string 20 #\!)))
279 (call-with-input-string
280 "defdef\nghighi\n"
281 (lambda (port)
282
283 (read-delimited! "\n" c port 'concat)
284 (pass-if "read-delimited! reads a first line"
285 (string=? c "defdef\n!!!!!!!!!!!!!"))
286
287 (read-delimited! "\n" c port 'concat 3)
288 (pass-if "read-delimited! reads a first line"
289 (string=? c "defghighi\n!!!!!!!!!!"))))))
290
291 \f
292 ;;;; char-ready?
293
294 (call-with-input-string
295 "howdy"
296 (lambda (port)
297 (pass-if "char-ready? returns true on string port"
298 (char-ready? port))))
299
300 ;;; This segfaults on some versions of Guile. We really should run
301 ;;; the tests in a subprocess...
302
303 (call-with-input-string
304 "howdy"
305 (lambda (port)
306 (with-input-from-port
307 port
308 (lambda ()
309 (pass-if "char-ready? returns true on string port as default port"
310 (char-ready?))))))