File: tests.lisp

package info (click to toggle)
cl-postmodern 20161031-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 660 kB
  • sloc: lisp: 5,344; makefile: 4
file content (117 lines) | stat: -rw-r--r-- 4,517 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
(defpackage :simple-date-tests
  (:use :common-lisp :fiveam :simple-date))

(in-package :simple-date-tests)

;; After loading the file, run the tests with (fiveam:run! :simple-date)

(def-suite :simple-date)
(in-suite :simple-date)

(test days-in-month
  ;; Note: internal date numbers, so 0 is March
  (is (= 31 (simple-date::days-in-month 0 2000)))
  (is (= 30 (simple-date::days-in-month 1 2000)))
  (is (= 31 (simple-date::days-in-month 2 2000)))
  (is (= 30 (simple-date::days-in-month 3 2000)))
  (is (= 31 (simple-date::days-in-month 4 2000)))
  (is (= 31 (simple-date::days-in-month 5 2000)))
  (is (= 30 (simple-date::days-in-month 6 2000)))
  (is (= 31 (simple-date::days-in-month 7 2000)))
  (is (= 30 (simple-date::days-in-month 8 2000)))
  (is (= 31 (simple-date::days-in-month 9 2000)))
  (is (= 31 (simple-date::days-in-month 10 2000)))
  (is (= 29 (simple-date::days-in-month 11 2000)))
  (is (= 28 (simple-date::days-in-month 11 2001))))

(defmacro with-random-dates (amount &body body)
  (let ((i (gensym)))
    `(dotimes (,i ,amount)
      (let ((year (+ 1900 (random 300)))
            (month (1+ (random 12)))
            (day (1+ (random 28)))
            (hour (random 24))
            (min (random 60))
            (sec (random 60))
            (millisec (random 1000)))
        ,@body))))

(test encode-date
  (with-random-dates 100
    (declare (ignore hour min sec millisec))
    (multiple-value-bind (year* month* day*) (decode-date (encode-date year month day))
      (is (and (= year* year)
               (= month* month)
               (= day* day))))))

(test leap-year
  (flet ((test-date (y m d)
           (multiple-value-bind (y2 m2 d2) (decode-date (encode-date y m d))
             (and (= y y2) (= m m2) (= d d2)))))
    (is (test-date 2000 2 29))
    (is (test-date 2004 2 29))
    (is (test-date 2108 2 29))
    (is (test-date 1992 2 29))))

(test encode-timestamp
  (with-random-dates 100
    (multiple-value-bind (year* month* day* hour* min* sec* millisec*)
        (decode-timestamp (encode-timestamp year month day hour min sec millisec))
      (is (and (= year* year)
               (= month* month)
               (= day* day)
               (= hour* hour)
               (= min* min)
               (= sec* sec)
               (= millisec* millisec))))))
  
(test timestamp-universal-times
  (with-random-dates 100
    (declare (ignore millisec))
    (let ((stamp (encode-timestamp year month day hour min sec 0))
          (utime (encode-universal-time sec min hour day month year 0)))
      (is (= (timestamp-to-universal-time stamp) utime))
      (is (time= (universal-time-to-timestamp utime) stamp)))))

(test add-month
  (with-random-dates 100
    (multiple-value-bind (year* month* day* hour* min* sec* millisec*)
        (decode-timestamp (time-add (encode-timestamp year month day hour min sec millisec)
                                    (encode-interval :month 1)))
      (is (and (or (and (= year* year) (= month* (1+ month)))
                   (and (= year* (1+ year)) (= month* 1)))
               (= day* day)
               (= hour* hour)
               (= min* min)
               (= sec* sec)
               (= millisec* millisec))))))

(test subtract-month
  (with-random-dates 100
    (multiple-value-bind (year* month* day* hour* min* sec* millisec*)
        (decode-timestamp (time-add (encode-timestamp year month day hour min sec millisec)
                                    (encode-interval :month -1)))
      (is (and (or (and (= year* year) (= month* (1- month)))
                   (and (= year* (1- year)) (= month* 12)))
               (= day* day)
               (= hour* hour)
               (= min* min)
               (= sec* sec)
               (= millisec* millisec))))))

(test add-hour
  (with-random-dates 100
    (declare (ignore millisec))
    (is (= (- (timestamp-to-universal-time (time-add (encode-timestamp year month day hour min sec 0)
                                                     (encode-interval :hour 1)))
              (encode-universal-time sec min hour day month year 0))
           3600))))

(test time<
  (with-random-dates 100
    (is (time< (encode-date year month day)
               (encode-date (1+ year) month day)))
    (is (time< (encode-timestamp year month day hour min sec millisec)
               (encode-timestamp year month day hour min (1+ sec) millisec)))
    (is (time< (encode-interval :month month :hour hour)
               (encode-interval :month month :hour hour :minute 30)))))