1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-05 16:26:17 +00:00

Added tests for os/clock

This commit is contained in:
Ico Doornekamp 2023-05-20 13:55:43 +02:00
parent e8e5f66f4c
commit 80db682109

View File

@ -333,4 +333,29 @@
(assert (pos? (length (gensym))) "gensym not empty, regression #753")
# os/clock
(defmacro measure-time [clocks & body]
(def t1 (gensym))
(def t2 (gensym))
~(do
(def ,t1 (map |(os/clock $) ,clocks))
,;body
(def ,t2 (map |(os/clock $) ,clocks))
(zipcoll ,clocks [ (- (,t2 0) (,t1 0)) (- (,t2 1) (,t1 1)) (- (,t2 2) (,t1 2))]))
)
# Spin for 0.1 seconds
(def dt (measure-time [:realtime :monotonic :cputime]
(def t1 (os/clock :monotonic))
(while (< (- (os/clock :monotonic) t1) 0.1) true)))
(assert (> (dt :monotonic) 0.10))
(assert (> (dt :cputime) 0.05))
# Sleep for 0.1 seconds
(def dt (measure-time [:realtime :monotonic :cputime] (os/sleep 0.1)))
(assert (> (dt :monotonic) 0.10))
(assert (< (dt :cputime) 0.05))
(end-suite)