aboutsummaryrefslogtreecommitdiffstats
path: root/test/src
diff options
context:
space:
mode:
authorPhilipp Stephani2019-04-18 17:42:45 +0200
committerPhilipp Stephani2019-04-24 11:33:52 +0200
commitbffceab6339fb4042588b893ef754c6264379e75 (patch)
tree03747b5cb5275a24418aa9fb421977eb6c0ee063 /test/src
parent5ae407aad4f2564fae7ddce077eb01aa8efa37fb (diff)
downloademacs-bffceab6339fb4042588b893ef754c6264379e75.tar.gz
emacs-bffceab6339fb4042588b893ef754c6264379e75.zip
Add conversions to and from struct timespec to module interface.
Time values are a fundamental data type, and such conversions are hard to implement within modules because of the various forms of time values in Emacs Lisp. Adding dedicated conversion functions can significantly simplify module code dealing with times. This approach uses nanosecond precision. While Emacs in theory has support for higher-precision time values, in practice most languages and standards, such as POSIX, C, Java, and Go, have settled on nanosecond-precision integers to represent time. * src/emacs-module.h.in: Add header for struct timespec. * src/module-env-27.h: Add module functions for time conversion. * src/emacs-module.c (module_extract_time, module_make_time): New functions. (initialize_environment): Use them. * test/data/emacs-module/mod-test.c (Fmod_test_add_nanosecond): New test function. (emacs_module_init): Define it. * test/src/emacs-module-tests.el (mod-test-add-nanosecond/valid) (mod-test-add-nanosecond/nil, mod-test-add-nanosecond/invalid): New unit tests. * doc/lispref/internals.texi (Module Values): Document time conversion functions.
Diffstat (limited to 'test/src')
-rw-r--r--test/src/emacs-module-tests.el28
1 files changed, 28 insertions, 0 deletions
diff --git a/test/src/emacs-module-tests.el b/test/src/emacs-module-tests.el
index 35aaaa64b65..eea4c611655 100644
--- a/test/src/emacs-module-tests.el
+++ b/test/src/emacs-module-tests.el
@@ -310,4 +310,32 @@ Interactively, you can try hitting \\[keyboard-quit] to quit."
310 'finished)) 310 'finished))
311 (quit))))) 311 (quit)))))
312 312
313(ert-deftest mod-test-add-nanosecond/valid ()
314 (dolist (input (list
315 ;; Some realistic examples.
316 (current-time) (time-to-seconds)
317 (encode-time 12 34 5 6 7 2019 t)
318 ;; Various legacy timestamp forms.
319 '(123 456) '(123 456 789) '(123 456 789 6000)
320 ;; Corner case: this will result in a nanosecond
321 ;; value of 1000000000 after addition. The module
322 ;; code should handle this correctly.
323 '(123 65535 999999 999000)
324 ;; Seconds since the epoch.
325 123 123.45
326 ;; New (TICKS . HZ) format.
327 '(123456789 . 1000000000)))
328 (ert-info ((format "input: %s" input))
329 (should (time-equal-p (mod-test-add-nanosecond input)
330 (time-add input '(0 0 0 1000)))))))
331
332(ert-deftest mod-test-add-nanosecond/nil ()
333 (should (<= (float-time (mod-test-add-nanosecond nil))
334 (+ (float-time) 1e-9))))
335
336(ert-deftest mod-test-add-nanosecond/invalid ()
337 (dolist (input '(1.0e+INF 1.0e-INF 0.0e+NaN (123) (123.45 6 7) "foo" [1 2]))
338 (ert-info ((format "input: %s" input))
339 (should-error (mod-test-add-nanosecond input)))))
340
313;;; emacs-module-tests.el ends here 341;;; emacs-module-tests.el ends here