1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; indent-tabs-mode: nil -*-
3 ;;; --- IO.STREAMS test suite.
5 ;;; Copyright (c) 2006-2007, Dr. Edmund Weitz. All rights reserved.
6 ;;; Copyright (c) 2007, Luis Oliveira <loliveira@common-lisp.net>
8 ;;; Redistribution and use in source and binary forms, with or without
9 ;;; modification, are permitted provided that the following conditions
12 ;;; * Redistributions of source code must retain the above copyright
13 ;;; notice, this list of conditions and the following disclaimer.
15 ;;; * Redistributions in binary form must reproduce the above
16 ;;; copyright notice, this list of conditions and the following
17 ;;; disclaimer in the documentation and/or other materials
18 ;;; provided with the distribution.
20 ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
21 ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
24 ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
26 ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 (in-package :iolib-tests
)
34 (in-suite* :io.streams
:in
:iolib
)
36 (defclass my-file-stream
(dual-channel-single-fd-gray-stream)
37 ((path :initarg
:path
:reader file-stream-path
)))
39 ;;; Very ad-hoc: doesn't do :DIRECTION :PROBE, or handle errors,
40 ;;; :IF-DOES-NOT-EXIST, among many other things. This kind of thing
41 ;;; should be moved into OSICAT, btw.
43 ;;; FIXME: implement single-channel stream
44 (defun make-file-stream (path &key
47 (if-does-not-exist (ecase direction
49 ((:io
:output
) :create
)))
50 (external-format :default
))
51 (declare (ignore if-does-not-exist
))
52 ;; move OPEN to INITIALIZE-INSTANCE
53 (let ((fd (nix:open path
54 (logior (ecase direction
56 (:output
(logior nix
:o-creat nix
:o-wronly
))
57 (:io
(logior nix
:o-creat nix
:o-rdwr
)))
60 (:supersede nix
:o-trunc
)
61 (:append nix
:o-append
)
63 (logior nix
:s-irusr nix
:s-iwusr
))))
64 (make-instance 'my-file-stream
68 :external-format external-format
)))
70 (defmacro with-open-file-stream
((var path
&rest options
) &body body
)
71 (with-gensyms (stream)
72 `(let ((,stream
(make-file-stream ,path
,@options
)))
73 (with-open-stream (,var
,stream
)
77 (let ((sys-pn (truename (asdf:system-definition-pathname
78 (asdf:find-system
'iolib-tests
)))))
79 (make-pathname :directory
(append (pathname-directory sys-pn
)
83 (ensure-directories-exist
85 (make-pathname :directory
'(:relative
"test-dir"))
86 (make-pathname :directory
88 (or *load-truename
* *compile-file-truename
*))))))
90 ;;; A list of test files where each entry consists of the name
91 ;;; prefix and a list of encodings.
93 '(("kafka" (#-cmu
:utf-8
:latin-1
#|
:cp1252|
#))
94 ("tilton" (#-cmu
:utf-8
:ascii
))
95 ("hebrew" (#-cmu
:utf-8
#|
:latin-8|
#))
96 ("russian" (#-cmu
:utf-8
#|
:koi8r|
#))
97 ("unicode_demo" (#-cmu
:utf-8
#|
:utf-16
:utf-32|
#))))
99 ;;; For a name suffix FILE-NAME and a symbol SYMBOL denoting an
100 ;;; encoding returns a list of pairs where the car is a full file name
101 ;;; and the cdr is the corresponding external format. This list
102 ;;; contains all possible line-end conversions.
103 (defun create-file-variants (file-name symbol
)
104 (loop :for eol-style
:in
'(:lf
:cr
:crlf
) :collect
105 (cons (format nil
"~A_~(~A~)_~(~A~).txt"
106 file-name symbol eol-style
)
107 (babel:make-external-format symbol
:eol-style eol-style
))))
109 ;;; For a name suffix FILE-NAME and a list of symbols SYMBOLS denoting
110 ;;; different encodings of the corresponding file returns a list of
111 ;;; lists which can be used as arglists for COMPARE-FILES.
112 (defun create-test-combinations (file-name symbols
)
113 (let ((file-variants (loop :for symbol
:in symbols
114 :nconc
(create-file-variants file-name symbol
))))
115 (loop :for
(name-in . external-format-in
) :in file-variants
116 :nconc
(loop :for
(name-out . external-format-out
) :in file-variants
117 :collect
(list name-in external-format-in
118 name-out external-format-out
)))))
120 ;;; Returns a true value iff FILE1 and FILE2 have the same contents
121 ;;; (viewed as binary files).
122 (defun file-equal (file1 file2
)
123 (with-open-file (stream1 file1
:element-type
'(unsigned-byte 8))
124 (with-open-file (stream2 file2
:element-type
'(unsigned-byte 8))
125 (and (= (file-length stream1
) (file-length stream2
))
126 (loop :for byte1
:= (read-byte stream1 nil nil
)
127 :for byte2
:= (read-byte stream2 nil nil
)
128 :while
(and byte1 byte2
)
129 :always
(= byte1 byte2
))))))
131 ;;; Copies the contents of the file denoted by the pathname PATH-IN to
132 ;;; the file denoted by the pathname PATH-OUT using flexi streams -
133 ;;; STREAM-IN is read with the external format EXTERNAL-FORMAT-IN and
134 ;;; STREAM-OUT is written with EXTERNAL-FORMAT-OUT. The input file is
135 ;;; opened with the :DIRECTION keyword argument DIRECTION-IN, the
136 ;;; output file is opened with the :DIRECTION keyword argument
138 (defun copy-file (path-in external-format-in path-out external-format-out
139 direction-out direction-in
)
140 (with-open-file-stream (in path-in
141 :direction direction-in
142 :if-does-not-exist
:error
143 :if-exists
:overwrite
144 :external-format external-format-in
)
145 (with-open-file-stream (out path-out
146 :direction direction-out
147 :if-does-not-exist
:create
148 :if-exists
:supersede
149 :external-format external-format-out
)
150 (loop :for line
:= (read-line in nil nil
)
151 :while line
:do
(write-line line out
)))))
155 (babel-encodings:enc-name
(babel:external-format-encoding ef
))
156 (babel:external-format-eol-style ef
)))
158 ;;; Copies the contents of the file (in the 'test') denoted by the
159 ;;; relative pathname PATH-IN to the file (in a temporary directory)
160 ;;; denoted by the relative pathname PATH-OUT using flexi streams -
161 ;;; STREAM-IN is read with the external format EXTERNAL-FORMAT-IN and
162 ;;; STREAM-OUT is written with EXTERNAL-FORMAT-OUT. The resulting
163 ;;; file is compared with an existing file in the 'test' directory to
164 ;;; check if the outcome is as expected. Uses various variants of the
165 ;;; :DIRECTION keyword when opening the files."
166 (defun compare-files (path-in external-format-in path-out external-format-out
)
167 (let ((full-path-in (merge-pathnames path-in
*data-dir
*))
168 (full-path-out (merge-pathnames path-out
*test-dir
*))
169 (full-path-orig (merge-pathnames path-out
*data-dir
*)))
170 (dolist (direction-out '(:output
:io
) t
)
171 (dolist (direction-in '(:input
:io
))
172 (let ((description (format nil
"Test ~S ~A [~A] --> ~A [~A]"
173 path-in
(ef-name external-format-in
)
174 direction-in
(ef-name external-format-out
)
176 (format *error-output
* "~&;; ~A.~%" description
)
177 (copy-file full-path-in external-format-in
178 full-path-out external-format-out
179 direction-out direction-in
)
180 (unless (file-equal full-path-out full-path-orig
)
181 (format *error-output
* "~&;; Test failed!!!~%")
184 (test big-stream-comparision-test
186 (let ((args-list (loop :for
(file-name symbols
) :in
*test-files
*
187 :nconc
(create-test-combinations file-name symbols
))))
188 (loop :for args
:in args-list
189 :unless
(apply #'compare-files args
)