~bakpakin/janet

80db6821097f34a59758088797ada8667896603a — Ico Doornekamp 1 year, 4 months ago e8e5f66
Added tests for os/clock
1 files changed, 25 insertions(+), 0 deletions(-)

M test/suite0007.janet
M test/suite0007.janet => test/suite0007.janet +25 -0
@@ 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)