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