Commit ee6d1507 authored by pfdietz's avatar pfdietz
Browse files

Reduced iteration count on randomized numeric tests to 1000. Added LCM tests.

parent 4738f34a
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
for vals = (multiple-value-list (ceiling n d)) for vals = (multiple-value-list (ceiling n d))
for (q r) = vals for (q r) = vals
for n2 = (+ (* q d) r) for n2 = (+ (* q d) r)
repeat 10000 repeat 1000
unless (and (eql (length vals) 2) unless (and (eql (length vals) 2)
(integerp q) (integerp q)
(= n n2) (= n n2)
...@@ -28,7 +28,7 @@ ...@@ -28,7 +28,7 @@
for vals = (multiple-value-list (ceiling n d)) for vals = (multiple-value-list (ceiling n d))
for (q r) = vals for (q r) = vals
for n2 = (+ (* q d) r) for n2 = (+ (* q d) r)
repeat 10000 repeat 1000
unless (and (eql (length vals) 2) unless (and (eql (length vals) 2)
(integerp q) (integerp q)
(<= r 0) (<= r 0)
...@@ -41,7 +41,7 @@ ...@@ -41,7 +41,7 @@
for vals = (multiple-value-list (ceiling n)) for vals = (multiple-value-list (ceiling n))
for (q r) = vals for (q r) = vals
for n2 = (+ q r) for n2 = (+ q r)
repeat 10000 repeat 1000
unless (and (eql (length vals) 2) unless (and (eql (length vals) 2)
(integerp q) (integerp q)
(= n n2) (= n n2)
...@@ -57,7 +57,7 @@ ...@@ -57,7 +57,7 @@
for vals = (multiple-value-list (ceiling n)) for vals = (multiple-value-list (ceiling n))
for (q r) = vals for (q r) = vals
for n2 = (+ q r) for n2 = (+ q r)
repeat 10000 repeat 1000
unless (and (eql (length vals) 2) unless (and (eql (length vals) 2)
(integerp q) (integerp q)
(rationalp r) (rationalp r)
...@@ -77,7 +77,7 @@ ...@@ -77,7 +77,7 @@
for vals = (multiple-value-list (ceiling n d)) for vals = (multiple-value-list (ceiling n d))
for (q r) = vals for (q r) = vals
for n2 = (+ (* q d) r) for n2 = (+ (* q d) r)
repeat 10000 repeat 1000
unless (and (eql (length vals) 2) unless (and (eql (length vals) 2)
(integerp q) (integerp q)
(rationalp r) (rationalp r)
...@@ -96,7 +96,7 @@ ...@@ -96,7 +96,7 @@
for vals = (multiple-value-list (ceiling n d)) for vals = (multiple-value-list (ceiling n d))
for (q r) = vals for (q r) = vals
for n2 = (+ (* q d) r) for n2 = (+ (* q d) r)
repeat 10000 repeat 1000
unless (and (eql (length vals) 2) unless (and (eql (length vals) 2)
(integerp q) (integerp q)
(rationalp r) (rationalp r)
......
...@@ -45,7 +45,7 @@ ...@@ -45,7 +45,7 @@
(deftest /.3 (deftest /.3
(loop for i = (random-fixnum) (loop for i = (random-fixnum)
repeat 10000 repeat 1000
unless (or (zerop i) unless (or (zerop i)
(let ((q1 (/ i)) (let ((q1 (/ i))
(q2 (/ 1 i))) (q2 (/ 1 i)))
...@@ -63,7 +63,7 @@ ...@@ -63,7 +63,7 @@
for g = (gcd i j) for g = (gcd i j)
for q = (/ i j) for q = (/ i j)
for q2 = (/ j) for q2 = (/ j)
repeat 10000 repeat 1000
unless (and (integerp g) unless (and (integerp g)
(zerop (mod i g)) (zerop (mod i g))
(zerop (mod j g)) (zerop (mod j g))
...@@ -80,7 +80,7 @@ ...@@ -80,7 +80,7 @@
(loop for i = (1+ (random bound)) (loop for i = (1+ (random bound))
for r1 = (/ i) for r1 = (/ i)
for r2 = (/ 1 i) for r2 = (/ 1 i)
repeat 10000 repeat 1000
unless (eql r1 r2) unless (eql r1 r2)
collect (list i r1 r2))) collect (list i r1 r2)))
nil) nil)
...@@ -91,7 +91,7 @@ ...@@ -91,7 +91,7 @@
for i = (if (zerop i1) 1 i1) for i = (if (zerop i1) 1 i1)
for c = (complex 0 i) for c = (complex 0 i)
for r = (/ c) for r = (/ c)
repeat 10000 repeat 1000
unless (eql r (complex 0 (- (/ i)))) unless (eql r (complex 0 (- (/ i))))
collect (list i c r)) collect (list i c r))
nil) nil)
...@@ -132,7 +132,7 @@ ...@@ -132,7 +132,7 @@
(loop for a = (random-fixnum) (loop for a = (random-fixnum)
for b = (random-fixnum) for b = (random-fixnum)
for m = (+ (* a a) (* b b)) for m = (+ (* a a) (* b b))
repeat 10000 repeat 1000
unless unless
(or (zerop m) (or (zerop m)
(let* ((q (/ (complex a b))) (let* ((q (/ (complex a b)))
...@@ -148,7 +148,7 @@ ...@@ -148,7 +148,7 @@
(loop for a = (random-from-interval bound) (loop for a = (random-from-interval bound)
for b = (random-from-interval bound) for b = (random-from-interval bound)
for m = (+ (* a a) (* b b)) for m = (+ (* a a) (* b b))
repeat 10000 repeat 1000
unless unless
(or (zerop m) (or (zerop m)
(let* ((q (/ (complex a b))) (let* ((q (/ (complex a b)))
...@@ -164,7 +164,7 @@ ...@@ -164,7 +164,7 @@
for b = (random-fixnum) for b = (random-fixnum)
for n = (complex (random-fixnum) (random-fixnum)) for n = (complex (random-fixnum) (random-fixnum))
for m = (+ (* a a) (* b b)) for m = (+ (* a a) (* b b))
repeat 10000 repeat 1000
unless unless
(or (zerop m) (or (zerop m)
(let* ((q (/ n (complex a b))) (let* ((q (/ n (complex a b)))
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
for vals = (multiple-value-list (floor n d)) for vals = (multiple-value-list (floor n d))
for (q r) = vals for (q r) = vals
for n2 = (+ (* q d) r) for n2 = (+ (* q d) r)
repeat 10000 repeat 1000
unless (and (eql (length vals) 2) unless (and (eql (length vals) 2)
(integerp q) (integerp q)
(= n n2) (= n n2)
...@@ -28,7 +28,7 @@ ...@@ -28,7 +28,7 @@
for vals = (multiple-value-list (floor n d)) for vals = (multiple-value-list (floor n d))
for (q r) = vals for (q r) = vals
for n2 = (+ (* q d) r) for n2 = (+ (* q d) r)
repeat 10000 repeat 1000
unless (and (eql (length vals) 2) unless (and (eql (length vals) 2)
(integerp q) (integerp q)
(<= 0 r) (<= 0 r)
...@@ -41,7 +41,7 @@ ...@@ -41,7 +41,7 @@
for vals = (multiple-value-list (floor n)) for vals = (multiple-value-list (floor n))
for (q r) = vals for (q r) = vals
for n2 = (+ q r) for n2 = (+ q r)
repeat 10000 repeat 1000
unless (and (eql (length vals) 2) unless (and (eql (length vals) 2)
(integerp q) (integerp q)
(= n n2) (= n n2)
...@@ -57,7 +57,7 @@ ...@@ -57,7 +57,7 @@
for vals = (multiple-value-list (floor n)) for vals = (multiple-value-list (floor n))
for (q r) = vals for (q r) = vals
for n2 = (+ q r) for n2 = (+ q r)
repeat 10000 repeat 1000
unless (and (eql (length vals) 2) unless (and (eql (length vals) 2)
(integerp q) (integerp q)
(rationalp r) (rationalp r)
...@@ -77,7 +77,7 @@ ...@@ -77,7 +77,7 @@
for vals = (multiple-value-list (floor n d)) for vals = (multiple-value-list (floor n d))
for (q r) = vals for (q r) = vals
for n2 = (+ (* q d) r) for n2 = (+ (* q d) r)
repeat 10000 repeat 1000
unless (and (eql (length vals) 2) unless (and (eql (length vals) 2)
(integerp q) (integerp q)
(rationalp r) (rationalp r)
...@@ -96,7 +96,7 @@ ...@@ -96,7 +96,7 @@
for vals = (multiple-value-list (floor n d)) for vals = (multiple-value-list (floor n d))
for (q r) = vals for (q r) = vals
for n2 = (+ (* q d) r) for n2 = (+ (* q d) r)
repeat 10000 repeat 1000
unless (and (eql (length vals) 2) unless (and (eql (length vals) 2)
(integerp q) (integerp q)
(rationalp r) (rationalp r)
......
...@@ -23,4 +23,12 @@ ...@@ -23,4 +23,12 @@
(psetq x (mod y x) (psetq x (mod y x)
y x))) y x)))
(defun my-lcm (x y)
(when (< x 0) (setf x (- x)))
(when (< y 0) (setf y (- y)))
(if (or (= x 0) (= y 0))
0
(/ (* x y) (my-gcd x y))))
;-*- Mode: Lisp -*-
;;;; Author: Paul Dietz
;;;; Created: Thu Sep 4 22:03:21 2003
;;;; Contains: Tests of LCM
(in-package :cl-test)
(compile-and-load "numbers-aux.lsp")
(compile-and-load "gcd-aux.lsp")
(deftest lcm.error.1
(loop for x in *mini-universe*
unless (or (integerp x)
(eq (eval `(classify-error (lcm ',x))) 'type-error))
collect x)
nil)
(deftest lcm.1
(lcm)
1)
(deftest lcm.2
(loop for i = (random-fixnum)
for a = (abs i)
repeat 1000
unless (and (eql a (lcm i))
(eql a (lcm 1 i)))
collect i)
nil)
(deftest lcm.3
(loop for i = (random-from-interval 10000000000000000)
for a = (abs i)
repeat 1000
unless (and (eql a (lcm i))
(eql a (lcm i 1)))
collect i)
nil)
(deftest lcm.4
(loop for i = (random-fixnum)
for j = (random-fixnum)
repeat 1000
unless (eql (my-lcm i j) (lcm i j))
collect (list i j))
nil)
(deftest lcm.5
(let ((bound (ash 1 200)))
(loop for i = (random-from-interval bound)
for j = (random-from-interval bound)
repeat 1000
unless (eql (my-lcm i j) (lcm i j))
collect (list i j)))
nil)
(deftest lcm.6
(loop for i = (random-fixnum)
for j = (random-fixnum)
for k = (random-fixnum)
repeat 1000
unless (eql (my-lcm i (my-lcm j k)) (lcm i j k))
collect (list i j k))
nil)
(deftest lcm.7
(loop for i = (random-fixnum)
for j = (random-fixnum)
for k = (random-fixnum)
for n = (random-fixnum)
repeat 1000
unless (eql (my-lcm (my-lcm i j) (my-lcm k n)) (lcm i j k n))
collect (list i j k))
nil)
(deftest lcm.8
(loop for i from 1 to (min 256 (1- call-arguments-limit))
always (eql (apply #'lcm (make-list i :initial-element 1)) 1))
t)
(deftest lcm.order.1
(let ((i 0) x y)
(values
(lcm (progn (setf x (incf i)) 15)
(progn (setf y (incf i)) 25))
i x y))
75 2 1 2)
(deftest lcm.order.2
(let ((i 0) x y)
(values
(lcm (progn (setf x (incf i)) 0)
(progn (setf y (incf i)) 10))
i x y))
0 2 1 2)
(deftest lcm.order.3
(let ((i 0))
(values
(lcm (progn (incf i) 0))
i))
0 1)
...@@ -35,6 +35,7 @@ ...@@ -35,6 +35,7 @@
(load "gcd.lsp") (load "gcd.lsp")
(load "incf.lsp") (load "incf.lsp")
(load "decf.lsp") (load "decf.lsp")
(load "lcm.lsp")
(load "rational.lsp") (load "rational.lsp")
(load "rationalize.lsp") (load "rationalize.lsp")
......
...@@ -71,7 +71,7 @@ ...@@ -71,7 +71,7 @@
(loop (loop
for x = (+ (random (- upper-bound lower-bound)) lower-bound) for x = (+ (random (- upper-bound lower-bound)) lower-bound)
for neg = (- x) for neg = (- x)
repeat 10000 repeat 1000
unless (and (integerp neg) unless (and (integerp neg)
(eql (abs x) (abs neg)) (eql (abs x) (abs neg))
(if (> x 0) (< neg 0) (>= neg 0)) (if (> x 0) (< neg 0) (>= neg 0))
...@@ -101,7 +101,7 @@ ...@@ -101,7 +101,7 @@
(loop (loop
for x = (random-fixnum) for x = (random-fixnum)
for y = (random-fixnum) for y = (random-fixnum)
repeat 10000 repeat 1000
unless (and (eql (+ x (- y)) (- x y)) unless (and (eql (+ x (- y)) (- x y))
(eql (+ 1 x (- y)) (- x (1- y))) (eql (+ 1 x (- y)) (- x (1- y)))
(eql (+ -1 x (- y)) (- x (1+ y)))) (eql (+ -1 x (- y)) (- x (1+ y))))
...@@ -150,7 +150,7 @@ ...@@ -150,7 +150,7 @@
for idiff2 = (- j i) for idiff2 = (- j i)
for diff1 = (- x y) for diff1 = (- x y)
for diff2 = (- y x) for diff2 = (- y x)
repeat 10000 repeat 1000
unless (or (zerop idiff1) unless (or (zerop idiff1)
(and (eql idiff1 (- idiff2)) (and (eql idiff1 (- idiff2))
(eql diff1 (- diff2)) (eql diff1 (- diff2))
...@@ -169,7 +169,7 @@ ...@@ -169,7 +169,7 @@
for j = (random-fixnum) for j = (random-fixnum)
for cj = (complex j (- j 200)) for cj = (complex j (- j 200))
for diff = (- ci cj) for diff = (- ci cj)
repeat 10000 repeat 1000
unless (eql diff (complex (- i j) (+ (- i j) 300))) unless (eql diff (complex (- i j) (+ (- i j) 300)))
collect (list i ci j cj (- ci cj))) collect (list i ci j cj (- ci cj)))
nil) nil)
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
(loop for x = (random-fixnum) (loop for x = (random-fixnum)
for y = (1- x) for y = (1- x)
for z = (- x 1) for z = (- x 1)
repeat 10000 repeat 1000
unless (eql y z) unless (eql y z)
collect (list x y z)) collect (list x y z))
nil) nil)
...@@ -41,7 +41,7 @@ ...@@ -41,7 +41,7 @@
(loop for x = (random (1- most-positive-short-float)) (loop for x = (random (1- most-positive-short-float))
for y = (1- x) for y = (1- x)
for z = (- x 1.0s0) for z = (- x 1.0s0)
repeat 10000 repeat 1000
unless (eql y z) unless (eql y z)
collect (list x y z)) collect (list x y z))
nil) nil)
...@@ -50,7 +50,7 @@ ...@@ -50,7 +50,7 @@
(loop for x = (random (1- most-positive-single-float)) (loop for x = (random (1- most-positive-single-float))
for y = (1- x) for y = (1- x)
for z = (- x 1.0f0) for z = (- x 1.0f0)
repeat 10000 repeat 1000
unless (eql y z) unless (eql y z)
collect (list x y z)) collect (list x y z))
nil) nil)
...@@ -59,7 +59,7 @@ ...@@ -59,7 +59,7 @@
(loop for x = (random (1- most-positive-double-float)) (loop for x = (random (1- most-positive-double-float))
for y = (1- x) for y = (1- x)
for z = (- x 1.0d0) for z = (- x 1.0d0)
repeat 10000 repeat 1000
unless (eql y z) unless (eql y z)
collect (list x y z)) collect (list x y z))
nil) nil)
...@@ -68,7 +68,7 @@ ...@@ -68,7 +68,7 @@
(loop for x = (random (1- most-positive-long-float)) (loop for x = (random (1- most-positive-long-float))
for y = (1- x) for y = (1- x)
for z = (- x 1.0l0) for z = (- x 1.0l0)
repeat 10000 repeat 1000
unless (eql y z) unless (eql y z)
collect (list x y z)) collect (list x y z))
nil) nil)
...@@ -80,7 +80,7 @@ ...@@ -80,7 +80,7 @@
for r = (/ x y2) for r = (/ x y2)
for r1 = (1- r) for r1 = (1- r)
for r2 = (- r 1) for r2 = (- r 1)
repeat 10000 repeat 1000
unless (eql r1 r2) unless (eql r1 r2)
collect (list x y2 r1 r2)) collect (list x y2 r1 r2))
nil) nil)
...@@ -93,7 +93,7 @@ ...@@ -93,7 +93,7 @@
for r = (/ x y2) for r = (/ x y2)
for r1 = (1- r) for r1 = (1- r)
for r2 = (- r 1) for r2 = (- r 1)
repeat 10000 repeat 1000
unless (eql r1 r2) unless (eql r1 r2)
collect (list x y2 r1 r2))) collect (list x y2 r1 r2)))
nil) nil)
...@@ -104,7 +104,7 @@ ...@@ -104,7 +104,7 @@
for xi = (random-fixnum) for xi = (random-fixnum)
for xc = (complex xr xi) for xc = (complex xr xi)
for xc1 = (1- xc) for xc1 = (1- xc)
repeat 10000 repeat 1000
unless (eql xc1 (complex (- xr 1) xi)) unless (eql xc1 (complex (- xr 1) xi))
collect (list xr xi xc xc1)) collect (list xr xi xc xc1))
nil) nil)
...@@ -116,7 +116,7 @@ ...@@ -116,7 +116,7 @@
for xi = (random-from-interval bound) for xi = (random-from-interval bound)
for xc = (complex xr xi) for xc = (complex xr xi)
for xc1 = (1- xc) for xc1 = (1- xc)
repeat 10000 repeat 1000
unless (eql xc1 (complex (- xr 1) xi)) unless (eql xc1 (complex (- xr 1) xi))
collect (list xr xi xc xc1))) collect (list xr xi xc xc1)))
nil) nil)
...@@ -127,7 +127,7 @@ ...@@ -127,7 +127,7 @@
for xi = (random bound) for xi = (random bound)
for xc = (complex xr xi) for xc = (complex xr xi)
for xc1 = (1- xc) for xc1 = (1- xc)
repeat 10000 repeat 1000
unless (eql xc1 (complex (- xr 1) xi)) unless (eql xc1 (complex (- xr 1) xi))
collect (list xr xi xc xc1))) collect (list xr xi xc xc1)))
nil) nil)
...@@ -138,7 +138,7 @@ ...@@ -138,7 +138,7 @@
for xi = (random bound) for xi = (random bound)
for xc = (complex xr xi) for xc = (complex xr xi)
for xc1 = (1- xc) for xc1 = (1- xc)
repeat 10000 repeat 1000
unless (eql xc1 (complex (- xr 1) xi)) unless (eql xc1 (complex (- xr 1) xi))
collect (list xr xi xc xc1))) collect (list xr xi xc xc1)))
nil) nil)
...@@ -149,7 +149,7 @@ ...@@ -149,7 +149,7 @@
for xi = (random bound) for xi = (random bound)
for xc = (complex xr xi) for xc = (complex xr xi)
for xc1 = (1- xc) for xc1 = (1- xc)
repeat 10000 repeat 1000
unless (eql xc1 (complex (- xr 1) xi)) unless (eql xc1 (complex (- xr 1) xi))
collect (list xr xi xc xc1))) collect (list xr xi xc xc1)))
nil) nil)
...@@ -160,7 +160,7 @@ ...@@ -160,7 +160,7 @@
for xi = (random bound) for xi = (random bound)
for xc = (complex xr xi) for xc = (complex xr xi)
for xc1 = (1- xc) for xc1 = (1- xc)
repeat 10000 repeat 1000
unless (eql xc1 (complex (- xr 1) xi)) unless (eql xc1 (complex (- xr 1) xi))
collect (list xr xi xc xc1))) collect (list xr xi xc xc1)))
nil) nil)
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
(loop for x = (random-fixnum) (loop for x = (random-fixnum)
for y = (1+ x) for y = (1+ x)
for z = (+ x 1) for z = (+ x 1)
repeat 10000 repeat 1000
unless (eql y z) unless (eql y z)
collect (list x y z)) collect (list x y z))
nil) nil)
...@@ -41,7 +41,7 @@ ...@@ -41,7 +41,7 @@
(loop for x = (random (1- most-positive-short-float)) (loop for x = (random (1- most-positive-short-float))
for y = (1+ x) for y = (1+ x)
for z = (+ x 1.0s0) for z = (+ x 1.0s0)
repeat 10000 repeat 1000
unless (eql y z) unless (eql y z)
collect (list x y z)) collect (list x y z))
nil) nil)
...@@ -50,7 +50,7 @@ ...@@ -50,7 +50,7 @@
(loop for x = (random (1- most-positive-single-float)) (loop for x = (random (1- most-positive-single-float))
for y = (1+ x) for y = (1+ x)
for z = (+ x 1.0f0) for z = (+ x 1.0f0)
repeat 10000 repeat 1000
unless (eql y z) unless (eql y z)
collect (list x y z)) collect (list x y z))
nil) nil)
...@@ -59,7 +59,7 @@ ...@@ -59,7 +59,7 @@
(loop for x = (random (1- most-positive-double-float)) (loop for x = (random (1- most-positive-double-float))
for y = (1+ x) for y = (1+ x)
for z = (+ x 1.0d0) for z = (+ x 1.0d0)
repeat 10000 repeat 1000
unless (eql y z) unless (eql y z)
collect (list x y z)) collect (list x y z))
nil) nil)
...@@ -68,7 +68,7 @@ ...@@ -68,7 +68,7 @@
(loop for x = (random (1- most-positive-long-float))