rationalize.lsp 1.56 KB
Newer Older
1 2 3 4 5
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Mon Sep  1 14:00:45 2003
;;;; Contains: Tests of RATIONALIZE

6

7 8

(deftest rationalize.error.1
9 10
  (signals-error (rationalize) program-error)
  t)
11 12

(deftest rationalize.error.2
13 14
  (signals-error (rationalize 0 nil) program-error)
  t)
15 16

(deftest rationalize.error.3
17 18
  (signals-error (rationalize 0 0) program-error)
  t)
19 20

(deftest rationalize.error.4
pfdietz's avatar
pfdietz committed
21
  (check-type-error #'rationalize #'realp)
22 23 24
  nil)

(deftest rationalize.1
25
  (loop for x in (loop for r in *reals*
sds's avatar
sds committed
26 27 28 29 30 31 32 33 34
                       when (or (not (floatp r))
                                (<= -1000 (nth-value 1 (integer-decode-float r)) 1000))
                       collect r)
        for r = (rationalize x)
        unless (and (rationalp r)
                    (if (floatp x)
                        (= (float r x) x)
                      (eql x r)))
        collect (list x r))
35 36 37 38
  nil)

(deftest rationalize.2
  (loop for type in '(short-float single-float double-float long-float)
sds's avatar
sds committed
39 40 41 42 43
        collect
        (loop for i from -10000 to 10000
              for x = (coerce i type)
              for r = (rationalize x)
              count (not (eql r i))))
44 45 46 47
  (0 0 0 0))

(deftest rationalize.3
  (loop for type in '(short-float single-float double-float long-float)
sds's avatar
sds committed
48 49 50 51 52 53 54 55
        for bound in '(1.0s5 1.0f10 1.0d20 1.0l30)
        nconc
        (loop for x = (random-from-interval bound)
              for r = (rationalize x)
              for x2 = (float r x)
              repeat 1000
              unless (and (rationalp r) (= x x2))
              collect (list x r x2)))
56
  nil)