srfi-19.test 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. ;;;; srfi-19.test --- test suite for SRFI-19 -*- scheme -*-
  2. ;;;; Matthias Koeppe <mkoeppe@mail.math.uni-magdeburg.de> --- June 2001
  3. ;;;;
  4. ;;;; Copyright (C) 2001, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
  5. ;;;;
  6. ;;;; This program is free software; you can redistribute it and/or modify
  7. ;;;; it under the terms of the GNU General Public License as published by
  8. ;;;; the Free Software Foundation; either version 2, or (at your option)
  9. ;;;; any later version.
  10. ;;;;
  11. ;;;; This program is distributed in the hope that it will be useful,
  12. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;;; GNU General Public License for more details.
  15. ;;;;
  16. ;;;; You should have received a copy of the GNU General Public License
  17. ;;;; along with this software; see the file COPYING. If not, write to
  18. ;;;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  19. ;;;; Boston, MA 02110-1301 USA
  20. ;; SRFI-19 overrides current-date, so we have to do the test in a
  21. ;; separate module, or later tests will fail.
  22. (define-module (test-suite test-srfi-19)
  23. :duplicates (last) ;; avoid warning about srfi-19 replacing `current-time'
  24. :use-module (test-suite lib)
  25. :use-module (srfi srfi-19)
  26. :use-module (ice-9 format))
  27. (define (with-tz* tz thunk)
  28. "Temporarily set the TZ environment variable to the passed string
  29. value and call THUNK."
  30. (let ((old-tz #f))
  31. (dynamic-wind
  32. (lambda ()
  33. (set! old-tz (getenv "TZ"))
  34. (putenv (format "TZ=~A" tz)))
  35. thunk
  36. (lambda ()
  37. (if old-tz
  38. (putenv (format "TZ=~A" old-tz))
  39. (putenv "TZ"))))))
  40. (defmacro with-tz (tz . body)
  41. `(with-tz* ,tz (lambda () ,@body)))
  42. (define (test-integral-time-structure date->time)
  43. "Test whether the given DATE->TIME procedure creates a time
  44. structure with integral seconds. (The seconds shall be maintained as
  45. integers, or precision may go away silently. The SRFI-19 reference
  46. implementation was not OK for Guile in this respect because of Guile's
  47. incomplete numerical tower implementation.)"
  48. (pass-if (format "~A makes integer seconds"
  49. date->time)
  50. (exact? (time-second
  51. (date->time (make-date 0 0 0 12 1 6 2001 0))))))
  52. (define (test-time->date time->date date->time)
  53. (pass-if (format "~A works"
  54. time->date)
  55. (begin
  56. (time->date (date->time (make-date 0 0 0 12 1 6 2001 0)))
  57. #t)))
  58. (define (test-dst time->date date->time)
  59. (pass-if (format "~A respects local DST if no TZ-OFFSET given"
  60. time->date)
  61. (let ((time (date->time (make-date 0 0 0 12 1 6 2001 0))))
  62. ;; on 2001-06-01, there should be 4 hours zone offset
  63. ;; between EST (EDT) and GMT
  64. (= (date-zone-offset
  65. (with-tz "EST5EDT"
  66. (time->date time)))
  67. -14400))))
  68. (define-macro (test-time-conversion a b)
  69. (let* ((a->b-sym (symbol-append a '-> b))
  70. (b->a-sym (symbol-append b '-> a)))
  71. `(pass-if (format "~A and ~A work and are inverses of each other"
  72. ',a->b-sym ',b->a-sym)
  73. (let ((time (make-time ,a 12345 67890123)))
  74. (time=? time (,b->a-sym (,a->b-sym time)))))))
  75. (define (test-time-comparison cmp a b)
  76. (pass-if (format #f "~A works" cmp)
  77. (cmp a b)))
  78. (define (test-time-arithmetic op a b res)
  79. (pass-if (format #f "~A works" op)
  80. (time=? (op a b) res)))
  81. ;; return true if time objects X and Y are equal
  82. (define (time-equal? x y)
  83. (and (eq? (time-type x) (time-type y))
  84. (eqv? (time-second x) (time-second y))
  85. (eqv? (time-nanosecond x) (time-nanosecond y))))
  86. (with-test-prefix "SRFI date/time library"
  87. ;; check for typos and silly errors
  88. (pass-if "date-zone-offset is defined"
  89. (and (defined? 'date-zone-offset)
  90. date-zone-offset
  91. #t))
  92. (pass-if "add-duration is defined"
  93. (and (defined? 'add-duration)
  94. add-duration
  95. #t))
  96. (pass-if "(current-time time-tai) works"
  97. (time? (current-time time-tai)))
  98. (pass-if "(current-time time-process) works"
  99. (time? (current-time time-process)))
  100. (test-time-conversion time-utc time-tai)
  101. (test-time-conversion time-utc time-monotonic)
  102. (test-time-conversion time-tai time-monotonic)
  103. (pass-if "string->date works"
  104. (begin (string->date "2001-06-01@14:00" "~Y-~m-~d@~H:~M")
  105. #t))
  106. ;; check for code paths where reals were passed to quotient, which
  107. ;; doesn't work in Guile (and is unspecified in R5RS)
  108. (test-time->date time-utc->date date->time-utc)
  109. (test-time->date time-tai->date date->time-tai)
  110. (test-time->date time-monotonic->date date->time-monotonic)
  111. (pass-if "Fractional nanoseconds are handled"
  112. (begin (make-time time-duration 1000000000.5 0) #t))
  113. ;; the seconds in a time shall be maintained as integers, or
  114. ;; precision may silently go away
  115. (test-integral-time-structure date->time-utc)
  116. (test-integral-time-structure date->time-tai)
  117. (test-integral-time-structure date->time-monotonic)
  118. ;; check for DST and zone related problems
  119. (pass-if "date->time-utc is the inverse of time-utc->date"
  120. (let ((time (date->time-utc
  121. (make-date 0 0 0 14 1 6 2001 7200))))
  122. (time=? time
  123. (date->time-utc (time-utc->date time 7200)))))
  124. (test-dst time-utc->date date->time-utc)
  125. (test-dst time-tai->date date->time-tai)
  126. (test-dst time-monotonic->date date->time-monotonic)
  127. (test-dst julian-day->date date->julian-day)
  128. (test-dst modified-julian-day->date date->modified-julian-day)
  129. (pass-if "`date->julian-day' honors timezone"
  130. (let ((now (current-date -14400)))
  131. (time=? (date->time-utc (julian-day->date (date->julian-day now)))
  132. (date->time-utc now))))
  133. (pass-if "string->date respects local DST if no time zone is read"
  134. (time=? (date->time-utc
  135. (with-tz "EST5EDT"
  136. (string->date "2001-06-01@08:00" "~Y-~m-~d@~H:~M")))
  137. (date->time-utc
  138. (make-date 0 0 0 12 1 6 2001 0))))
  139. ;; check time comparison procedures
  140. (let* ((time1 (make-time time-monotonic 0 0))
  141. (time2 (make-time time-monotonic 0 0))
  142. (time3 (make-time time-monotonic 385907 998360432))
  143. (time4 (make-time time-monotonic 385907 998360432)))
  144. (test-time-comparison time<=? time1 time3)
  145. (test-time-comparison time<? time1 time3)
  146. (test-time-comparison time=? time1 time2)
  147. (test-time-comparison time>=? time3 time3)
  148. (test-time-comparison time>? time3 time2))
  149. ;; check time arithmetic procedures
  150. (let* ((time1 (make-time time-monotonic 0 0))
  151. (time2 (make-time time-monotonic 385907 998360432))
  152. (diff (time-difference time2 time1)))
  153. (test-time-arithmetic add-duration time1 diff time2)
  154. (test-time-arithmetic subtract-duration time2 diff time1))
  155. (with-test-prefix "date->time-tai"
  156. ;; leap second 1 Jan 1999, 1 second of UTC in make-date is out as 2
  157. ;; seconds of TAI in date->time-tai
  158. (pass-if "31dec98 23:59:59"
  159. (time-equal? (make-time time-tai 0 915148830)
  160. (date->time-tai (make-date 0 59 59 23 31 12 1998 0))))
  161. (pass-if "1jan99 0:00:00"
  162. (time-equal? (make-time time-tai 0 915148832)
  163. (date->time-tai (make-date 0 0 0 0 1 1 1999 0))))
  164. ;; leap second 1 Jan 2006, 1 second of UTC in make-date is out as 2
  165. ;; seconds of TAI in date->time-tai
  166. (pass-if "31dec05 23:59:59"
  167. (time-equal? (make-time time-tai 0 1136073631)
  168. (date->time-tai (make-date 0 59 59 23 31 12 2005 0))))
  169. (pass-if "1jan06 0:00:00"
  170. (time-equal? (make-time time-tai 0 1136073633)
  171. (date->time-tai (make-date 0 0 0 0 1 1 2006 0)))))
  172. (with-test-prefix "date-week-number"
  173. (pass-if (= 0 (date-week-number (make-date 0 0 0 0 1 1 1984 0) 0)))
  174. (pass-if (= 0 (date-week-number (make-date 0 0 0 0 7 1 1984 0) 0)))
  175. (pass-if (= 1 (date-week-number (make-date 0 0 0 0 8 1 1984 0) 0)))))
  176. ;; Local Variables:
  177. ;; eval: (put 'with-tz 'scheme-indent-function 1)
  178. ;; End: