(in-package #:topic-maps.ctm.tests) (defparameter *date-ids* '("occurrence-date" "occurrence-date2" "occurrence-date3" "occurrence-date-negative" "occurrence-date-another-century")) (defvar *last-date-checks* nil) (defvar *date-observations* nil) (defvar *date-rejections* nil) (defvar *date-probes* nil) (defvar *date-calendar-decisions* nil) (defun date-checks () *last-date-checks*) (defun exercise-date-cases () (setf *date-observations* nil *date-rejections* nil) (loop for name in *date-ids* for values in '(("1976-09-19") ("1976-10-09") ("0001-12-02" "0011-12-02" "0111-12-02" "1111-12-02") ("-1976-09-19") ("191976-09-19")) 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 :source-name id)) (owner (local-topic map "topic" doc)) (items (all-occurrences map))) (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" "XSD 3.2.9-3.2.9.2" "XSD D" "XSD E.1")) (check (concatenate 'string name ":actual-values") (equal values (mapcar #'topic-maps.tmdm:occurrence-value items))) (check (concatenate 'string name ":no-extra-items") (and (= (1+ (length values)) (length (topic-map-topics map))) (= (length values) (length items)) (null (topic-maps.tmdm:topic-map-associations map)) (notany #'topic-names (topic-map-topics map)))) (loop for item in items for i from 1 do (check (format nil "~A:object-references:~D" name i) (and (eq owner (topic-maps.tmdm::%occurrence-parent item)) (eq (local-topic map (if (= (length values) 1) "occtype" (format nil "o~D" i)) doc) (topic-maps.tmdm::%occurrence-type item)) (equal (topic-maps.tmdm:date-datatype) (topic-maps.tmdm:occurrence-datatype item)) (null (topic-maps.tmdm:occurrence-item-identifiers item))))) (check (concatenate 'string name ":datatype-is-not-a-topic") (every (lambda (topic) (and (= 1 (length (topic-item-identifiers topic))) (null (topic-subject-identifiers topic)) (null (topic-subject-locators topic)))) (topic-map-topics map))) (let* ((state (map-state map)) (retained (occurrence-handle-state items)) (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))) (topic-maps.test-foundation:record-result case (if different :cxtm-mismatch :cxtm-match) :diagnostics (when different (list (format nil "First different byte ~D; actual ~D, expected ~D bytes." different (length actual) (length expected))))) (check (concatenate 'string name ":byte-exact-cxtm") (null different)) (check (concatenate 'string name ":serialization-pure") (and (equal state (map-state map)) (equal retained (occurrence-handle-state items)))) (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))))) (format t "~&DATE-CXTM ~A: PASS (~D bytes)~%" id (length actual))) (push (topic-maps.test-foundation::fields "id" id "documentIRI" doc "status" "pass" "ctmSource" (uiop:read-file-string (topic-maps.test-foundation:test-case-source-pathname case) :external-format :utf-8) "actualObjects" (occurrence-observation map)) *date-observations*))) (dolist (entry '(("date-invalid-day" "2000-12-32" :invalid-date-day) ("date-invalid-month" "2000-13-02" :invalid-date-month) ("date-invalid-year" "0000-01-02" :invalid-date-year) ("date-invalid-year2" "00002000-01-02" :invalid-date-year) ("date-invalid-year3" "+2000-01-02" :invalid-date-year))) (destructuring-bind (name token code) entry (let* ((id (format nil "ctm/invalid/~A.ctm" name)) (case (first (topic-maps.test-foundation:select-cases *cxtm-corpus* :id id))) (source (uiop:read-file-string (topic-maps.test-foundation:test-case-source-pathname case) :external-format :utf-8)) (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) :source-name id) nil) (ctm-error (c) c)))) (check (concatenate 'string name ":specific-date-rejection") (and (eq (type-of condition) 'ctm-rejection) (eq code (error-code condition)))) (check (concatenate 'string name ":position-and-source") (and (= (search token source) (error-position condition)) (> (error-line condition) 1) (= 14 (error-column condition)) (equal id (topic-maps.ctm:error-source condition)) (eq :character (topic-maps.ctm:error-position-unit condition)))) (setf (topic-maps.test-foundation:test-case-clauses case) '("CTM 3.4.1" "XSD 3.2.7.1" "XSD 3.2.9.1" "XSD D")) (topic-maps.test-foundation:record-result case :ctm-rejection :diagnostics (list (princ-to-string condition))) (push (topic-maps.test-foundation::fields "id" id "status" "pass" "ctmSource" source "code" (symbol-name (error-code condition)) "conditionClass" (symbol-name (type-of condition)) "position" (error-position condition) "line" (error-line condition) "column" (error-column condition)) *date-rejections*) (format t "~&DATE-REJECTION ~A: PASS (~A at ~D:~D)~%" id code (error-line condition) (error-column condition))))) (setf *date-observations* (nreverse *date-observations*) *date-rejections* (nreverse *date-rejections*)) (check "slice9:59-imported-passes-214-not-run" (equal (getf (topic-maps.test-foundation:summarize-corpus *cxtm-corpus*) :statuses) '(:pass 59 :fail 0 :unsupported 0 :not-run 214)))) (defun date-lexical-tests () (setf *date-probes* nil) (let ((values (append '("0001-01-01" "0011-12-02" "0111-12-02" "1111-12-02" "10000-01-01" "-10000-01-01" "2000-02-29" "2400-02-29" "1900-02-28" "2100-02-28" "-2000-02-29" "-1900-02-28" "-0004-02-29" "1976-10-09" "2000-04-30" "2000-12-31") (list (concatenate 'string (make-string 10000 :initial-element #\9) "-12-31") (concatenate 'string "-" (make-string 10000 :initial-element #\8) "-02-29")) (loop for year in '(4 100 400 1600 1700 1800 1900 1996 2000 2004 2100 2400 -4 -100 -400) append (loop for month from 1 to 12 collect (format nil "~A~4,'0D-~2,'0D-01" (if (minusp year) "-" "") (abs year) month)))))) (loop for value in values for i from 0 do (dolist (mode '(:implicit :explicit)) (let* ((input (if (eq mode :explicit) (format nil " ~C~A~C " #\Tab value #\Return) value)) (map (read-text (if (eq mode :implicit) (format nil "a t: ~A." input) (format nil "a t: \"~A\"^^." input)))) (o (first (all-occurrences map))) (state (map-state map)) (handles (occurrence-handle-state (list o))) (out (cxtm map))) (check (format nil "date-helper-valid:~D:~A" i mode) (topic-maps.tmdm:date-lexical-p input)) (check (format nil "date-canonical-form:~D:~A" i mode) (string= value (topic-maps.tmdm:canonical-date-lexical-form input))) (check (format nil "date-stored-lexical-value:~D:~A" i mode) (string= input (topic-maps.tmdm:occurrence-value o))) (check (format nil "date-output-and-purity:~D:~A" i mode) (and (equal (list value) (serialized-values out)) (equal state (map-state map)) (equal handles (occurrence-handle-state (list o))))) (push (topic-maps.test-foundation::fields "input" input "mode" (string-downcase (symbol-name mode)) "storedValue" (topic-maps.tmdm:occurrence-value o) "datatype" (topic-maps.tmdm:occurrence-datatype o) "actualCXTM" out) *date-probes*))))) (setf *date-probes* (nreverse *date-probes*)) (loop for (literal kind) in '(("2000-01-01" :date) ("-2000-01-01" :date) ("1" :integer) ("1.0" :decimal) (".3" :decimal)) do (let* ((lexer (topic-maps.ctm::make-lexer (concatenate 'string literal "."))) (value (topic-maps.ctm::next-token lexer)) (dot (topic-maps.ctm::next-token lexer))) (check (concatenate 'string "date-integer-decimal-terminator:" literal) (and (eq kind (topic-maps.ctm::token-kind value)) (string= literal (topic-maps.ctm::token-value value)) (eq :punctuation (topic-maps.ctm::token-kind dot)) (eql #\. (topic-maps.ctm::token-value dot)) (= (length literal) (topic-maps.ctm::token-position dot)))))) (dolist (source '("a t: 2000-01-01.b." "a t: 2000-01-01;." "a t: 2000-01-01#(comment)#.")) (check (concatenate 'string "date-token-layout:" source) (equal '("2000-01-01") (mapcar #'topic-maps.tmdm:occurrence-value (all-occurrences (read-text source)))))) (let ((map (read-text "%prefix ex ex:2000-01-01."))) (check "date-looking-qname-remains-topic-identity" (and (null (all-occurrences map)) (equal '("urn:date-name:2000-01-01") (topic-subject-identifiers (first-topic map)))))) (check "date-integer-decimal-and-string-distinct" (equal (list (topic-maps.tmdm:date-datatype) (topic-maps.tmdm:integer-datatype) (topic-maps.tmdm:decimal-datatype) (topic-maps.tmdm:string-datatype)) (mapcar #'topic-maps.tmdm:occurrence-datatype (all-occurrences (read-text "a t: 2000-01-01;t: 2000;t: 2000.0;t:\"2000-01-01\".")))))) (defun date-error-tests () (setf *date-calendar-decisions* nil) (loop for (year leap) in '((4 t) (100 nil) (400 t) (1600 t) (1700 nil) (1800 nil) (1900 nil) (1996 t) (2000 t) (2004 t) (2100 nil) (2400 t) (-4 t) (-100 nil) (-400 t)) do (let* ((value (format nil "~A~4,'0D-02-29" (if (minusp year) "-" "") (abs year))) (condition (ctm-condition (format nil "a t: ~A." value)))) (check (concatenate 'string "date-century-helper:" value) (eq (not (null leap)) (topic-maps.tmdm:date-lexical-p value))) (check (concatenate 'string "date-century-deserialization:" value) (if leap (null condition) (and (typep condition 'ctm-rejection) (eq :invalid-date-day (error-code condition))))) (push (topic-maps.test-foundation::fields "input" value "expected" (if leap "valid" "invalid")) *date-calendar-decisions*))) (setf *date-calendar-decisions* (nreverse *date-calendar-decisions*)) (loop for value in '("" "2000" "+2000-01-01" "0000-01-01" "-0000-01-01" "02000-01-01" "-02000-01-01" "123-01-01" "2000-00-01" "2000-13-01" "2000-01-00" "2000-01-32" "2000-04-31" "1900-02-29" "2100-02-29" "2001-02-29" "-1900-02-29" "2000-2-01" "2000-02-1" "2000-01-01junk" "2000-01-01.0" "2000-01-01+14:01" "2000-01-01-15:00" "2000-01-01+00:60" "2000-01-01z" "2000 -01-01" "2000-01-01") for i from 0 do (check (format nil "date-invalid-helper:~D" i) (and (not (topic-maps.tmdm:date-lexical-p value)) (null (topic-maps.tmdm:canonical-date-lexical-form value)))) (let ((condition (ctm-condition (format nil "a t:\"~A\"^^." value)))) (check (format nil "date-invalid-explicit:~D" i) (and (eq (type-of condition) 'ctm-rejection) (eq :invalid-date-value (error-code condition)) (= 4 (error-position condition)) (= 1 (error-line condition)) (= 5 (error-column condition)))))) (dolist (value '("0000-01-01" "+2000-01-01" "02000-01-01" "2000-04-31" "2001-02-29" "2000-13-01" "2000-01-01Z" "2000-01-01+00:00")) (let ((condition (ctm-condition (format nil "a.~%b t: ~A." value)))) (check (concatenate 'string "date-bare-specific-rejection:" value) (and (eq (type-of condition) 'ctm-rejection) (= 2 (error-line condition)) (= 6 (error-column condition)) (member (error-code condition) '(:invalid-date-year :invalid-date-month :invalid-date-day :invalid-date-syntax)) (equal "independent-fixture" (topic-maps.ctm:error-source condition)))))) (let* ((map (read-text "a. t.")) (owner (local-topic map "a")) (typ (local-topic map "t")) (state (map-state map))) (let ((condition (occurrence-condition (lambda () (topic-maps.tmdm:add-occurrence owner typ "1900-02-29" :datatype (topic-maps.tmdm:date-datatype)))))) (check "invalid-model-date-atomic" (and (eq :invalid-date-value (topic-maps.tmdm:occurrence-error-code condition)) (equal state (map-state map))))) (setf (topic-maps.tmdm::%topic-occurrences owner) (list (topic-maps.tmdm::%make-occurrence "0000-01-01" (topic-maps.tmdm:date-datatype) typ owner))) (let* ((state (map-state map)) (condition (cxtm-condition (lambda () (cxtm map))))) (check "invalid-private-model-date-cxtm-error" (and (eq (type-of condition) 'topic-maps.cxtm:cxtm-error) (eq :invalid-date-value (topic-maps.cxtm:error-code condition)) (equal state (map-state map)))))) (dolist (zone '("Z" "+00:00" "-00:00" "+14:00" "-14:00" "+12:00" "-11:59")) (let* ((value (concatenate 'string "2000-01-01" zone)) (map (read-text (format nil "a t:\"~A\"^^." value))) (state (map-state map)) (condition (cxtm-condition (lambda () (cxtm map))))) (check (concatenate 'string "date-timezone-valid-but-explicitly-unsupported:" zone) (and (topic-maps.tmdm:date-lexical-p value) (typep condition 'topic-maps.cxtm:cxtm-unsupported) (eq :date-timezone-canonicalization (topic-maps.cxtm:error-code condition)) (equal state (map-state map)))))) (check "dateTime-now-supported" (equal '("2000-01-01T12:00:00Z") (serialized-values (cxtm (read-text "a t: 2000-01-01T12:00:00Z.")))))) (defun date-merge-and-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:date-datatype)) (o1 (topic-maps.tmdm:add-occurrence a r "2000-01-01" :datatype dt :item-identifiers '("urn:date:1"))) (o2 (topic-maps.tmdm:add-occurrence b s "2000-01-01" :datatype dt :item-identifiers '("urn:date:2"))) (o3 (topic-maps.tmdm:add-occurrence a r " 2000-01-01 " :datatype dt))) (topic-maps.tmdm:merge-topics a b) (topic-maps.tmdm:merge-topics r s) (check "date-merge-by-stored-properties-not-identical-output" (and (= 2 (length (all-occurrences map))) (eq (topic-maps.tmdm:canonical-occurrence o1) (topic-maps.tmdm:canonical-occurrence o2)) (not (eq (topic-maps.tmdm:canonical-occurrence o1) (topic-maps.tmdm:canonical-occurrence o3))))) (check "date-retained-handles-and-identifiers" (and (equal '("urn:date:1" "urn:date:2") (sort (topic-maps.tmdm:occurrence-item-identifiers o1) #'string<)) (not (eq o1 (topic-maps.tmdm:canonical-occurrence o1))))) (let* ((live (topic-maps.tmdm:canonical-occurrence o1)) (state (map-state map)) (handles (occurrence-handle-state (list o1 o2 o3)))) (check "date-stored-parent-and-type-canonical" (and (eq (topic-maps.tmdm::%occurrence-parent live) (topic-maps.tmdm:canonical-topic a)) (eq (topic-maps.tmdm::%occurrence-type live) (topic-maps.tmdm:canonical-topic r)))) (check "date-identical-canonical-values-do-not-merge-items" (equal '("2000-01-01" "2000-01-01") (serialized-values (cxtm map)))) (check "date-canonicalization-leaves-merge-handles-untouched" (and (equal state (map-state map)) (equal handles (occurrence-handle-state (list o1 o2 o3))))))) (dolist (fixture '((("-2000-01-01" "-0001-01-01" "10000-01-01" "9999-01-01") ("-0001-01-01" "-2000-01-01" "10000-01-01" "9999-01-01")) (("2000-01-01" " 2000-01-02 " "1999-01-01") ("2000-01-02" "1999-01-01" "2000-01-01")))) (destructuring-bind (values expected) fixture (let ((reference nil)) (dolist (order (permutations values)) (let* ((map (read-text "a. t.")) (a (local-topic map "a")) (typ (local-topic map "t"))) (dolist (value order) (topic-maps.tmdm:add-occurrence a typ value :datatype (topic-maps.tmdm:date-datatype))) (let ((state (map-state map)) (out (cxtm map))) (unless reference (setf reference out)) (check (format nil "date-stored-lexical-order:~S" order) (equal expected (serialized-values out))) (check (format nil "date-construction-order-independent:~S" order) (and (string= reference out) (equal state (map-state map)))))))))) (let* ((map (read-text "a t: 2000-01-01;t: 2000-01-01;t:\"2000-01-01\"^^;t:\"2000-01-01\"."))) (check "date-implicit-explicit-duplicates-datatype-sensitive" (= 2 (length (all-occurrences map)))))) (defun run-date-tests () (run-decimal-tests) (let ((*checks* nil)) (exercise-date-cases) (date-lexical-tests) (date-error-tests) (date-merge-and-order-tests) (setf *last-date-checks* (nreverse *checks*)) (format t "~&DATES-PASS: ~D checks; 5 new exact baselines and 5 specific rejections; 59 imported PASS, 214 NOT-RUN.~%" (length *last-date-checks*)) t)) (defun write-date-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" "date-semantic-and-cxtm-evidence" "passed" (length *last-date-checks*) "failed" 0 "checks" (topic-maps.test-foundation::json-array *last-date-checks*) "cases" (topic-maps.test-foundation::json-array *date-observations*) "negativeCases" (topic-maps.test-foundation::json-array *date-rejections*) "calendarDecisions" (topic-maps.test-foundation::json-array *date-calendar-decisions*) "lexicalProbes" (topic-maps.test-foundation::json-array *date-probes*)) out) (terpri out)))