number-comparison.lsp 55.5 KB
Newer Older
1 2 3 4 5
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Mon Apr  7 07:17:42 2003
;;;; Contains: Tests of =, /=, <, <=, >, >=

6

7

8

pfdietz's avatar
pfdietz committed
9

10 11 12
;;; Errors tests on comparison functions

(deftest =.error.1
13 14
  (signals-error (=) program-error)
  t)
15 16

(deftest /=.error.1
17 18
  (signals-error (/=) program-error)
  t)
19 20

(deftest <.error.1
21 22
  (signals-error (<) program-error)
  t)
23 24

(deftest <=.error.1
25 26
  (signals-error (<=) program-error)
  t)
27 28

(deftest >.error.1
29 30
  (signals-error (>) program-error)
  t)
31 32

(deftest >=.error.1
33 34
  (signals-error (>=) program-error)
  t)
35 36 37 38 39

;;; Tests of =

(deftest =.1
  (loop for x in *numbers*
sds's avatar
sds committed
40 41
        unless (= x)
        collect x)
42 43 44 45
  nil)

(deftest =.2
  (loop for x in *numbers*
sds's avatar
sds committed
46 47
        unless (= x x)
        collect x)
48 49 50 51
  nil)

(deftest =.3
  (loop for x in *numbers*
sds's avatar
sds committed
52 53
        unless (= x x x)
        collect x)
54 55 56 57 58 59 60 61
  nil)

(deftest =.4
  (=.4-fn)
  nil)

(deftest =.5
  (loop for i from 1 to 10000
sds's avatar
sds committed
62 63
        for i2 = (1+ i)
        never (or (= i i2) (= i2 i)))
64 65 66 67
  t)

(deftest =.6
  (loop for i from 5 to 10000 by 17
sds's avatar
sds committed
68 69 70 71 72 73 74 75 76
        for j from 2 to i by 19
        for r = (/ i j)
        unless (and (not (= r (1+ r)))
                    (not (= r 0))
                    (not (= r (- r)))
                    (= r r))
        collect r)
  nil)

77 78
(deftest =.7
  (let ((args nil))
79
    (loop for i from 1 to (min 256 (1- call-arguments-limit))
sds's avatar
sds committed
80 81
          do (push 17 args)
          always (apply #'= args)))
82 83 84
  t)

(deftest =.8
85
  (loop for i from 2 to (min 256 (1- call-arguments-limit))
sds's avatar
sds committed
86 87 88 89
        for args = (append (make-list (1- i) :initial-element 7)
                           (list 23))
        when (apply #'= args)
        collect args)
90 91
  nil)

pfdietz's avatar
pfdietz committed
92

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
(deftest =.9
  (=t 0 0.0)
  t)

(deftest =.10
  (=t 0 #c(0 0))
  t)

(deftest =.11
  (=t 1 #c(1.0 0.0))
  t)

(deftest =.12
  (=t -0.0 0.0)
  t)
pfdietz's avatar
pfdietz committed
108 109 110

(deftest =.13
  (let ((nums '(0 0.0s0 0.0f0 0.0d0 0.0l0
sds's avatar
sds committed
111 112
                  #c(0.0s0 0.0s0) #c(0.0f0 0.0f0)
                  #c(0.0d0 0.0d0) #c(0.0l0 0.0l0))))
pfdietz's avatar
pfdietz committed
113
    (loop for x in nums
sds's avatar
sds committed
114 115 116 117
          append
          (loop for y in nums
                unless (= x y)
                collect (list x y))))
pfdietz's avatar
pfdietz committed
118 119 120 121
  nil)

(deftest =.14
  (let ((nums '(17 17.0s0 17.0f0 17.0d0 17.0l0
sds's avatar
sds committed
122 123
                   #c(17.0s0 0.0s0) #c(17.0f0 0.0f0)
                   #c(17.0d0 0.0d0) #c(17.0l0 0.0l0))))
pfdietz's avatar
pfdietz committed
124
    (loop for x in nums
sds's avatar
sds committed
125 126 127 128
          append
          (loop for y in nums
                unless (= x y)
                collect (list x y))))
pfdietz's avatar
pfdietz committed
129 130 131 132
  nil)

(deftest =.15
  (let ((nums '(-17 -17.0s0 -17.0f0 -17.0d0 -17.0l0
sds's avatar
sds committed
133 134
                    #c(-17.0s0 0.0s0) #c(-17.0f0 0.0f0)
                    #c(-17.0d0 0.0d0) #c(-17.0l0 0.0l0))))
pfdietz's avatar
pfdietz committed
135
    (loop for x in nums
sds's avatar
sds committed
136 137 138 139
          append
          (loop for y in nums
                unless (= x y)
                collect (list x y))))
pfdietz's avatar
pfdietz committed
140 141 142 143 144
  nil)

(deftest =.16
  (let ((n 60000) (m 30000))
    (loop for x = (- (random n) m)
sds's avatar
sds committed
145 146 147 148 149 150 151 152 153 154 155 156 157
          for y = (- (random n) m)
          for z = (- (random n) m)
          for w = (- (random n) m)
          for a = (* x y)
          for b = (* x w)
          for c = (* y z)
          for d = (* w z)
          repeat 10000
          when (and (/= b 0)
                    (/= d 0)
                    (or (not (= (/ a b) (/ c d)))
                        (/= (/ a b) (/ c d))))
          collect (list a b c d)))
pfdietz's avatar
pfdietz committed
158 159
  nil)

160 161 162 163
;;; Comparison of a rational with a float

(deftest =.17
  (loop for x in '(1.0s0 1.0f0 1.0d0 1.0l0)
sds's avatar
sds committed
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
        for eps in (list short-float-epsilon single-float-epsilon
                         double-float-epsilon long-float-epsilon)
        for exp = (nth-value 1 (decode-float eps))
        for radix = (float-radix eps)
        when (< (* (log radix 2) exp) 1000)
        nconc
        (let* ((rat (rational eps))
               (xrat (rational x)))
          (loop for i from 2 to 100
                for rat/i = (/ rat i)
                for xrat+rat/i = (+ xrat rat/i)
                nconc
                (if (= x xrat+rat/i)
                    (list (list x i  xrat+rat/i))
                  nil))))
179 180 181 182
  nil)

(deftest =.18
  (loop for x in '(1.0s0 1.0f0 1.0d0 1.0l0)
sds's avatar
sds committed
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
        for eps in (list short-float-negative-epsilon single-float-negative-epsilon
                         double-float-negative-epsilon long-float-negative-epsilon)
        for exp = (nth-value 1 (decode-float eps))
        for radix = (float-radix eps)
        when (< (* (log radix 2) exp) 1000)
        nconc
        (let* ((rat (rational eps))
               (xrat (rational x)))
          (loop for i from 2 to 100
                for rat/i = (/ rat i)
                for xrat-rat/i = (- xrat rat/i)
                nconc
                (if (= x xrat-rat/i)
                    (list (list x i xrat-rat/i))
                  nil))))
198 199
  nil)

200 201 202
(deftest =.19
  (let ((bound (expt 10 1000)))
    (loop for x in (list most-positive-short-float most-positive-single-float
sds's avatar
sds committed
203 204 205 206 207
                         most-positive-double-float most-positive-long-float)
          for d = (and (<= x bound) (truncate x))
          when (and d (or (= (* 3/2 d) x)
                          (= x (* 5/4 d))))
          collect (list x d (* 3/2 d) (* 5/4 d))))
208 209
  nil)

210 211 212 213
(deftest =.order.1
  (let ((i 0) x y)
    (values
     (= (progn (setf x (incf i)) 1)
sds's avatar
sds committed
214
        (progn (setf y (incf i)) 2))
215 216 217 218 219 220 221
     i x y))
  nil 2 1 2)

(deftest =.order.2
  (let ((i 0) x y z)
    (values
     (= (progn (setf x (incf i)) 1)
sds's avatar
sds committed
222 223
        (progn (setf y (incf i)) 2)
        (progn (setf z (incf i)) 3))
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
     i x y z))
  nil 3 1 2 3)

(deftest =.order.3
  (let ((i 0) u v w x y z)
    (values
     (=
      (progn (setf u (incf i)) 1)
      (progn (setf v (incf i)) 2)
      (progn (setf w (incf i)) 3)
      (progn (setf x (incf i)) 4)
      (progn (setf y (incf i)) 5)
      (progn (setf z (incf i)) 6))
     i u v w x y z))
  nil 6 1 2 3 4 5 6)

pfdietz's avatar
pfdietz committed
240 241 242 243
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(deftest /=.1
  (loop for x in *numbers*
sds's avatar
sds committed
244 245
        unless (/= x)
        collect x)
pfdietz's avatar
pfdietz committed
246 247 248 249
  nil)

(deftest /=.2
  (loop for x in *numbers*
sds's avatar
sds committed
250 251
        when (/= x x)
        collect x)
pfdietz's avatar
pfdietz committed
252 253 254 255
  nil)

(deftest /=.3
  (loop for x in *numbers*
sds's avatar
sds committed
256 257
        when (/= x x x)
        collect x)
pfdietz's avatar
pfdietz committed
258 259 260 261 262 263 264 265 266 267 268 269
  nil)

(deftest /=.4
  (/=.4-fn)
  nil)

(deftest /=.4a
  (/=.4a-fn)
  nil)

(deftest /=.5
  (loop for i from 1 to 10000
sds's avatar
sds committed
270 271
        for i2 = (1+ i)
        always (and (/= i i2) (/= i2 i)))
pfdietz's avatar
pfdietz committed
272 273 274 275
  t)

(deftest /=.6
  (loop for i from 5 to 10000 by 17
sds's avatar
sds committed
276 277 278 279 280 281 282 283 284
        for j from 2 to i by 19
        for r = (/ i j)
        when (or (not (/= r (1+ r)))
                 (not (/= r 0))
                 (not (/= r (- r)))
                 (/= r r))
        collect r)
  nil)

pfdietz's avatar
pfdietz committed
285 286
(deftest /=.7
  (let ((args (list 17))
sds's avatar
sds committed
287
        (args2 nil))
288
    (loop for i from 2 to (min 256 (1- call-arguments-limit))
sds's avatar
sds committed
289 290 291 292
          do (push 17 args)
          do (push i args2)
          always (and (not (apply #'/= args))
                      (apply #'/= args2))))
pfdietz's avatar
pfdietz committed
293 294 295
  t)

(deftest /=.8
296
  (loop for i from 2 to (min 256 (1- call-arguments-limit))
sds's avatar
sds committed
297 298 299 300
        for args = (append (make-list (1- i) :initial-element 7)
                           (list 7))
        when (apply #'/= args)
        collect args)
pfdietz's avatar
pfdietz committed
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
  nil)


(deftest /=.9
  (/= 0 0.0)
  nil)

(deftest /=.10
  (/= 0 #c(0 0))
  nil)

(deftest /=.11
  (/= 1 #c(1.0 0.0))
  nil)

(deftest /=.12
  (/= -0.0 0.0)
  nil)

(deftest /=.13
  (let ((nums '(0 0.0s0 0.0f0 0.0d0 0.0l0
sds's avatar
sds committed
322 323
                  #c(0.0s0 0.0s0) #c(0.0f0 0.0f0)
                  #c(0.0d0 0.0d0) #c(0.0l0 0.0l0))))
pfdietz's avatar
pfdietz committed
324
    (loop for x in nums
sds's avatar
sds committed
325 326 327 328
          append
          (loop for y in nums
                when (/= x y)
                collect (list x y))))
pfdietz's avatar
pfdietz committed
329 330 331 332
  nil)

(deftest /=.14
  (let ((nums '(17 17.0s0 17.0f0 17.0d0 17.0l0
sds's avatar
sds committed
333 334
                   #c(17.0s0 0.0s0) #c(17.0f0 0.0f0)
                   #c(17.0d0 0.0d0) #c(17.0l0 0.0l0))))
pfdietz's avatar
pfdietz committed
335
    (loop for x in nums
sds's avatar
sds committed
336 337 338 339
          append
          (loop for y in nums
                when (/= x y)
                collect (list x y))))
pfdietz's avatar
pfdietz committed
340 341 342 343
  nil)

(deftest /=.15
  (let ((nums '(-17 -17.0s0 -17.0f0 -17.0d0 -17.0l0
sds's avatar
sds committed
344 345
                    #c(-17.0s0 0.0s0) #c(-17.0f0 0.0f0)
                    #c(-17.0d0 0.0d0) #c(-17.0l0 0.0l0))))
pfdietz's avatar
pfdietz committed
346
    (loop for x in nums
sds's avatar
sds committed
347 348 349 350
          append
          (loop for y in nums
                when (/= x y)
                collect (list x y))))
pfdietz's avatar
pfdietz committed
351 352
  nil)

353 354
(deftest /=.17
  (loop for x in '(1.0s0 1.0f0 1.0d0 1.0l0)
sds's avatar
sds committed
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
        for eps in (list short-float-epsilon single-float-epsilon
                         double-float-epsilon long-float-epsilon)
        for exp = (nth-value 1 (decode-float eps))
        for radix = (float-radix eps)
        when (< (* (log radix 2) exp) 1000)
        nconc
        (let* ((rat (rational eps))
               (xrat (rational x)))
          (loop for i from 2 to 100
                for rat/i = (/ rat i)
                for xrat+rat/i = (+ xrat rat/i)
                nconc
                (if (/= x xrat+rat/i)
                    nil
                    (list (list x i  xrat+rat/i))))))
370 371 372 373
  nil)

(deftest /=.18
  (loop for x in '(1.0s0 1.0f0 1.0d0 1.0l0)
sds's avatar
sds committed
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
        for eps in (list short-float-negative-epsilon single-float-negative-epsilon
                         double-float-negative-epsilon long-float-negative-epsilon)
        for exp = (nth-value 1 (decode-float eps))
        for radix = (float-radix eps)
        when (< (* (log radix 2) exp) 1000)
        nconc
        (let* ((rat (rational eps))
               (xrat (rational x)))
          (loop for i from 2 to 100
                for rat/i = (/ rat i)
                for xrat-rat/i = (- xrat rat/i)
                nconc
                (if (/= x xrat-rat/i)
                    nil
                    (list (list x i xrat-rat/i))))))
389 390
  nil)

391 392 393
(deftest /=.19
  (let ((bound (expt 10 1000)))
    (loop for x in (list most-positive-short-float most-positive-single-float
sds's avatar
sds committed
394 395 396 397 398
                         most-positive-double-float most-positive-long-float)
          for d = (and (<= x bound) (truncate x))
          unless (or (null d) (and (/= (* 3/2 d) x)
                                   (/= x (* 5/4 d))))
          collect (list x d (* 3/2 d) (* 5/4 d))))
399
  nil)
400

401 402 403 404
(deftest /=.order.1
  (let ((i 0) x y)
    (values
     (notnot (/= (progn (setf x (incf i)) 1)
sds's avatar
sds committed
405
                 (progn (setf y (incf i)) 2)))
406 407 408 409 410 411 412
     i x y))
  t 2 1 2)

(deftest /=.order.2
  (let ((i 0) x y z)
    (values
     (notnot (/= (progn (setf x (incf i)) 1)
sds's avatar
sds committed
413 414
                 (progn (setf y (incf i)) 2)
                 (progn (setf z (incf i)) 3)))
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
     i x y z))
  t 3 1 2 3)

(deftest /=.order.3
  (let ((i 0) u v w x y z)
    (values
     (notnot
      (/=
       (progn (setf u (incf i)) 1)
       (progn (setf v (incf i)) 2)
       (progn (setf w (incf i)) 3)
       (progn (setf x (incf i)) 4)
       (progn (setf y (incf i)) 5)
       (progn (setf z (incf i)) 6)))
     i u v w x y z))
  t 6 1 2 3 4 5 6)

pfdietz's avatar
pfdietz committed
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(deftest <.1
  (let ((a 0) (b 1)) (notnot-mv (< a b)))
  t)

(deftest <.2
  (let ((a 0) (b 0)) (notnot-mv (< a b)))
  nil)

(deftest <.3
  (let ((a 1) (b 0)) (notnot-mv (< a b)))
  nil)

(defparameter *number-less-tests*
  (let* ((n (- most-positive-fixnum most-negative-fixnum))
sds's avatar
sds committed
448
         (n2 (* 1000 n)))
pfdietz's avatar
pfdietz committed
449 450
    (nconc
     (loop for i = (+ (random n) most-negative-fixnum)
sds's avatar
sds committed
451 452 453 454
           for i2 = (+ i (random most-positive-fixnum))
           repeat 1000
           nconc
           (list (list i i2 t) (list i2 i nil)))
pfdietz's avatar
pfdietz committed
455
     (loop for i = (random n2)
sds's avatar
sds committed
456 457 458 459
           for i2 = (+ (random n2) i)
           repeat 1000
           nconc
           (list (list i i2 t) (list i2 i nil)))
pfdietz's avatar
pfdietz committed
460
     (loop for x in *universe*
sds's avatar
sds committed
461 462 463
           when (integerp x)
           nconc (list (list x (1+ x) t)
                       (list (1+ x) x nil)))
pfdietz's avatar
pfdietz committed
464
     (loop for x in *universe*
sds's avatar
sds committed
465 466
           when (realp x)
           collect (list x x nil))
pfdietz's avatar
pfdietz committed
467 468

     (loop for x in *universe*
sds's avatar
sds committed
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
           when (and (realp x) (>= x 1))
           nconc
           (loop for epsilon in (list short-float-epsilon
                                      single-float-epsilon
                                      double-float-epsilon
                                      long-float-epsilon)
                 for bound in (list most-positive-short-float
                                    most-positive-single-float
                                    most-positive-double-float
                                    most-positive-long-float)
                 for lower-bound in (list most-negative-short-float
                                    most-negative-single-float
                                    most-negative-double-float
                                    most-negative-long-float)
                 for one in '(1.0s0 1.0f0 1.0d0 1.0l0)
                 when (and (<= (abs (float-exponent lower-bound)) 500)
                           (<= (abs (float-exponent x)) 500)
                           (<= (abs (float-exponent bound)) 500))
                 when (<= (rational lower-bound)
                          (rational x)
                          (rational bound))
                 nconc
                 (let* ((y (float x one))
                        (z (* y (- one (* 2 epsilon)))))
                   (list (list y z nil)
                         (list z y t)))))

pfdietz's avatar
pfdietz committed
496
     (loop for x in *universe*
sds's avatar
sds committed
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
           when (and (realp x) (<= x -1))
           nconc
           (loop for epsilon in (list short-float-epsilon
                                      single-float-epsilon
                                      double-float-epsilon
                                      long-float-epsilon)
                 for bound in (list most-negative-short-float
                                    most-negative-single-float
                                    most-negative-double-float
                                    most-negative-long-float)
                 for upper-bound in (list most-positive-short-float
                                    most-positive-single-float
                                    most-positive-double-float
                                    most-positive-long-float)
                 for one in '(1.0s0 1.0f0 1.0d0 1.0l0)
                 when (and (<= (abs (float-exponent bound)) 500)
                           (<= (abs (float-exponent x)) 500)
                           (<= (abs (float-exponent upper-bound)) 500))
                 when (<= (rational bound)
                          (rational x)
                          (rational upper-bound))
                 nconc
                 (let* ((y (float x one)))
                   (let ((z (* y (- one (* 2 epsilon)))))
                     (list (list y z t)
                           (list z y nil))))))

pfdietz's avatar
pfdietz committed
524
     (loop for x in *universe*
sds's avatar
sds committed
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
           when (and (realp x) (< -1 x 1))
           nconc
           (loop for epsilon in (list short-float-epsilon
                                      single-float-epsilon
                                      double-float-epsilon
                                      long-float-epsilon)
                 for lower-bound in (list most-negative-short-float
                                    most-negative-single-float
                                    most-negative-double-float
                                    most-negative-long-float)
                 for upper-bound in (list most-positive-short-float
                                    most-positive-single-float
                                    most-positive-double-float
                                    most-positive-long-float)
                 for one in '(1.0s0 1.0f0 1.0d0 1.0l0)
                 when (and (<= (abs (float-exponent lower-bound)) 500)
                           (<= (abs (float-exponent x)) 500)
                           (<= (abs (float-exponent upper-bound)) 500))
                 when (<= (rational lower-bound)
                          (rational x)
                          (rational upper-bound))
                 nconc
                 (handler-case
                  (let* ((y (float x one))
                         (z1 (+ y epsilon))
                         (z2 (- y epsilon)))
                    (list (list y z1 t)
                          (list z1 y nil)
                          (list y z2 nil)
                          (list z2 y t)))
                  (arithmetic-error () nil)))
           ))))
557

pfdietz's avatar
pfdietz committed
558 559
(deftest <.4
  (loop for (x y result . rest) in *number-less-tests*
sds's avatar
sds committed
560 561
        unless (if (< x y) result (not result))
        collect (list* x y result rest))
pfdietz's avatar
pfdietz committed
562 563 564 565
  nil)

(deftest <.5
  (loop for x in *universe*
sds's avatar
sds committed
566 567 568
        when (and (typep x 'real)
                  (not (< x)))
        collect x)
pfdietz's avatar
pfdietz committed
569 570 571 572
  nil)

(deftest <.6
  (let ((args (list 17))
sds's avatar
sds committed
573
        (args2 nil))
574
    (loop for i from 2 to (min 256 (1- call-arguments-limit))
sds's avatar
sds committed
575 576 577 578 579
          do (push 17 args)
          do (push (- i) args2)
          unless (and (not (apply #'< args))
                      (apply #'< args2))
          collect (list args args2)))
pfdietz's avatar
pfdietz committed
580 581 582
  nil)

(deftest <.7
583
  (let* ((len (min 256 (1- call-arguments-limit)))
sds's avatar
sds committed
584
         (args-proto (loop for i from 1 to len collect i)))
pfdietz's avatar
pfdietz committed
585
    (loop for i from 1 below len
sds's avatar
sds committed
586 587 588
          for args = (copy-list args-proto)
          do (setf (elt args i) 0)
          never (apply #'< args)))
pfdietz's avatar
pfdietz committed
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
  t)

;;; Check that < is antisymmetric
(deftest <.8
  (<.8-fn)
  nil)

;;;  < is symmetric with >
(deftest <.9
  (<.9-fn)
  nil)

;;;  < is negation of >=
(deftest <.10
  (<.10-fn)
  nil)

606 607
(deftest <.11
  (loop for x in '(0.0s0 0.0f0 0.0d0 0.0l0)
sds's avatar
sds committed
608 609
        never (or (< (- x) x)
                  (< x (- x))))
610 611
  t)

612 613
(deftest <.17
  (loop for x in '(1.0s0 1.0f0 1.0d0 1.0l0)
sds's avatar
sds committed
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
        for eps in (list short-float-epsilon single-float-epsilon
                         double-float-epsilon long-float-epsilon)
        for exp = (nth-value 1 (decode-float eps))
        for radix = (float-radix eps)
        when (< (* (log radix 2) exp) 1000)
        nconc
        (let* ((rat (rational eps))
               (xrat (rational x)))
          (loop for i from 2 to 100
                for rat/i = (/ rat i)
                for xrat+rat/i = (+ xrat rat/i)
                nconc
                (if (< x xrat+rat/i)
                    nil
                    (list (list x i  xrat+rat/i))))))
629 630 631 632
  nil)

(deftest <.18
  (loop for x in '(1.0s0 1.0f0 1.0d0 1.0l0)
sds's avatar
sds committed
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
        for eps in (list short-float-negative-epsilon single-float-negative-epsilon
                         double-float-negative-epsilon long-float-negative-epsilon)
        for exp = (nth-value 1 (decode-float eps))
        for radix = (float-radix eps)
        when (< (* (log radix 2) exp) 1000)
        nconc
        (let* ((rat (rational eps))
               (xrat (rational x)))
          (loop for i from 2 to 100
                for rat/i = (/ rat i)
                for xrat-rat/i = (- xrat rat/i)
                nconc
                (if (< x xrat-rat/i)
                    (list (list x i xrat-rat/i))
                  nil))))
648 649
  nil)

650 651 652
(deftest <.19
  (let ((bound (expt 10 1000)))
    (loop for x in (list most-positive-short-float most-positive-single-float
sds's avatar
sds committed
653 654 655 656 657
                         most-positive-double-float most-positive-long-float)
          for d = (and (<= x bound) (truncate x))
          unless (or (null d) (and (< x (* 3/2 d))
                                   (not (< (* 17/16 d) x))))
          collect (list x d (* 3/2 d) (* 17/16 d))))
658
  nil)
659

660 661 662 663
(deftest <.order.1
  (let ((i 0) x y)
    (values
     (notnot (< (progn (setf x (incf i)) 1)
sds's avatar
sds committed
664
                 (progn (setf y (incf i)) 2)))
665 666 667 668 669 670 671
     i x y))
  t 2 1 2)

(deftest <.order.2
  (let ((i 0) x y z)
    (values
     (notnot (< (progn (setf x (incf i)) 1)
sds's avatar
sds committed
672 673
                 (progn (setf y (incf i)) 2)
                 (progn (setf z (incf i)) 3)))
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
     i x y z))
  t 3 1 2 3)

(deftest <.order.3
  (let ((i 0) u v w x y z)
    (values
     (notnot
      (<
       (progn (setf u (incf i)) 1)
       (progn (setf v (incf i)) 2)
       (progn (setf w (incf i)) 3)
       (progn (setf x (incf i)) 4)
       (progn (setf y (incf i)) 5)
       (progn (setf z (incf i)) 6)))
     i u v w x y z))
  t 6 1 2 3 4 5 6)

pfdietz's avatar
pfdietz committed
691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(deftest <=.1
  (let ((a 0) (b 1)) (notnot-mv (<= a b)))
  t)

(deftest <=.2
  (let ((a 0) (b 0)) (notnot-mv (<= a b)))
  t)

(deftest <=.3
  (let ((a 1) (b 0)) (notnot-mv (<= a b)))
  nil)

(defparameter *number-less-or-equal-tests*
  (let* ((n (- most-positive-fixnum most-negative-fixnum))
sds's avatar
sds committed
707
         (n2 (* 1000 n)))
pfdietz's avatar
pfdietz committed
708 709
    (nconc
     (loop for i = (+ (random n) most-negative-fixnum)
sds's avatar
sds committed
710 711 712 713
           for i2 = (+ i (random most-positive-fixnum))
           repeat 1000
           nconc
           (list (list i i2 t) (list i2 i nil)))
pfdietz's avatar
pfdietz committed
714
     (loop for i = (random n2)
sds's avatar
sds committed
715 716 717 718
           for i2 = (+ (random n2) i)
           repeat 1000
           nconc
           (list (list i i2 t) (list i2 i nil)))
pfdietz's avatar
pfdietz committed
719
     (loop for x in *universe*
sds's avatar
sds committed
720 721 722
           when (integerp x)
           nconc (list (list x (1+ x) t)
                       (list (1+ x) x nil)))
pfdietz's avatar
pfdietz committed
723
     (loop for x in *universe*
sds's avatar
sds committed
724 725
           when (realp x)
           collect (list x x t))
pfdietz's avatar
pfdietz committed
726 727

     (loop for x in *universe*
sds's avatar
sds committed
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
           when (and (realp x) (>= x 1))
           nconc
           (loop for epsilon in (list short-float-epsilon
                                      single-float-epsilon
                                      double-float-epsilon
                                      long-float-epsilon)
                 for bound in (list most-positive-short-float
                                    most-positive-single-float
                                    most-positive-double-float
                                    most-positive-long-float)
                 for lower-bound in (list most-negative-short-float
                                    most-negative-single-float
                                    most-negative-double-float
                                    most-negative-long-float)
                 for one in '(1.0s0 1.0f0 1.0d0 1.0l0)
                 when (and (<= (abs (float-exponent lower-bound)) 500)
                           (<= (abs (float-exponent x)) 500)
                           (<= (abs (float-exponent bound)) 500))
                 when (<= (rational lower-bound)
                          (rational x)
                          (rational bound))
                 nconc
                 (let* ((y (float x one))
                        (z (* y (- one (* 2 epsilon)))))
                   (list (list y z nil)
                         (list z y t)))))
pfdietz's avatar
pfdietz committed
754
     (loop for x in *universe*
sds's avatar
sds committed
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
           when (and (realp x) (<= x -1))
           nconc
           (loop for epsilon in (list short-float-epsilon
                                      single-float-epsilon
                                      double-float-epsilon
                                      long-float-epsilon)
                 for bound in (list most-negative-short-float
                                    most-negative-single-float
                                    most-negative-double-float
                                    most-negative-long-float)
                 for upper-bound in (list most-positive-short-float
                                    most-positive-single-float
                                    most-positive-double-float
                                    most-positive-long-float)
                 for one in '(1.0s0 1.0f0 1.0d0 1.0l0)
                 when (and (<= (abs (float-exponent bound)) 500)
                           (<= (abs (float-exponent x)) 500)
                           (<= (abs (float-exponent upper-bound)) 500))
                 when (<= (rational bound)
                          (rational x)
                          (rational upper-bound))
                 nconc
                 (let* ((y (float x one))
                        (z (* y (- one (* 2 epsilon)))))
                   (list (list y z t)
                         (list z y nil)))))
pfdietz's avatar
pfdietz committed
781
     (loop for x in *universe*
sds's avatar
sds committed
782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812
           when (and (realp x) (< -1 x 1))
           nconc
           (loop for epsilon in (list short-float-epsilon
                                      single-float-epsilon
                                      double-float-epsilon
                                      long-float-epsilon)
                 for lower-bound in (list most-negative-short-float
                                    most-negative-single-float
                                    most-negative-double-float
                                    most-negative-long-float)
                 for upper-bound in (list most-positive-short-float
                                    most-positive-single-float
                                    most-positive-double-float
                                    most-positive-long-float)
                 for one in '(1.0s0 1.0f0 1.0d0 1.0l0)
                 when (and (<= (abs (float-exponent lower-bound)) 500)
                           (<= (abs (float-exponent x)) 500)
                           (<= (abs (float-exponent upper-bound)) 500))
                 when (<= (rational lower-bound)
                          (rational x)
                          (rational upper-bound))
                 nconc
                 (handler-case
                  (let* ((y (float x one))
                         (z1 (+ y epsilon))
                         (z2 (- y epsilon)))
                    (list (list y z1 t)
                          (list z1 y nil)
                          (list y z2 nil)
                          (list z2 y t)))
                  (floating-point-underflow () nil))))
pfdietz's avatar
pfdietz committed
813 814 815 816
     )))

(deftest <=.4
  (loop for (x y result . rest) in *number-less-or-equal-tests*
sds's avatar
sds committed
817 818
        unless (if (<= x y) result (not result))
        collect (list* x y result rest))
pfdietz's avatar
pfdietz committed
819 820 821 822
  nil)

(deftest <=.5
  (loop for x in *universe*
sds's avatar
sds committed
823 824 825
        when (and (typep x 'real)
                  (not (<= x)))
        collect x)
pfdietz's avatar
pfdietz committed
826 827 828 829
  nil)

(deftest <=.6
  (let ((args (list 17))
sds's avatar
sds committed
830 831
        (args2 nil)
        (args3 (list 0)))
832
    (loop for i from 2 to (min 256 (1- call-arguments-limit))
sds's avatar
sds committed
833 834 835 836 837 838 839
          do (push 17 args)
          do (push (- i) args2)
          do (push i args3)
          unless (and (apply #'<= args)
                      (apply #'<= args2)
                      (not (apply #'<= args3)))
          collect (list args args2 args3)))
pfdietz's avatar
pfdietz committed
840 841 842
  nil)

(deftest <=.7
843
  (let* ((len (min 256 (1- call-arguments-limit)))
sds's avatar
sds committed
844
         (args-proto (loop for i from 1 to len collect i)))
pfdietz's avatar
pfdietz committed
845
    (loop for i from 1 below len
sds's avatar
sds committed
846 847 848
          for args = (copy-list args-proto)
          do (setf (elt args i) 0)
          never (apply #'<= args)))
pfdietz's avatar
pfdietz committed
849 850 851 852 853 854 855 856 857 858 859 860
  t)

;;; Check that <= is symmetric with >=
(deftest <=.8
  (<=.8-fn)
  nil)

;;; Check that <= is equivalent to (or < =)
(deftest <=.9
  (<=.9-fn)
  nil)

861 862
(deftest <=.10
  (loop for x in '(0.0s0 0.0f0 0.0d0 0.0l0)
sds's avatar
sds committed
863 864
        always (and (<= (- x) x)
                    (<= x (- x))))
865 866
  t)

867 868
(deftest <=.17
  (loop for x in '(1.0s0 1.0f0 1.0d0 1.0l0)
sds's avatar
sds committed
869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
        for eps in (list short-float-epsilon single-float-epsilon
                         double-float-epsilon long-float-epsilon)
        for exp = (nth-value 1 (decode-float eps))
        for radix = (float-radix eps)
        when (< (* (log radix 2) exp) 1000)
        nconc
        (let* ((rat (rational eps))
               (xrat (rational x)))
          (loop for i from 2 to 100
                for rat/i = (/ rat i)
                for xrat+rat/i = (+ xrat rat/i)
                nconc
                (if (<= x xrat+rat/i)
                    nil
                    (list (list x i  xrat+rat/i))))))
884 885 886 887
  nil)

(deftest <=.18
  (loop for x in '(1.0s0 1.0f0 1.0d0 1.0l0)
sds's avatar
sds committed
888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
        for eps in (list short-float-negative-epsilon single-float-negative-epsilon
                         double-float-negative-epsilon long-float-negative-epsilon)
        for exp = (nth-value 1 (decode-float eps))
        for radix = (float-radix eps)
        when (< (* (log radix 2) exp) 1000)
        nconc
        (let* ((rat (rational eps))
               (xrat (rational x)))
          (loop for i from 2 to 100
                for rat/i = (/ rat i)
                for xrat-rat/i = (- xrat rat/i)
                nconc
                (if (<= x xrat-rat/i)
                    (list (list x i xrat-rat/i))
                  nil))))
903 904
  nil)

905 906 907
(deftest <=.19
  (let ((bound (expt 10 1000)))
    (loop for x in (list most-positive-short-float most-positive-single-float
sds's avatar
sds committed
908 909 910 911 912
                         most-positive-double-float most-positive-long-float)
          for d = (and (<= x bound) (truncate x))
          unless (or (null d) (and (<= x (* 3/2 d))
                                   (not (<= (* 5/4 d) x))))
          collect (list x d (* 3/2 d) (* 5/4 d))))
913 914
  nil)

915 916 917 918
(deftest <=.order.1
  (let ((i 0) x y)
    (values
     (notnot (<= (progn (setf x (incf i)) 1)
sds's avatar
sds committed
919
                 (progn (setf y (incf i)) 2)))
920 921 922 923 924 925 926
     i x y))
  t 2 1 2)

(deftest <=.order.2
  (let ((i 0) x y z)
    (values
     (notnot (<= (progn (setf x (incf i)) 1)
sds's avatar
sds committed
927 928
                 (progn (setf y (incf i)) 2)
                 (progn (setf z (incf i)) 3)))
929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946
     i x y z))
  t 3 1 2 3)

(deftest <=.order.3
  (let ((i 0) u v w x y z)
    (values
     (notnot
      (<=
       (progn (setf u (incf i)) 1)
       (progn (setf v (incf i)) 2)
       (progn (setf w (incf i)) 3)
       (progn (setf x (incf i)) 4)
       (progn (setf y (incf i)) 5)
       (progn (setf z (incf i)) 6)))
     i u v w x y z))
  t 6 1 2 3 4 5 6)


pfdietz's avatar
pfdietz committed
947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(deftest >.1
  (let ((a 0) (b 1)) (notnot-mv (> a b)))
  nil)

(deftest >.2
  (let ((a 0) (b 0)) (notnot-mv (> a b)))
  nil)

(deftest >.3
  (let ((a 1) (b 0)) (notnot-mv (> a b)))
  t)

(deftest >.4
  (loop for (x y result . rest) in *number-less-tests*
sds's avatar
sds committed
963 964
        unless (if (> y x) result (not result))
        collect (list* y x result rest))
pfdietz's avatar
pfdietz committed
965 966 967 968
  nil)

(deftest >.5
  (loop for x in *universe*
sds's avatar
sds committed
969 970 971
        when (and (typep x 'real)
                  (not (> x)))
        collect x)
pfdietz's avatar
pfdietz committed
972 973 974 975
  nil)

(deftest >.6
  (let ((args (list 17))
sds's avatar
sds committed
976
        (args2 nil))
977
    (loop for i from 2 to (min 256 (1- call-arguments-limit))
sds's avatar
sds committed
978 979 980 981 982
          do (push 17 args)
          do (push i args2)
          unless (and (not (apply #'> args))
                      (apply #'> args2))
          collect (list args args2)))
pfdietz's avatar
pfdietz committed
983 984 985
  nil)

(deftest >.7
986
  (let* ((len (min 256 (1- call-arguments-limit)))
sds's avatar
sds committed
987
         (args-proto (loop for i from 1 to len collect i)))
pfdietz's avatar
pfdietz committed
988
    (loop for i from 1 below len
sds's avatar
sds committed
989 990 991
          for args = (copy-list args-proto)
          do (setf (elt args i) 0)
          never (apply #'> args)))
pfdietz's avatar
pfdietz committed
992 993 994 995 996 997 998
  t)

;;; > is negation of <=
(deftest >.8
  (>.8-fn)
  nil)

999 1000
(deftest >.9
  (loop for x in '(0.0s0 0.0f0 0.0d0 0.0l0)
sds's avatar
sds committed
1001 1002
        never (or (> (- x) x)
                  (> x (- x))))
1003 1004
  t)

1005 1006
(deftest >.17
  (loop for x in '(1.0s0 1.0f0 1.0d0 1.0l0)
sds's avatar
sds committed
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
        for eps in (list short-float-epsilon single-float-epsilon
                         double-float-epsilon long-float-epsilon)
        for exp = (nth-value 1 (decode-float eps))
        for radix = (float-radix eps)
        when (< (* (log radix 2) exp) 1000)
        nconc
        (let* ((rat (rational eps))
               (xrat (rational x)))
          (loop for i from 2 to 100
                for rat/i = (/ rat i)
                for xrat+rat/i = (+ xrat rat/i)
                nconc
                (if (> x xrat+rat/i)
                    (list (list x i  xrat+rat/i))
                  nil))))
1022 1023 1024 1025
  nil)

(deftest >.18
  (loop for x in '(1.0s0 1.0f0 1.0d0 1.0l0)
sds's avatar
sds committed
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
        for eps in (list short-float-negative-epsilon single-float-negative-epsilon
                         double-float-negative-epsilon long-float-negative-epsilon)
        for exp = (nth-value 1 (decode-float eps))
        for radix = (float-radix eps)
        when (< (* (log radix 2) exp) 1000)
        nconc
        (let* ((rat (rational eps))
               (xrat (rational x)))
          (loop for i from 2 to 100
                for rat/i = (/ rat i)
                for xrat-rat/i = (- xrat rat/i)
                nconc
                (if (> x xrat-rat/i)
                    nil
                    (list (list x i  xrat-rat/i))))))
1041 1042
  nil)

1043 1044 1045
(deftest >.19
  (let ((bound (expt 10 1000)))
    (loop for x in (list most-positive-short-float most-positive-single-float
sds's avatar
sds committed
1046 1047 1048 1049 1050
                         most-positive-double-float most-positive-long-float)
          for d = (and (<= x bound) (truncate x))
          unless (or (null d) (and (> (* 3/2 d) x)
                                   (not (> x (* 17/16 d)))))
          collect (list x d (* 3/2 d) (* 17/16 d))))
1051 1052
  nil)

1053 1054 1055 1056
(deftest >.order.1
  (let ((i 0) x y)
    (values
     (notnot (> (progn (setf x (incf i)) 2)
sds's avatar
sds committed
1057
                (progn (setf y (incf i)) 1)))
1058 1059 1060 1061 1062 1063 1064
     i x y))
  t 2 1 2)

(deftest >.order.2
  (let ((i 0) x y z)
    (values
     (notnot (> (progn (setf x (incf i)) 3)
sds's avatar
sds committed
1065 1066
                 (progn (setf y (incf i)) 2)
                 (progn (setf z (incf i)) 1)))
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
     i x y z))
  t 3 1 2 3)

(deftest >.order.3
  (let ((i 0) u v w x y z)
    (values
     (notnot
      (>
       (progn (setf u (incf i)) 6)
       (progn (setf v (incf i)) 5)
       (progn (setf w (incf i)) 4)
       (progn (setf x (incf i)) 3)
       (progn (setf y (incf i)) 2)
       (progn (setf z (incf i)) 1)))
     i u v w x y z))
  t 6 1 2 3 4 5 6)

pfdietz's avatar
pfdietz committed
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(deftest >=.1
  (let ((a 0) (b 1)) (notnot-mv (>= a b)))
  nil)

(deftest >=.2
  (let ((a 0) (b 0)) (notnot-mv (>= a b)))
  t)

(deftest >=.3
  (let ((a 1) (b 0)) (notnot-mv (>= a b)))
  t)

(deftest >=.4
  (loop for (x y result . rest) in *number-less-or-equal-tests*
sds's avatar
sds committed
1100 1101
        unless (if (>= y x) result (not result))
        collect (list* y x result rest))
pfdietz's avatar
pfdietz committed
1102 1103 1104 1105
  nil)

(deftest >=.5
  (loop for x in *universe*
sds's avatar
sds committed
1106 1107 1108
        when (and (typep x 'real)
                  (not (>= x)))
        collect x)
pfdietz's avatar
pfdietz committed
1109 1110 1111 1112
  nil)

(deftest >=.6
  (let ((args (list 17))
sds's avatar
sds committed
1113 1114
        (args2 (list 0))
        (args3 nil))
1115
    (loop for i from 2 to (min 256 (1- call-arguments-limit))
sds's avatar
sds committed
1116 1117 1118 1119 1120 1121 1122
          do (push 17 args)
          do (push (- i) args2)
          do (push i args3)
          unless (and (apply #'>= args)
                      (not (apply #'>= args2))
                      (apply #'>= args3))
          collect (list args args2 args3)))
pfdietz's avatar
pfdietz committed
1123 1124 1125
  nil)

(deftest >=.7
1126
  (let* ((len (min 256 (1- call-arguments-limit)))
sds's avatar
sds committed
1127
         (args-proto (loop for i from 1 to len collect i)))
pfdietz's avatar
pfdietz committed
1128
    (loop for i from 1 below len
sds's avatar
sds committed
1129 1130 1131
          for args = (copy-list args-proto)
          do (setf (elt args i) 0)
          never (apply #'>= args)))
pfdietz's avatar
pfdietz committed
1132 1133 1134 1135 1136 1137 1138
  t)

;;; Check that >= is equivalent to (or > =)
(deftest >=.8
  (>=.8-fn)
  nil)

1139 1140
(deftest >=.9
  (loop for x in '(0.0s0 0.0f0 0.0d0 0.0l0)
sds's avatar
sds committed
1141 1142
        always (and (>= (- x) x)
                    (>= x (- x))))
1143 1144
  t)

1145 1146 1147

(deftest >=.17
  (loop for x in '(1.0s0 1.0f0 1.0d0 1.0l0)
sds's avatar
sds committed
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162
        for eps in (list short-float-epsilon single-float-epsilon
                         double-float-epsilon long-float-epsilon)
        for exp = (nth-value 1 (decode-float eps))
        for radix = (float-radix eps)
        when (< (* (log radix 2) exp) 1000)
        nconc
        (let* ((rat (rational eps))
               (xrat (rational x)))
          (loop for i from 2 to 100
                for rat/i = (/ rat i)
                for xrat+rat/i = (+ xrat rat/i)
                nconc
                (if (>= x xrat+rat/i)
                    (list (list x i  xrat+rat/i))
                  nil))))
1163 1164 1165 1166
  nil)

(deftest >=.18
  (loop for x in '(1.0s0 1.0f0 1.0d0 1.0l0)
sds's avatar
sds committed
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181
        for eps in (list short-float-negative-epsilon single-float-negative-epsilon
                         double-float-negative-epsilon long-float-negative-epsilon)
        for exp = (nth-value 1 (decode-float eps))
        for radix = (float-radix eps)
        when (< (* (log radix 2) exp) 1000)
        nconc
        (let* ((rat (rational eps))
               (xrat (rational x)))
          (loop for i from 2 to 100
                for rat/i = (/ rat i)
                for xrat-rat/i = (- xrat rat/i)
                nconc
                (if (>= x xrat-rat/i)
                    nil
                    (list (list x i xrat-rat/i))))))
1182 1183
  nil)

1184 1185 1186
(deftest >=.19
  (let ((bound (expt 10 1000)))
    (loop for x in (list most-positive-short-float most-positive-single-float
sds's avatar
sds committed
1187 1188 1189 1190 1191
                         most-positive-double-float most-positive-long-float)
          for d = (and (<= x bound) (truncate x))
          unless (or (null d) (and (>= (* 3/2 d) x)
                                   (not (>=  x(* 17/16 d)))))
          collect (list x d (* 3/2 d) (* 17/16 d))))
1192 1193
  nil)

1194 1195 1196 1197
(deftest >=.order.1
  (let ((i 0) x y)
    (values
     (notnot (>= (progn (setf x (incf i)) 2)
sds's avatar
sds committed
1198
                (progn (setf y (incf i)) 1)))
1199 1200 1201 1202 1203 1204 1205
     i x y))
  t 2 1 2)

(deftest >=.order.2
  (let ((i 0) x y z)
    (values
     (notnot (>= (progn (setf x (incf i)) 3)
sds's avatar
sds committed
1206 1207
                 (progn (setf y (incf i)) 2)
                 (progn (setf z (incf i)) 1)))
1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224
     i x y z))
  t 3 1 2 3)

(deftest >=.order.3
  (let ((i 0) u v w x y z)
    (values
     (notnot
      (>=
       (progn (setf u (incf i)) 6)
       (progn (setf v (incf i)) 5)
       (progn (setf w (incf i)) 4)
       (progn (setf x (incf i)) 3)
       (progn (setf y (incf i)) 2)
       (progn (setf z (incf i)) 1)))
     i u v w x y z))
  t 6 1 2 3 4 5 6)

pfdietz's avatar
pfdietz committed
1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; Comparison of rationals

(deftest compare-rationals.1
  (compare-random-rationals 60000 30000 10000)
  nil)

(deftest compare-rationals.2
  (compare-random-rationals 600000 300000 10000)
  nil)

(deftest compare-rationals.3
  (compare-random-rationals 6000000 3000000 10000)
  nil)

(deftest compare-rationals.4
  (compare-random-rationals 6000000000 3000000000 10000)
  nil)
1244 1245 1246 1247 1248

;;;; Comparison of bignums with floats

(deftest bignum.float.compare.1a
  (loop for x in *floats*
sds's avatar
sds committed
1249 1250 1251 1252 1253 1254 1255 1256 1257
        when (or (zerop x)
                 (< (abs (log (abs x))) 10000))
        nconc
        (loop for r = (1+ (random (ash 1 (random 32))))
              repeat 200
              nconc
              (let ((i (+ r (ceiling (rational x)))))
                (unless (< x i)
                  (list (list r x i))))))
1258 1259 1260 1261
  nil)

(deftest bignum.float.compare.1b
  (loop for x in *floats*
sds's avatar
sds committed
1262 1263 1264 1265 1266 1267 1268 1269 1270
        when (or (zerop x)
                 (< (abs (log (abs x))) 10000))
        nconc
        (loop for r = (1+ (random (ash 1 (random 32))))
              repeat 200
              nconc
              (let ((i (- (floor (rational x)) r)))
                (unless (< i x)
                  (list (list r x i))))))
1271 1272 1273 1274
  nil)

(deftest bignum.float.compare.2a
  (loop for x in *floats*
sds's avatar
sds committed
1275 1276 1277 1278 1279 1280 1281 1282 1283
        when (or (zerop x)
                 (< (abs (log (abs x))) 10000))
        nconc
        (loop for r = (1+ (random (ash 1 (random 32))))
              repeat 200
              nconc
              (let ((i (+ r (ceiling (rational x)))))
                (unless (> i x)
                  (list (list r x i))))))
1284 1285 1286 1287
  nil)

(deftest bignum.float.compare.2b
  (loop for x in *floats*
sds's avatar
sds committed
1288 1289 1290 1291 1292 1293 1294 1295 1296
        when (or (zerop x)
                 (< (abs (log (abs x))) 10000))
        nconc
        (loop for r = (1+ (random (ash 1 (random 32))))
              repeat 200
              nconc
              (let ((i (- (floor (rational x)) r)))
                (unless (> x i)
                  (list (list r x i))))))
1297 1298 1299 1300
  nil)

(deftest bignum.float.compare.3a
  (loop for x in *floats*
sds's avatar
sds committed
1301 1302 1303 1304 1305 1306 1307 1308 1309
        when (or (zerop x)
                 (< (abs (log (abs x))) 10000))
        nconc
        (loop for r = (1+ (random (ash 1 (random 32))))
              repeat 200
              nconc
              (let ((i (+ r (ceiling (rational x)))))
                (when (or (= x i) (= i x))
                  (list (list r x i))))))
1310 1311 1312 1313
  nil)

(deftest bignum.float.compare.3b
  (loop for x in *floats*
sds's avatar
sds committed
1314 1315 1316 1317 1318 1319 1320 1321 1322
        when (or (zerop x)
                 (< (abs (log (abs x))) 10000))
        nconc
        (loop for r = (1+ (random (ash 1 (random 32))))
              repeat 200
              nconc
              (let ((i (- (floor (rational x)) r)))
                (when (or (= x i) (= i x))
                  (list (list r x i))))))
1323 1324 1325 1326
  nil)

(deftest bignum.float.compare.4a
  (loop for x in *floats*
sds's avatar
sds committed
1327 1328 1329 1330 1331 1332 1333 1334 1335
        when (or (zerop x)
                 (< (abs (log (abs x))) 10000))
        nconc
        (loop for r = (1+ (random (ash 1 (random 32))))
              repeat 200
              nconc
              (let ((i (+ r (ceiling (rational x)))))
                (unless (and (/= i x) (/= x i))
                  (list (list r x i))))))
1336 1337 1338 1339
  nil)

(deftest bignum.float.compare.4b
  (loop for x in *floats*
sds's avatar
sds committed
1340 1341 1342 1343 1344 1345 1346 1347 1348
        when (or (zerop x)
                 (< (abs (log (abs x))) 10000))
        nconc
        (loop for r = (1+ (random (ash 1 (random 32))))
              repeat 200
              nconc
              (let ((i (- (floor (rational x)) r)))
                (unless (and (/= i x) (/= x i))
                  (list (list r x i))))))
1349 1350 1351 1352
  nil)

(deftest bignum.float.compare.5a
  (loop for x in *floats*
sds's avatar
sds committed
1353 1354 1355 1356 1357 1358 1359 1360 1361
        when (or (zerop x)
                 (< (abs (log (abs x))) 10000))
        nconc
        (loop for r = (1+ (random (ash 1 (random 32))))
              repeat 200
              nconc
              (let ((i (+ r (ceiling (rational x)))))
                (unless (<= x i)
                  (list (list r x i))))))
1362 1363 1364 1365
  nil)

(deftest bignum.float.compare.5b
  (loop for x in *floats*
sds's avatar
sds committed
1366 1367 1368 1369 1370 1371 1372 1373 1374
        when (or (zerop x)
                 (< (abs (log (abs x))) 10000))
        nconc
        (loop for r = (1+ (random (ash 1 (random 32))))
              repeat 200
              nconc
              (let ((i (- (floor (rational x)) r)))
                (unless (<= i x)
                  (list (list r x i))))))
1375 1376 1377 1378
  nil)

(deftest bignum.float.compare.6a
  (loop for x in *floats*
sds's avatar
sds committed
1379 1380 1381 1382 1383 1384 1385 1386 1387
        when (or (zerop x)
                 (< (abs (log (abs x))) 10000))
        nconc
        (loop for r = (1+ (random (ash 1 (random 32))))
              repeat 200
              nconc
              (let ((i (+ r (ceiling (rational x)))))
                (unless (>= i x)
                  (list (list r x i))))))
1388 1389 1390 1391
  nil)

(deftest bignum.float.compare.6b
  (loop for x in *floats*
sds's avatar
sds committed
1392 1393 1394 1395 1396 1397 1398 1399 1400
        when (or (zerop x)
                 (< (abs (log (abs x))) 10000))
        nconc
        (loop for r = (1+ (random (ash 1 (random 32))))
              repeat 200
              nconc
              (let ((i (- (floor (rational x)) r)))
                (unless (>= x i)
                  (list (list r x i))))))
1401 1402
  nil)

1403 1404
(deftest bignum.float.compare.7
  (let ((toobig (loop for x in *reals*
sds's avatar
sds committed
1405 1406
                      collect (and (> (abs x) 1.0)
                                   (> (abs (log (abs x))) 10000)))))
1407
    (loop for x in *reals*
sds's avatar
sds committed
1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420
          for xtoobig in toobig
          nconc
          (unless xtoobig
            (let ((fx (floor x)))
              (loop for y in *reals*
                    for ytoobig in toobig
                    when (and (not ytoobig)
                              (< x y)
                              (or (not (< fx y))
                                  (<= y fx)
                                  (not (> y fx))
                                  (>= fx y)))
                    collect (list x y))))))
1421 1422 1423 1424
  nil)

(deftest bignum.float.compare.8
  (let ((toobig (loop for x in *reals*
sds's avatar
sds committed
1425 1426
                      collect (and (> (abs x) 1.0)
                                   (> (abs (log (abs x))) 10000)))))
1427
    (loop for x in *reals*
sds's avatar
sds committed
1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440
          for xtoobig in toobig
          nconc
          (unless xtoobig
            (let ((fx (floor x)))
              (loop for y in *reals*
                    for ytoobig in toobig
                    when (and (not ytoobig)
                              (<= x y)
                              (or (not (<= fx y))
                                  (> fx y)
                                  (not (>= y fx))
                                  (< y fx)))
                    collect (list x y))))))
1441 1442 1443 1444 1445 1446
  nil)

;;; More randomized comparisons

(deftest bignum.short-float.random.compare.1
  (let* ((integer-bound (ash 1 1000))
sds's avatar
sds committed
1447 1448 1449
         (upper-bound (if (< (/ most-positive-short-float 2) integer-bound)
                          (/ most-positive-short-float 2)
                        (coerce integer-bound 'short-float))))
1450
    (loop for bound = 1.0s0 then (* bound 2)
sds's avatar
sds committed
1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462
          while (<= bound upper-bound)
          nconc
          (loop for r = (random bound)
                for fr = (floor r)
                for cr = (ceiling r)
                repeat 20
                unless (and (<= fr r cr)
                            (if (= r fr)
                                (= r cr)
                              (/= r cr))
                            (>= cr r fr))
                collect (list r fr cr))))
1463 1464 1465 1466
  nil)

(deftest bignum.single-float.random.compare.1
  (let* ((integer-bound (ash 1 100))
sds's avatar
sds committed
1467 1468 1469
         (upper-bound (if (< (/ most-positive-single-float 2) integer-bound)
                          (/ most-positive-single-float 2)
                        (coerce integer-bound 'single-float))))
1470
    (loop for bound = 1.0f0 then (* bound 2)
sds's avatar
sds committed
1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482
          while (<= bound upper-bound)
          nconc
          (loop for r = (random bound)
                for fr = (floor r)
                for cr = (ceiling r)
                repeat 20
                unless (and (<= fr r cr)
                            (if (= r fr)
                                (= r cr)
                              (/= r cr))
                            (>= cr r fr))
                collect (list r fr cr))))
1483 1484 1485 1486
  nil)

(deftest bignum.double-float.random.compare.1
  (let* ((integer-bound (ash 1 100))
sds's avatar
sds committed
1487 1488 1489
         (upper-bound (if (< (/ most-positive-double-float 2) integer-bound)
                          (/ most-positive-double-float 2)
                        (coerce integer-bound 'double-float))))
1490
    (loop for bound = 1.0d0 then (* bound 2)
sds's avatar
sds committed
1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502
          while (<= bound upper-bound)
          nconc
          (loop for r = (random bound)
                for fr = (floor r)
                for cr = (ceiling r)
                repeat 20
                unless (and (<= fr r cr)
                            (if (= r fr)
                                (= r cr)
                              (/= r cr))
                            (>= cr r fr))
                collect (list r fr cr))))
1503 1504 1505 1506
  nil)

(deftest bignum.long-float.random.compare.1
  (let* ((integer-bound (ash 1 100))
sds's avatar
sds committed
1507 1508 1509
         (upper-bound (if (< (/ most-positive-long-float 2) integer-bound)
                          (/ most-positive-long-float 2)
                        (coerce integer-bound 'long-float))))
1510
    (loop for bound = 1.0l0 then (* bound 2)
sds's avatar
sds committed
1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523
          while (< bound upper-bound)
          nconc
          (loop for r = (random bound)
                for fr = (floor r)
                for cr = (ceiling r)
                repeat 20
                unless (and (<= fr r cr)
                            (if (= r fr)
                                (= r cr)
                              (/= r cr))
                            (>= cr r fr))
                collect
                (list r fr cr))))
1524 1525 1526 1527 1528 1529
  nil)

;;; Rational/float comparisons

(deftest rational.short-float.random.compare.1
  (let* ((integer-bound (ash 1 1000))
sds's avatar
sds committed
1530 1531 1532
         (upper-bound (if (< (/ most-positive-short-float 2) integer-bound)
                          (/ most-positive-short-float 2)
                        (coerce integer-bound 'short-float))))
1533
    (loop for bound = 1.0s0 then (* bound 2)
sds's avatar
sds committed
1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552
          while (<= bound upper-bound)
          nconc
          (loop for r = (+ 1.s0 (random bound))
                for fr = (floor r)
                for cr = (ceiling r)
                for m = (ash 1 (1+ (random 30)))
                for p = (1+ (random m))
                for q = (1+ (random m))
                for x = 0
                repeat 50
                when (<= p q) do (psetf p (1+ q) q p)
                do (setf x (/ p q))
                unless (let ((fr/x (/ fr x))
                             (cr*x (* cr x)))
                         (and (<= fr/x r cr*x)
                              (< fr/x r cr*x)
                              (> cr*x r fr/x)
                              (>= cr*x r fr/x)))
                collect (list r p q x fr cr))))
1553 1554 1555 1556
  nil)

(deftest rational.single-float.random.compare.1
  (let* ((integer-bound (ash 1 1000))
sds's avatar
sds committed
1557 1558 1559
         (upper-bound (if (< (/ most-positive-single-float 2) integer-bound)
                          (/ most-positive-single-float 2)
                        (coerce integer-bound 'single-float))))
1560
    (loop for bound = 1.0f0 then (* bound 2)
sds's avatar
sds committed
1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579
          while (<= bound upper-bound)
          nconc
          (loop for r = (+ 1.s0 (random bound))
                for fr = (floor r)
                for cr = (ceiling r)
                for m = (ash 1 (1+ (random 30)))
                for p = (1+ (random m))
                for q = (1+ (random m))
                for x = 0
                repeat 50
                when (<= p q) do (psetf p (1+ q) q p)
                do (setf x (/ p q))
                unless (let ((fr/x (/ fr x))
                             (cr*x (* cr x)))
                         (and (<= fr/x r cr*x)
                              (< fr/x r cr*x)
                              (> cr*x r fr/x)
                              (>= cr*x r fr/x)))
                collect (list r p q x fr cr))))
1580 1581 1582 1583
  nil)

(deftest rational.double-float.random.compare.1
  (let* ((integer-bound (ash 1 1000))
sds's avatar
sds committed
1584 1585 1586
         (upper-bound (if (< (/ most-positive-double-float 4) integer-bound)
                          (/ most-positive-double-float 4)
                        (coerce integer-bound 'double-float))))
1587
    (loop for bound = 1.0d0 then (* bound 4)
sds's avatar
sds committed
1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606
          while (<= bound upper-bound)
          nconc
          (loop for r = (+ 1.s0 (random bound))
                for fr = (floor r)
                for cr = (ceiling r)
                for m = (ash 1 (1+ (random 30)))
                for p = (1+ (random m))
                for q = (1+ (random m))
                for x = 0
                repeat 50
                when (<= p q) do (psetf p (1+ q) q p)
                do (setf x (/ p q))
                unless (let ((fr/x (/ fr x))
                             (cr*x (* cr x)))
                         (and (<= fr/x r cr*x)
                              (< fr/x r cr*x)
                              (> cr*x r fr/x)
                              (>= cr*x r fr/x)))
                collect (list r p q x fr cr))))
1607 1608 1609 1610
  nil)

(deftest rational.long-float.random.compare.1
  (let* ((integer-bound (ash 1 1000))
sds's avatar
sds committed
1611 1612 1613
         (upper-bound (if (< (/ most-positive-long-float 4) integer-bound)
                          (/ most-positive-long-float 4)
                        (coerce integer-bound 'long-float))))
1614
    (loop for bound = 1.0d0 then (* bound 4)
sds's avatar
sds committed
1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633
          while (<= bound upper-bound)
          nconc
          (loop for r = (+ 1.s0 (random bound))
                for fr = (floor r)
                for cr = (ceiling r)
                for m = (ash 1 (1+ (random 30)))
                for p = (1+ (random m))
                for q = (1+ (random m))
                for x = 0
                repeat 50
                when (<= p q) do (psetf p (1+ q) q p)
                do (setf x (/ p q))
                unless (let ((fr/x (/ fr x))
                             (cr*x (* cr x)))
                         (and (<= fr/x r cr*x)
                              (< fr/x r cr*x)
                              (> cr*x r fr/x)
                              (>= cr*x r fr/x)))
                collect (list r p q x fr cr))))
1634 1635
  nil)

1636 1637 1638 1639 1640
;;; Test that explicit calls to macroexpand in subforms
;;; are done in the correct environment

(deftest =.env.1
  (macrolet ((%m (z) z))
sds's avatar
sds committed
1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658
            (mapcar 'notnot
                    (list (= (expand-in-current-env (%m 0)))
                          (= 1 (expand-in-current-env (%m 1)))
                          (= (expand-in-current-env (%m 2)) 2)
                          (= (expand-in-current-env (%m 3))
                             (expand-in-current-env (%m 3)))
                          (= (expand-in-current-env (%m #c(1 2)))
                             (expand-in-current-env (%m #c(1 2))))

                          (= 1 (expand-in-current-env (%m 2.0)))
                          (= (expand-in-current-env (%m 2)) 2/3)
                          (= (expand-in-current-env (%m 4))
                             (expand-in-current-env (%m 5)))

                          (= (expand-in-current-env (%m 0)) 0 0)
                          (= 0 (expand-in-current-env (%m 0)) 0)
                          (= 0 0 (expand-in-current-env (%m 0)))
                          )))
1659 1660 1661 1662 1663
  (t t t t t nil nil nil t t t))


(deftest /=.env.1
  (macrolet ((%m (z) z))
sds's avatar
sds committed
1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681
            (mapcar 'notnot
                    (list (/= (expand-in-current-env (%m 0)))
                          (/= 1 (expand-in-current-env (%m 1)))
                          (/= (expand-in-current-env (%m 2)) 2)
                          (/= (expand-in-current-env (%m 3))
                              (expand-in-current-env (%m 3)))
                          (/= (expand-in-current-env (%m #c(1 2)))
                              (expand-in-current-env (%m #c(1 2))))

                          (/= 1 (expand-in-current-env (%m 2.0)))
                          (/= (expand-in-current-env (%m 2)) 2/3)
                          (/= (expand-in-current-env (%m 4))
                              (expand-in-current-env (%m 5)))

                          (/= (expand-in-current-env (%m 2)) 0 1)
                          (/= 0 (expand-in-current-env (%m 2)) 1)
                          (/= 0 1 (expand-in-current-env (%m 2)))
                          )))
1682 1683 1684 1685
  (t nil nil nil nil t t t t t t))

(deftest <.env.1
  (macrolet ((%m (z) z))
sds's avatar
sds committed
1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701
            (mapcar 'notnot
                    (list (< (expand-in-current-env (%m 0)))
                          (< 0 (expand-in-current-env (%m 1)))
                          (< (expand-in-current-env (%m 2)) 3)
                          (< (expand-in-current-env (%m 5))
                             (expand-in-current-env (%m 7)))

                          (< 3 (expand-in-current-env (%m 2.0)))
                          (< (expand-in-current-env (%m 2)) 2/3)
                          (< (expand-in-current-env (%m 6))
                             (expand-in-current-env (%m 5)))

                          (< (expand-in-current-env (%m 1)) 2 3)
                          (< 1 (expand-in-current-env (%m 2)) 3)
                          (< 1 2 (expand-in-current-env (%m 3)))
                          )))
1702 1703 1704 1705
  (t t t t nil nil nil t t t))

(deftest <=.env.1
  (macrolet ((%m (z) z))
sds's avatar
sds committed
1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721
            (mapcar 'notnot
                    (list (<= (expand-in-current-env (%m 0)))
                          (<= 0 (expand-in-current-env (%m 1)))
                          (<= (expand-in-current-env (%m 2)) 3)
                          (<= (expand-in-current-env (%m 5))
                             (expand-in-current-env (%m 7)))

                          (<= 3 (expand-in-current-env (%m 2.0)))
                          (<= (expand-in-current-env (%m 2)) 2/3)
                          (<= (expand-in-current-env (%m 6))
                             (expand-in-current-env (%m 5)))

                          (<= (expand-in-current-env (%m 2)) 2 3)
                          (<= 1 (expand-in-current-env (%m 1)) 3)
                          (<= 1 2 (expand-in-current-env (%m 2)))
                          )))
1722 1723 1724 1725
  (t t t t nil nil nil t t t))

(deftest >.env.1
  (macrolet ((%m (z) z))
sds's avatar
sds committed
1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741
            (mapcar 'notnot
                    (list (> (expand-in-current-env (%m 0)))
                          (> 2 (expand-in-current-env (%m 1)))
                          (> (expand-in-current-env (%m 4)) 3)
                          (> (expand-in-current-env (%m 10))
                             (expand-in-current-env (%m 7)))

                          (> 1 (expand-in-current-env (%m 2.0)))
                          (> (expand-in-current-env (%m -1)) 2/3)
                          (> (expand-in-current-env (%m 4))
                             (expand-in-current-env (%m 5)))

                          (> (expand-in-current-env (%m 2)) 1 0)
                          (> 2 (expand-in-current-env (%m 1)) 0)
                          (> 2 1 (expand-in-current-env (%m 0)))
                          )))
1742 1743 1744 1745 1746
  (t t t t nil nil nil t t t))


(deftest >=.env.1
  (macrolet ((%m (z) z))
sds's avatar
sds committed
1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762
            (mapcar 'notnot
                    (list (>= (expand-in-current-env (%m 0)))
                          (>= 2 (expand-in-current-env (%m 1)))
                          (>= (expand-in-current-env (%m 4)) 3)
                          (>= (expand-in-current-env (%m 7))
                              (expand-in-current-env (%m 7)))

                          (>= 1 (expand-in-current-env (%m 2.0)))
                          (>= (expand-in-current-env (%m -1)) 2/3)
                          (>= (expand-in-current-env (%m 4))
                             (expand-in-current-env (%m 5)))

                          (>= (expand-in-current-env (%m 2)) 1 1)
                          (>= 1 (expand-in-current-env (%m 1)) 0)
                          (>= 2 2 (expand-in-current-env (%m 0)))
                          )))
1763
  (t t t t nil nil nil t t t))