0.9.0.13:
[sbcl.git] / tests / external-format.impure.lisp
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.
6
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.
17
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))))
23
24 (do-external-formats (xf)
25   (with-open-file (s "/dev/null" :direction :input :external-format xf)
26     (assert (eq (read-char s nil s) s))))
27
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))))))
39
40 ;;; Test UTF-8 writing and reading of 1, 2, 3 and 4 octet characters with
41 ;;; all possible offsets. Tests for buffer edge bugs. fd-stream buffers are
42 ;;; 4096 wide.
43 (dotimes (width-1 4)
44   (let ((character (code-char (elt '(1 #x81 #x801 #x10001) width-1))))
45     (dotimes (offset (+ width-1 1))
46       (with-open-file (s "external-format-test.txt" :direction :output
47                        :if-exists :supersede :external-format :utf-8)
48         (dotimes (n offset)
49           (write-char #\a s))
50         (dotimes (n 4097)
51           (write-char character s)))
52       (with-open-file (s "external-format-test.txt" :direction :input
53                        :external-format :utf-8)
54         (dotimes (n offset)
55           (assert (eql (read-char s) #\a)))
56         (dotimes (n 4097)
57           (assert (eql (read-char s) character)))
58         (assert (eql (read-char s nil s) s))))))
59
60 ;;; Test character decode restarts.
61 (with-open-file (s "external-format-test.txt" :direction :output
62                  :if-exists :supersede :element-type '(unsigned-byte 8))
63   (write-byte 65 s)
64   (write-byte 66 s)
65   (write-byte #xe0 s)
66   (write-byte 67 s))
67 (with-open-file (s "external-format-test.txt" :direction :input
68                  :external-format :utf-8)
69   (handler-bind
70       ((sb-int:character-decoding-error #'(lambda (decoding-error)
71                                             (declare (ignore decoding-error))
72                                             (invoke-restart
73                                              'sb-int:attempt-resync))))
74     (assert (equal (read-line s nil s) "ABC"))
75     (assert (equal (read-line s nil s) s))))
76 (with-open-file (s "external-format-test.txt" :direction :input
77                  :external-format :utf-8)
78   (handler-bind
79       ((sb-int:character-decoding-error #'(lambda (decoding-error)
80                                             (declare (ignore decoding-error))
81                                             (invoke-restart
82                                              'sb-int:force-end-of-file))))
83     (assert (equal (read-line s nil s) "AB"))
84     (assert (equal (read-line s nil s) s))))
85
86 ;;; Test character encode restarts.
87 (with-open-file (s "external-format-test.txt" :direction :output
88                  :if-exists :supersede :external-format :latin-1)
89   (handler-bind
90       ((sb-int:character-encoding-error #'(lambda (encoding-error)
91                                             (declare (ignore encoding-error))
92                                             (invoke-restart
93                                              'sb-impl::output-nothing))))
94     (write-char #\A s)
95     (write-char #\B s)
96     (write-char (code-char 322) s)
97     (write-char #\C s)))
98 (with-open-file (s "external-format-test.txt" :direction :input
99                  :external-format :latin-1)
100   (assert (equal (read-line s nil s) "ABC"))
101   (assert (equal (read-line s nil s) s)))
102
103 (with-open-file (s "external-format-test.txt" :direction :output
104                  :if-exists :supersede :external-format :latin-1)
105   (handler-bind
106       ((sb-int:character-encoding-error #'(lambda (encoding-error)
107                                             (declare (ignore encoding-error))
108                                             (invoke-restart
109                                              'sb-impl::output-nothing))))
110     (let ((string (make-array 4 :element-type 'character
111                               :initial-contents `(#\A #\B ,(code-char 322)
112                                                       #\C))))
113       (write-string string s))))
114 (with-open-file (s "external-format-test.txt" :direction :input
115                  :external-format :latin-1)
116   (assert (equal (read-line s nil s) "ABC"))
117   (assert (equal (read-line s nil s) s)))
118
119 ;;; Test skipping character-decode-errors in comments.
120 (let ((s (open "external-format-test.lisp" :direction :output
121                :if-exists :supersede :external-format :latin-1)))
122   (unwind-protect
123        (progn
124          (write-string ";;; ABCD" s)
125          (write-char (code-char 233) s)
126          (terpri s)
127          (close s)
128          (compile-file "external-format-test.lisp" :external-format :utf-8))
129     (delete-file s)
130     (let ((p (probe-file (compile-file-pathname "external-format-test.lisp"))))
131       (when p
132         (delete-file p)))))
133
134 (sb-ext:quit :unix-status 104)