;;;; Jim Blandy <jimb@red-bean.com> --- May 1999
;;;;
;;;; Copyright (C) 1999, 2001, 2004, 2006, 2007, 2009, 2010,
-;;;; 2011 Free Software Foundation, Inc.
+;;;; 2011, 2012 Free Software Foundation, Inc.
;;;;
;;;; This library is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU Lesser General Public
(with-fluids ((%default-port-encoding e))
(call-with-output-string
(lambda (p)
- (display (port-encoding p) p)))))
+ (and (string=? e (port-encoding p))
+ (display (port-encoding p) p))))))
encodings)
encodings)))
(= (port-line p) 0)
(= (port-column p) 0))))
- (pass-if "read-char, wrong encoding, error"
- (let ((p (with-fluids ((%default-port-encoding "UTF-8"))
- (open-bytevector-input-port #vu8(255 65 66 67)))))
- (catch 'decoding-error
- (lambda ()
- (set-port-conversion-strategy! p 'error)
- (read-char p)
- #f)
- (lambda (key subr message err port)
- (and (eq? port p)
-
- ;; PORT should point past the error.
- (equal? '(#\A #\B #\C)
- (list (read-char port)
- (read-char port)
- (read-char port)))
-
- (eof-object? (read-char port)))))))
-
- (pass-if "read-char, wrong encoding, escape"
- ;; `escape' should behave exactly like `error'.
- (let ((p (with-fluids ((%default-port-encoding "UTF-8"))
- (open-bytevector-input-port #vu8(255 65 66 67)))))
- (catch 'decoding-error
- (lambda ()
- (set-port-conversion-strategy! p 'escape)
- (read-char p)
- #f)
- (lambda (key subr message err port)
- (and (eq? port p)
-
- ;; PORT should point past the error.
- (equal? '(#\A #\B #\C)
- (list (read-char port)
- (read-char port)
- (read-char port)))
-
- (eof-object? (read-char port)))))))
+ (pass-if "peek-char [utf-16]"
+ (let ((p (with-fluids ((%default-port-encoding "UTF-16BE"))
+ (open-input-string "안녕하세요"))))
+ (and (char=? (peek-char p) #\안)
+ (char=? (peek-char p) #\안)
+ (char=? (peek-char p) #\안)
+ (= (port-line p) 0)
+ (= (port-column p) 0))))
- (pass-if "read-char, wrong encoding, substitute"
- (let ((p (with-fluids ((%default-port-encoding "UTF-8"))
- (open-bytevector-input-port #vu8(255 206 187 206 188)))))
- (set-port-conversion-strategy! p 'substitute)
- (equal? (list (read-char p) (read-char p) (read-char p))
- '(#\? #\λ #\μ))))
-
- (pass-if "peek-char, wrong encoding, error"
- (let-syntax ((decoding-error?
- (syntax-rules ()
- ((_ port exp)
- (catch 'decoding-error
- (lambda ()
- (pk 'exp exp)
- #f)
- (lambda (key subr message errno p)
- (eq? p port)))))))
- (let ((p (with-fluids ((%default-port-encoding "UTF-8"))
- (open-bytevector-input-port #vu8(255 65 66 67)))))
- (set-port-conversion-strategy! p 'error)
-
- ;; `peek-char' should repeatedly raise an error.
- (and (decoding-error? p (peek-char p))
- (decoding-error? p (peek-char p))
- (decoding-error? p (peek-char p))
-
- ;; Move past the error.
- (decoding-error? p (read-char p))
-
- ;; Finish happily.
- (equal? '(#\A #\B #\C)
- (list (read-char p)
- (read-char p)
- (read-char p)))
- (eof-object? (read-char p)))))))
+ ;; Mini DSL to test decoding error handling.
+ (letrec-syntax ((decoding-error?
+ (syntax-rules ()
+ ((_ port exp)
+ (catch 'decoding-error
+ (lambda ()
+ (pk 'exp exp)
+ #f)
+ (lambda (key subr message errno p)
+ (and (eq? p port)
+ (not (= 0 errno))))))))
+ (make-check
+ (syntax-rules (-> error eof)
+ ((_ port (proc -> error))
+ (if (eq? 'substitute
+ (port-conversion-strategy port))
+ (eq? (proc port) #\?)
+ (decoding-error? port (proc port))))
+ ((_ port (proc -> eof))
+ (eof-object? (proc port)))
+ ((_ port (proc -> char))
+ (eq? (proc port) char))))
+ (make-checks
+ (syntax-rules ()
+ ((_ port check ...)
+ (and (make-check port check) ...))))
+ (make-peek+read-checks
+ (syntax-rules ()
+ ((_ port (result ...) e1 expected ...)
+ (make-peek+read-checks port
+ (result ...
+ (peek-char -> e1)
+ (read-char -> e1))
+ expected ...))
+ ((_ port (result ...))
+ (make-checks port result ...))
+ ((_ port #f e1 expected ...)
+ (make-peek+read-checks port
+ ((peek-char -> e1)
+ (read-char -> e1))
+ expected ...))))
+
+ (test-decoding-error*
+ (syntax-rules ()
+ ((_ sequence encoding strategy (expected ...))
+ (begin
+ (pass-if (format #f "test-decoding-error: ~s ~s ~s"
+ 'sequence encoding strategy)
+ (let ((p (open-bytevector-input-port
+ (u8-list->bytevector 'sequence))))
+ (set-port-encoding! p encoding)
+ (set-port-conversion-strategy! p strategy)
+ (make-checks p
+ (read-char -> expected) ...)))
+
+ ;; Generate the same test, but with one
+ ;; `peek-char' call before each `read-char'.
+ ;; Both should yield the same result.
+ (pass-if (format #f "test-decoding-error: ~s ~s ~s + peek-char"
+ 'sequence encoding strategy)
+ (let ((p (open-bytevector-input-port
+ (u8-list->bytevector 'sequence))))
+ (set-port-encoding! p encoding)
+ (set-port-conversion-strategy! p strategy)
+ (make-peek+read-checks p #f expected
+ ...)))))))
+ (test-decoding-error
+ (syntax-rules ()
+ ((_ sequence encoding (expected ...))
+ (begin
+ (test-decoding-error* sequence encoding 'error
+ (expected ...))
+
+ ;; `escape' should behave exactly like `error'.
+ (test-decoding-error* sequence encoding 'escape
+ (expected ...))
+
+ (test-decoding-error* sequence encoding 'substitute
+ (expected ...)))))))
+
+ (test-decoding-error (255 65 66 67) "UTF-8"
+ (error #\A #\B #\C eof))
+
+ (test-decoding-error (255 206 187 206 188) "UTF-8"
+ (error #\λ #\μ eof))
+
+ (test-decoding-error (206 187 206) "UTF-8"
+ ;; Unterminated sequence.
+ (#\λ error eof))
+
+ ;; Check how ill-formed UTF-8 sequences are handled (see Table 3-7
+ ;; of the "Conformance" chapter of Unicode 6.0.0.)
+
+ (test-decoding-error (#xc0 #x80 #x41) "UTF-8"
+ (error ;; C0: should be in the C2..DF range
+ error ;; 80: invalid
+ #\A
+ eof))
+
+ (test-decoding-error (#xc2 #x41 #x42) "UTF-8"
+ ;; Section 3.9 of Unicode 6.0.0 reads:
+ ;; "If the converter encounters an ill-formed UTF-8 code unit
+ ;; sequence which starts with a valid first byte, but which does
+ ;; not continue with valid successor bytes (see Table 3-7), it
+ ;; must not consume the successor bytes".
+ ;; Glibc/libiconv do not conform to it and instead swallow the
+ ;; #x41. This example appears literally in Section 3.9.
+ (error ;; 41: invalid successor
+ #\A ;; 41: valid starting byte
+ #\B
+ eof))
+
+ (test-decoding-error (#xf0 #x80 #x80 #x41) "UTF-8"
+ ;; According to Unicode 6.0.0, Section 3.9, "the only formal
+ ;; requirement mandated by Unicode conformance for a converter is
+ ;; that the <41> be processed and correctly interpreted as
+ ;; <U+0041>".
+ (error ;; 2nd byte should be in the A0..BF range
+ error ;; 80: not a valid starting byte
+ error ;; 80: not a valid starting byte
+ #\A
+ eof))
+
+ (test-decoding-error (#xe0 #xa0 #x41 #x42) "UTF-8"
+ (error ;; 3rd byte should be in the 80..BF range
+ #\A
+ #\B
+ eof))
+
+ (test-decoding-error (#xf0 #x88 #x88 #x88) "UTF-8"
+ (error ;; 2nd byte should be in the 90..BF range
+ error ;; 88: not a valid starting byte
+ error ;; 88: not a valid starting byte
+ error ;; 88: not a valid starting byte
+ eof))))
(with-test-prefix "call-with-output-string"
(list read read-char read-line)
'("read" "read-char" "read-line")))
+\f
+
+(with-test-prefix "setvbuf"
+
+ (pass-if "line/column number preserved"
+ ;; In Guile 2.0.5, `setvbuf' would erroneously decrease the port's
+ ;; line and/or column number.
+ (call-with-output-file (test-file)
+ (lambda (p)
+ (display "This is GNU Guile.\nWelcome." p)))
+ (call-with-input-file (test-file)
+ (lambda (p)
+ (and (eq? #\T (read-char p))
+ (let ((line (port-line p))
+ (col (port-column p)))
+ (and (= line 0) (= col 1)
+ (begin
+ (setvbuf p _IOFBF 777)
+ (let ((line* (port-line p))
+ (col* (port-column p)))
+ (and (= line line*)
+ (= col col*)))))))))))
+
+\f
+
+(define-syntax-rule (with-load-path path body ...)
+ (let ((new path)
+ (old %load-path))
+ (dynamic-wind
+ (lambda ()
+ (set! %load-path new))
+ (lambda ()
+ body ...)
+ (lambda ()
+ (set! %load-path old)))))
+
+(with-test-prefix "%file-port-name-canonicalization"
+
+ (pass-if "absolute file name & empty %load-path entry"
+ ;; In Guile 2.0.5 and earlier, this would return "dev/null" instead
+ ;; of "/dev/null". See
+ ;; <http://lists.gnu.org/archive/html/guile-devel/2012-05/msg00059.html>
+ ;; for a discussion.
+ (equal? "/dev/null"
+ (with-load-path (cons "" (delete "/" %load-path))
+ (with-fluids ((%file-port-name-canonicalization 'relative))
+ (port-filename (open-input-file "/dev/null")))))))
+
(delete-file (test-file))
+
+;;; Local Variables:
+;;; eval: (put 'test-decoding-error 'scheme-indent-function 3)
+;;; eval: (put 'with-load-path 'scheme-indent-function 1)
+;;; End: