diff options
| author | Nicolas Petton | 2014-12-16 18:42:30 -0500 |
|---|---|---|
| committer | Stefan Monnier | 2014-12-16 18:42:30 -0500 |
| commit | cf0773272587ba131d9ea5825c8c3fc782713890 (patch) | |
| tree | 4d291d792a2fc9c92e46d32ed5b64e9fc3f87ad0 /test | |
| parent | 005b86c0d061dab4279c74c45368a557733433a1 (diff) | |
| download | emacs-cf0773272587ba131d9ea5825c8c3fc782713890.tar.gz emacs-cf0773272587ba131d9ea5825c8c3fc782713890.zip | |
* lisp/emacs-lisp/seq.el: New file.
* doc/lispref/sequences.texi (Seq Library): Add documentation for seq.el.
* test/automated/seq-tests.el: New file.
Diffstat (limited to 'test')
| -rw-r--r-- | test/ChangeLog | 4 | ||||
| -rw-r--r-- | test/automated/seq-tests.el | 196 |
2 files changed, 200 insertions, 0 deletions
diff --git a/test/ChangeLog b/test/ChangeLog index 78a03d1b335..86703d569f4 100644 --- a/test/ChangeLog +++ b/test/ChangeLog | |||
| @@ -1,3 +1,7 @@ | |||
| 1 | 2014-12-16 Nicolas Petton <petton.nicolas@gmail.com> | ||
| 2 | |||
| 3 | * automated/seq-tests.el: New file. | ||
| 4 | |||
| 1 | 2014-12-16 Glenn Morris <rgm@gnu.org> | 5 | 2014-12-16 Glenn Morris <rgm@gnu.org> |
| 2 | 6 | ||
| 3 | * automated/data/flymake/Makefile (check-syntax): | 7 | * automated/data/flymake/Makefile (check-syntax): |
diff --git a/test/automated/seq-tests.el b/test/automated/seq-tests.el new file mode 100644 index 00000000000..a46438e53eb --- /dev/null +++ b/test/automated/seq-tests.el | |||
| @@ -0,0 +1,196 @@ | |||
| 1 | ;;; seq-tests.el --- Tests for sequences.el | ||
| 2 | |||
| 3 | ;; Copyright (C) 2014 Free Software Foundation, Inc. | ||
| 4 | |||
| 5 | ;; Author: Nicolas Petton <petton.nicolas@gmail.com> | ||
| 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 <http://www.gnu.org/licenses/>. | ||
| 22 | |||
| 23 | ;;; Commentary: | ||
| 24 | |||
| 25 | ;; Tests for sequences.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-filter () | ||
| 96 | (with-test-sequences (seq '(6 7 8 9 10)) | ||
| 97 | (should (equal (seq-filter #'test-sequences-evenp seq) '(6 8 10))) | ||
| 98 | (should (equal (seq-filter #'test-sequences-oddp seq) '(7 9))) | ||
| 99 | (should (equal (seq-filter (lambda (elt) nil) seq) '()))) | ||
| 100 | (with-test-sequences (seq '()) | ||
| 101 | (should (equal (seq-filter #'test-sequences-evenp seq) '())))) | ||
| 102 | |||
| 103 | (ert-deftest test-seq-remove () | ||
| 104 | (with-test-sequences (seq '(6 7 8 9 10)) | ||
| 105 | (should (equal (seq-remove #'test-sequences-evenp seq) '(7 9))) | ||
| 106 | (should (equal (seq-remove #'test-sequences-oddp seq) '(6 8 10))) | ||
| 107 | (should (same-contents-p (seq-remove (lambda (elt) nil) seq) seq))) | ||
| 108 | (with-test-sequences (seq '()) | ||
| 109 | (should (equal (seq-remove #'test-sequences-evenp seq) '())))) | ||
| 110 | |||
| 111 | (ert-deftest test-seq-count () | ||
| 112 | (with-test-sequences (seq '(6 7 8 9 10)) | ||
| 113 | (should (equal (seq-count #'test-sequences-evenp seq) 3)) | ||
| 114 | (should (equal (seq-count #'test-sequences-oddp seq) 2)) | ||
| 115 | (should (equal (seq-count (lambda (elt) nil) seq) 0))) | ||
| 116 | (with-test-sequences (seq '()) | ||
| 117 | (should (equal (seq-count #'test-sequences-evenp seq) 0)))) | ||
| 118 | |||
| 119 | (ert-deftest test-seq-reduce () | ||
| 120 | (with-test-sequences (seq '(1 2 3 4)) | ||
| 121 | (should (= (seq-reduce #'+ seq 0) 10)) | ||
| 122 | (should (= (seq-reduce #'+ seq 5) 15))) | ||
| 123 | (with-test-sequences (seq '()) | ||
| 124 | (should (eq (seq-reduce #'+ seq 0) 0)) | ||
| 125 | (should (eq (seq-reduce #'+ seq 7) 7)))) | ||
| 126 | |||
| 127 | (ert-deftest test-seq-some-p () | ||
| 128 | (with-test-sequences (seq '(4 3 2 1)) | ||
| 129 | (should (= (seq-some-p #'test-sequences-evenp seq) 4)) | ||
| 130 | (should (= (seq-some-p #'test-sequences-oddp seq) 3)) | ||
| 131 | (should-not (seq-some-p (lambda (elt) (> elt 10)) seq))) | ||
| 132 | (with-test-sequences (seq '()) | ||
| 133 | (should-not (seq-some-p #'test-sequences-oddp seq)))) | ||
| 134 | |||
| 135 | (ert-deftest test-seq-contains-p () | ||
| 136 | (with-test-sequences (seq '(3 4 5 6)) | ||
| 137 | (should (seq-contains-p seq 3)) | ||
| 138 | (should-not (seq-contains-p seq 7))) | ||
| 139 | (with-test-sequences (seq '()) | ||
| 140 | (should-not (seq-contains-p seq 3)) | ||
| 141 | (should-not (seq-contains-p seq nil)))) | ||
| 142 | |||
| 143 | (ert-deftest test-seq-every-p () | ||
| 144 | (with-test-sequences (seq '(43 54 22 1)) | ||
| 145 | (should (seq-every-p (lambda (elt) t) seq)) | ||
| 146 | (should-not (seq-every-p #'test-sequences-oddp seq)) | ||
| 147 | (should-not (seq-every-p #'test-sequences-evenp seq))) | ||
| 148 | (with-test-sequences (seq '(42 54 22 2)) | ||
| 149 | (should (seq-every-p #'test-sequences-evenp seq)) | ||
| 150 | (should-not (seq-every-p #'test-sequences-oddp seq))) | ||
| 151 | (with-test-sequences (seq '()) | ||
| 152 | (should (seq-every-p #'identity seq)) | ||
| 153 | (should (seq-every-p #'test-sequences-evenp seq)))) | ||
| 154 | |||
| 155 | (ert-deftest test-seq-empty-p () | ||
| 156 | (with-test-sequences (seq '(0)) | ||
| 157 | (should-not (seq-empty-p seq))) | ||
| 158 | (with-test-sequences (seq '(0 1 2)) | ||
| 159 | (should-not (seq-empty-p seq))) | ||
| 160 | (with-test-sequences (seq '()) | ||
| 161 | (should (seq-empty-p seq)))) | ||
| 162 | |||
| 163 | (ert-deftest test-seq-sort () | ||
| 164 | (should (equal (seq-sort #'< "cbaf") "abcf")) | ||
| 165 | (should (equal (seq-sort #'< '(2 1 9 4)) '(1 2 4 9))) | ||
| 166 | (should (equal (seq-sort #'< [2 1 9 4]) [1 2 4 9])) | ||
| 167 | (should (equal (seq-sort #'< "") ""))) | ||
| 168 | |||
| 169 | (ert-deftest test-seq-uniq () | ||
| 170 | (with-test-sequences (seq '(2 4 6 8 6 4 3)) | ||
| 171 | (should (equal (seq-uniq seq) '(2 4 6 8 3)))) | ||
| 172 | (with-test-sequences (seq '(3 3 3 3 3)) | ||
| 173 | (should (equal (seq-uniq seq) '(3)))) | ||
| 174 | (with-test-sequences (seq '()) | ||
| 175 | (should (equal (seq-uniq seq) '())))) | ||
| 176 | |||
| 177 | (ert-deftest test-seq-subseq () | ||
| 178 | (with-test-sequences (seq '(2 3 4 5)) | ||
| 179 | (should (equal (seq-subseq seq 0 4) seq)) | ||
| 180 | (should (same-contents-p (seq-subseq seq 2 4) '(4 5))) | ||
| 181 | (should (same-contents-p (seq-subseq seq 1 3) '(3 4))) | ||
| 182 | (should (same-contents-p (seq-subseq seq 1 -1) '(3 4)))) | ||
| 183 | (should (vectorp (seq-subseq [2 3 4 5] 2))) | ||
| 184 | (should (stringp (seq-subseq "foo" 2 3))) | ||
| 185 | (should (listp (seq-subseq '(2 3 4 4) 2 3)))) | ||
| 186 | |||
| 187 | (ert-deftest test-seq-concatenate () | ||
| 188 | (with-test-sequences (seq '(2 4 6)) | ||
| 189 | (should (equal (seq-concatenate 'string seq [8]) (string 2 4 6 8))) | ||
| 190 | (should (equal (seq-concatenate 'list seq '(8 10)) '(2 4 6 8 10))) | ||
| 191 | (should (equal (seq-concatenate 'vector seq '(8 10)) [2 4 6 8 10])) | ||
| 192 | (should (equal (seq-concatenate 'vector nil '(8 10)) [8 10])) | ||
| 193 | (should (equal (seq-concatenate 'vector seq nil) [2 4 6])))) | ||
| 194 | |||
| 195 | (provide 'seq-tests) | ||
| 196 | ;;; seq-tests.el ends here | ||