X-Git-Url: http://repo.macrolet.net/gitweb/?a=blobdiff_plain;f=contrib%2Fsb-simple-streams%2Fsimple-stream-tests.lisp;h=6515a947966720fc87701eb4d830d8bb54adbea7;hb=4ed3f0d08c3a57a6762018d9622f253ab9d0f2b6;hp=010d0abf036830b1a6d03ec1d8a856f60d429503;hpb=ce58e434470b1ebefae6132d9c075c7d8a2c0c13;p=sbcl.git diff --git a/contrib/sb-simple-streams/simple-stream-tests.lisp b/contrib/sb-simple-streams/simple-stream-tests.lisp index 010d0ab..6515a94 100644 --- a/contrib/sb-simple-streams/simple-stream-tests.lisp +++ b/contrib/sb-simple-streams/simple-stream-tests.lisp @@ -10,19 +10,31 @@ "This file was created by simple-stream-tests.lisp. Nothing to see here, move along.") (defparameter *test-path* - (merge-pathnames (make-pathname :name nil :type nil :version nil) + (merge-pathnames (make-pathname :name :unspecific :type :unspecific + :version :unspecific) *load-truename*) "Directory for temporary test files.") -(eval-when (:load-toplevel) (ensure-directories-exist *test-path*)) +(defparameter *test-file* + (merge-pathnames #p"test-data.txt" *test-path*)) + +(eval-when (:load-toplevel) (ensure-directories-exist *test-path* :verbose t)) + +;;; Non-destructive functional analog of REMF +(defun remove-key (key list) + (loop for (current-key val . rest) on list by #'cddr + until (eql current-key key) + collect current-key into result + collect val into result + finally (return (nconc result rest)))) (defmacro with-test-file ((stream file &rest open-arguments &key (delete-afterwards t) initial-content &allow-other-keys) &body body) - (remf open-arguments :delete-afterwards) - (remf open-arguments :initial-content) + (setq open-arguments (remove-key :delete-afterwards open-arguments)) + (setq open-arguments (remove-key :initial-content open-arguments)) (if initial-content (let ((create-file-stream (gensym))) `(progn @@ -41,38 +53,36 @@ (deftest create-file-1 - ;; Create a file-simple-stream, write data. - (let* ((file (merge-pathnames #p"test-data.txt" *test-path*))) + ;; Create a file-simple-stream, write data. (prog1 (with-open-stream (s (make-instance 'file-simple-stream - :filename file + :filename *test-file* :direction :output :if-exists :overwrite :if-does-not-exist :create)) (string= (write-string *dumb-string* s) *dumb-string*)) - (delete-file file))) + (delete-file *test-file*)) t) (deftest create-file-2 - ;; Create a file-simple-stream via :class argument to open, write data. - (let ((file (merge-pathnames #p"test-data.txt" *test-path*))) - (with-test-file (s file :class 'file-simple-stream :direction :output - :if-exists :overwrite :if-does-not-exist :create) - (string= (write-string *dumb-string* s) *dumb-string*))) + ;; Create a file-simple-stream via :class argument to open, write data. + (with-test-file (s *test-file* :class 'file-simple-stream + :direction :output :if-exists :overwrite + :if-does-not-exist :create) + (string= (write-string *dumb-string* s) *dumb-string*)) t) (deftest create-read-file-1 ;; Via file-simple-stream objects, write and then re-read data. - (let ((result t) - (file (merge-pathnames #p"test-data.txt" *test-path*))) - (with-test-file (s file :class 'file-simple-stream :direction :output - :if-exists :overwrite :if-does-not-exist :create - :delete-afterwards nil) + (let ((result t)) + (with-test-file (s *test-file* :class 'file-simple-stream + :direction :output :if-exists :overwrite + :if-does-not-exist :create :delete-afterwards nil) (write-line *dumb-string* s) (setf result (and result (string= (write-string *dumb-string* s) *dumb-string*)))) - (with-test-file (s file :class 'file-simple-stream + (with-test-file (s *test-file* :class 'file-simple-stream :direction :input :if-does-not-exist :error) ;; Check first line (multiple-value-bind (string missing-newline-p) @@ -89,9 +99,8 @@ (deftest create-read-mapped-file-1 ;; Read data via a mapped-file-simple-stream object. - (let ((result t) - (file (merge-pathnames #p"test-data.txt" *test-path*))) - (with-test-file (s file :class 'mapped-file-simple-stream + (let ((result t)) + (with-test-file (s *test-file* :class 'mapped-file-simple-stream :direction :input :if-does-not-exist :error :initial-content *dumb-string*) (setf result (and result (string= (read-line s) *dumb-string*)))) @@ -102,7 +111,8 @@ (handler-case (with-open-stream (s (make-instance 'socket-simple-stream :remote-host #(127 0 0 1) - :remote-port 7)) + :remote-port 7 + :direction :io)) (string= (prog1 (write-line "Got it!" s) (finish-output s)) (read-line s))) (sb-bsd-sockets::connection-refused-error () t)) @@ -111,27 +121,67 @@ (deftest write-read-large-sc-1 ;; Do write and read with more data than the buffer will hold ;; (single-channel simple-stream) - (let* ((file (merge-pathnames #p"test-data.txt" *test-path*)) - (stream (make-instance 'file-simple-stream - :filename file :direction :output + (let* ((stream (make-instance 'file-simple-stream + :filename *test-file* :direction :output :if-exists :overwrite :if-does-not-exist :create)) (content (make-string (1+ (device-buffer-length stream)) :initial-element #\x))) (with-open-stream (s stream) (write-string content s)) - (with-test-file (s file :class 'file-simple-stream + (with-test-file (s *test-file* :class 'file-simple-stream :direction :input :if-does-not-exist :error) (string= content (read-line s)))) t) +(deftest write-read-large-sc-2 + (let* ((stream (make-instance 'file-simple-stream + :filename *test-file* :direction :output + :if-exists :overwrite + :if-does-not-exist :create)) + (length (1+ (* 3 (device-buffer-length stream)))) + (content (make-string length))) + (dotimes (i (length content)) + (setf (aref content i) (code-char (random 256)))) + (with-open-stream (s stream) + (write-string content s)) + (with-test-file (s *test-file* :class 'file-simple-stream + :direction :input :if-does-not-exist :error) + (let ((seq (make-string length))) + #+nil (read-sequence seq s) + #-nil (dotimes (i length) + (setf (char seq i) (read-char s))) + (string= content seq)))) + t) + +(deftest write-read-large-sc-3 + (let* ((stream (make-instance 'file-simple-stream + :filename *test-file* :direction :output + :if-exists :overwrite + :if-does-not-exist :create)) + (length (1+ (* 3 (device-buffer-length stream)))) + (content (make-array length :element-type '(unsigned-byte 8)))) + (dotimes (i (length content)) + (setf (aref content i) (random 256))) + (with-open-stream (s stream) + (write-sequence content s)) + (with-test-file (s *test-file* :class 'file-simple-stream + :direction :input :if-does-not-exist :error) + (let ((seq (make-array length :element-type '(unsigned-byte 8)))) + #+nil (read-sequence seq s) + #-nil (dotimes (i length) + (setf (aref seq i) (read-byte s))) + (equalp content seq)))) + t) + (deftest write-read-large-dc-1 ;; Do write and read with more data than the buffer will hold ;; (dual-channel simple-stream; we only have socket streams atm) (handler-case (let* ((stream (make-instance 'socket-simple-stream :remote-host #(127 0 0 1) - :remote-port 7)) + :remote-port 7 + :direction :io)) (content (make-string (1+ (device-buffer-length stream)) :initial-element #\x))) (with-open-stream (s stream) @@ -142,76 +192,79 @@ (deftest file-position-1 - ;; Test reading of file-position - (let* ((file (merge-pathnames #p"test-data.txt" *test-path*))) - (with-test-file (s file :class 'file-simple-stream :direction :input + ;; Test reading of file-position + (with-test-file (s *test-file* :class 'file-simple-stream :direction :input :initial-content *dumb-string*) - (file-position s))) + (file-position s)) 0) -;;; file-position-2 fails ONLY when called with -;;; (asdf:oos 'asdf:test-op :sb-simple-streams) -;;; TODO: Find out why -#+nil (deftest file-position-2 - ;; Test reading of file-position - (let* ((file (merge-pathnames #p"test-data.txt" *test-path*))) - (with-test-file (s file :class 'file-simple-stream :direction :input + ;; Test reading of file-position + (with-test-file (s *test-file* :class 'file-simple-stream :direction :input :initial-content *dumb-string*) (read-byte s) - (file-position s))) + (file-position s)) 1) (deftest file-position-3 - ;; Test reading of file-position in the presence of unsaved data - (let* ((file (merge-pathnames #p"test-data.txt" *test-path*))) - (with-test-file (s file :class 'file-simple-stream :direction :output - :if-exists :supersede :if-does-not-exist :create) + ;; Test reading of file-position in the presence of unsaved data + (with-test-file (s *test-file* :class 'file-simple-stream + :direction :output :if-exists :supersede + :if-does-not-exist :create) (write-byte 50 s) - (file-position s))) + (file-position s)) 1) (deftest file-position-4 + ;; Test reading of file-position in the presence of unsaved data and + ;; filled buffer + (with-test-file (s *test-file* :class 'file-simple-stream :direction :io + :if-exists :overwrite :if-does-not-exist :create + :initial-content *dumb-string*) + (read-byte s) ; fill buffer + (write-byte 50 s) ; advance file-position + (file-position s)) + 2) + +(deftest file-position-5 ;; Test file position when opening with :if-exists :append - (let ((file (merge-pathnames #p"test-data.txt" *test-path*))) - (with-test-file (s file :class 'file-simple-stream :direction :io - :if-exists :append :if-does-not-exist :create - :initial-content "Foo") - (= (file-length s) (file-position s)))) + (with-test-file (s *test-file* :class 'file-simple-stream :direction :io + :if-exists :append :if-does-not-exist :create + :initial-content *dumb-string*) + (= (file-length s) (file-position s))) T) (deftest write-read-unflushed-sc-1 - ;; Write something into a single-channel stream and read it back - ;; without explicitly flushing the buffer in-between - (let* ((file (merge-pathnames #p"test-data.txt" *test-path*))) - (with-test-file (s file :class 'file-simple-stream :direction :io + ;; Write something into a single-channel stream and read it back + ;; without explicitly flushing the buffer in-between + (with-test-file (s *test-file* :class 'file-simple-stream :direction :io :if-does-not-exist :create :if-exists :supersede) (write-char #\x s) (file-position s :start) - (read-char s))) + (read-char s)) #\x) (deftest write-read-unflushed-sc-2 - ;; Write something into a single-channel stream, try to read back too much - (handler-case - (let* ((file (merge-pathnames #p"test-data.txt" *test-path*))) - (with-test-file (s file :class 'file-simple-stream :direction :io - :if-does-not-exist :create :if-exists :supersede) - (write-char #\x s) - (file-position s :start) - (read-char s) - (read-char s)) - nil) - (end-of-file () t)) + ;; Write something into a single-channel stream, try to read back too much + (handler-case + (with-test-file (s *test-file* :class 'file-simple-stream + :direction :io :if-does-not-exist :create + :if-exists :supersede) + (write-char #\x s) + (file-position s :start) + (read-char s) + (read-char s) + nil) + (end-of-file () t)) t) (deftest write-read-unflushed-sc-3 - (let ((file (merge-pathnames #p"test-data.txt" *test-path*)) - (result t)) - (with-test-file (s file :class 'file-simple-stream :direction :io + ;; Test writing in a buffer filled with previous file contents + (let ((result t)) + (with-test-file (s *test-file* :class 'file-simple-stream :direction :io :if-exists :overwrite :if-does-not-exist :create :initial-content *dumb-string*) - (setq result (and result (char= (read-char s) (char *dumb-string* 0)))) + (setq result (and result (char= (read-char s) (schar *dumb-string* 0)))) (setq result (and result (= (file-position s) 1))) (let ((pos (file-position s))) (write-char #\x s) @@ -222,8 +275,8 @@ (deftest write-read-unflushed-sc-4 ;; Test flushing of buffers - (let ((file (merge-pathnames #p"test-data.txt" *test-path*))) - (with-test-file (s file :class 'file-simple-stream :direction :io + (progn + (with-test-file (s *test-file* :class 'file-simple-stream :direction :io :if-exists :overwrite :if-does-not-exist :create :initial-content "Foo" :delete-afterwards nil) @@ -232,16 +285,16 @@ (write-char #\X s) (file-position s :end) ; Extend file. (write-char #\X s)) - (with-test-file (s file :class 'file-simple-stream :direction :input - :if-does-not-exist :error) + (with-test-file (s *test-file* :class 'file-simple-stream + :direction :input :if-does-not-exist :error) (read-line s))) "XooX" T) (deftest write-read-append-sc-1 ;; Test writing in the middle of a stream opened in append mode - (let ((file (merge-pathnames #p"test-data.txt" *test-path*))) - (with-test-file (s file :class 'file-simple-stream :direction :io + (progn + (with-test-file (s *test-file* :class 'file-simple-stream :direction :io :if-exists :append :if-does-not-exist :create :initial-content "Foo" :delete-afterwards nil) @@ -249,12 +302,9 @@ (write-char #\X s) (file-position s :end) ; Extend file. (write-char #\X s)) - (with-test-file (s file :class 'file-simple-stream :direction :input - :if-does-not-exist :error) + (with-test-file (s *test-file* :class 'file-simple-stream + :direction :input :if-does-not-exist :error) (read-line s))) "XooX" T) - - -