branch: elpa/el-job
commit e90fd07c3c25399e77b9d8b0301e7f0937b251f1
Author: Martin Edström <meedstro...@gmail.com>
Commit: Martin Edström <meedstro...@gmail.com>

    Add a test
---
 el-job-test.el | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 el-job.el      | 15 ++++++---------
 2 files changed, 62 insertions(+), 9 deletions(-)

diff --git a/el-job-test.el b/el-job-test.el
new file mode 100644
index 0000000000..79cb3c6947
--- /dev/null
+++ b/el-job-test.el
@@ -0,0 +1,56 @@
+;;; el-job-test.el --- Test suite -*- lexical-binding: t; -*-
+
+;; Copyright (C) 2024 Martin Edström
+
+;; This program is free software: you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; This program is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+;;; Code:
+
+(require 'ert)
+(require 'map)
+(require 'cl-lib)
+(require 'el-job)
+
+(ert-deftest el-job--split-optimally ()
+  ;; All benchmarks at zero
+  (let* ((plist (cl-loop for i below 50
+                         nconc (list (format "file-%d.org" i)
+                                     (time-convert 0 t))))
+         (items (map-keys plist))
+         (big-table (map-into (take 100 plist) '(hash-table :test equal)))
+         (tiny-table (map-into (take 10 plist) '(hash-table :test equal)))
+         (empty-table (make-hash-table :test #'equal)))
+    (should (>= 15 (length (el-job--split-optimally items 15 big-table))))
+    (should (>= 15 (length (el-job--split-optimally items 15 tiny-table))))
+    (should (= 15 (length (el-job--split-optimally items 15 empty-table))))
+    (should (= 5 (length (el-job--split-optimally (take 5 items) 15 
big-table))))
+    (should (= 5 (length (el-job--split-optimally (take 5 items) 15 
tiny-table))))
+    (should (= 5 (length (el-job--split-optimally (take 5 items) 15 
empty-table)))))
+
+  ;; All benchmarks at random nonzero duration (up to 2.0s)
+  (let* ((plist (cl-loop for i below 50
+                         nconc (list (format "file-%d.org" i)
+                                     (time-convert (/ 2.0 (1+ (random 100))) 
t))))
+         (items (map-keys plist))
+         (big-table (map-into (take 100 plist) '(hash-table :test equal)))
+         (tiny-table (map-into (take 10 plist) '(hash-table :test equal)))
+         (empty-table (make-hash-table :test #'equal)))
+    (should (>= 15 (length (el-job--split-optimally items 15 big-table))))
+    (should (>= 15 (length (el-job--split-optimally items 15 tiny-table))))
+    (should (= 15 (length (el-job--split-optimally items 15 empty-table))))
+    (should (= 5 (length (el-job--split-optimally (take 5 items) 15 
big-table))))
+    (should (= 5 (length (el-job--split-optimally (take 5 items) 15 
tiny-table))))
+    (should (= 5 (length (el-job--split-optimally (take 5 items) 15 
empty-table))))))
+
+;;; el-job-test.el ends here
diff --git a/el-job.el b/el-job.el
index cae92db00f..9b209ab92c 100644
--- a/el-job.el
+++ b/el-job.el
@@ -148,7 +148,6 @@ Note: if you are currently editing the source code for 
FEATURE, use
       loaded)))
 
 ;; TODO: Never accept nil as a benchmarked input
-;; TODO: Write some tests (make a fake table)
 (defun el-job--split-optimally (items n table)
   "Split ITEMS into up to N lists of items.
 
@@ -182,7 +181,6 @@ being saddled with a mega-item in addition to the average 
workload."
               this-sublist
               untimed
               dur)
-          ;; Loop over items again now that we have the total...
           (catch 'filled
             (while-let ((item (pop items)))
               (if (length= sublists n)
@@ -205,12 +203,12 @@ being saddled with a mega-item in addition to the average 
workload."
                       (setq this-sublist-sum 0)
                       (setq this-sublist nil)
                       (push item items)))))))
-          ;; If last sublist did not hit the max, let it absorb any remaining
-          ;; items.  (Sloppy as there could be a lot in special cases, but
-          ;; benchmarks should make it moot for next time.)
           (if this-sublist
+              ;; Last sublist did not max out, let it absorb all remaining
+              ;; items.  (Sloppy, as there could be a lot in special cases, but
+              ;; benchmarks should make it moot for next time.)
               (push (nconc untimed items this-sublist) sublists)
-            ;; Last sublist already hit time limit, spread leftovers equally
+            ;; Last sublist already hit max, spread leftovers equally
             (let ((ctr 0)
                   (len (length sublists)))
               (if (= len 0)
@@ -786,9 +784,8 @@ Prevent its sentinel and filter from reacting."
   (let ((id (intern (completing-read "Get info on job: " el-jobs))))
     (with-current-buffer (get-buffer-create "*el-job*" t)
       (erase-buffer)
-      (cl-prin1 (gethash id el-jobs) (current-buffer))
-      (pp-buffer)
-      (switch-to-buffer (current-buffer)))
+      (switch-to-buffer (current-buffer))
+      (cl-prin1 (gethash id el-jobs) (current-buffer)))
     t))
 
 (defun el-job--await (id timeout &optional message)

Reply via email to