open-file should handle binary mode and coding declarations
[bpt/guile.git] / test-suite / tests / ports.test
index 9690122..bb5c173 100644 (file)
@@ -1,28 +1,28 @@
-;;;; ports.test --- test suite for Guile I/O ports     -*- scheme -*-
+;;;; ports.test --- Guile I/O ports.    -*- coding: utf-8; mode: scheme; -*-
 ;;;; Jim Blandy <jimb@red-bean.com> --- May 1999
 ;;;;
-;;;;   Copyright (C) 1999, 2001, 2004, 2006 Free Software Foundation, Inc.
+;;;;   Copyright (C) 1999, 2001, 2004, 2006, 2007, 2009, 2010 Free Software Foundation, Inc.
 ;;;; 
-;;;; This program is free software; you can redistribute it and/or modify
-;;;; it under the terms of the GNU General Public License as published by
-;;;; the Free Software Foundation; either version 2, or (at your option)
-;;;; any later version.
+;;;; This library is free software; you can redistribute it and/or
+;;;; modify it under the terms of the GNU Lesser General Public
+;;;; License as published by the Free Software Foundation; either
+;;;; version 3 of the License, or (at your option) any later version.
 ;;;; 
-;;;; This program is distributed in the hope that it will be useful,
+;;;; This library is distributed in the hope that it will be useful,
 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
-;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-;;;; GNU General Public License for more details.
+;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+;;;; Lesser General Public License for more details.
 ;;;; 
-;;;; You should have received a copy of the GNU General Public License
-;;;; along with this software; see the file COPYING.  If not, write to
-;;;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-;;;; Boston, MA 02110-1301 USA
+;;;; You should have received a copy of the GNU Lesser General Public
+;;;; License along with this library; if not, write to the Free Software
+;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
 (define-module (test-suite test-ports)
   :use-module (test-suite lib)
   :use-module (test-suite guile-test)
   :use-module (ice-9 popen)
-  :use-module (ice-9 rdelim))
+  :use-module (ice-9 rdelim)
+  :use-module (rnrs bytevectors))
 
 (define (display-line . args)
   (for-each display args)
 \f
 ;;;; Some general utilities for testing ports.
 
+;; Make sure we are set up for 8-bit Latin-1 data.
+(fluid-set! %default-port-encoding "ISO-8859-1")
+(for-each (lambda (p)
+            (set-port-encoding! p (fluid-ref %default-port-encoding)))
+          (list (current-input-port) (current-output-port)
+                (current-error-port)))
+
 ;;; Read from PORT until EOF, and return the result as a string.
 (define (read-all port)
   (let loop ((chars '()))
             (string=? line test-string)))
   (delete-file filename))
 
+;;; read-line should use the port encoding (not the locale encoding).
+(let ((str "ĉu bone?"))
+  (with-locale "C"
+               (let* ((filename (test-file))
+                      (port (open-file filename "wl")))
+                 (set-port-encoding! port "UTF-8")
+                 (write-line str port)
+                 (let ((in-port (open-input-file filename)))
+                   (set-port-encoding! in-port "UTF-8")
+                   (let ((line (read-line in-port)))
+                     (close-port in-port)
+                     (close-port port)
+                     (pass-if "file: read-line honors port encoding"
+                              (string=? line str))))
+                 (delete-file filename))))
+
+;;; binary mode ignores port encoding
+(pass-if "file: binary mode ignores port encoding"
+  (with-fluids ((%default-port-encoding "UTF-8"))
+               (let* ((filename (test-file))
+                      (port (open-file filename "w"))
+                      (test-string "一二三")
+                      (binary-test-string
+                       (apply string
+                              (map integer->char
+                                   (uniform-vector->list
+                                    (string->utf8 test-string))))))
+                 (write-line test-string port)
+                 (close-port port)
+                 (let* ((in-port (open-file filename "rb"))
+                        (line (read-line in-port)))
+                   (close-port in-port)
+                   (delete-file filename)
+                   (string=? line binary-test-string)))))
+
+;;; binary mode ignores file coding declaration
+(pass-if "file: binary mode ignores file coding declaration"
+  (with-fluids ((%default-port-encoding "UTF-8"))
+               (let* ((filename (test-file))
+                      (port (open-file filename "w"))
+                      (test-string "一二三")
+                      (binary-test-string
+                       (apply string
+                              (map integer->char
+                                   (uniform-vector->list
+                                    (string->utf8 test-string))))))
+                 (write-line ";; coding: utf-8" port)
+                 (write-line test-string port)
+                 (close-port port)
+                 (let* ((in-port (open-file filename "rb"))
+                        (line1 (read-line in-port))
+                        (line2 (read-line in-port)))
+                   (close-port in-port)
+                   (delete-file filename)
+                   (string=? line2 binary-test-string)))))
+
+;; open-file honors file coding declarations
+(pass-if "file: open-file honors coding declarations"
+  (with-fluids ((%default-port-encoding "UTF-8"))
+               (let* ((filename (test-file))
+                      (port (open-output-file filename))
+                      (test-string "€100"))
+                 (set-port-encoding! port "ISO-8859-15")
+                 (write-line ";; coding: iso-8859-15" port)
+                 (write-line test-string port)
+                 (close-port port)
+                 (let* ((in-port (open-input-file filename))
+                        (line1 (read-line in-port))
+                        (line2 (read-line in-port)))
+                   (close-port in-port)
+                   (delete-file filename)
+                   (string=? line2 test-string)))))
+
 ;;; ungetting characters and strings.
 (with-input-from-string "walk on the moon\nmoon"
                        (lambda ()
     (string-set! text 0 #\a)
     (string-set! text (- len 1) #\b)
     (pass-if "output check"
-            (string=? text result))))
+            (string=? text result)))
+
+  (pass-if "%default-port-encoding is honored"
+    (let ((encodings '("UTF-8" "UTF-16" "ISO-8859-1" "ISO-8859-3")))
+      (equal? (map (lambda (e)
+                     (with-fluids ((%default-port-encoding e))
+                       (call-with-output-string
+                         (lambda (p)
+                           (display (port-encoding p) p)))))
+                   encodings)
+              encodings)))
+
+  (pass-if "suitable encoding [latin-1]"
+    (let ((str "hello, world"))
+      (with-fluids ((%default-port-encoding "ISO-8859-1"))
+        (equal? str
+                (with-output-to-string
+                  (lambda ()
+                    (display str)))))))
+
+  (pass-if "suitable encoding [latin-3]"
+    (let ((str "ĉu bone?"))
+      (with-fluids ((%default-port-encoding "ISO-8859-3"))
+        (equal? str
+                (with-output-to-string
+                  (lambda ()
+                    (display str)))))))
+
+  (pass-if "wrong encoding"
+    (let ((str "ĉu bone?"))
+      (catch 'encoding-error
+        (lambda ()
+          ;; Latin-1 cannot represent ‘ĉ’.
+          (with-fluids ((%default-port-encoding "ISO-8859-1"))
+            (with-output-to-string
+              (lambda ()
+                (display str)))))
+        (lambda (key subr message errno from to faulty-str)
+          (and (eq? faulty-str str)
+               (string=? from "UTF-32")
+               (string=? to "ISO-8859-1")
+               (string? (strerror errno))))))))
 
 (with-test-prefix "call-with-output-string"
 
        (while (not (eof-object? (read-char port))))
        (= 8 (port-column port))))))
 
+(with-test-prefix "port-line"
+
+  ;; in guile 1.8.1 and earlier port-line was truncated to an int, whereas
+  ;; scm_t_port actually holds a long; this restricted the range on 64-bit
+  ;; systems
+  (pass-if "set most-positive-fixnum/2"
+    (let ((n    (quotient most-positive-fixnum 2))
+         (port (open-output-string)))
+      (set-port-line! port n)
+      (eqv? n (port-line port)))))
+
+;;;
+;;; port-for-each
+;;;
+
+(with-test-prefix "port-for-each"
+
+  ;; In guile 1.8.0 through 1.8.2, port-for-each could pass a freed cell to
+  ;; its iterator func if a port was inaccessible in the last gc mark but
+  ;; the lazy sweeping has not yet reached it to remove it from the port
+  ;; table (scm_i_port_table).  Provoking those gc conditions is a little
+  ;; tricky, but the following code made it happen in 1.8.2.
+  (pass-if "passing freed cell"
+    (let ((lst '()))
+      ;; clear out the heap
+      (gc) (gc) (gc)
+      ;; allocate cells so the opened ports aren't at the start of the heap
+      (make-list 1000)
+      (open-input-file "/dev/null")
+      (make-list 1000)
+      (open-input-file "/dev/null")
+      ;; this gc leaves the above ports unmarked, ie. inaccessible
+      (gc)
+      ;; but they're still in the port table, so this sees them
+      (port-for-each (lambda (port)
+                      (set! lst (cons port lst))))
+      ;; this forces completion of the sweeping
+      (gc) (gc) (gc)
+      ;; and (if the bug is present) the cells accumulated in LST are now
+      ;; freed cells, which give #f from `port?'
+      (not (memq #f (map port? lst))))))
+
+(with-test-prefix
+ "fdes->port"
+ (pass-if "fdes->ports finds port"
+         (let ((port (open-file (test-file) "w")))
+
+           (not (not (memq port (fdes->ports (port->fdes port))))))))
+
 ;;;
 ;;; seek
 ;;;