redoing dev style to be more test centric, from lessons learned with lisp-matrix.
[CommonLispStat.git] / src / unittests / unittests.lisp
bloba6a94c78393bd1164efb2eef0ef03c8c8b63553e
1 ;;; -*- mode: lisp -*-
2 ;;; Copyright (c) 2007, by A.J. Rossini <blindglobe@gmail.com>
3 ;;; See COPYRIGHT file for any additional restrictions (BSD license).
4 ;;; Since 1991, ANSI was finally finished. Edited for ANSI Common Lisp.
6 ;;; This is semi-external to lispstat core packages. The dependency
7 ;;; should be that lispstat packages are dependencies for the unit
8 ;;; tests. However, where they will end up is still to be
9 ;;; determined.
11 (in-package :lisp-stat-unittests)
13 ;;; TESTS
15 (defun run-lisp-stat-tests ()
16 (run-tests :suite 'lisp-stat-ut))
18 ;; (run-lisp-stat-tests)
21 (defun run-lisp-stat-test (&rest x)
22 (run-test x))
24 (deftestsuite lisp-stat-ut () ())
26 ;; others should be defined in another place...:
27 ;;(deftestsuite lisp-stat-ut-lin-alg (lisp-stat-ut) ())
28 (deftestsuite lisp-stat-ut-spec-fns (lisp-stat-ut) ())
29 (deftestsuite lisp-stat-ut-probdistn (lisp-stat-ut) ())
31 (defun almost= (a b &key (tol 0.000001))
32 "Numerically compares 2 values to a tolerance."
33 (< (abs (- a b)) tol))
35 (defun almost=lists (a b &key (tol 0.000001))
36 "Numerically compare 2 lists using almost=."
37 (if (and (null a) (null b))
39 (and (almost= (car a) (car b) :tol tol)
40 (almost=lists (cdr a) (cdr b) :tol tol))))
42 ;; Need to consider a CLOSy approach for almost= to cover the range of
43 ;; possible data structures that we would like to be equal to a
44 ;; particular tolerance range. For example, fill in a shell like:
46 (defgeneric numerical= (a b &key tol))
48 (defmethod numerical= ((a real) (b real) &key (tol 0.00001)) ;; real))
49 ;;(print (format nil " equality pred for real a=~w real b=~w" a b))
50 (< (abs (- a b)) tol))
52 ;; can we just worry about reals if integers are a subclass?
53 (defmethod numerical= ((a integer) (b integer) &key (tol 0.1)) ;; real))
54 ;;(print (format nil " equality pred for int a=~w int b=~w" a b))
55 (< (abs (- a b)) tol))
57 (defmethod numerical= ((a complex) (b complex) &key (tol 0.00001))
58 ;;(print (format nil " equality pred for cmplx a=~w cmplx b=~w" a b))
59 (< (abs (- a b)) tol))
61 (defmethod numerical= ((a sequence) (b sequence) &key (tol 0.00001))
62 ;; (print (format nil "checking equality for list a ~w list b=~w" a b))
63 ;; using sequence for lists and vectors, but not arrays.
64 ;; FIXME++++ This is too slow, too many comparisons!
65 (if (and (null a) (null b))
67 (if (and (= (length a) (length b))
68 (> (length a) 0)
69 (numerical= (car a) (car b) :tol tol))
70 (progn
71 (if (= (length (cdr a)) 0)
73 (numerical= (cdr a) (cdr b) :tol tol)))
74 nil)))
76 ;; To do.
78 (defmethod numerical= ((a array) (b array) &key (tol 0.00001))
79 (print (format nil "checking equality for array a ~w and array b=~w" a b))
80 ;;; FIXME Warning! Need to generalize past 2-d array!!
81 (if (/= (array-dimensions a) (array-dimensions b))
82 nil
83 (let* ((a-dim (array-dimensions a))
84 (a-b-elt-eq (loop for i from 0 to (nth 0 a-dim)
85 for j from 0 to (nth 1 a-dim)
86 collect (numerical= (apply #'aref a (list i j))
87 (apply #'aref b (list i j))
88 :tol tol))))
89 (every #'(lambda (x) x) a-b-elt-eq))))
91 (deftestsuite lisp-stat-ut-testsupport (lisp-stat-ut)
93 (:tests
94 (almost=1 (ensure (almost= 3 3.001 :tol 0.01)))
95 (almost=2 (ensure (almost= 3 3.01 :tol 0.01)))
96 (almost=3 (ensure (not (almost= 3 3.1 :tol 0.01))))
97 (almost=lists1 (ensure (almost=lists nil nil :tol 0.01)))
98 (almost=lists2 (ensure (almost=lists (list ) (list ) :tol 0.01)))
99 (almost=lists3 (ensure (almost=lists (list 1.0) (list 1.0) :tol 0.01)))
100 (almost=lists4 (ensure (almost=lists (list 1.0 1.0) (list 1.0 1.0) :tol 0.01)))
101 (almost=lists5 (ensure (not (almost=lists (list 1.0 1.0)
102 (list 1.0 1.1) :tol 0.01))))))
104 (deftestsuite lisp-stat-ut-testsupport2 (lisp-stat-ut)
106 (:tests
107 (numerical=1 (ensure (numerical= 3 3.001 :tol 0.01)))
108 (numerical=1.1 (ensure (numerical= 2 2)))
109 (numerical=1.2 (ensure (not (numerical= 2 3))))
110 (numerical=2 (ensure (numerical= 3 3.01 :tol 0.01)))
111 (numerical=3 (ensure (not (numerical= 3 3.1 :tol 0.01))))
112 (numerical=4 (ensure (numerical= nil nil :tol 0.01)))
113 (numerical=5 (ensure (numerical= (list ) (list ) :tol 0.01)))
114 (numerical=6 (ensure (numerical= (list 1.0) (list 1.0) :tol 0.01)))
115 (numerical=7 (ensure (numerical= (list 1.0 1.0) (list 1.0 1.0) :tol 0.01)))
116 (numerical=7.5 (ensure-error (numerical= 1.0 (list 1.0 1.0) :tol 0.01)))
117 (numerical=8 (ensure (not (numerical= (list 2.0 2.0 2.2) (list 2.1 2.0 2.2)))))
118 (numerical=9 (ensure (numerical= (list 2.1 2.0 2.2) (list 2.1 2.0 2.2)) ))
119 (numerical=10 (ensure (numerical= (list 2.1 2.0 2.2 4.2) (list 2.1 2.0 2.2 4.2))))
120 (numerical=11 (ensure (not (numerical= (list 2.1 2.0 2.3 4.0) (list 2.1 2.0 2.2 4.0)))))
121 (numerical=12 (ensure (not (numerical= (list 1.0 1.0)
122 (list 1.0 1.1) :tol 0.01))))
123 (numerical=C1 (ensure (numerical= #C(2 3) #C(2 3))))
124 (numerical=C2 (ensure (not(numerical= #C(2 3) #C(2 4)))))
125 (numerical=C3 (ensure (numerical= #C(2 3) #C(3 4) :tol 2)))
126 (numerical=C4 (ensure (not(numerical= #C(2 3) #C(3 4) :tol 1))))
128 ;;;; Tests to fix
130 (numerical=A1 (ensure (numerical= #1A(2 3 4)
131 #1A(2 3 4))))
133 (numerical=A2 (ensure (numerical= #2A((2 3 4) (1 2 4) (2 4 5))
134 #2A((2 3 4) (1 2 4) (2 4 5)))))
136 (numerical=A3 (ensure (not (numerical= #2A((2 3 4) (1 2 4) (2 5 4))
137 #2A((2 3 4) (1 2 4) (2 4 5))))))
139 (numerical=A4 (ensure (not (numerical= #1A(2 2 4)
140 #1A(2 3 4)))))
144 ;; (describe (run-tests :suite 'lisp-stat-ut-testsupport2))
148 ;;;; Log-gamma function
150 (addtest (lisp-stat-ut-spec-fns) log-gamma-fn
151 (ensure-same
152 (log-gamma 3.4)
153 1.0923280596789584
154 :test 'almost=))
157 ;;; Probability distributions
159 ;; This macro should be generalized, but it's a good start now.
160 ;;(defmacro ProbDistnTests (prefixName
161 ;; quant-params quant-answer
162 ;; cdf-params cdf-answer
163 ;; pmf-params pmf-answer
164 ;; rand-params rand-answer)
165 ;; (deftestsuite lisp-stat-ut-probdist-,prefixName (lisp-stat-ut-probdistn)
166 ;; ;; (( ))
167 ;; (:documentation "testing for ,testName distribution results")
168 ;; (:test (ensure-same
169 ;; (lisp-stat-ut-basics:,testName-quant ,quant-params) ,quant-answer))
170 ;; (:test (ensure-same
171 ;; (lisp-stat-ut-basics:,testName-cdf ,cdf-params) ,cdf-answer))
172 ;; (:test (ensure-same
173 ;; (lisp-stat-ut-basics:,testName-pmf ,pmf-params) ,pmf-answer))
174 ;; (:test (progn
175 ;; (set-seed 234)
176 ;; (ensure-same
177 ;; (lisp-stat-ut-basics:,testName-rand ,rand-params) ,rand-answer)))))
179 ;;; Normal distribution
181 (deftestsuite lisp-stat-ut-probdist-f (lisp-stat-ut-probdistn)
183 (:documentation "testing for Gaussian distn results")
184 (:test (ensure-same
185 (normal-quant 0.95)
186 1.6448536279366268))
187 (:test (ensure-same
188 (normal-cdf 1.3)
189 0.9031995154143897))
190 (:test (ensure-same
191 (normal-dens 1.3)
192 0.17136859204780736))
193 (:test (ensure-same
194 (normal-rand 2)
195 (list -0.40502015f0 -0.8091404f0)))
196 (:test (ensure-same
197 (bivnorm-cdf 0.2 0.4 0.6)
198 0.4736873734160288)))
200 ;;;; Cauchy distribution
202 (deftestsuite lisp-stat-ut-probdist-cauchy (lisp-stat-ut-probdistn)
204 (:documentation "testing for Cachy-distn results")
205 (:test (ensure-same
206 (cauchy-quant 0.95)
207 6.313751514675031))
208 (:test (ensure-same
209 (cauchy-cdf 1.3)
210 0.7912855998398473))
211 (:test (ensure-same
212 (cauchy-dens 1.3)
213 0.1183308127104695 ))
214 (:test (ensure-same
215 (cauchy-rand 2)
216 (list -1.06224644160405 -0.4524695943939537))))
218 ;;;; Gamma distribution
220 (deftestsuite lisp-stat-ut-probdist-gamma (lisp-stat-ut-probdistn)
222 (:documentation "testing for gamma distn results")
223 (:test (ensure-same
224 (gamma-quant 0.95 4.3)
225 8.178692439291645))
226 (:test (ensure-same
227 (gamma-cdf 1.3 4.3)
228 0.028895150986674906))
229 (:test (ensure-same
230 (gamma-dens 1.3 4.3)
231 0.0731517686447374))
232 (:test (ensure-same
233 (gamma-rand 2 4.3)
234 (list 2.454918912880936 4.081365384357454))))
236 ;;;; Chi-square distribution
238 (deftestsuite lisp-stat-ut-probdist-chisq (lisp-stat-ut-probdistn)
240 (:documentation "testing for Chi-square distn results")
241 (:test (ensure-same
242 (chisq-quant 0.95 3)
243 7.814727903379012))
244 (:test (ensure-same
245 (chisq-cdf 1 5)
246 0.03743422675631789))
247 (:test (ensure-same
248 (chisq-dens 1 5)
249 0.08065690818083521))
250 (:test (progn
251 (set-seed 353)
252 (ensure-same
253 (chisq-rand 2 4)
254 (list 1.968535826180572 2.9988646156942997)))))
256 ;;;; Beta distribution
258 (deftestsuite lisp-stat-ut-probdist-beta (lisp-stat-ut-probdistn)
260 (:documentation "testing for beta distn results")
261 (:test (ensure-same
262 (beta-quant 0.95 3 2)
263 0.9023885371149876))
264 (:test (ensure-same
265 (beta-cdf 0.4 2 2.4)
266 0.4247997418541529 ))
267 (:test (ensure-same
268 (beta-dens 0.4 2 2.4)
269 1.5964741858913518 ))
270 (:test (ensure-same
271 (beta-rand 2 2 2.4)
272 (list 0.8014897077282279 0.6516371997922659))))
274 ;;;; t distribution
276 (deftestsuite lisp-stat-ut-probdist-t (lisp-stat-ut-probdistn)
278 (:documentation "testing for t-distn results")
279 (:test (ensure-same
280 (t-quant 0.95 3)
281 2.35336343484194))
282 (:test (ensure-same
283 (t-cdf 1 2.3)
284 0.794733624298342))
285 (:test (ensure-same
286 (t-dens 1 2.3)
287 0.1978163816318102))
288 (:test (ensure-same
289 (t-rand 2 2.3)
290 (list -0.34303672776089306 -1.142505872436518))))
292 ;;;; F distribution
294 (deftestsuite lisp-stat-ut-probdist-f (lisp-stat-ut-probdistn)
296 (:documentation "testing for f-distn results")
297 (:test (ensure-same
298 (f-quant 0.95 3 5) 5.409451318117459))
299 (:test (ensure-same
300 (f-cdf 1 3.2 5.4)
301 0.5347130905510765))
302 (:test (ensure-same
303 (f-dens 1 3.2 5.4)
304 0.37551128864591415))
305 (:test (progn
306 (set-seed 234)
307 (ensure-same
308 (f-rand 2 3 2)
309 (list 0.7939093442091963 0.07442694152491144)))))
311 ;;;; Poisson distribution
313 (deftestsuite lisp-stat-ut-probdist-poisson (lisp-stat-ut-probdistn)
315 (:documentation "testing for poisson distribution results")
316 (:test (ensure-same
317 (poisson-quant 0.95 3.2) 6))
318 (:test (ensure-same
319 (poisson-cdf 1 3.2)
320 0.17120125672252395))
321 (:test (ensure-same
322 (poisson-pmf 1 3.2)
323 0.13043905274097067))
324 (:test (progn
325 (set-seed 234)
326 (ensure-same
327 (poisson-rand 5 3.2)
328 (list 2 1 2 0 3)))))
330 ;; Binomial distribution
332 (deftestsuite lisp-stat-ut-probdist-binomial (lisp-stat-ut-probdistn)
334 (:documentation "testing for binomial distribution results")
336 (:test (ensure-same
337 (binomial-quant 0.95 3 0.4) ;;; DOESN'T RETURN
339 (:test (ensure-same
340 (binomial-quant 0 3 0.4)
341 ;; -2147483648
343 (:test (ensure-same
344 (binomial-cdf 1 3 0.4)
345 0.6479999999965776))
347 (:test (ensure-same
348 (binomial-pmf 1 3 0.4)
349 0.4320000000226171))
350 (:test (progn
351 (set-seed 526)
352 (ensure-same
353 (binomial-rand 5 3 0.4)
354 (list 2 2 0 1 2)))))