1.0.3.7: Compile XEPs using the policy from the correct environment
[sbcl.git] / tests / external-format.impure.lisp
blob430078ba5961b88a716d57b62d5b67f6777d8b41
1 ;;;; This file is for testing external-format functionality, using
2 ;;;; test machinery which might have side effects (e.g. executing
3 ;;;; DEFUN, writing files). Note that the tests here reach into
4 ;;;; unexported functionality, and should not be used as a guide for
5 ;;;; users.
7 ;;;; This software is part of the SBCL system. See the README file for
8 ;;;; more information.
9 ;;;;
10 ;;;; While most of SBCL is derived from the CMU CL system, the test
11 ;;;; files (like this one) were written from scratch after the fork
12 ;;;; from CMU CL.
13 ;;;;
14 ;;;; This software is in the public domain and is provided with
15 ;;;; absolutely no warranty. See the COPYING and CREDITS files for
16 ;;;; more information.
18 (defmacro do-external-formats ((xf &optional result) &body body)
19 (let ((nxf (gensym)))
20 `(dolist (,nxf sb-impl::*external-formats* ,result)
21 (let ((,xf (first (first ,nxf))))
22 ,@body))))
24 (do-external-formats (xf)
25 (with-open-file (s #-win32 "/dev/null" #+win32 "nul" :direction :input :external-format xf)
26 (assert (eq (read-char s nil s) s))))
28 ;;; Test standard character read-write equivalency over all external formats.
29 (let ((standard-characters "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!$\"'(),_-./:;?+<=>#%&*@[\\]{|}`^~"))
30 (do-external-formats (xf)
31 (with-open-file (s "external-format-test.txt" :direction :output
32 :if-exists :supersede :external-format xf)
33 (loop for character across standard-characters
34 do (write-char character s)))
35 (with-open-file (s "external-format-test.txt" :direction :input
36 :external-format xf)
37 (loop for character across standard-characters
38 do (assert (eql (read-char s) character))))))
40 (delete-file "external-format-test.txt")
41 #-sb-unicode
42 (progn
43 (test-util:report-test-status)
44 (sb-ext:quit :unix-status 104))
46 ;;; Test UTF-8 writing and reading of 1, 2, 3 and 4 octet characters with
47 ;;; all possible offsets. Tests for buffer edge bugs. fd-stream buffers are
48 ;;; 4096 wide.
49 (dotimes (width-1 4)
50 (let ((character (code-char (elt '(1 #x81 #x801 #x10001) width-1))))
51 (dotimes (offset (+ width-1 1))
52 (with-open-file (s "external-format-test.txt" :direction :output
53 :if-exists :supersede :external-format :utf-8)
54 (dotimes (n offset)
55 (write-char #\a s))
56 (dotimes (n 4097)
57 (write-char character s)))
58 (with-open-file (s "external-format-test.txt" :direction :input
59 :external-format :utf-8)
60 (dotimes (n offset)
61 (assert (eql (read-char s) #\a)))
62 (dotimes (n 4097)
63 (assert (eql (read-char s) character)))
64 (assert (eql (read-char s nil s) s))))))
66 ;;; Test character decode restarts.
67 (with-open-file (s "external-format-test.txt" :direction :output
68 :if-exists :supersede :element-type '(unsigned-byte 8))
69 (write-byte 65 s)
70 (write-byte 66 s)
71 (write-byte #xe0 s)
72 (write-byte 67 s))
73 (with-open-file (s "external-format-test.txt" :direction :input
74 :external-format :utf-8)
75 (handler-bind
76 ((sb-int:character-decoding-error #'(lambda (decoding-error)
77 (declare (ignore decoding-error))
78 (invoke-restart
79 'sb-int:attempt-resync))))
80 (assert (equal (read-line s nil s) "ABC"))
81 (assert (equal (read-line s nil s) s))))
82 (with-open-file (s "external-format-test.txt" :direction :input
83 :external-format :utf-8)
84 (handler-bind
85 ((sb-int:character-decoding-error #'(lambda (decoding-error)
86 (declare (ignore decoding-error))
87 (invoke-restart
88 'sb-int:force-end-of-file))))
89 (assert (equal (read-line s nil s) "AB"))
90 (assert (equal (read-line s nil s) s))))
92 ;;; And again with more data to account for buffering (this was briefly)
93 ;;; broken in early 0.9.6.
94 (with-open-file (s "external-format-test.txt" :direction :output
95 :if-exists :supersede :element-type '(unsigned-byte 8))
96 (let ((a (make-array 50
97 :element-type '(unsigned-byte 64)
98 :initial-contents (map 'list #'char-code
99 "1234567890123456789012345678901234567890123456789."))))
100 (setf (aref a 49) (char-code #\Newline))
101 (dotimes (i 40)
102 (write-sequence a s))
103 (write-byte #xe0 s)
104 (dotimes (i 40)
105 (write-sequence a s))))
106 (with-test (:name (:character-decode-large :attempt-resync))
107 (with-open-file (s "external-format-test.txt" :direction :input
108 :external-format :utf-8)
109 (handler-bind
110 ((sb-int:character-decoding-error #'(lambda (decoding-error)
111 (declare (ignore decoding-error))
112 (invoke-restart
113 'sb-int:attempt-resync)))
114 ;; The failure mode is an infinite loop, add a timeout to detetct it.
115 (sb-ext:timeout (lambda () (error "Timeout"))))
116 (sb-ext:with-timeout 5
117 (dotimes (i 80)
118 (assert (equal (read-line s nil s)
119 "1234567890123456789012345678901234567890123456789")))))))
121 (with-test (:name (:character-decode-large :force-end-of-file)
122 :fails-on :sbcl)
123 (error "We can't reliably test this due to WITH-TIMEOUT race condition")
124 ;; This test will currently fail. But sometimes it will fail in
125 ;; ungracefully due to the WITH-TIMEOUT race mentioned above. This
126 ;; rightfully confuses some people, so we'll skip running the code
127 ;; for now. -- JES, 2006-01-27
128 #+nil
129 (with-open-file (s "external-format-test.txt" :direction :input
130 :external-format :utf-8)
131 (handler-bind
132 ((sb-int:character-decoding-error #'(lambda (decoding-error)
133 (declare (ignore decoding-error))
134 (invoke-restart
135 'sb-int:force-end-of-file)))
136 ;; The failure mode is an infinite loop, add a timeout to detetct it.
137 (sb-ext:timeout (lambda () (error "Timeout"))))
138 (sb-ext:with-timeout 5
139 (dotimes (i 80)
140 (assert (equal (read-line s nil s)
141 "1234567890123456789012345678901234567890123456789")))
142 (assert (equal (read-line s nil s) s))))))
144 ;;; Test character encode restarts.
145 (with-open-file (s "external-format-test.txt" :direction :output
146 :if-exists :supersede :external-format :latin-1)
147 (handler-bind
148 ((sb-int:character-encoding-error #'(lambda (encoding-error)
149 (declare (ignore encoding-error))
150 (invoke-restart
151 'sb-impl::output-nothing))))
152 (write-char #\A s)
153 (write-char #\B s)
154 (write-char (code-char 322) s)
155 (write-char #\C s)))
156 (with-open-file (s "external-format-test.txt" :direction :input
157 :external-format :latin-1)
158 (assert (equal (read-line s nil s) "ABC"))
159 (assert (equal (read-line s nil s) s)))
161 (with-open-file (s "external-format-test.txt" :direction :output
162 :if-exists :supersede :external-format :latin-1)
163 (handler-bind
164 ((sb-int:character-encoding-error #'(lambda (encoding-error)
165 (declare (ignore encoding-error))
166 (invoke-restart
167 'sb-impl::output-nothing))))
168 (let ((string (make-array 4 :element-type 'character
169 :initial-contents `(#\A #\B ,(code-char 322)
170 #\C))))
171 (write-string string s))))
172 (with-open-file (s "external-format-test.txt" :direction :input
173 :external-format :latin-1)
174 (assert (equal (read-line s nil s) "ABC"))
175 (assert (equal (read-line s nil s) s)))
177 ;;; Test skipping character-decode-errors in comments.
178 (let ((s (open "external-format-test.lisp" :direction :output
179 :if-exists :supersede :external-format :latin-1)))
180 (unwind-protect
181 (progn
182 (write-string ";;; ABCD" s)
183 (write-char (code-char 233) s)
184 (terpri s)
185 (close s)
186 (compile-file "external-format-test.lisp" :external-format :utf-8))
187 (delete-file s)
188 (let ((p (probe-file (compile-file-pathname "external-format-test.lisp"))))
189 (when p
190 (delete-file p)))))
193 ;;;; KOI8-R external format
194 (with-open-file (s "external-format-test.txt" :direction :output
195 :if-exists :supersede :external-format :koi8-r)
196 (write-char (code-char #xB0) s)
197 (assert (eq
198 (handler-case
199 (progn
200 (write-char (code-char #xBAAD) s)
201 :bad)
202 (sb-int:character-encoding-error ()
203 :good))
204 :good)))
205 (with-open-file (s "external-format-test.txt" :direction :input
206 :element-type '(unsigned-byte 8))
207 (let ((byte (read-byte s)))
208 (assert (= (eval byte) #x9C))))
209 (with-open-file (s "external-format-test.txt" :direction :input
210 :external-format :koi8-r)
211 (let ((char (read-char s)))
212 (assert (= (char-code (eval char)) #xB0))))
213 (delete-file "external-format-test.txt")
215 (let* ((koi8-r-codes (coerce '(240 210 201 215 197 212 33) '(vector (unsigned-byte 8))))
216 (uni-codes #(1055 1088 1080 1074 1077 1090 33))
218 (string (octets-to-string koi8-r-codes :external-format :koi8-r))
219 (uni-decoded (map 'vector #'char-code string)))
220 (assert (equalp (map 'vector #'char-code (octets-to-string koi8-r-codes :external-format :koi8-r))
221 uni-codes))
222 (assert (equalp (string-to-octets (map 'string #'code-char uni-codes) :external-format :koi8-r)
223 koi8-r-codes)))
225 ;;; tests of FILE-STRING-LENGTH
226 (let ((standard-characters "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!$\"'(),_-./:;?+<=>#%&*@[\\]{|}`^~"))
227 (do-external-formats (xf)
228 (with-open-file (s "external-format-test.txt" :direction :output
229 :external-format xf)
230 (loop for x across standard-characters
231 for position = (file-position s)
232 for char-length = (file-string-length s x)
233 do (write-char x s)
234 do (assert (= (file-position s) (+ position char-length))))
235 (let ((position (file-position s))
236 (string-length (file-string-length s standard-characters)))
237 (write-string standard-characters s)
238 (assert (= (file-position s) (+ position string-length)))))
239 (delete-file "external-format-test.txt")))
241 (let ((char-codes '(0 1 255 256 511 512 1023 1024 2047 2048 4095 4096
242 8191 8192 16383 16384 32767 32768 65535 65536 131071
243 131072 262143 262144)))
244 (with-open-file (s "external-format-test.txt" :direction :output
245 :external-format :utf-8)
246 (dolist (code char-codes)
247 (let* ((char (code-char code))
248 (position (file-position s))
249 (char-length (file-string-length s char)))
250 (write-char char s)
251 (assert (= (file-position s) (+ position char-length)))))
252 (let* ((string (map 'string #'code-char char-codes))
253 (position (file-position s))
254 (string-length (file-string-length s string)))
255 (write-string string s)
256 (assert (= (file-position s) (+ position string-length))))))
259 ;;; See sbcl-devel "Subject: Bug in FILE-POSITION on UTF-8-encoded files"
260 ;;; by Lutz Euler on 2006-03-05 for more details.
261 (with-test (:name (:file-position :utf-8))
262 (let ((path "external-format-test.txt"))
263 (with-open-file (s path
264 :direction :output
265 :if-exists :supersede
266 :element-type '(unsigned-byte 8))
267 ;; Write #\*, encoded in UTF-8, to the file.
268 (write-byte 42 s)
269 ;; Append #\adiaeresis, encoded in UTF-8, to the file.
270 (write-sequence '(195 164) s))
271 (with-open-file (s path :external-format :utf-8)
272 (read-char s)
273 (let ((pos (file-position s))
274 (char (read-char s)))
275 (format t "read character with code ~a successfully from file position ~a~%"
276 (char-code char) pos)
277 (file-position s pos)
278 (format t "set file position back to ~a, trying to read-char again~%" pos)
279 (let ((new-char (read-char s)))
280 (assert (char= char new-char)))))
281 (values)))
283 ;;; External format support in SB-ALIEN
285 (with-test (:name (:sb-alien :vanilla))
286 (define-alien-routine strdup c-string (str c-string))
287 (assert (equal "foo" (strdup "foo"))))
289 (with-test (:name (:sb-alien :utf-8 :utf-8))
290 (define-alien-routine strdup (c-string :external-format :utf-8)
291 (str (c-string :external-format :utf-8)))
292 (assert (equal "foo" (strdup "foo"))))
294 (with-test (:name (:sb-alien :latin-1 :utf-8))
295 (define-alien-routine strdup (c-string :external-format :latin-1)
296 (str (c-string :external-format :utf-8)))
297 (assert (= (length (strdup (string (code-char 246))))
298 2)))
300 (with-test (:name (:sb-alien :utf-8 :latin-1))
301 (define-alien-routine strdup (c-string :external-format :utf-8)
302 (str (c-string :external-format :latin-1)))
303 (assert (equal (string (code-char 228))
304 (strdup (concatenate 'string
305 (list (code-char 195))
306 (list (code-char 164)))))))
308 (with-test (:name (:sb-alien :ebcdic :ebcdic))
309 (define-alien-routine strdup (c-string :external-format :ebcdic-us)
310 (str (c-string :external-format :ebcdic-us)))
311 (assert (equal "foo" (strdup "foo"))))
313 (with-test (:name (:sb-alien :latin-1 :ebcdic))
314 (define-alien-routine strdup (c-string :external-format :latin-1)
315 (str (c-string :external-format :ebcdic-us)))
316 (assert (not (equal "foo" (strdup "foo")))))
318 (with-test (:name (:sb-alien :simple-base-string))
319 (define-alien-routine strdup (c-string :external-format :ebcdic-us
320 :element-type base-char)
321 (str (c-string :external-format :ebcdic-us)))
322 (assert (typep (strdup "foo") 'simple-base-string)))
324 ;;;; success