gcd.lsp 2.19 KB
Newer Older
pfdietz's avatar
pfdietz committed
1 2 3 4 5 6 7 8 9 10
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Wed Sep  3 06:51:03 2003
;;;; Contains: Tests of GCD

(in-package :cl-test)

(compile-and-load "numbers-aux.lsp")
(compile-and-load "gcd-aux.lsp")

11 12
;;; Error tests

pfdietz's avatar
pfdietz committed
13
(deftest gcd.error.1
14
  (check-type-error #'gcd #'integerp)
pfdietz's avatar
pfdietz committed
15 16
  nil)

17 18
;;; Non-error tests

pfdietz's avatar
pfdietz committed
19 20 21 22 23 24
(deftest gcd.1
  (gcd)
  0)

(deftest gcd.2
  (loop for i = (random-fixnum)
sds's avatar
sds committed
25 26 27 28 29
        for a = (abs i)
        repeat 10000
        unless (and (eql a (gcd i))
                    (eql a (gcd 0 i)))
        collect i)
pfdietz's avatar
pfdietz committed
30 31 32 33
  nil)

(deftest gcd.3
  (loop for i = (random-from-interval 10000000000000000)
sds's avatar
sds committed
34 35 36 37 38
        for a = (abs i)
        repeat 10000
        unless (and (eql a (gcd i))
                    (eql a (gcd i 0)))
        collect i)
pfdietz's avatar
pfdietz committed
39 40 41 42
  nil)

(deftest gcd.4
  (loop for i = (random-fixnum)
sds's avatar
sds committed
43 44 45 46
        for j = (random-fixnum)
        repeat 1000
        unless (eql (my-gcd i j) (gcd i j))
        collect (list i j))
pfdietz's avatar
pfdietz committed
47 48 49 50 51
  nil)

(deftest gcd.5
  (let ((bound (ash 1 200)))
    (loop for i = (random-from-interval bound)
sds's avatar
sds committed
52 53 54 55
          for j = (random-from-interval bound)
          repeat 1000
          unless (eql (my-gcd i j) (gcd i j))
          collect (list i j)))
pfdietz's avatar
pfdietz committed
56 57 58 59
  nil)

(deftest gcd.6
  (loop for i = (random-fixnum)
sds's avatar
sds committed
60 61 62 63 64
        for j = (random-fixnum)
        for k = (random-fixnum)
        repeat 1000
        unless (eql (my-gcd i (my-gcd j k)) (gcd i j k))
        collect (list i j k))
pfdietz's avatar
pfdietz committed
65 66
  nil)

67 68
(deftest gcd.7
  (loop for i = (random-fixnum)
sds's avatar
sds committed
69 70 71 72 73 74
        for j = (random-fixnum)
        for k = (random-fixnum)
        for n = (random-fixnum)
        repeat 1000
        unless (eql (my-gcd (my-gcd i j) (my-gcd k n)) (gcd i j k n))
        collect (list i j k))
75 76 77 78
  nil)

(deftest gcd.8
  (loop for i from 1 to (min 256 (1- call-arguments-limit))
sds's avatar
sds committed
79
        always (eql (apply #'gcd (make-list i :initial-element 1)) 1))
80 81 82 83 84 85
  t)

(deftest gcd.order.1
  (let ((i 0) x y)
    (values
     (gcd (progn (setf x (incf i)) 15)
sds's avatar
sds committed
86
          (progn (setf y (incf i)) 25))
87 88 89 90 91 92 93
     i x y))
  5 2 1 2)

(deftest gcd.order.2
  (let ((i 0) x y)
    (values
     (gcd (progn (setf x (incf i)) 0)
sds's avatar
sds committed
94
          (progn (setf y (incf i)) 10))
95 96 97 98 99 100 101 102 103 104
     i x y))
  10 2 1 2)

(deftest gcd.order.3
  (let ((i 0))
    (values
     (gcd (progn (incf i) 0))
     i))
  0 1)

sds's avatar
sds committed
105