summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2019-08-16 16:25:02 -0700
committerPaul Eggert <eggert@cs.ucla.edu>2019-08-16 16:27:27 -0700
commitf9fd12a30b3d94eb48f7b309907d136d7b2682ac (patch)
treed00aa93beec6eb1013127ee251342f39f135a994 /test
parente82923c817159c751aa9c902093a46b9457e8499 (diff)
downloademacs-f9fd12a30b3d94eb48f7b309907d136d7b2682ac.tar.gz
Fix time-add rounding bug
Without this fix, time arithmetic yielded results that were not mathematically accurate, even though the exact results were representable; for example, (time-add 0 1e-13) yielded a timestamp equal to 0 instead of to 1e-13. * lisp/timezone.el (timezone-time-from-absolute): Let time-add do its thing rather than using floating point internally, which has rounding errors. We now have bignums and so don’t need floating point to avoid overflow issues. * src/timefns.c (timeform_sub_ps_p): New function. (time_arith): If either argument is a float, represent the result exactly instead of discarding sub-ps info. * test/lisp/timezone-tests.el (timezone-tests-time-from-absolute): Don’t assume (HI LO US PS) timestamp format. * test/src/emacs-module-tests.el (mod-test-add-nanosecond/valid): Don’t assume that time-add discards sub-ns info. * test/src/timefns-tests.el (time-rounding-tests): Add regression test to detect time-add rounding bug.
Diffstat (limited to 'test')
-rw-r--r--test/lisp/timezone-tests.el7
-rw-r--r--test/src/emacs-module-tests.el7
-rw-r--r--test/src/timefns-tests.el3
3 files changed, 12 insertions, 5 deletions
diff --git a/test/lisp/timezone-tests.el b/test/lisp/timezone-tests.el
index 4b5f5617ecd..c374042fa5d 100644
--- a/test/lisp/timezone-tests.el
+++ b/test/lisp/timezone-tests.el
@@ -135,9 +135,10 @@
(should (equal (timezone-zone-to-minute "*invalid*") 0)))
(ert-deftest timezone-tests-time-from-absolute ()
- (should (equal (timezone-time-from-absolute (* 2020 365) ; Jan 1 2020
- (* 12 60 60)) ; 12:00
- '(23911 48704 0 0))))
+ (should (time-equal-p
+ (timezone-time-from-absolute (* 2020 365) ; Jan 1 2020
+ (* 12 60 60)) ; 12:00
+ '(23911 48704 0 0))))
;; TODO: Write tests for timezone-tests-time-zone-from-absolute, which is a
;; bit tricky since the results depend on `current-time-zone'.
diff --git a/test/src/emacs-module-tests.el b/test/src/emacs-module-tests.el
index c44c386d30b..c5107847318 100644
--- a/test/src/emacs-module-tests.el
+++ b/test/src/emacs-module-tests.el
@@ -335,12 +335,15 @@ Interactively, you can try hitting \\[keyboard-quit] to quit."
;; New (TICKS . HZ) format.
'(123456789 . 1000000000)))
(ert-info ((format "input: %s" input))
- (let ((result (mod-test-add-nanosecond input)))
+ (let ((result (mod-test-add-nanosecond input))
+ (desired-result
+ (let ((hz 1000000000))
+ (time-add (time-convert input hz) (cons 1 hz)))))
(should (consp result))
(should (integerp (car result)))
(should (integerp (cdr result)))
(should (cl-plusp (cdr result)))
- (should (time-equal-p result (time-add input '(0 0 0 1000))))))))
+ (should (time-equal-p result desired-result))))))
(ert-deftest mod-test-add-nanosecond/nil ()
(should (<= (float-time (mod-test-add-nanosecond nil))
diff --git a/test/src/timefns-tests.el b/test/src/timefns-tests.el
index 1b1032deaa1..362e7655a91 100644
--- a/test/src/timefns-tests.el
+++ b/test/src/timefns-tests.el
@@ -145,6 +145,9 @@
(< 0.99 (/ (- (float-time a)) (float-time b))
1.01))))))))
+(ert-deftest time-rounding-tests ()
+ (should (time-equal-p 1e-13 (time-add 0 1e-13))))
+
(ert-deftest encode-time-dst-numeric-zone ()
"Check for Bug#35502."
(should (time-equal-p