(in-package #:topic-maps.ctm.tests) (defvar *last-timezone-checks* nil) (defvar *timezone-probes* nil) (defun timezone-checks () *last-timezone-checks*) (defun timezone-probe (input expected) (multiple-value-bind (value reason) (topic-maps.tmdm:canonical-datetime-lexical-form input) (check (concatenate 'string "timezone-helper:" input) (and (string= value expected) (eq reason :canonical)))) (dolist (mode '(:bare :qualified :api)) (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))) (before (map-state map)) (handles (occurrence-handle-state (list item))) (out (cxtm map))) (check (format nil "timezone-model:~A:~A" input mode) (and (string= (if (eq mode :bare) expected stored) (topic-maps.tmdm:occurrence-value item)) (string= (topic-maps.tmdm:datetime-datatype) (topic-maps.tmdm:occurrence-datatype item)) (eq (local-topic map "a") (topic-maps.tmdm::%occurrence-parent item)) (eq (local-topic map "t") (topic-maps.tmdm::%occurrence-type item)) (= 2 (length (topic-map-topics map))))) (check (format nil "timezone-output-pure:~A:~A" input mode) (and (equal (list expected) (serialized-values out)) (equal before (map-state map)) (equal handles (occurrence-handle-state (list item))))) (push (topic-maps.test-foundation::fields "input" input "mode" (string-downcase (symbol-name mode)) "canonicalValue" expected "storedValue" (topic-maps.tmdm:occurrence-value item) "actualCXTM" out) *timezone-probes*)))) (defun timezone-calendar-tests () (dolist (pair '(("2011-01-01T00:00:00+02:00" "2010-12-31T22:00:00Z") ("2011-01-01T00:00:00-02:00" "2011-01-01T02:00:00Z") ("2000-01-01T00:15:00+01:00" "1999-12-31T23:15:00Z") ("1999-12-31T23:30:00-02:00" "2000-01-01T01:30:00Z") ("2011-01-01T00:00:00+00:00" "2011-01-01T00:00:00Z") ("2011-01-01T00:00:00-00:00" "2011-01-01T00:00:00Z") ("2011-01-01T00:00:00" "2011-01-01T00:00:00") ("2011-01-01T00:00:00Z" "2011-01-01T00:00:00Z") ("2000-03-01T00:00:00+00:01" "2000-02-29T23:59:00Z") ("1900-03-01T00:00:00+01:00" "1900-02-28T23:00:00Z") ("2100-03-01T00:00:00+01:00" "2100-02-28T23:00:00Z") ("2400-03-01T00:00:00+01:00" "2400-02-29T23:00:00Z") ("2000-02-28T23:30:00-01:00" "2000-02-29T00:30:00Z") ("1900-02-28T23:30:00-01:00" "1900-03-01T00:30:00Z") ("2000-04-01T00:00:00+01:00" "2000-03-31T23:00:00Z") ("2000-05-01T00:00:00+01:00" "2000-04-30T23:00:00Z") ("2000-04-30T23:59:59-00:01" "2000-05-01T00:00:59Z") ("2000-01-01T00:00:00+14:00" "1999-12-31T10:00:00Z") ("2000-01-01T23:59:59-14:00" "2000-01-02T13:59:59Z") ("2000-01-01T00:00:00+13:59" "1999-12-31T10:01:00Z") ("2000-01-01T00:00:00-13:59" "2000-01-01T13:59:00Z") ("2000-12-31T24:00:00+02:00" "2000-12-31T22:00:00Z") ("2000-12-31T24:00:00-02:00" "2001-01-01T02:00:00Z") ("2000-12-31T24:00:00.000+14:00" "2000-12-31T10:00:00Z") ("2000-01-01T00:00:00.123450000+05:45" "1999-12-31T18:15:00.12345Z") ("9999-12-31T23:59:59-00:01" "10000-01-01T00:00:59Z") ("10000-01-01T00:00:00+00:01" "9999-12-31T23:59:00Z") ("-0002-01-01T12:00:00-01:00" "-0002-01-01T13:00:00Z"))) (timezone-probe (first pair) (second pair))) (let ((year (make-string 10000 :initial-element #\9)) (fraction (concatenate 'string (make-string 10000 :initial-element #\1) "000"))) (timezone-probe (concatenate 'string year "-12-31T23:30:00-02:00") (concatenate 'string "1" (make-string 10000 :initial-element #\0) "-01-01T01:30:00Z")) (timezone-probe (concatenate 'string "1" (make-string 10000 :initial-element #\0) "-01-01T00:30:00+02:00") (concatenate 'string year "-12-31T22:30:00Z")) (timezone-probe (concatenate 'string "2000-03-01T00:00:00." fraction "+00:01") (concatenate 'string "2000-02-29T23:59:00." (string-right-trim "0" fraction) "Z"))) ;; Every valid signed offset minute, independently verified by the Python ;; UTC oracle. This boundary date exercises negative FLOOR across leap day. (loop for offset from -840 to 840 do (let* ((input (format nil "2000-03-01T00:30:45.123400~A~2,'0D:~2,'0D" (if (minusp offset) "-" "+") (floor (abs offset) 60) (mod (abs offset) 60))) (total (- 30 offset)) (minutes (mod total 1440)) (expected (format nil "~AT~2,'0D:~2,'0D:45.1234Z" (if (minusp total) "2000-02-29" "2000-03-01") (floor minutes 60) (mod minutes 60))) (actual (topic-maps.tmdm:canonical-datetime-lexical-form input))) (check (format nil "timezone-every-offset-minute:~D" offset) (equal expected actual)) (push (topic-maps.test-foundation::fields "input" input "mode" "helper" "canonicalValue" actual) *timezone-probes*)))) (defun timezone-diagnostic-tests () (loop for suffix in '("+14:01" "-14:01" "+14:59" "-15:00" "+15:00" "+00:60" "-01:60" "+1:00" "-1:00" "+001:00" "+00:000" "+00" "Z+00:00") for value = (concatenate 'string "2000-01-01T12:00:00" suffix) do (multiple-value-bind (canonical reason) (topic-maps.tmdm:canonical-datetime-lexical-form value) (check (concatenate 'string "timezone-invalid-helper:" suffix) (and (null canonical) (eq reason :invalid-datetime-timezone)))) (let ((bare (ctm-condition (format nil "a t: ~A." value))) (quoted (ctm-condition (format nil "a t: \"~A\"^^." value)))) (check (concatenate 'string "timezone-invalid-diagnostics:" suffix) (and (eq (type-of bare) 'ctm-rejection) (eq (error-code bare) :invalid-datetime-timezone) (eq (type-of quoted) 'ctm-rejection) (eq (error-code quoted) :invalid-datetime-value) (= 5 (error-position bare) (error-position quoted)) (= 1 (error-line bare) (error-line quoted)) (= 6 (error-column bare) (error-column quoted)))))) (dolist (entry '(("0001-01-01T00:00:00+00:01" :unsupported-bce-timezone-rollover :datetime-bce-timezone-rollover) ("-0001-12-31T23:30:00-02:00" :unsupported-bce-timezone-rollover :datetime-bce-timezone-rollover) ("-0004-03-01T00:00:00+01:00" :unsupported-bce-timezone-rollover :datetime-bce-timezone-rollover) ("1976-12-31T23:59:60+01:00" :unsupported-leap-second-context :datetime-leap-second-context) ("1976-09-19T12:01:60-01:00" :unsupported-leap-second-context :datetime-leap-second-context))) (destructuring-bind (input reason code) entry (multiple-value-bind (canonical actual-reason) (topic-maps.tmdm:canonical-datetime-lexical-form input) (check (concatenate 'string "timezone-deliberate-limit:" input) (and (topic-maps.tmdm:datetime-lexical-p input) (null canonical) (eq reason actual-reason)))) (let* ((bare (ctm-condition (format nil "a t: ~A." input))) (map (read-text (format nil "a t: \"~A\"^^." input))) (item (first (all-occurrences map))) (before (map-state map)) (handles (occurrence-handle-state (list item))) (condition (cxtm-condition (lambda () (cxtm map))))) (check (concatenate 'string "timezone-unsupported-conditions-pure:" input) (and (eq (type-of bare) 'ctm-unsupported) (eq code (error-code bare)) (= 5 (error-position bare)) (= 1 (error-line bare)) (= 6 (error-column bare)) (typep condition 'topic-maps.cxtm:cxtm-unsupported) (eq code (topic-maps.cxtm:error-code condition)) (string= input (topic-maps.tmdm:occurrence-value item)) (equal before (map-state map)) (equal handles (occurrence-handle-state (list item))))))))) (defun timezone-equality-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)) (local "2011-01-01T00:00:00+02:00") (utc "2010-12-31T22:00:00Z") (o1 (topic-maps.tmdm:add-occurrence a r local :datatype dt :item-identifiers '("urn:tz:1"))) (o2 (topic-maps.tmdm:add-occurrence b s local :datatype dt :item-identifiers '("urn:tz:2"))) (o3 (topic-maps.tmdm:add-occurrence a r utc :datatype dt))) (topic-maps.tmdm:merge-topics a b) (topic-maps.tmdm:merge-topics r s) (let ((current (topic-maps.tmdm:canonical-occurrence o1))) (check "timezone-merge-identical-properties-and-replacement-handles" (and (not (eq o1 current)) (eq current (topic-maps.tmdm:canonical-occurrence o2)) (not (eq current (topic-maps.tmdm:canonical-occurrence o3))) (= 2 (length (all-occurrences map))) (eq (topic-maps.tmdm:canonical-topic a) (topic-maps.tmdm::%occurrence-parent current)) (eq (topic-maps.tmdm:canonical-topic r) (topic-maps.tmdm::%occurrence-type current)) (equal '("urn:tz:1" "urn:tz:2") (topic-maps.tmdm:occurrence-item-identifiers current)) (string= local (topic-maps.tmdm:occurrence-value current)))) (let ((before (map-state map)) (handles (occurrence-handle-state (list o1 o2 o3 current))) (out (cxtm map))) (check "timezone-equal-xml-distinct-stored-values-no-merging" (equal (list utc utc) (serialized-values out))) (check "timezone-retained-handles-and-references-unchanged" (and (equal before (map-state map)) (equal handles (occurrence-handle-state (list o1 o2 o3 current)))))))) (let ((map (read-text "%prefix xsd 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."))) (check "timezone-bare-canonical-merging-qualified-lexical-separation" (and (= 3 (length (all-occurrences map))) (= 2 (count "2010-12-31T22:00:00Z" (serialized-values (cxtm map)) :test #'string=))))) (let ((values '("2011-01-01T00:00:00+02:00" "2010-12-31T23:00:00-02:00" "2011-01-01T00:00:00Z")) (reference nil)) (dolist (order (permutations values)) (let ((map (read-text "a. t."))) (dolist (value order) (topic-maps.tmdm:add-occurrence (local-topic map "a") (local-topic map "t") value :datatype (topic-maps.tmdm:datetime-datatype))) (let* ((before (map-state map)) (out (cxtm map))) (unless reference (setf reference out)) (check (format nil "timezone-cxtm-stored-order-not-instant-order:~S" order) (and (equal '("2011-01-01T01:00:00Z" "2010-12-31T22:00:00Z" "2011-01-01T00:00:00Z") (serialized-values out)) (string= out reference) (equal before (map-state map))))))))) (defun run-timezone-tests () (run-datetime-tests) (let ((*checks* nil)) (setf *timezone-probes* nil) (timezone-calendar-tests) (timezone-diagnostic-tests) (timezone-equality-order-tests) (setf *timezone-probes* (nreverse *timezone-probes*) *last-timezone-checks* (nreverse *checks*)) (format t "~&TIMEZONES-PASS: ~D checks; 1681 signed offset minutes, pure canonicalization and lexical TMDM equality.~%" (length *last-timezone-checks*)) t)) (defun write-timezone-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-timezone-evidence" "passed" (length *last-timezone-checks*) "failed" 0 "checks" (topic-maps.test-foundation::json-array *last-timezone-checks*) "lexicalProbes" (topic-maps.test-foundation::json-array *timezone-probes*)) out) (terpri out)))