]> git.eshelyaron.com Git - emacs.git/commitdiff
Prefer oddp/evenp to free-coding them in tests
authorStefan Kangas <stefankangas@gmail.com>
Mon, 17 Feb 2025 03:52:49 +0000 (04:52 +0100)
committerEshel Yaron <me@eshelyaron.com>
Tue, 18 Feb 2025 08:50:30 +0000 (09:50 +0100)
* 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
test/lisp/emacs-lisp/seq-tests.el
test/lisp/eshell/em-pred-tests.el
test/lisp/filenotify-tests.el
test/src/floatfns-tests.el
test/src/undo-tests.el

index f5dacfeaa7741e35721b9a4fd946b314929ae5d3..78021f0bc140f28ea56b278bb6a8a4b68c0ea9fd 100644 (file)
 (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))))
index ddca2d7ed9391973308becd0f7540d9f7ca8fea1..a8ed51e8e70c0f388d9cf8927343dcf3bab0d200 100644 (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))))))
index db8ce2c45a3bd4810db3760ad573cbe5511bcf20..a050c6426e91fb61fb0823b33656a07fa483add5 100644 (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:
index 4b4f054bb335ce6692f81f363e56397d2c267511..8744dc4987aa4e6f2a1ad3a757867560c19386de 100644 (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))
index 6820130a01746175697b740010407e71653622e1..04ee97a6137a5fd991d035335450023a2be5e469 100644 (file)
                    -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)))
             (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))
index 6150e09c52e232cfc7028dbafd8929aab8d0ea21..c0984c13ea69c567c66d6761493b9859a2b30217 100644 (file)
   (with-temp-buffer
     (buffer-enable-undo)
     (dotimes (i 1048576)
-      (if (zerop (% i 2))
+      (if (evenp i)
           (insert "Evenses")
         (insert "Oddses")))
     (undo-boundary)