* tests/ports.test (non-blocking-I/O): a couple more details:
[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> --- May 1999
3 ;;;;
4 ;;;; Copyright (C) 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 ;;; Buffered output-only and input-only ports with seeking.
119 (catch-test-errors
120 (let* ((filename (test-file))
121 (port (open-output-file filename)))
122 (display "J'Accuse" port)
123 (pass-if "file: out tell"
124 (= (seek port 0 SEEK_CUR) 8))
125 (seek port -1 SEEK_CUR)
126 (write-char #\x port)
127 (close-port port)
128 (let ((iport (open-input-file filename)))
129 (pass-if "file: in tell 0"
130 (= (seek iport 0 SEEK_CUR) 0))
131 (read-char iport)
132 (pass-if "file: in tell 1"
133 (= (seek iport 0 SEEK_CUR) 1))
134 (unread-char #\z iport)
135 (pass-if "file: in tell 0 after unread"
136 (= (seek iport 0 SEEK_CUR) 0))
137 (pass-if "file: unread char still there"
138 (char=? (read-char iport) #\z))
139 (seek iport 7 SEEK_SET)
140 (pass-if "file: in last char"
141 (char=? (read-char iport) #\x))
142 (close-port iport))
143 (delete-file filename)))
144
145 ;;; unusual characters.
146 (catch-test-errors
147 (let* ((filename (test-file))
148 (port (open-output-file filename)))
149 (display (string #\nul (integer->char 255) (integer->char 128)
150 #\nul) port)
151 (close-port port)
152 (let* ((port (open-input-file filename))
153 (line (read-line port)))
154 (pass-if "file: read back NUL 1"
155 (char=? (string-ref line 0) #\nul))
156 (pass-if "file: read back 255"
157 (char=? (string-ref line 1) (integer->char 255)))
158 (pass-if "file: read back 128"
159 (char=? (string-ref line 2) (integer->char 128)))
160 (pass-if "file: read back NUL 2"
161 (char=? (string-ref line 3) #\nul))
162 (pass-if "file: EOF"
163 (eof-object? (read-char port))))
164 (delete-file filename)))
165
166 ;;; line buffering mode.
167 (catch-test-errors
168 (let* ((filename (test-file))
169 (port (open-file filename "wl"))
170 (test-string "one line more or less"))
171 (write-line test-string port)
172 (let* ((in-port (open-input-file filename))
173 (line (read-line in-port)))
174 (close-port in-port)
175 (close-port port)
176 (pass-if "file: line buffering"
177 (string=? line test-string)))
178 (delete-file filename)))
179
180 ;;; ungetting characters and strings.
181 (catch-test-errors
182 (with-input-from-string "walk on the moon\nmoon"
183 (lambda ()
184 (read-char)
185 (unread-char #\a (current-input-port))
186 (pass-if "unread-char"
187 (char=? (read-char) #\a))
188 (read-line)
189 (let ((replacenoid "chicken enchilada"))
190 (unread-char #\newline (current-input-port))
191 (unread-string replacenoid (current-input-port))
192 (pass-if "unread-string"
193 (string=? (read-line) replacenoid)))
194 (pass-if "unread residue"
195 (string=? (read-line) "moon")))))
196
197 ;;; non-blocking mode on a port. create a pipe and set O_NONBLOCK on
198 ;;; the reading end. try to read a byte: should get EAGAIN or
199 ;;; EWOULDBLOCK error.
200 (catch-test-errors
201 (let* ((p (pipe))
202 (r (car p)))
203 (fcntl r F_SETFL (logior (fcntl r F_GETFL) O_NONBLOCK))
204 (pass-if "non-blocking-I/O"
205 (catch 'system-error
206 (lambda () (read-char r) #f)
207 (lambda (key . args)
208 (and (eq? key 'system-error)
209 (let ((errno (car (list-ref args 3))))
210 (or (= errno EAGAIN)
211 (= errno EWOULDBLOCK)))))))))
212 \f
213 ;;;; Pipe (popen) ports.
214
215 ;;; Run a command, and read its output.
216 (catch-test-errors
217 (let* ((pipe (open-pipe "echo 'Howdy there, partner!'" "r"))
218 (in-string (read-all pipe)))
219 (close-pipe pipe)
220 (pass-if "pipe: read"
221 (equal? in-string "Howdy there, partner!\n"))))
222
223 ;;; Run a command, send some output to it, and see if it worked.
224 (catch-test-errors
225 (let* ((filename (test-file))
226 (pipe (open-pipe (string-append "grep Mommy > " filename) "w")))
227 (display "Now Jimmy lives on a mushroom cloud\n" pipe)
228 (display "Mommy, why does everybody have a bomb?\n" pipe)
229 (close-pipe pipe)
230 (let ((in-string (read-file filename)))
231 (pass-if "pipe: write"
232 (equal? in-string "Mommy, why does everybody have a bomb?\n")))
233 (delete-file filename)))
234
235 \f
236 ;;;; Void ports. These are so trivial we don't test them.
237
238 \f
239 ;;;; String ports.
240
241 (with-test-prefix "string ports"
242
243 ;; Write text to a string port.
244 (catch-test-errors
245 (let* ((string "Howdy there, partner!")
246 (in-string (call-with-output-string
247 (lambda (port)
248 (display string port)
249 (newline port)))))
250 (pass-if "display text"
251 (equal? in-string (string-append string "\n")))))
252
253 ;; Write an s-expression to a string port.
254 (catch-test-errors
255 (let* ((sexpr '("more utterly random text" 1729 #(a vector) 3.1415926))
256 (in-sexpr
257 (call-with-input-string (call-with-output-string
258 (lambda (port)
259 (write sexpr port)))
260 read)))
261 (pass-if "write/read sexpr"
262 (equal? in-sexpr sexpr))))
263
264 ;; seeking and unreading from an input string.
265 (catch-test-errors
266 (let ((text "that text didn't look random to me"))
267 (call-with-input-string text
268 (lambda (p)
269 (pass-if "input tell 0"
270 (= (seek p 0 SEEK_CUR) 0))
271 (read-char p)
272 (pass-if "input tell 1"
273 (= (seek p 0 SEEK_CUR) 1))
274 (unread-char #\x p)
275 (pass-if "input tell back to 0"
276 (= (seek p 0 SEEK_CUR) 0))
277 (pass-if "input ungetted char"
278 (char=? (read-char p) #\x))
279 (seek p 0 SEEK_END)
280 (pass-if "input seek to end"
281 (= (seek p 0 SEEK_CUR)
282 (string-length text)))
283 (unread-char #\x p)
284 (pass-if "input seek to beginning"
285 (= (seek p 0 SEEK_SET) 0))
286 (pass-if "input reread first char"
287 (char=? (read-char p)
288 (string-ref text 0)))))))
289
290 ;; seeking an output string.
291 (catch-test-errors
292 (let* ((text "123456789")
293 (len (string-length text))
294 (result (call-with-output-string
295 (lambda (p)
296 (pass-if "output tell 0"
297 (= (seek p 0 SEEK_CUR) 0))
298 (display text p)
299 (pass-if "output tell end"
300 (= (seek p 0 SEEK_CUR) len))
301 (pass-if "output seek to beginning"
302 (= (seek p 0 SEEK_SET) 0))
303 (write-char #\a p)
304 (seek p -1 SEEK_END)
305 (pass-if "output seek to last char"
306 (= (seek p 0 SEEK_CUR)
307 (- len 1)))
308 (write-char #\b p)))))
309 (string-set! text 0 #\a)
310 (string-set! text (- len 1) #\b)
311 (pass-if "output check"
312 (string=? text result)))))
313
314
315 \f
316 ;;;; Soft ports. No tests implemented yet.
317
318 \f
319 ;;;; Generic operations across all port types.
320
321 (let ((port-loop-temp (test-file)))
322
323 ;; Return a list of input ports that all return the same text.
324 ;; We map tests over this list.
325 (define (input-port-list text)
326
327 ;; Create a text file some of the ports will use.
328 (let ((out-port (open-output-file port-loop-temp)))
329 (display text out-port)
330 (close-port out-port))
331
332 (list (open-input-file port-loop-temp)
333 (open-input-pipe (string-append "cat " port-loop-temp))
334 (call-with-input-string text (lambda (x) x))
335 ;; We don't test soft ports at the moment.
336 ))
337
338 (define port-list-names '("file" "pipe" "string"))
339
340 ;; Test the line counter.
341 (define (test-line-counter text second-line final-column)
342 (with-test-prefix "line counter"
343 (let ((ports (input-port-list text)))
344 (for-each
345 (lambda (port port-name)
346 (with-test-prefix port-name
347 (pass-if "at beginning of input"
348 (= (port-line port) 0))
349 (pass-if "read first character"
350 (eqv? (read-char port) #\x))
351 (pass-if "after reading one character"
352 (= (port-line port) 0))
353 (pass-if "read first newline"
354 (eqv? (read-char port) #\newline))
355 (pass-if "after reading first newline char"
356 (= (port-line port) 1))
357 (pass-if "second line read correctly"
358 (equal? (read-line port) second-line))
359 (pass-if "read-line increments line number"
360 (= (port-line port) 2))
361 (pass-if "read-line returns EOF"
362 (let loop ((i 0))
363 (cond
364 ((eof-object? (read-line port)) #t)
365 ((> i 20) #f)
366 (else (loop (+ i 1))))))
367 (pass-if "line count is 5 at EOF"
368 (= (port-line port) 5))
369 (pass-if "column is correct at EOF"
370 (= (port-column port) final-column))))
371 ports port-list-names)
372 (for-each close-port ports)
373 (delete-file port-loop-temp))))
374
375 (catch-test-errors
376 (with-test-prefix "newline"
377 (test-line-counter
378 (string-append "x\n"
379 "He who receives an idea from me, receives instruction\n"
380 "himself without lessening mine; as he who lights his\n"
381 "taper at mine, receives light without darkening me.\n"
382 " --- Thomas Jefferson\n")
383 "He who receives an idea from me, receives instruction"
384 0)))
385
386 (catch-test-errors
387 (with-test-prefix "no newline"
388 (test-line-counter
389 (string-append "x\n"
390 "He who receives an idea from me, receives instruction\n"
391 "himself without lessening mine; as he who lights his\n"
392 "taper at mine, receives light without darkening me.\n"
393 " --- Thomas Jefferson\n"
394 "no newline here")
395 "He who receives an idea from me, receives instruction"
396 15))))
397
398 \f
399 ;;;; testing read-delimited and friends
400
401 (with-test-prefix "read-delimited!"
402 (let ((c (make-string 20 #\!)))
403 (call-with-input-string
404 "defdef\nghighi\n"
405 (lambda (port)
406
407 (read-delimited! "\n" c port 'concat)
408 (pass-if "read-delimited! reads a first line"
409 (string=? c "defdef\n!!!!!!!!!!!!!"))
410
411 (read-delimited! "\n" c port 'concat 3)
412 (pass-if "read-delimited! reads a first line"
413 (string=? c "defghighi\n!!!!!!!!!!"))))))
414
415 \f
416 ;;;; char-ready?
417
418 (call-with-input-string
419 "howdy"
420 (lambda (port)
421 (pass-if "char-ready? returns true on string port"
422 (char-ready? port))))
423
424 ;;; This segfaults on some versions of Guile. We really should run
425 ;;; the tests in a subprocess...
426
427 (call-with-input-string
428 "howdy"
429 (lambda (port)
430 (with-input-from-port
431 port
432 (lambda ()
433 (pass-if "char-ready? returns true on string port as default port"
434 (char-ready?))))))
435
436 \f
437 ;;;; Close current-input-port, and make sure everyone can handle it.
438
439 (with-test-prefix "closing current-input-port"
440 (for-each (lambda (procedure name)
441 (with-input-from-port
442 (call-with-input-string "foo" (lambda (p) p))
443 (lambda ()
444 (close-port (current-input-port))
445 (pass-if name
446 (signals-error? 'wrong-type-arg (procedure))))))
447 (list read read-char read-line)
448 '("read" "read-char" "read-line")))