3 more bytes.
[bpt/guile.git] / test-suite / tests / ports.test
CommitLineData
0d572e91 1;;;; ports.test --- test suite for Guile I/O ports -*- scheme -*-
7ef450bf 2;;;; Jim Blandy <jimb@red-bean.com> --- May 1999
000ee07f 3;;;;
7ef450bf 4;;;; Copyright (C) 1999 Free Software Foundation, Inc.
000ee07f
JB
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
f5d4dde3
JB
21(use-modules (test-suite lib)
22 (ice-9 popen))
000ee07f
JB
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.
0d572e91
JB
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)))
000ee07f
JB
65
66;;; Write out a string, and read it back a character at a time.
0d572e91
JB
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)))
000ee07f 77
7c035009
GH
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)
75efe453 83 (seek port -1 SEEK_CUR)
7c035009
GH
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)))
75efe453 88 (seek port -1 SEEK_CUR)
7c035009 89 (write-char #\x port)
75efe453 90 (seek port 7 SEEK_SET)
7c035009
GH
91 (pass-if "file: r/w 3"
92 (char=? (read-char port) #\x))
75efe453 93 (seek port -2 SEEK_END)
7c035009
GH
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)
75efe453 103 (seek port -1 SEEK_CUR)
7c035009
GH
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)))
75efe453 108 (seek port -1 SEEK_CUR)
7c035009 109 (write-char #\x port)
75efe453 110 (seek port 7 SEEK_SET)
7c035009
GH
111 (pass-if "file: ub r/w 3"
112 (char=? (read-char port) #\x))
75efe453 113 (seek port -2 SEEK_END)
7c035009
GH
114 (pass-if "file: ub r/w 4"
115 (char=? (read-char port) #\s))
116 (delete-file filename)))
117
7f214e60
GH
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
0eb2e8cd
GH
139;;; line buffering mode.
140(catch-test-errors
141 (let* ((filename (test-file))
142 (port (open-file filename "wl"))
143 (test-string "one line more or less"))
144 (write-line test-string port)
145 (let* ((in-port (open-input-file filename))
146 (line (read-line in-port)))
147 (close-port in-port)
148 (close-port port)
149 (pass-if "file: line buffering"
150 (string=? line test-string)))
151 (delete-file filename)))
152
d1b143e9
GH
153;;; ungetting characters and strings.
154(catch-test-errors
155 (with-input-from-string "walk on the moon\nmoon"
156 (lambda ()
157 (read-char)
158 (unread-char #\a (current-input-port))
159 (pass-if "unread-char"
160 (char=? (read-char) #\a))
161 (read-line)
162 (let ((replacenoid "chicken enchilada"))
163 (unread-char #\newline (current-input-port))
164 (unread-string replacenoid (current-input-port))
165 (pass-if "unread-string"
166 (string=? (read-line) replacenoid)))
167 (pass-if "unread residue"
168 (string=? (read-line) "moon")))))
169
6e822cce
GH
170;;; non-blocking mode on a port. create a pipe and set O_NONBLOCK on
171;;; the reading end. try to read a byte: should get EAGAIN error.
172(catch-test-errors
173 (let* ((p (pipe))
174 (r (car p)))
175 (fcntl r F_SETFL O_NONBLOCK)
176 (pass-if "non-blocking-I/O"
177 (catch 'system-error
1ba57e89 178 (lambda () (read-char r) #f)
6e822cce
GH
179 (lambda (key . args)
180 (and (eq? key 'system-error)
181 (= (car (list-ref args 3)) EAGAIN)))))))
182
000ee07f 183\f
6e822cce 184;;;; Pipe (popen) ports.
000ee07f
JB
185
186;;; Run a command, and read its output.
0d572e91
JB
187(catch-test-errors
188 (let* ((pipe (open-pipe "echo 'Howdy there, partner!'" "r"))
189 (in-string (read-all pipe)))
f5d4dde3 190 (close-pipe pipe)
0d572e91
JB
191 (pass-if "pipe: read"
192 (equal? in-string "Howdy there, partner!\n"))))
000ee07f
JB
193
194;;; Run a command, send some output to it, and see if it worked.
0d572e91
JB
195(catch-test-errors
196 (let* ((filename (test-file))
197 (pipe (open-pipe (string-append "grep Mommy > " filename) "w")))
198 (display "Now Jimmy lives on a mushroom cloud\n" pipe)
199 (display "Mommy, why does everybody have a bomb?\n" pipe)
f5d4dde3 200 (close-pipe pipe)
0d572e91
JB
201 (let ((in-string (read-file filename)))
202 (pass-if "pipe: write"
203 (equal? in-string "Mommy, why does everybody have a bomb?\n")))
204 (delete-file filename)))
000ee07f
JB
205
206\f
207;;;; Void ports. These are so trivial we don't test them.
208
209\f
210;;;; String ports.
211
73cb0a97
JB
212(with-test-prefix "string ports"
213
214 ;; Write text to a string port.
215 (catch-test-errors
216 (let* ((string "Howdy there, partner!")
217 (in-string (call-with-output-string
218 (lambda (port)
219 (display string port)
220 (newline port)))))
221 (pass-if "display text"
222 (equal? in-string (string-append string "\n")))))
000ee07f 223
73cb0a97
JB
224 ;; Write an s-expression to a string port.
225 (catch-test-errors
226 (let* ((sexpr '("more utterly random text" 1729 #(a vector) 3.1415926))
227 (in-sexpr
228 (call-with-input-string (call-with-output-string
229 (lambda (port)
230 (write sexpr port)))
231 read)))
232 (pass-if "write/read sexpr"
233 (equal? in-sexpr sexpr)))))
000ee07f
JB
234
235\f
236;;;; Soft ports. No tests implemented yet.
237
238\f
239;;;; Generic operations across all port types.
240
241(let ((port-loop-temp (test-file)))
242
243 ;; Return a list of input ports that all return the same text.
244 ;; We map tests over this list.
245 (define (input-port-list text)
246
247 ;; Create a text file some of the ports will use.
248 (let ((out-port (open-output-file port-loop-temp)))
249 (display text out-port)
250 (close-port out-port))
251
252 (list (open-input-file port-loop-temp)
253 (open-input-pipe (string-append "cat " port-loop-temp))
254 (call-with-input-string text (lambda (x) x))
255 ;; We don't test soft ports at the moment.
256 ))
257
258 (define port-list-names '("file" "pipe" "string"))
259
260 ;; Test the line counter.
73cb0a97 261 (define (test-line-counter text second-line final-column)
000ee07f
JB
262 (with-test-prefix "line counter"
263 (let ((ports (input-port-list text)))
264 (for-each
265 (lambda (port port-name)
266 (with-test-prefix port-name
267 (pass-if "at beginning of input"
268 (= (port-line port) 0))
269 (pass-if "read first character"
270 (eqv? (read-char port) #\x))
271 (pass-if "after reading one character"
272 (= (port-line port) 0))
273 (pass-if "read first newline"
274 (eqv? (read-char port) #\newline))
275 (pass-if "after reading first newline char"
276 (= (port-line port) 1))
277 (pass-if "second line read correctly"
278 (equal? (read-line port) second-line))
279 (pass-if "read-line increments line number"
280 (= (port-line port) 2))
0b8faa0e
JB
281 (pass-if "read-line returns EOF"
282 (let loop ((i 0))
283 (cond
284 ((eof-object? (read-line port)) #t)
285 ((> i 20) #f)
286 (else (loop (+ i 1))))))
000ee07f 287 (pass-if "line count is 5 at EOF"
73cb0a97
JB
288 (= (port-line port) 5))
289 (pass-if "column is correct at EOF"
290 (= (port-column port) final-column))))
000ee07f
JB
291 ports port-list-names)
292 (for-each close-port ports)
293 (delete-file port-loop-temp))))
294
0d572e91
JB
295 (catch-test-errors
296 (with-test-prefix "newline"
297 (test-line-counter
298 (string-append "x\n"
299 "He who receives an idea from me, receives instruction\n"
300 "himself without lessening mine; as he who lights his\n"
301 "taper at mine, receives light without darkening me.\n"
302 " --- Thomas Jefferson\n")
73cb0a97
JB
303 "He who receives an idea from me, receives instruction"
304 0)))
0d572e91
JB
305
306 (catch-test-errors
307 (with-test-prefix "no newline"
308 (test-line-counter
309 (string-append "x\n"
310 "He who receives an idea from me, receives instruction\n"
311 "himself without lessening mine; as he who lights his\n"
312 "taper at mine, receives light without darkening me.\n"
313 " --- Thomas Jefferson\n"
314 "no newline here")
73cb0a97
JB
315 "He who receives an idea from me, receives instruction"
316 15))))
5bc1201f
JB
317
318\f
319;;;; testing read-delimited and friends
320
321(with-test-prefix "read-delimited!"
322 (let ((c (make-string 20 #\!)))
323 (call-with-input-string
324 "defdef\nghighi\n"
325 (lambda (port)
326
327 (read-delimited! "\n" c port 'concat)
328 (pass-if "read-delimited! reads a first line"
329 (string=? c "defdef\n!!!!!!!!!!!!!"))
330
331 (read-delimited! "\n" c port 'concat 3)
332 (pass-if "read-delimited! reads a first line"
333 (string=? c "defghighi\n!!!!!!!!!!"))))))
1b054952
JB
334
335\f
336;;;; char-ready?
337
338(call-with-input-string
339 "howdy"
340 (lambda (port)
341 (pass-if "char-ready? returns true on string port"
342 (char-ready? port))))
343
344;;; This segfaults on some versions of Guile. We really should run
345;;; the tests in a subprocess...
346
347(call-with-input-string
348 "howdy"
349 (lambda (port)
350 (with-input-from-port
351 port
352 (lambda ()
353 (pass-if "char-ready? returns true on string port as default port"
354 (char-ready?))))))