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/>.
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
'((0 .
3)
46 (,vec
(make-vector 3 nil
))
47 (,ht
(make-hash-table)))
54 (dolist (,var
(list ,alist
,vec
,ht
))
57 (ert-deftest test-map-elt
()
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
()
67 (assert (= 5 (map-elt map
7 5)))))
69 (ert-deftest test-map-elt-with-nil-value
()
70 (assert (null (map-elt '((a .
1)
75 (ert-deftest test-map-put
()
77 (map-put map
2 'hello
)
78 (assert (eq (map-elt map
2) 'hello
)))
79 (let ((ht (make-hash-table)))
81 (assert (eq (map-elt ht
2)
83 (let ((alist '((0 . a
) (1 . b
) (2 . c
))))
85 (assert (eq (map-elt alist
2)
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)
93 (assert (= (map-elt (map-put (make-hash-table) 'a
2) 'a
)
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
()
105 (assert (null (map-elt map
1))))
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)
122 (assert (eq (map-nested-elt alist
'(b e f
))
124 (let ((ht (make-hash-table)))
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
))
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
()
139 (assert (map-p '((a . b
) (c . d
))))
140 (assert (map-p '(a b c d
)))
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
()
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
()
156 (assert (equal (map-values map
) '(3 4 5)))))
158 (ert-deftest test-map-pairs
()
160 (assert (equal (map-pairs map
) '((0 .
3)
164 (ert-deftest test-map-length
()
165 (let ((ht (make-hash-table)))
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
()
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
()
186 (assert (equal (map-apply (lambda (k v
) (cons (int-to-string k
) v
))
188 '(("0" .
3) ("1" .
4) ("2" .
5)))))
190 (assert (equal (map-apply (lambda (k v
) (cons (1+ k
) v
))
196 (ert-deftest test-map-keys-apply
()
198 (assert (equal (map-keys-apply (lambda (k) (int-to-string k
))
202 (assert (equal (map-keys-apply (lambda (k) (1+ k
))
206 (ert-deftest test-map-values-apply
()
208 (assert (equal (map-values-apply (lambda (v) (1+ v
))
212 (assert (equal (map-values-apply (lambda (v) (symbol-name v
))
216 (ert-deftest test-map-filter
()
218 (assert (equal (map-keys (map-filter (lambda (k v
)
222 (assert (null (map-filter (lambda (k v
)
225 (assert (null (map-filter (lambda (k v
)
228 (assert (equal (map-filter (lambda (k v
)
233 (ert-deftest test-map-remove
()
235 (assert (equal (map-keys (map-remove (lambda (k v
)
239 (assert (equal (map-keys (map-remove (lambda (k v
)
243 (assert (equal (map-remove (lambda (k v
)
250 (assert (null (map-remove (lambda (k v
)
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
()
273 (assert (equal (map-some-p (lambda (k v
)
277 (assert (not (map-some-p (lambda (k v
)
281 (assert (equal (map-some-p (lambda (k v
)
285 (assert (not (map-some-p (lambda (k v
)
289 (ert-deftest test-map-every-p
()
291 (assert (map-every-p (lambda (k v
)
294 (assert (not (map-every-p (lambda (k v
)
298 (assert (map-every-p (lambda (k v
)
301 (assert (not (map-every-p (lambda (k v
)
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
)
311 (assert (listp (map-into ht
'list
)))
312 (assert (equal (map-keys (map-into (map-into ht
'list
) 'hash-table
))
314 (assert (equal (map-values (map-into (map-into ht
'list
) 'hash-table
))
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
))))
321 ;;; map-tests.el ends here