From 273ee7369f085fdb78b7a0f5ed5801285e983560 Mon Sep 17 00:00:00 2001 From: Stefan Kangas Date: Mon, 17 Feb 2025 04:52:49 +0100 Subject: [PATCH] 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. (cherry picked from commit 5ce746c3b0fa3a0b73796182960ff06cf3f31473) --- test/lisp/emacs-lisp/bindat-tests.el | 2 +- test/lisp/emacs-lisp/seq-tests.el | 60 ++++++++++++---------------- test/lisp/eshell/em-pred-tests.el | 2 +- test/lisp/filenotify-tests.el | 4 +- test/src/floatfns-tests.el | 4 +- test/src/undo-tests.el | 2 +- 6 files changed, 33 insertions(+), 41 deletions(-) diff --git a/test/lisp/emacs-lisp/bindat-tests.el b/test/lisp/emacs-lisp/bindat-tests.el index f5dacfeaa77..78021f0bc14 100644 --- a/test/lisp/emacs-lisp/bindat-tests.el +++ b/test/lisp/emacs-lisp/bindat-tests.el @@ -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)))) diff --git a/test/lisp/emacs-lisp/seq-tests.el b/test/lisp/emacs-lisp/seq-tests.el index ddca2d7ed93..a8ed51e8e70 100644 --- a/test/lisp/emacs-lisp/seq-tests.el +++ b/test/lisp/emacs-lisp/seq-tests.el @@ -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)))))) diff --git a/test/lisp/eshell/em-pred-tests.el b/test/lisp/eshell/em-pred-tests.el index db8ce2c45a3..a050c6426e9 100644 --- a/test/lisp/eshell/em-pred-tests.el +++ b/test/lisp/eshell/em-pred-tests.el @@ -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: diff --git a/test/lisp/filenotify-tests.el b/test/lisp/filenotify-tests.el index 4b4f054bb33..8744dc4987a 100644 --- a/test/lisp/filenotify-tests.el +++ b/test/lisp/filenotify-tests.el @@ -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)) diff --git a/test/src/floatfns-tests.el b/test/src/floatfns-tests.el index 6820130a017..04ee97a6137 100644 --- a/test/src/floatfns-tests.el +++ b/test/src/floatfns-tests.el @@ -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)) diff --git a/test/src/undo-tests.el b/test/src/undo-tests.el index 6150e09c52e..c0984c13ea6 100644 --- a/test/src/undo-tests.el +++ b/test/src/undo-tests.el @@ -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) -- 2.39.5