2016-05-17 23:55:38 +03:00
|
|
|
|
;;; char-fold-tests.el --- Tests for char-fold.el -*- lexical-binding: t; -*-
|
2015-10-30 12:18:46 +00:00
|
|
|
|
|
2016-01-01 01:16:19 -08:00
|
|
|
|
;; Copyright (C) 2013-2016 Free Software Foundation, Inc.
|
2015-10-30 12:18:46 +00:00
|
|
|
|
|
|
|
|
|
;; 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
|
|
|
|
|
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
|
|
|
|
(require 'ert)
|
2016-05-17 23:55:38 +03:00
|
|
|
|
(require 'char-fold)
|
2015-10-30 12:18:46 +00:00
|
|
|
|
|
2016-05-17 23:55:38 +03:00
|
|
|
|
(defun char-fold--random-word (n)
|
2015-10-30 12:18:46 +00:00
|
|
|
|
(mapconcat (lambda (_) (string (+ 9 (random 117))))
|
|
|
|
|
(make-list n nil) ""))
|
|
|
|
|
|
2016-05-17 23:55:38 +03:00
|
|
|
|
(defun char-fold--test-search-with-contents (contents string)
|
2015-10-30 12:18:46 +00:00
|
|
|
|
(with-temp-buffer
|
|
|
|
|
(insert contents)
|
|
|
|
|
(goto-char (point-min))
|
2016-05-17 23:55:38 +03:00
|
|
|
|
(should (search-forward-regexp (char-fold-to-regexp string) nil 'noerror))
|
2015-10-30 12:18:46 +00:00
|
|
|
|
(goto-char (point-min))
|
2016-05-17 23:55:38 +03:00
|
|
|
|
(should (char-fold-search-forward string nil 'noerror))
|
|
|
|
|
(should (char-fold-search-backward string nil 'noerror))))
|
2015-10-30 12:18:46 +00:00
|
|
|
|
|
|
|
|
|
|
2016-05-17 23:55:38 +03:00
|
|
|
|
(ert-deftest char-fold--test-consistency ()
|
2015-11-28 19:56:19 +00:00
|
|
|
|
(dotimes (n 30)
|
2016-05-17 23:55:38 +03:00
|
|
|
|
(let ((w (char-fold--random-word n)))
|
2015-10-30 12:18:46 +00:00
|
|
|
|
;; A folded string should always match the original string.
|
2016-05-17 23:55:38 +03:00
|
|
|
|
(char-fold--test-search-with-contents w w))))
|
2015-10-30 12:18:46 +00:00
|
|
|
|
|
2016-05-17 23:55:38 +03:00
|
|
|
|
(ert-deftest char-fold--test-lax-whitespace ()
|
2015-11-28 19:56:19 +00:00
|
|
|
|
(dotimes (n 40)
|
2016-05-17 23:55:38 +03:00
|
|
|
|
(let ((w1 (char-fold--random-word n))
|
|
|
|
|
(w2 (char-fold--random-word n))
|
2015-10-30 12:18:46 +00:00
|
|
|
|
(search-spaces-regexp "\\s-+"))
|
2016-05-17 23:55:38 +03:00
|
|
|
|
(char-fold--test-search-with-contents
|
2015-10-30 12:18:46 +00:00
|
|
|
|
(concat w1 "\s\n\s\t\f\t\n\r\t" w2)
|
|
|
|
|
(concat w1 " " w2))
|
2016-05-17 23:55:38 +03:00
|
|
|
|
(char-fold--test-search-with-contents
|
2015-10-30 12:18:46 +00:00
|
|
|
|
(concat w1 "\s\n\s\t\f\t\n\r\t" w2)
|
2015-11-28 12:15:17 +00:00
|
|
|
|
(concat w1 (make-string 10 ?\s) w2)))))
|
|
|
|
|
|
2016-05-17 23:55:38 +03:00
|
|
|
|
(defun char-fold--test-match-exactly (string &rest strings-to-match)
|
|
|
|
|
(let ((re (concat "\\`" (char-fold-to-regexp string) "\\'")))
|
2015-11-28 12:15:17 +00:00
|
|
|
|
(dolist (it strings-to-match)
|
2015-11-28 15:31:43 +00:00
|
|
|
|
(should (string-match re it)))
|
|
|
|
|
;; Case folding
|
|
|
|
|
(let ((case-fold-search t))
|
|
|
|
|
(dolist (it strings-to-match)
|
|
|
|
|
(should (string-match (upcase re) (downcase it)))
|
|
|
|
|
(should (string-match (downcase re) (upcase it)))))))
|
2015-11-28 12:15:17 +00:00
|
|
|
|
|
2016-05-17 23:55:38 +03:00
|
|
|
|
(ert-deftest char-fold--test-some-defaults ()
|
2015-11-28 12:15:17 +00:00
|
|
|
|
(dolist (it '(("ffl" . "ffl") ("ffi" . "ffi")
|
|
|
|
|
("fi" . "fi") ("ff" . "ff")
|
|
|
|
|
("ä" . "ä")))
|
2016-05-17 23:55:38 +03:00
|
|
|
|
(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)))))
|
|
|
|
|
|
|
|
|
|
(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")
|
2015-11-28 12:15:17 +00:00
|
|
|
|
(aset multi ?a '(("1" . "99")
|
|
|
|
|
("2" . "88")
|
|
|
|
|
("12" . "77")))
|
2016-05-17 23:55:38 +03:00
|
|
|
|
(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")
|
2015-11-28 12:15:17 +00:00
|
|
|
|
(aset multi ?1 '(("2" . "yy")))
|
2016-05-17 23:55:38 +03:00
|
|
|
|
(char-fold--test-match-exactly "a1" "xx44" "99")
|
|
|
|
|
(char-fold--test-match-exactly "a12" "77" "xx442" "992")
|
2015-12-01 13:52:50 +00:00
|
|
|
|
;; Support for this case is disabled. See function definition or:
|
|
|
|
|
;; https://lists.gnu.org/archive/html/emacs-devel/2015-11/msg02562.html
|
2016-05-17 23:55:38 +03:00
|
|
|
|
;; (char-fold--test-match-exactly "a12" "xxyy")
|
2015-12-01 13:52:50 +00:00
|
|
|
|
))
|
2015-11-28 12:15:17 +00:00
|
|
|
|
|
2016-05-17 23:55:38 +03:00
|
|
|
|
(ert-deftest char-fold--speed-test ()
|
2015-12-04 15:12:10 +00:00
|
|
|
|
(dolist (string (append '("tty-set-up-initial-frame-face"
|
|
|
|
|
"tty-set-up-initial-frame-face-frame-faceframe-faceframe-faceframe-face")
|
2016-05-17 23:55:38 +03:00
|
|
|
|
(mapcar #'char-fold--random-word '(10 50 100
|
2015-12-04 15:12:10 +00:00
|
|
|
|
50 100))))
|
|
|
|
|
(message "Testing %s" string)
|
|
|
|
|
;; Make sure we didn't just fallback on the trivial search.
|
|
|
|
|
(should-not (string= (regexp-quote string)
|
2016-05-17 23:55:38 +03:00
|
|
|
|
(char-fold-to-regexp string)))
|
2015-12-04 15:12:10 +00:00
|
|
|
|
(with-temp-buffer
|
|
|
|
|
(save-excursion (insert string))
|
|
|
|
|
(let ((time (time-to-seconds (current-time))))
|
|
|
|
|
;; 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
|
2015-12-07 08:35:53 -08:00
|
|
|
|
;; engine took a long time to realize that it doesn't match.
|
2016-05-17 23:55:38 +03:00
|
|
|
|
(should-not (char-fold-search-forward (concat string "c") nil 'noerror))
|
2015-12-04 15:12:10 +00:00
|
|
|
|
;; Ensure it took less than a second.
|
|
|
|
|
(should (< (- (time-to-seconds (current-time))
|
|
|
|
|
time)
|
|
|
|
|
1))))))
|
2015-11-28 10:32:46 +00:00
|
|
|
|
|
2016-05-17 23:55:38 +03:00
|
|
|
|
(provide 'char-fold-tests)
|
|
|
|
|
;;; char-fold-tests.el ends here
|