clon-test.lisp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. (in-package :clon-test)
  2. (defmacro assert-error (&body body)
  3. `(assert (nth-value 1 (ignore-errors (progn ,@body)))))
  4. (defun test-next-time ()
  5. (let ((now '(15 30 3 23 5 1974)))
  6. (flet ((foo (spec result &key (now now) allow)
  7. (let ((r (next-time spec
  8. :allow-now-p allow
  9. :now (clon::encode-universal-time* now))))
  10. (assert (or (and (null r) (null result))
  11. (equal result
  12. (subseq (clon::decode-universal-time* r)
  13. 0 6)))))))
  14. (assert-error (foo (make-cron-schedule) '(15 30 3 23 5 1974)))
  15. (foo (make-cron-schedule :second 14) '(14 31 3 23 5 1974) :allow t)
  16. (foo (make-cron-schedule :second 15) '(15 30 3 23 5 1974) :allow t)
  17. (foo (make-cron-schedule :second 15) '(15 31 3 23 5 1974))
  18. (foo (make-cron-schedule :second 16) '(16 30 3 23 5 1974))
  19. (foo (make-cron-schedule :minute 29) '(0 29 4 23 5 1974) :allow t)
  20. (foo (make-cron-schedule :minute 30) '(15 30 3 23 5 1974) :allow t)
  21. (foo (make-cron-schedule :minute 30) '(0 30 4 23 5 1974))
  22. (foo (make-cron-schedule :minute 31) '(0 31 3 23 5 1974))
  23. (foo (make-cron-schedule :hour 2) '(0 0 2 24 5 1974) :allow t)
  24. (foo (make-cron-schedule :hour 3) '(15 30 3 23 5 1974) :allow t)
  25. (foo (make-cron-schedule :hour 3) '(0 0 3 24 5 1974))
  26. (foo (make-cron-schedule :hour 4) '(0 0 4 23 5 1974))
  27. (foo (make-cron-schedule :day-of-month 22) '(0 0 0 22 6 1974) :allow t)
  28. (foo (make-cron-schedule :day-of-month 23) '(15 30 3 23 5 1974) :allow t)
  29. (foo (make-cron-schedule :day-of-month 23) '(0 0 0 23 6 1974))
  30. (foo (make-cron-schedule :day-of-month 24) '(0 0 0 24 5 1974))
  31. (foo (make-cron-schedule :month 4) '(0 0 0 1 4 1975) :allow t)
  32. (foo (make-cron-schedule :month 5) '(15 30 3 23 5 1974) :allow t)
  33. (foo (make-cron-schedule :month 5) '(0 0 0 1 5 1975))
  34. (foo (make-cron-schedule :month 6) '(0 0 0 1 6 1974))
  35. (foo (make-cron-schedule :year 1973) nil :allow t)
  36. (foo (make-cron-schedule :year 1974) '(15 30 3 23 5 1974) :allow t)
  37. (foo (make-cron-schedule :year 1974) nil)
  38. (foo (make-cron-schedule :year 1975) '(0 0 0 1 1 1975))
  39. (foo (make-cron-schedule :day-of-month 1 :month 5) '(0 0 0 1 5 1975))
  40. ;; Test limit
  41. (foo (make-cron-schedule :year 40000) nil)
  42. ;; Hour 3 -> hour 2 -> hour overflow -> day overflow -> month
  43. ;; overflow -> year overflow.
  44. (foo (make-cron-schedule :hour 2 :month 5) '(0 0 2 1 5 1975)
  45. :now '(15 30 3 31 5 1974))
  46. ;; Maximum overflow.
  47. (foo (make-cron-schedule :second 0) '(0 0 0 1 1 1975)
  48. :now '(1 59 23 31 12 1974))
  49. ;; Thursday -> Saturday
  50. (foo (make-cron-schedule :day-of-week 5) '(0 0 0 25 5 1974))
  51. ;; Thursday -> Wednesday
  52. (foo (make-cron-schedule :day-of-week 2) '(0 0 0 29 5 1974))
  53. ;; February doesn't have 31 days.
  54. (foo (make-cron-schedule :day-of-month 31) '(0 0 0 31 3 1974)
  55. :now '(15 30 3 20 2 1974))
  56. ;; So this never happens:
  57. (foo (make-cron-schedule :day-of-month 31 :month 2) nil)
  58. ;; Simple function bumpers.
  59. (foo (make-cron-schedule :second (constantly 15)) '(15 30 3 23 5 1974)
  60. :allow t)
  61. (foo (make-cron-schedule :second (constantly 19)) '(19 30 3 23 5 1974)
  62. :allow t)
  63. (foo (make-cron-schedule :second (constantly 10)) '(10 31 3 23 5 1974)
  64. :allow t)
  65. ;; Hairy typed bumpers.
  66. (let ((hairy (make-cron-schedule
  67. :second (make-typed-cron-bumper '(member 0 15 30 45))
  68. :minute (make-typed-cron-bumper '(and (integer 10 40)
  69. (satisfies evenp))))))
  70. (foo hairy '(15 30 3 31 5 1974) :now '(15 30 3 31 5 1974) :allow t)
  71. (foo hairy '(30 30 3 31 5 1974) :now '(15 30 3 31 5 1974))
  72. (foo hairy '(15 30 3 31 5 1974) :now '(13 30 3 31 5 1974))
  73. (foo hairy '(0 32 3 31 5 1974) :now '(48 30 3 31 5 1974))))))
  74. (defun test-clon ()
  75. (test-next-time))