1 ;;;; r6rs-ports.test --- R6RS I/O port tests. -*- coding: iso-8859-1; -*-
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 bytevector))
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 (not (eof-object? port))
52 (= (char->integer #\A) (get-u8 port))
53 (eof-object? (get-u8 port)))))
55 (pass-if "get-bytevector-n [short]"
56 (let* ((port (open-input-string "GNU Guile"))
57 (bv (get-bytevector-n port 4)))
59 (equal? (bytevector->u8-list bv)
60 (map char->integer (string->list "GNU "))))))
62 (pass-if "get-bytevector-n [long]"
63 (let* ((port (open-input-string "GNU Guile"))
64 (bv (get-bytevector-n port 256)))
66 (equal? (bytevector->u8-list bv)
67 (map char->integer (string->list "GNU Guile"))))))
69 (pass-if-exception "get-bytevector-n with closed port"
70 exception:wrong-type-arg
72 (let ((port (%make-void-port "r")))
75 (get-bytevector-n port 3)))
77 (pass-if "get-bytevector-n! [short]"
78 (let* ((port (open-input-string "GNU Guile"))
79 (bv (make-bytevector 4))
80 (read (get-bytevector-n! port bv 0 4)))
82 (equal? (bytevector->u8-list bv)
83 (map char->integer (string->list "GNU "))))))
85 (pass-if "get-bytevector-n! [long]"
86 (let* ((str "GNU Guile")
87 (port (open-input-string str))
88 (bv (make-bytevector 256))
89 (read (get-bytevector-n! port bv 0 256)))
90 (and (equal? read (string-length str))
91 (equal? (map (lambda (i)
92 (bytevector-u8-ref bv i))
94 (map char->integer (string->list str))))))
96 (pass-if "get-bytevector-some [simple]"
97 (let* ((str "GNU Guile")
98 (port (open-input-string str))
99 (bv (get-bytevector-some port)))
100 (and (bytevector? bv)
101 (equal? (bytevector->u8-list bv)
102 (map char->integer (string->list str))))))
104 (pass-if "get-bytevector-some [only-some]"
105 (let* ((str "GNU Guile")
107 (port (make-soft-port
110 (if (>= index (string-length str))
112 (let ((c (string-ref str index)))
113 (set! index (+ index 1))
117 ;; Number of readily available octets: falls to
118 ;; zero after 4 octets have been read.
119 (- 4 (modulo index 5))))
121 (bv (get-bytevector-some port)))
122 (and (bytevector? bv)
124 (= (bytevector-length bv) index)
125 (equal? (bytevector->u8-list bv)
126 (map char->integer (string->list "GNU "))))))
128 (pass-if "get-bytevector-all"
129 (let* ((str "GNU Guile")
131 (port (make-soft-port
134 (if (>= index (string-length str))
136 (let ((c (string-ref str index)))
137 (set! index (+ index 1))
142 ;; Number of readily available octets: falls to
143 ;; zero after 4 octets have been read and then
146 (- (string-length str) index)
147 (- 4 (modulo index 5)))))
148 (if (= 0 a) (set! cont? #t))
151 (bv (get-bytevector-all port)))
152 (and (bytevector? bv)
153 (= index (string-length str))
154 (= (bytevector-length bv) (string-length str))
155 (equal? (bytevector->u8-list bv)
156 (map char->integer (string->list str)))))))
159 (define (make-soft-output-port)
160 (let* ((bv (make-bytevector 1024))
163 (write-char (lambda (chr)
164 (bytevector-u8-set! bv write-index
166 (set! write-index (+ 1 write-index)))))
169 (lambda (str) ;; write-string
170 (for-each write-char (string->list str)))
171 (lambda () #t) ;; flush-output
172 (lambda () ;; read-char
173 (if (>= read-index (bytevector-length bv))
175 (let ((c (bytevector-u8-ref bv read-index)))
176 (set! read-index (+ read-index 1))
178 (lambda () #t)) ;; close-port
181 (with-test-prefix "7.2.11 Binary Output"
184 (let ((port (make-soft-output-port)))
186 (equal? (get-u8 port) 77)))
188 (pass-if "put-bytevector [2 args]"
189 (let ((port (make-soft-output-port))
190 (bv (make-bytevector 256)))
191 (put-bytevector port bv)
192 (equal? (bytevector->u8-list bv)
194 (get-bytevector-n port (bytevector-length bv))))))
196 (pass-if "put-bytevector [3 args]"
197 (let ((port (make-soft-output-port))
198 (bv (make-bytevector 256))
200 (put-bytevector port bv start)
201 (equal? (drop (bytevector->u8-list bv) start)
203 (get-bytevector-n port (- (bytevector-length bv) start))))))
205 (pass-if "put-bytevector [4 args]"
206 (let ((port (make-soft-output-port))
207 (bv (make-bytevector 256))
210 (put-bytevector port bv start count)
211 (equal? (take (drop (bytevector->u8-list bv) start) count)
213 (get-bytevector-n port count)))))
215 (pass-if-exception "put-bytevector with closed port"
216 exception:wrong-type-arg
218 (let* ((bv (make-bytevector 4))
219 (port (%make-void-port "w")))
222 (put-bytevector port bv)))
224 (pass-if "put-bytevector with UTF-16 string port"
225 (let* ((str "hello, world")
226 (bv (string->utf16 str)))
228 (with-fluids ((%default-port-encoding "UTF-16BE"))
229 (call-with-output-string
231 (put-bytevector port bv)))))))
233 (pass-if "put-bytevector with wrong-encoding string port"
234 (let* ((str "hello, world")
235 (bv (string->utf16 str)))
236 (catch 'encoding-error
238 (with-fluids ((%default-port-encoding "UTF-32"))
239 (call-with-output-string
241 (put-bytevector port bv)))))
242 (lambda (key subr message errno from to faulty-bv)
243 (and (bytevector=? faulty-bv bv)
244 (string=? to "UTF-32")
245 (string? (strerror errno))))))))
248 (with-test-prefix "7.2.7 Input Ports"
250 ;; This section appears here so that it can use the binary input
253 (pass-if "open-bytevector-input-port [1 arg]"
254 (let* ((str "Hello Port!")
255 (bv (u8-list->bytevector (map char->integer
256 (string->list str))))
257 (port (open-bytevector-input-port bv))
260 (let loop ((chr (read-char port))
262 (if (eof-object? chr)
263 (apply string (reverse! result))
264 (loop (read-char port)
265 (cons chr result)))))))
267 (equal? (read-to-string port) str)))
269 (pass-if-exception "bytevector-input-port is read-only"
270 exception:wrong-type-arg
272 (let* ((str "Hello Port!")
273 (bv (u8-list->bytevector (map char->integer
274 (string->list str))))
275 (port (open-bytevector-input-port bv #f)))
277 (write "hello" port)))
279 (pass-if "bytevector input port supports seeking"
280 (let* ((str "Hello Port!")
281 (bv (u8-list->bytevector (map char->integer
282 (string->list str))))
283 (port (open-bytevector-input-port bv #f)))
285 (and (port-has-port-position? port)
286 (= 0 (port-position port))
287 (port-has-set-port-position!? port)
289 (set-port-position! port 6)
290 (= 6 (port-position port)))
291 (bytevector=? (get-bytevector-all port)
293 (map char->integer (string->list "Port!")))))))
295 (pass-if-exception "make-custom-binary-input-port [wrong-num-args]"
296 exception:wrong-num-args
298 ;; Prior to Guile-R6RS-Libs 0.2, the last 3 arguments were wrongfully
300 (make-custom-binary-input-port "port" (lambda args #t)))
302 (pass-if "make-custom-binary-input-port"
303 (let* ((source (make-bytevector 7777))
305 (len (bytevector-length source)))
306 (lambda (bv start count)
307 (let ((amount (min count (- len pos))))
309 (bytevector-copy! source pos
311 (set! pos (+ pos amount))
313 (port (make-custom-binary-input-port "the port" read!
316 (bytevector=? (get-bytevector-all port) source)))
318 (pass-if "custom binary input port does not support `port-position'"
319 (let* ((str "Hello Port!")
320 (source (open-bytevector-input-port
322 (map char->integer (string->list str)))))
323 (read! (lambda (bv start count)
324 (let ((r (get-bytevector-n! source bv start count)))
328 (port (make-custom-binary-input-port "the port" read!
330 (not (or (port-has-port-position? port)
331 (port-has-set-port-position!? port)))))
333 (pass-if "custom binary input port supports `port-position'"
334 (let* ((str "Hello Port!")
335 (source (open-bytevector-input-port
337 (map char->integer (string->list str)))))
338 (read! (lambda (bv start count)
339 (let ((r (get-bytevector-n! source bv start count)))
344 (port-position source)))
345 (set-pos! (lambda (pos)
346 (set-port-position! source pos)))
347 (port (make-custom-binary-input-port "the port" read!
348 get-pos set-pos! #f)))
350 (and (port-has-port-position? port)
351 (= 0 (port-position port))
352 (port-has-set-port-position!? port)
354 (set-port-position! port 6)
355 (= 6 (port-position port)))
356 (bytevector=? (get-bytevector-all port)
358 (map char->integer (string->list "Port!")))))))
360 (pass-if "custom binary input port `close-proc' is called"
362 (read! (lambda (bv start count) 0))
363 (get-pos (lambda () 0))
364 (set-pos! (lambda (pos) #f))
365 (close! (lambda () (set! closed? #t)))
366 (port (make-custom-binary-input-port "the port" read!
371 (gc) ; Test for marking a closed port.
375 (with-test-prefix "8.2.10 Output ports"
377 (pass-if "open-bytevector-output-port"
378 (let-values (((port get-content)
379 (open-bytevector-output-port #f)))
380 (let ((source (make-bytevector 7777)))
381 (put-bytevector port source)
382 (and (bytevector=? (get-content) source)
383 (bytevector=? (get-content) (make-bytevector 0))))))
385 (pass-if "open-bytevector-output-port [put-u8]"
386 (let-values (((port get-content)
387 (open-bytevector-output-port)))
389 (and (bytevector=? (get-content) (make-bytevector 1 77))
390 (bytevector=? (get-content) (make-bytevector 0)))))
392 (pass-if "open-bytevector-output-port [display]"
393 (let-values (((port get-content)
394 (open-bytevector-output-port)))
395 (display "hello" port)
396 (and (bytevector=? (get-content) (string->utf8 "hello"))
397 (bytevector=? (get-content) (make-bytevector 0)))))
399 (pass-if "bytevector output port supports `port-position'"
400 (let-values (((port get-content)
401 (open-bytevector-output-port)))
402 (let ((source (make-bytevector 7777))
403 (overwrite (make-bytevector 33)))
404 (and (port-has-port-position? port)
405 (port-has-set-port-position!? port)
407 (put-bytevector port source)
408 (= (bytevector-length source)
409 (port-position port)))
411 (set-port-position! port 10)
412 (= 10 (port-position port)))
414 (put-bytevector port overwrite)
415 (bytevector-copy! overwrite 0 source 10
416 (bytevector-length overwrite))
417 (= (port-position port)
418 (+ 10 (bytevector-length overwrite))))
419 (bytevector=? (get-content) source)
420 (bytevector=? (get-content) (make-bytevector 0))))))
422 (pass-if "make-custom-binary-output"
423 (let ((port (make-custom-binary-output-port "cbop"
426 (and (output-port? port)
428 (not (port-has-port-position? port))
429 (not (port-has-set-port-position!? port)))))
431 (pass-if "make-custom-binary-output-port [partial writes]"
432 (let* ((source (uint-list->bytevector (iota 333)
433 (native-endianness) 2))
434 (sink (make-bytevector (bytevector-length source)))
437 (write! (lambda (bv start count)
442 (let ((u8 (bytevector-u8-ref bv start)))
443 ;; Get one byte at a time.
444 (bytevector-u8-set! sink sink-pos u8)
445 (set! sink-pos (+ 1 sink-pos))
447 (port (make-custom-binary-output-port "cbop" write!
449 (put-bytevector port source)
450 (and (= sink-pos (bytevector-length source))
452 (bytevector=? sink source))))
454 (pass-if "make-custom-binary-output-port [full writes]"
455 (let* ((source (uint-list->bytevector (iota 333)
456 (native-endianness) 2))
457 (sink (make-bytevector (bytevector-length source)))
460 (write! (lambda (bv start count)
466 (bytevector-copy! bv start
469 (set! sink-pos (+ sink-pos count))
471 (port (make-custom-binary-output-port "cbop" write!
473 (put-bytevector port source)
474 (and (= sink-pos (bytevector-length source))
476 (bytevector=? sink source)))))