]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - tests/ctm-decimals.lisp
perf(tmdm): index topic normalization merge candidates
[topic-maps.git] / tests / ctm-decimals.lisp
1 (in-package #:topic-maps.ctm.tests)
2
3 (defparameter *decimal-ids*
4 '("occurrence-decimal" "occurrence-decimal-dot" "occurrence-decimal-dot-neg"
5 "occurrence-decimal-leading-zeros" "occurrence-decimal-minus-sign"
6 "occurrence-decimal-plus-sign" "occurrence-decimal-1.0"))
7 (defvar *last-decimal-checks* nil)
8 (defvar *decimal-observations* nil)
9 (defvar *decimal-probes* nil)
10 (defun decimal-checks () *last-decimal-checks*)
11 (defun decimal-values (map) (mapcar #'topic-maps.tmdm:occurrence-value (all-occurrences map)))
12
13 (defun exercise-decimal-cases ()
14 (setf *decimal-observations* nil)
15 (loop for name in *decimal-ids* for expected-value in '("9.3" "0.3" "-0.3" "0.3" "-9.3" "9.3" "1.0") do
16 (let* ((id (format nil "ctm/in/~A.ctm" name))
17 (case (first (topic-maps.test-foundation:select-cases *cxtm-corpus* :id id)))
18 (doc (topic-maps.test-foundation:test-case-document-iri case))
19 (map (deserialize-file (topic-maps.test-foundation:test-case-source-pathname case) :document-iri doc :source-name id))
20 (owner (local-topic map "topic" doc)) (items (all-occurrences map)) (o (first items)))
21 (setf (topic-maps.test-foundation:test-case-clauses case)
22 '("CTM 3.2" "CTM 3.4.1" "CTM 3.10" "TMDM 4.3-4.4" "TMDM 5.6" "TMDM 6.5"
23 "CXTM 3.8" "CXTM 3.14" "CXTM 3.16" "CXTM 4.2-4.3" "CXTM 4.9"
24 "XSD 3.2.3-3.2.3.2" "XSD 4.3.6"))
25 (check (concatenate 'string name ":no-extra-items")
26 (and (= 2 (length (topic-map-topics map))) (= 1 (length items))
27 (notany #'topic-names (topic-map-topics map)) (null (topic-maps.tmdm:topic-map-associations map))))
28 (check (concatenate 'string name ":stored-value") (equal (list expected-value) (decimal-values map)))
29 (check (concatenate 'string name ":datatype-locator-not-topic")
30 (and (string= (topic-maps.tmdm:decimal-datatype) (topic-maps.tmdm:occurrence-datatype o))
31 (every (lambda (topic) (and (= 1 (length (topic-item-identifiers topic)))
32 (null (topic-subject-identifiers topic)) (null (topic-subject-locators topic))))
33 (topic-map-topics map))))
34 (check (concatenate 'string name ":stored-parent-type-references")
35 (and (eq owner (topic-maps.tmdm::%occurrence-parent o))
36 (eq (local-topic map "occtype" doc) (topic-maps.tmdm::%occurrence-type o))
37 (null (topic-maps.tmdm:occurrence-item-identifiers o))))
38 (let* ((state (map-state map)) (handles (occurrence-handle-state items))
39 (actual (topic-maps.cxtm:canonicalize-octets map :base-locator doc))
40 (expected (binary-file (topic-maps.test-foundation:test-case-expected-baseline case)))
41 (different (mismatch actual expected)))
42 (topic-maps.test-foundation:record-result case (if different :cxtm-mismatch :cxtm-match)
43 :diagnostics (when different (list (format nil "First mismatch byte ~D; actual ~D bytes, expected ~D bytes."
44 different (length actual) (length expected)))))
45 (check (concatenate 'string name ":byte-exact-cxtm") (null different))
46 (check (concatenate 'string name ":pure-model-and-handles")
47 (and (equal state (map-state map)) (equal handles (occurrence-handle-state items))))
48 (setf *cxtm-results* (append *cxtm-results*
49 (list (list :id id :base doc :actual (cxtm map doc)
50 :expected (uiop:read-file-string (topic-maps.test-foundation:test-case-expected-baseline case) :external-format :utf-8)
51 :match (null different) :bytes (length actual)))))
52 (format t "~&DECIMAL-CXTM ~A: PASS (~D bytes)~%" id (length actual)))
53 (push (topic-maps.test-foundation::fields "id" id "documentIRI" doc "status" "pass"
54 "ctmSource" (uiop:read-file-string (topic-maps.test-foundation:test-case-source-pathname case) :external-format :utf-8)
55 "actualObjects" (occurrence-observation map)) *decimal-observations*)))
56 (setf *decimal-observations* (nreverse *decimal-observations*))
57 (check "slice8:49-passes-224-unexecuted"
58 (equal (getf (topic-maps.test-foundation:summarize-corpus *cxtm-corpus*) :statuses)
59 '(:pass 49 :fail 0 :unsupported 0 :not-run 224))))
60
61 (defun decimal-lexical-tests ()
62 (setf *decimal-probes* nil)
63 (loop for (input expected) in '(("0" "0.0") ("0." "0.0") (".0" "0.0") ("-.0" "0.0")
64 ("-000.000" "0.0") ("+000.000" "0.0") ("1" "1.0") ("1." "1.0")
65 ("+001.2300" "1.23") ("-.3" "-0.3") ("+.3" "0.3") ("00100.00" "100.0")
66 ("000.0000100" "0.00001") ("-100.00100" "-100.001"))
67 for index from 0 do
68 (check (format nil "decimal-valid-lexical:~D" index) (topic-maps.tmdm:decimal-lexical-p input))
69 (check (format nil "decimal-canonical-form:~D" index) (equal expected (topic-maps.tmdm:canonical-decimal-lexical-form input))))
70 (let ((inputs (append '("0.0" "+001.2300" "-001.2300" "-000.000" ".3" "+.3" "-.3" "1.0000"
71 "000100.00" "0.0000100" "42" "42.")
72 (list (format nil " ~C+00042.0300~C~C " #\Tab #\Return #\Newline)
73 (concatenate 'string (make-string 10000 :initial-element #\9) "." (make-string 10000 :initial-element #\9))
74 (concatenate 'string "-000" (make-string 10000 :initial-element #\9) ".0001000")
75 (concatenate 'string "-0." (make-string 10000 :initial-element #\0) "1"))
76 (loop for i below 64 collect
77 (concatenate 'string (nth (mod i 3) '("" "+" "-")) (make-string (mod i 8) :initial-element #\0)
78 (format nil "~D." (* i 17))
79 (coerce (loop for j below (1+ i) collect (char "0123456789" (mod (+ (* i 7) (* j j) j) 10))) 'string)
80 (make-string (mod i 7) :initial-element #\0))))))
81 (loop for input in inputs for index from 0 do
82 (dolist (mode '(:implicit :explicit))
83 ;; Only CTM decimal grammar is implicit; XSD also permits 42 and 42.
84 (unless (and (eq mode :implicit)
85 (or (find-if #'topic-maps.ctm::whitespace-p input)
86 (not (find #\. input)) (char= #\. (char input (1- (length input))))))
87 (let* ((source (if (eq mode :implicit) (format nil "a t: ~A." input)
88 (format nil "a t:\"~A\"^^<http://www.w3.org/2001/XMLSchema#decimal>." input)))
89 (map (read-text source)) (o (first (all-occurrences map))) (state (map-state map))
90 (retained (occurrence-handle-state (list o))) (out (cxtm map))
91 (stored (topic-maps.tmdm:occurrence-value o)) (expected (topic-maps.tmdm:canonical-decimal-lexical-form input)))
92 (check (format nil "decimal-probe-value:~D:~A" index mode) (string= (if (eq mode :implicit) expected input) stored))
93 (check (format nil "decimal-probe-output:~D:~A" index mode) (equal (list expected) (serialized-values out)))
94 (check (format nil "decimal-probe-purity:~D:~A" index mode)
95 (and (equal state (map-state map)) (equal retained (occurrence-handle-state (list o)))))
96 (push (topic-maps.test-foundation::fields "input" input "mode" (string-downcase (symbol-name mode))
97 "storedValue" stored "datatype" (topic-maps.tmdm:occurrence-datatype o) "actualCXTM" out) *decimal-probes*))))))
98 (setf *decimal-probes* (nreverse *decimal-probes*))
99 (dolist (input '("1.0" ".3" "9.3" "+.3" "-.3" "000.300"))
100 (let* ((lexer (topic-maps.ctm::make-lexer (concatenate 'string input ".")))
101 (n (topic-maps.ctm::next-token lexer)) (dot (topic-maps.ctm::next-token lexer)))
102 (check (concatenate 'string "decimal-token-boundary:" input)
103 (and (eq :decimal (topic-maps.ctm::token-kind n)) (string= input (topic-maps.ctm::token-value n))
104 (eq :punctuation (topic-maps.ctm::token-kind dot)) (eql #\. (topic-maps.ctm::token-value dot))
105 (= (length input) (topic-maps.ctm::token-position dot))))))
106 (dolist (source (list "a t: 1.0." "a t: 1.0;." "a t: 1.0#(comment)#." (format nil "a t: .3# comment~%.")))
107 (check (concatenate 'string "decimal-layout-boundary:" source)
108 (equal (list (if (search ".3#" source) "0.3" "1.0")) (decimal-values (read-text source)))))
109 (check "decimal-terminator-next-topic" (= 3 (length (topic-map-topics (read-text "a t: 9.3.b.")))))
110 (check "decimal-and-integer-same-token-path"
111 (equal '("1.0" "1" "0.3") (decimal-values (read-text "a t: 1.0;t: 1;t: .3.")))))
112
113 (defun decimal-error-tests ()
114 (loop for value in (append '("" "+" "-" "." "+." "-." "1.2.3" "1e3" "1.0E2" "1 2" "1 .0" "1. 0"
115 "- .1" "+-1.0" "++1.0" "1/2" "NaN" "INF" "١.٢" "1,2" "1_0.0")
116 (list (concatenate 'string (string (code-char 160)) "1.0"))) for index from 0 do
117 (let* ((text (format nil "a t:\"~A\"^^<http://www.w3.org/2001/XMLSchema#decimal>." value)) (condition (ctm-condition text)))
118 (check (format nil "invalid-decimal-helper:~D" index)
119 (and (not (topic-maps.tmdm:decimal-lexical-p value)) (null (topic-maps.tmdm:canonical-decimal-lexical-form value))))
120 (check (format nil "invalid-decimal-source-position:~D" index)
121 (and (typep condition 'ctm-rejection) (not (typep condition 'ctm-unsupported))
122 (eq :invalid-decimal-value (error-code condition)) (= 4 (error-position condition))
123 (= 1 (error-line condition)) (= 5 (error-column condition))))))
124 (dolist (value '("1.2.3" "1e2" "0x1.2" "1.0f0" "1.0/2" "+.3x" "-.3x"))
125 (let ((condition (ctm-condition (format nil "a t: ~A." value))))
126 (check (concatenate 'string "malformed-bare-decimal:" value)
127 (and (typep condition 'ctm-rejection) (not (typep condition 'ctm-unsupported))
128 (eq :invalid-occurrence-literal (error-code condition)) (= 5 (error-position condition))))))
129 (let ((condition (ctm-condition (format nil "a.~%b t: \"1e2\"^^<http://www.w3.org/2001/XMLSchema#decimal>."))))
130 (check "decimal-diagnostic-line-column-source"
131 (and (typep condition 'ctm-rejection) (= 2 (error-line condition)) (= 6 (error-column condition))
132 (equal "independent-fixture" (topic-maps.ctm:error-source condition))
133 (eq :character (topic-maps.ctm:error-position-unit condition)))))
134 (let* ((map (read-text "a. t.")) (owner (local-topic map "a")) (typ (local-topic map "t")) (state (map-state map)))
135 (let ((condition (occurrence-condition (lambda () (topic-maps.tmdm:add-occurrence owner typ "1e2" :datatype (topic-maps.tmdm:decimal-datatype))))))
136 (check "invalid-decimal-api-atomic" (and (eq :invalid-decimal-value (topic-maps.tmdm:occurrence-error-code condition)) (equal state (map-state map)))))
137 (setf (topic-maps.tmdm::%topic-occurrences owner)
138 (list (topic-maps.tmdm::%make-occurrence "bad" (topic-maps.tmdm:decimal-datatype) typ owner)))
139 (let* ((state (map-state map)) (condition (cxtm-condition (lambda () (cxtm map)))))
140 (check "invalid-private-decimal-cxtm-error"
141 (and (typep condition 'topic-maps.cxtm:cxtm-error) (not (typep condition 'topic-maps.cxtm:cxtm-unsupported))
142 (eq :invalid-decimal-value (topic-maps.cxtm:error-code condition)) (equal state (map-state map)))))))
143
144 (defun decimal-equality-and-merge-tests ()
145 (let ((map (read-text "%prefix xsd <http://www.w3.org/2001/XMLSchema#> a t: 01.00; t: +1.0; t: 1.000; t:\"1.0\"^^xsd:decimal; t:\"01.00\"^^xsd:decimal.")))
146 (check "decimal-bare-value-policy-duplicates" (equal '("1.0" "01.00") (decimal-values map)))
147 (check "decimal-output-equivalence-not-merging" (equal '("1.0" "1.0") (serialized-values (cxtm map)))))
148 (let* ((a (read-text "a t: 1.0.")) (b (read-text "a t:\"001.000\"^^<http://www.w3.org/2001/XMLSchema#decimal>.")))
149 (check "decimal-equal-xml-not-tmdm-value-equality"
150 (and (string= (cxtm a) (cxtm b)) (not (equal (decimal-values a) (decimal-values b))))))
151 (let* ((map (read-text "%prefix xsd <http://www.w3.org/2001/XMLSchema#> a t:\"1\"^^xsd:decimal; t:\"1\"^^xsd:integer."))
152 (items (all-occurrences map)))
153 (check "integer-decimal-distinct-by-datatype-alone"
154 (and (= 2 (length items)) (equal '("1" "1") (decimal-values map))
155 (not (string= (topic-maps.tmdm:occurrence-datatype (first items)) (topic-maps.tmdm:occurrence-datatype (second items))))))
156 (check "integer-decimal-identical-stored-value-output-order" (equal '("1.0" "1") (serialized-values (cxtm map)))))
157 (let* ((map (read-text "a. b. r. s.")) (a (local-topic map "a")) (b (local-topic map "b"))
158 (r (local-topic map "r")) (s (local-topic map "s")) (dt (topic-maps.tmdm:decimal-datatype))
159 (o1 (topic-maps.tmdm:add-occurrence a r "01.00" :datatype dt :item-identifiers '("urn:decimal:1")))
160 (o2 (topic-maps.tmdm:add-occurrence b s "01.00" :datatype dt :item-identifiers '("urn:decimal:2")))
161 (o3 (topic-maps.tmdm:add-occurrence a r "1.0" :datatype dt)))
162 (topic-maps.tmdm:merge-topics a b) (topic-maps.tmdm:merge-topics r s)
163 (check "decimal-merging-equal-properties-only"
164 (and (= 2 (length (all-occurrences map)))
165 (eq (topic-maps.tmdm:canonical-occurrence o1) (topic-maps.tmdm:canonical-occurrence o2))
166 (not (eq (topic-maps.tmdm:canonical-occurrence o1) (topic-maps.tmdm:canonical-occurrence o3)))))
167 (check "decimal-merged-identifiers-union" (equal '("urn:decimal:1" "urn:decimal:2") (sort (topic-maps.tmdm:occurrence-item-identifiers o1) #'string<)))
168 (check "decimal-stored-references-canonical-after-merge"
169 (let ((live (topic-maps.tmdm:canonical-occurrence o1)))
170 (and (eq (topic-maps.tmdm::%occurrence-parent live) (topic-maps.tmdm:canonical-topic a))
171 (eq (topic-maps.tmdm::%occurrence-type live) (topic-maps.tmdm:canonical-topic r)))))
172 (let ((state (map-state map)) (retained (occurrence-handle-state (list o1 o2 o3))))
173 (check "decimal-merged-output-keeps-lexical-distinction" (equal '("1.0" "1.0") (serialized-values (cxtm map))))
174 (check "decimal-merged-model-handles-not-mutated" (and (equal state (map-state map)) (equal retained (occurrence-handle-state (list o1 o2 o3))))))))
175
176 (defun decimal-order-tests ()
177 (dolist (fixture '((("-2.0" "-10.0" "2.0" "10.0") ("-10.0" "-2.0" "10.0" "2.0"))
178 (("+002.00" "-10.00" "001.00" "2.0") ("2.0" "-10.0" "1.0" "2.0"))))
179 (destructuring-bind (values expected) fixture
180 (let ((reference nil))
181 (dolist (order (permutations values))
182 (let* ((map (read-text "a. t.")) (a (local-topic map "a")) (typ (local-topic map "t")))
183 (dolist (value order) (topic-maps.tmdm:add-occurrence a typ value :datatype (topic-maps.tmdm:decimal-datatype)))
184 (let ((state (map-state map)) (out (cxtm map)))
185 (unless reference (setf reference out))
186 (check (format nil "decimal-stored-string-order:~S" order) (equal expected (serialized-values out)))
187 (check (format nil "decimal-construction-independent-pure:~S" order) (and (string= reference out) (equal state (map-state map))))))))))
188 (let ((map (read-text "a t:\"001.00\"^^<http://www.w3.org/2001/XMLSchema#money>.")))
189 (check "decimal-only-exact-datatype-treated" (search "<value>001.00</value>" (cxtm map)))))
190
191 (defun run-decimal-tests ()
192 (run-integer-tests)
193 (let ((*checks* nil))
194 (exercise-decimal-cases) (decimal-lexical-tests) (decimal-error-tests) (decimal-equality-and-merge-tests) (decimal-order-tests)
195 (setf *last-decimal-checks* (nreverse *checks*))
196 (format t "~&DECIMALS-PASS: ~D checks; 7 new byte-exact baselines; 49 imported PASS, 224 NOT-RUN.~%" (length *last-decimal-checks*)) t))
197 (defun write-decimal-report (path)
198 (with-open-file (out path :direction :output :if-exists :supersede :if-does-not-exist :create)
199 (topic-maps.test-foundation::emit-json-value
200 (topic-maps.test-foundation::fields "kind" "decimal-semantic-and-cxtm-evidence"
201 "passed" (length *last-decimal-checks*) "failed" 0 "checks" (topic-maps.test-foundation::json-array *last-decimal-checks*)
202 "cases" (topic-maps.test-foundation::json-array *decimal-observations*)
203 "lexicalProbes" (topic-maps.test-foundation::json-array *decimal-probes*)) out) (terpri out)))