(defpackage #:topic-maps.ctm-integer.tests (:use #:cl #:topic-maps.tmdm) (:import-from #:topic-maps.registered-original.tests #:check #:fields #:binary #:case-state #:identity-fields #:captured-read #:condition-data) (:export #:run-tests #:write-reports)) (in-package #:topic-maps.ctm-integer.tests) (defparameter *doc* "https://example.invalid/ctm-integer/main.ctm") (defparameter *datatype* "http://psi.topicmaps.org/iso13250/ctm-integer") (defparameter *qualified* "\"*\"^^") (defvar *checks* nil) (defvar *positives* nil) (defvar *negatives* nil) (defvar *original* nil) (defvar *entry-statuses* nil) (defun canonical (map) (topic-maps.cxtm:canonicalize-octets map :base-locator *doc*)) (defun occurrences (map) (loop for topic in (topic-map-topics map) append (topic-occurrences topic))) (defun item-value-p (item value datatype) (and item (equal value (occurrence-value item)) (equal datatype (occurrence-datatype item)))) (defun accepted (label text &optional (initial-counter 0)) (multiple-value-bind (map counter) (topic-maps.ctm:deserialize-string text :document-iri *doc* :source-name label :wildcard-counter initial-counter) (let* ((held (topic-maps.import.tests::handles map)) (before (topic-maps.import.tests::state map held)) (actual (canonical map))) (check (concatenate 'string label ":current-model-and-pure-canonicalization") (and (topic-maps.reference.tests::references-current-p map) (equal before (topic-maps.import.tests::state map held)) (equalp actual (canonical map)))) (push (fields "label" label "sourceText" text "initialCounter" initial-counter "finalCounter" counter "model" (topic-maps.template.tests::observe map) "actualCXTM" (topic-maps.ctm::decode-utf8 actual)) *positives*)) (values map counter))) (defun assert-occurrence (label map value datatype) (let* ((items (occurrences map)) (item (first items)) (owner (topic-maps.registered-original.tests::local map *doc* "topic")) (type (topic-maps.registered-original.tests::local map *doc* "occtype"))) (check (concatenate 'string label ":occurrence-value-datatype-and-references") (and (= 1 (length items)) (item-value-p item value datatype) (eq owner (occurrence-parent item)) (eq type (occurrence-type item)) (= 2 (length (topic-map-topics map))) (null (topic-map-associations map)) (null (occurrence-scope item)) (null (item-identifiers item)))))) (defun positive-controls () (let* ((bare (accepted "equivalence:bare" (format nil "topic~%occtype: *;."))) (qualified (accepted "equivalence:qualified" (format nil "topic~%occtype: ~A;." *qualified*))) (string (accepted "distinction:string" (format nil "topic~%occtype: \"*\";.")))) (assert-occurrence "equivalence:bare" bare "*" *datatype*) (assert-occurrence "equivalence:qualified" qualified "*" *datatype*) (assert-occurrence "distinction:string" string "*" (string-datatype)) (check "equivalence:qualified-and-bare-CXTM-equal-string-distinct" (and (equalp (canonical bare) (canonical qualified)) (not (equalp (canonical bare) (canonical string)))))) (dolist (spec '(("compact" "topic occtype:*.") ("semicolon" "topic occtype:*;.") ("scope" "topic occtype:*@s.") ("reifier" "topic occtype:*~r.") ("comment" "topic occtype:*# comment .") ("block-comment" "topic occtype:*#( ignored )#."))) (destructuring-bind (label text) spec (multiple-value-bind (map counter) (accepted (concatenate 'string "boundary:" label) text 31) (check (concatenate 'string "boundary:" label ":exact-value-and-counter") (and (= 31 counter) (= 1 (length (occurrences map))) (item-value-p (first (occurrences map)) "*" *datatype*))))))) (defun numeric-controls () (dolist (spec '(("+0007" "7" "integer") ("-001" "-1" "integer") ("-00" "0" "integer") (".50" "0.5" "decimal") ("+01.2300" "1.23" "decimal") ("2001-01-01" "2001-01-01" "date") ("2000-01-01T01:00:00+01:00" "2000-01-01T00:00:00Z" "dateTime") ("\"001\"^^" "001" "integer"))) (destructuring-bind (literal value type) spec (let ((label (concatenate 'string "numeric:" literal))) (multiple-value-bind (map counter) (accepted label (format nil "topic occtype: ~A." literal) 31) (assert-occurrence label map value (concatenate 'string "http://www.w3.org/2001/XMLSchema#" type)) (check (concatenate 'string label ":counter") (= 31 counter))))))) (defun variant-controls () (let ((bare (accepted "variant:bare" "topic - \"N\" (* @s)." 19)) (qualified (accepted "variant:qualified" (format nil "topic - \"N\" (~A @s)." *qualified*) 19)) (string (accepted "variant:string" "topic - \"N\" (\"*\" @s)." 19))) (dolist (entry (list (list "bare" bare *datatype*) (list "qualified" qualified *datatype*) (list "string" string (string-datatype)))) (destructuring-bind (label map datatype) entry (let* ((topic (topic-maps.registered-original.tests::local map *doc* "topic")) (name (first (topic-names topic))) (variants (topic-name-variants name)) (v (first variants))) (check (concatenate 'string "variant:" label ":actual-Variant-value-datatype-parent-and-scope") (and (= 1 (length variants)) (equal "*" (variant-value v)) (equal datatype (variant-datatype v)) (eq name (variant-parent v)) (equal (list (topic-maps.registered-original.tests::local map *doc* "s")) (variant-scope v))))))) (check "variant:bare-qualified-CXTM-equal-string-distinct" (and (equalp (canonical bare) (canonical qualified)) (not (equalp (canonical bare) (canonical string))))))) (defun wildcard-controls () (multiple-value-bind (map counter) (accepted "counter:star-only" "topic occtype:*; other:*." 41) (check "counter:star-only-no-generated-identifiers" (and (= 41 counter) (= 2 (length (occurrences map))) (every (lambda (topic) (notany (lambda (iri) (search "#$__" iri)) (topic-item-identifiers topic))) (topic-map-topics map))))) (let ((source "? t:*. ?named t:*. ?named u:*. ? t:*.")) (multiple-value-bind (bare bare-counter) (accepted "counter:interleaved-bare" source 41) (multiple-value-bind (qualified qualified-counter) (accepted "counter:interleaved-qualified" (format nil "? t:~A. ?named t:~A. ?named u:~A. ? t:~A." *qualified* *qualified* *qualified* *qualified*) 41) (let* ((iris (loop for topic in (topic-map-topics bare) append (topic-item-identifiers topic))) (generated (remove-if-not (lambda (iri) (search "#$__" iri)) iris)) (expected (mapcar (lambda (suffix) (concatenate 'string *doc* suffix)) '("#$__42" "#$__43.named" "#$__44")))) (check "counter:only-question-wildcards-increment-and-named-identity-reused" (and (= 44 bare-counter qualified-counter) (topic-maps.registered-original.tests::same-set generated expected) (= 4 (length (occurrences bare))) (every (lambda (item) (item-value-p item "*" *datatype*)) (occurrences bare)) (equalp (canonical bare) (canonical qualified))))))))) (defun template-controls () (dolist (spec '(("argument" "def f($v) topic occtype:$v. end f(*)" :occurrence) ("body" "def f() topic occtype:*. end f()" :occurrence) ("nested-argument" "def outer($v) inner($v) end def inner($v) topic occtype:$v. end outer(*)" :occurrence) ("nested-body-argument" "def outer() inner(*) end def inner($v) topic occtype:$v. end outer()" :occurrence) ("variant-argument" "def f($v) topic - \"N\" ($v @s). end f(*)" :variant) ("variant-body" "def f() topic - \"N\" (* @s). end f()" :variant) ("unused-body" "def f() topic occtype:*. end" :empty))) (destructuring-bind (label text kind) spec (multiple-value-bind (map counter) (accepted (concatenate 'string "template:" label) text 53) (check (concatenate 'string "template:" label ":literal-and-counter") (and (= 53 counter) (ecase kind (:occurrence (and (= 1 (length (occurrences map))) (item-value-p (first (occurrences map)) "*" *datatype*))) (:variant (let* ((owner (topic-maps.registered-original.tests::local map *doc* "topic")) (name (first (topic-names owner))) (v (first (topic-name-variants name)))) (and v (equal "*" (variant-value v)) (equal *datatype* (variant-datatype v)) (eq name (variant-parent v))))) (:empty (and (null (topic-map-topics map)) (null (topic-map-associations map))))))))))) (defun token-controls () (dolist (entry '(("*" ((:ctm-integer "*" 0) (:eof nil 1))) ("*;." ((:ctm-integer "*" 0) (:punctuation #\; 1) (:punctuation #\. 2) (:eof nil 3))) ("? ?named * $v" ((:wildcard nil 0) (:wildcard "named" 2) (:ctm-integer "*" 9) (:variable "v" 11) (:eof nil 13))))) (destructuring-bind (text expected) entry (multiple-value-bind (tokens failure) (topic-maps.registered-original.tests::lexical-evidence text "tokens") (check (concatenate 'string "tokens:" text) (and (null failure) (equal expected tokens))))))) (defparameter *negative-specs* '( ("double-star" "topic occtype: **;." topic-maps.ctm:ctm-rejection :invalid-occurrence-literal 15 2 10) ("suffix-name" "topic occtype: *foo;." topic-maps.ctm:ctm-rejection :invalid-occurrence-literal 15 2 10) ("suffix-digit" "topic occtype: *1;." topic-maps.ctm:ctm-rejection :invalid-occurrence-literal 15 2 10) ("suffix-question" "topic occtype: *?;." topic-maps.ctm:ctm-rejection :invalid-occurrence-literal 15 2 10) ("suffix-colon" "topic occtype: *:;." topic-maps.ctm:ctm-rejection :invalid-occurrence-literal 15 2 10) ("signed-plus" "topic occtype: +*;." topic-maps.ctm:ctm-rejection :invalid-occurrence-literal 15 2 10) ("signed-minus" "topic occtype: -*;." topic-maps.ctm:ctm-rejection :expected-occurrence-value 15 2 10) ("date-looking" "topic occtype: *-1;." topic-maps.ctm:ctm-rejection :invalid-date-year 15 2 10) ("datetime-looking" "topic occtype: *T;." topic-maps.ctm:ctm-rejection :invalid-date-year 15 2 10) ("decimal-looking" "topic occtype: *.5;." topic-maps.ctm:ctm-rejection :invalid-occurrence-literal 15 2 10) ("qualified-bare" "topic occtype: *^^;." topic-maps.ctm:ctm-rejection :expected-separator 16 2 11) ("missing-separator" "topic occtype: * extra;." topic-maps.ctm:ctm-rejection :expected-separator 17 2 12) ("topic-position" "*." topic-maps.ctm:ctm-rejection :expected-topic-identity 0 1 1) ("name-position" "topic - *." topic-maps.ctm:ctm-rejection :expected-name-value 8 1 9) ("wildcard-value" "topic t: ?." topic-maps.ctm:ctm-rejection :expected-occurrence-value 9 1 10) ("named-wildcard-value" "topic t: ?named." topic-maps.ctm:ctm-rejection :expected-occurrence-value 9 1 10) ("variable-outside" "topic t: $value." topic-maps.ctm:unbound-template-variable :variable-outside-template 9 1 10) ("invalid-integer" "topic t: 42x." topic-maps.ctm:ctm-rejection :invalid-occurrence-literal 9 1 10) ("invalid-exponent" "topic t: 1e3." topic-maps.ctm:ctm-rejection :invalid-occurrence-literal 9 1 10) ("variant-suffix" "topic - \"Name\" (*x @s)." topic-maps.ctm:ctm-rejection :invalid-occurrence-literal 16 1 17) ("unterminated-string" "topic - \"Name." topic-maps.ctm:ctm-rejection :unterminated-string 8 1 9) ("unicode-escape" "topic - \"\\u0x00\"." topic-maps.ctm:ctm-rejection :invalid-unicode-escape 12 1 13) ("triple-unclosed" "topic t: \"\"\"x." topic-maps.ctm:ctm-rejection :unterminated-string 9 1 10) ("iso88591-still-unsupported" "%encoding \"iso-8859-1\" topic." topic-maps.ctm:ctm-unsupported :unsupported-encoding 11 1 12) ("template-topic-type" "def f($t) $t. end f(*)" topic-maps.ctm:invalid-template-argument :invalid-template-argument 10 1 11) ("template-name-type" "def f($n) topic - $n. end f(*)" topic-maps.ctm:invalid-template-argument :invalid-template-argument 18 1 19) )) (defun negative-controls () (dolist (spec *negative-specs*) (destructuring-bind (label text class code position line column) spec (multiple-value-bind (map condition) (captured-read (lambda () (topic-maps.ctm:deserialize-string text :document-iri *doc* :source-name label))) (check (concatenate 'string "negative:" label ":class-code-exact-position") (and (null map) condition (eq class (type-of condition)) (eq code (topic-maps.ctm:error-code condition)) (= position (topic-maps.ctm:error-position condition)) (= line (topic-maps.ctm:error-line condition)) (= column (topic-maps.ctm:error-column condition)) (eq :character (topic-maps.ctm:error-position-unit condition)) (equal label (topic-maps.ctm:error-source condition)) (if (eq class 'topic-maps.ctm:invalid-template-argument) (consp (topic-maps.ctm:error-context condition)) (null (topic-maps.ctm:error-context condition))) (null (topic-maps.ctm:error-cause condition)))) (push (fields "label" label "sourceText" text "condition" (condition-data condition)) *negatives*))))) (defun original-contract () (let* ((corpus topic-maps.ctm.tests::*cxtm-corpus*) (root (topic-maps.test-foundation:corpus-root corpus)) (id "ctm/in/occurrence-ctm-integer.ctm") (selected (topic-maps.test-foundation:select-cases corpus :id id)) (case (first selected)) (cases (topic-maps.test-foundation:corpus-cases corpus)) (previous (mapcar (lambda (c) (cons c (case-state c))) cases)) (old-results (copy-tree topic-maps.ctm.tests::*cxtm-results*)) (*doc* (topic-maps.test-foundation:test-case-document-iri case))) (setf *entry-statuses* (copy-list (getf (topic-maps.test-foundation:summarize-corpus corpus) :statuses))) (check "original:existing-positive-NOT-RUN-record-and-pinned-identities" (and (= 1 (length selected)) (member case cases :test #'eq) (eq :valid (topic-maps.test-foundation:test-case-category case)) (eq :cxtm-equal (topic-maps.test-foundation:test-case-expected-outcome case)) (eq :not-run (topic-maps.test-foundation:test-case-status case)) (null (topic-maps.test-foundation:test-case-actual-outcome case)) (equal *doc* (format nil "https://ctm-tests.example.invalid/~A/~A" (getf (topic-maps.test-foundation:corpus-metadata corpus) :commit) id)) (equal (truename (merge-pathnames id root)) (truename (topic-maps.test-foundation:test-case-source-pathname case))))) (check "original:unchanged-eighteen-source-bytes" (equalp (binary (topic-maps.test-foundation:test-case-source-pathname case)) (map '(vector (unsigned-byte 8)) #'char-code (format nil "topic~%occtype: *;.")))) (multiple-value-bind (row result) (topic-maps.registered-original.tests::exercise-case case :occurrences) (setf *original* row) (check "original:actual-356-byte-original-comparison-PASS" (and result (getf result :match) (= 356 (getf result :bytes)) (eq :pass (topic-maps.test-foundation:test-case-status case)))) (multiple-value-bind (control counter) (accepted "original:qualified-control" (format nil "topic~%occtype: ~A;." *qualified*)) (assert-occurrence "original:qualified-control" control "*" *datatype*) (check "original:qualified-control-356-byte-baseline-match-and-zero-counter" (and (= 0 counter) (= 356 (length (canonical control))) (equalp (canonical control) (binary (topic-maps.test-foundation:test-case-expected-baseline case))) (equal (getf result :actual) (topic-maps.ctm::decode-utf8 (canonical control)))))) (check "original:all-prior-case-records-and-observations-preserved" (and (eq corpus topic-maps.ctm.tests::*cxtm-corpus*) (equal cases (topic-maps.test-foundation:corpus-cases corpus)) (equal old-results topic-maps.ctm.tests::*cxtm-results*) (every (lambda (entry) (if (eq case (car entry)) (equal (first (cdr entry)) (identity-fields case)) (equal (cdr entry) (case-state (car entry))))) previous))) (setf topic-maps.ctm.tests::*cxtm-results* (append topic-maps.ctm.tests::*cxtm-results* (list result)))) (let ((after (getf (topic-maps.test-foundation:summarize-corpus corpus) :statuses))) (check "original:exactly-one-NOT-RUN-to-PASS-no-other-status-changes" (and (= (1+ (getf *entry-statuses* :pass)) (getf after :pass)) (= (1- (getf *entry-statuses* :not-run)) (getf after :not-run)) (= (getf *entry-statuses* :fail) (getf after :fail)) (= (getf *entry-statuses* :unsupported) (getf after :unsupported))))) (format t "~&CTM-INTEGER-ORIGINAL ~A: PASS; 356 byte-exact CXTM bytes~%" id))) (defun run-tests () (unless topic-maps.ctm.tests::*cxtm-corpus* (topic-maps.registered-original.tests:run-tests) (topic-maps.registered-original.tests:run-negative-tests) (topic-maps.prolog.tests:run-tests) (topic-maps.registered-original.tests:run-empty2-tests)) (setf *positives* nil *negatives* nil *original* nil) (let ((topic-maps.registered-original.tests::*checks* nil)) (positive-controls) (numeric-controls) (variant-controls) (wildcard-controls) (template-controls) (token-controls) (negative-controls) (original-contract) (check "ctm-integer:unique-check-names" (= (length topic-maps.registered-original.tests::*checks*) (length (remove-duplicates topic-maps.registered-original.tests::*checks* :test #'equal)))) (setf *checks* (nreverse topic-maps.registered-original.tests::*checks*))) (setf *positives* (nreverse *positives*) *negatives* (nreverse *negatives*)) (format t "~&CTM-INTEGER-PASS: ~D checks; one original plus ~D positive and ~D negative controls~%" (length *checks*) (length *positives*) (length *negatives*)) t) (defun write-reports (prefix) (with-open-file (out (concatenate 'string prefix ".literal.json") :direction :output :if-exists :supersede) (topic-maps.test-foundation::emit-json-value (fields "kind" "ctm-integer-literal-execution" "passed" (length *checks*) "failed" 0 "entryStatuses" (topic-maps.test-foundation::counts-object *entry-statuses*) "checks" (coerce *checks* 'vector) "positives" (coerce *positives* 'vector) "negativeControls" (coerce *negatives* 'vector) "original" *original*) out) (terpri out)) (topic-maps.ctm.tests::write-cxtm-report (concatenate 'string prefix ".validation.json") (concatenate 'string prefix ".cxtm.json") :additional-checks *checks* :scope "Slice 20F: original CTM '*' literal, lexical value '*' and CTM-integer datatype. Exact original/qualified CXTM, Occurrence/Variant and existing Template literal paths, token diagnostics and unchanged Wildcard counters; previous contracts and four known FAILs preserved."))