1 ;;;; ports.test --- Guile I/O ports. -*- coding: utf-8; mode: scheme; -*-
2 ;;;; Jim Blandy <jimb@red-bean.com> --- May 1999
4 ;;;; Copyright (C) 1999, 2001, 2004, 2006, 2007, 2009, 2010,
5 ;;;; 2011, 2012, 2013, 2014 Free Software Foundation, Inc.
7 ;;;; This library is free software; you can redistribute it and/or
8 ;;;; modify it under the terms of the GNU Lesser General Public
9 ;;;; License as published by the Free Software Foundation; either
10 ;;;; version 3 of the License, or (at your option) any later version.
12 ;;;; This library is distributed in the hope that it will be useful,
13 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 ;;;; Lesser General Public License for more details.
17 ;;;; You should have received a copy of the GNU Lesser General Public
18 ;;;; License along with this library; if not, write to the Free Software
19 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 (define-module (test-suite test-ports)
22 #:use-module (test-suite lib)
23 #:use-module (test-suite guile-test)
24 #:use-module (ice-9 popen)
25 #:use-module (ice-9 rdelim)
26 #:use-module (rnrs bytevectors)
27 #:use-module ((ice-9 binary-ports) #:select (open-bytevector-input-port
28 open-bytevector-output-port
34 (define (display-line . args)
35 (for-each display args)
39 (data-file-name "ports-test.tmp"))
42 ;;;; Some general utilities for testing ports.
44 ;; Make sure we are set up for 8-bit Latin-1 data.
45 (fluid-set! %default-port-encoding "ISO-8859-1")
47 (set-port-encoding! p (fluid-ref %default-port-encoding)))
48 (list (current-input-port) (current-output-port)
49 (current-error-port)))
51 ;;; Read from PORT until EOF, and return the result as a string.
52 (define (read-all port)
53 (let loop ((chars '()))
54 (let ((char (read-char port)))
55 (if (eof-object? char)
56 (list->string (reverse! chars))
57 (loop (cons char chars))))))
59 (define (read-file filename)
60 (let* ((port (open-input-file filename))
61 (string (read-all port)))
67 (with-test-prefix "%default-port-conversion-strategy"
69 (pass-if "initial value"
70 (eq? 'substitute (fluid-ref %default-port-conversion-strategy)))
73 (let ((strategies '(error substitute escape)))
74 (equal? (map (lambda (s)
75 (with-fluids ((%default-port-conversion-strategy s))
76 (call-with-output-file "/dev/null"
78 (port-conversion-strategy p)))))
82 (pass-if "(set-port-conversion-strategy! #f sym)"
84 (set-port-conversion-strategy! #f 'error)
85 (and (eq? (fluid-ref %default-port-conversion-strategy) 'error)
87 (set-port-conversion-strategy! #f 'substitute)
88 (eq? (fluid-ref %default-port-conversion-strategy)
94 ;;;; Normal file ports.
96 ;;; Write out an s-expression, and read it back.
97 (let ((string '("From fairest creatures we desire increase,"
98 "That thereby beauty's rose might never die,"))
99 (filename (test-file)))
100 (let ((port (open-output-file filename)))
103 (let ((port (open-input-file filename)))
104 (let ((in-string (read port)))
105 (pass-if "file: write and read back list of strings"
106 (equal? string in-string)))
108 (delete-file filename))
110 ;;; Write out a string, and read it back a character at a time.
111 (let ((string "This is a test string\nwith no newline at the end")
112 (filename (test-file)))
113 (let ((port (open-output-file filename)))
114 (display string port)
116 (let ((in-string (read-file filename)))
117 (pass-if "file: write and read back characters"
118 (equal? string in-string)))
119 (delete-file filename))
121 ;;; Buffered input/output port with seeking.
122 (let* ((filename (test-file))
123 (port (open-file filename "w+")))
124 (display "J'Accuse" port)
125 (seek port -1 SEEK_CUR)
126 (pass-if "file: r/w 1"
127 (char=? (read-char port) #\e))
128 (pass-if "file: r/w 2"
129 (eof-object? (read-char port)))
130 (seek port -1 SEEK_CUR)
131 (write-char #\x port)
132 (seek port 7 SEEK_SET)
133 (pass-if "file: r/w 3"
134 (char=? (read-char port) #\x))
135 (seek port -2 SEEK_END)
136 (pass-if "file: r/w 4"
137 (char=? (read-char port) #\s))
139 (delete-file filename))
141 ;;; Unbuffered input/output port with seeking.
142 (let* ((filename (test-file))
143 (port (open-file filename "w+0")))
144 (display "J'Accuse" port)
145 (seek port -1 SEEK_CUR)
146 (pass-if "file: ub r/w 1"
147 (char=? (read-char port) #\e))
148 (pass-if "file: ub r/w 2"
149 (eof-object? (read-char port)))
150 (seek port -1 SEEK_CUR)
151 (write-char #\x port)
152 (seek port 7 SEEK_SET)
153 (pass-if "file: ub r/w 3"
154 (char=? (read-char port) #\x))
155 (seek port -2 SEEK_END)
156 (pass-if "file: ub r/w 4"
157 (char=? (read-char port) #\s))
159 (delete-file filename))
161 ;;; Buffered output-only and input-only ports with seeking.
162 (let* ((filename (test-file))
163 (port (open-output-file filename)))
164 (display "J'Accuse" port)
165 (pass-if "file: out tell"
166 (= (seek port 0 SEEK_CUR) 8))
167 (seek port -1 SEEK_CUR)
168 (write-char #\x port)
170 (let ((iport (open-input-file filename)))
171 (pass-if "file: in tell 0"
172 (= (seek iport 0 SEEK_CUR) 0))
174 (pass-if "file: in tell 1"
175 (= (seek iport 0 SEEK_CUR) 1))
176 (unread-char #\z iport)
177 (pass-if "file: in tell 0 after unread"
178 (= (seek iport 0 SEEK_CUR) 0))
179 (pass-if "file: unread char still there"
180 (char=? (read-char iport) #\z))
181 (seek iport 7 SEEK_SET)
182 (pass-if "file: in last char"
183 (char=? (read-char iport) #\x))
185 (delete-file filename))
187 ;;; unusual characters.
188 (let* ((filename (test-file))
189 (port (open-output-file filename)))
190 (display (string #\nul (integer->char 255) (integer->char 128)
193 (let* ((port (open-input-file filename))
194 (line (read-line port)))
195 (pass-if "file: read back NUL 1"
196 (char=? (string-ref line 0) #\nul))
197 (pass-if "file: read back 255"
198 (char=? (string-ref line 1) (integer->char 255)))
199 (pass-if "file: read back 128"
200 (char=? (string-ref line 2) (integer->char 128)))
201 (pass-if "file: read back NUL 2"
202 (char=? (string-ref line 3) #\nul))
204 (eof-object? (read-char port)))
206 (delete-file filename))
208 ;;; line buffering mode.
209 (let* ((filename (test-file))
210 (port (open-file filename "wl"))
211 (test-string "one line more or less"))
212 (write-line test-string port)
213 (let* ((in-port (open-input-file filename))
214 (line (read-line in-port)))
217 (pass-if "file: line buffering"
218 (string=? line test-string)))
219 (delete-file filename))
221 ;;; read-line should use the port encoding (not the locale encoding).
222 (let ((str "ĉu bone?"))
224 (let* ((filename (test-file))
225 (port (open-file filename "wl")))
226 (set-port-encoding! port "UTF-8")
227 (write-line str port)
228 (let ((in-port (open-input-file filename)))
229 (set-port-encoding! in-port "UTF-8")
230 (let ((line (read-line in-port)))
233 (pass-if "file: read-line honors port encoding"
234 (string=? line str))))
235 (delete-file filename))))
237 ;;; binary mode ignores port encoding
238 (pass-if "file: binary mode ignores port encoding"
239 (with-fluids ((%default-port-encoding "UTF-8"))
240 (let* ((filename (test-file))
241 (port (open-file filename "w"))
247 (string->utf8 test-string))))))
248 (write-line test-string port)
250 (let* ((in-port (open-file filename "rb"))
251 (line (read-line in-port)))
253 (delete-file filename)
254 (string=? line binary-test-string)))))
256 ;;; binary mode ignores file coding declaration
257 (pass-if "file: binary mode ignores file coding declaration"
258 (with-fluids ((%default-port-encoding "UTF-8"))
259 (let* ((filename (test-file))
260 (port (open-file filename "w"))
266 (string->utf8 test-string))))))
267 (write-line ";; coding: utf-8" port)
268 (write-line test-string port)
270 (let* ((in-port (open-file filename "rb"))
271 (line1 (read-line in-port))
272 (line2 (read-line in-port)))
274 (delete-file filename)
275 (string=? line2 binary-test-string)))))
277 ;; open-file ignores file coding declaration by default
278 (pass-if "file: open-file ignores coding declaration by default"
279 (with-fluids ((%default-port-encoding "UTF-8"))
280 (let* ((filename (test-file))
281 (port (open-output-file filename))
282 (test-string "€100"))
283 (write-line ";; coding: iso-8859-15" port)
284 (write-line test-string port)
286 (let* ((in-port (open-input-file filename))
287 (line1 (read-line in-port))
288 (line2 (read-line in-port)))
290 (delete-file filename)
291 (string=? line2 test-string)))))
293 ;; open-input-file with guess-encoding honors coding declaration
294 (pass-if "file: open-input-file with guess-encoding honors coding declaration"
295 (with-fluids ((%default-port-encoding "UTF-8"))
296 (let* ((filename (test-file))
297 (port (open-output-file filename))
298 (test-string "€100"))
299 (set-port-encoding! port "iso-8859-15")
300 (write-line ";; coding: iso-8859-15" port)
301 (write-line test-string port)
303 (let* ((in-port (open-input-file filename
304 #:guess-encoding #t))
305 (line1 (read-line in-port))
306 (line2 (read-line in-port)))
308 (delete-file filename)
309 (string=? line2 test-string)))))
311 (with-test-prefix "keyword arguments for file openers"
312 (with-fluids ((%default-port-encoding "UTF-8"))
313 (let ((filename (test-file)))
315 (with-test-prefix "write #:encoding"
317 (pass-if-equal "open-file"
318 #vu8(116 0 101 0 115 0 116 0)
319 (let ((port (open-file filename "w"
320 #:encoding "UTF-16LE")))
321 (display "test" port)
323 (let* ((port (open-file filename "rb"))
324 (bv (get-bytevector-all port)))
328 (pass-if-equal "open-output-file"
329 #vu8(116 0 101 0 115 0 116 0)
330 (let ((port (open-output-file filename
331 #:encoding "UTF-16LE")))
332 (display "test" port)
334 (let* ((port (open-file filename "rb"))
335 (bv (get-bytevector-all port)))
339 (pass-if-equal "call-with-output-file"
340 #vu8(116 0 101 0 115 0 116 0)
341 (call-with-output-file filename
343 (display "test" port))
344 #:encoding "UTF-16LE")
345 (let* ((port (open-file filename "rb"))
346 (bv (get-bytevector-all port)))
350 (pass-if-equal "with-output-to-file"
351 #vu8(116 0 101 0 115 0 116 0)
352 (with-output-to-file filename
355 #:encoding "UTF-16LE")
356 (let* ((port (open-file filename "rb"))
357 (bv (get-bytevector-all port)))
361 (pass-if-equal "with-error-to-file"
362 #vu8(116 0 101 0 115 0 116 0)
366 (display "test" (current-error-port)))
367 #:encoding "UTF-16LE")
368 (let* ((port (open-file filename "rb"))
369 (bv (get-bytevector-all port)))
373 (with-test-prefix "write #:binary"
375 (pass-if-equal "open-output-file"
377 (let* ((port (open-output-file filename #:binary #t))
378 (enc (port-encoding port)))
382 (pass-if-equal "call-with-output-file"
384 (call-with-output-file filename port-encoding #:binary #t))
386 (pass-if-equal "with-output-to-file"
388 (with-output-to-file filename
389 (lambda () (port-encoding (current-output-port)))
392 (pass-if-equal "with-error-to-file"
396 (lambda () (port-encoding (current-error-port)))
399 (with-test-prefix "read #:encoding"
401 (pass-if-equal "open-file read #:encoding"
403 (call-with-output-file filename
405 (put-bytevector port #vu8(116 0 101 0 115 0 116 0))))
406 (let* ((port (open-file filename "r" #:encoding "UTF-16LE"))
407 (str (read-string port)))
411 (pass-if-equal "open-input-file #:encoding"
413 (call-with-output-file filename
415 (put-bytevector port #vu8(116 0 101 0 115 0 116 0))))
416 (let* ((port (open-input-file filename #:encoding "UTF-16LE"))
417 (str (read-string port)))
421 (pass-if-equal "call-with-input-file #:encoding"
423 (call-with-output-file filename
425 (put-bytevector port #vu8(116 0 101 0 115 0 116 0))))
426 (call-with-input-file filename
428 #:encoding "UTF-16LE"))
430 (pass-if-equal "with-input-from-file #:encoding"
432 (call-with-output-file filename
434 (put-bytevector port #vu8(116 0 101 0 115 0 116 0))))
435 (with-input-from-file filename
437 #:encoding "UTF-16LE")))
439 (with-test-prefix "read #:binary"
441 (pass-if-equal "open-input-file"
443 (let* ((port (open-input-file filename #:binary #t))
444 (enc (port-encoding port)))
448 (pass-if-equal "call-with-input-file"
450 (call-with-input-file filename port-encoding #:binary #t))
452 (pass-if-equal "with-input-from-file"
454 (with-input-from-file filename
455 (lambda () (port-encoding (current-input-port)))
458 (with-test-prefix "#:guess-encoding with coding declaration"
460 (pass-if-equal "open-file"
462 (with-output-to-file filename
465 (write-line "; coding: ISO-8859-15")
467 #:encoding "ISO-8859-15")
468 (let* ((port (open-file filename "r"
470 #:encoding "UTF-16LE"))
471 (str (begin (read-line port)
477 (pass-if-equal "open-input-file"
479 (with-output-to-file filename
482 (write-line "; coding: ISO-8859-15")
484 #:encoding "ISO-8859-15")
485 (let* ((port (open-input-file filename
487 #:encoding "UTF-16LE"))
488 (str (begin (read-line port)
494 (pass-if-equal "call-with-input-file"
496 (with-output-to-file filename
499 (write-line "; coding: ISO-8859-15")
501 #:encoding "ISO-8859-15")
502 (call-with-input-file filename
508 #:encoding "UTF-16LE"))
510 (pass-if-equal "with-input-from-file"
512 (with-output-to-file filename
515 (write-line "; coding: ISO-8859-15")
517 #:encoding "ISO-8859-15")
518 (with-input-from-file filename
524 #:encoding "UTF-16LE")))
526 (with-test-prefix "#:guess-encoding without coding declaration"
528 (pass-if-equal "open-file"
530 (with-output-to-file filename
531 (lambda () (write-line "€100"))
532 #:encoding "ISO-8859-15")
533 (let* ((port (open-file filename "r"
535 #:encoding "ISO-8859-15"))
536 (str (read-line port)))
540 (pass-if-equal "open-input-file"
542 (with-output-to-file filename
543 (lambda () (write-line "€100"))
544 #:encoding "ISO-8859-15")
545 (let* ((port (open-input-file filename
547 #:encoding "ISO-8859-15"))
548 (str (read-line port)))
552 (pass-if-equal "call-with-input-file"
554 (with-output-to-file filename
555 (lambda () (write-line "€100"))
556 #:encoding "ISO-8859-15")
557 (call-with-input-file filename
560 #:encoding "ISO-8859-15"))
562 (pass-if-equal "with-input-from-file"
564 (with-output-to-file filename
565 (lambda () (write-line "€100"))
566 #:encoding "ISO-8859-15")
567 (with-input-from-file filename
570 #:encoding "ISO-8859-15")))
572 (delete-file filename))))
574 ;;; ungetting characters and strings.
575 (with-input-from-string "walk on the moon\nmoon"
578 (unread-char #\a (current-input-port))
579 (pass-if "unread-char"
580 (char=? (read-char) #\a))
582 (let ((replacenoid "chicken enchilada"))
583 (unread-char #\newline (current-input-port))
584 (unread-string replacenoid (current-input-port))
585 (pass-if "unread-string"
586 (string=? (read-line) replacenoid)))
587 (pass-if "unread residue"
588 (string=? (read-line) "moon"))))
590 ;;; non-blocking mode on a port. create a pipe and set O_NONBLOCK on
591 ;;; the reading end. try to read a byte: should get EAGAIN or
592 ;;; EWOULDBLOCK error.
595 (fcntl r F_SETFL (logior (fcntl r F_GETFL) O_NONBLOCK))
596 (pass-if "non-blocking-I/O"
598 (lambda () (read-char r) #f)
600 (and (eq? key 'system-error)
601 (let ((errno (car (list-ref args 3))))
603 (= errno EWOULDBLOCK))))))))
606 ;;;; Pipe (popen) ports.
608 ;;; Run a command, and read its output.
609 (let* ((pipe (open-pipe "echo 'Howdy there, partner!'" "r"))
610 (in-string (read-all pipe)))
612 (pass-if "pipe: read"
613 (equal? in-string "Howdy there, partner!\n")))
615 ;;; Run a command, send some output to it, and see if it worked.
616 (let* ((filename (test-file))
617 (pipe (open-pipe (string-append "grep Mommy > " filename) "w")))
618 (display "Now Jimmy lives on a mushroom cloud\n" pipe)
619 (display "Mommy, why does everybody have a bomb?\n" pipe)
621 (let ((in-string (read-file filename)))
622 (pass-if "pipe: write"
623 (equal? in-string "Mommy, why does everybody have a bomb?\n")))
624 (delete-file filename))
626 (pass-if-equal "pipe, fdopen, and _IOLBF"
628 (let ((in+out (pipe))
629 (pid (primitive-fork)))
634 (close-port (car in+out))
635 (let ((port (cdr in+out)))
636 (setvbuf port _IOLBF )
637 ;; Strings containing '\n' or should be flushed; others
638 ;; should be kept in PORT's buffer.
639 (display "foo\n" port)
640 (display "bar\n" port)
641 (display "this will be kept in PORT's buffer" port)))
643 (primitive-_exit 0)))
645 (close-port (cdr in+out))
646 (let ((str (read-all (car in+out))))
651 ;;;; Void ports. These are so trivial we don't test them.
656 (with-test-prefix "string ports"
658 ;; Write text to a string port.
659 (let* ((string "Howdy there, partner!")
660 (in-string (call-with-output-string
662 (display string port)
664 (pass-if "display text"
665 (equal? in-string (string-append string "\n"))))
667 ;; Write an s-expression to a string port.
668 (let* ((sexpr '("more utterly random text" 1729 #(a vector) 3.1415926))
670 (call-with-input-string (call-with-output-string
674 (pass-if "write/read sexpr"
675 (equal? in-sexpr sexpr)))
677 ;; seeking and unreading from an input string.
678 (let ((text "that text didn't look random to me"))
679 (call-with-input-string text
681 (pass-if "input tell 0"
682 (= (seek p 0 SEEK_CUR) 0))
684 (pass-if "input tell 1"
685 (= (seek p 0 SEEK_CUR) 1))
687 (pass-if "input tell back to 0"
688 (= (seek p 0 SEEK_CUR) 0))
689 (pass-if "input ungetted char"
690 (char=? (read-char p) #\x))
692 (pass-if "input seek to end"
693 (= (seek p 0 SEEK_CUR)
694 (string-length text)))
696 (pass-if "input seek to beginning"
697 (= (seek p 0 SEEK_SET) 0))
698 (pass-if "input reread first char"
699 (char=? (read-char p)
700 (string-ref text 0))))))
702 ;; seeking an output string.
703 (let* ((text (string-copy "123456789"))
704 (len (string-length text))
705 (result (call-with-output-string
707 (pass-if "output tell 0"
708 (= (seek p 0 SEEK_CUR) 0))
710 (pass-if "output tell end"
711 (= (seek p 0 SEEK_CUR) len))
712 (pass-if "output seek to beginning"
713 (= (seek p 0 SEEK_SET) 0))
716 (pass-if "output seek to last char"
717 (= (seek p 0 SEEK_CUR)
719 (write-char #\b p)))))
720 (string-set! text 0 #\a)
721 (string-set! text (- len 1) #\b)
722 (pass-if "output check"
723 (string=? text result)))
725 (pass-if "%default-port-encoding is ignored"
726 (let ((str "ĉu bone?"))
727 ;; Latin-1 cannot represent ‘ĉ’.
728 (with-fluids ((%default-port-encoding "ISO-8859-1"))
729 (string=? (call-with-output-string
731 (set-port-conversion-strategy! p 'substitute)
735 (pass-if "%default-port-conversion-strategy is honored"
736 (let ((strategies '(error substitute escape)))
737 (equal? (map (lambda (s)
738 (with-fluids ((%default-port-conversion-strategy s))
739 (call-with-output-string
741 (and (eq? s (port-conversion-strategy p))
743 (set-port-conversion-strategy! p s)
744 (display (port-conversion-strategy p)
747 (map symbol->string strategies))))
749 (pass-if "suitable encoding [latin-1]"
750 (let ((str "hello, world")
751 (encoding "ISO-8859-1"))
753 (call-with-output-string
755 (set-port-encoding! p encoding)
758 (pass-if "suitable encoding [latin-3]"
759 (let ((str "ĉu bone?")
760 (encoding "ISO-8859-3"))
762 (call-with-output-string
764 (set-port-encoding! p encoding)
767 (pass-if "wrong encoding, error"
768 (let ((str "ĉu bone?"))
769 (catch 'encoding-error
771 (with-fluids ((%default-port-conversion-strategy 'error))
772 (call-with-output-string
774 ;; Latin-1 cannot represent ‘ĉ’.
775 (set-port-encoding! p "ISO-8859-1")
777 #f) ; so the test really fails here
778 (lambda (key subr message errno port chr)
780 (string? (strerror errno)))))))
782 (pass-if "wrong encoding, substitute"
783 (let ((str "ĉu bone?"))
784 (string=? (call-with-output-string
786 (set-port-encoding! p "ISO-8859-1")
787 (set-port-conversion-strategy! p 'substitute)
791 (pass-if "wrong encoding, escape"
792 (let ((str "ĉu bone?"))
793 (string=? (call-with-output-string
795 (set-port-encoding! p "ISO-8859-1")
796 (set-port-conversion-strategy! p 'escape)
801 (let ((p (open-input-string "안녕하세요")))
802 (and (char=? (peek-char p) #\안)
803 (char=? (peek-char p) #\안)
804 (char=? (peek-char p) #\안)
806 (= (port-column p) 0))))
808 ;; Mini DSL to test decoding error handling.
809 (letrec-syntax ((decoding-error?
812 (catch 'decoding-error
816 (lambda (key subr message errno p)
818 (not (= 0 errno))))))))
820 (syntax-rules (-> error eof)
821 ((_ port (proc -> error))
823 (port-conversion-strategy port))
824 (eqv? (proc port) #\?)
825 (decoding-error? port (proc port))))
826 ((_ port (proc -> eof))
827 (eof-object? (proc port)))
828 ((_ port (proc -> char))
829 (eqv? (proc port) char))))
833 (and (make-check port check) ...))))
834 (make-peek+read-checks
836 ((_ port (result ...) e1 expected ...)
837 (make-peek+read-checks port
842 ((_ port (result ...))
843 (make-checks port result ...))
844 ((_ port #f e1 expected ...)
845 (make-peek+read-checks port
850 (test-decoding-error*
852 ((_ sequence encoding strategy (expected ...))
854 (pass-if (format #f "test-decoding-error: ~s ~s ~s"
855 'sequence encoding strategy)
856 (let ((p (open-bytevector-input-port
857 (u8-list->bytevector 'sequence))))
858 (set-port-encoding! p encoding)
859 (set-port-conversion-strategy! p strategy)
861 (read-char -> expected) ...)))
863 ;; Generate the same test, but with one
864 ;; `peek-char' call before each `read-char'.
865 ;; Both should yield the same result.
866 (pass-if (format #f "test-decoding-error: ~s ~s ~s + peek-char"
867 'sequence encoding strategy)
868 (let ((p (open-bytevector-input-port
869 (u8-list->bytevector 'sequence))))
870 (set-port-encoding! p encoding)
871 (set-port-conversion-strategy! p strategy)
872 (make-peek+read-checks p #f expected
876 ((_ sequence encoding (expected ...))
878 (test-decoding-error* sequence encoding 'error
881 ;; `escape' should behave exactly like `error'.
882 (test-decoding-error* sequence encoding 'escape
885 (test-decoding-error* sequence encoding 'substitute
888 (test-decoding-error (255 65 66 67) "UTF-8"
889 (error #\A #\B #\C eof))
891 (test-decoding-error (255 206 187 206 188) "UTF-8"
894 (test-decoding-error (206 187 206) "UTF-8"
895 ;; Unterminated sequence.
898 ;; Check how ill-formed UTF-8 sequences are handled (see Table 3-7
899 ;; of the "Conformance" chapter of Unicode 6.0.0.)
901 (test-decoding-error (#xc0 #x80 #x41) "UTF-8"
902 (error ;; C0: should be in the C2..DF range
907 (test-decoding-error (#xc2 #x41 #x42) "UTF-8"
908 ;; Section 3.9 of Unicode 6.0.0 reads:
909 ;; "If the converter encounters an ill-formed UTF-8 code unit
910 ;; sequence which starts with a valid first byte, but which does
911 ;; not continue with valid successor bytes (see Table 3-7), it
912 ;; must not consume the successor bytes".
913 ;; Glibc/libiconv do not conform to it and instead swallow the
914 ;; #x41. This example appears literally in Section 3.9.
915 (error ;; 41: invalid successor
916 #\A ;; 41: valid starting byte
920 (test-decoding-error (#xf0 #x80 #x80 #x41) "UTF-8"
921 ;; According to Unicode 6.0.0, Section 3.9, "the only formal
922 ;; requirement mandated by Unicode conformance for a converter is
923 ;; that the <41> be processed and correctly interpreted as
925 (error ;; 2nd byte should be in the A0..BF range
926 error ;; 80: not a valid starting byte
927 error ;; 80: not a valid starting byte
931 (test-decoding-error (#xe0 #xa0 #x41 #x42) "UTF-8"
932 (error ;; 3rd byte should be in the 80..BF range
937 (test-decoding-error (#xf0 #x88 #x88 #x88) "UTF-8"
938 (error ;; 2nd byte should be in the 90..BF range
939 error ;; 88: not a valid starting byte
940 error ;; 88: not a valid starting byte
941 error ;; 88: not a valid starting byte
944 (with-test-prefix "call-with-output-string"
946 ;; In Guile 1.6.4, closing the port resulted in a segv, check that doesn't
948 (pass-if-exception "proc closes port" exception:wrong-type-arg
949 (call-with-output-string close-port)))
953 ;;;; Soft ports. No tests implemented yet.
956 ;;;; Generic operations across all port types.
958 (let ((port-loop-temp (test-file)))
960 ;; Return a list of input ports that all return the same text.
961 ;; We map tests over this list.
962 (define (input-port-list text)
964 ;; Create a text file some of the ports will use.
965 (let ((out-port (open-output-file port-loop-temp)))
966 (display text out-port)
967 (close-port out-port))
969 (list (open-input-file port-loop-temp)
970 (open-input-pipe (string-append "cat " port-loop-temp))
971 (call-with-input-string text (lambda (x) x))
972 ;; We don't test soft ports at the moment.
975 (define port-list-names '("file" "pipe" "string"))
977 ;; Test the line counter.
978 (define (test-line-counter text second-line final-column)
979 (with-test-prefix "line counter"
980 (let ((ports (input-port-list text)))
982 (lambda (port port-name)
983 (with-test-prefix port-name
984 (pass-if "at beginning of input"
985 (= (port-line port) 0))
986 (pass-if "read first character"
987 (eqv? (read-char port) #\x))
988 (pass-if "after reading one character"
989 (= (port-line port) 0))
990 (pass-if "read first newline"
991 (eqv? (read-char port) #\newline))
992 (pass-if "after reading first newline char"
993 (= (port-line port) 1))
994 (pass-if "second line read correctly"
995 (equal? (read-line port) second-line))
996 (pass-if "read-line increments line number"
997 (= (port-line port) 2))
998 (pass-if "read-line returns EOF"
1001 ((eof-object? (read-line port)) #t)
1003 (else (loop (+ i 1))))))
1004 (pass-if "line count is 5 at EOF"
1005 (= (port-line port) 5))
1006 (pass-if "column is correct at EOF"
1007 (= (port-column port) final-column))))
1008 ports port-list-names)
1009 (for-each close-port ports)
1010 (delete-file port-loop-temp))))
1012 (with-test-prefix "newline"
1014 (string-append "x\n"
1015 "He who receives an idea from me, receives instruction\n"
1016 "himself without lessening mine; as he who lights his\n"
1017 "taper at mine, receives light without darkening me.\n"
1018 " --- Thomas Jefferson\n")
1019 "He who receives an idea from me, receives instruction"
1022 (with-test-prefix "no newline"
1024 (string-append "x\n"
1025 "He who receives an idea from me, receives instruction\n"
1026 "himself without lessening mine; as he who lights his\n"
1027 "taper at mine, receives light without darkening me.\n"
1028 " --- Thomas Jefferson\n"
1030 "He who receives an idea from me, receives instruction"
1033 ;; Test port-line and port-column for output ports
1035 (define (test-output-line-counter text final-column)
1036 (with-test-prefix "port-line and port-column for output ports"
1037 (let ((port (open-output-string)))
1038 (pass-if "at beginning of input"
1039 (and (= (port-line port) 0)
1040 (= (port-column port) 0)))
1041 (write-char #\x port)
1042 (pass-if "after writing one character"
1043 (and (= (port-line port) 0)
1044 (= (port-column port) 1)))
1045 (write-char #\newline port)
1046 (pass-if "after writing first newline char"
1047 (and (= (port-line port) 1)
1048 (= (port-column port) 0)))
1050 (pass-if "line count is 5 at end"
1051 (= (port-line port) 5))
1052 (pass-if "column is correct at end"
1053 (= (port-column port) final-column)))))
1055 (test-output-line-counter
1056 (string-append "He who receives an idea from me, receives instruction\n"
1057 "himself without lessening mine; as he who lights his\n"
1058 "taper at mine, receives light without darkening me.\n"
1059 " --- Thomas Jefferson\n"
1063 (with-test-prefix "port-column"
1065 (with-test-prefix "output"
1068 (let ((port (open-output-string)))
1070 (= 1 (port-column port))))
1073 (let ((port (open-output-string)))
1075 (= 0 (port-column port))))
1078 (let ((port (open-output-string)))
1079 (display "x\a" port)
1080 (= 1 (port-column port))))
1082 (pass-if "\\x08 backspace"
1083 (let ((port (open-output-string)))
1084 (display "\x08" port)
1085 (= 0 (port-column port))))
1087 (pass-if "x\\x08 backspace"
1088 (let ((port (open-output-string)))
1089 (display "x\x08" port)
1090 (= 0 (port-column port))))
1093 (let ((port (open-output-string)))
1095 (= 0 (port-column port))))
1098 (let ((port (open-output-string)))
1099 (display "x\n" port)
1100 (= 0 (port-column port))))
1103 (let ((port (open-output-string)))
1105 (= 0 (port-column port))))
1108 (let ((port (open-output-string)))
1109 (display "x\r" port)
1110 (= 0 (port-column port))))
1113 (let ((port (open-output-string)))
1115 (= 8 (port-column port))))
1118 (let ((port (open-output-string)))
1119 (display "x\t" port)
1120 (= 8 (port-column port)))))
1122 (with-test-prefix "input"
1125 (let ((port (open-input-string "x")))
1126 (while (not (eof-object? (read-char port))))
1127 (= 1 (port-column port))))
1130 (let ((port (open-input-string "\a")))
1131 (while (not (eof-object? (read-char port))))
1132 (= 0 (port-column port))))
1135 (let ((port (open-input-string "x\a")))
1136 (while (not (eof-object? (read-char port))))
1137 (= 1 (port-column port))))
1139 (pass-if "\\x08 backspace"
1140 (let ((port (open-input-string "\x08")))
1141 (while (not (eof-object? (read-char port))))
1142 (= 0 (port-column port))))
1144 (pass-if "x\\x08 backspace"
1145 (let ((port (open-input-string "x\x08")))
1146 (while (not (eof-object? (read-char port))))
1147 (= 0 (port-column port))))
1150 (let ((port (open-input-string "\n")))
1151 (while (not (eof-object? (read-char port))))
1152 (= 0 (port-column port))))
1155 (let ((port (open-input-string "x\n")))
1156 (while (not (eof-object? (read-char port))))
1157 (= 0 (port-column port))))
1160 (let ((port (open-input-string "\r")))
1161 (while (not (eof-object? (read-char port))))
1162 (= 0 (port-column port))))
1165 (let ((port (open-input-string "x\r")))
1166 (while (not (eof-object? (read-char port))))
1167 (= 0 (port-column port))))
1170 (let ((port (open-input-string "\t")))
1171 (while (not (eof-object? (read-char port))))
1172 (= 8 (port-column port))))
1175 (let ((port (open-input-string "x\t")))
1176 (while (not (eof-object? (read-char port))))
1177 (= 8 (port-column port))))))
1179 (with-test-prefix "port-line"
1181 ;; in guile 1.8.1 and earlier port-line was truncated to an int, whereas
1182 ;; scm_t_port actually holds a long; this restricted the range on 64-bit
1184 (pass-if "set most-positive-fixnum/2"
1185 (let ((n (quotient most-positive-fixnum 2))
1186 (port (open-output-string)))
1187 (set-port-line! port n)
1188 (eqv? n (port-line port)))))
1190 (with-test-prefix "port-encoding"
1192 (pass-if-exception "set-port-encoding!, wrong encoding"
1193 exception:miscellaneous-error
1194 (let ((p (open-input-string "")))
1195 (set-port-encoding! p "does-not-exist")
1198 (let ((filename (test-file)))
1199 (with-output-to-file filename (lambda () (write 'test)))
1201 (pass-if-exception "%default-port-encoding, wrong encoding"
1202 exception:miscellaneous-error
1203 (read (with-fluids ((%default-port-encoding "does-not-exist"))
1204 (open-input-file filename))))
1206 (delete-file filename)))
1212 (with-test-prefix "port-for-each"
1214 ;; In guile 1.8.0 through 1.8.2, port-for-each could pass a freed cell to
1215 ;; its iterator func if a port was inaccessible in the last gc mark but
1216 ;; the lazy sweeping has not yet reached it to remove it from the port
1217 ;; table (scm_i_port_table). Provoking those gc conditions is a little
1218 ;; tricky, but the following code made it happen in 1.8.2.
1219 (pass-if "passing freed cell"
1221 ;; clear out the heap
1223 ;; allocate cells so the opened ports aren't at the start of the heap
1225 (open-input-file "/dev/null")
1227 (open-input-file "/dev/null")
1228 ;; this gc leaves the above ports unmarked, ie. inaccessible
1230 ;; but they're still in the port table, so this sees them
1231 (port-for-each (lambda (port)
1232 (set! lst (cons port lst))))
1233 ;; this forces completion of the sweeping
1235 ;; and (if the bug is present) the cells accumulated in LST are now
1236 ;; freed cells, which give #f from `port?'
1237 (not (memq #f (map port? lst))))))
1241 (pass-if "fdes->ports finds port"
1242 (let* ((port (open-file (test-file) "w"))
1243 (res (not (not (memq port (fdes->ports (port->fdes port)))))))
1251 (with-test-prefix "seek"
1253 (with-test-prefix "file port"
1256 (call-with-output-file (test-file)
1258 (display "abcde" port)))
1259 (let ((port (open-file (test-file) "r")))
1261 (seek port 2 SEEK_CUR)
1262 (let ((res (eqv? #\d (read-char port))))
1267 (call-with-output-file (test-file)
1269 (display "abcde" port)))
1270 (let ((port (open-file (test-file) "r")))
1272 (seek port 3 SEEK_SET)
1273 (let ((res (eqv? #\d (read-char port))))
1278 (call-with-output-file (test-file)
1280 (display "abcde" port)))
1281 (let ((port (open-file (test-file) "r")))
1283 (seek port -2 SEEK_END)
1284 (let ((res (eqv? #\d (read-char port))))
1292 (with-test-prefix "truncate-file"
1294 (pass-if-exception "flonum file" exception:wrong-type-arg
1295 (truncate-file 1.0 123))
1297 (pass-if-exception "frac file" exception:wrong-type-arg
1298 (truncate-file 7/3 123))
1300 (with-test-prefix "filename"
1302 (pass-if-exception "flonum length" exception:wrong-type-arg
1303 (call-with-output-file (test-file)
1305 (display "hello" port)))
1306 (truncate-file (test-file) 1.0))
1309 (call-with-output-file (test-file)
1311 (display "hello" port)))
1312 (truncate-file (test-file) 1)
1313 (eqv? 1 (stat:size (stat (test-file)))))
1315 (pass-if-exception "shorten to current pos" exception:miscellaneous-error
1316 (call-with-output-file (test-file)
1318 (display "hello" port)))
1319 (truncate-file (test-file))))
1321 (with-test-prefix "file descriptor"
1324 (call-with-output-file (test-file)
1326 (display "hello" port)))
1327 (let ((fd (open-fdes (test-file) O_RDWR)))
1328 (truncate-file fd 1)
1330 (eqv? 1 (stat:size (stat (test-file)))))
1332 (pass-if "shorten to current pos"
1333 (call-with-output-file (test-file)
1335 (display "hello" port)))
1336 (let ((fd (open-fdes (test-file) O_RDWR)))
1337 (seek fd 1 SEEK_SET)
1340 (eqv? 1 (stat:size (stat (test-file))))))
1342 (with-test-prefix "file port"
1345 (call-with-output-file (test-file)
1347 (display "hello" port)))
1348 (let ((port (open-file (test-file) "r+")))
1349 (truncate-file port 1)
1351 (eqv? 1 (stat:size (stat (test-file)))))
1353 (pass-if "shorten to current pos"
1354 (call-with-output-file (test-file)
1356 (display "hello" port)))
1357 (let ((port (open-file (test-file) "r+")))
1359 (truncate-file port)
1361 (eqv? 1 (stat:size (stat (test-file)))))))
1364 ;;;; testing read-delimited and friends
1366 (with-test-prefix "read-delimited!"
1367 (let ((c (make-string 20 #\!)))
1368 (call-with-input-string
1372 (read-delimited! "\n" c port 'concat)
1373 (pass-if "read-delimited! reads a first line"
1374 (string=? c "defdef\n!!!!!!!!!!!!!"))
1376 (read-delimited! "\n" c port 'concat 3)
1377 (pass-if "read-delimited! reads a first line"
1378 (string=? c "defghighi\n!!!!!!!!!!"))))))
1383 (call-with-input-string
1386 (pass-if "char-ready? returns true on string port"
1387 (char-ready? port))))
1389 ;;; This segfaults on some versions of Guile. We really should run
1390 ;;; the tests in a subprocess...
1392 (call-with-input-string
1395 (with-input-from-port
1398 (pass-if "char-ready? returns true on string port as default port"
1402 ;;;; pending-eof behavior
1404 (with-test-prefix "pending EOF behavior"
1405 ;; Make a test port that will produce the given sequence. Each
1406 ;; element of 'lst' may be either a character or #f (which means EOF).
1407 (define (test-soft-port . lst)
1409 (vector (lambda (c) #f) ; write char
1410 (lambda (s) #f) ; write string
1411 (lambda () #f) ; flush
1412 (lambda () ; read char
1413 (let ((c (car lst)))
1414 (set! lst (cdr lst))
1416 (lambda () #f)) ; close
1419 (define (call-with-port p proc)
1422 (lambda () (proc p))
1423 (lambda () (close-port p))))
1425 (define (call-with-test-file str proc)
1426 (let ((filename (test-file)))
1428 (lambda () (call-with-output-file filename
1429 (lambda (p) (display str p))))
1430 (lambda () (call-with-input-file filename proc))
1431 (lambda () (delete-file (test-file))))))
1433 (pass-if "peek-char does not swallow EOF (soft port)"
1434 (call-with-port (test-soft-port #\a #f #\b)
1436 (and (char=? #\a (peek-char p))
1437 (char=? #\a (read-char p))
1438 (eof-object? (peek-char p))
1439 (eof-object? (read-char p))
1440 (char=? #\b (peek-char p))
1441 (char=? #\b (read-char p))))))
1443 (pass-if "unread clears pending EOF (soft port)"
1444 (call-with-port (test-soft-port #\a #f #\b)
1446 (and (char=? #\a (read-char p))
1447 (eof-object? (peek-char p))
1448 (begin (unread-char #\u p)
1449 (char=? #\u (read-char p)))))))
1451 (pass-if "unread clears pending EOF (string port)"
1452 (call-with-input-string "a"
1454 (and (char=? #\a (read-char p))
1455 (eof-object? (peek-char p))
1456 (begin (unread-char #\u p)
1457 (char=? #\u (read-char p)))))))
1459 (pass-if "unread clears pending EOF (file port)"
1460 (call-with-test-file
1463 (and (char=? #\a (read-char p))
1464 (eof-object? (peek-char p))
1465 (begin (unread-char #\u p)
1466 (char=? #\u (read-char p)))))))
1468 (pass-if "seek clears pending EOF (string port)"
1469 (call-with-input-string "a"
1471 (and (char=? #\a (read-char p))
1472 (eof-object? (peek-char p))
1473 (begin (seek p 0 SEEK_SET)
1474 (char=? #\a (read-char p)))))))
1476 (pass-if "seek clears pending EOF (file port)"
1477 (call-with-test-file
1480 (and (char=? #\a (read-char p))
1481 (eof-object? (peek-char p))
1482 (begin (seek p 0 SEEK_SET)
1483 (char=? #\a (read-char p))))))))
1486 ;;;; Close current-input-port, and make sure everyone can handle it.
1488 (with-test-prefix "closing current-input-port"
1489 (for-each (lambda (procedure name)
1490 (with-input-from-port
1491 (call-with-input-string "foo" (lambda (p) p))
1493 (close-port (current-input-port))
1494 (pass-if-exception name
1495 exception:wrong-type-arg
1497 (list read read-char read-line)
1498 '("read" "read-char" "read-line")))
1502 (with-test-prefix "setvbuf"
1504 (pass-if-exception "closed port"
1505 exception:wrong-type-arg
1506 (let ((port (open-input-file "/dev/null")))
1508 (setvbuf port _IOFBF)))
1510 (pass-if-exception "string port"
1511 exception:wrong-type-arg
1512 (let ((port (open-input-string "Hey!")))
1514 (setvbuf port _IOFBF)))
1516 (pass-if "line/column number preserved"
1517 ;; In Guile 2.0.5, `setvbuf' would erroneously decrease the port's
1518 ;; line and/or column number.
1519 (call-with-output-file (test-file)
1521 (display "This is GNU Guile.\nWelcome." p)))
1522 (call-with-input-file (test-file)
1524 (and (eqv? #\T (read-char p))
1525 (let ((line (port-line p))
1526 (col (port-column p)))
1527 (and (= line 0) (= col 1)
1529 (setvbuf p _IOFBF 777)
1530 (let ((line* (port-line p))
1531 (col* (port-column p)))
1533 (= col col*)))))))))))
1537 (pass-if-equal "unget-bytevector"
1538 #vu8(10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 200 201 202 203
1539 1 2 3 4 251 253 254 255)
1540 (let ((port (open-bytevector-input-port #vu8(1 2 3 4 251 253 254 255))))
1541 (unget-bytevector port #vu8(200 201 202 203))
1542 (unget-bytevector port #vu8(20 21 22 23 24))
1543 (unget-bytevector port #vu8(10 11 12 13 14 15 16 17 18 19) 4)
1544 (unget-bytevector port #vu8(10 11 12 13 14 15 16 17 18 19) 2 2)
1545 (unget-bytevector port #vu8(10 11))
1546 (get-bytevector-all port)))
1550 (with-test-prefix "unicode byte-order marks (BOMs)"
1552 (define (bv-read-test* encoding bv proc)
1553 (let ((port (open-bytevector-input-port bv)))
1554 (set-port-encoding! port encoding)
1557 (define (bv-read-test encoding bv)
1558 (bv-read-test* encoding bv read-string))
1560 (define (bv-write-test* encoding proc)
1562 (lambda () (open-bytevector-output-port))
1563 (lambda (port get-bytevector)
1564 (set-port-encoding! port encoding)
1568 (define (bv-write-test encoding str)
1569 (bv-write-test* encoding
1573 (pass-if-equal "BOM not discarded from Latin-1 stream"
1575 (bv-read-test "ISO-8859-1" #vu8(#xEF #xBB #xBF #x61)))
1577 (pass-if-equal "BOM not discarded from Latin-2 stream"
1578 "\u010F\u0165\u017C\x61"
1579 (bv-read-test "ISO-8859-2" #vu8(#xEF #xBB #xBF #x61)))
1581 (pass-if-equal "BOM not discarded from UTF-16BE stream"
1583 (bv-read-test "UTF-16BE" #vu8(#xFE #xFF #x00 #x61)))
1585 (pass-if-equal "BOM not discarded from UTF-16LE stream"
1587 (bv-read-test "UTF-16LE" #vu8(#xFF #xFE #x61 #x00)))
1589 (pass-if-equal "BOM not discarded from UTF-32BE stream"
1591 (bv-read-test "UTF-32BE" #vu8(#x00 #x00 #xFE #xFF
1592 #x00 #x00 #x00 #x61)))
1594 (pass-if-equal "BOM not discarded from UTF-32LE stream"
1596 (bv-read-test "UTF-32LE" #vu8(#xFF #xFE #x00 #x00
1597 #x61 #x00 #x00 #x00)))
1599 (pass-if-equal "BOM not written to UTF-8 stream"
1601 (bv-write-test "UTF-8" "a"))
1603 (pass-if-equal "BOM not written to UTF-16BE stream"
1605 (bv-write-test "UTF-16BE" "a"))
1607 (pass-if-equal "BOM not written to UTF-16LE stream"
1609 (bv-write-test "UTF-16LE" "a"))
1611 (pass-if-equal "BOM not written to UTF-32BE stream"
1612 #vu8(#x00 #x00 #x00 #x61)
1613 (bv-write-test "UTF-32BE" "a"))
1615 (pass-if-equal "BOM not written to UTF-32LE stream"
1616 #vu8(#x61 #x00 #x00 #x00)
1617 (bv-write-test "UTF-32LE" "a"))
1619 (pass-if "Don't read from the port unless user asks to"
1620 (let* ((p (make-soft-port
1622 (lambda (c) #f) ; write char
1623 (lambda (s) #f) ; write string
1624 (lambda () #f) ; flush
1625 (lambda () (throw 'fail)) ; read char
1628 (set-port-encoding! p "UTF-16")
1630 (set-port-encoding! p "UTF-32")
1634 ;; TODO: test that input and output streams are independent when
1635 ;; appropriate, and linked when appropriate.
1637 (pass-if-equal "BOM discarded from start of UTF-8 stream"
1639 (bv-read-test "Utf-8" #vu8(#xEF #xBB #xBF #x61)))
1641 (pass-if-equal "BOM discarded from start of UTF-8 stream after seek to 0"
1643 (bv-read-test* "uTf-8" #vu8(#xEF #xBB #xBF #x61)
1645 (let ((c (read-char p)))
1647 (let ((s (read-string p)))
1650 (pass-if-equal "Only one BOM discarded from start of UTF-8 stream"
1652 (bv-read-test "UTF-8" #vu8(#xEF #xBB #xBF #xEF #xBB #xBF #x61)))
1654 (pass-if-equal "BOM not discarded from UTF-8 stream after seek to > 0"
1656 (bv-read-test* "UTF-8" #vu8(#x61 #xEF #xBB #xBF #x62)
1661 (pass-if-equal "BOM not discarded unless at start of UTF-8 stream"
1663 (bv-read-test "UTF-8" #vu8(#x61 #xEF #xBB #xBF #x62)))
1665 (pass-if-equal "BOM (BE) written to start of UTF-16 stream"
1666 #vu8(#xFE #xFF #x00 #x61 #x00 #x62)
1667 (bv-write-test "UTF-16" "ab"))
1669 (pass-if-equal "BOM (BE) written to UTF-16 stream after set-port-encoding!"
1670 #vu8(#xFE #xFF #x00 #x61 #x00 #x62 #xFE #xFF #x00 #x63 #x00 #x64)
1671 (bv-write-test* "UTF-16"
1674 (set-port-encoding! p "UTF-16")
1677 (pass-if-equal "BOM discarded from start of UTF-16 stream (BE)"
1679 (bv-read-test "UTF-16" #vu8(#xFE #xFF #x00 #x61)))
1681 (pass-if-equal "BOM discarded from start of UTF-16 stream (BE) after seek to 0"
1683 (bv-read-test* "utf-16" #vu8(#xFE #xFF #x00 #x61)
1685 (let ((c (read-char p)))
1687 (let ((s (read-string p)))
1690 (pass-if-equal "Only one BOM discarded from start of UTF-16 stream (BE)"
1692 (bv-read-test "Utf-16" #vu8(#xFE #xFF #xFE #xFF #x00 #x61)))
1694 (pass-if-equal "BOM not discarded from UTF-16 stream (BE) after seek to > 0"
1696 (bv-read-test* "uTf-16" #vu8(#xFE #xFF #xFE #xFF #x00 #x61)
1701 (pass-if-equal "BOM not discarded unless at start of UTF-16 stream"
1703 (bv-read-test "utf-16" #vu8(#x00 #x61 #xFE #xFF #x00 #x62)))
1705 (pass-if-equal "BOM discarded from start of UTF-16 stream (LE)"
1707 (bv-read-test "UTF-16" #vu8(#xFF #xFE #x61 #x00)))
1709 (pass-if-equal "BOM discarded from start of UTF-16 stream (LE) after seek to 0"
1711 (bv-read-test* "Utf-16" #vu8(#xFF #xFE #x61 #x00)
1713 (let ((c (read-char p)))
1715 (let ((s (read-string p)))
1718 (pass-if-equal "Only one BOM discarded from start of UTF-16 stream (LE)"
1720 (bv-read-test "UTf-16" #vu8(#xFF #xFE #xFF #xFE #x61 #x00)))
1722 (pass-if-equal "BOM discarded from start of UTF-32 stream (BE)"
1724 (bv-read-test "UTF-32" #vu8(#x00 #x00 #xFE #xFF
1725 #x00 #x00 #x00 #x61)))
1727 (pass-if-equal "BOM discarded from start of UTF-32 stream (BE) after seek to 0"
1729 (bv-read-test* "utF-32" #vu8(#x00 #x00 #xFE #xFF
1730 #x00 #x00 #x00 #x61)
1732 (let ((c (read-char p)))
1734 (let ((s (read-string p)))
1737 (pass-if-equal "Only one BOM discarded from start of UTF-32 stream (BE)"
1739 (bv-read-test "UTF-32" #vu8(#x00 #x00 #xFE #xFF
1741 #x00 #x00 #x00 #x61)))
1743 (pass-if-equal "BOM not discarded from UTF-32 stream (BE) after seek to > 0"
1745 (bv-read-test* "UtF-32" #vu8(#x00 #x00 #xFE #xFF
1747 #x00 #x00 #x00 #x61)
1752 (pass-if-equal "BOM discarded within UTF-16 stream (BE) after set-port-encoding!"
1754 (bv-read-test* "UTF-16" #vu8(#x00 #x61 #xFE #xFF #x00 #x62)
1756 (let ((a (read-char p)))
1757 (set-port-encoding! p "UTF-16")
1758 (string a (read-char p))))))
1760 (pass-if-equal "BOM discarded within UTF-16 stream (LE,BE) after set-port-encoding!"
1762 (bv-read-test* "utf-16" #vu8(#x00 #x61 #xFF #xFE #x62 #x00)
1764 (let ((a (read-char p)))
1765 (set-port-encoding! p "UTF-16")
1766 (string a (read-char p))))))
1768 (pass-if-equal "BOM discarded within UTF-32 stream (BE) after set-port-encoding!"
1770 (bv-read-test* "UTF-32" #vu8(#x00 #x00 #x00 #x61
1772 #x00 #x00 #x00 #x62)
1774 (let ((a (read-char p)))
1775 (set-port-encoding! p "UTF-32")
1776 (string a (read-char p))))))
1778 (pass-if-equal "BOM discarded within UTF-32 stream (LE,BE) after set-port-encoding!"
1780 (bv-read-test* "UTF-32" #vu8(#x00 #x00 #x00 #x61
1782 #x62 #x00 #x00 #x00)
1784 (let ((a (read-char p)))
1785 (set-port-encoding! p "UTF-32")
1786 (string a (read-char p))))))
1788 (pass-if-equal "BOM not discarded unless at start of UTF-32 stream"
1790 (bv-read-test "UTF-32" #vu8(#x00 #x00 #x00 #x61
1792 #x00 #x00 #x00 #x62)))
1794 (pass-if-equal "BOM discarded from start of UTF-32 stream (LE)"
1796 (bv-read-test "UTF-32" #vu8(#xFF #xFE #x00 #x00
1797 #x61 #x00 #x00 #x00)))
1799 (pass-if-equal "BOM discarded from start of UTF-32 stream (LE) after seek to 0"
1801 (bv-read-test* "UTf-32" #vu8(#xFF #xFE #x00 #x00
1802 #x61 #x00 #x00 #x00)
1804 (let ((c (read-char p)))
1806 (let ((s (read-string p)))
1809 (pass-if-equal "Only one BOM discarded from start of UTF-32 stream (LE)"
1811 (bv-read-test "UTF-32" #vu8(#xFF #xFE #x00 #x00
1813 #x61 #x00 #x00 #x00))))
1817 (define-syntax-rule (with-load-path path body ...)
1822 (set! %load-path new))
1826 (set! %load-path old)))))
1828 (with-test-prefix "%file-port-name-canonicalization"
1830 (pass-if-equal "absolute file name & empty %load-path entry" "/dev/null"
1831 ;; In Guile 2.0.5 and earlier, this would return "dev/null" instead
1832 ;; of "/dev/null". See
1833 ;; <http://lists.gnu.org/archive/html/guile-devel/2012-05/msg00059.html>
1834 ;; for a discussion.
1835 (with-load-path (cons "" (delete "/" %load-path))
1836 (with-fluids ((%file-port-name-canonicalization 'relative))
1837 (port-filename (open-input-file "/dev/null")))))
1839 (pass-if-equal "relative canonicalization with /" "dev/null"
1840 (with-load-path (cons "/" %load-path)
1841 (with-fluids ((%file-port-name-canonicalization 'relative))
1842 (port-filename (open-input-file "/dev/null")))))
1844 (pass-if-equal "relative canonicalization from ice-9" "ice-9/q.scm"
1845 ;; If an entry in %LOAD-PATH is not canonical, then
1846 ;; `scm_i_relativize_path' is unable to do its job.
1847 (if (equal? (map canonicalize-path %load-path) %load-path)
1848 (with-fluids ((%file-port-name-canonicalization 'relative))
1850 (open-input-file (%search-load-path "ice-9/q.scm"))))
1851 (throw 'unresolved)))
1853 (pass-if-equal "absolute canonicalization from ice-9"
1855 (string-append (assoc-ref %guile-build-info 'top_srcdir)
1856 "/module/ice-9/q.scm"))
1857 (with-fluids ((%file-port-name-canonicalization 'absolute))
1858 (port-filename (open-input-file (%search-load-path "ice-9/q.scm"))))))
1860 (with-test-prefix "file name separators"
1862 (pass-if "no backslash separators in Windows file names"
1863 ;; In Guile 2.0.11 and earlier, %load-path on Windows could
1864 ;; include file names with backslashes, and `getcwd' on Windows
1865 ;; would always return a directory name with backslashes.
1866 (or (not (file-name-separator? #\\))
1867 (with-load-path (cons (getcwd) %load-path)
1868 (not (string-index (%search-load-path (basename (test-file)))
1871 (delete-file (test-file))
1873 ;;; Local Variables:
1874 ;;; eval: (put 'test-decoding-error 'scheme-indent-function 3)
1875 ;;; eval: (put 'with-load-path 'scheme-indent-function 1)