(in-package #:topic-maps.ctm.tests) (defparameter *integer-ids* '("occurrence-integer" "occurrence-integer2" "occurrence-integer-plus-sign" "occurrence-integer-minus-sign" "occurrence-integer-leading-zeros" "occurrence-explicit-datatype2")) (defvar *last-integer-checks* nil) (defvar *integer-observations* nil) (defvar *integer-probes* nil) (defun integer-checks () *last-integer-checks*) (defun integer-values (map) (mapcar #'topic-maps.tmdm:occurrence-value (all-occurrences map))) (defun serialized-values (text) (loop with start = 0 for open = (search "" text :start2 start) while open for close = (search "" text :start2 (+ open 7)) collect (subseq text (+ open 7) close) do (setf start (+ close 8)))) (defun exercise-integer-cases () (setf *integer-observations* nil) (dolist (name *integer-ids*) (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)) (zeros (string= name "occurrence-integer-leading-zeros")) (owner (local-topic map "topic" doc)) (items (all-occurrences map)) (values (cond (zeros '("1" "1" "-1" "100" "0" "0" "0")) ((string= name "occurrence-integer-minus-sign") '("-1")) ((string= name "occurrence-explicit-datatype2") '("42")) (t '("1"))))) (setf (topic-maps.test-foundation:test-case-clauses case) '("CTM 3.2" "CTM 3.3.11" "CTM 3.4.1" "CTM 3.10" "CTM 3.16.1" "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.3.13.1-3.3.13.2" "XSD 4.3.6")) (check (concatenate 'string name ":no-extra-items") (and (= (if zeros 8 2) (length (topic-map-topics map))) (= (length values) (length items)) (notany #'topic-names (topic-map-topics map)) (null (topic-maps.tmdm:topic-map-associations map)))) (check (concatenate 'string name ":tmdm-values") (equal values (integer-values map))) (check (concatenate 'string name ":integer-datatype-not-topic") (and (every (lambda (o) (string= (topic-maps.tmdm:integer-datatype) (topic-maps.tmdm:occurrence-datatype o))) items) (every (lambda (topic) (and (= 1 (length (topic-item-identifiers topic))) (null (topic-subject-identifiers topic)) (null (topic-subject-locators topic)))) (topic-map-topics map)))) (loop for o in items for index from 1 do (check (format nil "~A:stored-references:~D" name index) (and (eq owner (topic-maps.tmdm::%occurrence-parent o)) (eq (local-topic map (if zeros (format nil "o~D" index) "occtype") doc) (topic-maps.tmdm::%occurrence-type o)) (null (topic-maps.tmdm:occurrence-item-identifiers o))))) (let* ((state (map-state map)) (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") (equal state (map-state map))) (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 "~&INTEGER-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)) *integer-observations*))) (setf *integer-observations* (nreverse *integer-observations*)) (check "slice7:42-passes-231-unexecuted" (equal (getf (topic-maps.test-foundation:summarize-corpus *cxtm-corpus*) :statuses) '(:pass 42 :fail 0 :unsupported 0 :not-run 231)))) (defun integer-lexical-tests () (setf *integer-probes* nil) (loop for entry in '(("0" "0") ("-0" "0") ("+0" "0") ("00000" "0") ("-00000" "0") ("+000001" "1") ("-000001" "-1") ("00100" "100") ("+42" "42") ("9223372036854775808" "9223372036854775808") ("-9223372036854775809" "-9223372036854775809")) for index from 0 do (destructuring-bind (value expected) entry (check (format nil "integer-valid-lexical:~D" index) (topic-maps.tmdm:integer-lexical-p value)) (check (format nil "integer-canonical-form:~D" index) (equal expected (topic-maps.tmdm:canonical-integer-lexical-form value))))) ;; Independent Python arithmetic checks each recorded output, including 10,000 ;; digits. No randomized implementation-dependent state is used in the report. (let ((inputs (append '("0" "+001" "-001" "-000" "000100") (list (format nil " ~C+00042~C~C " #\Tab #\Return #\Newline) (make-string 10000 :initial-element #\9) (concatenate 'string "-000" (make-string 10000 :initial-element #\9))) (loop for i below 64 collect (concatenate 'string (nth (mod i 3) '("" "+" "-")) (make-string (mod i 8) :initial-element #\0) (coerce (loop for j below (+ 1 (* i 2)) collect (char "0123456789" (mod (+ (* i 7) (* j j) j) 10))) 'string)))))) (loop for input in inputs for index from 0 do (dolist (mode '(:implicit :explicit)) (unless (and (eq mode :implicit) (find-if (lambda (c) (find c '(#\Space #\Tab #\Return #\Newline))) 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)) (out (cxtm map)) (stored (topic-maps.tmdm:occurrence-value o)) (expected (topic-maps.tmdm:canonical-integer-lexical-form input))) (check (format nil "integer-probe-value:~D:~A" index mode) (string= (if (eq mode :implicit) expected input) stored)) (check (format nil "integer-probe-output:~D:~A" index mode) (equal (list expected) (serialized-values out))) (check (format nil "integer-probe-purity:~D:~A" index mode) (equal state (map-state map))) (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) *integer-probes*)))))) (setf *integer-probes* (nreverse *integer-probes*)) (let* ((lexer (topic-maps.ctm::make-lexer "+001.")) (n (topic-maps.ctm::next-token lexer)) (dot (topic-maps.ctm::next-token lexer))) (check "integer-lexer-keeps-original-spelling" (and (eq :integer (topic-maps.ctm::token-kind n)) (equal "+001" (topic-maps.ctm::token-value n)))) (check "integer-lexer-leaves-topic-terminator" (and (eq :punctuation (topic-maps.ctm::token-kind dot)) (eql #\. (topic-maps.ctm::token-value dot)) (= 4 (topic-maps.ctm::token-position dot))))) (dolist (source (list "a t: 1." "a t: 1;." "a t: 1#(comment)#." (format nil "a t: 1# comment~%."))) (check (concatenate 'string "integer-boundary:" source) (equal '("1") (integer-values (read-text source))))) (check "integer-terminator-next-topic" (equal '("1") (integer-values (read-text "a t: 1.b.")))) (check "integer-semicolon-next-occurrence" (equal '("1" "2") (integer-values (read-text "a t: 1;t: 2."))))) (defun integer-error-tests () (loop for value in (append '("" "+" "-" "1.0" "1." "1e3" "1 2" "#x10" "- 1" "+-1" "++1" "1/2" "NaN" "INF" "١") (list (concatenate 'string (string (code-char 160)) "1"))) for index from 0 do (let* ((text (format nil "a t:\"~A\"^^." value)) (condition (ctm-condition text))) (check (format nil "invalid-integer-helper:~D" index) (and (not (topic-maps.tmdm:integer-lexical-p value)) (null (topic-maps.tmdm:canonical-integer-lexical-form value)))) (check (format nil "invalid-integer-ctm-position:~D" index) (and (typep condition 'ctm-rejection) (not (typep condition 'ctm-unsupported)) (eq :invalid-integer-value (error-code condition)) (= 4 (error-position condition)) (= 1 (error-line condition)) (= 5 (error-column condition)))))) (dolist (value '("+" "--1" "1e3" "0x10" "42x" "1_0" "1/2" "+ 1" "- 1" "NaN" "١")) (let ((condition (ctm-condition (format nil "a t: ~A." value)))) (check (concatenate 'string "malformed-integer-rejected:" value) (and (typep condition 'ctm-rejection) (= 5 (error-position condition)) (member (error-code condition) '(:invalid-occurrence-literal :expected-occurrence-value)))))) (let ((condition (ctm-condition (format nil "a.~%b t: \"1x\"^^.")))) (check "integer-error-has-source-name-and-line" (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))))) (dolist (value '("1.5" ".5" "-.5" "+.5")) (check (concatenate 'string "integer-slice-now-allows-decimal:" value) (string= (topic-maps.tmdm:decimal-datatype) (topic-maps.tmdm:occurrence-datatype (first (all-occurrences (read-text (format nil "a t: ~A." value)))))))) (dolist (value '("2000-12-31T12:00:60+01:00" "2000-12-31T12:00:60-01:00" "*")) (let ((condition (ctm-condition (format nil "a t: ~A." value)))) (check (concatenate 'string "excluded-numeric-sugar:" value) (and (typep condition 'ctm-unsupported) (eq (if (string= value "*") :occurrence-literal-datatype :datetime-leap-second-context) (error-code condition)) (= 5 (error-position 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 "1.0" :datatype (topic-maps.tmdm:integer-datatype)))))) (check "invalid-integer-api-rejected-atomically" (and (eq :invalid-integer-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 "not an integer" (topic-maps.tmdm:integer-datatype) typ owner))) (let* ((state (map-state map)) (condition (cxtm-condition (lambda () (cxtm map))))) (check "invalid-private-integer-cxtm-error-not-unsupported" (and (typep condition 'topic-maps.cxtm:cxtm-error) (not (typep condition 'topic-maps.cxtm:cxtm-unsupported)) (eq :invalid-integer-value (topic-maps.cxtm:error-code condition)) (equal state (map-state map)))))) (check "integer-slice-now-supports-decimal-cxtm" (equal '("42.0") (serialized-values (cxtm (read-text "a t:\"42\"^^."))))) (check "integer-slice-now-supports-date-cxtm" (equal '("2000-12-31") (serialized-values (cxtm (read-text "a t: 2000-12-31."))))) (check "integer-slice-rejects-invalid-dateTime-value" (typep (ctm-condition "a t:\"42\"^^.") 'ctm-rejection))) (defun integer-equality-and-merge-tests () (let ((map (read-text "%prefix xsd a t: 001; t: +1; t: 1; t:\"1\"^^xsd:integer; t:\"001\"^^xsd:integer; t:\"1\"."))) (check "integer-numeric-spellings-share-a-stored-value" (equal '("1" "001" "1") (integer-values map))) (check "integer-tmdm-equality-is-lexical-and-datatype-sensitive" (= 3 (length (all-occurrences map)))) (check "integer-equivalent-value-not-deduplicated-by-output" (equal '("1" "1" "1") (serialized-values (cxtm map))))) (let* ((a (read-text "a t: 1.")) (b (read-text "a t:\"001\"^^."))) (check "equal-cxtm-does-not-prove-equal-tmdm-value" (and (string= (cxtm a) (cxtm b)) (not (equal (integer-values a) (integer-values b)))))) (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:integer-datatype)) (o1 (topic-maps.tmdm:add-occurrence a r "001" :datatype dt :item-identifiers '("urn:int:1"))) (o2 (topic-maps.tmdm:add-occurrence b s "001" :datatype dt :item-identifiers '("urn:int:2"))) (o3 (topic-maps.tmdm:add-occurrence a r "1" :datatype dt))) (topic-maps.tmdm:merge-topics a b) (topic-maps.tmdm:merge-topics r s) (check "integer-merge-removes-lexical-duplicates-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 "integer-merge-identifier-union" (equal '("urn:int:1" "urn:int:2") (sort (topic-maps.tmdm:occurrence-item-identifiers o1) #'string<))) (check "integer-merge-stored-parent-type-canonical" (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 "integer-merge-canonical-output-keeps-distinct-items" (equal '("1" "1") (serialized-values (cxtm map)))) (check "integer-serialization-preserves-model-and-handles" (and (equal state (map-state map)) (equal retained (occurrence-handle-state (list o1 o2 o3)))))))) (defun integer-order-tests () (dolist (fixture '((("-2" "-10" "2" "10") ("-10" "-2" "10" "2")) (("+002" "-10" "001" "2") ("2" "-10" "1" "2")))) (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:integer-datatype))) (let ((state (map-state map)) (out (cxtm map))) (unless reference (setf reference out)) (check (format nil "integer-string-order:~S" order) (equal expected (serialized-values out))) (check (format nil "integer-construction-independent-and-pure:~S" order) (and (string= reference out) (equal state (map-state map)))))))))) (let* ((map (read-text "%prefix xsd a t:\"1\"^^; t:\"1\"; t:\"1\"^^xsd:integer.")) (out (cxtm map))) (check "integer-datatype-breaks-identical-value-tie" (< (search "http://www.w3.org/2001/XMLSchema#integer" out) (search "http://www.w3.org/2001/XMLSchema#string" out) (search "urn:custom" out)))) (let ((map (read-text "a t:\"001\"^^."))) (check "integer-support-does-not-coerce-derived-datatypes" (search "001" (cxtm map))))) (defun run-integer-tests () (run-prefix-tests) (let ((*checks* nil)) (exercise-integer-cases) (integer-lexical-tests) (integer-error-tests) (integer-equality-and-merge-tests) (integer-order-tests) (setf *last-integer-checks* (nreverse *checks*)) (format t "~&INTEGERS-PASS: ~D checks; 6 new byte-exact baselines; 42 imported PASS, 231 NOT-RUN.~%" (length *last-integer-checks*)) t)) (defun write-integer-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" "integer-semantic-and-cxtm-evidence" "passed" (length *last-integer-checks*) "failed" 0 "checks" (topic-maps.test-foundation::json-array *last-integer-checks*) "cases" (topic-maps.test-foundation::json-array *integer-observations*) "lexicalProbes" (topic-maps.test-foundation::json-array *integer-probes*)) out) (terpri out)))