]> code.delx.au - gnu-emacs/blob - test/src/fns-tests.el
848589692ea25a2bc77dd59a2428e8a29d5fd446
[gnu-emacs] / test / src / fns-tests.el
1 ;;; fns-tests.el --- tests for src/fns.c
2
3 ;; Copyright (C) 2014-2016 Free Software Foundation, Inc.
4
5 ;; This file is part of GNU Emacs.
6
7 ;; This program is free software: you can redistribute it and/or
8 ;; modify it under the terms of the GNU General Public License as
9 ;; published by the Free Software Foundation, either version 3 of the
10 ;; License, or (at your option) any later version.
11 ;;
12 ;; This program is distributed in the hope that it will be useful, but
13 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 ;; General Public License for more details.
16 ;;
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with this program. If not, see `http://www.gnu.org/licenses/'.
19
20 ;;; Commentary:
21
22 ;;; Code:
23
24 (require 'cl-lib)
25 (eval-when-compile (require 'cl))
26
27 (ert-deftest fns-tests-reverse ()
28 (should-error (reverse))
29 (should-error (reverse 1))
30 (should-error (reverse (make-char-table 'foo)))
31 (should (equal [] (reverse [])))
32 (should (equal [0] (reverse [0])))
33 (should (equal [1 2 3 4] (reverse (reverse [1 2 3 4]))))
34 (should (equal '(a b c d) (reverse (reverse '(a b c d)))))
35 (should (equal "xyzzy" (reverse (reverse "xyzzy"))))
36 (should (equal "こんにちは / コンニチハ" (reverse (reverse "こんにちは / コンニチハ")))))
37
38 (ert-deftest fns-tests-nreverse ()
39 (should-error (nreverse))
40 (should-error (nreverse 1))
41 (should-error (nreverse (make-char-table 'foo)))
42 (should (equal (nreverse "xyzzy") "yzzyx"))
43 (let ((A []))
44 (nreverse A)
45 (should (equal A [])))
46 (let ((A [0]))
47 (nreverse A)
48 (should (equal A [0])))
49 (let ((A [1 2 3 4]))
50 (nreverse A)
51 (should (equal A [4 3 2 1])))
52 (let ((A [1 2 3 4]))
53 (nreverse A)
54 (nreverse A)
55 (should (equal A [1 2 3 4])))
56 (let* ((A [1 2 3 4])
57 (B (nreverse (nreverse A))))
58 (should (equal A B))))
59
60 (ert-deftest fns-tests-reverse-bool-vector ()
61 (let ((A (make-bool-vector 10 nil)))
62 (dotimes (i 5) (aset A i t))
63 (should (equal [nil nil nil nil nil t t t t t] (vconcat (reverse A))))
64 (should (equal A (reverse (reverse A))))))
65
66 (ert-deftest fns-tests-nreverse-bool-vector ()
67 (let ((A (make-bool-vector 10 nil)))
68 (dotimes (i 5) (aset A i t))
69 (nreverse A)
70 (should (equal [nil nil nil nil nil t t t t t] (vconcat A)))
71 (should (equal [t t t t t nil nil nil nil nil] (vconcat (nreverse A))))))
72
73 (ert-deftest fns-tests-compare-strings ()
74 (should-error (compare-strings))
75 (should-error (compare-strings "xyzzy" "xyzzy"))
76 (should (= (compare-strings "xyzzy" 0 10 "zyxxy" 0 5) -1))
77 (should-error (compare-strings "xyzzy" 0 5 "zyxxy" -1 2))
78 (should-error (compare-strings "xyzzy" 'foo nil "zyxxy" 0 1))
79 (should-error (compare-strings "xyzzy" 0 'foo "zyxxy" 2 3))
80 (should-error (compare-strings "xyzzy" 0 2 "zyxxy" 'foo 3))
81 (should-error (compare-strings "xyzzy" nil 3 "zyxxy" 4 'foo))
82 (should (eq (compare-strings "" nil nil "" nil nil) t))
83 (should (eq (compare-strings "" 0 0 "" 0 0) t))
84 (should (eq (compare-strings "test" nil nil "test" nil nil) t))
85 (should (eq (compare-strings "test" nil nil "test" nil nil t) t))
86 (should (eq (compare-strings "test" nil nil "test" nil nil nil) t))
87 (should (eq (compare-strings "Test" nil nil "test" nil nil t) t))
88 (should (= (compare-strings "Test" nil nil "test" nil nil) -1))
89 (should (= (compare-strings "Test" nil nil "test" nil nil) -1))
90 (should (= (compare-strings "test" nil nil "Test" nil nil) 1))
91 (should (= (compare-strings "foobaz" nil nil "barbaz" nil nil) 1))
92 (should (= (compare-strings "barbaz" nil nil "foobar" nil nil) -1))
93 (should (= (compare-strings "foobaz" nil nil "farbaz" nil nil) 2))
94 (should (= (compare-strings "farbaz" nil nil "foobar" nil nil) -2))
95 (should (eq (compare-strings "abcxyz" 0 2 "abcprq" 0 2) t))
96 (should (eq (compare-strings "abcxyz" 0 -3 "abcprq" 0 -3) t))
97 (should (= (compare-strings "abcxyz" 0 6 "abcprq" 0 6) 4))
98 (should (= (compare-strings "abcprq" 0 6 "abcxyz" 0 6) -4))
99 (should (eq (compare-strings "xyzzy" -3 4 "azza" -3 3) t))
100 (should (eq (compare-strings "こんにちはコンニチハ" nil nil "こんにちはコンニチハ" nil nil) t))
101 (should (= (compare-strings "んにちはコンニチハこ" nil nil "こんにちはコンニチハ" nil nil) 1))
102 (should (= (compare-strings "こんにちはコンニチハ" nil nil "んにちはコンニチハこ" nil nil) -1)))
103
104 (defun fns-tests--collate-enabled-p ()
105 "Check whether collation functions are enabled."
106 (and
107 ;; When there is no collation library, collation functions fall back
108 ;; to their lexicographic counterparts. We don't need to test then.
109 (not (ignore-errors (string-collate-equalp "" "" t)))
110 ;; We use a locale, which might not be installed. Check it.
111 (ignore-errors
112 (string-collate-equalp
113 "" "" (if (eq system-type 'windows-nt) "enu_USA" "en_US.UTF-8")))))
114
115 (ert-deftest fns-tests-collate-strings ()
116 (skip-unless (fns-tests--collate-enabled-p))
117
118 (should (string-collate-equalp "xyzzy" "xyzzy"))
119 (should-not (string-collate-equalp "xyzzy" "XYZZY"))
120
121 ;; In POSIX or C locales, collation order is lexicographic.
122 (should (string-collate-lessp "XYZZY" "xyzzy" "POSIX"))
123 ;; In a language specific locale, collation order is different.
124 (should (string-collate-lessp
125 "xyzzy" "XYZZY"
126 (if (eq system-type 'windows-nt) "enu_USA" "en_US.UTF-8")))
127
128 ;; Ignore case.
129 (should (string-collate-equalp "xyzzy" "XYZZY" nil t))
130
131 ;; Locale must be valid.
132 (should-error (string-collate-equalp "xyzzy" "xyzzy" "en_DE.UTF-8")))
133
134 ;; There must be a check for valid codepoints. (Check not implemented yet)
135 ; (should-error
136 ; (string-collate-equalp (string ?\x00110000) (string ?\x00110000)))
137 ;; Invalid UTF-8 sequences shall be indicated. How to create such strings?
138
139 (ert-deftest fns-tests-sort ()
140 (should (equal (sort '(9 5 2 -1 5 3 8 7 4) (lambda (x y) (< x y)))
141 '(-1 2 3 4 5 5 7 8 9)))
142 (should (equal (sort '(9 5 2 -1 5 3 8 7 4) (lambda (x y) (> x y)))
143 '(9 8 7 5 5 4 3 2 -1)))
144 (should (equal (sort '[9 5 2 -1 5 3 8 7 4] (lambda (x y) (< x y)))
145 [-1 2 3 4 5 5 7 8 9]))
146 (should (equal (sort '[9 5 2 -1 5 3 8 7 4] (lambda (x y) (> x y)))
147 [9 8 7 5 5 4 3 2 -1]))
148 (should (equal
149 (sort
150 (vector
151 '(8 . "xxx") '(9 . "aaa") '(8 . "bbb") '(9 . "zzz")
152 '(9 . "ppp") '(8 . "ttt") '(8 . "eee") '(9 . "fff"))
153 (lambda (x y) (< (car x) (car y))))
154 [(8 . "xxx") (8 . "bbb") (8 . "ttt") (8 . "eee")
155 (9 . "aaa") (9 . "zzz") (9 . "ppp") (9 . "fff")])))
156
157 (ert-deftest fns-tests-collate-sort ()
158 ;; See https://lists.gnu.org/archive/html/emacs-devel/2015-10/msg02505.html.
159 :expected-result (if (eq system-type 'cygwin) :failed :passed)
160 (skip-unless (fns-tests--collate-enabled-p))
161
162 ;; Punctuation and whitespace characters are relevant for POSIX.
163 (should
164 (equal
165 (sort '("11" "12" "1 1" "1 2" "1.1" "1.2")
166 (lambda (a b) (string-collate-lessp a b "POSIX")))
167 '("1 1" "1 2" "1.1" "1.2" "11" "12")))
168 ;; Punctuation and whitespace characters are not taken into account
169 ;; for collation in other locales.
170 (should
171 (equal
172 (sort '("11" "12" "1 1" "1 2" "1.1" "1.2")
173 (lambda (a b)
174 (let ((w32-collate-ignore-punctuation t))
175 (string-collate-lessp
176 a b (if (eq system-type 'windows-nt) "enu_USA" "en_US.UTF-8")))))
177 '("11" "1 1" "1.1" "12" "1 2" "1.2")))
178
179 ;; Diacritics are different letters for POSIX, they sort lexicographical.
180 (should
181 (equal
182 (sort '("Ævar" "Agustín" "Adrian" "Eli")
183 (lambda (a b) (string-collate-lessp a b "POSIX")))
184 '("Adrian" "Agustín" "Eli" "Ævar")))
185 ;; Diacritics are sorted between similar letters for other locales.
186 (should
187 (equal
188 (sort '("Ævar" "Agustín" "Adrian" "Eli")
189 (lambda (a b)
190 (let ((w32-collate-ignore-punctuation t))
191 (string-collate-lessp
192 a b (if (eq system-type 'windows-nt) "enu_USA" "en_US.UTF-8")))))
193 '("Adrian" "Ævar" "Agustín" "Eli"))))
194
195 (ert-deftest fns-tests-string-version-lessp ()
196 (should (string-version-lessp "foo2.png" "foo12.png"))
197 (should (not (string-version-lessp "foo12.png" "foo2.png")))
198 (should (string-version-lessp "foo12.png" "foo20000.png"))
199 (should (not (string-version-lessp "foo20000.png" "foo12.png")))
200 (should (string-version-lessp "foo.png" "foo2.png"))
201 (should (not (string-version-lessp "foo2.png" "foo.png")))
202 (should (equal (sort '("foo12.png" "foo2.png" "foo1.png")
203 'string-version-lessp)
204 '("foo1.png" "foo2.png" "foo12.png")))
205 (should (string-version-lessp "foo2" "foo1234"))
206 (should (not (string-version-lessp "foo1234" "foo2")))
207 (should (string-version-lessp "foo.png" "foo2"))
208 (should (string-version-lessp "foo1.25.5.png" "foo1.125.5"))
209 (should (string-version-lessp "2" "1245"))
210 (should (not (string-version-lessp "1245" "2"))))
211
212 (ert-deftest fns-tests-func-arity ()
213 (should (equal (func-arity 'car) '(1 . 1)))
214 (should (equal (func-arity 'caar) '(1 . 1)))
215 (should (equal (func-arity 'format) '(1 . many)))
216 (require 'info)
217 (should (equal (func-arity 'Info-goto-node) '(1 . 3)))
218 (should (equal (func-arity (lambda (&rest x))) '(0 . many)))
219 (should (equal (func-arity (eval (lambda (x &optional y)) nil)) '(1 . 2)))
220 (should (equal (func-arity (eval (lambda (x &optional y)) t)) '(1 . 2)))
221 (should (equal (func-arity 'let) '(1 . unevalled))))
222
223 (ert-deftest fns-tests-hash-buffer ()
224 (should (equal (sha1 "foo") "0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33"))
225 (should (equal (with-temp-buffer
226 (insert "foo")
227 (buffer-hash))
228 (sha1 "foo")))
229 ;; This tests whether the presence of a gap in the middle of the
230 ;; buffer is handled correctly.
231 (should (equal (with-temp-buffer
232 (insert "foo")
233 (goto-char 2)
234 (insert " ")
235 (backward-delete-char 1)
236 (buffer-hash))
237 (sha1 "foo"))))