XEmacs 21.2.27 "Hera".
[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 ;; Maintainer: Hrvoje Niksic <hniksic@xemacs.org>
5 ;; Created: 1999
6 ;; Keywords: tests
7
8 ;; This file is part of XEmacs.
9
10 ;; XEmacs is free software; you can redistribute it and/or modify it
11 ;; under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; XEmacs is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 ;; General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with XEmacs; see the file COPYING.  If not, write to the Free
22 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23 ;; 02111-1307, USA.
24
25 ;;; Synched up with: Not in FSF.
26
27 ;;; Commentary:
28
29 ;; Test some Mule functionality (most of these remain to be written) .
30 ;; See test-harness.el for instructions on how to run these tests.
31
32 ;; This file will be (read)ed by a non-mule XEmacs, so don't use
33 ;; literal non-Latin1 characters.  Use (make-char) instead.
34
35 ;;-----------------------------------------------------------------
36 ;; Test whether all legal chars may be safely inserted to a buffer.
37 ;;-----------------------------------------------------------------
38
39 (defun test-chars (&optional for-test-harness)
40   "Insert all characters in a buffer, to see if XEmacs will crash.
41 This is done by creating a string with all the legal characters
42 in [0, 2^19) range, inserting it into the buffer, and checking
43 that the buffer's contents are equivalent to the string.
44
45 If FOR-TEST-HARNESS is specified, a temporary buffer is used, and
46 the Assert macro checks for correctness."
47   (let ((max (expt 2 (if (featurep 'mule) 19 8)))
48         (list nil)
49         (i 0))
50     (while (< i max)
51       (and (not for-test-harness)
52            (zerop (% i 1000))
53            (message "%d" i))
54       (and (int-char i)
55            ;; Don't aset to a string directly because random string
56            ;; access is O(n) under Mule.
57            (setq list (cons (int-char i) list)))
58       (setq i (1+ i)))
59     (let ((string (apply #'string (nreverse list))))
60       (if for-test-harness
61           ;; For use with test-harness, use Assert and a temporary
62           ;; buffer.
63           (with-temp-buffer
64             (insert string)
65             (Assert (equal (buffer-string) string)))
66         ;; For use without test harness: use a normal buffer, so that
67         ;; you can also test whether redisplay works.
68         (switch-to-buffer (get-buffer-create "test"))
69         (erase-buffer)
70         (buffer-disable-undo)
71         (insert string)
72         (assert (equal (buffer-string) string))))))
73
74 ;; It would be really *really* nice if test-harness allowed a way to
75 ;; run a test in byte-compiled mode only.  It's tedious to have
76 ;; time-consuming tests like this one run twice, once interpreted and
77 ;; once compiled, for no good reason.
78 (test-chars t)
79
80 ;;-----------------------------------------------------------------
81 ;; Test string modification functions that modify the length of a char.
82 ;;-----------------------------------------------------------------
83
84 (when (featurep 'mule)
85   ;; Test fillarray
86   (macrolet
87       ((fillarray-test
88         (charset1 charset2)
89         (let ((char1 (make-char charset1 69))
90               (char2 (make-char charset2 69)))
91           `(let ((string (make-string 1000 ,char1)))
92              (fillarray string ,char2)
93              (Assert (eq (aref string 0) ,char2))
94              (Assert (eq (aref string (1- (length string))) ,char2))
95              (Assert (eq (length string) 1000))))))
96     (fillarray-test ascii latin-iso8859-1)
97     (fillarray-test ascii latin-iso8859-2)
98     (fillarray-test latin-iso8859-1 ascii)
99     (fillarray-test latin-iso8859-2 ascii))
100
101   ;; Test aset
102   (let ((string (string (make-char 'ascii 69) (make-char 'latin-iso8859-2 69))))
103     (aset string 0 (make-char 'latin-iso8859-2 42))
104     (Assert (eq (aref string 1) (make-char 'latin-iso8859-2 69))))
105
106   ;; Test strings waxing and waning across the 8k BIG_STRING limit (see alloc.c)
107   (defun charset-char-string (charset)
108     (let (lo hi string n)
109       (if (= (charset-chars charset) 94)
110           (setq lo 33 hi 126)
111         (setq lo 32 hi 127))
112       (if (= (charset-dimension charset) 1)
113           (progn
114             (setq string (make-string (1+ (- hi lo)) ??))
115             (setq n 0)
116             (loop for j from lo to hi do
117               (progn
118                 (aset string n (make-char charset j))
119                 (incf n)))
120             string)
121         (progn
122           (setq string (make-string (* (1+ (- hi lo)) (1+ (- hi lo))) ??))
123           (setq n 0)
124           (loop for j from lo to hi do
125             (loop for k from lo to hi do
126               (progn
127                 (aset string n (make-char charset j k))
128                 (incf n))))
129           string))))
130
131   ;; The following two used to crash xemacs!
132   (Assert (charset-char-string 'japanese-jisx0208))
133   (aset (make-string 9003 ??) 1 (make-char 'latin-iso8859-1 77))
134
135   (let ((greek-string (charset-char-string 'greek-iso8859-7))
136         (string (make-string (* 96 60) ??)))
137     (loop for j from 0 below (length string) do
138       (aset string j (aref greek-string (mod j 96))))
139     (loop for k in '(0 1 58 59) do
140       (Assert (equal (substring string (* 96 k) (* 96 (1+ k))) greek-string))))
141
142   (let ((greek-string (charset-char-string 'greek-iso8859-7))
143         (string (make-string (* 96 60) ??)))
144    (loop for j from (1- (length string)) downto 0 do
145      (aset string j (aref greek-string (mod j 96))))
146    (loop for k in '(0 1 58 59) do
147      (Assert (equal (substring string (* 96 k) (* 96 (1+ k))) greek-string))))
148
149   (let ((ascii-string (charset-char-string 'ascii))
150         (string (make-string (* 94 60) (make-char 'greek-iso8859-7 57))))
151    (loop for j from 0 below (length string) do
152       (aset string j (aref ascii-string (mod j 94))))
153     (loop for k in '(0 1 58 59) do
154       (Assert (equal (substring string (* 94 k) (+ 94 (* 94 k))) ascii-string))))
155
156   (let ((ascii-string (charset-char-string 'ascii))
157         (string (make-string (* 94 60) (make-char 'greek-iso8859-7 57))))
158     (loop for j from (1- (length string)) downto 0 do
159       (aset string j (aref ascii-string (mod j 94))))
160     (loop for k in '(0 1 58 59) do
161       (Assert (equal (substring string (* 94 k) (* 94 (1+ k))) ascii-string))))
162
163   )