XEmacs 21.4.21.
[chise/xemacs-chise.git.1] / tests / automated / mule-tests.el
1 ;; Copyright (C) 1999 Free Software Foundation, Inc.
2
3 ;; Author: Hrvoje Niksic <hniksic@xemacs.org>
4 ;; Maintainers: Hrvoje Niksic <hniksic@xemacs.org>,
5 ;;              Martin Buchholz <martin@xemacs.org>
6 ;; Created: 1999
7 ;; Keywords: tests
8
9 ;; This file is part of XEmacs.
10
11 ;; XEmacs is free software; you can redistribute it and/or modify it
12 ;; under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; XEmacs is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 ;; General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with XEmacs; see the file COPYING.  If not, write to the Free
23 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24 ;; 02111-1307, USA.
25
26 ;;; Synched up with: Not in FSF.
27
28 ;;; Commentary:
29
30 ;; Test some Mule functionality (most of these remain to be written) .
31 ;; See test-harness.el for instructions on how to run these tests.
32
33 ;; This file will be (read)ed by a non-mule XEmacs, so don't use
34 ;; literal non-Latin1 characters.  Use (make-char) instead.
35
36 ;;-----------------------------------------------------------------
37 ;; Test whether all legal chars may be safely inserted to a buffer.
38 ;;-----------------------------------------------------------------
39
40 (defun test-chars (&optional for-test-harness)
41   "Insert all characters in a buffer, to see if XEmacs will crash.
42 This is done by creating a string with all the legal characters
43 in [0, 2^19) range, inserting it into the buffer, and checking
44 that the buffer's contents are equivalent to the string.
45
46 If FOR-TEST-HARNESS is specified, a temporary buffer is used, and
47 the Assert macro checks for correctness."
48   (let ((max (expt 2 (if (featurep 'mule) 19 8)))
49         (list nil)
50         (i 0))
51     (while (< i max)
52       (and (not for-test-harness)
53            (zerop (% i 1000))
54            (message "%d" i))
55       (and (int-char i)
56            ;; Don't aset to a string directly because random string
57            ;; access is O(n) under Mule.
58            (setq list (cons (int-char i) list)))
59       (setq i (1+ i)))
60     (let ((string (apply #'string (nreverse list))))
61       (if for-test-harness
62           ;; For use with test-harness, use Assert and a temporary
63           ;; buffer.
64           (with-temp-buffer
65             (insert string)
66             (Assert (equal (buffer-string) string)))
67         ;; For use without test harness: use a normal buffer, so that
68         ;; you can also test whether redisplay works.
69         (switch-to-buffer (get-buffer-create "test"))
70         (erase-buffer)
71         (buffer-disable-undo)
72         (insert string)
73         (assert (equal (buffer-string) string))))))
74
75 ;; It would be really *really* nice if test-harness allowed a way to
76 ;; run a test in byte-compiled mode only.  It's tedious to have
77 ;; time-consuming tests like this one run twice, once interpreted and
78 ;; once compiled, for no good reason.
79 (test-chars t)
80
81 ;;----------------------------------------------------------------
82 ;; Test that revert-buffer resets the modiff
83 ;; Bug reported 2007-06-20 <200706201902.32191.scop@xemacs.org>.
84 ;; Fixed 2007-06-22 <18043.2793.611745.734215@parhasard.net>.
85 ;;----------------------------------------------------------------
86
87 ;; #### need a temp file name but this will do for now
88 (let ((test-file-name (expand-file-name "~/test-revert-buffer-resets-modiff"))
89       revert-buffer-function
90       kill-buffer-hook)         ; paranoia
91   (find-file test-file-name)
92   (erase-buffer)
93   (insert "a string\n")
94   (save-buffer 0)
95   (insert "more text\n")
96   (revert-buffer t t)
97   ;; Just "find-file" with autodetect coding didn't fail for me, but it does
98   ;; fail under test harness.  Still we'll redo the test with an explicit
99   ;; coding system just in case.
100   (Assert (not (buffer-modified-p)))
101   (kill-buffer nil)
102   (when (find-coding-system 'utf-8)
103     (find-file test-file-name 'utf-8)
104     (insert "more text\n")
105     (revert-buffer t t)
106     (Assert (not (buffer-modified-p)))
107     (kill-buffer nil))
108   (delete-file test-file-name))
109
110 ;;-----------------------------------------------------------------
111 ;; Test string modification functions that modify the length of a char.
112 ;;-----------------------------------------------------------------
113
114 (when (featurep 'mule)
115   ;;---------------------------------------------------------------
116   ;; Test fillarray
117   ;;---------------------------------------------------------------
118   (macrolet
119       ((fillarray-test
120         (charset1 charset2)
121         (let ((char1 (make-char charset1 69))
122               (char2 (make-char charset2 69)))
123           `(let ((string (make-string 1000 ,char1)))
124              (fillarray string ,char2)
125              (Assert (eq (aref string 0) ,char2))
126              (Assert (eq (aref string (1- (length string))) ,char2))
127              (Assert (eq (length string) 1000))))))
128     (fillarray-test ascii latin-iso8859-1)
129     (fillarray-test ascii latin-iso8859-2)
130     (fillarray-test latin-iso8859-1 ascii)
131     (fillarray-test latin-iso8859-2 ascii))
132
133   ;; Test aset
134   (let ((string (string (make-char 'ascii 69) (make-char 'latin-iso8859-2 69))))
135     (aset string 0 (make-char 'latin-iso8859-2 42))
136     (Assert (eq (aref string 1) (make-char 'latin-iso8859-2 69))))
137
138   ;;---------------------------------------------------------------
139   ;; Test coding system functions
140   ;;---------------------------------------------------------------
141
142   ;; Create alias for coding system without subsidiaries
143   (Assert (coding-system-p (find-coding-system 'binary)))
144   (Assert (coding-system-canonical-name-p 'binary))
145   (Assert (not (coding-system-alias-p 'binary)))
146   (Assert (not (coding-system-alias-p 'mule-tests-alias)))
147   (Assert (not (coding-system-canonical-name-p 'mule-tests-alias)))
148   (Check-Error-Message
149    error "Symbol is the canonical name of a coding system and cannot be redefined"
150    (define-coding-system-alias 'binary 'iso8859-2))
151   (Check-Error-Message
152    error "Symbol is not a coding system alias"
153    (coding-system-aliasee 'binary))
154
155   (define-coding-system-alias 'mule-tests-alias 'binary)
156   (Assert (coding-system-alias-p 'mule-tests-alias))
157   (Assert (not (coding-system-canonical-name-p 'mule-tests-alias)))
158   (Assert (eq (get-coding-system 'binary) (get-coding-system 'mule-tests-alias)))
159   (Assert (eq 'binary (coding-system-aliasee 'mule-tests-alias)))
160   (Assert (not (coding-system-alias-p 'mule-tests-alias-unix)))
161   (Assert (not (coding-system-alias-p 'mule-tests-alias-dos)))
162   (Assert (not (coding-system-alias-p 'mule-tests-alias-mac)))
163
164   (define-coding-system-alias 'mule-tests-alias (get-coding-system 'binary))
165   (Assert (coding-system-alias-p 'mule-tests-alias))
166   (Assert (not (coding-system-canonical-name-p 'mule-tests-alias)))
167   (Assert (eq (get-coding-system 'binary) (get-coding-system 'mule-tests-alias)))
168   (Assert (eq 'binary (coding-system-aliasee 'mule-tests-alias)))
169   (Assert (not (coding-system-alias-p 'mule-tests-alias-unix)))
170   (Assert (not (coding-system-alias-p 'mule-tests-alias-dos)))
171   (Assert (not (coding-system-alias-p 'mule-tests-alias-mac)))
172
173   (define-coding-system-alias 'nested-mule-tests-alias 'mule-tests-alias)
174   (Assert (coding-system-alias-p 'nested-mule-tests-alias))
175   (Assert (not (coding-system-canonical-name-p 'nested-mule-tests-alias)))
176   (Assert (eq (get-coding-system 'binary) (get-coding-system 'nested-mule-tests-alias)))
177   (Assert (eq (coding-system-aliasee 'nested-mule-tests-alias) 'mule-tests-alias))
178   (Assert (eq 'mule-tests-alias (coding-system-aliasee 'nested-mule-tests-alias)))
179   (Assert (not (coding-system-alias-p 'nested-mule-tests-alias-unix)))
180   (Assert (not (coding-system-alias-p 'nested-mule-tests-alias-dos)))
181   (Assert (not (coding-system-alias-p 'nested-mule-tests-alias-mac)))
182
183   (Check-Error-Message
184    error "Attempt to create a coding system alias loop"
185    (define-coding-system-alias 'mule-tests-alias 'nested-mule-tests-alias))
186   (Check-Error-Message
187    error "No such coding system"
188    (define-coding-system-alias 'no-such-coding-system 'no-such-coding-system))
189   (Check-Error-Message
190    error "Attempt to create a coding system alias loop"
191    (define-coding-system-alias 'mule-tests-alias 'mule-tests-alias))
192
193   (define-coding-system-alias 'nested-mule-tests-alias nil)
194   (define-coding-system-alias 'mule-tests-alias nil)
195   (Assert (coding-system-p (find-coding-system 'binary)))
196   (Assert (coding-system-canonical-name-p 'binary))
197   (Assert (not (coding-system-alias-p 'binary)))
198   (Assert (not (coding-system-alias-p 'mule-tests-alias)))
199   (Assert (not (coding-system-canonical-name-p 'mule-tests-alias)))
200   (Check-Error-Message
201    error "Symbol is the canonical name of a coding system and cannot be redefined"
202    (define-coding-system-alias 'binary 'iso8859-2))
203   (Check-Error-Message
204    error "Symbol is not a coding system alias"
205    (coding-system-aliasee 'binary))
206
207   (define-coding-system-alias 'nested-mule-tests-alias nil)
208   (define-coding-system-alias 'mule-tests-alias nil)
209
210   ;; Create alias for coding system with subsidiaries
211   (define-coding-system-alias 'mule-tests-alias 'iso-8859-7)
212   (Assert (coding-system-alias-p 'mule-tests-alias))
213   (Assert (not (coding-system-canonical-name-p 'mule-tests-alias)))
214   (Assert (eq (get-coding-system 'iso-8859-7) (get-coding-system 'mule-tests-alias)))
215   (Assert (eq 'iso-8859-7 (coding-system-aliasee 'mule-tests-alias)))
216   (Assert (coding-system-alias-p 'mule-tests-alias-unix))
217   (Assert (coding-system-alias-p 'mule-tests-alias-dos))
218   (Assert (coding-system-alias-p 'mule-tests-alias-mac))
219
220   (define-coding-system-alias 'mule-tests-alias (get-coding-system 'iso-8859-7))
221   (Assert (coding-system-alias-p 'mule-tests-alias))
222   (Assert (not (coding-system-canonical-name-p 'mule-tests-alias)))
223   (Assert (eq (get-coding-system 'iso-8859-7) (get-coding-system 'mule-tests-alias)))
224   (Assert (eq 'iso-8859-7 (coding-system-aliasee 'mule-tests-alias)))
225   (Assert (coding-system-alias-p 'mule-tests-alias-unix))
226   (Assert (coding-system-alias-p 'mule-tests-alias-dos))
227   (Assert (coding-system-alias-p 'mule-tests-alias-mac))
228   (Assert (eq (find-coding-system 'mule-tests-alias-mac)
229               (find-coding-system 'iso-8859-7-mac)))
230
231   (define-coding-system-alias 'nested-mule-tests-alias 'mule-tests-alias)
232   (Assert (coding-system-alias-p 'nested-mule-tests-alias))
233   (Assert (not (coding-system-canonical-name-p 'nested-mule-tests-alias)))
234   (Assert (eq (get-coding-system 'iso-8859-7)
235               (get-coding-system 'nested-mule-tests-alias)))
236   (Assert (eq (coding-system-aliasee 'nested-mule-tests-alias) 'mule-tests-alias))
237   (Assert (eq 'mule-tests-alias (coding-system-aliasee 'nested-mule-tests-alias)))
238   (Assert (coding-system-alias-p 'nested-mule-tests-alias-unix))
239   (Assert (coding-system-alias-p 'nested-mule-tests-alias-dos))
240   (Assert (coding-system-alias-p 'nested-mule-tests-alias-mac))
241   (Assert (eq (find-coding-system 'nested-mule-tests-alias-unix)
242               (find-coding-system 'iso-8859-7-unix)))
243
244   (Check-Error-Message
245    error "Attempt to create a coding system alias loop"
246    (define-coding-system-alias 'mule-tests-alias 'nested-mule-tests-alias))
247   (Check-Error-Message
248    error "No such coding system"
249    (define-coding-system-alias 'no-such-coding-system 'no-such-coding-system))
250   (Check-Error-Message
251    error "Attempt to create a coding system alias loop"
252    (define-coding-system-alias 'mule-tests-alias 'mule-tests-alias))
253
254   ;; Test dangling alias deletion
255   (define-coding-system-alias 'mule-tests-alias nil)
256   (Assert (not (coding-system-alias-p 'mule-tests-alias)))
257   (Assert (not (coding-system-alias-p 'mule-tests-alias-unix)))
258   (Assert (not (coding-system-alias-p 'nested-mule-tests-alias)))
259   (Assert (not (coding-system-alias-p 'nested-mule-tests-alias-dos)))
260
261   ;;---------------------------------------------------------------
262   ;; Test strings waxing and waning across the 8k BIG_STRING limit (see alloc.c)
263   ;;---------------------------------------------------------------
264   (defun charset-char-string (charset)
265     (let (lo hi string n (gc-cons-threshold most-positive-fixnum))
266       (if (= (charset-chars charset) 94)
267           (setq lo 33 hi 126)
268         (setq lo 32 hi 127))
269       (if (= (charset-dimension charset) 1)
270           (progn
271             (setq string (make-string (1+ (- hi lo)) ??))
272             (setq n 0)
273             (loop for j from lo to hi do
274               (progn
275                 (aset string n (make-char charset j))
276                 (incf n)))
277             (garbage-collect)
278             string)
279         (progn
280           (setq string (make-string (* (1+ (- hi lo)) (1+ (- hi lo))) ??))
281           (setq n 0)
282           (loop for j from lo to hi do
283             (loop for k from lo to hi do
284               (progn
285                 (aset string n (make-char charset j k))
286                 (incf n))))
287           (garbage-collect)
288           string))))
289
290   ;; The following two used to crash xemacs!
291   (Assert (charset-char-string 'japanese-jisx0208))
292   (aset (make-string 9003 ??) 1 (make-char 'latin-iso8859-1 77))
293
294   (let ((greek-string (charset-char-string 'greek-iso8859-7))
295         (string (make-string (* 96 60) ??)))
296     (loop for j from 0 below (length string) do
297       (aset string j (aref greek-string (mod j 96))))
298     (loop for k in '(0 1 58 59) do
299       (Assert (equal (substring string (* 96 k) (* 96 (1+ k))) greek-string))))
300
301   (let ((greek-string (charset-char-string 'greek-iso8859-7))
302         (string (make-string (* 96 60) ??)))
303    (loop for j from (1- (length string)) downto 0 do
304      (aset string j (aref greek-string (mod j 96))))
305    (loop for k in '(0 1 58 59) do
306      (Assert (equal (substring string (* 96 k) (* 96 (1+ k))) greek-string))))
307
308   (let ((ascii-string (charset-char-string 'ascii))
309         (string (make-string (* 94 60) (make-char 'greek-iso8859-7 57))))
310    (loop for j from 0 below (length string) do
311       (aset string j (aref ascii-string (mod j 94))))
312     (loop for k in '(0 1 58 59) do
313       (Assert (equal (substring string (* 94 k) (+ 94 (* 94 k))) ascii-string))))
314
315   (let ((ascii-string (charset-char-string 'ascii))
316         (string (make-string (* 94 60) (make-char 'greek-iso8859-7 57))))
317     (loop for j from (1- (length string)) downto 0 do
318       (aset string j (aref ascii-string (mod j 94))))
319     (loop for k in '(0 1 58 59) do
320       (Assert (equal (substring string (* 94 k) (* 94 (1+ k))) ascii-string))))
321
322   ;;---------------------------------------------------------------
323   ;; Test file-system character conversion (and, en passant, file ops)
324   ;;---------------------------------------------------------------
325   (let* ((scaron (make-char 'latin-iso8859-2 57))
326          (latin2-string (make-string 4 scaron))
327          (prefix (concat (file-name-as-directory
328                           (file-truename (temp-directory)))
329                          latin2-string))
330          (name1 (make-temp-name prefix))
331          (name2 (make-temp-name prefix))
332          (file-name-coding-system 'iso-8859-2))
333     (Silence-Message
334       (Assert (not (equal name1 name2)))
335       ;; Kludge to handle Mac OS X which groks only UTF-8.
336       (cond ((eq system-type 'darwin)
337              (Check-Error-Message 'file-error "Opening output file"
338               (write-region (point-min) (point-max) name1))
339              (require 'un-define)
340              (setq file-name-coding-system 'utf-8)))
341       (Assert (not (file-exists-p name1)))
342       (write-region (point-min) (point-max) name1)
343       (Assert (file-exists-p name1))
344       (when (fboundp 'make-symbolic-link)
345         (make-symbolic-link name1 name2)
346         (Assert (file-exists-p name2))
347         (Assert (equal (file-truename name2) name1))
348         (Assert (equal (file-truename name1) name1)))
349
350       (ignore-file-errors (delete-file name1) (delete-file name2))))
351   ;; Is a non-Latin-1 directory name preserved for call-process?
352   (when (and
353          ;; The bug should manifest itself on Windows, but I've no access
354          ;; to a Windows machine to verify that any test works.
355          (eq directory-sep-char ?/)
356          ;; file-name-coding-system on Darwin is _always_ UTF-8--the system
357          ;; enforces this--which coding system we don't have available in
358          ;; 21.4, outside of packages. I could jump through lots of hoops to
359          ;; have the test work anyway, but I'm not really into that right
360          ;; now.
361          (not (eq system-type 'darwin)))
362     (let ((process-coding-system-alist '((".*" . iso-8859-1)))
363           (file-name-coding-system 'iso-8859-1)
364           default-directory)
365       (make-directory (concat (temp-directory) "/\260\354"))
366       (setq file-name-coding-system 'euc-jp)
367       (setq default-directory (format "%s/%c/" (temp-directory)
368                                       (make-char 'japanese-jisx0208 48 108)))
369       (Assert (equal (shell-command-to-string "pwd") 
370                      (format "%s/\260\354\n" (temp-directory))))
371       (delete-directory default-directory)))
372
373   ;; Add many more file operation tests here...
374
375   ;;---------------------------------------------------------------
376   ;; Test Unicode-related functions
377   ;;---------------------------------------------------------------
378   (let* ((scaron (make-char 'latin-iso8859-2 57)))
379     (loop for code in '(#x0000 #x2222 #x4444 #xffff) do
380       (progn
381         (set-ucs-char code scaron)
382         (Assert (eq scaron (ucs-char code)))))
383   
384     (Assert (eq nil (set-ucs-char #x1ffff scaron)))
385     (Check-Error wrong-type-argument (set-ucs-char -10000 scaron)))
386   
387   )