]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - tests/ctm-datetimes.lisp
feat(ctm): decode ISO-8859-5 with qualified charset aliases
[topic-maps.git] / tests / ctm-datetimes.lisp
1 (in-package #:topic-maps.ctm.tests)
2
3 (defvar *last-datetime-checks* nil)
4 (defvar *datetime-observations* nil)
5 (defvar *datetime-probes* nil)
6 (defvar *datetime-offset-observation* nil)
7 (defun datetime-checks () *last-datetime-checks*)
8
9 (defun exercise-datetime-cases ()
10 (setf *datetime-observations* nil)
11 (loop for name in '("occurrence-datetime" "occurrence-datetime2" "occurrence-datetime-fractional" "occurrence-datetime-timezone" "occurrence-datetime-timezone2")
12 for value in '("1976-09-19T12:01:01" "1976-09-19T12:02:00" "2011-01-01T00:00:00.001" "2011-01-01T00:00:00Z" "2010-12-31T22:00:00Z") do
13 (let* ((id (format nil "ctm/in/~A.ctm" name))
14 (case (first (topic-maps.test-foundation:select-cases *cxtm-corpus* :id id)))
15 (doc (topic-maps.test-foundation:test-case-document-iri case))
16 (map (deserialize-file (topic-maps.test-foundation:test-case-source-pathname case) :document-iri doc))
17 (item (first (all-occurrences map)))
18 (named (member name '("occurrence-datetime-fractional" "occurrence-datetime-timezone" "occurrence-datetime-timezone2") :test #'string=))
19 (owner (local-topic map (if named "some-topic" "topic") doc))
20 (type (local-topic map (if named "datetime" "occtype") doc))
21 (state (map-state map)) (retained (occurrence-handle-state (list item)))
22 (actual (topic-maps.cxtm:canonicalize-octets map :base-locator doc))
23 (expected (binary-file (topic-maps.test-foundation:test-case-expected-baseline case)))
24 (different (mismatch actual expected)) (leap (string= name "occurrence-datetime2"))
25 (offset (string= name "occurrence-datetime-timezone2")))
26 (setf (topic-maps.test-foundation:test-case-clauses case)
27 '("CTM 3.2" "CTM 3.4.1" "CTM 3.10" "TMDM 4.3-4.4" "TMDM 5.6" "TMDM 6.5" "CXTM 3.16" "CXTM 4.9" "XSD 3.2.7.1-3.2.7.3" "XSD D.1"))
28 (check (concatenate 'string name ":actual-model")
29 (and (= 1 (length (all-occurrences map))) (= (if named 3 2) (length (topic-map-topics map)))
30 (null (topic-maps.tmdm:topic-map-associations map))
31 (= (if named 2 0) (reduce #'+ (topic-map-topics map) :key (lambda (tpc) (length (topic-names tpc)))))
32 (eq owner (topic-maps.tmdm::%occurrence-parent item)) (eq type (topic-maps.tmdm::%occurrence-type item))
33 (string= value (topic-maps.tmdm:occurrence-value item))
34 (string= (topic-maps.tmdm:datetime-datatype) (topic-maps.tmdm:occurrence-datatype item))
35 (null (topic-maps.tmdm:occurrence-item-identifiers item))))
36 (check (concatenate 'string name ":datatype-no-topic")
37 (notany (lambda (tpc) (member (topic-maps.tmdm:datetime-datatype) (topic-subject-identifiers tpc) :test #'string=)) (topic-map-topics map)))
38 (topic-maps.test-foundation:record-result case (if different :cxtm-mismatch :cxtm-match)
39 :diagnostics (when different (list (if leap
40 "Known normative discrepancy: XSD 1.0 D.1 interprets the noncandidate leap second in the following minute; pinned baseline retains :60."
41 "Known normative discrepancy: CXTM 3.16 / XSD 3.2.7.2 require 2010-12-31T22:00:00Z; pinned baseline retains +02:00."))))
42 (check (concatenate 'string name ":required-comparison-result") (if (or leap offset) different (null different)))
43 (check (concatenate 'string name ":pure") (and (equal state (map-state map)) (equal retained (occurrence-handle-state (list item)))))
44 (setf *cxtm-results* (append *cxtm-results* (list (list :id id :base doc :actual (cxtm map doc)
45 :expected (uiop:read-file-string (topic-maps.test-foundation:test-case-expected-baseline case) :external-format :utf-8)
46 :match (null different) :bytes (length actual)))))
47 (push (topic-maps.test-foundation::fields "id" id "documentIRI" doc "status" (if different "fail" "pass")
48 "classification" (if (or leap offset) "normative-baseline-discrepancy" "byte-exact-cxtm")
49 "ctmSource" (uiop:read-file-string (topic-maps.test-foundation:test-case-source-pathname case) :external-format :utf-8)
50 "actualObjects" (occurrence-observation map)) *datetime-observations*)
51 (format t "~&DATETIME-CXTM ~A: ~A (~D bytes)~%" id (if different "FAIL-known-baseline-discrepancy" "PASS") (length actual))))
52 (setf *datetime-observations* (nreverse *datetime-observations*))
53 (setf *datetime-offset-observation* (car (last *datetime-observations*)))
54 (check "slice11:62-pass-2-discrepancies-209-not-run"
55 (equal (getf (topic-maps.test-foundation:summarize-corpus *cxtm-corpus*) :statuses) '(:pass 62 :fail 2 :unsupported 0 :not-run 209))))
56
57 (defun datetime-lexical-tests ()
58 (setf *datetime-probes* nil)
59 (let ((fraction (concatenate 'string (make-string 10000 :initial-element #\1) "000")))
60 (loop for (input expected) in
61 (append '(("1976-09-19T12:01:01" "1976-09-19T12:01:01")
62 ("1976-09-19T12:01:60" "1976-09-19T12:02:00")
63 ("1976-09-19T12:59:60.5000Z" "1976-09-19T13:00:00.5Z")
64 ("1976-12-31T23:59:60.1200Z" "1976-12-31T23:59:60.12Z")
65 ("2000-02-28T24:00:00.000Z" "2000-02-29T00:00:00Z")
66 ("1900-02-28T24:00:00" "1900-03-01T00:00:00")
67 ("2000-12-31T24:00:00+00:00" "2001-01-01T00:00:00Z")
68 ("9999-12-31T24:00:00Z" "10000-01-01T00:00:00Z")
69 ("-0001-12-31T24:00:00" "0001-01-01T00:00:00")
70 ("-0002-12-31T24:00:00" "-0001-01-01T00:00:00")
71 ("-10000-12-31T24:00:00" "-9999-01-01T00:00:00")
72 ("2000-01-01T00:00:00.000-00:00" "2000-01-01T00:00:00Z")
73 ("2000-01-01T00:00:00.123450000" "2000-01-01T00:00:00.12345"))
74 (list (list (concatenate 'string "2000-01-01T00:00:00." fraction "Z")
75 (concatenate 'string "2000-01-01T00:00:00." (string-right-trim "0" fraction) "Z")))
76 (let ((year (make-string 10000 :initial-element #\9)))
77 (list (list (concatenate 'string year "-12-31T24:00:00")
78 (concatenate 'string "1" (make-string 10000 :initial-element #\0) "-01-01T00:00:00")))))
79 for i from 0 do
80 (check (format nil "datetime-valid-helper:~D" i) (topic-maps.tmdm:datetime-lexical-p input))
81 (check (format nil "datetime-canonical-helper:~D" i) (equal expected (topic-maps.tmdm:canonical-datetime-lexical-form input)))
82 (loop for mode in '(:bare :qualified :api) do
83 (let* ((stored (if (eq mode :qualified) (concatenate 'string " " input (string #\Tab)) input))
84 (map (case mode
85 (:bare (read-text (format nil "a t: ~A." input)))
86 (:qualified (read-text (format nil "a t: \"~A\"^^<http://www.w3.org/2001/XMLSchema#dateTime>." stored)))
87 (:api (let* ((map (read-text "a. t.")))
88 (topic-maps.tmdm:add-occurrence (local-topic map "a") (local-topic map "t") stored :datatype (topic-maps.tmdm:datetime-datatype)) map))))
89 (item (first (all-occurrences map))) (state (map-state map)) (handles (occurrence-handle-state (list item)))
90 (out (cxtm map)))
91 (check (format nil "datetime-model:~D:~A" i mode)
92 (and (string= (if (eq mode :bare) expected stored) (topic-maps.tmdm:occurrence-value item))
93 (eq (local-topic map "a") (topic-maps.tmdm::%occurrence-parent item))
94 (eq (local-topic map "t") (topic-maps.tmdm::%occurrence-type item))
95 (string= (topic-maps.tmdm:datetime-datatype) (topic-maps.tmdm:occurrence-datatype item)) (= 2 (length (topic-map-topics map)))))
96 (check (format nil "datetime-output-purity:~D:~A" i mode)
97 (and (equal (list expected) (serialized-values out)) (equal state (map-state map)) (equal handles (occurrence-handle-state (list item)))))
98 (push (topic-maps.test-foundation::fields "input" input "mode" (string-downcase (symbol-name mode))
99 "storedValue" (topic-maps.tmdm:occurrence-value item) "canonicalValue" expected "actualCXTM" out) *datetime-probes*)))))
100 (setf *datetime-probes* (nreverse *datetime-probes*)))
101
102 (defun datetime-error-tests ()
103 (loop for value in '("2000-01-01T25:00:00" "2000-01-01T12:60:00" "2000-01-01T12:00:61" "2000-01-01T24:00:01"
104 "2000-01-01T24:01:00" "2000-01-01T24:00:00.001" "2000-01-01T12:00:00." "2000-01-01T12:00:00.1.2"
105 "2000-01-01T12:00:00+14:01" "2000-01-01T12:00:00-15:00" "2000-01-01T12:00:00+00:60"
106 "2000-01-01T1:00:00" "2000-01-01T12:0:00" "2000-01-01T12:00:0" "2000-01-01T12:00:00z"
107 "1900-02-29T00:00:00" "0000-01-01T00:00:00" "+2000-01-01T00:00:00" "02000-01-01T00:00:00")
108 for i from 0 do
109 (check (format nil "datetime-invalid-helper:~D" i) (not (topic-maps.tmdm:datetime-lexical-p value)))
110 (let ((c (ctm-condition (format nil "a.~%b t: \"~A\"^^<http://www.w3.org/2001/XMLSchema#dateTime>." value))))
111 (check (format nil "datetime-invalid-qualified:~D" i) (and (eq (type-of c) 'ctm-rejection) (eq :invalid-datetime-value (error-code c)) (= 2 (error-line c)) (= 6 (error-column c)))))
112 (let* ((map (read-text "a. t.")) (state (map-state map))
113 (c (occurrence-condition (lambda () (topic-maps.tmdm:add-occurrence (local-topic map "a") (local-topic map "t") value :datatype (topic-maps.tmdm:datetime-datatype))))))
114 (check (format nil "datetime-invalid-api-atomic:~D" i) (and c (eq :invalid-datetime-value (topic-maps.tmdm:occurrence-error-code c)) (equal state (map-state map))))))
115 (dolist (entry '(("2000-01-01T25:00:00" :invalid-datetime-hour) ("2000-01-01T12:60:00" :invalid-datetime-minute)
116 ("2000-01-01T12:00:61" :invalid-datetime-second) ("2000-01-01T24:00:01" :invalid-datetime-midnight)))
117 (let ((c (ctm-condition (format nil "a t: ~A." (first entry)))))
118 (check (concatenate 'string "datetime-bare-diagnostic:" (first entry))
119 (and (eq (type-of c) 'ctm-rejection) (eq (second entry) (error-code c)) (= 5 (error-position c)) (= 1 (error-line c)) (= 6 (error-column c))))))
120 (dolist (entry '(("2000-06-30T12:00:60+02:00" :datetime-leap-second-context) ("2000-06-30T12:00:60" :datetime-leap-second-context)))
121 (let* ((value (first entry)) (map (read-text (format nil "a t: \"~A\"^^<http://www.w3.org/2001/XMLSchema#dateTime>." value)))
122 (state (map-state map)) (c (cxtm-condition (lambda () (cxtm map)))))
123 (check (concatenate 'string "datetime-valid-but-unsupported:" value)
124 (and (topic-maps.tmdm:datetime-lexical-p value) (typep c 'topic-maps.cxtm:cxtm-unsupported)
125 (eq (second entry) (topic-maps.cxtm:error-code c)) (equal state (map-state map))))))
126 (let* ((map (read-text "a. t.")) (owner (local-topic map "a")))
127 (setf (topic-maps.tmdm::%topic-occurrences owner) (list (topic-maps.tmdm::%make-occurrence "42" (topic-maps.tmdm:datetime-datatype) (local-topic map "t") owner)))
128 (let ((c (cxtm-condition (lambda () (cxtm map)))))
129 (check "datetime-invalid-private-slot-rejected" (and (typep c 'topic-maps.cxtm:cxtm-error) (not (typep c 'topic-maps.cxtm:cxtm-unsupported)) (eq :invalid-datetime-value (topic-maps.cxtm:error-code c)))))))
130
131 (defun datetime-merge-order-tests ()
132 (let* ((map (read-text "a. b. r. s.")) (a (local-topic map "a")) (b (local-topic map "b")) (r (local-topic map "r")) (s (local-topic map "s"))
133 (dt (topic-maps.tmdm:datetime-datatype))
134 (o1 (topic-maps.tmdm:add-occurrence a r "2000-01-01T00:00:00Z" :datatype dt :item-identifiers '("urn:dt:1")))
135 (o2 (topic-maps.tmdm:add-occurrence b s "2000-01-01T00:00:00Z" :datatype dt :item-identifiers '("urn:dt:2"))))
136 (topic-maps.tmdm:merge-topics a b) (topic-maps.tmdm:merge-topics r s)
137 (let ((item (topic-maps.tmdm:canonical-occurrence o1)))
138 (check "datetime-merge-replacement-contract"
139 (and (not (eq o1 item)) (eq item (topic-maps.tmdm:canonical-occurrence o2))
140 (eq (topic-maps.tmdm:canonical-topic a) (topic-maps.tmdm::%occurrence-parent item))
141 (eq (topic-maps.tmdm:canonical-topic r) (topic-maps.tmdm::%occurrence-type item))
142 (equal '("urn:dt:1" "urn:dt:2") (topic-maps.tmdm:occurrence-item-identifiers item))))
143 (let ((state (map-state map)) (handles (occurrence-handle-state (list o1 o2 item))))
144 (cxtm map) (check "datetime-retained-handles-pure" (and (equal state (map-state map)) (equal handles (occurrence-handle-state (list o1 o2 item))))))))
145 (let* ((map (read-text "%prefix xsd <http://www.w3.org/2001/XMLSchema#> a t: 2000-01-01T00:00:00.1200Z;t:\"2000-01-01T00:00:00.12Z\"^^xsd:dateTime;t:\"2000-01-01T00:00:00.1200Z\"^^xsd:dateTime;t: 2000-01-01T00:00:00;t:\"2000-01-01T00:00:00.12Z\".")))
146 (check "datetime-equality-lexical-not-canonical-xml" (= 4 (length (all-occurrences map))))
147 (check "datetime-equivalent-xml-does-not-merge-distinct-stored-strings" (= 3 (count "2000-01-01T00:00:00.12Z" (serialized-values (cxtm map)) :test #'string=))))
148 (let* ((values '("2000-01-01T00:00:00.1" "2000-01-01T00:00:00.100Z" "2000-01-01T00:00:00Z"))
149 (expected '("2000-01-01T00:00:00.1" "2000-01-01T00:00:00.1Z" "2000-01-01T00:00:00Z")) (reference nil))
150 (dolist (order (permutations values))
151 (let* ((map (read-text "a. t.")))
152 (dolist (v order) (topic-maps.tmdm:add-occurrence (local-topic map "a") (local-topic map "t") v :datatype (topic-maps.tmdm:datetime-datatype)))
153 (let ((out (cxtm map)) (state (map-state map)))
154 (unless reference (setf reference out))
155 (check (format nil "datetime-stored-string-order-and-construction:~S" order)
156 (and (equal expected (serialized-values out)) (string= reference (cxtm map)) (equal state (map-state map))))))))
157
158 (let* ((map (read-text "%prefix xsd <http://www.w3.org/2001/XMLSchema#> a t: 2000-01-01T00:00:00Z;t: 2000-01-01T00:00:00;t:\"2000-01-01T00:00:00+00:00\"^^xsd:dateTime;t:\"2000-01-01T00:00:00-00:00\"^^xsd:dateTime; t: 2000-01-01T00:00:00Z.")))
159 (check "datetime-utc-absent-zero-offset-stored-equality" (= 4 (length (all-occurrences map))))
160 (check "datetime-three-utc-xml-values-do-not-establish-tmdm-equality"
161 (= 3 (count "2000-01-01T00:00:00Z" (serialized-values (cxtm map)) :test #'string=))))
162 )
163
164 (defun run-datetime-tests ()
165 (run-date-tests)
166 (let ((*checks* nil))
167 (exercise-datetime-cases) (datetime-lexical-tests) (datetime-error-tests) (datetime-merge-order-tests)
168 (setf *last-datetime-checks* (nreverse *checks*))
169 (format t "~&DATETIMES-PASS: ~D semantic checks; corpus: 62 PASS, 2 known normative discrepancy FAIL, 209 NOT-RUN.~%" (length *last-datetime-checks*)) t))
170
171 (defun write-datetime-report (path)
172 (with-open-file (out path :direction :output :if-exists :supersede :if-does-not-exist :create)
173 (topic-maps.test-foundation::emit-json-value
174 (topic-maps.test-foundation::fields "kind" "datetime-semantic-and-cxtm-evidence" "passed" (length *last-datetime-checks*) "failed" 0
175 "checks" (topic-maps.test-foundation::json-array *last-datetime-checks*) "cases" (topic-maps.test-foundation::json-array *datetime-observations*)
176 "lexicalProbes" (topic-maps.test-foundation::json-array *datetime-probes*)
177 "timezoneObservation" *datetime-offset-observation*
178 "normativeDiscrepancies" (topic-maps.test-foundation::json-array
179 (list (topic-maps.test-foundation::fields "id" "ctm/in/occurrence-datetime2.ctm" "contractStatus" "fail" "reason" "XSD D.1 following-minute interpretation: 1976-09-19T12:02:00; baseline retains :60")
180 (topic-maps.test-foundation::fields "id" "ctm/in/occurrence-datetime-timezone2.ctm" "contractStatus" "fail" "reason" "XSD 3.2.7.2 UTC form is 2010-12-31T22:00:00Z; unchanged baseline retains +02:00; complete comparison executed")))) out) (terpri out)))