1 ;;;; r6rs-ports.test --- R6RS I/O port tests. -*- coding: utf-8; -*-
3 ;;;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
6 ;;;; This library is free software; you can redistribute it and/or
7 ;;;; modify it under the terms of the GNU Lesser General Public
8 ;;;; License as published by the Free Software Foundation; either
9 ;;;; version 3 of the License, or (at your option) any later version.
11 ;;;; This library 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 GNU
14 ;;;; Lesser General Public License for more details.
16 ;;;; You should have received a copy of the GNU Lesser General Public
17 ;;;; License along with this library; if not, write to the Free Software
18 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 (define-module (test-io-ports)
21 #:use-module (test-suite lib)
22 #:use-module (srfi srfi-1)
23 #:use-module (srfi srfi-11)
24 #:use-module (rnrs io ports)
25 #:use-module (rnrs bytevectors))
27 ;;; All these tests assume Guile 1.8's port system, where characters are
28 ;;; treated as octets.
30 ;; Set the default encoding of future ports to be Latin-1.
31 (fluid-set! %default-port-encoding #f)
34 (with-test-prefix "7.2.5 End-of-File Object"
37 (and (eqv? (eof-object) (eof-object))
38 (eq? (eof-object) (eof-object)))))
41 (with-test-prefix "7.2.8 Binary Input"
44 (let ((port (open-input-string "A")))
45 (and (= (char->integer #\A) (get-u8 port))
46 (eof-object? (get-u8 port)))))
48 (pass-if "lookahead-u8"
49 (let ((port (open-input-string "A")))
50 (and (= (char->integer #\A) (lookahead-u8 port))
51 (= (char->integer #\A) (lookahead-u8 port))
52 (= (char->integer #\A) (get-u8 port))
53 (eof-object? (get-u8 port)))))
55 (pass-if "lookahead-u8 non-ASCII"
56 (let ((port (with-fluids ((%default-port-encoding "UTF-8"))
57 (open-input-string "λ"))))
58 (and (= 206 (lookahead-u8 port))
59 (= 206 (lookahead-u8 port))
61 (= 187 (lookahead-u8 port))
62 (= 187 (lookahead-u8 port))
64 (eof-object? (lookahead-u8 port))
65 (eof-object? (get-u8 port)))))
67 (pass-if "lookahead-u8: result is unsigned"
69 (let ((port (open-bytevector-input-port #vu8(255))))
70 (= (lookahead-u8 port) 255)))
72 (pass-if "get-bytevector-n [short]"
73 (let* ((port (open-input-string "GNU Guile"))
74 (bv (get-bytevector-n port 4)))
76 (equal? (bytevector->u8-list bv)
77 (map char->integer (string->list "GNU "))))))
79 (pass-if "get-bytevector-n [long]"
80 (let* ((port (open-input-string "GNU Guile"))
81 (bv (get-bytevector-n port 256)))
83 (equal? (bytevector->u8-list bv)
84 (map char->integer (string->list "GNU Guile"))))))
86 (pass-if-exception "get-bytevector-n with closed port"
87 exception:wrong-type-arg
89 (let ((port (%make-void-port "r")))
92 (get-bytevector-n port 3)))
94 (pass-if "get-bytevector-n! [short]"
95 (let* ((port (open-input-string "GNU Guile"))
96 (bv (make-bytevector 4))
97 (read (get-bytevector-n! port bv 0 4)))
99 (equal? (bytevector->u8-list bv)
100 (map char->integer (string->list "GNU "))))))
102 (pass-if "get-bytevector-n! [long]"
103 (let* ((str "GNU Guile")
104 (port (open-input-string str))
105 (bv (make-bytevector 256))
106 (read (get-bytevector-n! port bv 0 256)))
107 (and (equal? read (string-length str))
108 (equal? (map (lambda (i)
109 (bytevector-u8-ref bv i))
111 (map char->integer (string->list str))))))
113 (pass-if "get-bytevector-some [simple]"
114 (let* ((str "GNU Guile")
115 (port (open-input-string str))
116 (bv (get-bytevector-some port)))
117 (and (bytevector? bv)
118 (equal? (bytevector->u8-list bv)
119 (map char->integer (string->list str))))))
121 (pass-if "get-bytevector-some [only-some]"
122 (let* ((str "GNU Guile")
124 (port (make-soft-port
127 (if (>= index (string-length str))
129 (let ((c (string-ref str index)))
130 (set! index (+ index 1))
134 ;; Number of readily available octets: falls to
135 ;; zero after 4 octets have been read.
136 (- 4 (modulo index 5))))
138 (bv (get-bytevector-some port)))
139 (and (bytevector? bv)
141 (= (bytevector-length bv) index)
142 (equal? (bytevector->u8-list bv)
143 (map char->integer (string->list "GNU "))))))
145 (pass-if "get-bytevector-all"
146 (let* ((str "GNU Guile")
148 (port (make-soft-port
151 (if (>= index (string-length str))
153 (let ((c (string-ref str index)))
154 (set! index (+ index 1))
159 ;; Number of readily available octets: falls to
160 ;; zero after 4 octets have been read and then
163 (- (string-length str) index)
164 (- 4 (modulo index 5)))))
165 (if (= 0 a) (set! cont? #t))
168 (bv (get-bytevector-all port)))
169 (and (bytevector? bv)
170 (= index (string-length str))
171 (= (bytevector-length bv) (string-length str))
172 (equal? (bytevector->u8-list bv)
173 (map char->integer (string->list str)))))))
176 (define (make-soft-output-port)
177 (let* ((bv (make-bytevector 1024))
180 (write-char (lambda (chr)
181 (bytevector-u8-set! bv write-index
183 (set! write-index (+ 1 write-index)))))
186 (lambda (str) ;; write-string
187 (for-each write-char (string->list str)))
188 (lambda () #t) ;; flush-output
189 (lambda () ;; read-char
190 (if (>= read-index (bytevector-length bv))
192 (let ((c (bytevector-u8-ref bv read-index)))
193 (set! read-index (+ read-index 1))
195 (lambda () #t)) ;; close-port
198 (with-test-prefix "7.2.11 Binary Output"
201 (let ((port (make-soft-output-port)))
203 (equal? (get-u8 port) 77)))
205 ;; Note: The `put-bytevector' tests below require a Latin-1 locale so
206 ;; that the `scm_from_locale_stringn' call in `sf_write' will let all
207 ;; the bytes through, unmodified. This is hacky, but we can't use
208 ;; "custom binary output ports" here because they're only tested
211 (pass-if "put-bytevector [2 args]"
213 (let ((port (make-soft-output-port))
214 (bv (make-bytevector 256)))
215 (put-bytevector port bv)
216 (equal? (bytevector->u8-list bv)
218 (get-bytevector-n port (bytevector-length bv)))))))
220 (pass-if "put-bytevector [3 args]"
222 (let ((port (make-soft-output-port))
223 (bv (make-bytevector 256))
225 (put-bytevector port bv start)
226 (equal? (drop (bytevector->u8-list bv) start)
228 (get-bytevector-n port (- (bytevector-length bv) start)))))))
230 (pass-if "put-bytevector [4 args]"
232 (let ((port (make-soft-output-port))
233 (bv (make-bytevector 256))
236 (put-bytevector port bv start count)
237 (equal? (take (drop (bytevector->u8-list bv) start) count)
239 (get-bytevector-n port count))))))
241 (pass-if-exception "put-bytevector with closed port"
242 exception:wrong-type-arg
244 (let* ((bv (make-bytevector 4))
245 (port (%make-void-port "w")))
248 (put-bytevector port bv)))
250 (pass-if "put-bytevector with UTF-16 string port"
251 (let* ((str "hello, world")
252 (bv (string->utf16 str)))
254 (with-fluids ((%default-port-encoding "UTF-16BE"))
255 (call-with-output-string
257 (put-bytevector port bv)))))))
259 (pass-if "put-bytevector with wrong-encoding string port"
260 (let* ((str "hello, world")
261 (bv (string->utf16 str)))
262 (catch 'encoding-error
264 (with-fluids ((%default-port-encoding "UTF-32"))
265 (call-with-output-string
267 (put-bytevector port bv)))))
268 (lambda (key subr message errno from to faulty-bv)
269 (and (bytevector=? faulty-bv bv)
270 (string=? to "UTF-32")
271 (string? (strerror errno))))))))
274 (with-test-prefix "7.2.7 Input Ports"
276 ;; This section appears here so that it can use the binary input
279 (pass-if "open-bytevector-input-port [1 arg]"
280 (let* ((str "Hello Port!")
281 (bv (u8-list->bytevector (map char->integer
282 (string->list str))))
283 (port (open-bytevector-input-port bv))
286 (let loop ((chr (read-char port))
288 (if (eof-object? chr)
289 (apply string (reverse! result))
290 (loop (read-char port)
291 (cons chr result)))))))
293 (equal? (read-to-string port) str)))
295 (pass-if-exception "bytevector-input-port is read-only"
296 exception:wrong-type-arg
298 (let* ((str "Hello Port!")
299 (bv (u8-list->bytevector (map char->integer
300 (string->list str))))
301 (port (open-bytevector-input-port bv #f)))
303 (write "hello" port)))
305 (pass-if "bytevector input port supports seeking"
306 (let* ((str "Hello Port!")
307 (bv (u8-list->bytevector (map char->integer
308 (string->list str))))
309 (port (open-bytevector-input-port bv #f)))
311 (and (port-has-port-position? port)
312 (= 0 (port-position port))
313 (port-has-set-port-position!? port)
315 (set-port-position! port 6)
316 (= 6 (port-position port)))
317 (bytevector=? (get-bytevector-all port)
319 (map char->integer (string->list "Port!")))))))
321 (pass-if-exception "make-custom-binary-input-port [wrong-num-args]"
322 exception:wrong-num-args
324 ;; Prior to Guile-R6RS-Libs 0.2, the last 3 arguments were wrongfully
326 (make-custom-binary-input-port "port" (lambda args #t)))
328 (pass-if "make-custom-binary-input-port"
329 (let* ((source (make-bytevector 7777))
331 (len (bytevector-length source)))
332 (lambda (bv start count)
333 (let ((amount (min count (- len pos))))
335 (bytevector-copy! source pos
337 (set! pos (+ pos amount))
339 (port (make-custom-binary-input-port "the port" read!
342 (bytevector=? (get-bytevector-all port) source)))
344 (pass-if "custom binary input port does not support `port-position'"
345 (let* ((str "Hello Port!")
346 (source (open-bytevector-input-port
348 (map char->integer (string->list str)))))
349 (read! (lambda (bv start count)
350 (let ((r (get-bytevector-n! source bv start count)))
354 (port (make-custom-binary-input-port "the port" read!
356 (not (or (port-has-port-position? port)
357 (port-has-set-port-position!? port)))))
359 (pass-if "custom binary input port supports `port-position'"
360 (let* ((str "Hello Port!")
361 (source (open-bytevector-input-port
363 (map char->integer (string->list str)))))
364 (read! (lambda (bv start count)
365 (let ((r (get-bytevector-n! source bv start count)))
370 (port-position source)))
371 (set-pos! (lambda (pos)
372 (set-port-position! source pos)))
373 (port (make-custom-binary-input-port "the port" read!
374 get-pos set-pos! #f)))
376 (and (port-has-port-position? port)
377 (= 0 (port-position port))
378 (port-has-set-port-position!? port)
380 (set-port-position! port 6)
381 (= 6 (port-position port)))
382 (bytevector=? (get-bytevector-all port)
384 (map char->integer (string->list "Port!")))))))
386 (pass-if "custom binary input port `close-proc' is called"
388 (read! (lambda (bv start count) 0))
389 (get-pos (lambda () 0))
390 (set-pos! (lambda (pos) #f))
391 (close! (lambda () (set! closed? #t)))
392 (port (make-custom-binary-input-port "the port" read!
397 (gc) ; Test for marking a closed port.
401 (with-test-prefix "8.2.10 Output ports"
403 (pass-if "open-bytevector-output-port"
404 (let-values (((port get-content)
405 (open-bytevector-output-port #f)))
406 (let ((source (make-bytevector 7777)))
407 (put-bytevector port source)
408 (and (bytevector=? (get-content) source)
409 (bytevector=? (get-content) (make-bytevector 0))))))
411 (pass-if "open-bytevector-output-port [extract after close]"
412 (let-values (((port get-content)
413 (open-bytevector-output-port)))
414 (let ((source (make-bytevector 12345 #xFE)))
415 (put-bytevector port source)
417 (bytevector=? (get-content) source))))
419 (pass-if "open-bytevector-output-port [put-u8]"
420 (let-values (((port get-content)
421 (open-bytevector-output-port)))
423 (and (bytevector=? (get-content) (make-bytevector 1 77))
424 (bytevector=? (get-content) (make-bytevector 0)))))
426 (pass-if "open-bytevector-output-port [display]"
427 (let-values (((port get-content)
428 (open-bytevector-output-port)))
429 (display "hello" port)
430 (and (bytevector=? (get-content) (string->utf8 "hello"))
431 (bytevector=? (get-content) (make-bytevector 0)))))
433 (pass-if "bytevector output port supports `port-position'"
434 (let-values (((port get-content)
435 (open-bytevector-output-port)))
436 (let ((source (make-bytevector 7777))
437 (overwrite (make-bytevector 33)))
438 (and (port-has-port-position? port)
439 (port-has-set-port-position!? port)
441 (put-bytevector port source)
442 (= (bytevector-length source)
443 (port-position port)))
445 (set-port-position! port 10)
446 (= 10 (port-position port)))
448 (put-bytevector port overwrite)
449 (bytevector-copy! overwrite 0 source 10
450 (bytevector-length overwrite))
451 (= (port-position port)
452 (+ 10 (bytevector-length overwrite))))
453 (bytevector=? (get-content) source)
454 (bytevector=? (get-content) (make-bytevector 0))))))
456 (pass-if "make-custom-binary-output"
457 (let ((port (make-custom-binary-output-port "cbop"
460 (and (output-port? port)
462 (not (port-has-port-position? port))
463 (not (port-has-set-port-position!? port)))))
465 (pass-if "make-custom-binary-output-port [partial writes]"
466 (let* ((source (uint-list->bytevector (iota 333)
467 (native-endianness) 2))
468 (sink (make-bytevector (bytevector-length source)))
471 (write! (lambda (bv start count)
476 (let ((u8 (bytevector-u8-ref bv start)))
477 ;; Get one byte at a time.
478 (bytevector-u8-set! sink sink-pos u8)
479 (set! sink-pos (+ 1 sink-pos))
481 (port (make-custom-binary-output-port "cbop" write!
483 (put-bytevector port source)
484 (and (= sink-pos (bytevector-length source))
486 (bytevector=? sink source))))
488 (pass-if "make-custom-binary-output-port [full writes]"
489 (let* ((source (uint-list->bytevector (iota 333)
490 (native-endianness) 2))
491 (sink (make-bytevector (bytevector-length source)))
494 (write! (lambda (bv start count)
500 (bytevector-copy! bv start
503 (set! sink-pos (+ sink-pos count))
505 (port (make-custom-binary-output-port "cbop" write!
507 (put-bytevector port source)
508 (and (= sink-pos (bytevector-length source))
510 (bytevector=? sink source)))))
513 (with-test-prefix "8.2.6 Input and output ports"
515 (pass-if "transcoded-port [output]"
516 (let ((s "Hello\nÄÖÜ"))
519 (call-with-bytevector-output-port
521 (call-with-port (transcoded-port bv-port (make-transcoder (utf-8-codec)))
523 (put-string utf8-port s))))))))
525 (pass-if "transcoded-port [input]"
526 (let ((s "Hello\nÄÖÜ"))
530 (transcoded-port (open-bytevector-input-port (string->utf8 s))
531 (make-transcoder (utf-8-codec)))))))
533 (pass-if "transcoded-port [input line]"
535 (get-line (transcoded-port
536 (open-bytevector-input-port (string->utf8 "ÄÖÜ\nFooBar"))
537 (make-transcoder (utf-8-codec))))))
539 (pass-if "transcoded-port [error handling mode = raise]"
540 (let* ((t (make-transcoder (utf-8-codec) (native-eol-style)
541 (error-handling-mode raise)))
542 (b (open-bytevector-input-port #vu8(255 2 1)))
543 (tp (transcoded-port b t)))
544 ;; FIXME: Should be (guard (c ((i/o-decoding-error? c) #t)) ...).
545 (catch 'encoding-error
552 (pass-if "transcoded-port [error handling mode = replace]"
553 (let* ((t (make-transcoder (utf-8-codec) (native-eol-style)
554 (error-handling-mode replace)))
555 (b (open-bytevector-input-port #vu8(255 1 2 3 103 110 117)))
556 (tp (transcoded-port b t)))
557 (string-suffix? "gnu" (get-line tp)))))