(in-package #:topic-maps.ctm.tests) (defvar *last-datetime-checks* nil) (defvar *datetime-observations* nil) (defvar *datetime-probes* nil) (defvar *datetime-offset-observation* nil) (defun datetime-checks () *last-datetime-checks*) (defun exercise-datetime-cases () (setf *datetime-observations* nil) (loop for name in '("occurrence-datetime" "occurrence-datetime2" "occurrence-datetime-fractional" "occurrence-datetime-timezone") 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 (let* ((id (format nil "ctm/in/~A.ctm" name)) (case (first (topic-maps.test-foundation:select-cases *cxtm-corpus* :id id))) (doc (topic-maps.test-foundation:test-case-document-iri case)) (map (deserialize-file (topic-maps.test-foundation:test-case-source-pathname case) :document-iri doc)) (item (first (all-occurrences map))) (named (member name '("occurrence-datetime-fractional" "occurrence-datetime-timezone") :test #'string=)) (owner (local-topic map (if named "some-topic" "topic") doc)) (type (local-topic map (if named "datetime" "occtype") doc)) (state (map-state map)) (retained (occurrence-handle-state (list item))) (actual (topic-maps.cxtm:canonicalize-octets map :base-locator doc)) (expected (binary-file (topic-maps.test-foundation:test-case-expected-baseline case))) (different (mismatch actual expected)) (leap (string= name "occurrence-datetime2"))) (setf (topic-maps.test-foundation:test-case-clauses case) '("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")) (check (concatenate 'string name ":actual-model") (and (= 1 (length (all-occurrences map))) (= (if named 3 2) (length (topic-map-topics map))) (null (topic-maps.tmdm:topic-map-associations map)) (= (if named 2 0) (reduce #'+ (topic-map-topics map) :key (lambda (tpc) (length (topic-names tpc))))) (eq owner (topic-maps.tmdm::%occurrence-parent item)) (eq type (topic-maps.tmdm::%occurrence-type item)) (string= value (topic-maps.tmdm:occurrence-value item)) (string= (topic-maps.tmdm:datetime-datatype) (topic-maps.tmdm:occurrence-datatype item)) (null (topic-maps.tmdm:occurrence-item-identifiers item)))) (check (concatenate 'string name ":datatype-no-topic") (notany (lambda (tpc) (member (topic-maps.tmdm:datetime-datatype) (topic-subject-identifiers tpc) :test #'string=)) (topic-map-topics map))) (topic-maps.test-foundation:record-result case (if different :cxtm-mismatch :cxtm-match) :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."))) (check (concatenate 'string name ":required-comparison-result") (if leap different (null different))) (check (concatenate 'string name ":pure") (and (equal state (map-state map)) (equal retained (occurrence-handle-state (list item))))) (setf *cxtm-results* (append *cxtm-results* (list (list :id id :base doc :actual (cxtm map doc) :expected (uiop:read-file-string (topic-maps.test-foundation:test-case-expected-baseline case) :external-format :utf-8) :match (null different) :bytes (length actual))))) (push (topic-maps.test-foundation::fields "id" id "documentIRI" doc "status" (if different "fail" "pass") "classification" (if leap "normative-baseline-discrepancy" "byte-exact-cxtm") "ctmSource" (uiop:read-file-string (topic-maps.test-foundation:test-case-source-pathname case) :external-format :utf-8) "actualObjects" (occurrence-observation map)) *datetime-observations*) (format t "~&DATETIME-CXTM ~A: ~A (~D bytes)~%" id (if different "FAIL-known-baseline-discrepancy" "PASS") (length actual)))) (setf *datetime-observations* (nreverse *datetime-observations*)) (let* ((case (first (topic-maps.test-foundation:select-cases *cxtm-corpus* :id "ctm/in/occurrence-datetime-timezone2.ctm"))) (condition (handler-case (progn (deserialize-file (topic-maps.test-foundation:test-case-source-pathname case) :document-iri (topic-maps.test-foundation:test-case-document-iri case)) nil) (ctm-error (c) c)))) (check "nonzero-offset-original-partial-read-is-specific-unsupported" (and (eq (type-of condition) 'ctm-unsupported) (eq :datetime-timezone-canonicalization (error-code condition)) (= 4 (error-line condition)) (> (error-column condition) 1))) (setf *datetime-offset-observation* (topic-maps.test-foundation::fields "id" (topic-maps.test-foundation:test-case-id case) "contractStatus" "not-run" "probe" "partial-read-only; no complete CXTM contract execution" "conditionClass" (symbol-name (type-of condition)) "code" (symbol-name (error-code condition)) "position" (error-position condition) "line" (error-line condition) "column" (error-column condition) "normativeCanonicalValue" "2010-12-31T22:00:00Z" "ctmSource" (uiop:read-file-string (topic-maps.test-foundation:test-case-source-pathname case) :external-format :utf-8) "upstreamCXTM" (uiop:read-file-string (topic-maps.test-foundation:test-case-expected-baseline case) :external-format :utf-8))) (check "nonzero-offset-original-contract-not-run" (eq :not-run (topic-maps.test-foundation:test-case-status case)))) (check "slice10:62-pass-1-discrepancy-210-not-run" (equal (getf (topic-maps.test-foundation:summarize-corpus *cxtm-corpus*) :statuses) '(:pass 62 :fail 1 :unsupported 0 :not-run 210)))) (defun datetime-lexical-tests () (setf *datetime-probes* nil) (let ((fraction (concatenate 'string (make-string 10000 :initial-element #\1) "000"))) (loop for (input expected) in (append '(("1976-09-19T12:01:01" "1976-09-19T12:01:01") ("1976-09-19T12:01:60" "1976-09-19T12:02:00") ("1976-09-19T12:59:60.5000Z" "1976-09-19T13:00:00.5Z") ("1976-12-31T23:59:60.1200Z" "1976-12-31T23:59:60.12Z") ("2000-02-28T24:00:00.000Z" "2000-02-29T00:00:00Z") ("1900-02-28T24:00:00" "1900-03-01T00:00:00") ("2000-12-31T24:00:00+00:00" "2001-01-01T00:00:00Z") ("9999-12-31T24:00:00Z" "10000-01-01T00:00:00Z") ("-0001-12-31T24:00:00" "0001-01-01T00:00:00") ("-0002-12-31T24:00:00" "-0001-01-01T00:00:00") ("-10000-12-31T24:00:00" "-9999-01-01T00:00:00") ("2000-01-01T00:00:00.000-00:00" "2000-01-01T00:00:00Z") ("2000-01-01T00:00:00.123450000" "2000-01-01T00:00:00.12345")) (list (list (concatenate 'string "2000-01-01T00:00:00." fraction "Z") (concatenate 'string "2000-01-01T00:00:00." (string-right-trim "0" fraction) "Z"))) (let ((year (make-string 10000 :initial-element #\9))) (list (list (concatenate 'string year "-12-31T24:00:00") (concatenate 'string "1" (make-string 10000 :initial-element #\0) "-01-01T00:00:00"))))) for i from 0 do (check (format nil "datetime-valid-helper:~D" i) (topic-maps.tmdm:datetime-lexical-p input)) (check (format nil "datetime-canonical-helper:~D" i) (equal expected (topic-maps.tmdm:canonical-datetime-lexical-form input))) (loop for mode in '(:bare :qualified :api) do (let* ((stored (if (eq mode :qualified) (concatenate 'string " " input (string #\Tab)) input)) (map (case mode (:bare (read-text (format nil "a t: ~A." input))) (:qualified (read-text (format nil "a t: \"~A\"^^." stored))) (:api (let* ((map (read-text "a. t."))) (topic-maps.tmdm:add-occurrence (local-topic map "a") (local-topic map "t") stored :datatype (topic-maps.tmdm:datetime-datatype)) map)))) (item (first (all-occurrences map))) (state (map-state map)) (handles (occurrence-handle-state (list item))) (out (cxtm map))) (check (format nil "datetime-model:~D:~A" i mode) (and (string= (if (eq mode :bare) expected stored) (topic-maps.tmdm:occurrence-value item)) (eq (local-topic map "a") (topic-maps.tmdm::%occurrence-parent item)) (eq (local-topic map "t") (topic-maps.tmdm::%occurrence-type item)) (string= (topic-maps.tmdm:datetime-datatype) (topic-maps.tmdm:occurrence-datatype item)) (= 2 (length (topic-map-topics map))))) (check (format nil "datetime-output-purity:~D:~A" i mode) (and (equal (list expected) (serialized-values out)) (equal state (map-state map)) (equal handles (occurrence-handle-state (list item))))) (push (topic-maps.test-foundation::fields "input" input "mode" (string-downcase (symbol-name mode)) "storedValue" (topic-maps.tmdm:occurrence-value item) "canonicalValue" expected "actualCXTM" out) *datetime-probes*))))) (setf *datetime-probes* (nreverse *datetime-probes*))) (defun datetime-error-tests () (loop for value in '("2000-01-01T25:00:00" "2000-01-01T12:60:00" "2000-01-01T12:00:61" "2000-01-01T24:00:01" "2000-01-01T24:01:00" "2000-01-01T24:00:00.001" "2000-01-01T12:00:00." "2000-01-01T12:00:00.1.2" "2000-01-01T12:00:00+14:01" "2000-01-01T12:00:00-15:00" "2000-01-01T12:00:00+00:60" "2000-01-01T1:00:00" "2000-01-01T12:0:00" "2000-01-01T12:00:0" "2000-01-01T12:00:00z" "1900-02-29T00:00:00" "0000-01-01T00:00:00" "+2000-01-01T00:00:00" "02000-01-01T00:00:00") for i from 0 do (check (format nil "datetime-invalid-helper:~D" i) (not (topic-maps.tmdm:datetime-lexical-p value))) (let ((c (ctm-condition (format nil "a.~%b t: \"~A\"^^." value)))) (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))))) (let* ((map (read-text "a. t.")) (state (map-state map)) (c (occurrence-condition (lambda () (topic-maps.tmdm:add-occurrence (local-topic map "a") (local-topic map "t") value :datatype (topic-maps.tmdm:datetime-datatype)))))) (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)))))) (dolist (entry '(("2000-01-01T25:00:00" :invalid-datetime-hour) ("2000-01-01T12:60:00" :invalid-datetime-minute) ("2000-01-01T12:00:61" :invalid-datetime-second) ("2000-01-01T24:00:01" :invalid-datetime-midnight))) (let ((c (ctm-condition (format nil "a t: ~A." (first entry))))) (check (concatenate 'string "datetime-bare-diagnostic:" (first entry)) (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)))))) (dolist (entry '(("2011-01-01T00:00:00+02:00" :datetime-timezone-canonicalization) ("2000-06-30T12:00:60" :datetime-leap-second-context))) (let* ((value (first entry)) (map (read-text (format nil "a t: \"~A\"^^." value))) (state (map-state map)) (c (cxtm-condition (lambda () (cxtm map))))) (check (concatenate 'string "datetime-valid-but-unsupported:" value) (and (topic-maps.tmdm:datetime-lexical-p value) (typep c 'topic-maps.cxtm:cxtm-unsupported) (eq (second entry) (topic-maps.cxtm:error-code c)) (equal state (map-state map)))))) (let* ((map (read-text "a. t.")) (owner (local-topic map "a"))) (setf (topic-maps.tmdm::%topic-occurrences owner) (list (topic-maps.tmdm::%make-occurrence "42" (topic-maps.tmdm:datetime-datatype) (local-topic map "t") owner))) (let ((c (cxtm-condition (lambda () (cxtm map))))) (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))))))) (defun datetime-merge-order-tests () (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")) (dt (topic-maps.tmdm:datetime-datatype)) (o1 (topic-maps.tmdm:add-occurrence a r "2000-01-01T00:00:00Z" :datatype dt :item-identifiers '("urn:dt:1"))) (o2 (topic-maps.tmdm:add-occurrence b s "2000-01-01T00:00:00Z" :datatype dt :item-identifiers '("urn:dt:2")))) (topic-maps.tmdm:merge-topics a b) (topic-maps.tmdm:merge-topics r s) (let ((item (topic-maps.tmdm:canonical-occurrence o1))) (check "datetime-merge-replacement-contract" (and (not (eq o1 item)) (eq item (topic-maps.tmdm:canonical-occurrence o2)) (eq (topic-maps.tmdm:canonical-topic a) (topic-maps.tmdm::%occurrence-parent item)) (eq (topic-maps.tmdm:canonical-topic r) (topic-maps.tmdm::%occurrence-type item)) (equal '("urn:dt:1" "urn:dt:2") (topic-maps.tmdm:occurrence-item-identifiers item)))) (let ((state (map-state map)) (handles (occurrence-handle-state (list o1 o2 item)))) (cxtm map) (check "datetime-retained-handles-pure" (and (equal state (map-state map)) (equal handles (occurrence-handle-state (list o1 o2 item)))))))) (let* ((map (read-text "%prefix xsd 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\"."))) (check "datetime-equality-lexical-not-canonical-xml" (= 4 (length (all-occurrences map)))) (check "datetime-equivalent-xml-does-not-merge-distinct-stored-strings" (= 3 (count "2000-01-01T00:00:00.12Z" (serialized-values (cxtm map)) :test #'string=)))) (let* ((values '("2000-01-01T00:00:00.1" "2000-01-01T00:00:00.100Z" "2000-01-01T00:00:00Z")) (expected '("2000-01-01T00:00:00.1" "2000-01-01T00:00:00.1Z" "2000-01-01T00:00:00Z")) (reference nil)) (dolist (order (permutations values)) (let* ((map (read-text "a. t."))) (dolist (v order) (topic-maps.tmdm:add-occurrence (local-topic map "a") (local-topic map "t") v :datatype (topic-maps.tmdm:datetime-datatype))) (let ((out (cxtm map)) (state (map-state map))) (unless reference (setf reference out)) (check (format nil "datetime-stored-string-order-and-construction:~S" order) (and (equal expected (serialized-values out)) (string= reference (cxtm map)) (equal state (map-state map)))))))) (let* ((map (read-text "%prefix xsd 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."))) (check "datetime-utc-absent-zero-offset-stored-equality" (= 4 (length (all-occurrences map)))) (check "datetime-three-utc-xml-values-do-not-establish-tmdm-equality" (= 3 (count "2000-01-01T00:00:00Z" (serialized-values (cxtm map)) :test #'string=)))) ) (defun run-datetime-tests () (run-date-tests) (let ((*checks* nil)) (exercise-datetime-cases) (datetime-lexical-tests) (datetime-error-tests) (datetime-merge-order-tests) (setf *last-datetime-checks* (nreverse *checks*)) (format t "~&DATETIMES-PASS: ~D semantic checks; corpus: 62 PASS, 1 known normative discrepancy FAIL, 210 NOT-RUN.~%" (length *last-datetime-checks*)) t)) (defun write-datetime-report (path) (with-open-file (out path :direction :output :if-exists :supersede :if-does-not-exist :create) (topic-maps.test-foundation::emit-json-value (topic-maps.test-foundation::fields "kind" "datetime-semantic-and-cxtm-evidence" "passed" (length *last-datetime-checks*) "failed" 0 "checks" (topic-maps.test-foundation::json-array *last-datetime-checks*) "cases" (topic-maps.test-foundation::json-array *datetime-observations*) "lexicalProbes" (topic-maps.test-foundation::json-array *datetime-probes*) "unsupportedConstructObservations" *datetime-offset-observation* "normativeDiscrepancies" (topic-maps.test-foundation::json-array (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") (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)))