srfi-19-check.scm 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. ; Part of Scheme 48 1.9. See file COPYING for notices and license.
  2. ; Authors: Will Fitzgerald, John Clements, Emilio Lopes
  3. ;;; Test suite for SRFI-19
  4. ;; tests by Will Fitzgerald, augmented by John Clements -- 2004-08-16
  5. ;; port to Scheme 48 by Emilio Lopes -- 2006-12-29
  6. (define-test-suite srfi-19-tests)
  7. (define-test-case creating-time-structures srfi-19-tests
  8. (check (current-time 'time-tai))
  9. (check (current-time 'time-utc))
  10. (check (current-time 'time-monotonic))
  11. ;; currently not supported:
  12. ;; (current-time 'time-thread)
  13. ;; (current-time 'time-process)
  14. )
  15. (define-test-case testing-time-resolutions srfi-19-tests
  16. (check (time-resolution 'time-tai))
  17. (check (time-resolution 'time-utc))
  18. (check (time-resolution 'time-monotonic))
  19. (check (time-resolution 'time-thread))
  20. (check (time-resolution 'time-process)))
  21. (define-test-case time-comparisons srfi-19-tests
  22. (let ((t1 (make-time 'time-utc 0 1))
  23. (t2 (make-time 'time-utc 0 1))
  24. (t3 (make-time 'time-utc 0 2))
  25. (t11 (make-time 'time-utc 1001 1))
  26. (t12 (make-time 'time-utc 1001 1))
  27. (t13 (make-time 'time-utc 1001 2)))
  28. (check (time=? t1 t2))
  29. (check (time>? t3 t2))
  30. (check (time<? t2 t3))
  31. (check (time>=? t1 t2))
  32. (check (time>=? t3 t2))
  33. (check (time<=? t1 t2))
  34. (check (time<=? t2 t3))
  35. (check (time=? t11 t12))
  36. (check (time>? t13 t12))
  37. (check (time<? t12 t13))
  38. (check (time>=? t11 t12))
  39. (check (time>=? t13 t12))
  40. (check (time<=? t11 t12))
  41. (check (time<=? t12 t13))))
  42. (define-test-case time-difference srfi-19-tests
  43. (let ((t1 (make-time 'time-utc 0 3000))
  44. (t2 (make-time 'time-utc 0 1000))
  45. (t3 (make-time 'time-duration 0 2000))
  46. (t4 (make-time 'time-duration 0 -2000)))
  47. (check-that t3 (is time=? (time-difference t1 t2)))
  48. (check-that t4 (is time=? (time-difference t2 t1)))))
  49. (define-test-case tai-utc-conversions srfi-19-tests
  50. (test-one-utc-tai-edge 915148800 32 31)
  51. (test-one-utc-tai-edge 867715200 31 30)
  52. (test-one-utc-tai-edge 820454400 30 29)
  53. (test-one-utc-tai-edge 773020800 29 28)
  54. (test-one-utc-tai-edge 741484800 28 27)
  55. (test-one-utc-tai-edge 709948800 27 26)
  56. (test-one-utc-tai-edge 662688000 26 25)
  57. (test-one-utc-tai-edge 631152000 25 24)
  58. (test-one-utc-tai-edge 567993600 24 23)
  59. (test-one-utc-tai-edge 489024000 23 22)
  60. (test-one-utc-tai-edge 425865600 22 21)
  61. (test-one-utc-tai-edge 394329600 21 20)
  62. (test-one-utc-tai-edge 362793600 20 19)
  63. (test-one-utc-tai-edge 315532800 19 18)
  64. (test-one-utc-tai-edge 283996800 18 17)
  65. (test-one-utc-tai-edge 252460800 17 16)
  66. (test-one-utc-tai-edge 220924800 16 15)
  67. (test-one-utc-tai-edge 189302400 15 14)
  68. (test-one-utc-tai-edge 157766400 14 13)
  69. (test-one-utc-tai-edge 126230400 13 12)
  70. (test-one-utc-tai-edge 94694400 12 11)
  71. (test-one-utc-tai-edge 78796800 11 10)
  72. (test-one-utc-tai-edge 63072000 10 0)
  73. (test-one-utc-tai-edge 0 0 0) ;; at the epoch
  74. (test-one-utc-tai-edge 10 0 0) ;; close to it ...
  75. (test-one-utc-tai-edge 1045789645 32 32) ;; about now ...
  76. )
  77. (define-test-case tai-date-conversions srfi-19-tests
  78. (check-that (time-tai->date (make-time time-tai 0 (+ 915148800 29)) 0)
  79. (is tm:date= (make-date 0 58 59 23 31 12 1998 0)))
  80. (check-that (time-tai->date (make-time time-tai 0 (+ 915148800 30)) 0)
  81. (is tm:date= (make-date 0 59 59 23 31 12 1998 0)))
  82. (check-that (time-tai->date (make-time time-tai 0 (+ 915148800 31)) 0)
  83. (is tm:date= (make-date 0 60 59 23 31 12 1998 0)))
  84. (check-that (time-tai->date (make-time time-tai 0 (+ 915148800 32)) 0)
  85. (is tm:date= (make-date 0 0 0 0 1 1 1999 0))))
  86. (define-test-case date-utc-conversions srfi-19-tests
  87. (check-that (make-time time-utc 0 (- 915148800 2))
  88. (is time=? (date->time-utc (make-date 0 58 59 23 31 12 1998 0))))
  89. (check-that (make-time time-utc 0 (- 915148800 1))
  90. (is time=? (date->time-utc (make-date 0 59 59 23 31 12 1998 0))))
  91. ;; yes, I think this is acutally right.
  92. (check-that (make-time time-utc 0 (- 915148800 0))
  93. (is time=? (date->time-utc (make-date 0 60 59 23 31 12 1998 0))))
  94. (check-that (make-time time-utc 0 (- 915148800 0))
  95. (is time=? (date->time-utc (make-date 0 0 0 0 1 1 1999 0))))
  96. (check-that (make-time time-utc 0 (+ 915148800 1))
  97. (is time=? (date->time-utc (make-date 0 1 0 0 1 1 1999 0)))))
  98. (define-test-case tz-offset-conversions srfi-19-tests
  99. (let ((ct-utc (make-time time-utc 6320000 1045944859))
  100. (ct-tai (make-time time-tai 6320000 1045944891))
  101. (cd (make-date 6320000 19 14 15 22 2 2003 -18000)))
  102. (check-that ct-utc (is time=? (date->time-utc cd)))
  103. (check-that ct-tai (is time=? (date->time-tai cd)))))
  104. (define-test-case date->string-conversions srfi-19-tests
  105. (check (date->string (make-date 1000 2 3 4 5 6 2007 -120)
  106. "~~.~a.~A.~b.~B.~c.~d.~D.~e,~f,~h.~H")
  107. =>
  108. ;; original, bogus: "~.Tue.Tuesday.Jun.June.Tue Jun 5 4:03:02-0200 2007.05.06/05/07. 5,2.000001,Jun.03"
  109. "~.Tue.Tuesday.Jun.June.Tue Jun 05 04:03:02-0002 2007.05.06/05/07. 5,02.000001,Jun.04"))
  110. (define-test-case date<->julian-day-conversion srfi-19-tests
  111. (check (- (date->julian-day (make-date 0 0 0 0 1 1 2004 0))
  112. (date->julian-day (make-date 0 0 0 0 1 1 2003 0)))
  113. => 365)
  114. (let ((test-date (make-date 0 0 0 0 1 1 2003 -7200)))
  115. (check-that test-date
  116. (is tm:date= (julian-day->date (date->julian-day test-date) -7200)))))
  117. (define-test-case date->modified-julian-day-conversion srfi-19-tests
  118. (check (- (date->modified-julian-day (make-date 0 0 0 0 1 1 2004 0))
  119. (date->modified-julian-day (make-date 0 0 0 0 1 1 2003 0)))
  120. => 365)
  121. (let ((test-date (make-date 0 0 0 0 1 1 2003 -7200)))
  122. (check-that test-date
  123. (is tm:date= (modified-julian-day->date (date->modified-julian-day test-date) -7200)))))
  124. (define-test-case leap-seconds srfi-19-tests
  125. (check (time-second
  126. (date->time-tai (make-date 0 59 59 23 31 12 2008 0)))
  127. => 1230768032)
  128. (check (time-second
  129. (date->time-tai (make-date 0 60 59 23 31 12 2008 0)))
  130. => 1230768033)
  131. (check (time-second
  132. (date->time-tai (make-date 0 0 0 0 1 1 2009 0)))
  133. => 1230768034))
  134. (define (test-one-utc-tai-edge utc tai-diff tai-last-diff)
  135. (let* ( ;; right on the edge they should be the same
  136. (utc-basic (make-time 'time-utc 0 utc))
  137. (tai-basic (make-time 'time-tai 0 (+ utc tai-diff)))
  138. (utc->tai-basic (time-utc->time-tai utc-basic))
  139. (tai->utc-basic (time-tai->time-utc tai-basic))
  140. ;; a second before they should be the old diff
  141. (utc-basic-1 (make-time 'time-utc 0 (- utc 1)))
  142. (tai-basic-1 (make-time 'time-tai 0 (- (+ utc tai-last-diff) 1)))
  143. (utc->tai-basic-1 (time-utc->time-tai utc-basic-1))
  144. (tai->utc-basic-1 (time-tai->time-utc tai-basic-1))
  145. ;; a second later they should be the new diff
  146. (utc-basic+1 (make-time 'time-utc 0 (+ utc 1)))
  147. (tai-basic+1 (make-time 'time-tai 0 (+ (+ utc tai-diff) 1)))
  148. (utc->tai-basic+1 (time-utc->time-tai utc-basic+1))
  149. (tai->utc-basic+1 (time-tai->time-utc tai-basic+1))
  150. ;; ok, let's move the clock half a month or so plus half a second
  151. (shy (* 15 24 60 60))
  152. (hs (/ (expt 10 9) 2))
  153. ;; a second later they should be the new diff
  154. (utc-basic+2 (make-time 'time-utc hs (+ utc shy)))
  155. (tai-basic+2 (make-time 'time-tai hs (+ (+ utc tai-diff) shy)))
  156. (utc->tai-basic+2 (time-utc->time-tai utc-basic+2))
  157. (tai->utc-basic+2 (time-tai->time-utc tai-basic+2)))
  158. (check-that utc-basic (is time=? tai->utc-basic))
  159. (check-that tai-basic (is time=? utc->tai-basic))
  160. (check-that utc-basic-1 (is time=? tai->utc-basic-1))
  161. (check-that tai-basic-1 (is time=? utc->tai-basic-1))
  162. (check-that utc-basic+1 (is time=? tai->utc-basic+1))
  163. (check-that tai-basic+1 (is time=? utc->tai-basic+1))
  164. (check-that utc-basic+2 (is time=? tai->utc-basic+2))
  165. (check-that tai-basic+2 (is time=? utc->tai-basic+2))))
  166. (define (tm:date= d1 d2)
  167. (and (= (date-year d1) (date-year d2))
  168. (= (date-month d1) (date-month d2))
  169. (= (date-day d1) (date-day d2))
  170. (= (date-hour d1) (date-hour d2))
  171. (= (date-second d1) (date-second d2))
  172. (= (date-nanosecond d1) (date-nanosecond d2))
  173. (= (date-zone-offset d1) (date-zone-offset d2))))