test-gettimeofday.ss
#lang scheme
(require "gettimeofday.ss")
(require (planet schematics/schemeunit))

(define (for iterations thunk)
  (let loop ([i 0])
   (if (> i iterations) (void)
       (begin
         (thunk)
         (loop (+ i 1))))))

(define tests
  (test-suite
   "microtime"
   (test-suite
    "get-time-of-day"
    (test-case
     "seconds line up with current-seconds"
     (for 1000
       (λ ()
         (call-with-values
          get-time-of-day
          (λ (seconds microseconds)
            (check-equal? seconds (current-seconds))
            (sleep 0.001))))))
    (test-case
     "microseconds always increase when seconds do not"
     (let ([last-ms #f])
       (for 1000
         (λ ()
           (call-with-values
            get-time-of-day
            (λ (seconds microseconds)
              (when last-ms
                (when (= seconds (car last-ms))
                  (check > microseconds (cdr last-ms))))
              (set! last-ms (cons seconds microseconds))
              (sleep 0.001))))))))))