]> git.eshelyaron.com Git - emacs.git/commitdiff
Add more sorting unit tests
authorAndrew G Cohen <cohen@andy.bu.edu>
Thu, 17 Mar 2022 08:50:11 +0000 (16:50 +0800)
committerAndrew G Cohen <cohen@andy.bu.edu>
Thu, 17 Mar 2022 08:50:11 +0000 (16:50 +0800)
* test/src/fns-tests.el (fns-tests-sort): New sorting unit tests.

test/src/fns-tests.el

index 723ef4c710f7b074c434266db2d35b84b9c3ab48..2623b4c1b93b03a10df9308460ccb29bfc01c2dc 100644 (file)
                 [-1 2 3 4 5 5 7 8 9]))
   (should (equal (sort (vector 9 5 2 -1 5 3 8 7 4) (lambda (x y) (> x y)))
                 [9 8 7 5 5 4 3 2 -1]))
+  ;; Sort a reversed list and vector.
+  (should (equal
+        (sort (reverse (number-sequence 1 1000)) (lambda (x y) (< x y)))
+        (number-sequence 1 1000)))
+  (should (equal
+          (sort (reverse (vconcat (number-sequence 1 1000)))
+                 (lambda (x y) (< x y)))
+        (vconcat (number-sequence 1 1000))))
+  ;; Sort a constant list and vector.
+  (should (equal
+           (sort (make-vector 100 1) (lambda (x y) (> x y)))
+           (make-vector 100 1)))
+  (should (equal
+           (sort (append (make-vector 100 1) nil) (lambda (x y) (> x y)))
+           (append (make-vector 100 1) nil)))
+  ;; sort a long list and vector with every pair reversed.
+  (let ((vec (make-vector 100000 nil))
+        (logxor-vec (make-vector 100000 nil)))
+    (dotimes (i 100000)
+      (aset logxor-vec i  (logxor i 1))
+      (aset vec i i))
+    (should (equal
+             (sort logxor-vec (lambda (x y) (< x y)))
+             vec))
+    (should (equal
+             (sort (append logxor-vec nil) (lambda (x y) (< x y)))
+             (append vec nil))))
+  ;; sort a list and vector with seven swaps
+  (let ((vec (make-vector 100 nil))
+        (swap-vec (make-vector 100 nil)))
+    (dotimes (i 100)
+      (aset vec i (- i 50))
+      (aset swap-vec i (- i 50)))
+    (mapc (lambda (p)
+       (let ((tmp (elt swap-vec (car p))))
+         (aset swap-vec (car p) (elt swap-vec (cdr p)))
+         (aset swap-vec (cdr p) tmp)))
+          '((48 . 94) (75 . 77) (33 . 41) (92 . 52)
+            (10 . 96) (1 . 14) (43 . 81)))
+    (should (equal
+             (sort (copy-sequence swap-vec) (lambda (x y) (< x y)))
+             vec))
+    (should (equal
+             (sort (append swap-vec nil) (lambda (x y) (< x y)))
+             (append vec nil))))
+  ;; Check for sorting stability.
   (should (equal
           (sort
            (vector