]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - tests/cxtm.lisp
Add XTM Variant reification and qualify Slice 21O originals
[topic-maps.git] / tests / cxtm.lisp
1 (in-package #:topic-maps.ctm.tests)
2
3 (defparameter *cxtm-ids*
4 '("simple" "subject-identifier" "subject-locator" "item-identifier" "name"
5 "topic-subject-identifier" "topic-subject-identifier-wrapped" "topic-subject-identifier-duplicate"
6 "topic-subject-locator" "topic-subject-locator-wrapped" "topic-subject-locator-semi"
7 "topic-subject-locator-duplicate" "topic-item-identifier" "topic-item-identifier-wrapped"
8 "topic-item-identifier-duplicate" "name-duplicate" "association" "association-binary"
9 "association-binary-duplicate" "association-type-sid" "association-with-comment"
10 "association-with-comment2" "association-with-comment3" "association-with-comment4"))
11 (defvar *last-cxtm-checks* nil)
12 (defvar *cxtm-results* nil)
13 (defvar *cxtm-corpus* nil)
14 (defstruct (extended-cxtm-map (:include topic-maps.tmdm:topic-map)) occurrences)
15 (defun cxtm (map &optional (base "https://example.invalid/a/doc.ctm"))
16 (topic-maps.cxtm:canonicalize map :base-locator base))
17 (defun binary-file (path)
18 (with-open-file (in path :element-type '(unsigned-byte 8))
19 (let ((out (make-array (file-length in) :element-type '(unsigned-byte 8))))
20 (read-sequence out in) out)))
21 (defun cxtm-condition (thunk)
22 (handler-case (progn (funcall thunk) nil) (topic-maps.cxtm:cxtm-error (c) c)))
23 (defun map-state (map)
24 ;; Includes object handles, all forward links, raw stored references/sets.
25 (list (topic-maps.tmdm:item-reifier map) (topic-maps.tmdm:item-identifiers map)
26 (copy-list (topic-maps.tmdm::%topic-map-topics map))
27 (copy-list (topic-maps.tmdm::%topic-map-associations map))
28 (loop for topic in (topic-map-topics map) collect
29 (list topic (topic-maps.tmdm::%topic-forward topic) (topic-parent topic)
30 (topic-item-identifiers topic) (topic-subject-identifiers topic) (topic-subject-locators topic)
31 (loop for n in (topic-names topic) collect
32 (list n (topic-maps.tmdm:item-reifier n) (topic-maps.tmdm:item-identifiers n) (topic-maps.tmdm::%topic-name-forward n) (topic-name-parent n)
33 (topic-name-type n) (topic-name-value n) (copy-list (topic-maps.tmdm::%topic-name-scope n))
34 (loop for v in (topic-maps.tmdm::%topic-name-variants n) collect
35 (list v (topic-maps.tmdm:item-reifier v) (topic-maps.tmdm:item-identifiers v) (topic-maps.tmdm::%variant-forward v) (topic-maps.tmdm::%variant-parent v)
36 (topic-maps.tmdm::%variant-value v) (topic-maps.tmdm::%variant-datatype v)
37 (copy-list (topic-maps.tmdm::%variant-scope v))))))
38 (loop for o in (topic-maps.tmdm:topic-occurrences topic) collect
39 (list o (topic-maps.tmdm:item-reifier o) (topic-maps.tmdm::%occurrence-forward o) (topic-maps.tmdm:occurrence-parent o)
40 (topic-maps.tmdm:occurrence-type o) (topic-maps.tmdm:occurrence-value o)
41 (topic-maps.tmdm:occurrence-datatype o) (topic-maps.tmdm:occurrence-item-identifiers o)
42 (copy-list (topic-maps.tmdm::%occurrence-scope o))))))
43 (loop for a in (topic-maps.tmdm:topic-map-associations map) collect
44 (list a (topic-maps.tmdm:item-reifier a) (topic-maps.tmdm:item-identifiers a) (topic-maps.tmdm::%association-forward a) (topic-maps.tmdm:association-type a)
45 (topic-maps.tmdm:association-parent a) (copy-list (topic-maps.tmdm::%association-scope a))
46 (loop for r in (topic-maps.tmdm:association-roles a) collect
47 (list r (topic-maps.tmdm:item-reifier r) (topic-maps.tmdm:item-identifiers r) (topic-maps.tmdm::%association-role-forward r)
48 (topic-maps.tmdm:association-role-type r) (topic-maps.tmdm:association-role-player r)
49 (topic-maps.tmdm:association-role-parent r)))))))
50 (defun compare-cxtm-cases ()
51 (let ((corpus (topic-maps.test-foundation:discover-corpus)) (results nil))
52 (dolist (name *cxtm-ids*)
53 (let* ((id (format nil "ctm/in/~A.ctm" name))
54 (case (first (topic-maps.test-foundation:select-cases corpus :id id)))
55 (base (topic-maps.test-foundation:test-case-document-iri case)))
56 (setf (topic-maps.test-foundation:test-case-clauses case)
57 (append (topic-maps.test-foundation:test-case-clauses case)
58 '("CXTM 3.1" "CXTM 3.3" "CXTM 3.4" "CXTM 3.5" "CXTM 3.11" "CXTM 3.12"
59 "CXTM 3.13" "CXTM 3.17" "CXTM 3.18" "CXTM 3.19" "CXTM 3.20"
60 "CXTM 3.21" "CXTM 3.22" "CXTM 3.23" "CXTM 4.1" "CXTM 4.3"
61 "CXTM 4.4" "CXTM 4.5" "CXTM 4.6")
62 (when (search "name" name) '("CXTM 3.6" "CXTM 3.15" "CXTM 3.16" "CXTM 4.7"))
63 (when (search "association" name)
64 '("CXTM 3.9" "CXTM 3.10" "CXTM 3.15" "CXTM 4.10" "CXTM 4.11"))))
65 (handler-case
66 (let* ((map (deserialize-file (topic-maps.test-foundation:test-case-source-pathname case)
67 :document-iri base :source-name id))
68 (before (map-state map))
69 (actual (topic-maps.cxtm:canonicalize-octets map :base-locator base))
70 (expected (binary-file (topic-maps.test-foundation:test-case-expected-baseline case)))
71 (different (mismatch actual expected)))
72 (check (concatenate 'string id ":canonicalization-is-pure") (equal before (map-state map)))
73 (topic-maps.test-foundation:record-result case (if different :cxtm-mismatch :cxtm-match)
74 :diagnostics (when different (list (format nil "First different byte: ~D; actual ~D bytes, expected ~D bytes."
75 different (length actual) (length expected)))))
76 (push (list :id id :base base :actual (cxtm map base)
77 :expected (uiop:read-file-string (topic-maps.test-foundation:test-case-expected-baseline case)
78 :external-format :utf-8)
79 :match (null different) :bytes (length actual)) results))
80 ((or ctm-unsupported topic-maps.cxtm:cxtm-unsupported) (c)
81 (topic-maps.test-foundation:record-result case :unsupported :diagnostics (list (princ-to-string c))))
82 (error (c) (topic-maps.test-foundation:record-result case :execution-error :diagnostics (list (princ-to-string c)))))
83 (format t "~&CXTM ~A: ~A~{ ~A~}~%" id (topic-maps.test-foundation:test-case-status case)
84 (topic-maps.test-foundation:test-case-diagnostics case))))
85 (let* ((case (first (topic-maps.test-foundation:select-cases corpus :id "ctm/invalid/prefix-undefined.ctm")))
86 (condition (handler-case
87 (progn (deserialize-file (topic-maps.test-foundation:test-case-source-pathname case)
88 :document-iri (topic-maps.test-foundation:test-case-document-iri case)) nil)
89 (undefined-prefix (c) c))))
90 (check "cxtm-executor-retains-specific-negative-contract"
91 (and condition (eq :undefined-prefix (error-code condition))))
92 (topic-maps.test-foundation:record-result case :ctm-rejection :diagnostics (list (princ-to-string condition))))
93 (setf results (nreverse results) *cxtm-corpus* corpus *cxtm-results* results)
94 (check "24-original-byte-comparisons-pass"
95 (and (= 24 (length results)) (every (lambda (r) (getf r :match)) results)))
96 (check "25-imported-passes-248-unexecuted"
97 (equal (getf (topic-maps.test-foundation:summarize-corpus corpus) :statuses)
98 '(:pass 25 :fail 0 :unsupported 0 :not-run 248)))))
99
100 (defun permutations (items)
101 (if (null items) (list nil)
102 (loop for item in items append
103 (mapcar (lambda (rest) (cons item rest)) (permutations (remove item items :count 1))))))
104
105 (defun canonical-order-tests ()
106 (let* ((map (read-text "z. a. a - \"Zulu\"; - \"Alpha\";. a - \"Middle\";."))
107 (out (cxtm map)))
108 (check "topic-order-not-insertion-order" (< (search "#a<" out) (search "#z<" out)))
109 (check "name-value-order" (< (search "<value>Alpha" out) (search "<value>Middle" out) (search "<value>Zulu" out)))
110 (check "names-numbered-within-parent" (and (search "<name number=\"1\">" out) (search "<name number=\"3\">" out)))
111 (check "root-has-mandated-final-lf" (char= #\Newline (char out (1- (length out))))))
112 (let ((map (topic-maps.tmdm:make-topic-map)))
113 (let ((many (api-topic map "urn:ii:a")) (one (api-topic map "urn:ii:z")))
114 (topic-maps.tmdm:add-topic-identifier many :item-identifier "urn:ii:b")
115 (check "identifier-set-size-before-lexical-value"
116 (< (search "urn:ii:z" (cxtm map)) (search "urn:ii:a" (cxtm map))))
117 (topic-maps.tmdm:add-topic-identifier one :subject-locator "urn:sl:z")
118 (check "topic-si-then-sl-then-ii" (< (search "urn:ii:a" (cxtm map)) (search "urn:sl:z" (cxtm map))))))
119 (let* ((text "a(z: b, y: c) a(y: b) d(y: b)") (out (cxtm (read-text text))))
120 (check "association-role-set-cardinality-first" (< (search "<association number=\"1\">" out)
121 (search "<association number=\"2\">" out)))
122 (check "role-player-before-type" (search (format nil "<role number=\"1\">~%<player topicref=\"2\"></player>~%<type topicref=\"6\"></type>") out))
123 (check "rolePlayed-parent-tie-order"
124 (< (search "ref=\"association.1.role.1\"" out) (search "ref=\"association.3.role.1\"" out)
125 (search "ref=\"association.2.role.1\"" out))))
126 (let* ((map (read-text "a.")) (state (map-state map)))
127 (uiop:with-temporary-file (:pathname path :type "cxtm")
128 (topic-maps.cxtm:write-cxtm map path :base-locator "https://example.invalid/a/doc.ctm")
129 (check "file-api-exact-canonical-octets"
130 (equalp (binary-file path) (topic-maps.cxtm:canonicalize-octets map :base-locator "https://example.invalid/a/doc.ctm")))
131 (check "file-api-does-not-mutate-map" (equal state (map-state map)))))
132 (let ((reference nil))
133 (dolist (order (permutations '("a" "b" "r" "s")))
134 (dolist (reverse '(nil t))
135 (let ((map (topic-maps.tmdm:make-topic-map)) (table (make-hash-table :test #'equal)))
136 (dolist (id order) (setf (gethash id table) (api-topic map (concatenate 'string "urn:item:" id))))
137 (dolist (roles (if reverse '(("s" "a") ("r" "b")) '(("r" "b") ("s" "a"))))
138 (topic-maps.tmdm:make-association map (gethash "a" table)
139 (list (mapcar (lambda (id) (gethash id table)) roles))))
140 (let ((out (cxtm map)))
141 (unless reference (setf reference out))
142 (check (format nil "construction-permutation:~{~A~}:~A" order reverse) (string= out reference)))))))
143 (let* ((map (read-text "a(r: b, s: c)")) (expected (cxtm map))
144 (*print-base* 16) (*print-radix* t) (*print-escape* nil))
145 (check "independent-of-lisp-printer-settings" (string= expected (cxtm map)))))
146
147 (defun canonical-merge-tests ()
148 ;; Force Topic, Name, Association and Role replacement in one connected map.
149 (let* ((map (read-text "a - \"Same\";. b - \"Same\";. a(r: a, s: b) b(r: b, s: a)"))
150 (topics (topic-map-topics map))
151 (a (find "https://example.invalid/a/doc.ctm#a" topics :test #'string= :key (lambda (x) (first (topic-item-identifiers x)))))
152 (b (find "https://example.invalid/a/doc.ctm#b" topics :test #'string= :key (lambda (x) (first (topic-item-identifiers x)))))
153 (r (find "https://example.invalid/a/doc.ctm#r" topics :test #'string= :key (lambda (x) (first (topic-item-identifiers x)))))
154 (s (find "https://example.invalid/a/doc.ctm#s" topics :test #'string= :key (lambda (x) (first (topic-item-identifiers x)))))
155 (names (append (topic-names a) (topic-names b)))
156 (associations (topic-maps.tmdm:topic-map-associations map))
157 (roles (mapcan #'topic-maps.tmdm:association-roles (copy-list associations))))
158 (topic-maps.tmdm:merge-topics a b)
159 (topic-maps.tmdm:merge-topics r s)
160 (let* ((expected (read-text "a ^<#b>; - \"Same\";. r ^<#s>;. a(r: a)"))
161 (state (map-state map)) (out (cxtm map)))
162 (check "all-four-merge-kinds-canonical-output" (string= out (cxtm expected)))
163 (check "merged-output-is-pure" (equal state (map-state map)))
164 (check "old-topic-alias-survives-canonicalization" (eq (topic-maps.tmdm:canonical-topic a) (topic-maps.tmdm:canonical-topic b)))
165 (check "old-name-aliases-survive-canonicalization" (eq (topic-maps.tmdm:canonical-topic-name (first names)) (topic-maps.tmdm:canonical-topic-name (second names))))
166 (check "old-association-aliases-survive-canonicalization"
167 (apply #'eq (mapcar #'topic-maps.tmdm:canonical-association associations)))
168 (check "all-old-role-aliases-survive-canonicalization"
169 (every (lambda (role) (eq (topic-maps.tmdm:canonical-association-role role)
170 (topic-maps.tmdm:canonical-association-role (first roles)))) roles))
171 (check "postmerge-reference-integrity" (association-invariants map)))))
172
173 (defun unicode-and-locator-tests ()
174 (let ((topic-maps.cxtm::*base-locator* "http://example.org/a/b.ctm?query#fragment")
175 (topic-maps.cxtm::*locator-cache* (make-hash-table :test #'equal)))
176 (dolist (pair '(("http://example.org/a/b.ctm#id" "#id") ("http://example.org/a/c.ctm#id" "c.ctm#id")
177 ("http://example.org/x" "x") ("http://other.example/x" "http://other.example/x")
178 ("http://example.org/a/b.ctmextra" "extra") ("http://example.org/a/b.ctm" "")))
179 (check (concatenate 'string "normative-locator-prefix:" (first pair))
180 (string= (second pair) (topic-maps.cxtm::normalized-locator (first pair))))))
181 (dolist (base '("http://example.org/a/" "http://example.org/a///" "urn:base:document"))
182 (let ((topic-maps.cxtm::*base-locator* base) (topic-maps.cxtm::*locator-cache* (make-hash-table :test #'equal)))
183 (check (concatenate 'string "trailing-slash-or-opaque:" base)
184 (string= "#x" (topic-maps.cxtm::normalized-locator (concatenate 'string (string-right-trim "/" base) "#x"))))))
185 (dolist (pair '(((101 769) (233)) ((65 778) (197)) ((8491) (197)) ((4352 4449 4520) (44033))
186 ((44033) (44033)) ((97 769 807) (225 807)) ((65 768 769) (192 769))
187 ((769 768) (769 768)) ((97 847 769) (97 847 769)) ((832) (768))))
188 (let ((text (map 'string #'code-char (first pair))) (expected (map 'string #'code-char (second pair))))
189 (check (format nil "nfc:~S" (first pair)) (string= expected (topic-maps.cxtm::nfc text)))))
190 (let* ((map (read-text "a."))
191 (value (concatenate 'string "<&>\"" (string #\Return) (string #\Newline) (string #\Tab)
192 (map 'string #'code-char '(101 769 #x1f600)))))
193 (topic-maps.tmdm:add-default-name (first-topic map) value)
194 (let ((out (cxtm map)) (bytes (topic-maps.cxtm:canonicalize-octets map :base-locator "https://example.invalid/a/doc.ctm")))
195 (check "canonical-xml-character-escaping" (search "&lt;&amp;&gt;\"&#xD;" out))
196 (check "nfc-name-emitted" (find (code-char 233) out))
197 (check "utf8-supplementary-character" (search #(#xf0 #x9f #x98 #x80) bytes))
198 (check "no-bom-mandated-final-newline" (and (= 60 (aref bytes 0)) (= 10 (aref bytes (1- (length bytes))))))))
199 (let ((map (read-text "a.")))
200 (topic-maps.tmdm:add-default-name (first-topic map) (string (code-char 0)))
201 (check "xml-invalid-character-explicit-error"
202 (eq :xml-character (topic-maps.cxtm:error-code (cxtm-condition (lambda () (cxtm map)))))))
203 (check "newer-unicode-character-explicit-unsupported"
204 (typep (cxtm-condition (lambda () (topic-maps.cxtm::nfc (string (code-char #x1c89)))))
205 'topic-maps.cxtm:cxtm-unsupported))
206 (check "base-locator-required" (eq :invalid-base-locator (topic-maps.cxtm:error-code
207 (cxtm-condition (lambda () (topic-maps.cxtm:canonicalize (read-text "a.")))))))
208 (check "unsupported-model-input-explicit"
209 (typep (cxtm-condition (lambda () (cxtm '(:occurrence "unsupported")))) 'topic-maps.cxtm:cxtm-unsupported))
210 (check "extended-model-property-not-silently-discarded"
211 (eq :unsupported-information-item
212 (topic-maps.cxtm:error-code (cxtm-condition
213 (lambda () (cxtm (make-extended-cxtm-map :occurrences '("unsupported"))))))))
214 (let* ((map (read-text "a(r: b)"))
215 (a (first (topic-maps.tmdm:topic-map-associations map)))
216 (role (first (topic-maps.tmdm:association-roles a)))
217 (foreign (api-topic (topic-maps.tmdm:make-topic-map) "urn:foreign")))
218 ;; Deliberately corrupt only this fixture to exercise defensive validation.
219 (setf (topic-maps.tmdm::%association-role-player role) foreign)
220 (let ((state (map-state map)))
221 (check "foreign-role-player-explicit-error"
222 (eq :invalid-topic-reference (topic-maps.cxtm:error-code (cxtm-condition (lambda () (cxtm map))))))
223 (check "error-path-is-pure" (equal state (map-state map)))))
224 (let ((map (topic-maps.tmdm:make-topic-map)))
225 (api-topic map (concatenate 'string "urn:id:" (map 'string #'code-char '(101 769))))
226 (api-topic map (concatenate 'string "urn:id:" (string (code-char 233))))
227 (check "nfc-identity-tie-explicit-unsupported"
228 (eq :ambiguous-topic-order (topic-maps.cxtm:error-code (cxtm-condition (lambda () (cxtm map))))))))
229
230 (defun run-cxtm-tests ()
231 (let ((*checks* nil))
232 (compare-cxtm-cases)
233 (canonical-order-tests) (canonical-merge-tests) (canonical-graph-tests) (unicode-and-locator-tests)
234 (setf *last-cxtm-checks* (nreverse *checks*))
235 (format t "~&CXTM-PASS: ~D checks; 24 original byte comparisons; 25 imported PASS, 248 NOT-RUN.~%" (length *last-cxtm-checks*)) t))
236
237 (defun write-cxtm-report (validation-path detail-path &key additional-checks
238 (scope "Slice 4: 24 byte-equal original baselines, explicit base locator"))
239 (topic-maps.test-foundation:write-validation-report *cxtm-corpus* validation-path
240 :infrastructure-checks topic-maps.tests::*last-checks*
241 :implementation (list :ctm t :tmdm t :cxtm t :scope scope))
242 (with-open-file (out detail-path :direction :output :if-exists :supersede :if-does-not-exist :create)
243 (topic-maps.test-foundation::emit-json-value
244 (topic-maps.test-foundation::fields
245 "kind" "cxtm-byte-equality" "passed" (+ (length *last-cxtm-checks*) (length additional-checks)) "failed" 0
246 "checkOutcomeInterpretation" "passed/failed count local assertions; contractCounts and case status report imported comparisons independently"
247 "contractCounts" (topic-maps.test-foundation::fields "pass" (count-if (lambda (r) (getf r :match)) *cxtm-results*)
248 "fail" (count-if-not (lambda (r) (getf r :match)) *cxtm-results*))
249 "checks" (topic-maps.test-foundation::json-array (append *last-cxtm-checks* additional-checks))
250 "cases" (topic-maps.test-foundation::json-array
251 (loop for r in *cxtm-results* collect
252 (topic-maps.test-foundation::fields "id" (getf r :id) "baseLocator" (getf r :base)
253 "status" (if (getf r :match) "pass" "fail") "byteCount" (getf r :bytes)
254 "actualCXTM" (getf r :actual) "expectedCXTM" (getf r :expected))))) out)
255 (terpri out)))
256
257 (defun canonical-graph-tests ()
258 ;; The 64 independent edge sets induce different identity partitions. Compare
259 ;; full CXTM after both edge insertion orders, including all live references.
260 (let ((possible '((0 1) (0 2) (0 3) (1 2) (1 3) (2 3))))
261 (dotimes (mask 64)
262 (let ((edges (loop for edge in possible for bit from 0 when (logbitp bit mask) collect edge))
263 (reference nil))
264 (dolist (reverse '(nil t))
265 (let* ((map (topic-maps.tmdm:make-topic-map))
266 (topics (loop for i below 4 collect (api-topic map (format nil "urn:node:~D" i))))
267 (a (topic-maps.tmdm:make-association map (first topics)
268 (list (list (second topics) (third topics)) (list (third topics) (fourth topics))))))
269 (declare (ignore a))
270 (dolist (edge (if reverse (reverse edges) edges))
271 (dolist (node edge)
272 (topic-maps.tmdm:add-topic-identifier (nth node topics) :subject-identifier
273 (format nil "urn:edge:~D:~D" (first edge) (second edge)))))
274 (let* ((state (map-state map)) (out (cxtm map)))
275 (if reference
276 (check (format nil "canonical-graph:~D:edge-order-independent" mask) (string= reference out))
277 (setf reference out))
278 (check (format nil "canonical-graph:~D:~A:pure" mask reverse) (equal state (map-state map)))
279 (check (format nil "canonical-graph:~D:~A:references" mask reverse) (association-invariants map)))))))))