Commit b3d7f7be authored by pfdietz's avatar pfdietz

New tests for isqrt, sqrt, random, random-state-p, make-random-state, and numberp.

parent 73ac2b22
;-*- Mode: Lisp -*-
;;;; Author: Paul Dietz
;;;; Created: Sat Sep 6 15:40:09 2003
;;;; Contains: Tests of ISQRT
(in-package :cl-test)
(compile-and-load "numbers-aux.lsp")
(deftest isqrt.error.1
(classify-error (isqrt))
program-error)
(deftest isqrt.error.2
(classify-error (isqrt 0 0))
program-error)
(deftest isqrt.error.3
(classify-error (isqrt 0 nil))
program-error)
(deftest isqrt.error.4
(classify-error (isqrt 0 0 0))
program-error)
(deftest isqrt.error.5
(loop for x in *mini-universe*
unless (or (and (integerp x) (>= x 0))
(eq (eval `(classify-error (isqrt ',x))) 'type-error))
collect x)
nil)
(deftest isqrt.1
(loop for i from 0 to 10000
for i2 = (* i i)
for s = (isqrt i2)
unless (eql s i)
collect i)
nil)
(deftest isqrt.2
(loop for i = (random-from-interval most-positive-fixnum 0)
for s = (isqrt i)
repeat 1000
unless (and (integerp s)
(>= s 0)
(<= (* s s) i)
(> (* (1+ s) (1+ s)) i))
collect (list i s))
nil)
(deftest isqrt.3
(loop for i = (random-from-interval 1000000000000000 0)
for s = (isqrt i)
repeat 1000
unless (and (integerp s)
(>= s 0)
(<= (* s s) i)
(> (* (1+ s) (1+ s)) i))
collect (list i s))
nil)
......@@ -37,6 +37,12 @@
(load "decf.lsp")
(load "lcm.lsp")
(load "signum.lsp")
(load "sqrt.lsp")
(load "isqrt.lsp")
(load "random.lsp")
(load "random-state-p.lsp")
(load "make-random-state.lsp")
(load "numberp.lsp")
(load "rational.lsp")
(load "rationalize.lsp")
......
;-*- Mode: Lisp -*-
;;;; Author: Paul Dietz
;;;; Created: Sat Sep 6 17:53:30 2003
;;;; Contains: Tests of MAKE-RANDOM-STATE
(in-package :cl-test)
(deftest make-random-state.error.1
(classify-error (make-random-state nil nil))
program-error)
(deftest make-random-state.error.2
(classify-error (make-random-state t nil))
program-error)
(deftest make-random-state.error.3
(classify-error (make-random-state *random-state* nil))
program-error)
(deftest make-random-state.error.4
(loop for x in *mini-universe*
unless (or (null x) (eq x t) (random-state-p x)
(eq (eval `(classify-error (make-random-state ',x)))
'type-error))
collect x)
nil)
(deftest make-random-state.1
(let ((rs (make-random-state)))
(and (not (eq rs *random-state*))
(random-state-p rs)
(eqlt (random 1000000) (random 1000000 rs))))
t)
(deftest make-random-state.2
(let ((rs (make-random-state *random-state*)))
(and (not (eq rs *random-state*))
(random-state-p rs)
(eqlt (random 1000000) (random 1000000 rs))))
t)
(deftest make-random-state.3
(let ((rs (make-random-state)))
(random 10)
(let ((rs2 (make-random-state rs)))
(and (not (eq rs *random-state*))
(not (eq rs rs2))
(not (eq rs2 *random-state*))
(random-state-p rs)
(random-state-p rs2)
(eqlt (random 1.0 rs) (random 1.0 rs2)))))
t)
(deftest make-random-state.4
(let ((rs (make-random-state t))
(rs2 (make-random-state t)))
(and (random-state-p rs)
(not (eq rs *random-state*))
(random-state-p rs2)
(not (eq rs2 *random-state*))
(not (eq rs rs2))
(integerp (random 10 rs))
(floatp (random 1.0 rs2))
t))
t)
;-*- Mode: Lisp -*-
;;;; Author: Paul Dietz
;;;; Created: Sat Sep 6 18:20:36 2003
;;;; Contains: Tests of NUMBERP
(in-package :cl-test)
(deftest numberp.error.1
(classify-error (numberp))
program-error)
(deftest numberp.error.2
(classify-error (numberp 0 nil))
program-error)
(deftest numberp.error.3
(classify-error (numberp 'a nil nil))
program-error)
(deftest numberp.1
(loop for x in *universe*
for vals = (multiple-value-list (numberp x))
for val = (car vals)
unless (and (= (length vals) 1)
(if val (typep x 'number)
(not (typep x 'number))))
collect (cons x vals))
nil)
......@@ -44,13 +44,18 @@
(long-float
(<= #.(rational most-negative-long-float)
x
#.(rational most-positive-long-float))))))))
#.(rational most-positive-long-float))))))))
;;; NOTE! According to section 12.1.4.1, when a rational is compared
;;; to a float, the effect is as if the float is convert to a rational
;;; (by RATIONAL), not as if the rational is converted to a float.
;;; This means the calls to numbers-are-compatible are not necessary.
(defun =.4-fn ()
(loop for x in *numbers*
append
(loop for y in *numbers*
unless (or (not (numbers-are-compatible x y))
unless (or ;; (not (numbers-are-compatible x y))
(if (= x y) (= y x) (not (= y x))))
collect (list x y))))
......@@ -58,7 +63,7 @@
(loop for x in *numbers*
append
(loop for y in *numbers*
unless (or (not (numbers-are-compatible x y))
unless (or ;; (not (numbers-are-compatible x y))
(if (/= x y) (/= y x) (not (/= y x))))
collect (list x y))))
......@@ -66,7 +71,7 @@
(loop for x in *numbers*
append
(loop for y in *numbers*
when (and (numbers-are-compatible x y)
when (and ;; (numbers-are-compatible x y)
(if (= x y)
(/= x y)
(not (/= x y))))
......@@ -76,7 +81,7 @@
(loop for x in *reals*
nconc
(loop for y in *reals*
when (and (numbers-are-compatible x y)
when (and ;; (numbers-are-compatible x y)
(and (< x y) (> x y)))
collect (list x y))))
......@@ -84,7 +89,7 @@
(loop for x in *reals*
nconc
(loop for y in *reals*
when (and (numbers-are-compatible x y)
when (and ;; (numbers-are-compatible x y)
(if (< x y) (not (> y x))
(> y x)))
collect (list x y))))
......@@ -93,7 +98,7 @@
(loop for x in *reals*
nconc
(loop for y in *reals*
when (and (numbers-are-compatible x y)
when (and ;; (numbers-are-compatible x y)
(if (< x y) (>= x y)
(not (>= x y))))
collect (list x y))))
......@@ -102,7 +107,7 @@
(loop for x in *reals*
nconc
(loop for y in *reals*
when (and (numbers-are-compatible x y)
when (and ;; (numbers-are-compatible x y)
(if (<= x y) (not (>= y x))
(>= y x)))
collect (list x y))))
......@@ -111,7 +116,7 @@
(loop for x in *reals*
nconc
(loop for y in *reals*
when (and (numbers-are-compatible x y)
when (and ;; (numbers-are-compatible x y)
(if (<= x y) (not (or (= x y) (< x y)))
(or (= x y) (< x y))))
collect (list x y))))
......@@ -120,7 +125,7 @@
(loop for x in *reals*
nconc
(loop for y in *reals*
when (and (numbers-are-compatible x y)
when (and ;; (numbers-are-compatible x y)
(if (> x y) (<= x y)
(not (<= x y))))
collect (list x y))))
......@@ -129,7 +134,7 @@
(loop for x in *reals*
nconc
(loop for y in *reals*
when (and (numbers-are-compatible x y)
when (and ;; (numbers-are-compatible x y)
(if (>= x y) (not (or (= x y) (> x y)))
(or (= x y) (> x y))))
collect (list x y))))
......
......@@ -31,3 +31,14 @@
(rotatef (elt seq r) (elt seq (1- i))))))
seq)
(defun binomial-distribution-test (n fn)
(let* ((count (loop repeat n count (funcall fn)))
(sigma (/ (sqrt n) 2.0))
(bound (* sigma 6))
(expected (/ n 2.0)))
(<= (- expected bound)
count
(+ expected bound))))
;-*- Mode: Lisp -*-
;;;; Author: Paul Dietz
;;;; Created: Sat Sep 6 17:50:04 2003
;;;; Contains: Tests of RANDOM-STATE-P
(in-package :cl-test)
(deftest random-state-p.error.1
(classify-error (random-state-p))
program-error)
(deftest random-state-p.error.2
(classify-error (random-state-p nil nil))
program-error)
(deftest random-state-p.1
(loop for x in *universe*
when (if (typep x 'random-state)
(not (random-state-p x))
(random-state-p x))
collect x)
nil)
(deftest random-state-p.2
(notnot-mv (random-state-p *random-state*))
t)
(deftest random-state-p.3
(notnot-mv (random-state-p (make-random-state)))
t)
;-*- Mode: Lisp -*-
;;;; Author: Paul Dietz
;;;; Created: Sat Sep 6 15:47:42 2003
;;;; Contains: Tests of RANDOM
(in-package :cl-test)
(compile-and-load "numbers-aux.lsp")
(compile-and-load "random-aux.lsp")
(deftest random.error.1
(classify-error (random))
program-error)
(deftest random.error.2
(classify-error (random 10 *random-state* nil))
program-error)
(deftest random.error.3
(loop for x in *mini-universe*
unless (or (and (realp x) (> x 0))
(eq (eval `(classify-error (random ',x))) 'type-error))
collect x)
nil)
(deftest random.1
(loop for i from 2 to 30
for n = (ash 1 i)
nconc
(loop for j = (1+ (random n))
repeat 20
nconc
(loop for r = (random j)
repeat i
unless (and (integerp r)
(<= 0 r)
(< r j))
collect (list j r))))
nil)
(deftest random.2
(loop for i from 2 to 20
for n = (ash 1 i)
nconc
(loop for j = (random (float n))
repeat 20
unless (zerop j)
nconc
(loop for r = (random j)
repeat 20
unless (and (eql (float r j) r)
(<= 0 r)
(< r j))
collect (list j r))))
nil)
(deftest random.3
(binomial-distribution-test 10000
#'(lambda () (eql (random 2) 0)))
t)
(deftest random.4
(binomial-distribution-test 10000
#'(lambda () (< (random 1.0s0) 0.5s0)))
t)
(deftest random.5
(binomial-distribution-test 10000
#'(lambda () (< (random 1.0d0) 0.5d0)))
t)
(deftest random.6
(binomial-distribution-test 10000
#'(lambda () (evenp (random 1024))))
t)
(deftest random.7
(loop for x in '(10.0s0 20.0f0 30.0d0 40.0l0)
for r = (random x)
unless (eql (float r x) r)
collect (list x r))
nil)
;;; Do more statistical tests here
\ No newline at end of file
......@@ -109,6 +109,6 @@
t)
(deftest signum.11
(let ((i 0)) (values (signum (incf i)) i))
(let ((i 0)) (values (signum (the (integer 1 1) (incf i))) i))
1 1)
;-*- Mode: Lisp -*-
;;;; Author: Paul Dietz
;;;; Created: Sat Sep 6 10:54:17 2003
;;;; Contains: Tests of SQRT
(in-package :cl-test)
(compile-and-load "numbers-aux.lsp")
(deftest sqrt.error.1
(classify-error (sqrt))
program-error)
(deftest sqrt.error.2
(classify-error (sqrt 0 nil))
program-error)
(deftest sqrt.error.3
(loop for x in *mini-universe*
unless (or (numberp x)
(eq (eval `(classify-error (sqrt ',x))) 'type-error))
collect x)
nil)
(deftest sqrt.1
(let ((s (sqrt 0)))
(and (realp s)
(=t s 0)))
t)
(deftest sqrt.2
(let ((s (sqrt 1)))
(and (realp s)
(=t s 1)))
t)
(deftest sqrt.3
(loop for x in '(0.0s0 1.0s0 0.0f0 1.0f0 0.0d0 1.0d0 0.0l0 1.0l0)
for s = (sqrt x)
unless (eql s x)
collect (list x s))
nil)
(deftest sqrt.4
(loop for x in '(0.0s0 1.0s0 0.0f0 1.0f0 0.0d0 1.0d0 0.0l0 1.0l0)
for c = (complex x 0)
for s = (sqrt c)
unless (eql s c)
collect (list x c s))
nil)
(deftest sqrt.5
(loop for x in '(-1.0s0 -1.0f0 -1.0d0 -1.0l0)
for s = (sqrt x)
unless (eql s (complex 0 (- x)))
collect (list x s))
nil)
;;; (deftest sqrt.6
;;; (let ((result (sqrt (ash 1 10000))))
;;; (if (integerp result)
;;; (=t result (ash 1 5000))
;;; (=t result (float (ash 1 5000) result))))
;;; t)
(deftest sqrt.7
(let ((result (sqrt -1)))
(or (eqlt result #c(0 1))
(eqlt result #c(0.0 1.0))))
t)
(deftest sqrt.8
(loop for x in *floats*
for s = (sqrt x)
unless (cond
((zerop x) (=t x 0))
((plusp x) (and (eqlt (float s x) s)
(eqlt (float x s) x)))
(t (complexp s)))
collect (list x s))
nil)
(deftest sqrt.9
(let ((upper (rational most-positive-double-float))
(lower (rational most-negative-double-float)))
(loop for x = (random-fixnum)
repeat 1000
unless (or (< x lower)
(> x upper)
(let ((s (sqrt x)))
(or (and (rationalp s)
(>= s 0)
(eql (* s s) x))
(and (floatp s) (>= x 0))
(and (complexp s)
(zerop (realpart s))
(> (imagpart s) 0)
(< x 0)))))
collect (list x (sqrt x))))
nil)
(deftest sqrt.10
(loop for x from 1 to 1000
for x2 = (* x x)
for s = (sqrt x2)
unless (if (rationalp s) (eql x s)
(and (typep s 'single-float)
(= x s)))
collect (list x s))
nil)
(deftest sqrt.11
(loop for x from 1 to 1000
for x2 = (* x x)
for s = (sqrt (- x2))
unless (and (complexp s)
(zerop (realpart s))
(let ((i (imagpart s)))
(if (rationalp i)
(eql i x)
(= i x))))
collect (list x s))
nil)
;;; Tests of the branch cut
(deftest sqrt.12
(loop for xr = (random-fixnum)
for xi = (random-fixnum)
for c = (complex xr xi)
for s = (sqrt c)
repeat 1000
unless (or (> (realpart s) 0)
(and (= (realpart s) 0)
(>= (imagpart s) 0)))
collect (list c s))
nil)
(deftest sqrt.13
(loop for xr = (random-from-interval 1.0f6 -1.0f6)
for xi = (random-from-interval 1.0f6 -1.0f6)
for c = (complex xr xi)
for s = (sqrt c)
repeat 1000
unless (or (> (realpart s) 0)
(and (= (realpart s) 0)
(>= (imagpart s) 0)))
collect (list c s))
nil)
(deftest sqrt.14
(loop for xr = (random-from-interval 1.0s3 -1.0s3)
for xi = (random-from-interval 1.0s3 -1.0s3)
for c = (complex xr xi)
for s = (sqrt c)
repeat 1000
unless (or (> (realpart s) 0)
(and (= (realpart s) 0)
(>= (imagpart s) 0)))
collect (list c s))
nil)
(deftest sqrt.15
(loop for xr = (random-from-interval 1.0d7 -1.0d7)
for xi = (random-from-interval 1.0d7 -1.0d7)
for c = (complex xr xi)
for s = (sqrt c)
repeat 1000
unless (or (> (realpart s) 0)
(and (= (realpart s) 0)
(>= (imagpart s) 0)))
collect (list c s))
nil)
(deftest sqrt.16
(loop for xr = (random-from-interval 1.0l9 -1.0l9)
for xi = (random-from-interval 1.0l9 -1.0l9)
for c = (complex xr xi)
for s = (sqrt c)
repeat 1000
unless (or (> (realpart s) 0)
(and (= (realpart s) 0)
(>= (imagpart s) 0)))
collect (list c s))
nil)
(deftest sqrt.17
(loop for i = (random-from-interval most-positive-fixnum 0)
repeat 1000
unless (>= (sqrt i) (isqrt i))
collect i)
nil)
(deftest sqrt.18
(loop for x = (random-from-interval 1.0f6 0.0f0)
repeat 1000
unless (>= (sqrt x) (isqrt (floor x)))
collect x)
nil)
(deftest sqrt.19
(loop for x in '(0.0s0 0.0f0 0.0d0 0.0l0)
for s = (sqrt x)
unless (= s x)
collect (list x s))
nil)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment