char-fold-tests.el 8.31 KB
Newer Older
1
;;; char-fold-tests.el --- Tests for char-fold.el  -*- lexical-binding: t; -*-
2

Paul Eggert's avatar
Paul Eggert committed
3
;; Copyright (C) 2013-2019 Free Software Foundation, Inc.
4 5 6 7 8 9 10 11 12 13 14 15 16 17

;; Author: Artur Malabarba <bruce.connor.am@gmail.com>

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; 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/>.
19 20 21 22

;;; Code:

(require 'ert)
23
(require 'char-fold)
24

25
(defun char-fold--random-word (n)
26 27 28
  (mapconcat (lambda (_) (string (+ 9 (random 117))))
             (make-list n nil) ""))

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
(defun char-fold--ascii-upcase (string)
  "Like `upcase' but acts on ASCII characters only."
  (replace-regexp-in-string "[a-z]+" 'upcase string))

(defun char-fold--ascii-downcase (string)
  "Like `downcase' but acts on ASCII characters only."
  (replace-regexp-in-string "[a-z]+" 'downcase string))

(defun char-fold--test-match-exactly (string &rest strings-to-match)
  (let ((re (concat "\\`" (char-fold-to-regexp string) "\\'")))
    (dolist (it strings-to-match)
      (should (string-match re it)))
    ;; Case folding
    (let ((case-fold-search t))
      (dolist (it strings-to-match)
        (should (string-match (char-fold--ascii-upcase re) (downcase it)))
        (should (string-match (char-fold--ascii-downcase re) (upcase it)))))))

47 48 49 50 51 52 53 54 55 56
(defun char-fold--test-no-match-exactly (string &rest strings-to-match)
  (let ((re (concat "\\`" (char-fold-to-regexp string) "\\'")))
    (dolist (it strings-to-match)
      (should-not (string-match re it)))
    ;; Case folding
    (let ((case-fold-search t))
      (dolist (it strings-to-match)
        (should-not (string-match (char-fold--ascii-upcase re) (downcase it)))
        (should-not (string-match (char-fold--ascii-downcase re) (upcase it)))))))

57
(defun char-fold--test-search-with-contents (contents string)
58 59 60
  (with-temp-buffer
    (insert contents)
    (goto-char (point-min))
61
    (should (search-forward-regexp (char-fold-to-regexp string) nil 'noerror))
62
    (goto-char (point-min))
63 64
    (should (char-fold-search-forward string nil 'noerror))
    (should (char-fold-search-backward string nil 'noerror))))
65

66 67 68 69 70
(defun char-fold--permutation (strings)
  (mapcar (lambda (string)
            (cons string (remove string strings)))
          strings))

71

72
(ert-deftest char-fold--test-consistency ()
73
  (dotimes (n 30)
74
    (let ((w (char-fold--random-word n)))
75
      ;; A folded string should always match the original string.
76
      (char-fold--test-search-with-contents w w))))
77

78
(ert-deftest char-fold--test-lax-whitespace ()
79
  (dotimes (n 40)
80 81
    (let ((w1 (char-fold--random-word n))
          (w2 (char-fold--random-word n))
82
          (search-spaces-regexp "\\s-+"))
83
      (char-fold--test-search-with-contents
84 85
       (concat w1 "\s\n\s\t\f\t\n\r\t" w2)
       (concat w1 " " w2))
86
      (char-fold--test-search-with-contents
87
       (concat w1 "\s\n\s\t\f\t\n\r\t" w2)
88 89
       (concat w1 (make-string 10 ?\s) w2)))))

90
(ert-deftest char-fold--test-multi-defaults ()
91 92 93
  (dolist (it '(("ffl" . "ffl") ("ffi" . "ffi")
                ("fi" . "fi") ("ff" . "ff")
                ("ä" . "ä")))
94 95 96 97 98 99
    (char-fold--test-search-with-contents (cdr it) (car it))
    (let ((multi (char-table-extra-slot char-fold-table 0))
          (char-fold-table (make-char-table 'char-fold-table)))
      (set-char-table-extra-slot char-fold-table 0 multi)
      (char-fold--test-match-exactly (car it) (cdr it)))))

100 101 102 103 104 105 106 107
(ert-deftest char-fold--test-multi-lax ()
 (dolist (it '(("f" . "fi") ("f" . "ff")))
   (with-temp-buffer
     (insert (cdr it))
     (goto-char (point-min))
     (should (search-forward-regexp
              (char-fold-to-regexp (car it) 'lax) nil 'noerror)))))

108 109 110 111 112 113 114 115 116
(ert-deftest char-fold--test-fold-to-regexp ()
  (let ((char-fold-table (make-char-table 'char-fold-table))
        (multi  (make-char-table 'char-fold-table)))
    (set-char-table-extra-slot char-fold-table 0 multi)
    (aset char-fold-table ?a "xx")
    (aset char-fold-table ?1 "44")
    (aset char-fold-table ?\s "-!-")
    (char-fold--test-match-exactly "a1a1" "xx44xx44")
    (char-fold--test-match-exactly "a1  a 1" "xx44-!--!-xx-!-44")
117 118 119
    (aset multi ?a '(("1" . "99")
                     ("2" . "88")
                     ("12" . "77")))
120 121 122 123
    (char-fold--test-match-exactly "a" "xx")
    (char-fold--test-match-exactly "a1" "xx44" "99")
    (char-fold--test-match-exactly "a12" "77" "xx442" "992")
    (char-fold--test-match-exactly "a2" "88")
124
    (aset multi ?1 '(("2" . "yy")))
125 126
    (char-fold--test-match-exactly "a1" "xx44" "99")
    (char-fold--test-match-exactly "a12" "77" "xx442" "992")
127
    ;; Support for this case is disabled.  See function definition or:
128
    ;; https://lists.gnu.org/r/emacs-devel/2015-11/msg02562.html
129
    ;; (char-fold--test-match-exactly "a12" "xxyy")
130
    ))
131

132
(ert-deftest char-fold--speed-test ()
133 134
  (dolist (string (append '("tty-set-up-initial-frame-face"
                            "tty-set-up-initial-frame-face-frame-faceframe-faceframe-faceframe-face")
135
                          (mapcar #'char-fold--random-word '(10 50 100 50 100))))
136 137
    ;; Make sure we didn't just fallback on the trivial search.
    (should-not (string= (regexp-quote string)
138
                         (char-fold-to-regexp string)))
139 140
    (with-temp-buffer
      (save-excursion (insert string))
141
      (let ((time (time-to-seconds)))
142 143 144
        ;; Our initial implementation of case-folding in char-folding
        ;; created a lot of redundant paths in the regexp. Because of
        ;; that, if a really long string "almost" matches, the regexp
Paul Eggert's avatar
Paul Eggert committed
145
        ;; engine took a long time to realize that it doesn't match.
146
        (should-not (char-fold-search-forward (concat string "c") nil 'noerror))
147
        ;; Ensure it took less than a second.
148
        (should (< (- (time-to-seconds) time) 1))))))
149

150 151 152
(ert-deftest char-fold--test-without-customization ()
  (let* ((matches
          '(
153
            ("'" "’")
154 155 156 157
            ("e" "ℯ" "ḗ" "ë" "ë")
            ("ι"
             "ί" ;; 1 level decomposition
             "ί" ;; 2 level decomposition
158
             "ΐ" ;; 3 level decomposition
159
             )
160 161 162 163 164
            ("ß" "ss")
            ))
         (no-matches
          '(
            ("и" "й")
165 166
            )))
    (dolist (strings matches)
167 168 169
      (apply 'char-fold--test-match-exactly strings))
    (dolist (strings no-matches)
      (apply 'char-fold--test-no-match-exactly strings))))
170 171 172

(ert-deftest char-fold--test-with-customization ()
  :tags '(:expensive-test)
173
  ;; FIXME: move some language-specific settings to defaults
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
  (let ((char-fold-include
         (append char-fold-include
                 '(
                   (?o "ø")  ;; da no nb nn
                   (?l "ł")  ;; pl
                   (?æ "ae")
                   (? "->")
                   (? "=>")
                   )))
        (char-fold-exclude
         (append char-fold-exclude
                 '(
                   (?a "å")  ;; da no nb nn sv
                   (?a "ä")  ;; et fi sv
                   (?o "ö")  ;; et fi sv
                   (?n "ñ")  ;; es
                   )))
        (char-fold-symmetric t)
        (matches
         '(
           ("e" "ℯ" "ḗ" "ë" "ë")
           ("е" "ё" "ё")
           ("ι" "ί" "ί" "ΐ")
           ("ß" "ss")
           ("o" "ø")
           ("l" "ł")
           ("æ" "ae")
           ("→" "->")
           ("⇒" "=>")
           ))
        (no-matches
         '(
           ("a" "å")
           ("a" "ä")
           ("o" "ö")
           ("n" "ñ")
           ("и" "й")
           ))
        ;; Don't override global value by char-fold-update-table below
        char-fold-table)
    (char-fold-update-table)
215 216 217 218 219 220 221
    (dolist (strings matches)
      (dolist (permutation (char-fold--permutation strings))
        (apply 'char-fold--test-match-exactly permutation)))
    (dolist (strings no-matches)
      (dolist (permutation (char-fold--permutation strings))
        (apply 'char-fold--test-no-match-exactly permutation)))))

222 223
(provide 'char-fold-tests)
;;; char-fold-tests.el ends here