map-tests.el 12.2 KB
Newer Older
1
;;; map-tests.el --- Tests for map.el  -*- lexical-binding:t -*-
2

Paul Eggert's avatar
Paul Eggert committed
3
;; Copyright (C) 2015-2018 Free Software Foundation, Inc.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

;; Author: Nicolas Petton <nicolas@petton.fr>
;; Maintainer: emacs-devel@gnu.org

;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
21
;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.
22 23 24 25 26 27 28 29 30 31

;;; Commentary:

;; Tests for map.el

;;; Code:

(require 'ert)
(require 'map)

32 33 34 35 36 37 38
(defmacro with-maps-do (var &rest body)
  "Successively bind VAR to an alist, vector and hash-table.
Each map is built from the following alist data:
'((0 . 3) (1 . 4) (2 . 5)).
Evaluate BODY for each created map.

\(fn (var map) body)"
39
  (declare (indent 1) (debug (symbolp body)))
40
  (let ((alist (make-symbol "alist"))
41
        (plist (make-symbol "plist"))
42 43
        (vec (make-symbol "vec"))
        (ht (make-symbol "ht")))
44 45 46
   `(let ((,alist (list (cons 0 3)
                        (cons 1 4)
                        (cons 2 5)))
47
          (,plist (list 0 3 1 4 2 5))
48
          (,vec (vector 3 4 5))
49
          (,ht (make-hash-table)))
50 51 52
      (puthash 0 3 ,ht)
      (puthash 1 4 ,ht)
      (puthash 2 5 ,ht)
53
      (dolist (,var (list ,alist ,plist ,vec ,ht))
54
        ,@body))))
55 56

(ert-deftest test-map-elt ()
57
  (with-maps-do map
58 59 60 61 62
    (should (= 3 (map-elt map 0)))
    (should (= 4 (map-elt map 1)))
    (should (= 5 (map-elt map 2)))
    (should (null (map-elt map -1)))
    (should (null (map-elt map 4)))))
63 64

(ert-deftest test-map-elt-default ()
65
  (with-maps-do map
66
    (should (= 5 (map-elt map 7 5)))))
67

68
(ert-deftest test-map-elt-testfn ()
Noam Postavsky's avatar
Noam Postavsky committed
69 70 71 72 73
  (let ((map (list (cons "a" 1) (cons "b" 2)))
        ;; Make sure to use a non-eq "a", even when compiled.
        (noneq-key (string ?a)))
    (should-not (map-elt map noneq-key))
    (should (map-elt map noneq-key nil 'equal))))
74

75
(ert-deftest test-map-elt-with-nil-value ()
76
  (should (null (map-elt '((a . 1)
77 78 79 80
                           (b))
                         'b
                         '2))))

81
(ert-deftest test-map-put! ()
82 83 84
  (with-maps-do map
    (setf (map-elt map 2) 'hello)
    (should (eq (map-elt map 2) 'hello)))
85 86
  (with-maps-do map
    (map-put map 2 'hello)
87
    (should (eq (map-elt map 2) 'hello)))
88 89 90
  (with-maps-do map
    (map-put! map 2 'hello)
    (should (eq (map-elt map 2) 'hello))
91 92
    (if (not (or (hash-table-p map)
                 (and (listp map) (not (listp (car map)))))) ;plist!
93 94 95 96 97 98 99 100
        (should-error (map-put! map 5 'value)
                      ;; For vectors, it could arguably signal
                      ;; map-not-inplace as well, but it currently doesn't.
                      :type (if (listp map)
                                'map-not-inplace
                              'error))
      (map-put! map 5 'value)
      (should (eq (map-elt map 5) 'value))))
101
  (let ((ht (make-hash-table)))
102
    (setf (map-elt ht 2) 'a)
103
    (should (eq (map-elt ht 2)
104 105
                'a)))
  (let ((alist '((0 . a) (1 . b) (2 . c))))
106
    (setf (map-elt alist 2) 'a)
107
    (should (eq (map-elt alist 2)
108 109
                'a)))
  (let ((vec [3 4 5]))
110
    (should-error (setf (map-elt vec 3) 6))))
111

112 113 114 115 116 117 118
(ert-deftest test-map-put-alist-new-key ()
  "Regression test for Bug#23105."
  (let ((alist '((0 . a))))
    (map-put alist 2 'b)
    (should (eq (map-elt alist 2)
                'b))))

119
(ert-deftest test-map-put-testfn-alist ()
Noam Postavsky's avatar
Noam Postavsky committed
120 121 122
  (let ((alist (list (cons "a" 1) (cons "b" 2)))
        ;; Make sure to use a non-eq "a", even when compiled.
        (noneq-key (string ?a)))
123
    (map-put alist noneq-key 3 #'equal)
124
    (should-not (cddr alist))
125
    (map-put alist noneq-key 9 #'eql)
126 127
    (should (cddr alist))))

128 129
(ert-deftest test-map-put-return-value ()
  (let ((ht (make-hash-table)))
130
    (should (eq (map-put ht 'a 'hello) 'hello))))
131 132

(ert-deftest test-map-delete ()
133 134
  (with-maps-do map
    (map-delete map 1)
135
    (should (null (map-elt map 1))))
136 137
  (with-maps-do map
    (map-delete map -2)
138
    (should (null (map-elt map -2)))))
139 140 141

(ert-deftest test-map-delete-return-value ()
  (let ((ht (make-hash-table)))
142
    (should (eq (map-delete ht 'a) ht))))
143

144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
(ert-deftest test-map-nested-elt ()
  (let ((vec [a b [c d [e f]]]))
    (should (eq (map-nested-elt vec '(2 2 0)) 'e)))
  (let ((alist '((a . 1)
                 (b . ((c . 2)
                       (d . 3)
                       (e . ((f . 4)
                             (g . 5))))))))
    (should (eq (map-nested-elt alist '(b e f))
                4)))
  (let ((ht (make-hash-table)))
    (setf (map-elt ht 'a) 1)
    (setf (map-elt ht 'b) (make-hash-table))
    (setf (map-elt (map-elt ht 'b) 'c) 2)
    (should (eq (map-nested-elt ht '(b c))
                2))))
160 161 162

(ert-deftest test-map-nested-elt-default ()
  (let ((vec [a b [c d]]))
163 164 165
    (should (null (map-nested-elt vec '(2 3))))
    (should (null (map-nested-elt vec '(2 1 1))))
    (should (= 4 (map-nested-elt vec '(2 1 1) 4)))))
166

167 168 169 170 171 172 173 174 175 176
(ert-deftest test-mapp ()
  (should (mapp nil))
  (should (mapp '((a . b) (c . d))))
  (should (mapp '(a b c d)))
  (should (mapp []))
  (should (mapp [1 2 3]))
  (should (mapp (make-hash-table)))
  (should (mapp "hello"))
  (should (not (mapp 1)))
  (should (not (mapp 'hello))))
177 178

(ert-deftest test-map-keys ()
179
  (with-maps-do map
180 181 182
    (should (equal (map-keys map) '(0 1 2))))
  (should (null (map-keys nil)))
  (should (null (map-keys []))))
183 184

(ert-deftest test-map-values ()
185
  (with-maps-do map
186
    (should (equal (map-values map) '(3 4 5)))))
187 188

(ert-deftest test-map-pairs ()
189
  (with-maps-do map
190
    (should (equal (map-pairs map) '((0 . 3)
191 192
                                     (1 . 4)
                                     (2 . 5))))))
193 194 195 196 197 198 199

(ert-deftest test-map-length ()
  (let ((ht (make-hash-table)))
    (puthash 'a 1 ht)
    (puthash 'b 2 ht)
    (puthash 'c 3 ht)
    (puthash 'd 4 ht)
200 201 202 203 204 205
    (should (= 0 (map-length nil)))
    (should (= 0 (map-length [])))
    (should (= 0 (map-length (make-hash-table))))
    (should (= 5 (map-length [0 1 2 3 4])))
    (should (= 2 (map-length '((a . 1) (b . 2)))))
    (should (= 4 (map-length ht)))))
206 207

(ert-deftest test-map-copy ()
208 209
  (with-maps-do map
    (let ((copy (map-copy map)))
210 211 212
      (should (equal (map-keys map) (map-keys copy)))
      (should (equal (map-values map) (map-values copy)))
      (should (not (eq map copy))))))
213 214

(ert-deftest test-map-apply ()
215
  (with-maps-do map
216
    (should (equal (map-apply (lambda (k v) (cons (int-to-string k) v))
217 218 219
                              map)
                   '(("0" . 3) ("1" . 4) ("2" . 5)))))
  (let ((vec [a b c]))
220
    (should (equal (map-apply (lambda (k v) (cons (1+ k) v))
221 222 223 224 225
                              vec)
                   '((1 . a)
                     (2 . b)
                     (3 . c))))))

NicolasPetton's avatar
NicolasPetton committed
226 227 228 229 230 231 232 233
(ert-deftest test-map-do ()
  (with-maps-do map
    (let ((result nil))
      (map-do (lambda (k v)
                (add-to-list 'result (list (int-to-string k) v)))
              map)
      (should (equal result '(("2" 5) ("1" 4) ("0" 3)))))))

234
(ert-deftest test-map-keys-apply ()
235
  (with-maps-do map
236
    (should (equal (map-keys-apply (lambda (k) (int-to-string k))
237 238 239
                                   map)
                   '("0" "1" "2"))))
  (let ((vec [a b c]))
240
    (should (equal (map-keys-apply (lambda (k) (1+ k))
241
                                   vec)
242 243 244
                   '(1 2 3)))))

(ert-deftest test-map-values-apply ()
245
  (with-maps-do map
246
    (should (equal (map-values-apply (lambda (v) (1+ v))
247 248 249
                                     map)
                   '(4 5 6))))
  (let ((vec [a b c]))
250
    (should (equal (map-values-apply (lambda (v) (symbol-name v))
251
                                     vec)
252 253 254
                   '("a" "b" "c")))))

(ert-deftest test-map-filter ()
255
  (with-maps-do map
256
    (should (equal (map-keys (map-filter (lambda (_k v)
257 258 259
                                           (<= 4 v))
                                         map))
                   '(1 2)))
260
    (should (null (map-filter (lambda (k _v)
261 262
                                (eq 'd k))
                              map))))
263
  (should (null (map-filter (lambda (_k v)
264 265
                              (eq 3 v))
                            [1 2 4 5])))
266
  (should (equal (map-filter (lambda (k _v)
267 268 269
                               (eq 3 k))
                             [1 2 4 5])
                 '((3 . 5)))))
270 271

(ert-deftest test-map-remove ()
272
  (with-maps-do map
273
    (should (equal (map-keys (map-remove (lambda (_k v)
274 275 276
                                           (>= v 4))
                                         map))
                   '(0)))
277
    (should (equal (map-keys (map-remove (lambda (k _v)
278 279 280
                                           (eq 'd k))
                                         map))
                   (map-keys map))))
281
  (should (equal (map-remove (lambda (_k v)
282 283 284 285 286 287
                               (eq 3 v))
                             [1 2 4 5])
                 '((0 . 1)
                   (1 . 2)
                   (2 . 4)
                   (3 . 5))))
288
  (should (null (map-remove (lambda (k _v)
289 290
                              (>= k 0))
                            [1 2 4 5]))))
291 292

(ert-deftest test-map-empty-p ()
293 294 295 296 297 298 299
  (should (map-empty-p nil))
  (should (not (map-empty-p '((a . b) (c . d)))))
  (should (map-empty-p []))
  (should (not (map-empty-p [1 2 3])))
  (should (map-empty-p (make-hash-table)))
  (should (not (map-empty-p "hello")))
  (should (map-empty-p "")))
300

301 302 303 304 305 306 307 308 309
(ert-deftest test-map-contains-key ()
  (should (map-contains-key '((a . 1) (b . 2)) 'a))
  (should (not (map-contains-key '((a . 1) (b . 2)) 'c)))
  (should (map-contains-key '(("a" . 1)) "a"))
  (should (not (map-contains-key '(("a" . 1)) "a" #'eq)))
  (should (map-contains-key [a b c] 2))
  (should (not (map-contains-key [a b c] 3))))

(ert-deftest test-map-some ()
310
  (with-maps-do map
311 312 313 314 315 316
    (should (map-some (lambda (k _v)
                        (eq 1 k))
                      map))
    (should-not (map-some (lambda (k _v)
                            (eq 'd k))
                          map)))
317
  (let ((vec [a b c]))
318 319 320 321 322 323
    (should (map-some (lambda (k _v)
                        (> k 1))
                      vec))
    (should-not (map-some (lambda (k _v)
                            (> k 3))
                          vec))))
324 325

(ert-deftest test-map-every-p ()
326
  (with-maps-do map
327
    (should (map-every-p (lambda (k _v)
328 329
                           k)
                         map))
330
    (should (not (map-every-p (lambda (_k _v)
331 332 333
                                nil)
                              map))))
  (let ((vec [a b c]))
334
    (should (map-every-p (lambda (k _v)
335 336
                           (>= k 0))
                         vec))
337
    (should (not (map-every-p (lambda (k _v)
338
                                (> k 3))
339 340 341
                              vec)))))

(ert-deftest test-map-into ()
342 343
  (let* ((alist '((a . 1) (b . 2)))
         (ht (map-into alist 'hash-table)))
344 345
    (should (hash-table-p ht))
    (should (equal (map-into (map-into alist 'hash-table) 'list)
346
                   alist))
347 348
    (should (listp (map-into ht 'list)))
    (should (equal (map-keys (map-into (map-into ht 'list) 'hash-table))
349
                   (map-keys ht)))
350
    (should (equal (map-values (map-into (map-into ht 'list) 'hash-table))
351
                   (map-values ht)))
352 353
    (should (null (map-into nil 'list)))
    (should (map-empty-p (map-into nil 'hash-table)))
354
    (should-error (map-into [1 2 3] 'string))))
355

356 357
(ert-deftest test-map-let ()
  (map-let (foo bar baz) '((foo . 1) (bar . 2))
358 359 360
    (should (= foo 1))
    (should (= bar 2))
    (should (null baz)))
NicolasPetton's avatar
NicolasPetton committed
361 362
  (map-let (('foo a)
            ('bar b)
363 364
            ('baz c))
      '((foo . 1) (bar . 2))
365 366 367
    (should (= a 1))
    (should (= b 2))
    (should (null c))))
368

369 370 371 372 373 374 375
(ert-deftest test-map-merge-with ()
  (should (equal (map-merge-with 'list #'+
                                 '((1 . 2))
                                 '((1 . 3) (2 . 4))
                                 '((1 . 1) (2 . 5) (3 . 0)))
                 '((3 . 0) (2 . 9) (1 . 6)))))

376 377
(provide 'map-tests)
;;; map-tests.el ends here