]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - tests/ctm-integers.lisp
feat(xtm): support anyURI resource data on name variants
[topic-maps.git] / tests / ctm-integers.lisp
1 (in-package #:topic-maps.ctm.tests)
2
3 (defparameter *integer-ids*
4 '("occurrence-integer" "occurrence-integer2" "occurrence-integer-plus-sign"
5 "occurrence-integer-minus-sign" "occurrence-integer-leading-zeros" "occurrence-explicit-datatype2"))
6 (defvar *last-integer-checks* nil)
7 (defvar *integer-observations* nil)
8 (defvar *integer-probes* nil)
9 (defun integer-checks () *last-integer-checks*)
10 (defun integer-values (map) (mapcar #'topic-maps.tmdm:occurrence-value (all-occurrences map)))
11 (defun serialized-values (text)
12 (loop with start = 0 for open = (search "<value>" text :start2 start) while open
13 for close = (search "</value>" text :start2 (+ open 7))
14 collect (subseq text (+ open 7) close) do (setf start (+ close 8))))
15
16 (defun exercise-integer-cases ()
17 (setf *integer-observations* nil)
18 (dolist (name *integer-ids*)
19 (let* ((id (format nil "ctm/in/~A.ctm" name))
20 (case (first (topic-maps.test-foundation:select-cases *cxtm-corpus* :id id)))
21 (doc (topic-maps.test-foundation:test-case-document-iri case))
22 (map (deserialize-file (topic-maps.test-foundation:test-case-source-pathname case) :document-iri doc :source-name id))
23 (zeros (string= name "occurrence-integer-leading-zeros"))
24 (owner (local-topic map "topic" doc)) (items (all-occurrences map))
25 (values (cond (zeros '("1" "1" "-1" "100" "0" "0" "0"))
26 ((string= name "occurrence-integer-minus-sign") '("-1"))
27 ((string= name "occurrence-explicit-datatype2") '("42")) (t '("1")))))
28 (setf (topic-maps.test-foundation:test-case-clauses case)
29 '("CTM 3.2" "CTM 3.3.11" "CTM 3.4.1" "CTM 3.10" "CTM 3.16.1"
30 "TMDM 4.3-4.4" "TMDM 5.6" "TMDM 6.5" "CXTM 3.8" "CXTM 3.14" "CXTM 3.16"
31 "CXTM 4.2-4.3" "CXTM 4.9" "XSD 3.3.13.1-3.3.13.2" "XSD 4.3.6"))
32 (check (concatenate 'string name ":no-extra-items")
33 (and (= (if zeros 8 2) (length (topic-map-topics map))) (= (length values) (length items))
34 (notany #'topic-names (topic-map-topics map)) (null (topic-maps.tmdm:topic-map-associations map))))
35 (check (concatenate 'string name ":tmdm-values") (equal values (integer-values map)))
36 (check (concatenate 'string name ":integer-datatype-not-topic")
37 (and (every (lambda (o) (string= (topic-maps.tmdm:integer-datatype) (topic-maps.tmdm:occurrence-datatype o))) items)
38 (every (lambda (topic) (and (= 1 (length (topic-item-identifiers topic)))
39 (null (topic-subject-identifiers topic)) (null (topic-subject-locators topic))))
40 (topic-map-topics map))))
41 (loop for o in items for index from 1 do
42 (check (format nil "~A:stored-references:~D" name index)
43 (and (eq owner (topic-maps.tmdm::%occurrence-parent o))
44 (eq (local-topic map (if zeros (format nil "o~D" index) "occtype") doc) (topic-maps.tmdm::%occurrence-type o))
45 (null (topic-maps.tmdm:occurrence-item-identifiers o)))))
46 (let* ((state (map-state map)) (actual (topic-maps.cxtm:canonicalize-octets map :base-locator doc))
47 (expected (binary-file (topic-maps.test-foundation:test-case-expected-baseline case)))
48 (different (mismatch actual expected)))
49 (topic-maps.test-foundation:record-result case (if different :cxtm-mismatch :cxtm-match)
50 :diagnostics (when different (list (format nil "First mismatch byte ~D; actual ~D bytes, expected ~D bytes."
51 different (length actual) (length expected)))))
52 (check (concatenate 'string name ":byte-exact-cxtm") (null different))
53 (check (concatenate 'string name ":pure") (equal state (map-state map)))
54 (setf *cxtm-results* (append *cxtm-results*
55 (list (list :id id :base doc :actual (cxtm map doc)
56 :expected (uiop:read-file-string (topic-maps.test-foundation:test-case-expected-baseline case) :external-format :utf-8)
57 :match (null different) :bytes (length actual)))))
58 (format t "~&INTEGER-CXTM ~A: PASS (~D bytes)~%" id (length actual)))
59 (push (topic-maps.test-foundation::fields "id" id "documentIRI" doc "status" "pass"
60 "ctmSource" (uiop:read-file-string (topic-maps.test-foundation:test-case-source-pathname case) :external-format :utf-8)
61 "actualObjects" (occurrence-observation map)) *integer-observations*)))
62 (setf *integer-observations* (nreverse *integer-observations*))
63 (check "slice7:42-passes-231-unexecuted"
64 (equal (getf (topic-maps.test-foundation:summarize-corpus *cxtm-corpus*) :statuses)
65 '(:pass 42 :fail 0 :unsupported 0 :not-run 231))))
66
67 (defun integer-lexical-tests ()
68 (setf *integer-probes* nil)
69 (loop for entry in '(("0" "0") ("-0" "0") ("+0" "0") ("00000" "0") ("-00000" "0")
70 ("+000001" "1") ("-000001" "-1") ("00100" "100") ("+42" "42")
71 ("9223372036854775808" "9223372036854775808") ("-9223372036854775809" "-9223372036854775809"))
72 for index from 0 do
73 (destructuring-bind (value expected) entry
74 (check (format nil "integer-valid-lexical:~D" index) (topic-maps.tmdm:integer-lexical-p value))
75 (check (format nil "integer-canonical-form:~D" index) (equal expected (topic-maps.tmdm:canonical-integer-lexical-form value)))))
76 ;; Independent Python arithmetic checks each recorded output, including 10,000
77 ;; digits. No randomized implementation-dependent state is used in the report.
78 (let ((inputs (append '("0" "+001" "-001" "-000" "000100")
79 (list (format nil " ~C+00042~C~C " #\Tab #\Return #\Newline)
80 (make-string 10000 :initial-element #\9)
81 (concatenate 'string "-000" (make-string 10000 :initial-element #\9)))
82 (loop for i below 64 collect
83 (concatenate 'string (nth (mod i 3) '("" "+" "-")) (make-string (mod i 8) :initial-element #\0)
84 (coerce (loop for j below (+ 1 (* i 2)) collect (char "0123456789" (mod (+ (* i 7) (* j j) j) 10))) 'string))))))
85 (loop for input in inputs for index from 0 do
86 (dolist (mode '(:implicit :explicit))
87 (unless (and (eq mode :implicit) (find-if (lambda (c) (find c '(#\Space #\Tab #\Return #\Newline))) input))
88 (let* ((source (if (eq mode :implicit) (format nil "a t: ~A." input)
89 (format nil "a t:\"~A\"^^<http://www.w3.org/2001/XMLSchema#integer>." input)))
90 (map (read-text source)) (o (first (all-occurrences map))) (state (map-state map)) (out (cxtm map))
91 (stored (topic-maps.tmdm:occurrence-value o)) (expected (topic-maps.tmdm:canonical-integer-lexical-form input)))
92 (check (format nil "integer-probe-value:~D:~A" index mode) (string= (if (eq mode :implicit) expected input) stored))
93 (check (format nil "integer-probe-output:~D:~A" index mode) (equal (list expected) (serialized-values out)))
94 (check (format nil "integer-probe-purity:~D:~A" index mode) (equal state (map-state map)))
95 (push (topic-maps.test-foundation::fields "input" input "mode" (string-downcase (symbol-name mode))
96 "storedValue" stored "datatype" (topic-maps.tmdm:occurrence-datatype o) "actualCXTM" out) *integer-probes*))))))
97 (setf *integer-probes* (nreverse *integer-probes*))
98 (let* ((lexer (topic-maps.ctm::make-lexer "+001."))
99 (n (topic-maps.ctm::next-token lexer)) (dot (topic-maps.ctm::next-token lexer)))
100 (check "integer-lexer-keeps-original-spelling" (and (eq :integer (topic-maps.ctm::token-kind n)) (equal "+001" (topic-maps.ctm::token-value n))))
101 (check "integer-lexer-leaves-topic-terminator" (and (eq :punctuation (topic-maps.ctm::token-kind dot)) (eql #\. (topic-maps.ctm::token-value dot))
102 (= 4 (topic-maps.ctm::token-position dot)))))
103 (dolist (source (list "a t: 1." "a t: 1;." "a t: 1#(comment)#." (format nil "a t: 1# comment~%.")))
104 (check (concatenate 'string "integer-boundary:" source) (equal '("1") (integer-values (read-text source)))))
105 (check "integer-terminator-next-topic" (equal '("1") (integer-values (read-text "a t: 1.b."))))
106 (check "integer-semicolon-next-occurrence" (equal '("1" "2") (integer-values (read-text "a t: 1;t: 2.")))))
107
108 (defun integer-error-tests ()
109 (loop for value in (append '("" "+" "-" "1.0" "1." "1e3" "1 2" "#x10" "- 1" "+-1" "++1" "1/2" "NaN" "INF" "١")
110 (list (concatenate 'string (string (code-char 160)) "1"))) for index from 0 do
111 (let* ((text (format nil "a t:\"~A\"^^<http://www.w3.org/2001/XMLSchema#integer>." value)) (condition (ctm-condition text)))
112 (check (format nil "invalid-integer-helper:~D" index)
113 (and (not (topic-maps.tmdm:integer-lexical-p value)) (null (topic-maps.tmdm:canonical-integer-lexical-form value))))
114 (check (format nil "invalid-integer-ctm-position:~D" index)
115 (and (typep condition 'ctm-rejection) (not (typep condition 'ctm-unsupported))
116 (eq :invalid-integer-value (error-code condition)) (= 4 (error-position condition))
117 (= 1 (error-line condition)) (= 5 (error-column condition))))))
118 (dolist (value '("+" "--1" "1e3" "0x10" "42x" "1_0" "1/2" "+ 1" "- 1" "NaN" "١"))
119 (let ((condition (ctm-condition (format nil "a t: ~A." value))))
120 (check (concatenate 'string "malformed-integer-rejected:" value)
121 (and (typep condition 'ctm-rejection) (= 5 (error-position condition))
122 (member (error-code condition) '(:invalid-occurrence-literal :expected-occurrence-value))))))
123 (let ((condition (ctm-condition (format nil "a.~%b t: \"1x\"^^<http://www.w3.org/2001/XMLSchema#integer>."))))
124 (check "integer-error-has-source-name-and-line"
125 (and (typep condition 'ctm-rejection) (= 2 (error-line condition)) (= 6 (error-column condition))
126 (equal "independent-fixture" (topic-maps.ctm:error-source condition))
127 (eq :character (topic-maps.ctm:error-position-unit condition)))))
128 (dolist (value '("1.5" ".5" "-.5" "+.5"))
129 (check (concatenate 'string "integer-slice-now-allows-decimal:" value)
130 (string= (topic-maps.tmdm:decimal-datatype)
131 (topic-maps.tmdm:occurrence-datatype (first (all-occurrences (read-text (format nil "a t: ~A." value))))))))
132 (dolist (value '("2000-12-31T12:00:60+01:00" "2000-12-31T12:00:60-01:00" "*"))
133 (let ((condition (ctm-condition (format nil "a t: ~A." value))))
134 (check (if (string= value "*") "integer-slice-now-allows-ctm-integer"
135 (concatenate 'string "excluded-numeric-sugar:" value))
136 (if (string= value "*")
137 (let ((item (first (all-occurrences (read-text "a t: *.")))))
138 (and (null condition) (equal "*" (topic-maps.tmdm:occurrence-value item))
139 (equal "http://psi.topicmaps.org/iso13250/ctm-integer"
140 (topic-maps.tmdm:occurrence-datatype item))))
141 (and (typep condition 'ctm-unsupported) (eq :datetime-leap-second-context (error-code condition))
142 (= 5 (error-position condition)))))))
143 (let* ((map (read-text "a. t.")) (owner (local-topic map "a")) (typ (local-topic map "t")) (state (map-state map)))
144 (let ((condition (occurrence-condition (lambda () (topic-maps.tmdm:add-occurrence owner typ "1.0" :datatype (topic-maps.tmdm:integer-datatype))))))
145 (check "invalid-integer-api-rejected-atomically" (and (eq :invalid-integer-value (topic-maps.tmdm:occurrence-error-code condition)) (equal state (map-state map)))))
146 (setf (topic-maps.tmdm::%topic-occurrences owner)
147 (list (topic-maps.tmdm::%make-occurrence "not an integer" (topic-maps.tmdm:integer-datatype) typ owner)))
148 (let* ((state (map-state map)) (condition (cxtm-condition (lambda () (cxtm map)))))
149 (check "invalid-private-integer-cxtm-error-not-unsupported"
150 (and (typep condition 'topic-maps.cxtm:cxtm-error) (not (typep condition 'topic-maps.cxtm:cxtm-unsupported))
151 (eq :invalid-integer-value (topic-maps.cxtm:error-code condition)) (equal state (map-state map))))))
152 (check "integer-slice-now-supports-decimal-cxtm"
153 (equal '("42.0") (serialized-values (cxtm (read-text "a t:\"42\"^^<http://www.w3.org/2001/XMLSchema#decimal>.")))))
154 (check "integer-slice-now-supports-date-cxtm"
155 (equal '("2000-12-31") (serialized-values (cxtm (read-text "a t: 2000-12-31.")))))
156 (check "integer-slice-rejects-invalid-dateTime-value"
157 (typep (ctm-condition "a t:\"42\"^^<http://www.w3.org/2001/XMLSchema#dateTime>.")
158 'ctm-rejection)))
159
160 (defun integer-equality-and-merge-tests ()
161 (let ((map (read-text "%prefix xsd <http://www.w3.org/2001/XMLSchema#> a t: 001; t: +1; t: 1; t:\"1\"^^xsd:integer; t:\"001\"^^xsd:integer; t:\"1\".")))
162 (check "integer-numeric-spellings-share-a-stored-value" (equal '("1" "001" "1") (integer-values map)))
163 (check "integer-tmdm-equality-is-lexical-and-datatype-sensitive" (= 3 (length (all-occurrences map))))
164 (check "integer-equivalent-value-not-deduplicated-by-output" (equal '("1" "1" "1") (serialized-values (cxtm map)))))
165 (let* ((a (read-text "a t: 1.")) (b (read-text "a t:\"001\"^^<http://www.w3.org/2001/XMLSchema#integer>.")))
166 (check "equal-cxtm-does-not-prove-equal-tmdm-value" (and (string= (cxtm a) (cxtm b)) (not (equal (integer-values a) (integer-values b))))))
167 (let* ((map (read-text "a. b. r. s.")) (a (local-topic map "a")) (b (local-topic map "b"))
168 (r (local-topic map "r")) (s (local-topic map "s")) (dt (topic-maps.tmdm:integer-datatype))
169 (o1 (topic-maps.tmdm:add-occurrence a r "001" :datatype dt :item-identifiers '("urn:int:1")))
170 (o2 (topic-maps.tmdm:add-occurrence b s "001" :datatype dt :item-identifiers '("urn:int:2")))
171 (o3 (topic-maps.tmdm:add-occurrence a r "1" :datatype dt)))
172 (topic-maps.tmdm:merge-topics a b) (topic-maps.tmdm:merge-topics r s)
173 (check "integer-merge-removes-lexical-duplicates-only"
174 (and (= 2 (length (all-occurrences map)))
175 (eq (topic-maps.tmdm:canonical-occurrence o1) (topic-maps.tmdm:canonical-occurrence o2))
176 (not (eq (topic-maps.tmdm:canonical-occurrence o1) (topic-maps.tmdm:canonical-occurrence o3)))))
177 (check "integer-merge-identifier-union" (equal '("urn:int:1" "urn:int:2") (sort (topic-maps.tmdm:occurrence-item-identifiers o1) #'string<)))
178 (check "integer-merge-stored-parent-type-canonical"
179 (let ((live (topic-maps.tmdm:canonical-occurrence o1)))
180 (and (eq (topic-maps.tmdm::%occurrence-parent live) (topic-maps.tmdm:canonical-topic a))
181 (eq (topic-maps.tmdm::%occurrence-type live) (topic-maps.tmdm:canonical-topic r)))))
182 (let ((state (map-state map)) (retained (occurrence-handle-state (list o1 o2 o3))))
183 (check "integer-merge-canonical-output-keeps-distinct-items" (equal '("1" "1") (serialized-values (cxtm map))))
184 (check "integer-serialization-preserves-model-and-handles" (and (equal state (map-state map)) (equal retained (occurrence-handle-state (list o1 o2 o3))))))))
185
186 (defun integer-order-tests ()
187 (dolist (fixture '((("-2" "-10" "2" "10") ("-10" "-2" "10" "2"))
188 (("+002" "-10" "001" "2") ("2" "-10" "1" "2"))))
189 (destructuring-bind (values expected) fixture
190 (let ((reference nil))
191 (dolist (order (permutations values))
192 (let* ((map (read-text "a. t.")) (a (local-topic map "a")) (typ (local-topic map "t")))
193 (dolist (value order) (topic-maps.tmdm:add-occurrence a typ value :datatype (topic-maps.tmdm:integer-datatype)))
194 (let ((state (map-state map)) (out (cxtm map)))
195 (unless reference (setf reference out))
196 (check (format nil "integer-string-order:~S" order) (equal expected (serialized-values out)))
197 (check (format nil "integer-construction-independent-and-pure:~S" order) (and (string= reference out) (equal state (map-state map))))))))))
198 (let* ((map (read-text "%prefix xsd <http://www.w3.org/2001/XMLSchema#> a t:\"1\"^^<urn:custom>; t:\"1\"; t:\"1\"^^xsd:integer."))
199 (out (cxtm map)))
200 (check "integer-datatype-breaks-identical-value-tie"
201 (< (search "<datatype>http://www.w3.org/2001/XMLSchema#integer</datatype>" out)
202 (search "<datatype>http://www.w3.org/2001/XMLSchema#string</datatype>" out) (search "<datatype>urn:custom</datatype>" out))))
203 (let ((map (read-text "a t:\"001\"^^<http://www.w3.org/2001/XMLSchema#int>.")))
204 (check "integer-support-does-not-coerce-derived-datatypes" (search "<value>001</value>" (cxtm map)))))
205
206 (defun run-integer-tests ()
207 (run-prefix-tests)
208 (let ((*checks* nil))
209 (exercise-integer-cases) (integer-lexical-tests) (integer-error-tests) (integer-equality-and-merge-tests) (integer-order-tests)
210 (setf *last-integer-checks* (nreverse *checks*))
211 (format t "~&INTEGERS-PASS: ~D checks; 6 new byte-exact baselines; 42 imported PASS, 231 NOT-RUN.~%" (length *last-integer-checks*)) t))
212 (defun write-integer-report (path)
213 (with-open-file (out path :direction :output :if-exists :supersede :if-does-not-exist :create)
214 (topic-maps.test-foundation::emit-json-value
215 (topic-maps.test-foundation::fields "kind" "integer-semantic-and-cxtm-evidence"
216 "passed" (length *last-integer-checks*) "failed" 0 "checks" (topic-maps.test-foundation::json-array *last-integer-checks*)
217 "cases" (topic-maps.test-foundation::json-array *integer-observations*)
218 "lexicalProbes" (topic-maps.test-foundation::json-array *integer-probes*)) out) (terpri out)))