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