branch: externals/el-job
commit 9ac5c57966fca58e08a99c9fd04ff6139b4916ae
Author: Martin Edström <[email protected]>
Commit: Martin Edström <[email protected]>
Add tests for split-{optimally,evenly}
---
el-job-test.el | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 62 insertions(+)
diff --git a/el-job-test.el b/el-job-test.el
index c9b40848e7..0f4a5cc59f 100644
--- a/el-job-test.el
+++ b/el-job-test.el
@@ -49,6 +49,68 @@
(equal (mapcar #'upcase nonsense)
(el-job-parallel-mapcar #'upcase nonsense))))
+(cl-defun test-split-optimally (items n &optional (table (make-hash-table
:test 'equal)))
+ (let ((sublists (el-job-ng--split-optimally items n table)))
+ (should (equal items (apply 'append sublists)))
+ (should-not (> (length sublists) n))
+ (should-not (memq nil sublists))
+ sublists))
+
+(defun test-split-evenly (items n &optional _)
+ (let ((sublists (el-job-ng--split-evenly items n)))
+ (should (equal items (apply 'append sublists)))
+ (should-not (> (length sublists) n))
+ (should-not (memq nil sublists))
+ sublists))
+
+(ert-deftest splitting ()
+ (should-error (test-split-evenly '(1 2 3) 0))
+ (should-error (test-split-optimally '(1 2 3) 0))
+ (test-split-optimally nil 5)
+
+ (test-split-evenly '(1 2 3 4 5 6) 4) ;; Even, lesser even
+ (test-split-evenly '(1 2 3 4 5 6) 5) ;; Even, lesser odd
+ (test-split-evenly '(1 2 3 4 5 6) 6) ;; Even, equal
+ (test-split-evenly '(1 2 3 4 5 6) 7) ;; Even, greater odd
+ (test-split-evenly '(1 2 3 4 5 6) 8) ;; Even, greater even
+ (test-split-evenly '(1 2 3 4 5) 3) ;; Odd, lesser odd
+ (test-split-evenly '(1 2 3 4 5) 4) ;; Odd, lesser even
+ (test-split-evenly '(1 2 3 4 5) 5) ;; Odd, equal
+ (test-split-evenly '(1 2 3 4 5) 6) ;; Odd, greater even
+ (test-split-evenly '(1 2 3 4 5) 7) ;; Odd, greater odd
+
+ (let* ((alist ;; Simulated benchmarks
+ (cl-loop
+ for i below 50
+ collect (cons (format "filename-%d.org" i)
+ (seconds-to-time
+ ;; (/ (random 1000) 100.0)
+ (- (expt 1.2 (/ 10.0 (1+ (random 100)))) 1)
+ ;; (/ 4.0 (1+ (random 10)))
+ ))))
+ (items (map-keys alist))
+ (big-table (map-into (take 100 alist) '(hash-table :test equal)))
+ (tiny-table (map-into (take 10 alist) '(hash-table :test equal)))
+ (empty-table (make-hash-table :test #'equal)))
+ ;; (cl-loop for sublist in (test-split-optimally items 15 big-table)
+ ;; collect (cons (gethash (car sublist) big-table) sublist))
+ (should (>= 15 (length (test-split-optimally items 15 big-table))))
+ (should (>= 15 (length (test-split-optimally items 15 tiny-table))))
+ (should (= 5 (length (test-split-optimally (take 5 items) 15 big-table))))
+ (should (= 5 (length (test-split-optimally (take 5 items) 15 tiny-table))))
+ (should (= 5 (length (test-split-optimally (take 5 items) 15
empty-table)))))
+
+ ;; (let ((table (map-into (list "a" (seconds-to-time 0.00003214)
+ ;; "b" (seconds-to-time 2.002)
+ ;; "c" (seconds-to-time 0.5)
+ ;; "d" (seconds-to-time 0.5)
+ ;; "e" (seconds-to-time 0.5)
+ ;; "f" (seconds-to-time 0.5)
+ ;; "g" (seconds-to-time 0.5)
+ ;; "h" (seconds-to-time 0.05))
+ ;; '(hash-table :test equal)))))
+
+ )
(ert-deftest el-job-old--split-optimally ()
;; All benchmarks at zero