d1a9644efbc1d0d49e47f2302af50a4fb958761e
[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 (delete-file "external-format-test.txt")
41 #-sb-unicode
42 (sb-ext:quit :unix-status 104)
43
44 ;;; Test UTF-8 writing and reading of 1, 2, 3 and 4 octet characters with
45 ;;; all possible offsets. Tests for buffer edge bugs. fd-stream buffers are
46 ;;; 4096 wide.
47 (dotimes (width-1 4)
48   (let ((character (code-char (elt '(1 #x81 #x801 #x10001) width-1))))
49     (dotimes (offset (+ width-1 1))
50       (with-open-file (s "external-format-test.txt" :direction :output
51                        :if-exists :supersede :external-format :utf-8)
52         (dotimes (n offset)
53           (write-char #\a s))
54         (dotimes (n 4097)
55           (write-char character s)))
56       (with-open-file (s "external-format-test.txt" :direction :input
57                        :external-format :utf-8)
58         (dotimes (n offset)
59           (assert (eql (read-char s) #\a)))
60         (dotimes (n 4097)
61           (assert (eql (read-char s) character)))
62         (assert (eql (read-char s nil s) s))))))
63
64 ;;; Test character decode restarts.
65 (with-open-file (s "external-format-test.txt" :direction :output
66                  :if-exists :supersede :element-type '(unsigned-byte 8))
67   (write-byte 65 s)
68   (write-byte 66 s)
69   (write-byte #xe0 s)
70   (write-byte 67 s))
71 (with-open-file (s "external-format-test.txt" :direction :input
72                  :external-format :utf-8)
73   (handler-bind
74       ((sb-int:character-decoding-error #'(lambda (decoding-error)
75                                             (declare (ignore decoding-error))
76                                             (invoke-restart
77                                              'sb-int:attempt-resync))))
78     (assert (equal (read-line s nil s) "ABC"))
79     (assert (equal (read-line s nil s) s))))
80 (with-open-file (s "external-format-test.txt" :direction :input
81                  :external-format :utf-8)
82   (handler-bind
83       ((sb-int:character-decoding-error #'(lambda (decoding-error)
84                                             (declare (ignore decoding-error))
85                                             (invoke-restart
86                                              'sb-int:force-end-of-file))))
87     (assert (equal (read-line s nil s) "AB"))
88     (assert (equal (read-line s nil s) s))))
89
90 ;;; Test character encode restarts.
91 (with-open-file (s "external-format-test.txt" :direction :output
92                  :if-exists :supersede :external-format :latin-1)
93   (handler-bind
94       ((sb-int:character-encoding-error #'(lambda (encoding-error)
95                                             (declare (ignore encoding-error))
96                                             (invoke-restart
97                                              'sb-impl::output-nothing))))
98     (write-char #\A s)
99     (write-char #\B s)
100     (write-char (code-char 322) s)
101     (write-char #\C s)))
102 (with-open-file (s "external-format-test.txt" :direction :input
103                  :external-format :latin-1)
104   (assert (equal (read-line s nil s) "ABC"))
105   (assert (equal (read-line s nil s) s)))
106
107 (with-open-file (s "external-format-test.txt" :direction :output
108                  :if-exists :supersede :external-format :latin-1)
109   (handler-bind
110       ((sb-int:character-encoding-error #'(lambda (encoding-error)
111                                             (declare (ignore encoding-error))
112                                             (invoke-restart
113                                              'sb-impl::output-nothing))))
114     (let ((string (make-array 4 :element-type 'character
115                               :initial-contents `(#\A #\B ,(code-char 322)
116                                                       #\C))))
117       (write-string string s))))
118 (with-open-file (s "external-format-test.txt" :direction :input
119                  :external-format :latin-1)
120   (assert (equal (read-line s nil s) "ABC"))
121   (assert (equal (read-line s nil s) s)))
122
123 ;;; Test skipping character-decode-errors in comments.
124 (let ((s (open "external-format-test.lisp" :direction :output
125                :if-exists :supersede :external-format :latin-1)))
126   (unwind-protect
127        (progn
128          (write-string ";;; ABCD" s)
129          (write-char (code-char 233) s)
130          (terpri s)
131          (close s)
132          (compile-file "external-format-test.lisp" :external-format :utf-8))
133     (delete-file s)
134     (let ((p (probe-file (compile-file-pathname "external-format-test.lisp"))))
135       (when p
136         (delete-file p)))))
137
138 \f
139 ;;;; KOI8-R external format
140 (with-open-file (s "external-format-test.txt" :direction :output
141                  :if-exists :supersede :external-format :koi8-r)
142   (write-char (code-char #xB0) s)
143   (assert (eq
144            (handler-case
145                (progn
146                  (write-char (code-char #xBAAD) s)
147                  :bad)
148              (sb-int:character-encoding-error ()
149                :good))
150            :good)))
151 (with-open-file (s "external-format-test.txt" :direction :input
152                  :element-type '(unsigned-byte 8))
153   (let ((byte (read-byte s)))
154     (assert (= (eval byte) #x9C))))
155 (with-open-file (s "external-format-test.txt" :direction :input
156                  :external-format :koi8-r)
157   (let ((char (read-char s)))
158     (assert (= (char-code (eval char)) #xB0))))
159
160 \f
161 (delete-file "external-format-test.txt")
162 (sb-ext:quit :unix-status 104)