(in-package #:topic-maps.ctm.tests) (defparameter *decimal-ids* '("occurrence-decimal" "occurrence-decimal-dot" "occurrence-decimal-dot-neg" "occurrence-decimal-leading-zeros" "occurrence-decimal-minus-sign" "occurrence-decimal-plus-sign" "occurrence-decimal-1.0")) (defvar *last-decimal-checks* nil) (defvar *decimal-observations* nil) (defvar *decimal-probes* nil) (defun decimal-checks () *last-decimal-checks*) (defun decimal-values (map) (mapcar #'topic-maps.tmdm:occurrence-value (all-occurrences map))) (defun exercise-decimal-cases () (setf *decimal-observations* nil) (loop for name in *decimal-ids* for expected-value in '("9.3" "0.3" "-0.3" "0.3" "-9.3" "9.3" "1.0") 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)) (o (first items))) (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.8" "CXTM 3.14" "CXTM 3.16" "CXTM 4.2-4.3" "CXTM 4.9" "XSD 3.2.3-3.2.3.2" "XSD 4.3.6")) (check (concatenate 'string name ":no-extra-items") (and (= 2 (length (topic-map-topics map))) (= 1 (length items)) (notany #'topic-names (topic-map-topics map)) (null (topic-maps.tmdm:topic-map-associations map)))) (check (concatenate 'string name ":stored-value") (equal (list expected-value) (decimal-values map))) (check (concatenate 'string name ":datatype-locator-not-topic") (and (string= (topic-maps.tmdm:decimal-datatype) (topic-maps.tmdm:occurrence-datatype o)) (every (lambda (topic) (and (= 1 (length (topic-item-identifiers topic))) (null (topic-subject-identifiers topic)) (null (topic-subject-locators topic)))) (topic-map-topics map)))) (check (concatenate 'string name ":stored-parent-type-references") (and (eq owner (topic-maps.tmdm::%occurrence-parent o)) (eq (local-topic map "occtype" doc) (topic-maps.tmdm::%occurrence-type o)) (null (topic-maps.tmdm:occurrence-item-identifiers o)))) (let* ((state (map-state map)) (handles (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 mismatch byte ~D; actual ~D bytes, expected ~D bytes." different (length actual) (length expected))))) (check (concatenate 'string name ":byte-exact-cxtm") (null different)) (check (concatenate 'string name ":pure-model-and-handles") (and (equal state (map-state map)) (equal handles (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 "~&DECIMAL-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)) *decimal-observations*))) (setf *decimal-observations* (nreverse *decimal-observations*)) (check "slice8:49-passes-224-unexecuted" (equal (getf (topic-maps.test-foundation:summarize-corpus *cxtm-corpus*) :statuses) '(:pass 49 :fail 0 :unsupported 0 :not-run 224)))) (defun decimal-lexical-tests () (setf *decimal-probes* nil) (loop for (input expected) in '(("0" "0.0") ("0." "0.0") (".0" "0.0") ("-.0" "0.0") ("-000.000" "0.0") ("+000.000" "0.0") ("1" "1.0") ("1." "1.0") ("+001.2300" "1.23") ("-.3" "-0.3") ("+.3" "0.3") ("00100.00" "100.0") ("000.0000100" "0.00001") ("-100.00100" "-100.001")) for index from 0 do (check (format nil "decimal-valid-lexical:~D" index) (topic-maps.tmdm:decimal-lexical-p input)) (check (format nil "decimal-canonical-form:~D" index) (equal expected (topic-maps.tmdm:canonical-decimal-lexical-form input)))) (let ((inputs (append '("0.0" "+001.2300" "-001.2300" "-000.000" ".3" "+.3" "-.3" "1.0000" "000100.00" "0.0000100" "42" "42.") (list (format nil " ~C+00042.0300~C~C " #\Tab #\Return #\Newline) (concatenate 'string (make-string 10000 :initial-element #\9) "." (make-string 10000 :initial-element #\9)) (concatenate 'string "-000" (make-string 10000 :initial-element #\9) ".0001000") (concatenate 'string "-0." (make-string 10000 :initial-element #\0) "1")) (loop for i below 64 collect (concatenate 'string (nth (mod i 3) '("" "+" "-")) (make-string (mod i 8) :initial-element #\0) (format nil "~D." (* i 17)) (coerce (loop for j below (1+ i) collect (char "0123456789" (mod (+ (* i 7) (* j j) j) 10))) 'string) (make-string (mod i 7) :initial-element #\0)))))) (loop for input in inputs for index from 0 do (dolist (mode '(:implicit :explicit)) ;; Only CTM decimal grammar is implicit; XSD also permits 42 and 42. (unless (and (eq mode :implicit) (or (find-if #'topic-maps.ctm::whitespace-p input) (not (find #\. input)) (char= #\. (char input (1- (length input)))))) (let* ((source (if (eq mode :implicit) (format nil "a t: ~A." input) (format nil "a t:\"~A\"^^." input))) (map (read-text source)) (o (first (all-occurrences map))) (state (map-state map)) (retained (occurrence-handle-state (list o))) (out (cxtm map)) (stored (topic-maps.tmdm:occurrence-value o)) (expected (topic-maps.tmdm:canonical-decimal-lexical-form input))) (check (format nil "decimal-probe-value:~D:~A" index mode) (string= (if (eq mode :implicit) expected input) stored)) (check (format nil "decimal-probe-output:~D:~A" index mode) (equal (list expected) (serialized-values out))) (check (format nil "decimal-probe-purity:~D:~A" index mode) (and (equal state (map-state map)) (equal retained (occurrence-handle-state (list o))))) (push (topic-maps.test-foundation::fields "input" input "mode" (string-downcase (symbol-name mode)) "storedValue" stored "datatype" (topic-maps.tmdm:occurrence-datatype o) "actualCXTM" out) *decimal-probes*)))))) (setf *decimal-probes* (nreverse *decimal-probes*)) (dolist (input '("1.0" ".3" "9.3" "+.3" "-.3" "000.300")) (let* ((lexer (topic-maps.ctm::make-lexer (concatenate 'string input "."))) (n (topic-maps.ctm::next-token lexer)) (dot (topic-maps.ctm::next-token lexer))) (check (concatenate 'string "decimal-token-boundary:" input) (and (eq :decimal (topic-maps.ctm::token-kind n)) (string= input (topic-maps.ctm::token-value n)) (eq :punctuation (topic-maps.ctm::token-kind dot)) (eql #\. (topic-maps.ctm::token-value dot)) (= (length input) (topic-maps.ctm::token-position dot)))))) (dolist (source (list "a t: 1.0." "a t: 1.0;." "a t: 1.0#(comment)#." (format nil "a t: .3# comment~%."))) (check (concatenate 'string "decimal-layout-boundary:" source) (equal (list (if (search ".3#" source) "0.3" "1.0")) (decimal-values (read-text source))))) (check "decimal-terminator-next-topic" (= 3 (length (topic-map-topics (read-text "a t: 9.3.b."))))) (check "decimal-and-integer-same-token-path" (equal '("1.0" "1" "0.3") (decimal-values (read-text "a t: 1.0;t: 1;t: .3."))))) (defun decimal-error-tests () (loop for value in (append '("" "+" "-" "." "+." "-." "1.2.3" "1e3" "1.0E2" "1 2" "1 .0" "1. 0" "- .1" "+-1.0" "++1.0" "1/2" "NaN" "INF" "١.٢" "1,2" "1_0.0") (list (concatenate 'string (string (code-char 160)) "1.0"))) for index from 0 do (let* ((text (format nil "a t:\"~A\"^^." value)) (condition (ctm-condition text))) (check (format nil "invalid-decimal-helper:~D" index) (and (not (topic-maps.tmdm:decimal-lexical-p value)) (null (topic-maps.tmdm:canonical-decimal-lexical-form value)))) (check (format nil "invalid-decimal-source-position:~D" index) (and (typep condition 'ctm-rejection) (not (typep condition 'ctm-unsupported)) (eq :invalid-decimal-value (error-code condition)) (= 4 (error-position condition)) (= 1 (error-line condition)) (= 5 (error-column condition)))))) (dolist (value '("1.2.3" "1e2" "0x1.2" "1.0f0" "1.0/2" "+.3x" "-.3x")) (let ((condition (ctm-condition (format nil "a t: ~A." value)))) (check (concatenate 'string "malformed-bare-decimal:" value) (and (typep condition 'ctm-rejection) (not (typep condition 'ctm-unsupported)) (eq :invalid-occurrence-literal (error-code condition)) (= 5 (error-position condition)))))) (let ((condition (ctm-condition (format nil "a.~%b t: \"1e2\"^^.")))) (check "decimal-diagnostic-line-column-source" (and (typep condition 'ctm-rejection) (= 2 (error-line condition)) (= 6 (error-column condition)) (equal "independent-fixture" (topic-maps.ctm:error-source condition)) (eq :character (topic-maps.ctm:error-position-unit 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 "1e2" :datatype (topic-maps.tmdm:decimal-datatype)))))) (check "invalid-decimal-api-atomic" (and (eq :invalid-decimal-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 "bad" (topic-maps.tmdm:decimal-datatype) typ owner))) (let* ((state (map-state map)) (condition (cxtm-condition (lambda () (cxtm map))))) (check "invalid-private-decimal-cxtm-error" (and (typep condition 'topic-maps.cxtm:cxtm-error) (not (typep condition 'topic-maps.cxtm:cxtm-unsupported)) (eq :invalid-decimal-value (topic-maps.cxtm:error-code condition)) (equal state (map-state map))))))) (defun decimal-equality-and-merge-tests () (let ((map (read-text "%prefix xsd a t: 01.00; t: +1.0; t: 1.000; t:\"1.0\"^^xsd:decimal; t:\"01.00\"^^xsd:decimal."))) (check "decimal-bare-value-policy-duplicates" (equal '("1.0" "01.00") (decimal-values map))) (check "decimal-output-equivalence-not-merging" (equal '("1.0" "1.0") (serialized-values (cxtm map))))) (let* ((a (read-text "a t: 1.0.")) (b (read-text "a t:\"001.000\"^^."))) (check "decimal-equal-xml-not-tmdm-value-equality" (and (string= (cxtm a) (cxtm b)) (not (equal (decimal-values a) (decimal-values b)))))) (let* ((map (read-text "%prefix xsd a t:\"1\"^^xsd:decimal; t:\"1\"^^xsd:integer.")) (items (all-occurrences map))) (check "integer-decimal-distinct-by-datatype-alone" (and (= 2 (length items)) (equal '("1" "1") (decimal-values map)) (not (string= (topic-maps.tmdm:occurrence-datatype (first items)) (topic-maps.tmdm:occurrence-datatype (second items)))))) (check "integer-decimal-identical-stored-value-output-order" (equal '("1.0" "1") (serialized-values (cxtm map))))) (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:decimal-datatype)) (o1 (topic-maps.tmdm:add-occurrence a r "01.00" :datatype dt :item-identifiers '("urn:decimal:1"))) (o2 (topic-maps.tmdm:add-occurrence b s "01.00" :datatype dt :item-identifiers '("urn:decimal:2"))) (o3 (topic-maps.tmdm:add-occurrence a r "1.0" :datatype dt))) (topic-maps.tmdm:merge-topics a b) (topic-maps.tmdm:merge-topics r s) (check "decimal-merging-equal-properties-only" (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 "decimal-merged-identifiers-union" (equal '("urn:decimal:1" "urn:decimal:2") (sort (topic-maps.tmdm:occurrence-item-identifiers o1) #'string<))) (check "decimal-stored-references-canonical-after-merge" (let ((live (topic-maps.tmdm:canonical-occurrence o1))) (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))))) (let ((state (map-state map)) (retained (occurrence-handle-state (list o1 o2 o3)))) (check "decimal-merged-output-keeps-lexical-distinction" (equal '("1.0" "1.0") (serialized-values (cxtm map)))) (check "decimal-merged-model-handles-not-mutated" (and (equal state (map-state map)) (equal retained (occurrence-handle-state (list o1 o2 o3)))))))) (defun decimal-order-tests () (dolist (fixture '((("-2.0" "-10.0" "2.0" "10.0") ("-10.0" "-2.0" "10.0" "2.0")) (("+002.00" "-10.00" "001.00" "2.0") ("2.0" "-10.0" "1.0" "2.0")))) (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:decimal-datatype))) (let ((state (map-state map)) (out (cxtm map))) (unless reference (setf reference out)) (check (format nil "decimal-stored-string-order:~S" order) (equal expected (serialized-values out))) (check (format nil "decimal-construction-independent-pure:~S" order) (and (string= reference out) (equal state (map-state map)))))))))) (let ((map (read-text "a t:\"001.00\"^^."))) (check "decimal-only-exact-datatype-treated" (search "001.00" (cxtm map))))) (defun run-decimal-tests () (run-integer-tests) (let ((*checks* nil)) (exercise-decimal-cases) (decimal-lexical-tests) (decimal-error-tests) (decimal-equality-and-merge-tests) (decimal-order-tests) (setf *last-decimal-checks* (nreverse *checks*)) (format t "~&DECIMALS-PASS: ~D checks; 7 new byte-exact baselines; 49 imported PASS, 224 NOT-RUN.~%" (length *last-decimal-checks*)) t)) (defun write-decimal-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" "decimal-semantic-and-cxtm-evidence" "passed" (length *last-decimal-checks*) "failed" 0 "checks" (topic-maps.test-foundation::json-array *last-decimal-checks*) "cases" (topic-maps.test-foundation::json-array *decimal-observations*) "lexicalProbes" (topic-maps.test-foundation::json-array *decimal-probes*)) out) (terpri out)))