seq-tests.el (14097B)
1 ;;; seq-tests.el --- Tests for sequences.el 2 3 ;; Copyright (C) 2014-2015 Free Software Foundation, Inc. 4 5 ;; Author: Nicolas Petton <nicolas@petton.fr> 6 ;; Maintainer: emacs-devel@gnu.org 7 8 ;; This file is part of GNU Emacs. 9 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. 14 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. 19 20 ;; 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 ;;; Commentary: 24 25 ;; Tests for seq.el 26 27 ;;; Code: 28 29 (require 'ert) 30 (require 'seq) 31 32 (defmacro with-test-sequences (spec &rest body) 33 "Successively bind VAR to a list, vector, and string built from SEQ. 34 Evaluate BODY for each created sequence. 35 36 \(fn (var seq) body)" 37 (declare (indent 1) (debug ((symbolp form) body))) 38 (let ((initial-seq (make-symbol "initial-seq"))) 39 `(let ((,initial-seq ,(cadr spec))) 40 ,@(mapcar (lambda (s) 41 `(let ((,(car spec) (apply (function ,s) ,initial-seq))) 42 ,@body)) 43 '(list vector string))))) 44 45 (defun same-contents-p (seq1 seq2) 46 "Return t if SEQ1 and SEQ2 have the same contents, nil otherwise." 47 (equal (append seq1 '()) (append seq2 '()))) 48 49 (defun test-sequences-evenp (integer) 50 "Return t if INTEGER is even." 51 (eq (logand integer 1) 0)) 52 53 (defun test-sequences-oddp (integer) 54 "Return t if INTEGER is odd." 55 (not (test-sequences-evenp integer))) 56 57 (ert-deftest test-seq-drop () 58 (with-test-sequences (seq '(1 2 3 4)) 59 (should (equal (seq-drop seq 0) seq)) 60 (should (equal (seq-drop seq 1) (seq-subseq seq 1))) 61 (should (equal (seq-drop seq 2) (seq-subseq seq 2))) 62 (should (seq-empty-p (seq-drop seq 4))) 63 (should (seq-empty-p (seq-drop seq 10)))) 64 (with-test-sequences (seq '()) 65 (should (seq-empty-p (seq-drop seq 0))) 66 (should (seq-empty-p (seq-drop seq 1))))) 67 68 (ert-deftest test-seq-take () 69 (with-test-sequences (seq '(2 3 4 5)) 70 (should (seq-empty-p (seq-take seq 0))) 71 (should (= (seq-length (seq-take seq 1)) 1)) 72 (should (= (seq-elt (seq-take seq 1) 0) 2)) 73 (should (same-contents-p (seq-take seq 3) '(2 3 4))) 74 (should (equal (seq-take seq 10) seq)))) 75 76 (ert-deftest test-seq-drop-while () 77 (with-test-sequences (seq '(1 3 2 4)) 78 (should (equal (seq-drop-while #'test-sequences-oddp seq) 79 (seq-drop seq 2))) 80 (should (equal (seq-drop-while #'test-sequences-evenp seq) 81 seq)) 82 (should (seq-empty-p (seq-drop-while #'numberp seq)))) 83 (with-test-sequences (seq '()) 84 (should (seq-empty-p (seq-drop-while #'test-sequences-oddp seq))))) 85 86 (ert-deftest test-seq-take-while () 87 (with-test-sequences (seq '(1 3 2 4)) 88 (should (equal (seq-take-while #'test-sequences-oddp seq) 89 (seq-take seq 2))) 90 (should (seq-empty-p (seq-take-while #'test-sequences-evenp seq))) 91 (should (equal (seq-take-while #'numberp seq) seq))) 92 (with-test-sequences (seq '()) 93 (should (seq-empty-p (seq-take-while #'test-sequences-oddp seq))))) 94 95 (ert-deftest test-seq-map-indexed () 96 (should (equal (seq-map-indexed (lambda (elt i) 97 (list elt i)) 98 nil) 99 nil)) 100 (should (equal (seq-map-indexed (lambda (elt i) 101 (list elt i)) 102 '(a b c d)) 103 '((a 0) (b 1) (c 2) (d 3))))) 104 105 (ert-deftest test-seq-filter () 106 (with-test-sequences (seq '(6 7 8 9 10)) 107 (should (equal (seq-filter #'test-sequences-evenp seq) '(6 8 10))) 108 (should (equal (seq-filter #'test-sequences-oddp seq) '(7 9))) 109 (should (equal (seq-filter (lambda (elt) nil) seq) '()))) 110 (with-test-sequences (seq '()) 111 (should (equal (seq-filter #'test-sequences-evenp seq) '())))) 112 113 (ert-deftest test-seq-remove () 114 (with-test-sequences (seq '(6 7 8 9 10)) 115 (should (equal (seq-remove #'test-sequences-evenp seq) '(7 9))) 116 (should (equal (seq-remove #'test-sequences-oddp seq) '(6 8 10))) 117 (should (same-contents-p (seq-remove (lambda (elt) nil) seq) seq))) 118 (with-test-sequences (seq '()) 119 (should (equal (seq-remove #'test-sequences-evenp seq) '())))) 120 121 (ert-deftest test-seq-count () 122 (with-test-sequences (seq '(6 7 8 9 10)) 123 (should (equal (seq-count #'test-sequences-evenp seq) 3)) 124 (should (equal (seq-count #'test-sequences-oddp seq) 2)) 125 (should (equal (seq-count (lambda (elt) nil) seq) 0))) 126 (with-test-sequences (seq '()) 127 (should (equal (seq-count #'test-sequences-evenp seq) 0)))) 128 129 (ert-deftest test-seq-reduce () 130 (with-test-sequences (seq '(1 2 3 4)) 131 (should (= (seq-reduce #'+ seq 0) 10)) 132 (should (= (seq-reduce #'+ seq 5) 15))) 133 (with-test-sequences (seq '()) 134 (should (eq (seq-reduce #'+ seq 0) 0)) 135 (should (eq (seq-reduce #'+ seq 7) 7)))) 136 137 (ert-deftest test-seq-some () 138 (with-test-sequences (seq '(4 3 2 1)) 139 (should (seq-some #'test-sequences-evenp seq)) 140 (should (seq-some #'test-sequences-oddp seq)) 141 (should-not (seq-some (lambda (elt) (> elt 10)) seq))) 142 (with-test-sequences (seq '()) 143 (should-not (seq-some #'test-sequences-oddp seq))) 144 (should (seq-some #'null '(1 nil 2)))) 145 146 (ert-deftest test-seq-find () 147 (with-test-sequences (seq '(4 3 2 1)) 148 (should (= 4 (seq-find #'test-sequences-evenp seq))) 149 (should (= 3 (seq-find #'test-sequences-oddp seq))) 150 (should-not (seq-find (lambda (elt) (> elt 10)) seq))) 151 (should-not (seq-find #'null '(1 nil 2))) 152 (should-not (seq-find #'null '(1 nil 2) t)) 153 (should-not (seq-find #'null '(1 2 3))) 154 (should (seq-find #'null '(1 2 3) 'sentinel))) 155 156 (ert-deftest test-seq-contains () 157 (with-test-sequences (seq '(3 4 5 6)) 158 (should (seq-contains seq 3)) 159 (should-not (seq-contains seq 7))) 160 (with-test-sequences (seq '()) 161 (should-not (seq-contains seq 3)) 162 (should-not (seq-contains seq nil)))) 163 164 (ert-deftest test-seq-every-p () 165 (with-test-sequences (seq '(43 54 22 1)) 166 (should (seq-every-p (lambda (elt) t) seq)) 167 (should-not (seq-every-p #'test-sequences-oddp seq)) 168 (should-not (seq-every-p #'test-sequences-evenp seq))) 169 (with-test-sequences (seq '(42 54 22 2)) 170 (should (seq-every-p #'test-sequences-evenp seq)) 171 (should-not (seq-every-p #'test-sequences-oddp seq))) 172 (with-test-sequences (seq '()) 173 (should (seq-every-p #'identity seq)) 174 (should (seq-every-p #'test-sequences-evenp seq)))) 175 176 (ert-deftest test-seq-empty-p () 177 (with-test-sequences (seq '(0)) 178 (should-not (seq-empty-p seq))) 179 (with-test-sequences (seq '(0 1 2)) 180 (should-not (seq-empty-p seq))) 181 (with-test-sequences (seq '()) 182 (should (seq-empty-p seq)))) 183 184 (ert-deftest test-seq-sort () 185 (should (equal (seq-sort #'< "cbaf") "abcf")) 186 (should (equal (seq-sort #'< '(2 1 9 4)) '(1 2 4 9))) 187 (should (equal (seq-sort #'< [2 1 9 4]) [1 2 4 9])) 188 (should (equal (seq-sort #'< "") ""))) 189 190 (ert-deftest test-seq-uniq () 191 (with-test-sequences (seq '(2 4 6 8 6 4 3)) 192 (should (equal (seq-uniq seq) '(2 4 6 8 3)))) 193 (with-test-sequences (seq '(3 3 3 3 3)) 194 (should (equal (seq-uniq seq) '(3)))) 195 (with-test-sequences (seq '()) 196 (should (equal (seq-uniq seq) '())))) 197 198 (ert-deftest test-seq-subseq () 199 (with-test-sequences (seq '(2 3 4 5)) 200 (should (equal (seq-subseq seq 0 4) seq)) 201 (should (same-contents-p (seq-subseq seq 2 4) '(4 5))) 202 (should (same-contents-p (seq-subseq seq 1 3) '(3 4))) 203 (should (same-contents-p (seq-subseq seq 1 -1) '(3 4)))) 204 (should (vectorp (seq-subseq [2 3 4 5] 2))) 205 (should (stringp (seq-subseq "foo" 2 3))) 206 (should (listp (seq-subseq '(2 3 4 4) 2 3))) 207 (should-error (seq-subseq '(1 2 3) 4)) 208 (should-not (seq-subseq '(1 2 3) 3)) 209 (should (seq-subseq '(1 2 3) -3)) 210 (should-error (seq-subseq '(1 2 3) 1 4)) 211 (should (seq-subseq '(1 2 3) 1 3))) 212 213 (ert-deftest test-seq-concatenate () 214 (with-test-sequences (seq '(2 4 6)) 215 (should (equal (seq-concatenate 'string seq [8]) (string 2 4 6 8))) 216 (should (equal (seq-concatenate 'list seq '(8 10)) '(2 4 6 8 10))) 217 (should (equal (seq-concatenate 'vector seq '(8 10)) [2 4 6 8 10])) 218 (should (equal (seq-concatenate 'vector nil '(8 10)) [8 10])) 219 (should (equal (seq-concatenate 'vector seq nil) [2 4 6])))) 220 221 (ert-deftest test-seq-mapcat () 222 (should (equal (seq-mapcat #'seq-reverse '((3 2 1) (6 5 4))) 223 '(1 2 3 4 5 6))) 224 (should (equal (seq-mapcat #'seq-reverse '[(3 2 1) (6 5 4)]) 225 '(1 2 3 4 5 6))) 226 (should (equal (seq-mapcat #'seq-reverse '((3 2 1) (6 5 4)) 'vector) 227 '[1 2 3 4 5 6]))) 228 229 (ert-deftest test-seq-partition () 230 (should (same-contents-p (seq-partition '(0 1 2 3 4 5 6 7) 3) 231 '((0 1 2) (3 4 5) (6 7)))) 232 (should (same-contents-p (seq-partition '[0 1 2 3 4 5 6 7] 3) 233 '([0 1 2] [3 4 5] [6 7]))) 234 (should (same-contents-p (seq-partition "Hello world" 2) 235 '("He" "ll" "o " "wo" "rl" "d"))) 236 (should (equal (seq-partition '() 2) '())) 237 (should (equal (seq-partition '(1 2 3) -1) '()))) 238 239 (ert-deftest test-seq-group-by () 240 (with-test-sequences (seq '(1 2 3 4)) 241 (should (equal (seq-group-by #'test-sequences-oddp seq) 242 '((t 1 3) (nil 2 4))))) 243 (should (equal (seq-group-by #'car '((a 1) (b 3) (c 4) (a 2))) 244 '((b (b 3)) (c (c 4)) (a (a 1) (a 2)))))) 245 246 (ert-deftest test-seq-reverse () 247 (with-test-sequences (seq '(1 2 3 4)) 248 (should (same-contents-p (seq-reverse seq) '(4 3 2 1))) 249 (should (equal (type-of (seq-reverse seq)) 250 (type-of seq))))) 251 252 (ert-deftest test-seq-into () 253 (let* ((vector [1 2 3]) 254 (list (seq-into vector 'list))) 255 (should (same-contents-p vector list)) 256 (should (listp list))) 257 (let* ((list '(hello world)) 258 (vector (seq-into list 'vector))) 259 (should (same-contents-p vector list)) 260 (should (vectorp vector))) 261 (let* ((string "hello") 262 (list (seq-into string 'list))) 263 (should (same-contents-p string list)) 264 (should (stringp string))) 265 (let* ((string "hello") 266 (vector (seq-into string 'vector))) 267 (should (same-contents-p string vector)) 268 (should (stringp string))) 269 (let* ((list nil) 270 (vector (seq-into list 'vector))) 271 (should (same-contents-p list vector)) 272 (should (vectorp vector)))) 273 274 (ert-deftest test-seq-intersection () 275 (let ((v1 [2 3 4 5]) 276 (v2 [1 3 5 6 7])) 277 (should (same-contents-p (seq-intersection v1 v2) 278 '(3 5)))) 279 (let ((l1 '(2 3 4 5)) 280 (l2 '(1 3 5 6 7))) 281 (should (same-contents-p (seq-intersection l1 l2) 282 '(3 5)))) 283 (let ((v1 [2 4 6]) 284 (v2 [1 3 5])) 285 (should (seq-empty-p (seq-intersection v1 v2))))) 286 287 (ert-deftest test-seq-difference () 288 (let ((v1 [2 3 4 5]) 289 (v2 [1 3 5 6 7])) 290 (should (same-contents-p (seq-difference v1 v2) 291 '(2 4)))) 292 (let ((l1 '(2 3 4 5)) 293 (l2 '(1 3 5 6 7))) 294 (should (same-contents-p (seq-difference l1 l2) 295 '(2 4)))) 296 (let ((v1 [2 4 6]) 297 (v2 [2 4 6])) 298 (should (seq-empty-p (seq-difference v1 v2))))) 299 300 (ert-deftest test-seq-let () 301 (with-test-sequences (seq '(1 2 3 4)) 302 (seq-let (a b c d e) seq 303 (should (= a 1)) 304 (should (= b 2)) 305 (should (= c 3)) 306 (should (= d 4)) 307 (should (null e))) 308 (seq-let (a b &rest others) seq 309 (should (= a 1)) 310 (should (= b 2)) 311 (should (same-contents-p others (seq-drop seq 2))))) 312 (let ((seq '(1 (2 (3 (4)))))) 313 (seq-let (_ (_ (_ (a)))) seq 314 (should (= a 4)))) 315 (let (seq) 316 (seq-let (a b c) seq 317 (should (null a)) 318 (should (null b)) 319 (should (null c))))) 320 321 (ert-deftest test-seq-min-max () 322 (with-test-sequences (seq '(4 5 3 2 0 4)) 323 (should (= (seq-min seq) 0)) 324 (should (= (seq-max seq) 5)))) 325 326 (ert-deftest test-seq-position () 327 (with-test-sequences (seq '(2 4 6)) 328 (should (null (seq-position seq 1))) 329 (should (= (seq-position seq 4) 1))) 330 (let ((seq '(a b c))) 331 (should (null (seq-position seq 'd #'eq))) 332 (should (= (seq-position seq 'a #'eq) 0)) 333 (should (null (seq-position seq (make-symbol "a") #'eq))))) 334 335 (ert-deftest test-seq-mapn () 336 (should-error (seq-mapn #'identity)) 337 (with-test-sequences (seq '(1 2 3 4 5 6 7)) 338 (should (equal (append seq nil) 339 (seq-mapn #'identity seq))) 340 (should (equal (seq-mapn #'1+ seq) 341 (seq-map #'1+ seq))) 342 343 (with-test-sequences (seq-2 '(10 20 30 40 50)) 344 (should (equal (seq-mapn #'+ seq seq-2) 345 '(11 22 33 44 55))) 346 (should (equal (seq-mapn #'+ seq seq-2 nil) nil))))) 347 348 (ert-deftest test-seq-sort-by () 349 (let ((seq ["x" "xx" "xxx"])) 350 (should (equal (seq-sort-by #'seq-length #'> seq) 351 ["xxx" "xx" "x"])))) 352 353 (ert-deftest test-seq-random-elt-take-all () 354 (let ((seq '(a b c d e)) 355 (elts '())) 356 (should (= 0 (length elts))) 357 (dotimes (_ 1000) 358 (let ((random-elt (seq-random-elt seq))) 359 (add-to-list 'elts 360 random-elt))) 361 (should (= 5 (length elts))))) 362 363 (ert-deftest test-seq-random-elt-signal-on-empty () 364 (should-error (seq-random-elt nil)) 365 (should-error (seq-random-elt [])) 366 (should-error (seq-random-elt ""))) 367 368 (ert-deftest test-seq-mapn-circular-lists () 369 (let ((l1 '#1=(1 . #1#))) 370 (should (equal (seq-mapn #'+ '(3 4 5 7) l1) 371 '(4 5 6 8))))) 372 373 (ert-deftest test-seq-into-and-identity () 374 (let ((lst '(1 2 3)) 375 (vec [1 2 3]) 376 (str "foo bar")) 377 (should (eq (seq-into lst 'list) lst)) 378 (should (eq (seq-into vec 'vector) vec)) 379 (should (eq (seq-into str 'string) str)))) 380 381 (provide 'seq-tests) 382 ;;; seq-tests.el ends here