| 1 |
;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil -*- |
| 2 |
;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab: |
| 3 |
|
| 4 |
;;;; Author: Juan Jose Garcia-Ripoll |
| 5 |
;;;; Created: Sat Jan 03 2:56:03 CEST 2007 |
| 6 |
;;;; Contains: External format tests |
| 7 |
;;;; |
| 8 |
;;;; Based on the code and files from FLEXI-STREAMS 1.0.7 |
| 9 |
;;;; |
| 10 |
|
| 11 |
(in-package :cl-test) |
| 12 |
|
| 13 |
(suite 'eformat) |
| 14 |
|
| 15 |
|
| 16 |
;;; eformat-001 |
| 17 |
|
| 18 |
(defconstant +buffer-size+ 8192 |
| 19 |
"Size of buffers for COPY-STREAM* below.") |
| 20 |
|
| 21 |
(defparameter *copy-function* 'copy-stream |
| 22 |
"Which function to use when copying from one stream to the other - |
| 23 |
see for example COPY-FILE below.") |
| 24 |
|
| 25 |
(defparameter *eformat-test-files* |
| 26 |
'(("unicode_demo" (:utf8 :ucs2 :ucs4)) |
| 27 |
("kafka" (:utf8 :latin1 :cp1252)) |
| 28 |
("hebrew" (:utf8 :latin8)) |
| 29 |
("russian" (:utf8 :koi8r)) |
| 30 |
("tilton" (:utf8 :ascii))) |
| 31 |
"A list of test files where each entry consists of the name |
| 32 |
prefix and a list of encodings.") |
| 33 |
|
| 34 |
(defparameter *eformat-tests-directory* |
| 35 |
(merge-pathnames "eformat-tests/" *aux-dir*)) |
| 36 |
|
| 37 |
(defparameter *eformat-sandbox-directory* |
| 38 |
(merge-pathnames "eformat-tests/" *tmp-dir*)) |
| 39 |
|
| 40 |
(defun create-file-variants (file-name symbol) |
| 41 |
"For a name suffix FILE-NAME and a symbol SYMBOL denoting an |
| 42 |
encoding returns a list of pairs where the car is a full file |
| 43 |
name and the cdr is the corresponding external format. This list |
| 44 |
contains all possible variants w.r.t. to line-end conversion and |
| 45 |
endianness." |
| 46 |
(let ((variants (ecase symbol |
| 47 |
(:ascii '(:us-ascii)) |
| 48 |
(:latin1 '(:latin-1)) |
| 49 |
(:latin8 '(:iso-8859-8)) |
| 50 |
(:cp1252 '(:windows-cp1252)) |
| 51 |
(:koi8r '(:koi8-r)) |
| 52 |
(:utf8 '(:utf-8)) |
| 53 |
(:ucs2 '(:ucs-2be :ucs-2le)) |
| 54 |
(:ucs4 '(:ucs-4be :ucs-4le))))) |
| 55 |
(loop for arg in variants |
| 56 |
nconc (let* ((endian-suffix (case arg |
| 57 |
((:ucs-2be :ucs-4be) "_be") |
| 58 |
((:ucs-2le :ucs-4le) "_le") |
| 59 |
(t "")))) |
| 60 |
(loop for eol-style in '(:lf :cr :crlf) |
| 61 |
collect (cons (format nil "~A_~(~A~)_~(~A~)~A.txt" |
| 62 |
file-name symbol eol-style endian-suffix) |
| 63 |
(list eol-style arg))))))) |
| 64 |
|
| 65 |
(defun create-test-combinations (file-name symbols &optional simplep) |
| 66 |
"For a name suffix FILE-NAME and a list of symbols SYMBOLS denoting |
| 67 |
different encodings of the corresponding file returns a list of lists |
| 68 |
which can be used as arglists by DO-EFORMAT-TEST-001. If SIMPLEP is true, a |
| 69 |
list which can be used for the string and sequence tests below is |
| 70 |
returned." |
| 71 |
(let ((file-variants (loop for symbol in symbols |
| 72 |
nconc (create-file-variants file-name symbol)))) |
| 73 |
(loop for (name-in . external-format-in) in file-variants |
| 74 |
when simplep |
| 75 |
collect (list name-in external-format-in) |
| 76 |
else |
| 77 |
nconc (loop for (name-out . external-format-out) in file-variants |
| 78 |
collect (list name-in external-format-in name-out external-format-out))))) |
| 79 |
|
| 80 |
(defun file-equal (file1 file2) |
| 81 |
"Returns a true value iff FILE1 and FILE2 have the same |
| 82 |
contents \(viewed as binary files)." |
| 83 |
(with-open-file (stream1 file1 :element-type '(unsigned-byte 8)) |
| 84 |
(with-open-file (stream2 file2 :element-type '(unsigned-byte 8)) |
| 85 |
(if (= (file-length stream1) (file-length stream2)) |
| 86 |
(loop for p1 = (file-position stream1) |
| 87 |
for byte1 = (read-byte stream1 nil nil) |
| 88 |
for byte2 = (read-byte stream2 nil nil) |
| 89 |
while (and byte1 byte2) |
| 90 |
unless (= byte1 byte2) |
| 91 |
do (return (values nil p1 byte1 byte2)) |
| 92 |
finally (return (values t 0 0 0))) |
| 93 |
(values nil -1 (file-length stream1) (file-length stream2)))))) |
| 94 |
|
| 95 |
(defun copy-stream (in out) |
| 96 |
"Copies the contents of the binary stream STREAM-IN to the |
| 97 |
binary stream STREAM-OUT using flexi streams - STREAM-IN is read |
| 98 |
with the external format EXTERNAL-FORMAT-IN and STREAM-OUT is |
| 99 |
written with EXTERNAL-FORMAT-OUT." |
| 100 |
(loop for line = (read-line in nil nil) |
| 101 |
while line |
| 102 |
do (write-line line out))) |
| 103 |
|
| 104 |
(defun copy-stream* (in out) |
| 105 |
"Like COPY-STREAM, but uses READ-SEQUENCE and WRITE-SEQUENCE instead |
| 106 |
of READ-LINE and WRITE-LINE." |
| 107 |
(let ((buffer (make-array +buffer-size+ :element-type 'character))) |
| 108 |
(loop |
| 109 |
(let ((position (read-sequence buffer in))) |
| 110 |
(when (zerop position) (return)) |
| 111 |
(write-sequence buffer out :end position))))) |
| 112 |
|
| 113 |
(defun copy-stream** (in out) |
| 114 |
"Like COPY-STREAM, but uses a random combination of PEEK-CHAR, |
| 115 |
READ-CHAR, READ-LINE, READ-SEQUENCE, UNREAD-CHAR and WRITE-CHAR, |
| 116 |
WRITE-LINE, WRITE-SEQUENCE, WRITE-STRING instead of READ-LINE and |
| 117 |
WRITE-LINE." |
| 118 |
(flet ((random (n) |
| 119 |
(if (> n 0) |
| 120 |
(cl:random n) |
| 121 |
0))) |
| 122 |
(loop |
| 123 |
with buffer = (make-array +buffer-size+ :element-type 'character :initial-element #\0) |
| 124 |
with pos = 0 |
| 125 |
with previous-operation = nil |
| 126 |
with eof = nil |
| 127 |
for operation = (elt '(peek-char read-char read-line read-sequence unread-char) |
| 128 |
(random 5)) |
| 129 |
for previous-pos = pos |
| 130 |
if (eql operation 'peek-char) do |
| 131 |
(let ((char (peek-char nil in nil nil))) |
| 132 |
(unless char |
| 133 |
(setf eof t))) |
| 134 |
if (eql operation 'read-char) do |
| 135 |
(let ((char (read-char in nil nil))) |
| 136 |
(if char |
| 137 |
(progn (setf (elt buffer pos) char) |
| 138 |
(incf pos)) |
| 139 |
(setf eof t))) |
| 140 |
if (eql operation 'read-line) do |
| 141 |
(multiple-value-bind (line missing-newline) |
| 142 |
(read-line in nil nil) |
| 143 |
(if line |
| 144 |
(progn (unless missing-newline |
| 145 |
(setf line (concatenate 'string line " |
| 146 |
"))) |
| 147 |
(setf buffer (concatenate 'string (subseq buffer 0 pos) line)) |
| 148 |
(incf pos (length line))) |
| 149 |
(setf eof t))) |
| 150 |
if (eql operation 'read-sequence) do |
| 151 |
(let ((end (+ pos (random (- (length buffer) pos))))) |
| 152 |
(when (> end pos) |
| 153 |
(setf pos (read-sequence buffer in |
| 154 |
:start pos |
| 155 |
:end end)) |
| 156 |
(when (< pos end) |
| 157 |
(setf eof t)))) |
| 158 |
if (eql operation 'unread-char) do |
| 159 |
(when (and (> pos 0) (not (or (eql previous-operation 'unread-char) |
| 160 |
(= pos previous-pos)))) |
| 161 |
(decf pos) |
| 162 |
(unread-char (elt buffer pos) in)) |
| 163 |
do (setf previous-operation operation) |
| 164 |
if (and (or (>= pos (length buffer)) eof) (> pos 0)) do |
| 165 |
(loop |
| 166 |
with pos2 = 0 |
| 167 |
for operation = (elt '(write-char write-line write-sequence write-string) |
| 168 |
(random 4)) |
| 169 |
if (eql operation 'write-char) do |
| 170 |
(write-char (elt buffer pos2) out) |
| 171 |
(incf pos2) |
| 172 |
if (eql operation 'write-line) do |
| 173 |
(let ((newline-pos (position #\Newline buffer :start pos2 :end pos))) |
| 174 |
(when newline-pos |
| 175 |
(write-line buffer out :start pos2 :end newline-pos) |
| 176 |
(setf pos2 (1+ newline-pos)))) |
| 177 |
if (eql operation 'write-sequence) do |
| 178 |
(let ((end (+ pos2 (random (- pos pos2))))) |
| 179 |
(write-sequence buffer out :start pos2 :end end) |
| 180 |
(setf pos2 end)) |
| 181 |
if (eql operation 'write-string) do |
| 182 |
(let ((end (+ pos2 (random (- pos pos2))))) |
| 183 |
(write-string buffer out :start pos2 :end end) |
| 184 |
(setf pos2 end)) |
| 185 |
while (< pos2 pos) |
| 186 |
finally (setf pos 0)) |
| 187 |
while (not eof)))) |
| 188 |
|
| 189 |
(defun do-eformat-test-001 (*copy-function*) |
| 190 |
"Each test in this suite copies the contents of one file \(in the |
| 191 |
`test' directory) to another file \(in a temporary directory) using |
| 192 |
flexi streams with different external formats. The resulting file is |
| 193 |
compared with an existing file in the `test' directory to check if the |
| 194 |
outcome is as expected. Uses various variants of the :DIRECTION |
| 195 |
keyword when opening the files. |
| 196 |
|
| 197 |
Returns a true value iff all tests succeeded. Prints information |
| 198 |
about each individual comparison if VERBOSE is true." |
| 199 |
(labels |
| 200 |
((copy-file (path-in external-format-in path-out external-format-out |
| 201 |
direction-out direction-in) |
| 202 |
(with-open-file (in path-in |
| 203 |
:element-type 'character |
| 204 |
:direction direction-in |
| 205 |
:if-does-not-exist :error |
| 206 |
:if-exists :overwrite |
| 207 |
:external-format external-format-in) |
| 208 |
(with-open-file (out path-out |
| 209 |
:element-type 'character |
| 210 |
:direction direction-out |
| 211 |
:if-does-not-exist :create |
| 212 |
:if-exists :supersede |
| 213 |
:external-format external-format-out) |
| 214 |
(funcall *copy-function* in out)))) |
| 215 |
(one-comparison (path-in external-format-in path-out external-format-out) |
| 216 |
(loop with full-path-in = (merge-pathnames path-in *eformat-tests-directory*) |
| 217 |
and full-path-out = (ensure-directories-exist |
| 218 |
(merge-pathnames path-out *eformat-sandbox-directory*)) |
| 219 |
and full-path-orig = (merge-pathnames path-out *eformat-tests-directory*) |
| 220 |
for direction-out in '(:output :io) |
| 221 |
nconc (loop for direction-in in '(:input :io) |
| 222 |
for args = (list path-in external-format-in direction-in |
| 223 |
path-out external-format-out direction-out) |
| 224 |
with ok = nil |
| 225 |
with pos = 0 |
| 226 |
with byte1 = 0 |
| 227 |
with byte2 = 0 |
| 228 |
unless (progn |
| 229 |
(copy-file full-path-in external-format-in |
| 230 |
full-path-out external-format-out |
| 231 |
direction-out direction-in) |
| 232 |
(is (multiple-value-setq (ok pos byte1 byte2) |
| 233 |
(file-equal full-path-out full-path-orig)) |
| 234 |
"~%~A -> ~A" path-in path-out)) |
| 235 |
collect (progn |
| 236 |
(format t "~%;;; Discordance at pos ~D between ~A and ~A~%in files ~A~%and ~A~%" |
| 237 |
pos byte1 byte2 full-path-out full-path-orig) |
| 238 |
args))))) |
| 239 |
(loop with do-eformat-test-001-args-list = |
| 240 |
(loop for (file-name symbols) in *eformat-test-files* |
| 241 |
nconc (create-test-combinations file-name symbols)) |
| 242 |
for (path-in external-format-in path-out external-format-out) in do-eformat-test-001-args-list |
| 243 |
nconc (one-comparison path-in external-format-in path-out external-format-out)))) |
| 244 |
|
| 245 |
;;; Date: 02/01/2007 |
| 246 |
;;; From: Juanjo |
| 247 |
;;; Fixed: Not a bug |
| 248 |
;;; Description: |
| 249 |
;;; |
| 250 |
;;; Test external formats by transcoding several files into all possible |
| 251 |
;;; supported formats and checking against the expected results. This |
| 252 |
;;; test uses READ/WRITE-LINE. |
| 253 |
;;; |
| 254 |
(test external-format.0001-transcode-read-char |
| 255 |
(is-false (do-eformat-test-001 'copy-stream))) |
| 256 |
|
| 257 |
;;; Date: 02/01/2007 |
| 258 |
;;; From: Juanjo |
| 259 |
;;; Fixed: Not a bug |
| 260 |
;;; Description: |
| 261 |
;;; |
| 262 |
;;; Test external formats by transcoding several files into all possible |
| 263 |
;;; supported formats and checking against the expected results. This |
| 264 |
;;; test uses READ/WRITE-SEQUENCE. |
| 265 |
;;; |
| 266 |
(test external-format.0002-transcode-read-char |
| 267 |
(is-false (do-eformat-test-001 'copy-stream*))) |
| 268 |
|
| 269 |
;;; Date: 12/06/2019 |
| 270 |
;;; From: Marius Gerbershagen |
| 271 |
;;; Fixed: Not a bug |
| 272 |
;;; Description: |
| 273 |
;;; |
| 274 |
;;; Test external formats by transcoding several files into all possible |
| 275 |
;;; supported formats and checking against the expected results. This |
| 276 |
;;; test uses a random combination of READ/WRITE functions. |
| 277 |
;;; |
| 278 |
(test external-format.0003-transcode-read-char |
| 279 |
(is-false (do-eformat-test-001 'copy-stream**))) |
| 280 |
|
| 281 |
|
| 282 |
;;; eformat-002 |
| 283 |
|
| 284 |
(load "sys:encodings;tools") |
| 285 |
|
| 286 |
(setf *print-circle* t) ; some mappings contain circular structures |
| 287 |
|
| 288 |
(defun binary-dump (filename &optional (position 0) (limit nil)) |
| 289 |
(format t "~%FILE: ~A from ~D, ~D bytes" filename position limit) |
| 290 |
(with-open-file (file filename :element-type '(unsigned-byte 8)) |
| 291 |
(file-position file position) |
| 292 |
(loop for i from 0 |
| 293 |
for byte = (read-byte file nil nil) |
| 294 |
for c = (and byte (code-char byte)) |
| 295 |
while (and byte (or (null limit) (< i limit))) |
| 296 |
do (progn (when (zerop (mod i 8)) (terpri)) |
| 297 |
(format t "~5X ~3A" byte |
| 298 |
(cond ((and (< 31 byte 127) (standard-char-p c)) |
| 299 |
c) |
| 300 |
((eql c #\Esc) "ESC") |
| 301 |
(t " "))) |
| 302 |
))) |
| 303 |
(terpri) |
| 304 |
(force-output)) |
| 305 |
|
| 306 |
(defun random-strings (char-bag n) |
| 307 |
(if (consp char-bag) |
| 308 |
(apply #'concatenate 'string |
| 309 |
(loop for i from 0 below 2 |
| 310 |
for actual-bag = (elt char-bag (random (length char-bag))) |
| 311 |
collect (random-strings actual-bag (random n)))) |
| 312 |
(concatenate 'string |
| 313 |
(loop for i from 0 to n |
| 314 |
for c = (char char-bag (random (length char-bag))) |
| 315 |
unless (eql c #\Newline) |
| 316 |
collect c)))) |
| 317 |
|
| 318 |
(defun compare-files (a b &optional all-chars) |
| 319 |
(with-open-file (sa a :direction :input :element-type '(unsigned-byte 8)) |
| 320 |
(with-open-file (sb b :direction :input :element-type '(unsigned-byte 8)) |
| 321 |
(loop for b1 = (read-byte sa nil nil) |
| 322 |
for b2 = (read-byte sb nil nil) |
| 323 |
while (or b1 b2) |
| 324 |
do (unless (eql b1 b2) |
| 325 |
(let* ((position (1- (file-position sa))) |
| 326 |
(start-dump (max 0 (- position 8)))) |
| 327 |
(setf position (logandc2 position 3)) |
| 328 |
(binary-dump a start-dump 32) |
| 329 |
(binary-dump b start-dump 32) |
| 330 |
(format t "~%Mismatch between~%~T~A~% and~T~A~% at file position ~D~%" |
| 331 |
a b position) |
| 332 |
(when all-chars |
| 333 |
(loop with imin = (floor start-dump 4) |
| 334 |
with imax = (min (+ imin 9) (length all-chars)) |
| 335 |
for i from imin below imax |
| 336 |
for j from 0 |
| 337 |
for c = (char all-chars i) |
| 338 |
do (progn (when (zerop (mod j 8)) (terpri)) |
| 339 |
(format t "~4X " (char-code c)))) |
| 340 |
(terpri)) |
| 341 |
(return nil))) |
| 342 |
finally (return t))))) |
| 343 |
|
| 344 |
(defun test-output (format-name &optional iconv-name (nlines 128) (nchars 10)) |
| 345 |
(set 'ext::foo format-name) |
| 346 |
(let* ((*print-circle* t) |
| 347 |
(mappings (loop for table = (ext::make-encoding format-name) |
| 348 |
while (and table (symbolp table)) |
| 349 |
do (setf format-name table) |
| 350 |
finally (return (or table format-name)))) |
| 351 |
(char-bags (all-valid-unicode-chars mappings)) |
| 352 |
(encoded-filename (format nil "sandbox/eformat-tmp/iconv-~A.txt" format-name)) |
| 353 |
(decoded-filename (format nil "sandbox/eformat-tmp/iconv-~A-utf32.txt" format-name)) |
| 354 |
(iconv-filename (format nil "sandbox/eformat-tmp/iconv-~A-iconv-utf32.txt" format-name)) |
| 355 |
(random-lines (loop for line from 1 to nlines |
| 356 |
collect (random-strings char-bags nchars))) |
| 357 |
(all-chars (apply #'concatenate 'string |
| 358 |
(loop for i in random-lines |
| 359 |
nconc (list i (list #\Newline)))))) |
| 360 |
(ensure-directories-exist encoded-filename) |
| 361 |
;; Output in that format |
| 362 |
(with-open-file (out encoded-filename :direction :output :external-format format-name |
| 363 |
:if-exists :supersede) |
| 364 |
(loop for i in random-lines |
| 365 |
do (write-line i out))) |
| 366 |
(with-open-file (out decoded-filename :direction :output :external-format :ucs-4be |
| 367 |
:if-exists :supersede) |
| 368 |
(loop for i in random-lines |
| 369 |
do (write-line i out))) |
| 370 |
(with-open-file (in encoded-filename :direction :input :external-format format-name) |
| 371 |
(loop for line = (read-line in nil nil) |
| 372 |
for i in random-lines |
| 373 |
for n from 1 |
| 374 |
while line |
| 375 |
unless (string= i line) |
| 376 |
do (progn |
| 377 |
(format t "Mismatch on line ~D between~% ~S and~% ~S" n line i) |
| 378 |
(return-from test-output nil)))) |
| 379 |
(when iconv-name |
| 380 |
(let ((command (format nil "iconv -f ~A -t UTF-32BE ~A > ~A" |
| 381 |
iconv-name encoded-filename iconv-filename))) |
| 382 |
(if (zerop |
| 383 |
(si::system command)) |
| 384 |
(compare-files decoded-filename iconv-filename all-chars) |
| 385 |
(prog1 T |
| 386 |
(format t "~&;;; iconv command failed:~A~%" command))))))) |
| 387 |
|
| 388 |
;;; Date: 09/01/2007 |
| 389 |
;;; From: Juanjo |
| 390 |
;;; Fixed: Not a bug |
| 391 |
;;; Description: |
| 392 |
;;; |
| 393 |
;;; Test external formats by transcoding random sequences of characters using |
| 394 |
;;; ECL and iconv. |
| 395 |
;;; |
| 396 |
#-msvc |
| 397 |
;; In Windows SYSTEM does not fail with a nonzero code when it |
| 398 |
;; fails to execute a command. Hence in that case we assume |
| 399 |
;; we simply can not run these tests |
| 400 |
(when (zerop (si::system "iconv -l >/dev/null 2>&1")) |
| 401 |
(test external-format.simple-iconv-check |
| 402 |
(is-false |
| 403 |
(loop for name in '(:ISO-8859-1 :ISO-8859-2 :ISO-8859-3 :ISO-8859-4 |
| 404 |
:ISO-8859-5 :ISO-8859-6 :ISO-8859-7 :ISO-8859-8 |
| 405 |
:ISO-8859-9 :ISO-8859-10 :ISO-8859-11 :ISO-8859-13 |
| 406 |
:ISO-8859-14 :ISO-8859-15 :ISO-8859-16 |
| 407 |
|
| 408 |
:KOI8-R :KOI8-U |
| 409 |
|
| 410 |
:IBM437 :IBM850 :IBM852 :IBM855 :IBM857 :IBM860 |
| 411 |
:IBM861 :IBM862 :IBM863 :IBM864 :IBM865 :IBM866 |
| 412 |
:IBM869 |
| 413 |
|
| 414 |
:CP936 :CP949 :CP950 |
| 415 |
|
| 416 |
:WINDOWS-1250 :WINDOWS-1251 :WINDOWS-1252 :WINDOWS-1253 |
| 417 |
:WINDOWS-1254 :WINDOWS-1256 :WINDOWS-1257 |
| 418 |
|
| 419 |
;; :CP932 :WINDOWS-1255 :WINDOWS-1258 with |
| 420 |
;; iconv may output combined characters, when ECL would |
| 421 |
;; output the base and the comibining one. Hence, no simple |
| 422 |
;; comparison is possible. |
| 423 |
|
| 424 |
:ISO-2022-JP |
| 425 |
;; :ISO-2022-JP-1 |
| 426 |
;; iconv doesn't support ISO-2022-JP-1 (hue hue hue) |
| 427 |
) |
| 428 |
unless (progn |
| 429 |
(loop for i from 1 to 10 |
| 430 |
always (is (test-output name (symbol-name name)) |
| 431 |
"iconv test ~s failed" name))) |
| 432 |
collect name)))) |
| 433 |
|
| 434 |
|