]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - tests/ctm-datetime-timezones.lisp
test(ctm): register seven reviewed negative Slice 20B contracts
[topic-maps.git] / tests / ctm-datetime-timezones.lisp
1 (in-package #:topic-maps.ctm.tests)
2
3 (defvar *last-timezone-checks* nil)
4 (defvar *timezone-probes* nil)
5 (defun timezone-checks () *last-timezone-checks*)
6
7 (defun timezone-probe (input expected)
8 (multiple-value-bind (value reason) (topic-maps.tmdm:canonical-datetime-lexical-form input)
9 (check (concatenate 'string "timezone-helper:" input)
10 (and (string= value expected) (eq reason :canonical))))
11 (dolist (mode '(:bare :qualified :api))
12 (let* ((stored (if (eq mode :qualified) (concatenate 'string " " input (string #\Tab)) input))
13 (map (case mode
14 (:bare (read-text (format nil "a t: ~A." input)))
15 (:qualified (read-text (format nil "a t: \"~A\"^^<http://www.w3.org/2001/XMLSchema#dateTime>." stored)))
16 (:api (let ((map (read-text "a. t.")))
17 (topic-maps.tmdm:add-occurrence (local-topic map "a") (local-topic map "t") stored
18 :datatype (topic-maps.tmdm:datetime-datatype)) map))))
19 (item (first (all-occurrences map))) (before (map-state map))
20 (handles (occurrence-handle-state (list item))) (out (cxtm map)))
21 (check (format nil "timezone-model:~A:~A" input mode)
22 (and (string= (if (eq mode :bare) expected stored) (topic-maps.tmdm:occurrence-value item))
23 (string= (topic-maps.tmdm:datetime-datatype) (topic-maps.tmdm:occurrence-datatype item))
24 (eq (local-topic map "a") (topic-maps.tmdm::%occurrence-parent item))
25 (eq (local-topic map "t") (topic-maps.tmdm::%occurrence-type item))
26 (= 2 (length (topic-map-topics map)))))
27 (check (format nil "timezone-output-pure:~A:~A" input mode)
28 (and (equal (list expected) (serialized-values out))
29 (equal before (map-state map)) (equal handles (occurrence-handle-state (list item)))))
30 (push (topic-maps.test-foundation::fields
31 "input" input "mode" (string-downcase (symbol-name mode)) "canonicalValue" expected
32 "storedValue" (topic-maps.tmdm:occurrence-value item) "actualCXTM" out)
33 *timezone-probes*))))
34
35 (defun timezone-calendar-tests ()
36 (dolist (pair '(("2011-01-01T00:00:00+02:00" "2010-12-31T22:00:00Z")
37 ("2011-01-01T00:00:00-02:00" "2011-01-01T02:00:00Z")
38 ("2000-01-01T00:15:00+01:00" "1999-12-31T23:15:00Z")
39 ("1999-12-31T23:30:00-02:00" "2000-01-01T01:30:00Z")
40 ("2011-01-01T00:00:00+00:00" "2011-01-01T00:00:00Z")
41 ("2011-01-01T00:00:00-00:00" "2011-01-01T00:00:00Z")
42 ("2011-01-01T00:00:00" "2011-01-01T00:00:00")
43 ("2011-01-01T00:00:00Z" "2011-01-01T00:00:00Z")
44 ("2000-03-01T00:00:00+00:01" "2000-02-29T23:59:00Z")
45 ("1900-03-01T00:00:00+01:00" "1900-02-28T23:00:00Z")
46 ("2100-03-01T00:00:00+01:00" "2100-02-28T23:00:00Z")
47 ("2400-03-01T00:00:00+01:00" "2400-02-29T23:00:00Z")
48 ("2000-02-28T23:30:00-01:00" "2000-02-29T00:30:00Z")
49 ("1900-02-28T23:30:00-01:00" "1900-03-01T00:30:00Z")
50 ("2000-04-01T00:00:00+01:00" "2000-03-31T23:00:00Z")
51 ("2000-05-01T00:00:00+01:00" "2000-04-30T23:00:00Z")
52 ("2000-04-30T23:59:59-00:01" "2000-05-01T00:00:59Z")
53 ("2000-01-01T00:00:00+14:00" "1999-12-31T10:00:00Z")
54 ("2000-01-01T23:59:59-14:00" "2000-01-02T13:59:59Z")
55 ("2000-01-01T00:00:00+13:59" "1999-12-31T10:01:00Z")
56 ("2000-01-01T00:00:00-13:59" "2000-01-01T13:59:00Z")
57 ("2000-12-31T24:00:00+02:00" "2000-12-31T22:00:00Z")
58 ("2000-12-31T24:00:00-02:00" "2001-01-01T02:00:00Z")
59 ("2000-12-31T24:00:00.000+14:00" "2000-12-31T10:00:00Z")
60 ("2000-01-01T00:00:00.123450000+05:45" "1999-12-31T18:15:00.12345Z")
61 ("9999-12-31T23:59:59-00:01" "10000-01-01T00:00:59Z")
62 ("10000-01-01T00:00:00+00:01" "9999-12-31T23:59:00Z")
63 ("-0002-01-01T12:00:00-01:00" "-0002-01-01T13:00:00Z")))
64 (timezone-probe (first pair) (second pair)))
65 (let ((year (make-string 10000 :initial-element #\9))
66 (fraction (concatenate 'string (make-string 10000 :initial-element #\1) "000")))
67 (timezone-probe (concatenate 'string year "-12-31T23:30:00-02:00")
68 (concatenate 'string "1" (make-string 10000 :initial-element #\0) "-01-01T01:30:00Z"))
69 (timezone-probe (concatenate 'string "1" (make-string 10000 :initial-element #\0) "-01-01T00:30:00+02:00")
70 (concatenate 'string year "-12-31T22:30:00Z"))
71 (timezone-probe (concatenate 'string "2000-03-01T00:00:00." fraction "+00:01")
72 (concatenate 'string "2000-02-29T23:59:00." (string-right-trim "0" fraction) "Z")))
73 ;; Every valid signed offset minute, independently verified by the Python
74 ;; UTC oracle. This boundary date exercises negative FLOOR across leap day.
75 (loop for offset from -840 to 840 do
76 (let* ((input (format nil "2000-03-01T00:30:45.123400~A~2,'0D:~2,'0D"
77 (if (minusp offset) "-" "+") (floor (abs offset) 60) (mod (abs offset) 60)))
78 (total (- 30 offset)) (minutes (mod total 1440))
79 (expected (format nil "~AT~2,'0D:~2,'0D:45.1234Z"
80 (if (minusp total) "2000-02-29" "2000-03-01") (floor minutes 60) (mod minutes 60)))
81 (actual (topic-maps.tmdm:canonical-datetime-lexical-form input)))
82 (check (format nil "timezone-every-offset-minute:~D" offset) (equal expected actual))
83 (push (topic-maps.test-foundation::fields "input" input "mode" "helper" "canonicalValue" actual) *timezone-probes*))))
84
85 (defun timezone-diagnostic-tests ()
86 (loop for suffix in '("+14:01" "-14:01" "+14:59" "-15:00" "+15:00" "+00:60" "-01:60"
87 "+1:00" "-1:00" "+001:00" "+00:000" "+00" "Z+00:00")
88 for value = (concatenate 'string "2000-01-01T12:00:00" suffix) do
89 (multiple-value-bind (canonical reason) (topic-maps.tmdm:canonical-datetime-lexical-form value)
90 (check (concatenate 'string "timezone-invalid-helper:" suffix)
91 (and (null canonical) (eq reason :invalid-datetime-timezone))))
92 (let ((bare (ctm-condition (format nil "a t: ~A." value)))
93 (quoted (ctm-condition (format nil "a t: \"~A\"^^<http://www.w3.org/2001/XMLSchema#dateTime>." value))))
94 (check (concatenate 'string "timezone-invalid-diagnostics:" suffix)
95 (and (eq (type-of bare) 'ctm-rejection) (eq (error-code bare) :invalid-datetime-timezone)
96 (eq (type-of quoted) 'ctm-rejection) (eq (error-code quoted) :invalid-datetime-value)
97 (= 5 (error-position bare) (error-position quoted)) (= 1 (error-line bare) (error-line quoted))
98 (= 6 (error-column bare) (error-column quoted))))))
99 (dolist (entry '(("0001-01-01T00:00:00+00:01" :unsupported-bce-timezone-rollover :datetime-bce-timezone-rollover)
100 ("-0001-12-31T23:30:00-02:00" :unsupported-bce-timezone-rollover :datetime-bce-timezone-rollover)
101 ("-0004-03-01T00:00:00+01:00" :unsupported-bce-timezone-rollover :datetime-bce-timezone-rollover)
102 ("1976-12-31T23:59:60+01:00" :unsupported-leap-second-context :datetime-leap-second-context)
103 ("1976-09-19T12:01:60-01:00" :unsupported-leap-second-context :datetime-leap-second-context)))
104 (destructuring-bind (input reason code) entry
105 (multiple-value-bind (canonical actual-reason) (topic-maps.tmdm:canonical-datetime-lexical-form input)
106 (check (concatenate 'string "timezone-deliberate-limit:" input)
107 (and (topic-maps.tmdm:datetime-lexical-p input) (null canonical) (eq reason actual-reason))))
108 (let* ((bare (ctm-condition (format nil "a t: ~A." input)))
109 (map (read-text (format nil "a t: \"~A\"^^<http://www.w3.org/2001/XMLSchema#dateTime>." input)))
110 (item (first (all-occurrences map))) (before (map-state map))
111 (handles (occurrence-handle-state (list item)))
112 (condition (cxtm-condition (lambda () (cxtm map)))))
113 (check (concatenate 'string "timezone-unsupported-conditions-pure:" input)
114 (and (eq (type-of bare) 'ctm-unsupported) (eq code (error-code bare))
115 (= 5 (error-position bare)) (= 1 (error-line bare)) (= 6 (error-column bare))
116 (typep condition 'topic-maps.cxtm:cxtm-unsupported)
117 (eq code (topic-maps.cxtm:error-code condition))
118 (string= input (topic-maps.tmdm:occurrence-value item))
119 (equal before (map-state map)) (equal handles (occurrence-handle-state (list item)))))))))
120
121 (defun timezone-equality-order-tests ()
122 (let* ((map (read-text "a. b. r. s.")) (a (local-topic map "a")) (b (local-topic map "b"))
123 (r (local-topic map "r")) (s (local-topic map "s")) (dt (topic-maps.tmdm:datetime-datatype))
124 (local "2011-01-01T00:00:00+02:00") (utc "2010-12-31T22:00:00Z")
125 (o1 (topic-maps.tmdm:add-occurrence a r local :datatype dt :item-identifiers '("urn:tz:1")))
126 (o2 (topic-maps.tmdm:add-occurrence b s local :datatype dt :item-identifiers '("urn:tz:2")))
127 (o3 (topic-maps.tmdm:add-occurrence a r utc :datatype dt)))
128 (topic-maps.tmdm:merge-topics a b) (topic-maps.tmdm:merge-topics r s)
129 (let ((current (topic-maps.tmdm:canonical-occurrence o1)))
130 (check "timezone-merge-identical-properties-and-replacement-handles"
131 (and (not (eq o1 current)) (eq current (topic-maps.tmdm:canonical-occurrence o2))
132 (not (eq current (topic-maps.tmdm:canonical-occurrence o3)))
133 (= 2 (length (all-occurrences map)))
134 (eq (topic-maps.tmdm:canonical-topic a) (topic-maps.tmdm::%occurrence-parent current))
135 (eq (topic-maps.tmdm:canonical-topic r) (topic-maps.tmdm::%occurrence-type current))
136 (equal '("urn:tz:1" "urn:tz:2") (topic-maps.tmdm:occurrence-item-identifiers current))
137 (string= local (topic-maps.tmdm:occurrence-value current))))
138 (let ((before (map-state map)) (handles (occurrence-handle-state (list o1 o2 o3 current))) (out (cxtm map)))
139 (check "timezone-equal-xml-distinct-stored-values-no-merging"
140 (equal (list utc utc) (serialized-values out)))
141 (check "timezone-retained-handles-and-references-unchanged"
142 (and (equal before (map-state map)) (equal handles (occurrence-handle-state (list o1 o2 o3 current))))))))
143 (let ((map (read-text "%prefix xsd <http://www.w3.org/2001/XMLSchema#> a t: 2011-01-01T00:00:00+02:00;t: 2010-12-31T22:00:00Z;t:\"2011-01-01T00:00:00+02:00\"^^xsd:dateTime;t:\"2011-01-01T00:00:00\"^^xsd:dateTime.")))
144 (check "timezone-bare-canonical-merging-qualified-lexical-separation"
145 (and (= 3 (length (all-occurrences map)))
146 (= 2 (count "2010-12-31T22:00:00Z" (serialized-values (cxtm map)) :test #'string=)))))
147 (let ((values '("2011-01-01T00:00:00+02:00" "2010-12-31T23:00:00-02:00" "2011-01-01T00:00:00Z")) (reference nil))
148 (dolist (order (permutations values))
149 (let ((map (read-text "a. t.")))
150 (dolist (value order)
151 (topic-maps.tmdm:add-occurrence (local-topic map "a") (local-topic map "t") value :datatype (topic-maps.tmdm:datetime-datatype)))
152 (let* ((before (map-state map)) (out (cxtm map)))
153 (unless reference (setf reference out))
154 (check (format nil "timezone-cxtm-stored-order-not-instant-order:~S" order)
155 (and (equal '("2011-01-01T01:00:00Z" "2010-12-31T22:00:00Z" "2011-01-01T00:00:00Z") (serialized-values out))
156 (string= out reference) (equal before (map-state map)))))))))
157
158 (defun run-timezone-tests ()
159 (run-datetime-tests)
160 (let ((*checks* nil))
161 (setf *timezone-probes* nil)
162 (timezone-calendar-tests) (timezone-diagnostic-tests) (timezone-equality-order-tests)
163 (setf *timezone-probes* (nreverse *timezone-probes*) *last-timezone-checks* (nreverse *checks*))
164 (format t "~&TIMEZONES-PASS: ~D checks; 1681 signed offset minutes, pure canonicalization and lexical TMDM equality.~%" (length *last-timezone-checks*)) t))
165
166 (defun write-timezone-report (path)
167 (with-open-file (out path :direction :output :if-exists :supersede :if-does-not-exist :create)
168 (topic-maps.test-foundation::emit-json-value
169 (topic-maps.test-foundation::fields "kind" "datetime-timezone-evidence" "passed" (length *last-timezone-checks*) "failed" 0
170 "checks" (topic-maps.test-foundation::json-array *last-timezone-checks*)
171 "lexicalProbes" (topic-maps.test-foundation::json-array *timezone-probes*)) out) (terpri out)))