Merge remote-tracking branch 'origin/lexical-literals'
[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 (test-suite guile-test)
23 #:use-module (srfi srfi-1)
24 #:use-module (srfi srfi-11)
25 #:use-module (rnrs io ports)
26 #:use-module (rnrs io simple)
27 #:use-module (rnrs exceptions)
28 #:use-module (rnrs bytevectors))
29
30 (define-syntax pass-if-condition
31 (syntax-rules ()
32 ((_ name predicate body0 body ...)
33 (let ((cookie (list 'cookie)))
34 (pass-if name
35 (eq? cookie (guard (c ((predicate c) cookie))
36 body0 body ...)))))))
37
38 (define (test-file)
39 (data-file-name "ports-test.tmp"))
40
41 ;; A input/output port that swallows all output, and produces just
42 ;; spaces on input. Reading and writing beyond `failure-position'
43 ;; produces `system-error' exceptions. Used for testing exception
44 ;; behavior.
45 (define* (make-failing-port #:optional (failure-position 0))
46 (define (maybe-fail index errno)
47 (if (> index failure-position)
48 (scm-error 'system-error
49 'failing-port
50 "I/O beyond failure position" '()
51 (list errno))))
52 (let ((read-index 0)
53 (write-index 0))
54 (define (write-char chr)
55 (set! write-index (+ 1 write-index))
56 (maybe-fail write-index ENOSPC))
57 (make-soft-port
58 (vector write-char
59 (lambda (str) ;; write-string
60 (for-each write-char (string->list str)))
61 (lambda () #t) ;; flush-output
62 (lambda () ;; read-char
63 (set! read-index (+ read-index 1))
64 (maybe-fail read-index EIO)
65 #\space)
66 (lambda () #t)) ;; close-port
67 "rw")))
68
69 (define (call-with-bytevector-output-port/transcoded transcoder receiver)
70 (call-with-bytevector-output-port
71 (lambda (bv-port)
72 (call-with-port (transcoded-port bv-port transcoder)
73 receiver))))
74
75 \f
76 (with-test-prefix "7.2.5 End-of-File Object"
77
78 (pass-if "eof-object"
79 (and (eqv? (eof-object) (eof-object))
80 (eq? (eof-object) (eof-object))))
81
82 (pass-if "port-eof?"
83 (port-eof? (open-input-string ""))))
84
85 \f
86 (with-test-prefix "7.2.8 Binary Input"
87
88 (pass-if "get-u8"
89 (let ((port (open-input-string "A")))
90 (and (= (char->integer #\A) (get-u8 port))
91 (eof-object? (get-u8 port)))))
92
93 (pass-if "lookahead-u8"
94 (let ((port (open-input-string "A")))
95 (and (= (char->integer #\A) (lookahead-u8 port))
96 (= (char->integer #\A) (lookahead-u8 port))
97 (= (char->integer #\A) (get-u8 port))
98 (eof-object? (get-u8 port)))))
99
100 (pass-if "lookahead-u8 non-ASCII"
101 (let ((port (with-fluids ((%default-port-encoding "UTF-8"))
102 (open-input-string "λ"))))
103 (and (= 206 (lookahead-u8 port))
104 (= 206 (lookahead-u8 port))
105 (= 206 (get-u8 port))
106 (= 187 (lookahead-u8 port))
107 (= 187 (lookahead-u8 port))
108 (= 187 (get-u8 port))
109 (eof-object? (lookahead-u8 port))
110 (eof-object? (get-u8 port)))))
111
112 (pass-if "lookahead-u8: result is unsigned"
113 ;; Bug #31081.
114 (let ((port (open-bytevector-input-port #vu8(255))))
115 (= (lookahead-u8 port) 255)))
116
117 (pass-if "get-bytevector-n [short]"
118 (let* ((port (open-input-string "GNU Guile"))
119 (bv (get-bytevector-n port 4)))
120 (and (bytevector? bv)
121 (equal? (bytevector->u8-list bv)
122 (map char->integer (string->list "GNU "))))))
123
124 (pass-if "get-bytevector-n [long]"
125 (let* ((port (open-input-string "GNU Guile"))
126 (bv (get-bytevector-n port 256)))
127 (and (bytevector? bv)
128 (equal? (bytevector->u8-list bv)
129 (map char->integer (string->list "GNU Guile"))))))
130
131 (pass-if-exception "get-bytevector-n with closed port"
132 exception:wrong-type-arg
133
134 (let ((port (%make-void-port "r")))
135
136 (close-port port)
137 (get-bytevector-n port 3)))
138
139 (pass-if "get-bytevector-n! [short]"
140 (let* ((port (open-input-string "GNU Guile"))
141 (bv (make-bytevector 4))
142 (read (get-bytevector-n! port bv 0 4)))
143 (and (equal? read 4)
144 (equal? (bytevector->u8-list bv)
145 (map char->integer (string->list "GNU "))))))
146
147 (pass-if "get-bytevector-n! [long]"
148 (let* ((str "GNU Guile")
149 (port (open-input-string str))
150 (bv (make-bytevector 256))
151 (read (get-bytevector-n! port bv 0 256)))
152 (and (equal? read (string-length str))
153 (equal? (map (lambda (i)
154 (bytevector-u8-ref bv i))
155 (iota read))
156 (map char->integer (string->list str))))))
157
158 (pass-if "get-bytevector-some [simple]"
159 (let* ((str "GNU Guile")
160 (port (open-input-string str))
161 (bv (get-bytevector-some port)))
162 (and (bytevector? bv)
163 (equal? (bytevector->u8-list bv)
164 (map char->integer (string->list str))))))
165
166 (pass-if "get-bytevector-some [only-some]"
167 (let* ((str "GNU Guile")
168 (index 0)
169 (port (make-soft-port
170 (vector #f #f #f
171 (lambda ()
172 (if (>= index (string-length str))
173 (eof-object)
174 (let ((c (string-ref str index)))
175 (set! index (+ index 1))
176 c)))
177 (lambda () #t)
178 (lambda ()
179 ;; Number of readily available octets: falls to
180 ;; zero after 4 octets have been read.
181 (- 4 (modulo index 5))))
182 "r"))
183 (bv (get-bytevector-some port)))
184 (and (bytevector? bv)
185 (= index 4)
186 (= (bytevector-length bv) index)
187 (equal? (bytevector->u8-list bv)
188 (map char->integer (string->list "GNU "))))))
189
190 (pass-if "get-bytevector-all"
191 (let* ((str "GNU Guile")
192 (index 0)
193 (port (make-soft-port
194 (vector #f #f #f
195 (lambda ()
196 (if (>= index (string-length str))
197 (eof-object)
198 (let ((c (string-ref str index)))
199 (set! index (+ index 1))
200 c)))
201 (lambda () #t)
202 (let ((cont? #f))
203 (lambda ()
204 ;; Number of readily available octets: falls to
205 ;; zero after 4 octets have been read and then
206 ;; starts again.
207 (let ((a (if cont?
208 (- (string-length str) index)
209 (- 4 (modulo index 5)))))
210 (if (= 0 a) (set! cont? #t))
211 a))))
212 "r"))
213 (bv (get-bytevector-all port)))
214 (and (bytevector? bv)
215 (= index (string-length str))
216 (= (bytevector-length bv) (string-length str))
217 (equal? (bytevector->u8-list bv)
218 (map char->integer (string->list str)))))))
219
220 \f
221 (define (make-soft-output-port)
222 (let* ((bv (make-bytevector 1024))
223 (read-index 0)
224 (write-index 0)
225 (write-char (lambda (chr)
226 (bytevector-u8-set! bv write-index
227 (char->integer chr))
228 (set! write-index (+ 1 write-index)))))
229 (make-soft-port
230 (vector write-char
231 (lambda (str) ;; write-string
232 (for-each write-char (string->list str)))
233 (lambda () #t) ;; flush-output
234 (lambda () ;; read-char
235 (if (>= read-index (bytevector-length bv))
236 (eof-object)
237 (let ((c (bytevector-u8-ref bv read-index)))
238 (set! read-index (+ read-index 1))
239 (integer->char c))))
240 (lambda () #t)) ;; close-port
241 "rw")))
242
243 (with-test-prefix "7.2.11 Binary Output"
244
245 (pass-if "put-u8"
246 (let ((port (make-soft-output-port)))
247 (put-u8 port 77)
248 (equal? (get-u8 port) 77)))
249
250 ;; Note: The `put-bytevector' tests below require a Latin-1 locale so
251 ;; that the `scm_from_locale_stringn' call in `sf_write' will let all
252 ;; the bytes through, unmodified. This is hacky, but we can't use
253 ;; "custom binary output ports" here because they're only tested
254 ;; later.
255
256 (pass-if "put-bytevector [2 args]"
257 (with-latin1-locale
258 (let ((port (make-soft-output-port))
259 (bv (make-bytevector 256)))
260 (put-bytevector port bv)
261 (equal? (bytevector->u8-list bv)
262 (bytevector->u8-list
263 (get-bytevector-n port (bytevector-length bv)))))))
264
265 (pass-if "put-bytevector [3 args]"
266 (with-latin1-locale
267 (let ((port (make-soft-output-port))
268 (bv (make-bytevector 256))
269 (start 10))
270 (put-bytevector port bv start)
271 (equal? (drop (bytevector->u8-list bv) start)
272 (bytevector->u8-list
273 (get-bytevector-n port (- (bytevector-length bv) start)))))))
274
275 (pass-if "put-bytevector [4 args]"
276 (with-latin1-locale
277 (let ((port (make-soft-output-port))
278 (bv (make-bytevector 256))
279 (start 10)
280 (count 77))
281 (put-bytevector port bv start count)
282 (equal? (take (drop (bytevector->u8-list bv) start) count)
283 (bytevector->u8-list
284 (get-bytevector-n port count))))))
285
286 (pass-if-exception "put-bytevector with closed port"
287 exception:wrong-type-arg
288
289 (let* ((bv (make-bytevector 4))
290 (port (%make-void-port "w")))
291
292 (close-port port)
293 (put-bytevector port bv)))
294
295 (pass-if "put-bytevector with UTF-16 string port"
296 (let* ((str "hello, world")
297 (bv (string->utf16 str)))
298 (equal? str
299 (with-fluids ((%default-port-encoding "UTF-16BE"))
300 (call-with-output-string
301 (lambda (port)
302 (put-bytevector port bv)))))))
303
304 (pass-if "put-bytevector with wrong-encoding string port"
305 (let* ((str "hello, world")
306 (bv (string->utf16 str)))
307 (catch 'decoding-error
308 (lambda ()
309 (with-fluids ((%default-port-encoding "UTF-32"))
310 (call-with-output-string
311 (lambda (port)
312 (put-bytevector port bv)))))
313 (lambda (key subr message errno port)
314 (string? (strerror errno)))))))
315
316 \f
317 (with-test-prefix "7.2.7 Input Ports"
318
319 (let ((filename (test-file))
320 (contents (string->utf8 "GNU λ")))
321
322 ;; Create file
323 (call-with-output-file filename
324 (lambda (port) (put-bytevector port contents)))
325
326 (pass-if "open-file-input-port [opens binary port]"
327 (with-fluids ((%default-port-encoding "UTF-8"))
328 (call-with-port (open-file-input-port filename)
329 (lambda (port)
330 (and (binary-port? port)
331 (bytevector=? contents (get-bytevector-all port)))))))
332
333 (delete-file filename))
334
335 ;; This section appears here so that it can use the binary input
336 ;; primitives.
337
338 (pass-if "open-bytevector-input-port [1 arg]"
339 (let* ((str "Hello Port!")
340 (bv (u8-list->bytevector (map char->integer
341 (string->list str))))
342 (port (open-bytevector-input-port bv))
343 (read-to-string
344 (lambda (port)
345 (let loop ((chr (read-char port))
346 (result '()))
347 (if (eof-object? chr)
348 (apply string (reverse! result))
349 (loop (read-char port)
350 (cons chr result)))))))
351
352 (equal? (read-to-string port) str)))
353
354 (pass-if "bytevector-input-port is binary"
355 (with-fluids ((%default-port-encoding "UTF-8"))
356 (binary-port? (open-bytevector-input-port #vu8(1 2 3)))))
357
358 (pass-if-exception "bytevector-input-port is read-only"
359 exception:wrong-type-arg
360
361 (let* ((str "Hello Port!")
362 (bv (u8-list->bytevector (map char->integer
363 (string->list str))))
364 (port (open-bytevector-input-port bv #f)))
365
366 (write "hello" port)))
367
368 (pass-if "bytevector input port supports seeking"
369 (let* ((str "Hello Port!")
370 (bv (u8-list->bytevector (map char->integer
371 (string->list str))))
372 (port (open-bytevector-input-port bv #f)))
373
374 (and (port-has-port-position? port)
375 (= 0 (port-position port))
376 (port-has-set-port-position!? port)
377 (begin
378 (set-port-position! port 6)
379 (= 6 (port-position port)))
380 (bytevector=? (get-bytevector-all port)
381 (u8-list->bytevector
382 (map char->integer (string->list "Port!")))))))
383
384 (pass-if "bytevector input port can seek to very end"
385 (let ((empty (open-bytevector-input-port '#vu8()))
386 (not-empty (open-bytevector-input-port '#vu8(1 2 3))))
387 (and (begin (set-port-position! empty (port-position empty))
388 (= 0 (port-position empty)))
389 (begin (get-bytevector-n not-empty 3)
390 (set-port-position! not-empty (port-position not-empty))
391 (= 3 (port-position not-empty))))))
392
393 (pass-if-exception "make-custom-binary-input-port [wrong-num-args]"
394 exception:wrong-num-args
395
396 ;; Prior to Guile-R6RS-Libs 0.2, the last 3 arguments were wrongfully
397 ;; optional.
398 (make-custom-binary-input-port "port" (lambda args #t)))
399
400 (pass-if "make-custom-binary-input-port"
401 (let* ((source (make-bytevector 7777))
402 (read! (let ((pos 0)
403 (len (bytevector-length source)))
404 (lambda (bv start count)
405 (let ((amount (min count (- len pos))))
406 (if (> amount 0)
407 (bytevector-copy! source pos
408 bv start amount))
409 (set! pos (+ pos amount))
410 amount))))
411 (port (make-custom-binary-input-port "the port" read!
412 #f #f #f)))
413
414 (and (binary-port? port)
415 (input-port? port)
416 (bytevector=? (get-bytevector-all port) source))))
417
418 (pass-if "custom binary input port does not support `port-position'"
419 (let* ((str "Hello Port!")
420 (source (open-bytevector-input-port
421 (u8-list->bytevector
422 (map char->integer (string->list str)))))
423 (read! (lambda (bv start count)
424 (let ((r (get-bytevector-n! source bv start count)))
425 (if (eof-object? r)
426 0
427 r))))
428 (port (make-custom-binary-input-port "the port" read!
429 #f #f #f)))
430 (not (or (port-has-port-position? port)
431 (port-has-set-port-position!? port)))))
432
433 (pass-if "custom binary input port supports `port-position'"
434 (let* ((str "Hello Port!")
435 (source (open-bytevector-input-port
436 (u8-list->bytevector
437 (map char->integer (string->list str)))))
438 (read! (lambda (bv start count)
439 (let ((r (get-bytevector-n! source bv start count)))
440 (if (eof-object? r)
441 0
442 r))))
443 (get-pos (lambda ()
444 (port-position source)))
445 (set-pos! (lambda (pos)
446 (set-port-position! source pos)))
447 (port (make-custom-binary-input-port "the port" read!
448 get-pos set-pos! #f)))
449
450 (and (port-has-port-position? port)
451 (= 0 (port-position port))
452 (port-has-set-port-position!? port)
453 (begin
454 (set-port-position! port 6)
455 (= 6 (port-position port)))
456 (bytevector=? (get-bytevector-all port)
457 (u8-list->bytevector
458 (map char->integer (string->list "Port!")))))))
459
460 (pass-if "custom binary input port `close-proc' is called"
461 (let* ((closed? #f)
462 (read! (lambda (bv start count) 0))
463 (get-pos (lambda () 0))
464 (set-pos! (lambda (pos) #f))
465 (close! (lambda () (set! closed? #t)))
466 (port (make-custom-binary-input-port "the port" read!
467 get-pos set-pos!
468 close!)))
469
470 (close-port port)
471 (gc) ; Test for marking a closed port.
472 closed?))
473
474 (pass-if "standard-input-port is binary"
475 (with-fluids ((%default-port-encoding "UTF-8"))
476 (binary-port? (standard-input-port)))))
477
478 \f
479 (with-test-prefix "8.2.10 Output ports"
480
481 (let ((filename (test-file)))
482 (with-fluids ((%default-port-encoding "UTF-8"))
483 (pass-if "open-file-output-port [opens binary port]"
484 (call-with-port (open-file-output-port filename)
485 (lambda (port)
486 (put-bytevector port '#vu8(1 2 3))
487 (binary-port? port)))))
488
489 (pass-if-condition "open-file-output-port [exception: already-exists]"
490 i/o-file-already-exists-error?
491 (open-file-output-port filename))
492
493 (pass-if "open-file-output-port [no-fail no-truncate]"
494 (and
495 (call-with-port (open-file-output-port filename
496 (file-options no-fail no-truncate))
497 (lambda (port)
498 (= 0 (port-position port))))
499 (= 3 (stat:size (stat filename)))))
500
501 (pass-if "open-file-output-port [no-fail]"
502 (and
503 (call-with-port (open-file-output-port filename (file-options no-fail))
504 binary-port?)
505 (= 0 (stat:size (stat filename)))))
506
507 (delete-file filename)
508
509 (pass-if-condition "open-file-output-port [exception: does-not-exist]"
510 i/o-file-does-not-exist-error?
511 (open-file-output-port filename (file-options no-create))))
512
513 (pass-if "open-bytevector-output-port"
514 (let-values (((port get-content)
515 (open-bytevector-output-port #f)))
516 (let ((source (make-bytevector 7777)))
517 (put-bytevector port source)
518 (and (bytevector=? (get-content) source)
519 (bytevector=? (get-content) (make-bytevector 0))))))
520
521 (pass-if "bytevector-output-port is binary"
522 (binary-port? (open-bytevector-output-port)))
523
524 (pass-if "open-bytevector-output-port [extract after close]"
525 (let-values (((port get-content)
526 (open-bytevector-output-port)))
527 (let ((source (make-bytevector 12345 #xFE)))
528 (put-bytevector port source)
529 (close-port port)
530 (bytevector=? (get-content) source))))
531
532 (pass-if "open-bytevector-output-port [put-u8]"
533 (let-values (((port get-content)
534 (open-bytevector-output-port)))
535 (put-u8 port 77)
536 (and (bytevector=? (get-content) (make-bytevector 1 77))
537 (bytevector=? (get-content) (make-bytevector 0)))))
538
539 (pass-if "open-bytevector-output-port [display]"
540 (let-values (((port get-content)
541 (open-bytevector-output-port)))
542 (display "hello" port)
543 (and (bytevector=? (get-content) (string->utf8 "hello"))
544 (bytevector=? (get-content) (make-bytevector 0)))))
545
546 (pass-if "bytevector output port supports `port-position'"
547 (let-values (((port get-content)
548 (open-bytevector-output-port)))
549 (let ((source (make-bytevector 7777))
550 (overwrite (make-bytevector 33)))
551 (and (port-has-port-position? port)
552 (port-has-set-port-position!? port)
553 (begin
554 (put-bytevector port source)
555 (= (bytevector-length source)
556 (port-position port)))
557 (begin
558 (set-port-position! port 10)
559 (= 10 (port-position port)))
560 (begin
561 (put-bytevector port overwrite)
562 (bytevector-copy! overwrite 0 source 10
563 (bytevector-length overwrite))
564 (= (port-position port)
565 (+ 10 (bytevector-length overwrite))))
566 (bytevector=? (get-content) source)
567 (bytevector=? (get-content) (make-bytevector 0))))))
568
569 (pass-if "make-custom-binary-output-port"
570 (let ((port (make-custom-binary-output-port "cbop"
571 (lambda (x y z) 0)
572 #f #f #f)))
573 (and (output-port? port)
574 (binary-port? port)
575 (not (port-has-port-position? port))
576 (not (port-has-set-port-position!? port)))))
577
578 (pass-if "make-custom-binary-output-port [partial writes]"
579 (let* ((source (uint-list->bytevector (iota 333)
580 (native-endianness) 2))
581 (sink (make-bytevector (bytevector-length source)))
582 (sink-pos 0)
583 (eof? #f)
584 (write! (lambda (bv start count)
585 (if (= 0 count)
586 (begin
587 (set! eof? #t)
588 0)
589 (let ((u8 (bytevector-u8-ref bv start)))
590 ;; Get one byte at a time.
591 (bytevector-u8-set! sink sink-pos u8)
592 (set! sink-pos (+ 1 sink-pos))
593 1))))
594 (port (make-custom-binary-output-port "cbop" write!
595 #f #f #f)))
596 (put-bytevector port source)
597 (and (= sink-pos (bytevector-length source))
598 (not eof?)
599 (bytevector=? sink source))))
600
601 (pass-if "make-custom-binary-output-port [full writes]"
602 (let* ((source (uint-list->bytevector (iota 333)
603 (native-endianness) 2))
604 (sink (make-bytevector (bytevector-length source)))
605 (sink-pos 0)
606 (eof? #f)
607 (write! (lambda (bv start count)
608 (if (= 0 count)
609 (begin
610 (set! eof? #t)
611 0)
612 (begin
613 (bytevector-copy! bv start
614 sink sink-pos
615 count)
616 (set! sink-pos (+ sink-pos count))
617 count))))
618 (port (make-custom-binary-output-port "cbop" write!
619 #f #f #f)))
620 (put-bytevector port source)
621 (and (= sink-pos (bytevector-length source))
622 (not eof?)
623 (bytevector=? sink source))))
624
625 (pass-if "standard-output-port is binary"
626 (with-fluids ((%default-port-encoding "UTF-8"))
627 (binary-port? (standard-output-port))))
628
629 (pass-if "standard-error-port is binary"
630 (with-fluids ((%default-port-encoding "UTF-8"))
631 (binary-port? (standard-error-port)))))
632
633 \f
634 (with-test-prefix "8.2.6 Input and output ports"
635
636 (pass-if "transcoded-port [output]"
637 (let ((s "Hello\nÄÖÜ"))
638 (bytevector=?
639 (string->utf8 s)
640 (call-with-bytevector-output-port/transcoded (make-transcoder (utf-8-codec))
641 (lambda (utf8-port)
642 (put-string utf8-port s))))))
643
644 (pass-if "transcoded-port [input]"
645 (let ((s "Hello\nÄÖÜ"))
646 (string=?
647 s
648 (get-string-all
649 (transcoded-port (open-bytevector-input-port (string->utf8 s))
650 (make-transcoder (utf-8-codec)))))))
651
652 (pass-if "transcoded-port [input line]"
653 (string=? "ÄÖÜ"
654 (get-line (transcoded-port
655 (open-bytevector-input-port (string->utf8 "ÄÖÜ\nFooBar"))
656 (make-transcoder (utf-8-codec))))))
657
658 (pass-if "transcoded-port [error handling mode = raise]"
659 (let* ((t (make-transcoder (utf-8-codec) (native-eol-style)
660 (error-handling-mode raise)))
661 (b (open-bytevector-input-port #vu8(255 2 1)))
662 (tp (transcoded-port b t)))
663 (guard (c ((i/o-decoding-error? c)
664 (eq? (i/o-error-port c) tp)))
665 (get-line tp))))
666
667 (pass-if "transcoded-port [error handling mode = replace]"
668 (let* ((t (make-transcoder (utf-8-codec) (native-eol-style)
669 (error-handling-mode replace)))
670 (b (open-bytevector-input-port #vu8(255 1 2 3 103 110 117)))
671 (tp (transcoded-port b t)))
672 (string-suffix? "gnu" (get-line tp))))
673
674 (pass-if "transcoded-port, output [error handling mode = raise]"
675 (let-values (((p get)
676 (open-bytevector-output-port)))
677 (let* ((t (make-transcoder (latin-1-codec) (native-eol-style)
678 (error-handling-mode raise)))
679 (tp (transcoded-port p t)))
680 (guard (c ((i/o-encoding-error? c)
681 (and (eq? (i/o-error-port c) tp)
682 (char=? (i/o-encoding-error-char c) #\λ)
683 (bytevector=? (get) (string->utf8 "The letter ")))))
684 (put-string tp "The letter λ cannot be represented in Latin-1.")
685 #f))))
686
687 (pass-if "port-transcoder [binary port]"
688 (not (port-transcoder (open-bytevector-input-port #vu8()))))
689
690 (pass-if "port-transcoder [transcoded port]"
691 (let* ((p (transcoded-port (open-bytevector-input-port (string->utf8 "foo"))
692 (make-transcoder (utf-8-codec))))
693 (t (port-transcoder p)))
694 (and t
695 (transcoder-codec t)
696 (eq? (native-eol-style)
697 (transcoder-eol-style t))
698 (eq? (error-handling-mode replace)
699 (transcoder-error-handling-mode t))))))
700
701 (with-test-prefix "8.2.9 Textual input"
702
703 (pass-if "get-string-n [short]"
704 (let ((port (open-input-string "GNU Guile")))
705 (string=? "GNU " (get-string-n port 4))))
706 (pass-if "get-string-n [long]"
707 (let ((port (open-input-string "GNU Guile")))
708 (string=? "GNU Guile" (get-string-n port 256))))
709 (pass-if "get-string-n [eof]"
710 (let ((port (open-input-string "")))
711 (eof-object? (get-string-n port 4))))
712
713 (pass-if "get-string-n! [short]"
714 (let ((port (open-input-string "GNU Guile"))
715 (s (string-copy "Isn't XXX great?")))
716 (and (= 3 (get-string-n! port s 6 3))
717 (string=? s "Isn't GNU great?"))))
718
719 (with-test-prefix "read error"
720 (pass-if-condition "get-char" i/o-read-error?
721 (get-char (make-failing-port)))
722 (pass-if-condition "lookahead-char" i/o-read-error?
723 (lookahead-char (make-failing-port)))
724 ;; FIXME: these are not yet exception-correct
725 #|
726 (pass-if-condition "get-string-n" i/o-read-error?
727 (get-string-n (make-failing-port) 5))
728 (pass-if-condition "get-string-n!" i/o-read-error?
729 (get-string-n! (make-failing-port) (make-string 5) 0 5))
730 |#
731 (pass-if-condition "get-string-all" i/o-read-error?
732 (get-string-all (make-failing-port 100)))
733 (pass-if-condition "get-line" i/o-read-error?
734 (get-line (make-failing-port)))
735 (pass-if-condition "get-datum" i/o-read-error?
736 (get-datum (make-failing-port)))))
737
738 (define (encoding-error-predicate char)
739 (lambda (c)
740 (and (i/o-encoding-error? c)
741 (char=? char (i/o-encoding-error-char c)))))
742
743 (with-test-prefix "8.2.12 Textual Output"
744
745 (with-test-prefix "write error"
746 (pass-if-condition "put-char" i/o-write-error?
747 (put-char (make-failing-port) #\G))
748 (pass-if-condition "put-string" i/o-write-error?
749 (put-string (make-failing-port) "Hello World!"))
750 (pass-if-condition "put-datum" i/o-write-error?
751 (put-datum (make-failing-port) '(hello world!))))
752 (with-test-prefix "encoding error"
753 (pass-if-condition "put-char" (encoding-error-predicate #\λ)
754 (call-with-bytevector-output-port/transcoded
755 (make-transcoder (latin-1-codec)
756 (native-eol-style)
757 (error-handling-mode raise))
758 (lambda (port)
759 (put-char port #\λ))))
760 (pass-if-condition "put-string" (encoding-error-predicate #\λ)
761 (call-with-bytevector-output-port/transcoded
762 (make-transcoder (latin-1-codec)
763 (native-eol-style)
764 (error-handling-mode raise))
765 (lambda (port)
766 (put-string port "FooλBar"))))))
767
768 (with-test-prefix "8.3 Simple I/O"
769 (with-test-prefix "read error"
770 (pass-if-condition "read-char" i/o-read-error?
771 (read-char (make-failing-port)))
772 (pass-if-condition "peek-char" i/o-read-error?
773 (peek-char (make-failing-port)))
774 (pass-if-condition "read" i/o-read-error?
775 (read (make-failing-port))))
776 (with-test-prefix "write error"
777 (pass-if-condition "display" i/o-write-error?
778 (display "Hi there!" (make-failing-port)))
779 (pass-if-condition "write" i/o-write-error?
780 (write '(hi there!) (make-failing-port)))
781 (pass-if-condition "write-char" i/o-write-error?
782 (write-char #\G (make-failing-port)))
783 (pass-if-condition "newline" i/o-write-error?
784 (newline (make-failing-port))))
785 (let ((filename (test-file)))
786 ;; ensure the test file exists
787 (call-with-output-file filename
788 (lambda (port) (write "foo" port)))
789 (pass-if "call-with-input-file [port is textual]"
790 (call-with-input-file filename textual-port?))
791 (pass-if-condition "call-with-input-file [exception: not-found]"
792 i/o-file-does-not-exist-error?
793 (call-with-input-file ",this-is-highly-unlikely-to-exist!"
794 values))
795 (pass-if-condition "call-with-output-file [exception: already-exists]"
796 i/o-file-already-exists-error?
797 (call-with-output-file filename
798 values))
799 (delete-file filename)))
800
801 ;;; Local Variables:
802 ;;; mode: scheme
803 ;;; eval: (put 'guard 'scheme-indent-function 1)
804 ;;; End: