0.8alpha.0.42:
[sbcl.git] / contrib / sb-simple-streams / simple-stream-tests.lisp
1 ;;;; -*- lisp -*-
2
3 (defpackage sb-simple-streams-test
4   (:use #:common-lisp #:sb-simple-streams #:sb-rt))
5
6
7 (in-package #:sb-simple-streams-test)
8
9 (defparameter *dumb-string*
10   "This file created by simple-stream-tests.lisp. Nothing to see here, move along.")
11
12 (defparameter *test-path*
13   (merge-pathnames (make-pathname :name nil :type nil :version nil)
14                    *load-truename*)
15   "Directory for temporary test files.")
16
17 (eval-when (:load-toplevel) (ensure-directories-exist *test-path*))
18
19
20
21 (deftest create-file-1
22   ;; Create a file-simple-stream, write data.
23   (let* ((file (merge-pathnames #p"test-data.txt" *test-path*)))
24     (prog1
25         (with-open-stream (s (make-instance 'file-simple-stream
26                                             :filename file
27                                             :direction :output
28                                             :if-exists :overwrite))
29           (string= (write-string *dumb-string* s) *dumb-string*))
30       (delete-file file)))
31   t)
32
33 (deftest create-file-2
34   ;; Create a file-simple-stream via :class argument to open, write data.
35   (let ((file (merge-pathnames #p"test-data.txt" *test-path*)))
36     (prog1
37         (with-open-file (s file
38                            :class 'file-simple-stream
39                            :direction :output :if-exists :overwrite)
40            (string= (write-string *dumb-string* s) *dumb-string*))
41       (delete-file file)))
42   t)
43
44 (deftest create-read-file-1
45   ;; Via file-simple-stream objects, write and then re-read data.
46   (let ((result t)
47         (file (merge-pathnames #p"test-data.txt" *test-path*)))
48     (with-open-stream (s (make-instance 'file-simple-stream
49                                         :filename file
50                                         :direction :output
51                                         :if-exists :overwrite))
52       (write-line *dumb-string* s)
53       (setf result (and result (string= (write-string *dumb-string* s)
54                                         *dumb-string*))))
55     (with-open-stream (s (make-instance 'file-simple-stream
56                                         :filename file
57                                         :direction :input
58                                         :if-does-not-exist :error))
59       ;; Check first line
60       (multiple-value-bind (string missing-newline-p)
61           (read-line s)
62         (setf result (and result (string= string *dumb-string*)
63                           (not missing-newline-p))))
64       ;; Check second line
65       (multiple-value-bind (string missing-newline-p)
66           (read-line s)
67         (setf result (and result (string= string *dumb-string*)
68                           missing-newline-p))))
69     (delete-file file)
70     result)
71   t)
72
73 (deftest create-read-mapped-file-1
74   ;; Read data via a mapped-file-simple-stream object.
75   (let ((result t)
76         (file (merge-pathnames #p"test-data.txt" *test-path*)))
77     (with-open-file (s file
78                        :class 'file-simple-stream
79                        :direction :output :if-exists :overwrite)
80        (setf result (and result (string= (write-string *dumb-string* s)
81                                          *dumb-string*))))
82     (with-open-file (s file
83                        :class 'mapped-file-simple-stream
84                        :direction :input)
85        (setf result (and result (string= (read-line s) *dumb-string*))))
86     (delete-file file)
87     result)
88   t)
89
90 (deftest write-read-inet
91   (handler-case
92       (with-open-stream (s (make-instance 'socket-simple-stream
93                                           :remote-host #(127 0 0 1)
94                                           :remote-port 7))
95         (string= (prog1 (write-line "Got it!" s) (finish-output s))
96                  (read-line s)))
97     (sb-bsd-sockets::connection-refused-error () t))
98   t)
99
100 (deftest write-read-large-sc-1
101   ;; Do write and read with more data than the buffer will hold
102   ;; (single-channel simple-stream)
103   (let* ((file (merge-pathnames #p"test-data.txt" *test-path*))
104          (stream (make-instance 'file-simple-stream
105                                 :filename file
106                                 :direction :output))
107          (content (make-string (1+ (device-buffer-length stream))
108                                :initial-element #\x)))
109     (with-open-stream (s stream)
110       (write-string content s))
111     (with-open-stream (s (make-instance 'file-simple-stream
112                                         :filename file
113                                         :direction :input))
114       (prog1 (string= content (read-line s))
115         (delete-file file))))
116   t)
117
118 (deftest write-read-large-dc-1
119   ;; Do write and read with more data than the buffer will hold
120   ;; (dual-channel simple-stream; we only have socket streams atm)
121   (handler-case
122    (let* ((stream (make-instance 'socket-simple-stream
123                                  :remote-host #(127 0 0 1)
124                                  :remote-port 7))
125           (content (make-string (1+ (device-buffer-length stream))
126                                 :initial-element #\x)))
127      (with-open-stream (s stream)
128        (string= (prog1 (write-line content s) (finish-output s))
129                 (read-line s))))
130    (sb-bsd-sockets::connection-refused-error () t))
131   t)
132