;;;; ports.test --- test suite for Guile I/O ports -*- scheme -*-
;;;; Jim Blandy <jimb@red-bean.com> --- May 1999
;;;;
-;;;; Copyright (C) 1999 Free Software Foundation, Inc.
+;;;; Copyright (C) 1999, 2001, 2004, 2006 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
;;;;
;;;; 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., 59 Temple Place, Suite 330,
-;;;; Boston, MA 02111-1307 USA
+;;;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+;;;; Boston, MA 02110-1301 USA
-(use-modules (test-suite lib)
- (ice-9 popen)
- (ice-9 rdelim))
+(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))
(define (display-line . args)
(for-each display args)
(seek port -2 SEEK_END)
(pass-if "file: r/w 4"
(char=? (read-char port) #\s))
+ (close-port port)
(delete-file filename))
;;; Unbuffered input/output port with seeking.
(seek port -2 SEEK_END)
(pass-if "file: ub r/w 4"
(char=? (read-char port) #\s))
+ (close-port port)
(delete-file filename))
;;; Buffered output-only and input-only ports with seeking.
(pass-if "file: read back NUL 2"
(char=? (string-ref line 3) #\nul))
(pass-if "file: EOF"
- (eof-object? (read-char port))))
+ (eof-object? (read-char port)))
+ (close-port port))
(delete-file filename))
;;; line buffering mode.
(string-ref text 0))))))
;; seeking an output string.
- (let* ((text "123456789")
+ (let* ((text (string-copy "123456789"))
(len (string-length text))
(result (call-with-output-string
(lambda (p)
(pass-if "output check"
(string=? text result))))
+(with-test-prefix "call-with-output-string"
+
+ ;; In Guile 1.6.4, closing the port resulted in a segv, check that doesn't
+ ;; occur.
+ (pass-if-exception "proc closes port" exception:wrong-type-arg
+ (call-with-output-string close-port)))
+
\f
;;;; Soft ports. No tests implemented yet.
"He who receives an idea from me, receives instruction"
15)))
-\f
+;; Test port-line and port-column for output ports
+
+(define (test-output-line-counter text final-column)
+ (with-test-prefix "port-line and port-column for output ports"
+ (let ((port (open-output-string)))
+ (pass-if "at beginning of input"
+ (and (= (port-line port) 0)
+ (= (port-column port) 0)))
+ (write-char #\x port)
+ (pass-if "after writing one character"
+ (and (= (port-line port) 0)
+ (= (port-column port) 1)))
+ (write-char #\newline port)
+ (pass-if "after writing first newline char"
+ (and (= (port-line port) 1)
+ (= (port-column port) 0)))
+ (display text port)
+ (pass-if "line count is 5 at end"
+ (= (port-line port) 5))
+ (pass-if "column is correct at end"
+ (= (port-column port) final-column)))))
+
+(test-output-line-counter
+ (string-append "He who receives an idea from me, receives instruction\n"
+ "himself without lessening mine; as he who lights his\n"
+ "taper at mine, receives light without darkening me.\n"
+ " --- Thomas Jefferson\n"
+ "no newline here")
+ 15)
+
+(with-test-prefix "port-column"
+
+ (with-test-prefix "output"
+
+ (pass-if "x"
+ (let ((port (open-output-string)))
+ (display "x" port)
+ (= 1 (port-column port))))
+
+ (pass-if "\\a"
+ (let ((port (open-output-string)))
+ (display "\a" port)
+ (= 0 (port-column port))))
+
+ (pass-if "x\\a"
+ (let ((port (open-output-string)))
+ (display "x\a" port)
+ (= 1 (port-column port))))
+
+ (pass-if "\\x08 backspace"
+ (let ((port (open-output-string)))
+ (display "\x08" port)
+ (= 0 (port-column port))))
+
+ (pass-if "x\\x08 backspace"
+ (let ((port (open-output-string)))
+ (display "x\x08" port)
+ (= 0 (port-column port))))
+
+ (pass-if "\\n"
+ (let ((port (open-output-string)))
+ (display "\n" port)
+ (= 0 (port-column port))))
+
+ (pass-if "x\\n"
+ (let ((port (open-output-string)))
+ (display "x\n" port)
+ (= 0 (port-column port))))
+
+ (pass-if "\\r"
+ (let ((port (open-output-string)))
+ (display "\r" port)
+ (= 0 (port-column port))))
+
+ (pass-if "x\\r"
+ (let ((port (open-output-string)))
+ (display "x\r" port)
+ (= 0 (port-column port))))
+
+ (pass-if "\\t"
+ (let ((port (open-output-string)))
+ (display "\t" port)
+ (= 8 (port-column port))))
+
+ (pass-if "x\\t"
+ (let ((port (open-output-string)))
+ (display "x\t" port)
+ (= 8 (port-column port)))))
+
+ (with-test-prefix "input"
+
+ (pass-if "x"
+ (let ((port (open-input-string "x")))
+ (while (not (eof-object? (read-char port))))
+ (= 1 (port-column port))))
+
+ (pass-if "\\a"
+ (let ((port (open-input-string "\a")))
+ (while (not (eof-object? (read-char port))))
+ (= 0 (port-column port))))
+
+ (pass-if "x\\a"
+ (let ((port (open-input-string "x\a")))
+ (while (not (eof-object? (read-char port))))
+ (= 1 (port-column port))))
+
+ (pass-if "\\x08 backspace"
+ (let ((port (open-input-string "\x08")))
+ (while (not (eof-object? (read-char port))))
+ (= 0 (port-column port))))
+
+ (pass-if "x\\x08 backspace"
+ (let ((port (open-input-string "x\x08")))
+ (while (not (eof-object? (read-char port))))
+ (= 0 (port-column port))))
+
+ (pass-if "\\n"
+ (let ((port (open-input-string "\n")))
+ (while (not (eof-object? (read-char port))))
+ (= 0 (port-column port))))
+
+ (pass-if "x\\n"
+ (let ((port (open-input-string "x\n")))
+ (while (not (eof-object? (read-char port))))
+ (= 0 (port-column port))))
+
+ (pass-if "\\r"
+ (let ((port (open-input-string "\r")))
+ (while (not (eof-object? (read-char port))))
+ (= 0 (port-column port))))
+
+ (pass-if "x\\r"
+ (let ((port (open-input-string "x\r")))
+ (while (not (eof-object? (read-char port))))
+ (= 0 (port-column port))))
+
+ (pass-if "\\t"
+ (let ((port (open-input-string "\t")))
+ (while (not (eof-object? (read-char port))))
+ (= 8 (port-column port))))
+
+ (pass-if "x\\t"
+ (let ((port (open-input-string "x\t")))
+ (while (not (eof-object? (read-char port))))
+ (= 8 (port-column port))))))
+
+;;;
+;;; truncate-file
+;;;
+
+(with-test-prefix "truncate-file"
+
+ (with-test-prefix "filename"
+
+ (pass-if "shorten"
+ (call-with-output-file (test-file)
+ (lambda (port)
+ (display "hello" port)))
+ (truncate-file (test-file) 1)
+ (eqv? 1 (stat:size (stat (test-file))))))
+
+ (with-test-prefix "file descriptor"
+
+ (pass-if "shorten"
+ (call-with-output-file (test-file)
+ (lambda (port)
+ (display "hello" port)))
+ (let ((fd (open-fdes (test-file) O_RDWR)))
+ (truncate-file fd 1)
+ (close-fdes fd))
+ (eqv? 1 (stat:size (stat (test-file))))))
+
+ (with-test-prefix "file port"
+
+ (pass-if "shorten"
+ (call-with-output-file (test-file)
+ (lambda (port)
+ (display "hello" port)))
+ (let ((port (open-file (test-file) "r+")))
+ (truncate-file port 1))
+ (eqv? 1 (stat:size (stat (test-file)))))))
+
+
;;;; testing read-delimited and friends
(with-test-prefix "read-delimited!"
(procedure)))))
(list read read-char read-line)
'("read" "read-char" "read-line")))
+
+(delete-file (test-file))