Prefer oddp/evenp to free-coding them in tests

* test/lisp/emacs-lisp/bindat-tests.el (bindat-test--sint):
* test/lisp/emacs-lisp/seq-tests.el (test-seq-drop-while)
(test-seq-take-while, test-seq-filter, test-seq-remove)
(test-seq-count, test-seq-some, test-seq-find, test-seq-every-p)
(test-seq-group-by):
* test/lisp/eshell/em-pred-tests.el (eshell-with-file-attributes-from-name):
* test/lisp/filenotify-tests.el (file-notify-test07-many-events)
(file-notify-test09-watched-file-in-watched-dir):
* test/src/floatfns-tests.el (bignum-expt, bignum-round):
* test/src/undo-tests.el (undo-test4): Prefer oddp/evenp to free-coding
them.
This commit is contained in:
Stefan Kangas 2025-02-17 04:52:49 +01:00
parent 657f4658a7
commit 5ce746c3b0
6 changed files with 33 additions and 41 deletions

View file

@ -125,7 +125,7 @@
(ert-deftest bindat-test--sint ()
(dotimes (kind 32)
(let ((bitlen (* 8 (/ kind 2)))
(r (zerop (% kind 2))))
(r (evenp kind)))
(dotimes (_ 100)
(let* ((n (random (ash 1 bitlen)))
(i (- n (ash 1 (1- bitlen))))

View file

@ -49,14 +49,6 @@ Evaluate BODY for each created sequence.
"Return t if SEQ1 and SEQ2 have the same contents, nil otherwise."
(equal (append seq1 '()) (append seq2 '())))
(defun test-sequences-evenp (integer)
"Return t if INTEGER is even."
(eq (logand integer 1) 0))
(defun test-sequences-oddp (integer)
"Return t if INTEGER is odd."
(not (test-sequences-evenp integer)))
(ert-deftest test-setf-seq-elt ()
(with-test-sequences (seq '(1 2 3))
(setf (seq-elt seq 1) 4)
@ -83,22 +75,22 @@ Evaluate BODY for each created sequence.
(ert-deftest test-seq-drop-while ()
(with-test-sequences (seq '(1 3 2 4))
(should (equal (seq-drop-while #'test-sequences-oddp seq)
(should (equal (seq-drop-while #'oddp seq)
(seq-drop seq 2)))
(should (equal (seq-drop-while #'test-sequences-evenp seq)
(should (equal (seq-drop-while #'evenp seq)
seq))
(should (seq-empty-p (seq-drop-while #'numberp seq))))
(with-test-sequences (seq '())
(should (seq-empty-p (seq-drop-while #'test-sequences-oddp seq)))))
(should (seq-empty-p (seq-drop-while #'oddp seq)))))
(ert-deftest test-seq-take-while ()
(with-test-sequences (seq '(1 3 2 4))
(should (equal (seq-take-while #'test-sequences-oddp seq)
(should (equal (seq-take-while #'oddp seq)
(seq-take seq 2)))
(should (seq-empty-p (seq-take-while #'test-sequences-evenp seq)))
(should (seq-empty-p (seq-take-while #'evenp seq)))
(should (equal (seq-take-while #'numberp seq) seq)))
(with-test-sequences (seq '())
(should (seq-empty-p (seq-take-while #'test-sequences-oddp seq)))))
(should (seq-empty-p (seq-take-while #'oddp seq)))))
(ert-deftest test-seq-map-indexed ()
(should (equal (seq-map-indexed (lambda (elt i)
@ -123,19 +115,19 @@ Evaluate BODY for each created sequence.
(ert-deftest test-seq-filter ()
(with-test-sequences (seq '(6 7 8 9 10))
(should (equal (seq-filter #'test-sequences-evenp seq) '(6 8 10)))
(should (equal (seq-filter #'test-sequences-oddp seq) '(7 9)))
(should (equal (seq-filter #'evenp seq) '(6 8 10)))
(should (equal (seq-filter #'oddp seq) '(7 9)))
(should (equal (seq-filter (lambda (_) nil) seq) '())))
(with-test-sequences (seq '())
(should (equal (seq-filter #'test-sequences-evenp seq) '()))))
(should (equal (seq-filter #'evenp seq) '()))))
(ert-deftest test-seq-remove ()
(with-test-sequences (seq '(6 7 8 9 10))
(should (equal (seq-remove #'test-sequences-evenp seq) '(7 9)))
(should (equal (seq-remove #'test-sequences-oddp seq) '(6 8 10)))
(should (equal (seq-remove #'evenp seq) '(7 9)))
(should (equal (seq-remove #'oddp seq) '(6 8 10)))
(should (same-contents-p (seq-remove (lambda (_) nil) seq) seq)))
(with-test-sequences (seq '())
(should (equal (seq-remove #'test-sequences-evenp seq) '()))))
(should (equal (seq-remove #'evenp seq) '()))))
(ert-deftest test-seq-remove-at-position ()
(with-test-sequences (seq '(1 2 3 4))
@ -147,11 +139,11 @@ Evaluate BODY for each created sequence.
(ert-deftest test-seq-count ()
(with-test-sequences (seq '(6 7 8 9 10))
(should (equal (seq-count #'test-sequences-evenp seq) 3))
(should (equal (seq-count #'test-sequences-oddp seq) 2))
(should (equal (seq-count #'evenp seq) 3))
(should (equal (seq-count #'oddp seq) 2))
(should (equal (seq-count (lambda (_) nil) seq) 0)))
(with-test-sequences (seq '())
(should (equal (seq-count #'test-sequences-evenp seq) 0))))
(should (equal (seq-count #'evenp seq) 0))))
(ert-deftest test-seq-reduce ()
(with-test-sequences (seq '(1 2 3 4))
@ -163,17 +155,17 @@ Evaluate BODY for each created sequence.
(ert-deftest test-seq-some ()
(with-test-sequences (seq '(4 3 2 1))
(should (seq-some #'test-sequences-evenp seq))
(should (seq-some #'test-sequences-oddp seq))
(should (seq-some #'evenp seq))
(should (seq-some #'oddp seq))
(should-not (seq-some (lambda (elt) (> elt 10)) seq)))
(with-test-sequences (seq '())
(should-not (seq-some #'test-sequences-oddp seq)))
(should-not (seq-some #'oddp seq)))
(should (seq-some #'null '(1 nil 2))))
(ert-deftest test-seq-find ()
(with-test-sequences (seq '(4 3 2 1))
(should (= 4 (seq-find #'test-sequences-evenp seq)))
(should (= 3 (seq-find #'test-sequences-oddp seq)))
(should (= 4 (seq-find #'evenp seq)))
(should (= 3 (seq-find #'oddp seq)))
(should-not (seq-find (lambda (elt) (> elt 10)) seq)))
(should-not (seq-find #'null '(1 nil 2)))
(should-not (seq-find #'null '(1 nil 2) t))
@ -209,14 +201,14 @@ Evaluate BODY for each created sequence.
(ert-deftest test-seq-every-p ()
(with-test-sequences (seq '(43 54 22 1))
(should (seq-every-p (lambda (_) t) seq))
(should-not (seq-every-p #'test-sequences-oddp seq))
(should-not (seq-every-p #'test-sequences-evenp seq)))
(should-not (seq-every-p #'oddp seq))
(should-not (seq-every-p #'evenp seq)))
(with-test-sequences (seq '(42 54 22 2))
(should (seq-every-p #'test-sequences-evenp seq))
(should-not (seq-every-p #'test-sequences-oddp seq)))
(should (seq-every-p #'evenp seq))
(should-not (seq-every-p #'oddp seq)))
(with-test-sequences (seq '())
(should (seq-every-p #'identity seq))
(should (seq-every-p #'test-sequences-evenp seq))))
(should (seq-every-p #'evenp seq))))
(ert-deftest test-seq-set-equal-p ()
(with-test-sequences (seq1 '(1 2 3))
@ -340,7 +332,7 @@ Evaluate BODY for each created sequence.
(ert-deftest test-seq-group-by ()
(with-test-sequences (seq '(1 2 3 4))
(should (equal (seq-group-by #'test-sequences-oddp seq)
(should (equal (seq-group-by #'oddp seq)
'((t 1 3) (nil 2 4)))))
(should (equal (seq-group-by #'car '((a 1) (b 3) (c 4) (a 2)))
'((b (b 3)) (c (c 4)) (a (a 1) (a 2))))))

View file

@ -142,7 +142,7 @@ behavior for real files.
(let ((attrs (eshell-parse-file-name-attributes file)))
;; For simplicity, just return whether the file is
;; world-executable.
(= (logand (or (alist-get 'modes attrs) 0) 1) 1)))))
(oddp (or (alist-get 'modes attrs) 0))))))
,@body))
;;; Tests:

View file

@ -1241,7 +1241,7 @@ delivered."
;; It matters which direction we rename, at least for
;; kqueue. This backend parses directories in alphabetic
;; order (x%d before y%d). So we rename into both directions.
(if (zerop (mod i 2))
(if (evenp i)
(progn
(push (expand-file-name (format "x%d" i)) source-file-list)
(push (expand-file-name (format "y%d" i)) target-file-list))
@ -1469,7 +1469,7 @@ the file watch."
(make-list (/ n 2) 'created)))
(dotimes (i n)
(file-notify--test-wait-event)
(if (zerop (mod i 2))
(if (evenp i)
(write-region
"any text" nil file-notify--test-tmpfile1 t 'no-message)
(let ((file-notify--test-tmpdir file-notify--test-tmpfile))

View file

@ -120,7 +120,7 @@
-2 -1 0 1 2))
(should (or (<= n 0) (= (expt 0 n) 0)))
(should (= (expt 1 n) 1))
(should (or (< n 0) (= (expt -1 n) (if (zerop (logand n 1)) 1 -1))))
(should (or (< n 0) (= (expt -1 n) (if (evenp n) 1 -1))))
(should (= (expt n 0) 1))
(should (= (expt n 1) n))
(should (= (expt n 2) (* n n)))
@ -167,7 +167,7 @@
(should (if (zerop r)
(= 0 cdelta fdelta rdelta)
(or (/= cdelta fdelta)
(zerop (% (round n d) 2)))))))))))
(evenp (round n d)))))))))))
(ert-deftest special-round ()
(dolist (f '(ceiling floor round truncate))

View file

@ -150,7 +150,7 @@
(with-temp-buffer
(buffer-enable-undo)
(dotimes (i 1048576)
(if (zerop (% i 2))
(if (evenp i)
(insert "Evenses")
(insert "Oddses")))
(undo-boundary)