Typo in example
[emacs.git] / test / automated / map-tests.el
blob8693415a784cbae66414a9f1fca8285dbfb360e7
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/>.
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 (list (cons 0 3)
44 (cons 1 4)
45 (cons 2 5)))
46 (,vec (vector 3 4 5))
47 (,ht (make-hash-table)))
48 (puthash 0 3 ,ht)
49 (puthash 1 4 ,ht)
50 (puthash 2 5 ,ht)
51 (dolist (,var (list ,alist ,vec ,ht))
52 ,@body))))
54 (ert-deftest test-map-elt ()
55 (with-maps-do map
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 ()
63 (with-maps-do map
64 (should (= 5 (map-elt map 7 5)))))
66 (ert-deftest test-map-elt-with-nil-value ()
67 (should (null (map-elt '((a . 1)
68 (b))
70 '2))))
72 (ert-deftest test-map-put ()
73 (with-maps-do map
74 (setf (map-elt map 2) 'hello)
75 (should (eq (map-elt map 2) 'hello)))
76 (with-maps-do map
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)
82 'a)))
83 (let ((alist '((0 . a) (1 . b) (2 . c))))
84 (setf (map-elt alist 2) 'a)
85 (should (eq (map-elt alist 2)
86 'a)))
87 (let ((vec [3 4 5]))
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 ()
95 (with-maps-do map
96 (map-delete map 1)
97 (should (null (map-elt map 1))))
98 (with-maps-do map
99 (map-delete map -2)
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)
110 (b . ((c . 2)
111 (d . 3)
112 (e . ((f . 4)
113 (g . 5))))))))
114 (should (eq (map-nested-elt alist '(b e f))
115 4)))
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))
121 2))))
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 ()
130 (should (map-p nil))
131 (should (map-p '((a . b) (c . d))))
132 (should (map-p '(a b c d)))
133 (should (map-p []))
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 ()
141 (with-maps-do map
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 ()
147 (with-maps-do map
148 (should (equal (map-values map) '(3 4 5)))))
150 (ert-deftest test-map-pairs ()
151 (with-maps-do map
152 (should (equal (map-pairs map) '((0 . 3)
153 (1 . 4)
154 (2 . 5))))))
156 (ert-deftest test-map-length ()
157 (let ((ht (make-hash-table)))
158 (puthash 'a 1 ht)
159 (puthash 'b 2 ht)
160 (puthash 'c 3 ht)
161 (puthash 'd 4 ht)
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 ()
170 (with-maps-do map
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 ()
177 (with-maps-do map
178 (should (equal (map-apply (lambda (k v) (cons (int-to-string k) v))
179 map)
180 '(("0" . 3) ("1" . 4) ("2" . 5)))))
181 (let ((vec [a b c]))
182 (should (equal (map-apply (lambda (k v) (cons (1+ k) v))
183 vec)
184 '((1 . a)
185 (2 . b)
186 (3 . c))))))
188 (ert-deftest test-map-keys-apply ()
189 (with-maps-do map
190 (should (equal (map-keys-apply (lambda (k) (int-to-string k))
191 map)
192 '("0" "1" "2"))))
193 (let ((vec [a b c]))
194 (should (equal (map-keys-apply (lambda (k) (1+ k))
195 vec)
196 '(1 2 3)))))
198 (ert-deftest test-map-values-apply ()
199 (with-maps-do map
200 (should (equal (map-values-apply (lambda (v) (1+ v))
201 map)
202 '(4 5 6))))
203 (let ((vec [a b c]))
204 (should (equal (map-values-apply (lambda (v) (symbol-name v))
205 vec)
206 '("a" "b" "c")))))
208 (ert-deftest test-map-filter ()
209 (with-maps-do map
210 (should (equal (map-keys (map-filter (lambda (_k v)
211 (<= 4 v))
212 map))
213 '(1 2)))
214 (should (null (map-filter (lambda (k _v)
215 (eq 'd k))
216 map))))
217 (should (null (map-filter (lambda (_k v)
218 (eq 3 v))
219 [1 2 4 5])))
220 (should (equal (map-filter (lambda (k _v)
221 (eq 3 k))
222 [1 2 4 5])
223 '((3 . 5)))))
225 (ert-deftest test-map-remove ()
226 (with-maps-do map
227 (should (equal (map-keys (map-remove (lambda (_k v)
228 (>= v 4))
229 map))
230 '(0)))
231 (should (equal (map-keys (map-remove (lambda (k _v)
232 (eq 'd k))
233 map))
234 (map-keys map))))
235 (should (equal (map-remove (lambda (_k v)
236 (eq 3 v))
237 [1 2 4 5])
238 '((0 . 1)
239 (1 . 2)
240 (2 . 4)
241 (3 . 5))))
242 (should (null (map-remove (lambda (k _v)
243 (>= k 0))
244 [1 2 4 5]))))
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 ()
264 (with-maps-do map
265 (should (map-some (lambda (k _v)
266 (eq 1 k))
267 map))
268 (should-not (map-some (lambda (k _v)
269 (eq 'd k))
270 map)))
271 (let ((vec [a b c]))
272 (should (map-some (lambda (k _v)
273 (> k 1))
274 vec))
275 (should-not (map-some (lambda (k _v)
276 (> k 3))
277 vec))))
279 (ert-deftest test-map-every-p ()
280 (with-maps-do map
281 (should (map-every-p (lambda (k _v)
283 map))
284 (should (not (map-every-p (lambda (_k _v)
285 nil)
286 map))))
287 (let ((vec [a b c]))
288 (should (map-every-p (lambda (k _v)
289 (>= k 0))
290 vec))
291 (should (not (map-every-p (lambda (k _v)
292 (> k 3))
293 vec)))))
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)
300 alist))
301 (should (listp (map-into ht 'list)))
302 (should (equal (map-keys (map-into (map-into ht 'list) 'hash-table))
303 (map-keys ht)))
304 (should (equal (map-values (map-into (map-into ht 'list) 'hash-table))
305 (map-values ht)))
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))
312 (should (= foo 1))
313 (should (= bar 2))
314 (should (null baz)))
315 (map-let (('foo a)
316 ('bar b)
317 ('baz c))
318 '((foo . 1) (bar . 2))
319 (should (= a 1))
320 (should (= b 2))
321 (should (null c))))
323 (provide 'map-tests)
324 ;;; map-tests.el ends here