lisp/obsolete/*tls.el: Note when obsolescence was decided
[emacs.git] / test / lisp / char-fold-tests.el
blob364975317f20d5946880765577a4f139b5f7e17e
1 ;;; char-fold-tests.el --- Tests for char-fold.el -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2013-2018 Free Software Foundation, Inc.
5 ;; Author: Artur Malabarba <bruce.connor.am@gmail.com>
7 ;; This program is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation, either version 3 of the License, or
10 ;; (at your option) any later version.
12 ;; This program is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
20 ;;; Code:
22 (require 'ert)
23 (require 'char-fold)
25 (defun char-fold--random-word (n)
26 (mapconcat (lambda (_) (string (+ 9 (random 117))))
27 (make-list n nil) ""))
29 (defun char-fold--test-search-with-contents (contents string)
30 (with-temp-buffer
31 (insert contents)
32 (goto-char (point-min))
33 (should (search-forward-regexp (char-fold-to-regexp string) nil 'noerror))
34 (goto-char (point-min))
35 (should (char-fold-search-forward string nil 'noerror))
36 (should (char-fold-search-backward string nil 'noerror))))
39 (ert-deftest char-fold--test-consistency ()
40 (dotimes (n 30)
41 (let ((w (char-fold--random-word n)))
42 ;; A folded string should always match the original string.
43 (char-fold--test-search-with-contents w w))))
45 (ert-deftest char-fold--test-lax-whitespace ()
46 (dotimes (n 40)
47 (let ((w1 (char-fold--random-word n))
48 (w2 (char-fold--random-word n))
49 (search-spaces-regexp "\\s-+"))
50 (char-fold--test-search-with-contents
51 (concat w1 "\s\n\s\t\f\t\n\r\t" w2)
52 (concat w1 " " w2))
53 (char-fold--test-search-with-contents
54 (concat w1 "\s\n\s\t\f\t\n\r\t" w2)
55 (concat w1 (make-string 10 ?\s) w2)))))
57 (defun char-fold--ascii-upcase (string)
58 "Like `upcase' but acts on ASCII characters only."
59 (replace-regexp-in-string "[a-z]+" 'upcase string))
61 (defun char-fold--ascii-downcase (string)
62 "Like `downcase' but acts on ASCII characters only."
63 (replace-regexp-in-string "[a-z]+" 'downcase string))
65 (defun char-fold--test-match-exactly (string &rest strings-to-match)
66 (let ((re (concat "\\`" (char-fold-to-regexp string) "\\'")))
67 (dolist (it strings-to-match)
68 (should (string-match re it)))
69 ;; Case folding
70 (let ((case-fold-search t))
71 (dolist (it strings-to-match)
72 (should (string-match (char-fold--ascii-upcase re) (downcase it)))
73 (should (string-match (char-fold--ascii-downcase re) (upcase it)))))))
75 (ert-deftest char-fold--test-some-defaults ()
76 (dolist (it '(("ffl" . "ffl") ("ffi" . "ffi")
77 ("fi" . "fi") ("ff" . "ff")
78 ("ä" . "ä")))
79 (char-fold--test-search-with-contents (cdr it) (car it))
80 (let ((multi (char-table-extra-slot char-fold-table 0))
81 (char-fold-table (make-char-table 'char-fold-table)))
82 (set-char-table-extra-slot char-fold-table 0 multi)
83 (char-fold--test-match-exactly (car it) (cdr it)))))
85 (ert-deftest char-fold--test-fold-to-regexp ()
86 (let ((char-fold-table (make-char-table 'char-fold-table))
87 (multi (make-char-table 'char-fold-table)))
88 (set-char-table-extra-slot char-fold-table 0 multi)
89 (aset char-fold-table ?a "xx")
90 (aset char-fold-table ?1 "44")
91 (aset char-fold-table ?\s "-!-")
92 (char-fold--test-match-exactly "a1a1" "xx44xx44")
93 (char-fold--test-match-exactly "a1 a 1" "xx44-!--!-xx-!-44")
94 (aset multi ?a '(("1" . "99")
95 ("2" . "88")
96 ("12" . "77")))
97 (char-fold--test-match-exactly "a" "xx")
98 (char-fold--test-match-exactly "a1" "xx44" "99")
99 (char-fold--test-match-exactly "a12" "77" "xx442" "992")
100 (char-fold--test-match-exactly "a2" "88")
101 (aset multi ?1 '(("2" . "yy")))
102 (char-fold--test-match-exactly "a1" "xx44" "99")
103 (char-fold--test-match-exactly "a12" "77" "xx442" "992")
104 ;; Support for this case is disabled. See function definition or:
105 ;; https://lists.gnu.org/r/emacs-devel/2015-11/msg02562.html
106 ;; (char-fold--test-match-exactly "a12" "xxyy")
109 (ert-deftest char-fold--speed-test ()
110 (dolist (string (append '("tty-set-up-initial-frame-face"
111 "tty-set-up-initial-frame-face-frame-faceframe-faceframe-faceframe-face")
112 (mapcar #'char-fold--random-word '(10 50 100
113 50 100))))
114 (message "Testing %s" string)
115 ;; Make sure we didn't just fallback on the trivial search.
116 (should-not (string= (regexp-quote string)
117 (char-fold-to-regexp string)))
118 (with-temp-buffer
119 (save-excursion (insert string))
120 (let ((time (time-to-seconds)))
121 ;; Our initial implementation of case-folding in char-folding
122 ;; created a lot of redundant paths in the regexp. Because of
123 ;; that, if a really long string "almost" matches, the regexp
124 ;; engine took a long time to realize that it doesn't match.
125 (should-not (char-fold-search-forward (concat string "c") nil 'noerror))
126 ;; Ensure it took less than a second.
127 (should (< (- (time-to-seconds) time) 1))))))
129 (provide 'char-fold-tests)
130 ;;; char-fold-tests.el ends here