character-fold-tests.el 5.4 KB

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