XEmacs 21.4.11 "Native Windows TTY Support".
[chise/xemacs-chise.git.1] / tests / automated / regexp-tests.el
1 ;;; -*- coding: iso-8859-1 -*-
2
3 ;; Copyright (C) 2000 Free Software Foundation, Inc.
4
5 ;; Author: Yoshiki Hayashi  <yoshiki@xemacs.org>
6 ;; Maintainer: Yoshiki Hayashi  <yoshiki@xemacs.org>
7 ;; Created: 2000
8 ;; Keywords: tests
9
10 ;; This file is part of XEmacs.
11
12 ;; XEmacs is free software; you can redistribute it and/or modify it
13 ;; under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; XEmacs is distributed in the hope that it will be useful, but
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20 ;; General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with XEmacs; see the file COPYING.  If not, write to the Free
24 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25 ;; 02111-1307, USA.
26
27 ;;; Synched up with: Not in FSF.
28
29 ;;; Commentary:
30
31 ;; Test regular expression.
32
33 (Check-Error-Message error "Trailing backslash"
34                      (string-match "\\" "a"))
35 (Check-Error-Message error "Invalid preceding regular expression"
36                      (string-match "a++" "a"))
37 (Check-Error-Message error "Invalid preceding regular expression"
38                      (string-match "a**" "a"))
39 (Check-Error-Message error "Invalid preceding regular expression"
40                      (string-match "a???" "a"))
41 (Check-Error-Message error "Unmatched \\[ or \\[^"
42                      (string-match "[" "a"))
43 (Check-Error-Message error "Unmatched \\[ or \\[^"
44                      (string-match "[abc" "a"))
45 (Check-Error-Message error "Unmatched ) or \\\\)"
46                      (string-match "\\)" "a"))
47 (Check-Error-Message error "Invalid regular expression"
48                      (string-match "\\(?.\\)" "a"))
49 (Check-Error-Message error "Unmatched \\\\{"
50                      (string-match "a\\{" "a"))
51 (Check-Error-Message error "Invalid content of \\\\{\\\\}"
52                      (string-match "a\\{a\\}" "a"))
53
54 ;; exactn
55
56 ;; string-match
57 (with-temp-buffer
58   ;; case-insensitive
59   (Assert (string-match "ä" "ä"))
60   (Assert (string-match "ä" "Ä"))
61   (Assert (string-match "Ä" "Ä"))
62   (Assert (string-match "Ä" "ä"))
63   ;; case-sensitive
64   (setq case-fold-search nil)
65   (Assert (string-match "ä" "ä"))
66   (Assert (not (string-match "ä" "Ä")))
67   (Assert (string-match "Ä" "Ä"))
68   (Assert (not (string-match "Ä" "ä"))))
69
70 ;; looking-at
71 (with-temp-buffer
72   (insert "äÄ")
73   ;; case-insensitive
74   (goto-char (point-min))
75   (Assert (looking-at "ä"))
76   (Assert (looking-at "Ä"))
77   (forward-char)
78   (Assert (looking-at "ä"))
79   (Assert (looking-at "Ä"))
80   ;; case-sensitive
81   (setq case-fold-search nil)
82   (goto-char (point-min))
83   (Assert (looking-at "ä"))
84   (Assert (not (looking-at "Ä")))
85   (forward-char)
86   (Assert (not (looking-at "ä")))
87   (Assert (looking-at "Ä")))
88
89 ;; re-search-forward and re-search-backward
90 (with-temp-buffer
91   (insert "äÄ")
92   ;; case insensitive
93   ;; forward
94   (goto-char (point-min))
95   ;; Avoid trivial regexp.
96   (Assert (eq 2 (re-search-forward "ä\\|a" nil t)))
97   (goto-char (point-min))
98   (Assert (eq 2 (re-search-forward "Ä\\|a" nil t)))
99   (goto-char (1+ (point-min)))
100   (Assert (eq 3 (re-search-forward "ä\\|a" nil t)))
101   (goto-char (1+ (point-min)))
102   (Assert (eq 3 (re-search-forward "Ä\\|a" nil t)))
103   ;; backward
104   (goto-char (point-max))
105   (Assert (eq 2 (re-search-backward "ä\\|a" nil t)))
106   (goto-char (point-max))
107   (Assert (eq 2 (re-search-backward "Ä\\|a" nil t)))
108   (goto-char (1- (point-max)))
109   (Assert (eq 1 (re-search-backward "ä\\|a" nil t)))
110   (goto-char (1- (point-max)))
111   (Assert (eq 1 (re-search-backward "Ä\\|a" nil t)))
112   ;; case sensitive
113   (setq case-fold-search nil)
114   ;; forward
115   (goto-char (point-min))
116   (Assert (eq 2 (re-search-forward "ä\\|a" nil t)))
117   (goto-char (point-min))
118   (Assert (eq 3 (re-search-forward "Ä\\|a" nil t)))
119   (goto-char (1+ (point-min)))
120   (Assert (not (re-search-forward "ä\\|a" nil t)))
121   (goto-char (1+ (point-min)))
122   (Assert (eq 3 (re-search-forward "Ä\\|a" nil t)))
123   ;; backward
124   (goto-char (point-max))
125   (Assert (eq 1 (re-search-backward "ä\\|a" nil t)))
126   (goto-char (point-max))
127   (Assert (eq 2 (re-search-backward "Ä\\|a" nil t)))
128   (goto-char (1- (point-max)))
129   (Assert (eq 1 (re-search-backward "ä\\|a" nil t)))
130   (goto-char (1- (point-max)))
131   (Assert (not (re-search-backward "Ä\\|a" nil t))))
132
133 ;; duplicate
134 (with-temp-buffer
135   ;; case insensitive
136   (Assert (string-match "^\\(ä\\)\\1$" "ää"))
137   (Assert (string-match "^\\(ä\\)\\1$" "äÄ"))
138   (Assert (string-match "^\\(ä\\)\\1$" "ÄÄ"))
139   (Assert (string-match "^\\(ä\\)\\1$" "Ää"))
140   (Assert (string-match "^\\(Ä\\)\\1$" "ää"))
141   (Assert (string-match "^\\(Ä\\)\\1$" "äÄ"))
142   (Assert (string-match "^\\(Ä\\)\\1$" "ÄÄ"))
143   (Assert (string-match "^\\(Ä\\)\\1$" "Ää"))
144   ;; case sensitive
145   (setq case-fold-search nil)
146   (Assert (string-match "^\\(ä\\)\\1$" "ää"))
147   (Assert (not (string-match "^\\(ä\\)\\1$" "äÄ")))
148   (Assert (not (string-match "^\\(ä\\)\\1$" "ÄÄ")))
149   (Assert (not (string-match "^\\(ä\\)\\1$" "Ää")))
150   (Assert (not (string-match "^\\(Ä\\)\\1$" "ää")))
151   (Assert (not (string-match "^\\(Ä\\)\\1$" "äÄ")))
152   (Assert (string-match "^\\(Ä\\)\\1$" "ÄÄ"))
153   (Assert (not (string-match "^\\(Ä\\)\\1$" "Ää"))))
154
155 ;; charset, charset_not
156 ;; Not called because it takes too much time.
157 (defun test-regexp-charset-paranoid ()
158   (let ((i 0)
159         (max (expt 2 (if (featurep 'mule) 19 8)))
160         (range "[a-z]")
161         (range-not "[^a-z]")
162         char string)
163     (while (< i max)
164       (when (setq char (int-to-char i))
165         (setq string (char-to-string char))
166         (if (or (and (<= 65 i)
167                      (<= i 90))
168                 (and (<= 97 i)
169                      (<= i 122)))
170             (progn
171               (Assert (string-match range string))
172               (Assert (not (string-match range-not string))))
173           (Assert (not (string-match range string)))
174           (Assert (string-match range-not string))))
175       (setq i (1+ i)))))
176
177 ;; (test-regexp-charset-paranoid)
178
179 ;; charset_mule, charset_mule_not
180 ;; Not called because it takes too much time.
181 (defun test-regex-charset-mule-paranoid ()
182   (if (featurep 'mule)
183       (let ((i 0)
184             (max (expt 2 19))
185             (range (format "[%c-%c]"
186                            (make-char 'japanese-jisx0208 36 34)
187                            (make-char 'japanese-jisx0208 36 42)))
188             (range-not (format "[^%c-%c]"
189                                (make-char 'japanese-jisx0208 36 34)
190                                (make-char 'japanese-jisx0208 36 42)))
191             (min-int (char-to-int (make-char 'japanese-jisx0208 36 34)))
192             (max-int (char-to-int (make-char 'japanese-jisx0208 36 42)))
193             char string)
194         (while (< i max)
195           (when (setq char (int-to-char i))
196             (setq string (char-to-string char))
197             (if (and (<= min-int i)
198                      (<= i max-int))
199                 (progn
200                   (Assert (string-match range string))
201                   (Assert (not (string-match range-not string))))
202               (Assert (not (string-match range string)))
203               (Assert (string-match range-not string))))
204           (setq i (1+ i))))))
205
206 ;; (test-regex-charset-mule-paranoid)
207
208 ;; Test replace-match
209 (with-temp-buffer
210   (insert "This is a test buffer.")
211   (goto-char (point-min))
212   (search-forward "this is a test ")
213   (looking-at "Unmatchable text")
214   (replace-match "")
215   (Assert (looking-at "^buffer.$")))
216
217 ;; Test that trivial regexps reset unused registers
218 ;; Thanks to Martin Sternholm for the report.
219 ;; xemacs-beta <5blm6h2ki5.fsf@lister.roxen.com>
220 (with-temp-buffer
221   (insert "ab")
222   (goto-char (point-min))
223   (re-search-forward "\\(a\\)")
224   ;; test the whole-match data, too -- one try scotched that, too!
225   (Assert (string= (match-string 0) "a"))
226   (Assert (string= (match-string 1) "a"))
227   (re-search-forward "b")
228   (Assert (string= (match-string 0) "b"))
229   (Assert (not (match-string 1))))
230
231 ;; Test word boundaries
232 (Assert (= (string-match " \\<a" " a") 0))
233 (Assert (= (string-match "a\\> " "a ") 0))
234 (Assert (= (string-match " \\ba" " a") 0))
235 (Assert (= (string-match "a\\b " "a ") 0))
236 (Assert (= (string-match "\\ba" " a") 1))
237 (Assert (= (string-match "a\\b" "a ") 0))
238 ;; should work at target boundaries
239 (Assert (= (string-match "\\<a" "a") 0))
240 (Assert (= (string-match "a\\>" "a") 0))
241 (Assert (= (string-match "\\ba" "a") 0))
242 (Assert (= (string-match "a\\b" "a") 0))
243 ;; but not if the "word" would be on the null side of the boundary!
244 (Assert (not (string-match "\\<" "")))
245 (Assert (not (string-match "\\>" "")))
246 (Assert (not (string-match " \\<" " ")))
247 (Assert (not (string-match "\\> " " ")))
248 (Assert (not (string-match "a\\<" "a")))
249 (Assert (not (string-match "\\>a" "a")))
250 ;; Expect these to fail :-(
251 ;; Added Known-Bug 2002-09-09
252 (Assert (not (string-match "\\b" "")))
253 (Assert (not (string-match " \\b" " ")))
254 (Assert (not (string-match "\\b " " ")))
255
256 ;; Added 2002-12-27
257 (if (featurep 'mule)
258     ;; note: (int-to-char 65) => ?A
259     (let ((ch0 (make-char 'japanese-jisx0208 52 65))
260           (ch1 (make-char 'japanese-jisx0208 51 65)))
261       (Assert (not (string-match "A" (string ch0))))
262       (Assert (not (string-match "[A]" (string ch0))))
263       (Assert (eq (string-match "[^A]" (string ch0)) 0))
264       (Assert (not (string-match "@A" (string ?@ ch0))))
265       (Assert (not (string-match "@[A]" (string ?@ ch0))))
266       (Assert (eq (string-match "@[^A]" (string ?@ ch0)) 0))
267       (Assert (not (string-match "@?A" (string ?@ ch0))))
268       (Assert (not (string-match "A" (string ch1))))
269       (Assert (not (string-match "[A]" (string ch1))))
270       (Assert (eq (string-match "[^A]" (string ch1)) 0))
271       (Assert (not (string-match "@A" (string ?@ ch1))))
272       (Assert (not (string-match "@[A]" (string ?@ ch1))))
273       (Assert (eq (string-match "@[^A]" (string ?@ ch1)) 0))
274       (Assert (not (string-match "@?A" (string ?@ ch1))))))