Fix fencepost error in bip_seek
[bpt/guile.git] / test-suite / tests / r6rs-ports.test
1 ;;;; r6rs-ports.test --- R6RS I/O port tests. -*- coding: utf-8; -*-
2 ;;;;
3 ;;;; Copyright (C) 2009, 2010, 2011 Free Software Foundation, Inc.
4 ;;;; Ludovic Courtès
5 ;;;;
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.
10 ;;;;
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.
15 ;;;;
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
19
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 exceptions)
26 #:use-module (rnrs bytevectors))
27
28 ;;; All these tests assume Guile 1.8's port system, where characters are
29 ;;; treated as octets.
30
31 ;; Set the default encoding of future ports to be Latin-1.
32 (fluid-set! %default-port-encoding #f)
33
34 \f
35 (with-test-prefix "7.2.5 End-of-File Object"
36
37 (pass-if "eof-object"
38 (and (eqv? (eof-object) (eof-object))
39 (eq? (eof-object) (eof-object))))
40
41 (pass-if "port-eof?"
42 (port-eof? (open-input-string ""))))
43
44 \f
45 (with-test-prefix "7.2.8 Binary Input"
46
47 (pass-if "get-u8"
48 (let ((port (open-input-string "A")))
49 (and (= (char->integer #\A) (get-u8 port))
50 (eof-object? (get-u8 port)))))
51
52 (pass-if "lookahead-u8"
53 (let ((port (open-input-string "A")))
54 (and (= (char->integer #\A) (lookahead-u8 port))
55 (= (char->integer #\A) (lookahead-u8 port))
56 (= (char->integer #\A) (get-u8 port))
57 (eof-object? (get-u8 port)))))
58
59 (pass-if "lookahead-u8 non-ASCII"
60 (let ((port (with-fluids ((%default-port-encoding "UTF-8"))
61 (open-input-string "λ"))))
62 (and (= 206 (lookahead-u8 port))
63 (= 206 (lookahead-u8 port))
64 (= 206 (get-u8 port))
65 (= 187 (lookahead-u8 port))
66 (= 187 (lookahead-u8 port))
67 (= 187 (get-u8 port))
68 (eof-object? (lookahead-u8 port))
69 (eof-object? (get-u8 port)))))
70
71 (pass-if "lookahead-u8: result is unsigned"
72 ;; Bug #31081.
73 (let ((port (open-bytevector-input-port #vu8(255))))
74 (= (lookahead-u8 port) 255)))
75
76 (pass-if "get-bytevector-n [short]"
77 (let* ((port (open-input-string "GNU Guile"))
78 (bv (get-bytevector-n port 4)))
79 (and (bytevector? bv)
80 (equal? (bytevector->u8-list bv)
81 (map char->integer (string->list "GNU "))))))
82
83 (pass-if "get-bytevector-n [long]"
84 (let* ((port (open-input-string "GNU Guile"))
85 (bv (get-bytevector-n port 256)))
86 (and (bytevector? bv)
87 (equal? (bytevector->u8-list bv)
88 (map char->integer (string->list "GNU Guile"))))))
89
90 (pass-if-exception "get-bytevector-n with closed port"
91 exception:wrong-type-arg
92
93 (let ((port (%make-void-port "r")))
94
95 (close-port port)
96 (get-bytevector-n port 3)))
97
98 (pass-if "get-bytevector-n! [short]"
99 (let* ((port (open-input-string "GNU Guile"))
100 (bv (make-bytevector 4))
101 (read (get-bytevector-n! port bv 0 4)))
102 (and (equal? read 4)
103 (equal? (bytevector->u8-list bv)
104 (map char->integer (string->list "GNU "))))))
105
106 (pass-if "get-bytevector-n! [long]"
107 (let* ((str "GNU Guile")
108 (port (open-input-string str))
109 (bv (make-bytevector 256))
110 (read (get-bytevector-n! port bv 0 256)))
111 (and (equal? read (string-length str))
112 (equal? (map (lambda (i)
113 (bytevector-u8-ref bv i))
114 (iota read))
115 (map char->integer (string->list str))))))
116
117 (pass-if "get-bytevector-some [simple]"
118 (let* ((str "GNU Guile")
119 (port (open-input-string str))
120 (bv (get-bytevector-some port)))
121 (and (bytevector? bv)
122 (equal? (bytevector->u8-list bv)
123 (map char->integer (string->list str))))))
124
125 (pass-if "get-bytevector-some [only-some]"
126 (let* ((str "GNU Guile")
127 (index 0)
128 (port (make-soft-port
129 (vector #f #f #f
130 (lambda ()
131 (if (>= index (string-length str))
132 (eof-object)
133 (let ((c (string-ref str index)))
134 (set! index (+ index 1))
135 c)))
136 (lambda () #t)
137 (lambda ()
138 ;; Number of readily available octets: falls to
139 ;; zero after 4 octets have been read.
140 (- 4 (modulo index 5))))
141 "r"))
142 (bv (get-bytevector-some port)))
143 (and (bytevector? bv)
144 (= index 4)
145 (= (bytevector-length bv) index)
146 (equal? (bytevector->u8-list bv)
147 (map char->integer (string->list "GNU "))))))
148
149 (pass-if "get-bytevector-all"
150 (let* ((str "GNU Guile")
151 (index 0)
152 (port (make-soft-port
153 (vector #f #f #f
154 (lambda ()
155 (if (>= index (string-length str))
156 (eof-object)
157 (let ((c (string-ref str index)))
158 (set! index (+ index 1))
159 c)))
160 (lambda () #t)
161 (let ((cont? #f))
162 (lambda ()
163 ;; Number of readily available octets: falls to
164 ;; zero after 4 octets have been read and then
165 ;; starts again.
166 (let ((a (if cont?
167 (- (string-length str) index)
168 (- 4 (modulo index 5)))))
169 (if (= 0 a) (set! cont? #t))
170 a))))
171 "r"))
172 (bv (get-bytevector-all port)))
173 (and (bytevector? bv)
174 (= index (string-length str))
175 (= (bytevector-length bv) (string-length str))
176 (equal? (bytevector->u8-list bv)
177 (map char->integer (string->list str)))))))
178
179 \f
180 (define (make-soft-output-port)
181 (let* ((bv (make-bytevector 1024))
182 (read-index 0)
183 (write-index 0)
184 (write-char (lambda (chr)
185 (bytevector-u8-set! bv write-index
186 (char->integer chr))
187 (set! write-index (+ 1 write-index)))))
188 (make-soft-port
189 (vector write-char
190 (lambda (str) ;; write-string
191 (for-each write-char (string->list str)))
192 (lambda () #t) ;; flush-output
193 (lambda () ;; read-char
194 (if (>= read-index (bytevector-length bv))
195 (eof-object)
196 (let ((c (bytevector-u8-ref bv read-index)))
197 (set! read-index (+ read-index 1))
198 (integer->char c))))
199 (lambda () #t)) ;; close-port
200 "rw")))
201
202 (with-test-prefix "7.2.11 Binary Output"
203
204 (pass-if "put-u8"
205 (let ((port (make-soft-output-port)))
206 (put-u8 port 77)
207 (equal? (get-u8 port) 77)))
208
209 ;; Note: The `put-bytevector' tests below require a Latin-1 locale so
210 ;; that the `scm_from_locale_stringn' call in `sf_write' will let all
211 ;; the bytes through, unmodified. This is hacky, but we can't use
212 ;; "custom binary output ports" here because they're only tested
213 ;; later.
214
215 (pass-if "put-bytevector [2 args]"
216 (with-latin1-locale
217 (let ((port (make-soft-output-port))
218 (bv (make-bytevector 256)))
219 (put-bytevector port bv)
220 (equal? (bytevector->u8-list bv)
221 (bytevector->u8-list
222 (get-bytevector-n port (bytevector-length bv)))))))
223
224 (pass-if "put-bytevector [3 args]"
225 (with-latin1-locale
226 (let ((port (make-soft-output-port))
227 (bv (make-bytevector 256))
228 (start 10))
229 (put-bytevector port bv start)
230 (equal? (drop (bytevector->u8-list bv) start)
231 (bytevector->u8-list
232 (get-bytevector-n port (- (bytevector-length bv) start)))))))
233
234 (pass-if "put-bytevector [4 args]"
235 (with-latin1-locale
236 (let ((port (make-soft-output-port))
237 (bv (make-bytevector 256))
238 (start 10)
239 (count 77))
240 (put-bytevector port bv start count)
241 (equal? (take (drop (bytevector->u8-list bv) start) count)
242 (bytevector->u8-list
243 (get-bytevector-n port count))))))
244
245 (pass-if-exception "put-bytevector with closed port"
246 exception:wrong-type-arg
247
248 (let* ((bv (make-bytevector 4))
249 (port (%make-void-port "w")))
250
251 (close-port port)
252 (put-bytevector port bv)))
253
254 (pass-if "put-bytevector with UTF-16 string port"
255 (let* ((str "hello, world")
256 (bv (string->utf16 str)))
257 (equal? str
258 (with-fluids ((%default-port-encoding "UTF-16BE"))
259 (call-with-output-string
260 (lambda (port)
261 (put-bytevector port bv)))))))
262
263 (pass-if "put-bytevector with wrong-encoding string port"
264 (let* ((str "hello, world")
265 (bv (string->utf16 str)))
266 (catch 'decoding-error
267 (lambda ()
268 (with-fluids ((%default-port-encoding "UTF-32"))
269 (call-with-output-string
270 (lambda (port)
271 (put-bytevector port bv)))))
272 (lambda (key subr message errno port)
273 (string? (strerror errno)))))))
274
275 \f
276 (with-test-prefix "7.2.7 Input Ports"
277
278 ;; This section appears here so that it can use the binary input
279 ;; primitives.
280
281 (pass-if "open-bytevector-input-port [1 arg]"
282 (let* ((str "Hello Port!")
283 (bv (u8-list->bytevector (map char->integer
284 (string->list str))))
285 (port (open-bytevector-input-port bv))
286 (read-to-string
287 (lambda (port)
288 (let loop ((chr (read-char port))
289 (result '()))
290 (if (eof-object? chr)
291 (apply string (reverse! result))
292 (loop (read-char port)
293 (cons chr result)))))))
294
295 (equal? (read-to-string port) str)))
296
297 (pass-if-exception "bytevector-input-port is read-only"
298 exception:wrong-type-arg
299
300 (let* ((str "Hello Port!")
301 (bv (u8-list->bytevector (map char->integer
302 (string->list str))))
303 (port (open-bytevector-input-port bv #f)))
304
305 (write "hello" port)))
306
307 (pass-if "bytevector input port supports seeking"
308 (let* ((str "Hello Port!")
309 (bv (u8-list->bytevector (map char->integer
310 (string->list str))))
311 (port (open-bytevector-input-port bv #f)))
312
313 (and (port-has-port-position? port)
314 (= 0 (port-position port))
315 (port-has-set-port-position!? port)
316 (begin
317 (set-port-position! port 6)
318 (= 6 (port-position port)))
319 (bytevector=? (get-bytevector-all port)
320 (u8-list->bytevector
321 (map char->integer (string->list "Port!")))))))
322
323 (pass-if "bytevector input port can seek to very end"
324 (let ((empty (open-bytevector-input-port '#vu8()))
325 (not-empty (open-bytevector-input-port '#vu8(1 2 3))))
326 (and (begin (set-port-position! empty (port-position empty))
327 (= 0 (port-position empty)))
328 (begin (get-bytevector-n not-empty 3)
329 (set-port-position! not-empty (port-position not-empty))
330 (= 3 (port-position not-empty))))))
331
332 (pass-if-exception "make-custom-binary-input-port [wrong-num-args]"
333 exception:wrong-num-args
334
335 ;; Prior to Guile-R6RS-Libs 0.2, the last 3 arguments were wrongfully
336 ;; optional.
337 (make-custom-binary-input-port "port" (lambda args #t)))
338
339 (pass-if "make-custom-binary-input-port"
340 (let* ((source (make-bytevector 7777))
341 (read! (let ((pos 0)
342 (len (bytevector-length source)))
343 (lambda (bv start count)
344 (let ((amount (min count (- len pos))))
345 (if (> amount 0)
346 (bytevector-copy! source pos
347 bv start amount))
348 (set! pos (+ pos amount))
349 amount))))
350 (port (make-custom-binary-input-port "the port" read!
351 #f #f #f)))
352
353 (bytevector=? (get-bytevector-all port) source)))
354
355 (pass-if "custom binary input port does not support `port-position'"
356 (let* ((str "Hello Port!")
357 (source (open-bytevector-input-port
358 (u8-list->bytevector
359 (map char->integer (string->list str)))))
360 (read! (lambda (bv start count)
361 (let ((r (get-bytevector-n! source bv start count)))
362 (if (eof-object? r)
363 0
364 r))))
365 (port (make-custom-binary-input-port "the port" read!
366 #f #f #f)))
367 (not (or (port-has-port-position? port)
368 (port-has-set-port-position!? port)))))
369
370 (pass-if "custom binary input port supports `port-position'"
371 (let* ((str "Hello Port!")
372 (source (open-bytevector-input-port
373 (u8-list->bytevector
374 (map char->integer (string->list str)))))
375 (read! (lambda (bv start count)
376 (let ((r (get-bytevector-n! source bv start count)))
377 (if (eof-object? r)
378 0
379 r))))
380 (get-pos (lambda ()
381 (port-position source)))
382 (set-pos! (lambda (pos)
383 (set-port-position! source pos)))
384 (port (make-custom-binary-input-port "the port" read!
385 get-pos set-pos! #f)))
386
387 (and (port-has-port-position? port)
388 (= 0 (port-position port))
389 (port-has-set-port-position!? port)
390 (begin
391 (set-port-position! port 6)
392 (= 6 (port-position port)))
393 (bytevector=? (get-bytevector-all port)
394 (u8-list->bytevector
395 (map char->integer (string->list "Port!")))))))
396
397 (pass-if "custom binary input port `close-proc' is called"
398 (let* ((closed? #f)
399 (read! (lambda (bv start count) 0))
400 (get-pos (lambda () 0))
401 (set-pos! (lambda (pos) #f))
402 (close! (lambda () (set! closed? #t)))
403 (port (make-custom-binary-input-port "the port" read!
404 get-pos set-pos!
405 close!)))
406
407 (close-port port)
408 (gc) ; Test for marking a closed port.
409 closed?))
410
411 (pass-if "standard-input-port is binary"
412 (with-fluids ((%default-port-encoding "UTF-8"))
413 (binary-port? (standard-input-port)))))
414
415 \f
416 (with-test-prefix "8.2.10 Output ports"
417
418 (pass-if "open-bytevector-output-port"
419 (let-values (((port get-content)
420 (open-bytevector-output-port #f)))
421 (let ((source (make-bytevector 7777)))
422 (put-bytevector port source)
423 (and (bytevector=? (get-content) source)
424 (bytevector=? (get-content) (make-bytevector 0))))))
425
426 (pass-if "open-bytevector-output-port [extract after close]"
427 (let-values (((port get-content)
428 (open-bytevector-output-port)))
429 (let ((source (make-bytevector 12345 #xFE)))
430 (put-bytevector port source)
431 (close-port port)
432 (bytevector=? (get-content) source))))
433
434 (pass-if "open-bytevector-output-port [put-u8]"
435 (let-values (((port get-content)
436 (open-bytevector-output-port)))
437 (put-u8 port 77)
438 (and (bytevector=? (get-content) (make-bytevector 1 77))
439 (bytevector=? (get-content) (make-bytevector 0)))))
440
441 (pass-if "open-bytevector-output-port [display]"
442 (let-values (((port get-content)
443 (open-bytevector-output-port)))
444 (display "hello" port)
445 (and (bytevector=? (get-content) (string->utf8 "hello"))
446 (bytevector=? (get-content) (make-bytevector 0)))))
447
448 (pass-if "bytevector output port supports `port-position'"
449 (let-values (((port get-content)
450 (open-bytevector-output-port)))
451 (let ((source (make-bytevector 7777))
452 (overwrite (make-bytevector 33)))
453 (and (port-has-port-position? port)
454 (port-has-set-port-position!? port)
455 (begin
456 (put-bytevector port source)
457 (= (bytevector-length source)
458 (port-position port)))
459 (begin
460 (set-port-position! port 10)
461 (= 10 (port-position port)))
462 (begin
463 (put-bytevector port overwrite)
464 (bytevector-copy! overwrite 0 source 10
465 (bytevector-length overwrite))
466 (= (port-position port)
467 (+ 10 (bytevector-length overwrite))))
468 (bytevector=? (get-content) source)
469 (bytevector=? (get-content) (make-bytevector 0))))))
470
471 (pass-if "make-custom-binary-output"
472 (let ((port (make-custom-binary-output-port "cbop"
473 (lambda (x y z) 0)
474 #f #f #f)))
475 (and (output-port? port)
476 (binary-port? port)
477 (not (port-has-port-position? port))
478 (not (port-has-set-port-position!? port)))))
479
480 (pass-if "make-custom-binary-output-port [partial writes]"
481 (let* ((source (uint-list->bytevector (iota 333)
482 (native-endianness) 2))
483 (sink (make-bytevector (bytevector-length source)))
484 (sink-pos 0)
485 (eof? #f)
486 (write! (lambda (bv start count)
487 (if (= 0 count)
488 (begin
489 (set! eof? #t)
490 0)
491 (let ((u8 (bytevector-u8-ref bv start)))
492 ;; Get one byte at a time.
493 (bytevector-u8-set! sink sink-pos u8)
494 (set! sink-pos (+ 1 sink-pos))
495 1))))
496 (port (make-custom-binary-output-port "cbop" write!
497 #f #f #f)))
498 (put-bytevector port source)
499 (and (= sink-pos (bytevector-length source))
500 (not eof?)
501 (bytevector=? sink source))))
502
503 (pass-if "make-custom-binary-output-port [full writes]"
504 (let* ((source (uint-list->bytevector (iota 333)
505 (native-endianness) 2))
506 (sink (make-bytevector (bytevector-length source)))
507 (sink-pos 0)
508 (eof? #f)
509 (write! (lambda (bv start count)
510 (if (= 0 count)
511 (begin
512 (set! eof? #t)
513 0)
514 (begin
515 (bytevector-copy! bv start
516 sink sink-pos
517 count)
518 (set! sink-pos (+ sink-pos count))
519 count))))
520 (port (make-custom-binary-output-port "cbop" write!
521 #f #f #f)))
522 (put-bytevector port source)
523 (and (= sink-pos (bytevector-length source))
524 (not eof?)
525 (bytevector=? sink source))))
526
527 (pass-if "standard-output-port is binary"
528 (with-fluids ((%default-port-encoding "UTF-8"))
529 (binary-port? (standard-output-port))))
530
531 (pass-if "standard-error-port is binary"
532 (with-fluids ((%default-port-encoding "UTF-8"))
533 (binary-port? (standard-error-port)))))
534
535 \f
536 (with-test-prefix "8.2.6 Input and output ports"
537
538 (pass-if "transcoded-port [output]"
539 (let ((s "Hello\nÄÖÜ"))
540 (bytevector=?
541 (string->utf8 s)
542 (call-with-bytevector-output-port
543 (lambda (bv-port)
544 (call-with-port (transcoded-port bv-port (make-transcoder (utf-8-codec)))
545 (lambda (utf8-port)
546 (put-string utf8-port s))))))))
547
548 (pass-if "transcoded-port [input]"
549 (let ((s "Hello\nÄÖÜ"))
550 (string=?
551 s
552 (get-string-all
553 (transcoded-port (open-bytevector-input-port (string->utf8 s))
554 (make-transcoder (utf-8-codec)))))))
555
556 (pass-if "transcoded-port [input line]"
557 (string=? "ÄÖÜ"
558 (get-line (transcoded-port
559 (open-bytevector-input-port (string->utf8 "ÄÖÜ\nFooBar"))
560 (make-transcoder (utf-8-codec))))))
561
562 (pass-if "transcoded-port [error handling mode = raise]"
563 (let* ((t (make-transcoder (utf-8-codec) (native-eol-style)
564 (error-handling-mode raise)))
565 (b (open-bytevector-input-port #vu8(255 2 1)))
566 (tp (transcoded-port b t)))
567 (guard (c ((i/o-decoding-error? c)
568 (eq? (i/o-error-port c) tp)))
569 (get-line tp))))
570
571 (pass-if "transcoded-port [error handling mode = replace]"
572 (let* ((t (make-transcoder (utf-8-codec) (native-eol-style)
573 (error-handling-mode replace)))
574 (b (open-bytevector-input-port #vu8(255 1 2 3 103 110 117)))
575 (tp (transcoded-port b t)))
576 (string-suffix? "gnu" (get-line tp))))
577
578 (pass-if "transcoded-port, output [error handling mode = raise]"
579 (let-values (((p get)
580 (open-bytevector-output-port)))
581 (let* ((t (make-transcoder (latin-1-codec) (native-eol-style)
582 (error-handling-mode raise)))
583 (tp (transcoded-port p t)))
584 (guard (c ((i/o-encoding-error? c)
585 (and (eq? (i/o-error-port c) tp)
586 (char=? (i/o-encoding-error-char c) #\λ)
587 (bytevector=? (get) (string->utf8 "The letter ")))))
588 (put-string tp "The letter λ cannot be represented in Latin-1.")
589 #f))))
590
591 (pass-if "port-transcoder [binary port]"
592 (not (port-transcoder (open-bytevector-input-port #vu8()))))
593
594 (pass-if "port-transcoder [transcoded port]"
595 (let* ((p (transcoded-port (open-bytevector-input-port (string->utf8 "foo"))
596 (make-transcoder (utf-8-codec))))
597 (t (port-transcoder p)))
598 (and t
599 (transcoder-codec t)
600 (eq? (native-eol-style)
601 (transcoder-eol-style t))
602 (eq? (error-handling-mode replace)
603 (transcoder-error-handling-mode t))))))
604
605 (with-test-prefix "8.2.9 Textual input"
606
607 (pass-if "get-string-n [short]"
608 (let ((port (open-input-string "GNU Guile")))
609 (string=? "GNU " (get-string-n port 4))))
610 (pass-if "get-string-n [long]"
611 (let ((port (open-input-string "GNU Guile")))
612 (string=? "GNU Guile" (get-string-n port 256))))
613 (pass-if "get-string-n [eof]"
614 (let ((port (open-input-string "")))
615 (eof-object? (get-string-n port 4))))
616
617 (pass-if "get-string-n! [short]"
618 (let ((port (open-input-string "GNU Guile"))
619 (s (string-copy "Isn't XXX great?")))
620 (and (= 3 (get-string-n! port s 6 3))
621 (string=? s "Isn't GNU great?")))))
622
623 ;;; Local Variables:
624 ;;; mode: scheme
625 ;;; eval: (put 'guard 'scheme-indent-function 1)
626 ;;; End: