Commit 379e792f authored by pfdietz's avatar pfdietz

Added tests for - function. Refactored some other tests.

parent efa99087
......@@ -5,6 +5,8 @@
(in-package :cl-test)
(compile-and-load "numbers-aux.lsp")
(deftest evenp.error.1
(classify-error (evenp))
program-error)
......@@ -27,27 +29,25 @@
nil)
(deftest evenp.3
(let ((upper-bound most-positive-fixnum)
(lower-bound most-negative-fixnum))
(loop for x = (- (random (- upper-bound lower-bound)) lower-bound)
repeat 10000
when (or
(not (evenp (+ x x)))
(evenp (+ x x 1))
(if (evenp x)
(or (evenp (1+ x))
(evenp (1- x))
(/= (mod x 2) 0))
(or (not (evenp (1+ x)))
(not (evenp (1- x)))
(= (mod x 2) 0))))
collect x))
(loop for x = (random-fixnum)
repeat 10000
when (or
(not (evenp (+ x x)))
(evenp (+ x x 1))
(if (evenp x)
(or (evenp (1+ x))
(evenp (1- x))
(/= (mod x 2) 0))
(or (not (evenp (1+ x)))
(not (evenp (1- x)))
(= (mod x 2) 0))))
collect x)
nil)
(deftest evenp.4
(let ((upper-bound 1000000000000000)
(lower-bound -1000000000000000))
(loop for x = (- (random (- upper-bound lower-bound)) lower-bound)
(loop for x = (random-from-interval upper-bound lower-bound)
repeat 10000
when (or
(not (evenp (+ x x)))
......
......@@ -25,6 +25,7 @@
(load "times.lsp")
(load "plus.lsp")
(load "minus.lsp")
(load "evenp.lsp")
(load "oddp.lsp")
......
;-*- Mode: Lisp -*-
;;;; Author: Paul Dietz
;;;; Created: Sun Aug 31 11:15:14 2003
;;;; Contains: Tests of the - function
(in-package :cl-test)
(compile-and-load "numbers-aux.lsp")
(deftest minus.error.1
(classify-error (-))
program-error)
;;; Unary minus tests
(deftest minus.1
(loop for x in *numbers*
unless (eql (- (- x)) x)
collect x)
nil)
(deftest minus.2
(locally
(declare (notinline -))
(loop for x in *numbers*
unless (eql (- (- x)) x)
collect x))
nil)
(deftest minus.3
(loop for x in *reals*
when (and (integerp x)
(not (eql (- x) (- 0 x))))
collect x)
nil)
(deftest minus.4
(loop for x in *reals*
for neg = (- x)
when (and (floatp x)
(not (zerop x))
(not (eql neg (- 0.0s0 x)))
(eql (float 1.0s0 x)
(float 1.0s0 neg)))
collect x)
nil)
(deftest minus.5
(loop for x in *numbers*
when (and (complexp x)
(rationalp (realpart x))
(not (eql (- x) (- 0 x))))
collect x)
nil)
(deftest minus.6
(loop for x in *numbers*
for neg = (- x)
when (and (complexp x)
(floatp (realpart x))
(eql (float 1.0s0 (realpart x))
(float 1.0s0 (realpart neg)))
(or (/= neg (- 0 x))
(and (not (zerop (realpart x)))
(not (eql neg (- 0 x))))))
collect x)
nil)
(deftest minus.7
(let ((upper-bound most-positive-fixnum)
(lower-bound most-negative-fixnum))
(loop
for x = (+ (random (- upper-bound lower-bound)) lower-bound)
for neg = (- x)
repeat 10000
unless (and (integerp neg)
(eql (abs x) (abs neg))
(if (> x 0) (< neg 0) (>= neg 0))
(zerop (+ x neg))
(eql x (- neg)))
collect x))
nil)
(deftest minus.8
(let ((upper-bound (ash 1 1000))
(lower-bound (- (ash 1 1000))))
(loop
for x = (+ (random (- upper-bound lower-bound)) lower-bound)
for neg = (- x)
repeat 1000
unless (and (integerp neg)
(eql (abs x) (abs neg))
(if (> x 0) (< neg 0) (>= neg 0))
(zerop (+ x neg))
(eql x (- neg)))
collect x))
nil)
;;; Binary minus tests
(deftest subtract.1
(loop
for x = (random-fixnum)
for y = (random-fixnum)
repeat 10000
unless (and (eql (+ x (- y)) (- x y))
(eql (+ 1 x (- y)) (- x (1- y)))
(eql (+ -1 x (- y)) (- x (1+ y))))
collect (list x y))
nil)
(deftest subtract.2
(let ((bound (ash 1 1000)))
(loop
for x = (random-from-interval bound (- bound))
for y = (random-from-interval bound (- bound))
repeat 1000
unless (and (eql (+ x (- y)) (- x y))
(eql (+ 1 x (- y)) (- x (1- y)))
(eql (+ -1 x (- y)) (- x (1+ y))))
collect (list x y)))
nil)
(deftest subtract.3
(let ((args nil))
(loop for i from 1 to (min 256 call-arguments-limit)
do (push 1 args)
always (eql (apply #'- 1000 args) (- 1000 i))))
t)
;;; Float contagion
(deftest subtract.4
(loop
for type1 in '(short-float single-float double-float long-float)
for bits1 in '(13 24 50 50)
for bound1 = (ash 1 (- bits1 2))
for c1 from 1
nconc
(loop for type2 in '(short-float single-float double-float long-float)
for bits2 in '(13 24 50 50)
for bound2 = (ash 1 (- bits2 2))
for c2 from 1
nconc
(loop
for i = (random-from-interval bound1)
for x = (coerce i type1)
for j = (random-from-interval bound2)
for y = (coerce j type2)
for idiff1 = (- i j)
for idiff2 = (- j i)
for diff1 = (- x y)
for diff2 = (- y x)
repeat 10000
unless (or (zerop idiff1)
(and (eql idiff1 (- idiff2))
(eql diff1 (- diff2))
(if (<= c1 c2)
(eql (float diff1 y) diff1)
(eql (float diff1 x) diff1))
(eql (float idiff1 diff1) diff1)))
collect (list i x j y idiff1 idiff2 diff1 diff2))))
nil)
;;; Complex subtraction
(deftest subtract.5
(loop for i = (random-fixnum)
for ci = (complex i (+ i 100))
for j = (random-fixnum)
for cj = (complex j (- j 200))
for diff = (- ci cj)
repeat 10000
unless (eql diff (complex (- i j) (+ (- i j) 300)))
collect (list i ci j cj (- ci cj)))
nil)
......@@ -190,6 +190,13 @@
(if (coin) (random-nonnegative-real)
(- (random-nonnegative-real))))
(defun random-fixnum ()
(+ (random (1+ (- most-positive-fixnum most-negative-fixnum)))
most-negative-fixnum))
(defun random-from-interval (upper &optional (lower (- upper)))
(+ (random (- upper lower)) lower))
(defun epsilon (number)
(etypecase number
(short-float short-float-epsilon)
......
......@@ -5,6 +5,8 @@
(in-package :cl-test)
(compile-and-load "numbers-aux.lsp")
(deftest oddp.error.1
(classify-error (oddp))
program-error)
......@@ -27,27 +29,25 @@
nil)
(deftest oddp.3
(let ((upper-bound most-positive-fixnum)
(lower-bound most-negative-fixnum))
(loop for x = (- (random (- upper-bound lower-bound)) lower-bound)
repeat 10000
when (or
(oddp (+ x x))
(not (oddp (+ x x 1)))
(if (oddp x)
(or (oddp (1+ x))
(oddp (1- x))
(/= (mod x 2) 1))
(or (not (oddp (1+ x)))
(not (oddp (1- x)))
(/= (mod x 2) 0))))
collect x))
(loop for x = (random-fixnum)
repeat 10000
when (or
(oddp (+ x x))
(not (oddp (+ x x 1)))
(if (oddp x)
(or (oddp (1+ x))
(oddp (1- x))
(/= (mod x 2) 1))
(or (not (oddp (1+ x)))
(not (oddp (1- x)))
(/= (mod x 2) 0))))
collect x)
nil)
(deftest oddp.4
(let ((upper-bound 1000000000000000)
(lower-bound -1000000000000000))
(loop for x = (- (random (- upper-bound lower-bound)) lower-bound)
(loop for x = (random-from-interval upper-bound lower-bound)
repeat 10000
when (or
(oddp (+ x x))
......
......@@ -37,7 +37,7 @@
(let* ((upper-bound most-positive-fixnum)
(lower-bound most-negative-fixnum)
(spread (- upper-bound lower-bound)))
(flet ((%r () (- (random spread) lower-bound)))
(flet ((%r () (+ (random spread) lower-bound)))
(loop for x = (%r)
for y = (%r)
for z = (%r)
......@@ -57,7 +57,7 @@
(let* ((upper-bound 1000000000000000)
(lower-bound -1000000000000000)
(spread (- upper-bound lower-bound)))
(flet ((%r () (- (random spread) lower-bound)))
(flet ((%r () (+ (random spread) lower-bound)))
(loop for x = (%r)
for y = (%r)
for z = (%r)
......@@ -77,7 +77,7 @@
(let* ((upper-bound most-positive-fixnum)
(lower-bound most-negative-fixnum)
(spread (- upper-bound lower-bound)))
(flet ((%r () (- (random spread) lower-bound)))
(flet ((%r () (+ (random spread) lower-bound)))
(loop for x = (/ (%r) (max 1 (%r)))
for y = (/ (%r) (max 1 (%r)))
for z = (/ (%r) (max 1 (%r)))
......@@ -113,7 +113,7 @@
(let* ((upper-bound most-positive-fixnum)
(lower-bound most-negative-fixnum)
(spread (- upper-bound lower-bound)))
(flet ((%r () (- (random spread) lower-bound)))
(flet ((%r () (+ (random spread) lower-bound)))
(loop
for xr = (%r)
for xi = (%r)
......@@ -389,5 +389,3 @@
(list x x))
collect (list x eps eps2))
nil)
......@@ -52,8 +52,8 @@
(let* ((upper-bound (* 1000 1000 1000 1000))
(lower-bound (- upper-bound))
(spread (1+ (- upper-bound lower-bound))))
(loop for x = (+ (random spread) lower-bound)
for y = (+ (random spread) lower-bound)
(loop for x = (random-from-interval upper-bound)
for y = (random-from-interval upper-bound)
for prod = (* x y)
for prod2 = (integer-times x y)
repeat 1000
......@@ -93,10 +93,8 @@
nil)
(deftest *.9
(let* ((upper-bound (* 1000 1000 1000 1000))
(lower-bound (- upper-bound))
(spread (1+ (- upper-bound lower-bound))))
(flet ((%r () (+ (random spread) lower-bound)))
(let* ((upper-bound (* 1000 1000 1000 1000)))
(flet ((%r () (random-from-interval upper-bound)))
(loop for xr = (%r)
for xc = (%r)
for x = (complex xr xc)
......@@ -182,8 +180,8 @@
(deftest *.14
(let ((bound (- (sqrt most-positive-short-float) 1)))
(loop for x = (- (random (* bound 2)) bound)
for y = (- (random (* bound 2)) bound)
(loop for x = (random-from-interval bound)
for y = (random-from-interval bound)
for p = (* x y)
repeat 10000
unless (and (eql p (* y x))
......@@ -193,8 +191,8 @@
(deftest *.15
(let ((bound (- (sqrt most-positive-single-float) 1)))
(loop for x = (- (random (* bound 2)) bound)
for y = (- (random (* bound 2)) bound)
(loop for x = (random-from-interval bound)
for y = (random-from-interval bound)
for p = (* x y)
repeat 10000
unless (and (eql p (* y x))
......@@ -204,8 +202,8 @@
(deftest *.16
(let ((bound (- (sqrt most-positive-double-float) 1)))
(loop for x = (- (random (* bound 2)) bound)
for y = (- (random (* bound 2)) bound)
(loop for x = (random-from-interval bound)
for y = (random-from-interval bound)
for p = (* x y)
repeat 10000
unless (and (eql p (* y x))
......@@ -215,8 +213,8 @@
(deftest *.17
(let ((bound (- (sqrt most-positive-long-float) 1)))
(loop for x = (- (random (* bound 2)) bound)
for y = (- (random (* bound 2)) bound)
(loop for x = (random-from-interval bound)
for y = (random-from-interval bound)
for p = (* x y)
repeat 10000
unless (and (eql p (* y x))
......@@ -227,8 +225,8 @@
(deftest *.18
(let ((bound (- (sqrt most-positive-short-float) 1))
(bound2 (- (sqrt most-positive-single-float) 1)))
(loop for x = (- (random (* bound 2)) bound)
for y = (- (random (* bound2 2)) bound2)
(loop for x = (random-from-interval bound)
for y = (random-from-interval bound2)
for p = (* x y)
repeat 10000
unless (and (eql p (* y x))
......@@ -239,8 +237,8 @@
(deftest *.19
(let ((bound (- (sqrt most-positive-short-float) 1))
(bound2 (- (sqrt most-positive-double-float) 1)))
(loop for x = (- (random (* bound 2)) bound)
for y = (- (random (* bound2 2)) bound2)
(loop for x = (random-from-interval bound)
for y = (random-from-interval bound2)
for p = (* x y)
repeat 10000
unless (and (eql p (* y x))
......@@ -251,8 +249,8 @@
(deftest *.20
(let ((bound (- (sqrt most-positive-short-float) 1))
(bound2 (- (sqrt most-positive-long-float) 1)))
(loop for x = (- (random (* bound 2)) bound)
for y = (- (random (* bound2 2)) bound2)
(loop for x = (random-from-interval bound)
for y = (random-from-interval bound2)
for p = (* x y)
repeat 10000
unless (and (eql p (* y x))
......@@ -263,8 +261,8 @@
(deftest *.21
(let ((bound (- (sqrt most-positive-single-float) 1))
(bound2 (- (sqrt most-positive-double-float) 1)))
(loop for x = (- (random (* bound 2)) bound)
for y = (- (random (* bound2 2)) bound2)
(loop for x = (random-from-interval bound)
for y = (random-from-interval bound2)
for p = (* x y)
repeat 10000
unless (and (eql p (* y x))
......@@ -275,8 +273,8 @@
(deftest *.22
(let ((bound (- (sqrt most-positive-single-float) 1))
(bound2 (- (sqrt most-positive-long-float) 1)))
(loop for x = (- (random (* bound 2)) bound)
for y = (- (random (* bound2 2)) bound2)
(loop for x = (random-from-interval bound)
for y = (random-from-interval bound2)
for p = (* x y)
repeat 10000
unless (and (eql p (* y x))
......@@ -287,8 +285,8 @@
(deftest *.23
(let ((bound (- (sqrt most-positive-double-float) 1))
(bound2 (- (sqrt most-positive-long-float) 1)))
(loop for x = (- (random (* bound 2)) bound)
for y = (- (random (* bound2 2)) bound2)
(loop for x = (random-from-interval bound)
for y = (random-from-interval bound2)
for p = (* x y)
repeat 10000
unless (and (eql p (* y x))
......
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