srfi-19-check.scm 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. ;;; Test suite for SRFI-19
  2. ;; tests by Will Fitzgerald, augmented by John Clements -- 2004-08-16
  3. ;; port to Scheme 48 by Emilio Lopes -- 2006-12-29
  4. (define-syntax assert
  5. (syntax-rules ()
  6. ((_ op ...)
  7. (check (op ...) => #t))))
  8. (define-syntax assert-equal?
  9. (syntax-rules ()
  10. ((_ op1 op2)
  11. (check op1 (=> equal?) op2))))
  12. (define-syntax not-exception
  13. (syntax-rules ()
  14. ((_ expr)
  15. (guard (condition
  16. ((serious-condition? condition) #f)
  17. (else #t))
  18. expr
  19. #t))))
  20. (define-syntax assert-not-exn
  21. (syntax-rules ()
  22. ((_ expr)
  23. (check (not-exception expr) => #t))))
  24. (define-record-type :test-case
  25. (really-make-test-case description expression)
  26. (description test-case-description)
  27. (expression test-case-expression))
  28. (define-syntax make-test-case
  29. (syntax-rules ()
  30. ((_ description expression)
  31. (really-make-test-case description (lambda () expression)))))
  32. (define (run-test-case test-case)
  33. (format #t "** Running test: ~a~%" (test-case-description test-case))
  34. ((test-case-expression test-case)))
  35. (define-record-type :test-suite
  36. (really-make-test-suite description test-cases)
  37. (description test-suite-description)
  38. (test-cases test-suite-cases))
  39. (define (make-test-suite description . tests)
  40. (really-make-test-suite description tests))
  41. (define (run-test-suite test-suite)
  42. (format #t "*** Running test suite: ~a~%" (test-suite-description test-suite))
  43. (check-reset!)
  44. (check-set-mode! 'report-failed)
  45. (for-each run-test-case (test-suite-cases test-suite))
  46. (check-report))
  47. (define test/text-ui run-test-suite)
  48. (define (run-tests)
  49. (run-test-suite srfi-19-test-suite))
  50. (define srfi-19-test-suite
  51. (make-test-suite
  52. "Tests for SRFI 19"
  53. (make-test-case
  54. "Creating time structures"
  55. (assert-not-exn
  56. (list (current-time 'time-tai)
  57. (current-time 'time-utc)
  58. (current-time 'time-monotonic)
  59. ;; currently not supported:
  60. ;; (current-time 'time-thread)
  61. ;; (current-time 'time-process)
  62. )))
  63. (make-test-case
  64. "Testing time resolutions"
  65. (assert-not-exn
  66. (list (time-resolution 'time-tai)
  67. (time-resolution 'time-utc)
  68. (time-resolution 'time-monotonic)
  69. (time-resolution 'time-thread)
  70. (time-resolution 'time-process))))
  71. (make-test-case
  72. "Time comparisons (time=?, etc.)"
  73. (let ((t1 (make-time 'time-utc 0 1))
  74. (t2 (make-time 'time-utc 0 1))
  75. (t3 (make-time 'time-utc 0 2))
  76. (t11 (make-time 'time-utc 1001 1))
  77. (t12 (make-time 'time-utc 1001 1))
  78. (t13 (make-time 'time-utc 1001 2)))
  79. (assert time=? t1 t2)
  80. (assert time>? t3 t2)
  81. (assert time<? t2 t3)
  82. (assert time>=? t1 t2)
  83. (assert time>=? t3 t2)
  84. (assert time<=? t1 t2)
  85. (assert time<=? t2 t3)
  86. (assert time=? t11 t12)
  87. (assert time>? t13 t12)
  88. (assert time<? t12 t13)
  89. (assert time>=? t11 t12)
  90. (assert time>=? t13 t12)
  91. (assert time<=? t11 t12)
  92. (assert time<=? t12 t13)))
  93. (make-test-case
  94. "Time difference"
  95. (let ((t1 (make-time 'time-utc 0 3000))
  96. (t2 (make-time 'time-utc 0 1000))
  97. (t3 (make-time 'time-duration 0 2000))
  98. (t4 (make-time 'time-duration 0 -2000)))
  99. (assert time=? t3 (time-difference t1 t2))
  100. (assert time=? t4 (time-difference t2 t1))))
  101. (make-test-case
  102. "TAI-UTC Conversions"
  103. (begin
  104. (test-one-utc-tai-edge 915148800 32 31)
  105. (test-one-utc-tai-edge 867715200 31 30)
  106. (test-one-utc-tai-edge 820454400 30 29)
  107. (test-one-utc-tai-edge 773020800 29 28)
  108. (test-one-utc-tai-edge 741484800 28 27)
  109. (test-one-utc-tai-edge 709948800 27 26)
  110. (test-one-utc-tai-edge 662688000 26 25)
  111. (test-one-utc-tai-edge 631152000 25 24)
  112. (test-one-utc-tai-edge 567993600 24 23)
  113. (test-one-utc-tai-edge 489024000 23 22)
  114. (test-one-utc-tai-edge 425865600 22 21)
  115. (test-one-utc-tai-edge 394329600 21 20)
  116. (test-one-utc-tai-edge 362793600 20 19)
  117. (test-one-utc-tai-edge 315532800 19 18)
  118. (test-one-utc-tai-edge 283996800 18 17)
  119. (test-one-utc-tai-edge 252460800 17 16)
  120. (test-one-utc-tai-edge 220924800 16 15)
  121. (test-one-utc-tai-edge 189302400 15 14)
  122. (test-one-utc-tai-edge 157766400 14 13)
  123. (test-one-utc-tai-edge 126230400 13 12)
  124. (test-one-utc-tai-edge 94694400 12 11)
  125. (test-one-utc-tai-edge 78796800 11 10)
  126. (test-one-utc-tai-edge 63072000 10 0)
  127. (test-one-utc-tai-edge 0 0 0) ;; at the epoch
  128. (test-one-utc-tai-edge 10 0 0) ;; close to it ...
  129. (test-one-utc-tai-edge 1045789645 32 32) ;; about now ...
  130. ))
  131. (make-test-case
  132. "TAI-Date Conversions"
  133. (begin
  134. (assert tm:date= (time-tai->date (make-time time-tai 0 (+ 915148800 29)) 0)
  135. (make-date 0 58 59 23 31 12 1998 0))
  136. (assert tm:date= (time-tai->date (make-time time-tai 0 (+ 915148800 30)) 0)
  137. (make-date 0 59 59 23 31 12 1998 0))
  138. (assert tm:date= (time-tai->date (make-time time-tai 0 (+ 915148800 31)) 0)
  139. (make-date 0 60 59 23 31 12 1998 0))
  140. (assert tm:date= (time-tai->date (make-time time-tai 0 (+ 915148800 32)) 0)
  141. (make-date 0 0 0 0 1 1 1999 0))))
  142. (make-test-case
  143. "Date-UTC Conversions"
  144. (begin
  145. (assert time=? (make-time time-utc 0 (- 915148800 2))
  146. (date->time-utc (make-date 0 58 59 23 31 12 1998 0)))
  147. (assert time=? (make-time time-utc 0 (- 915148800 1))
  148. (date->time-utc (make-date 0 59 59 23 31 12 1998 0)))
  149. ;; yes, I think this is acutally right.
  150. (assert time=? (make-time time-utc 0 (- 915148800 0))
  151. (date->time-utc (make-date 0 60 59 23 31 12 1998 0)))
  152. (assert time=? (make-time time-utc 0 (- 915148800 0))
  153. (date->time-utc (make-date 0 0 0 0 1 1 1999 0)))
  154. (assert time=? (make-time time-utc 0 (+ 915148800 1))
  155. (date->time-utc (make-date 0 1 0 0 1 1 1999 0)))))
  156. (make-test-case
  157. "TZ Offset conversions"
  158. (let ((ct-utc (make-time time-utc 6320000 1045944859))
  159. (ct-tai (make-time time-tai 6320000 1045944891))
  160. (cd (make-date 6320000 19 14 15 22 2 2003 -18000)))
  161. (assert time=? ct-utc (date->time-utc cd))
  162. (assert time=? ct-tai (date->time-tai cd))))
  163. (make-test-case
  164. "date->string conversions"
  165. (begin
  166. (assert-equal? "~.Tue.Tuesday.Jun.June.Tue Jun 05 04:03:02-0002 2007.05.06/05/07. 5,02.000001,Jun.04"
  167. ;; original, bogus: "~.Tue.Tuesday.Jun.June.Tue Jun 5 4:03:02-0200 2007.05.06/05/07. 5,2.000001,Jun.03"
  168. (date->string (make-date 1000 2 3 4 5 6 2007 -120)
  169. "~~.~a.~A.~b.~B.~c.~d.~D.~e,~f,~h.~H"))))
  170. (make-test-case
  171. "date<->julian-day conversion"
  172. (begin (assert = 365 (- (date->julian-day (make-date 0 0 0 0 1 1 2004 0))
  173. (date->julian-day (make-date 0 0 0 0 1 1 2003 0))))
  174. (let ((test-date (make-date 0 0 0 0 1 1 2003 -7200)))
  175. (assert tm:date= test-date (julian-day->date (date->julian-day test-date) -7200)))))
  176. (make-test-case
  177. "date->modified-julian-day conversion"
  178. (begin (assert = 365 (- (date->modified-julian-day (make-date 0 0 0 0 1 1 2004 0))
  179. (date->modified-julian-day (make-date 0 0 0 0 1 1 2003 0))))
  180. (let ((test-date (make-date 0 0 0 0 1 1 2003 -7200)))
  181. (assert tm:date= test-date (modified-julian-day->date (date->modified-julian-day test-date) -7200)))))
  182. ))
  183. (define (test-one-utc-tai-edge utc tai-diff tai-last-diff)
  184. (let* (;; right on the edge they should be the same
  185. (utc-basic (make-time 'time-utc 0 utc))
  186. (tai-basic (make-time 'time-tai 0 (+ utc tai-diff)))
  187. (utc->tai-basic (time-utc->time-tai utc-basic))
  188. (tai->utc-basic (time-tai->time-utc tai-basic))
  189. ;; a second before they should be the old diff
  190. (utc-basic-1 (make-time 'time-utc 0 (- utc 1)))
  191. (tai-basic-1 (make-time 'time-tai 0 (- (+ utc tai-last-diff) 1)))
  192. (utc->tai-basic-1 (time-utc->time-tai utc-basic-1))
  193. (tai->utc-basic-1 (time-tai->time-utc tai-basic-1))
  194. ;; a second later they should be the new diff
  195. (utc-basic+1 (make-time 'time-utc 0 (+ utc 1)))
  196. (tai-basic+1 (make-time 'time-tai 0 (+ (+ utc tai-diff) 1)))
  197. (utc->tai-basic+1 (time-utc->time-tai utc-basic+1))
  198. (tai->utc-basic+1 (time-tai->time-utc tai-basic+1))
  199. ;; ok, let's move the clock half a month or so plus half a second
  200. (shy (* 15 24 60 60))
  201. (hs (/ (expt 10 9) 2))
  202. ;; a second later they should be the new diff
  203. (utc-basic+2 (make-time 'time-utc hs (+ utc shy)))
  204. (tai-basic+2 (make-time 'time-tai hs (+ (+ utc tai-diff) shy)))
  205. (utc->tai-basic+2 (time-utc->time-tai utc-basic+2))
  206. (tai->utc-basic+2 (time-tai->time-utc tai-basic+2)))
  207. (assert time=? utc-basic tai->utc-basic)
  208. (assert time=? tai-basic utc->tai-basic)
  209. (assert time=? utc-basic-1 tai->utc-basic-1)
  210. (assert time=? tai-basic-1 utc->tai-basic-1)
  211. (assert time=? utc-basic+1 tai->utc-basic+1)
  212. (assert time=? tai-basic+1 utc->tai-basic+1)
  213. (assert time=? utc-basic+2 tai->utc-basic+2)
  214. (assert time=? tai-basic+2 utc->tai-basic+2)))
  215. (define (tm:date= d1 d2)
  216. (and (= (date-year d1) (date-year d2))
  217. (= (date-month d1) (date-month d2))
  218. (= (date-day d1) (date-day d2))
  219. (= (date-hour d1) (date-hour d2))
  220. (= (date-second d1) (date-second d2))
  221. (= (date-nanosecond d1) (date-nanosecond d2))
  222. (= (date-zone-offset d1) (date-zone-offset d2))))