1 ;;; map-tests.el --- Tests for map.el -*- lexical-binding:t -*-
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/>.
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.
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
(list (cons 0 3)
47 (,ht
(make-hash-table)))
51 (dolist (,var
(list ,alist
,vec
,ht
))
54 (ert-deftest test-map-elt
()
56 (should (= 3 (map-elt map
0)))
57 (should (= 4 (map-elt map
1)))
58 (should (= 5 (map-elt map
2)))
59 (should (null (map-elt map -
1)))
60 (should (null (map-elt map
4)))))
62 (ert-deftest test-map-elt-default
()
64 (should (= 5 (map-elt map
7 5)))))
66 (ert-deftest test-map-elt-with-nil-value
()
67 (should (null (map-elt '((a .
1)
72 (ert-deftest test-map-put
()
74 (setf (map-elt map
2) 'hello
)
75 (should (eq (map-elt map
2) 'hello
)))
77 (map-put map
2 'hello
)
78 (should (eq (map-elt map
2) 'hello
)))
79 (let ((ht (make-hash-table)))
80 (setf (map-elt ht
2) 'a
)
81 (should (eq (map-elt ht
2)
83 (let ((alist '((0 . a
) (1 . b
) (2 . c
))))
84 (setf (map-elt alist
2) 'a
)
85 (should (eq (map-elt alist
2)
88 (should-error (setf (map-elt vec
3) 6))))
90 (ert-deftest test-map-put-return-value
()
91 (let ((ht (make-hash-table)))
92 (should (eq (map-put ht
'a
'hello
) ht
))))
94 (ert-deftest test-map-delete
()
97 (should (null (map-elt map
1))))
100 (should (null (map-elt map -
2)))))
102 (ert-deftest test-map-delete-return-value
()
103 (let ((ht (make-hash-table)))
104 (should (eq (map-delete ht
'a
) ht
))))
106 (ert-deftest test-map-nested-elt
()
107 (let ((vec [a b
[c d
[e f
]]]))
108 (should (eq (map-nested-elt vec
'(2 2 0)) 'e
)))
109 (let ((alist '((a .
1)
114 (should (eq (map-nested-elt alist
'(b e f
))
116 (let ((ht (make-hash-table)))
117 (setf (map-elt ht
'a
) 1)
118 (setf (map-elt ht
'b
) (make-hash-table))
119 (setf (map-elt (map-elt ht
'b
) 'c
) 2)
120 (should (eq (map-nested-elt ht
'(b c
))
123 (ert-deftest test-map-nested-elt-default
()
124 (let ((vec [a b
[c d
]]))
125 (should (null (map-nested-elt vec
'(2 3))))
126 (should (null (map-nested-elt vec
'(2 1 1))))
127 (should (= 4 (map-nested-elt vec
'(2 1 1) 4)))))
129 (ert-deftest test-map-p
()
131 (should (map-p '((a . b
) (c . d
))))
132 (should (map-p '(a b c d
)))
134 (should (map-p [1 2 3]))
135 (should (map-p (make-hash-table)))
136 (should (map-p "hello"))
137 (should (not (map-p 1)))
138 (should (not (map-p 'hello
))))
140 (ert-deftest test-map-keys
()
142 (should (equal (map-keys map
) '(0 1 2))))
143 (should (null (map-keys nil
)))
144 (should (null (map-keys []))))
146 (ert-deftest test-map-values
()
148 (should (equal (map-values map
) '(3 4 5)))))
150 (ert-deftest test-map-pairs
()
152 (should (equal (map-pairs map
) '((0 .
3)
156 (ert-deftest test-map-length
()
157 (let ((ht (make-hash-table)))
162 (should (= 0 (map-length nil
)))
163 (should (= 0 (map-length [])))
164 (should (= 0 (map-length (make-hash-table))))
165 (should (= 5 (map-length [0 1 2 3 4])))
166 (should (= 2 (map-length '((a .
1) (b .
2)))))
167 (should (= 4 (map-length ht
)))))
169 (ert-deftest test-map-copy
()
171 (let ((copy (map-copy map
)))
172 (should (equal (map-keys map
) (map-keys copy
)))
173 (should (equal (map-values map
) (map-values copy
)))
174 (should (not (eq map copy
))))))
176 (ert-deftest test-map-apply
()
178 (should (equal (map-apply (lambda (k v
) (cons (int-to-string k
) v
))
180 '(("0" .
3) ("1" .
4) ("2" .
5)))))
182 (should (equal (map-apply (lambda (k v
) (cons (1+ k
) v
))
188 (ert-deftest test-map-keys-apply
()
190 (should (equal (map-keys-apply (lambda (k) (int-to-string k
))
194 (should (equal (map-keys-apply (lambda (k) (1+ k
))
198 (ert-deftest test-map-values-apply
()
200 (should (equal (map-values-apply (lambda (v) (1+ v
))
204 (should (equal (map-values-apply (lambda (v) (symbol-name v
))
208 (ert-deftest test-map-filter
()
210 (should (equal (map-keys (map-filter (lambda (_k v
)
214 (should (null (map-filter (lambda (k _v
)
217 (should (null (map-filter (lambda (_k v
)
220 (should (equal (map-filter (lambda (k _v
)
225 (ert-deftest test-map-remove
()
227 (should (equal (map-keys (map-remove (lambda (_k v
)
231 (should (equal (map-keys (map-remove (lambda (k _v
)
235 (should (equal (map-remove (lambda (_k v
)
242 (should (null (map-remove (lambda (k _v
)
246 (ert-deftest test-map-empty-p
()
247 (should (map-empty-p nil
))
248 (should (not (map-empty-p '((a . b
) (c . d
)))))
249 (should (map-empty-p []))
250 (should (not (map-empty-p [1 2 3])))
251 (should (map-empty-p (make-hash-table)))
252 (should (not (map-empty-p "hello")))
253 (should (map-empty-p "")))
255 (ert-deftest test-map-contains-key
()
256 (should (map-contains-key '((a .
1) (b .
2)) 'a
))
257 (should (not (map-contains-key '((a .
1) (b .
2)) 'c
)))
258 (should (map-contains-key '(("a" .
1)) "a"))
259 (should (not (map-contains-key '(("a" .
1)) "a" #'eq
)))
260 (should (map-contains-key [a b c
] 2))
261 (should (not (map-contains-key [a b c
] 3))))
263 (ert-deftest test-map-some
()
265 (should (map-some (lambda (k _v
)
268 (should-not (map-some (lambda (k _v
)
272 (should (map-some (lambda (k _v
)
275 (should-not (map-some (lambda (k _v
)
279 (ert-deftest test-map-every-p
()
281 (should (map-every-p (lambda (k _v
)
284 (should (not (map-every-p (lambda (_k _v
)
288 (should (map-every-p (lambda (k _v
)
291 (should (not (map-every-p (lambda (k _v
)
295 (ert-deftest test-map-into
()
296 (let* ((alist '((a .
1) (b .
2)))
297 (ht (map-into alist
'hash-table
)))
298 (should (hash-table-p ht
))
299 (should (equal (map-into (map-into alist
'hash-table
) 'list
)
301 (should (listp (map-into ht
'list
)))
302 (should (equal (map-keys (map-into (map-into ht
'list
) 'hash-table
))
304 (should (equal (map-values (map-into (map-into ht
'list
) 'hash-table
))
306 (should (null (map-into nil
'list
)))
307 (should (map-empty-p (map-into nil
'hash-table
)))
308 (should-error (map-into [1 2 3] 'string
))))
310 (ert-deftest test-map-let
()
311 (map-let (foo bar baz
) '((foo .
1) (bar .
2))
318 '((foo .
1) (bar .
2))
324 ;;; map-tests.el ends here