unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
blob 00bc3c83d0537240e78217188f429dfb2ac2a85a 5618 bytes (raw)
name: test/lisp/char-fold-tests.el 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
 
;;; char-fold-tests.el --- Tests for char-fold.el  -*- lexical-binding: t; -*-

;; Copyright (C) 2013-2017 Free Software Foundation, Inc.

;; 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)
(require 'char-fold)

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

(defun char-fold--test-search-with-contents (contents string)
  (with-temp-buffer
    (insert contents)
    (goto-char (point-min))
    (should (search-forward-regexp (char-fold-to-regexp string) nil 'noerror))
    (goto-char (point-min))
    (should (char-fold-search-forward string nil 'noerror))
    (should (char-fold-search-backward string nil 'noerror))))

\f
(ert-deftest char-fold--test-consistency ()
  (dotimes (n 30)
    (let ((w (char-fold--random-word n)))
      ;; A folded string should always match the original string.
      (char-fold--test-search-with-contents w w))))

(ert-deftest char-fold--test-lax-whitespace ()
  (dotimes (n 40)
    (let ((w1 (char-fold--random-word n))
          (w2 (char-fold--random-word n))
          (search-spaces-regexp "\\s-+"))
      (char-fold--test-search-with-contents
       (concat w1 "\s\n\s\t\f\t\n\r\t" w2)
       (concat w1 " " w2))
      (char-fold--test-search-with-contents
       (concat w1 "\s\n\s\t\f\t\n\r\t" w2)
       (concat w1 (make-string 10 ?\s) w2)))))

(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)))))))

(ert-deftest char-fold--test-some-defaults ()
  (dolist (it '(("ffl" . "ffl") ("ffi" . "ffi")
                ("fi" . "fi") ("ff" . "ff")
                ("ä" . "ä")))
    (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")
    (aset multi ?a '(("1" . "99")
                     ("2" . "88")
                     ("12" . "77")))
    (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")
    (aset multi ?1 '(("2" . "yy")))
    (char-fold--test-match-exactly "a1" "xx44" "99")
    (char-fold--test-match-exactly "a12" "77" "xx442" "992")
    ;; Support for this case is disabled.  See function definition or:
    ;; https://lists.gnu.org/archive/html/emacs-devel/2015-11/msg02562.html
    ;; (char-fold--test-match-exactly "a12" "xxyy")
    ))

(ert-deftest char-fold--speed-test ()
  (dolist (string (append '("tty-set-up-initial-frame-face"
                            "tty-set-up-initial-frame-face-frame-faceframe-faceframe-faceframe-face")
                          (mapcar #'char-fold--random-word '(10 50 100
                                                                     50 100))))
    (message "Testing %s" string)
    ;; Make sure we didn't just fallback on the trivial search.
    (should-not (string= (regexp-quote string)
                         (char-fold-to-regexp string)))
    (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
        ;; engine took a long time to realize that it doesn't match.
        (should-not (char-fold-search-forward (concat string "c") nil 'noerror))
        ;; Ensure it took less than a second.
        (should (< (- (time-to-seconds (current-time))
                      time)
                   1))))))

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

debug log:

solving 00bc3c83d05 ...
found 00bc3c83d05 in https://git.savannah.gnu.org/cgit/emacs.git

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).