Skip to content

Commit 678cb61

Browse files
committed
huge update to Math library, including removing arrays and internal-random.
1 parent 7f221a1 commit 678cb61

File tree

2 files changed

+50
-43
lines changed

2 files changed

+50
-43
lines changed

src/provisdom/test/core.cljc

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,15 @@
202202
([form] `(is-not ~form nil))
203203
([form msg] `(t/is (~'not ~form) ~msg)))
204204

205+
(defmacro is-approx=
206+
"Asserts that x1 and x2 are approximately equal within tolerance.
207+
Options:
208+
:tolerance - maximum allowed difference (default 1e-6)
209+
:nan-equal? - if true, two NaN values are considered equal"
210+
([x1 x2] `(is-approx= ~x1 ~x2 :tolerance 1e-6))
211+
([x1 x2 & opts]
212+
`(t/is (#'approx= ~x1 ~x2 ~@opts))))
213+
205214
(defn midje-just
206215
[expected actual]
207216
(if (= (count expected) (count actual))
@@ -229,7 +238,7 @@
229238
:else (assoc acc cur-path x)))]
230239
(data-to-paths' x {} [])))
231240

232-
(defn approx=
241+
(defn ^:private approx=
233242
([x1 x2] (approx= x1 x2 :tolerance 1e-6))
234243
([x1 x2 & {:keys [tolerance nan-equal?]}]
235244
(cond
@@ -252,10 +261,6 @@
252261
:else
253262
(< (abs (double (- x1 x2))) tolerance))))
254263

255-
(s/fdef approx=
256-
:args (s/cat :x1 number? :x2 number? :opts (s/? (s/keys* :req-un [::tolerance])))
257-
:ret boolean?)
258-
259264
(defmacro no-problems
260265
"Returns true if x has no problems when validated against spec, false otherwise.
261266
Most useful to be called inside an clojure.test/is form:

test/provisdom/test/core_test.cljc

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -126,44 +126,46 @@
126126
"not coll or map expansion")))
127127

128128
(deftest approx=-test
129-
(testing "Default tolerance (1e-6)"
130-
(is (t/approx= 1.0 1.0))
131-
(is (t/approx= 1.0 1.000001))
132-
(is (not (t/approx= 1.0 1.00001)))
133-
(is (t/approx= 0.0 0.0))
134-
(is (t/approx= -1.0 -1.0))
135-
(is (t/approx= 1000000.0 1000000.0000009)))
136-
137-
(testing "Custom tolerance"
138-
(is (t/approx= 1.0 1.001 :tolerance 1e-2))
139-
(is (not (t/approx= 1.0 1.01 :tolerance 1e-2)))
140-
(is (t/approx= 1.0 1.01 :tolerance 1e-1)))
141-
142-
#?(:clj
143-
(testing "Infinity cases"
144-
(is (t/approx= Double/POSITIVE_INFINITY Double/POSITIVE_INFINITY))
145-
(is (t/approx= Double/NEGATIVE_INFINITY Double/NEGATIVE_INFINITY))
146-
(is (not (t/approx= Double/POSITIVE_INFINITY Double/NEGATIVE_INFINITY)))
147-
(is (not (t/approx= Double/POSITIVE_INFINITY 1000.0)))
148-
(is (not (t/approx= 1000.0 Double/POSITIVE_INFINITY)))
149-
(is (not (t/approx= Double/NEGATIVE_INFINITY 1000.0)))
150-
(is (not (t/approx= 1000.0 Double/NEGATIVE_INFINITY)))
151-
(is (not (t/approx= Double/POSITIVE_INFINITY Double/NEGATIVE_INFINITY :tolerance 1e10)))))
152-
153-
#?(:clj
154-
(testing "NaN cases without nan-equal? flag"
155-
(is (not (t/approx= Double/NaN Double/NaN)))
156-
(is (not (t/approx= Double/NaN 1.0)))
157-
(is (not (t/approx= 1.0 Double/NaN)))
158-
(is (not (t/approx= Double/NaN Double/POSITIVE_INFINITY)))
159-
(is (not (t/approx= Double/POSITIVE_INFINITY Double/NaN)))))
160-
161-
#?(:clj
162-
(testing "NaN cases with nan-equal? true"
163-
(is (t/approx= Double/NaN Double/NaN :nan-equal? true))
164-
(is (not (t/approx= Double/NaN 1.0 :nan-equal? true)))
165-
(is (not (t/approx= 1.0 Double/NaN :nan-equal? true)))
166-
(is (not (t/approx= Double/NaN Double/POSITIVE_INFINITY :nan-equal? true))))))
129+
;; Testing private approx= function via #'
130+
(let [approx= @#'t/approx=]
131+
(testing "Default tolerance (1e-6)"
132+
(is (approx= 1.0 1.0))
133+
(is (approx= 1.0 1.000001))
134+
(is (not (approx= 1.0 1.00001)))
135+
(is (approx= 0.0 0.0))
136+
(is (approx= -1.0 -1.0))
137+
(is (approx= 1000000.0 1000000.0000009)))
138+
139+
(testing "Custom tolerance"
140+
(is (approx= 1.0 1.001 :tolerance 1e-2))
141+
(is (not (approx= 1.0 1.01 :tolerance 1e-2)))
142+
(is (approx= 1.0 1.01 :tolerance 1e-1)))
143+
144+
#?(:clj
145+
(testing "Infinity cases"
146+
(is (approx= Double/POSITIVE_INFINITY Double/POSITIVE_INFINITY))
147+
(is (approx= Double/NEGATIVE_INFINITY Double/NEGATIVE_INFINITY))
148+
(is (not (approx= Double/POSITIVE_INFINITY Double/NEGATIVE_INFINITY)))
149+
(is (not (approx= Double/POSITIVE_INFINITY 1000.0)))
150+
(is (not (approx= 1000.0 Double/POSITIVE_INFINITY)))
151+
(is (not (approx= Double/NEGATIVE_INFINITY 1000.0)))
152+
(is (not (approx= 1000.0 Double/NEGATIVE_INFINITY)))
153+
(is (not (approx= Double/POSITIVE_INFINITY Double/NEGATIVE_INFINITY :tolerance 1e10)))))
154+
155+
#?(:clj
156+
(testing "NaN cases without nan-equal? flag"
157+
(is (not (approx= Double/NaN Double/NaN)))
158+
(is (not (approx= Double/NaN 1.0)))
159+
(is (not (approx= 1.0 Double/NaN)))
160+
(is (not (approx= Double/NaN Double/POSITIVE_INFINITY)))
161+
(is (not (approx= Double/POSITIVE_INFINITY Double/NaN)))))
162+
163+
#?(:clj
164+
(testing "NaN cases with nan-equal? true"
165+
(is (approx= Double/NaN Double/NaN :nan-equal? true))
166+
(is (not (approx= Double/NaN 1.0 :nan-equal? true)))
167+
(is (not (approx= 1.0 Double/NaN :nan-equal? true)))
168+
(is (not (approx= Double/NaN Double/POSITIVE_INFINITY :nan-equal? true)))))))
167169

168170
(deftest is-valid-test
169171
(t/is-valid int? 1))

0 commit comments

Comments
 (0)