;;;; 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
(make-check
(syntax-rules (-> error eof)
((_ port (proc -> error))
- (decoding-error? port (proc port)))
+ (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))
(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 (tests)
- ((_ sequence encoding strategy (tests checks ...))
- (pass-if (format #f "test-decoding-error: ~s ~s ~s ~s"
- (caar '(checks ...))
- '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 checks ...)))))))
-
- (test-decoding-error (255 65 66 67) "UTF-8" 'error
- (tests
- (read-char -> error)
- (read-char -> #\A)
- (read-char -> #\B)
- (read-char -> #\C)
- (read-char -> eof)))
-
- (test-decoding-error (255 65 66 67) "UTF-8" 'escape
- ;; `escape' should behave exactly like `error'.
- (tests
- (read-char -> error)
- (read-char -> #\A)
- (read-char -> #\B)
- (read-char -> #\C)
- (read-char -> eof)))
-
- (test-decoding-error (255 206 187 206 188) "UTF-8" 'substitute
- (tests
- (read-char -> #\?)
- (read-char -> #\λ)
- (read-char -> #\μ)
- (read-char -> eof)))
-
- (test-decoding-error (255 65 66 67) "UTF-8" 'error
- (tests
- ;; `peek-char' should repeatedly raise an error.
- (peek-char -> error)
- (peek-char -> error)
- (peek-char -> error)
-
- ;; Move past the error.
- (read-char -> error)
-
- (read-char -> #\A)
- (read-char -> #\B)
- (read-char -> #\C)
- (read-char -> eof)))))
+ (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: