Fix a false negative in `map-elt' with alists and values being nil
[emacs.git] / test / automated / map-tests.el
blobe65af89427584450710e486d0ffaecac8a501421
1 ;;; map-tests.el --- Tests for map.el
3 ;; Copyright (C) 2015 Free Software Foundation, Inc.
5 ;; Author: Nicolas Petton <nicolas@petton.fr>
6 ;; Maintainer: emacs-devel@gnu.org
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; Tests for map.el
27 ;;; Code:
29 (require 'ert)
30 (require 'map)
32 (defmacro with-maps-do (var &rest body)
33 "Successively bind VAR to an alist, vector and hash-table.
34 Each map is built from the following alist data:
35 '((0 . 3) (1 . 4) (2 . 5)).
36 Evaluate BODY for each created map.
38 \(fn (var map) body)"
39 (declare (indent 1) (debug t))
40 (let ((alist (make-symbol "alist"))
41 (vec (make-symbol "vec"))
42 (ht (make-symbol "ht")))
43 `(let ((,alist '((0 . 3)
44 (1 . 4)
45 (2 . 5)))
46 (,vec (make-vector 3 nil))
47 (,ht (make-hash-table)))
48 (aset ,vec 0 '3)
49 (aset ,vec 1 '4)
50 (aset ,vec 2 '5)
51 (puthash '0 3 ,ht)
52 (puthash '1 4 ,ht)
53 (puthash '2 5 ,ht)
54 (dolist (,var (list ,alist ,vec ,ht))
55 ,@body))))
57 (ert-deftest test-map-elt ()
58 (with-maps-do map
59 (assert (= 3 (map-elt map 0)))
60 (assert (= 4 (map-elt map 1)))
61 (assert (= 5 (map-elt map 2)))
62 (assert (null (map-elt map -1)))
63 (assert (null (map-elt map 4)))))
65 (ert-deftest test-map-elt-default ()
66 (with-maps-do map
67 (assert (= 5 (map-elt map 7 5)))))
69 (ert-deftest test-map-elt-with-nil-value ()
70 (assert (null (map-elt '((a . 1)
71 (b))
73 '2))))
75 (ert-deftest test-map-put ()
76 (with-maps-do map
77 (map-put map 2 'hello)
78 (assert (eq (map-elt map 2) 'hello)))
79 (let ((ht (make-hash-table)))
80 (map-put ht 2 'a)
81 (assert (eq (map-elt ht 2)
82 'a)))
83 (let ((alist '((0 . a) (1 . b) (2 . c))))
84 (map-put alist 2 'a)
85 (assert (eq (map-elt alist 2)
86 'a)))
87 (let ((vec [3 4 5]))
88 (should-error (map-put vec 3 6))))
90 (ert-deftest test-map-put-literal ()
91 (assert (= (map-elt (map-put [1 2 3] 1 4) 1)
92 4))
93 (assert (= (map-elt (map-put (make-hash-table) 'a 2) 'a)
94 2))
95 (should-error (map-put '((a . 1)) 'b 2))
96 (should-error (map-put '() 'a 1)))
98 (ert-deftest test-map-put-return-value ()
99 (let ((ht (make-hash-table)))
100 (assert (eq (map-put ht 'a 'hello) ht))))
102 (ert-deftest test-map-delete ()
103 (with-maps-do map
104 (map-delete map 1)
105 (assert (null (map-elt map 1))))
106 (with-maps-do map
107 (map-delete map -2)
108 (assert (null (map-elt map -2)))))
110 (ert-deftest test-map-delete-return-value ()
111 (let ((ht (make-hash-table)))
112 (assert (eq (map-delete ht 'a) ht))))
114 (ert-deftest test-map-nested-elt ()
115 (let ((vec [a b [c d [e f]]]))
116 (assert (eq (map-nested-elt vec '(2 2 0)) 'e)))
117 (let ((alist '((a . 1)
118 (b . ((c . 2)
119 (d . 3)
120 (e . ((f . 4)
121 (g . 5))))))))
122 (assert (eq (map-nested-elt alist '(b e f))
123 4)))
124 (let ((ht (make-hash-table)))
125 (map-put ht 'a 1)
126 (map-put ht 'b (make-hash-table))
127 (map-put (map-elt ht 'b) 'c 2)
128 (assert (eq (map-nested-elt ht '(b c))
129 2))))
131 (ert-deftest test-map-nested-elt-default ()
132 (let ((vec [a b [c d]]))
133 (assert (null (map-nested-elt vec '(2 3))))
134 (assert (null (map-nested-elt vec '(2 1 1))))
135 (assert (= 4 (map-nested-elt vec '(2 1 1) 4)))))
137 (ert-deftest test-map-p ()
138 (assert (map-p nil))
139 (assert (map-p '((a . b) (c . d))))
140 (assert (map-p '(a b c d)))
141 (assert (map-p []))
142 (assert (map-p [1 2 3]))
143 (assert (map-p (make-hash-table)))
144 (assert (map-p "hello"))
145 (assert (not (map-p 1)))
146 (assert (not (map-p 'hello))))
148 (ert-deftest test-map-keys ()
149 (with-maps-do map
150 (assert (equal (map-keys map) '(0 1 2))))
151 (assert (null (map-keys nil)))
152 (assert (null (map-keys []))))
154 (ert-deftest test-map-values ()
155 (with-maps-do map
156 (assert (equal (map-values map) '(3 4 5)))))
158 (ert-deftest test-map-pairs ()
159 (with-maps-do map
160 (assert (equal (map-pairs map) '((0 . 3)
161 (1 . 4)
162 (2 . 5))))))
164 (ert-deftest test-map-length ()
165 (let ((ht (make-hash-table)))
166 (puthash 'a 1 ht)
167 (puthash 'b 2 ht)
168 (puthash 'c 3 ht)
169 (puthash 'd 4 ht)
170 (assert (= 0 (map-length nil)))
171 (assert (= 0 (map-length [])))
172 (assert (= 0 (map-length (make-hash-table))))
173 (assert (= 5 (map-length [0 1 2 3 4])))
174 (assert (= 2 (map-length '((a . 1) (b . 2)))))
175 (assert (= 4 (map-length ht)))))
177 (ert-deftest test-map-copy ()
178 (with-maps-do map
179 (let ((copy (map-copy map)))
180 (assert (equal (map-keys map) (map-keys copy)))
181 (assert (equal (map-values map) (map-values copy)))
182 (assert (not (eq map copy))))))
184 (ert-deftest test-map-apply ()
185 (with-maps-do map
186 (assert (equal (map-apply (lambda (k v) (cons (int-to-string k) v))
187 map)
188 '(("0" . 3) ("1" . 4) ("2" . 5)))))
189 (let ((vec [a b c]))
190 (assert (equal (map-apply (lambda (k v) (cons (1+ k) v))
191 vec)
192 '((1 . a)
193 (2 . b)
194 (3 . c))))))
196 (ert-deftest test-map-keys-apply ()
197 (with-maps-do map
198 (assert (equal (map-keys-apply (lambda (k) (int-to-string k))
199 map)
200 '("0" "1" "2"))))
201 (let ((vec [a b c]))
202 (assert (equal (map-keys-apply (lambda (k) (1+ k))
203 vec)
204 '(1 2 3)))))
206 (ert-deftest test-map-values-apply ()
207 (with-maps-do map
208 (assert (equal (map-values-apply (lambda (v) (1+ v))
209 map)
210 '(4 5 6))))
211 (let ((vec [a b c]))
212 (assert (equal (map-values-apply (lambda (v) (symbol-name v))
213 vec)
214 '("a" "b" "c")))))
216 (ert-deftest test-map-filter ()
217 (with-maps-do map
218 (assert (equal (map-keys (map-filter (lambda (k v)
219 (<= 4 v))
220 map))
221 '(1 2)))
222 (assert (null (map-filter (lambda (k v)
223 (eq 'd k))
224 map))))
225 (assert (null (map-filter (lambda (k v)
226 (eq 3 v))
227 [1 2 4 5])))
228 (assert (equal (map-filter (lambda (k v)
229 (eq 3 k))
230 [1 2 4 5])
231 '((3 . 5)))))
233 (ert-deftest test-map-remove ()
234 (with-maps-do map
235 (assert (equal (map-keys (map-remove (lambda (k v)
236 (>= v 4))
237 map))
238 '(0)))
239 (assert (equal (map-keys (map-remove (lambda (k v)
240 (eq 'd k))
241 map))
242 (map-keys map))))
243 (assert (equal (map-remove (lambda (k v)
244 (eq 3 v))
245 [1 2 4 5])
246 '((0 . 1)
247 (1 . 2)
248 (2 . 4)
249 (3 . 5))))
250 (assert (null (map-remove (lambda (k v)
251 (>= k 0))
252 [1 2 4 5]))))
254 (ert-deftest test-map-empty-p ()
255 (assert (map-empty-p nil))
256 (assert (not (map-empty-p '((a . b) (c . d)))))
257 (assert (map-empty-p []))
258 (assert (not (map-empty-p [1 2 3])))
259 (assert (map-empty-p (make-hash-table)))
260 (assert (not (map-empty-p "hello")))
261 (assert (map-empty-p "")))
263 (ert-deftest test-map-contains-key-p ()
264 (assert (map-contains-key-p '((a . 1) (b . 2)) 'a))
265 (assert (not (map-contains-key-p '((a . 1) (b . 2)) 'c)))
266 (assert (map-contains-key-p '(("a" . 1)) "a"))
267 (assert (not (map-contains-key-p '(("a" . 1)) "a" #'eq)))
268 (assert (map-contains-key-p [a b c] 2))
269 (assert (not (map-contains-key-p [a b c] 3))))
271 (ert-deftest test-map-some-p ()
272 (with-maps-do map
273 (assert (equal (map-some-p (lambda (k v)
274 (eq 1 k))
275 map)
276 (cons 1 4)))
277 (assert (not (map-some-p (lambda (k v)
278 (eq 'd k))
279 map))))
280 (let ((vec [a b c]))
281 (assert (equal (map-some-p (lambda (k v)
282 (> k 1))
283 vec)
284 (cons 2 'c)))
285 (assert (not (map-some-p (lambda (k v)
286 (> k 3))
287 vec)))))
289 (ert-deftest test-map-every-p ()
290 (with-maps-do map
291 (assert (map-every-p (lambda (k v)
293 map))
294 (assert (not (map-every-p (lambda (k v)
295 nil)
296 map))))
297 (let ((vec [a b c]))
298 (assert (map-every-p (lambda (k v)
299 (>= k 0))
300 vec))
301 (assert (not (map-every-p (lambda (k v)
302 (> k 3))
303 vec)))))
305 (ert-deftest test-map-into ()
306 (let* ((alist '((a . 1) (b . 2)))
307 (ht (map-into alist 'hash-table)))
308 (assert (hash-table-p ht))
309 (assert (equal (map-into (map-into alist 'hash-table) 'list)
310 alist))
311 (assert (listp (map-into ht 'list)))
312 (assert (equal (map-keys (map-into (map-into ht 'list) 'hash-table))
313 (map-keys ht)))
314 (assert (equal (map-values (map-into (map-into ht 'list) 'hash-table))
315 (map-values ht)))
316 (assert (null (map-into nil 'list)))
317 (assert (map-empty-p (map-into nil 'hash-table)))
318 (should-error (map-into [1 2 3] 'string))))
320 (provide 'map-tests)
321 ;;; map-tests.el ends here