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