logandc2.lsp 1.99 KB
Newer Older
1 2 3 4 5 6 7 8 9
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Tue Sep  9 05:52:31 2003
;;;; Contains: Tests of LOGANDC2

(in-package :cl-test)

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

10 11
;;; Error tests

12
(deftest logandc2.error.1
pfdietz's avatar
pfdietz committed
13
  (check-type-error #'(lambda (x) (logandc2 x 0)) #'integerp)
14 15 16
  nil)

(deftest logandc2.error.2
pfdietz's avatar
pfdietz committed
17
  (check-type-error #'(lambda (x) (logandc2 0 x)) #'integerp)
18 19 20
  nil)

(deftest logandc2.error.3
21 22
  (signals-error (logandc2) program-error)
  t)
23 24

(deftest logandc2.error.4
25 26
  (signals-error (logandc2 0) program-error)
  t)
27 28

(deftest logandc2.error.5
29 30 31 32
  (signals-error (logandc2 1 2 3) program-error)
  t)

;;; Non-error tests
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

(deftest logandc2.1
  (logandc2 0 0)
  0)

(deftest logandc2.2
  (logandc2 -1 0)
  -1)

(deftest logandc2.3
  (logandc2 (1+ most-positive-fixnum) 0)
  #.(1+ most-positive-fixnum))

(deftest logandc2.4
  (loop for x in *integers*
sds's avatar
sds committed
48 49 50 51
        always (and (eql x (logandc2 x 0))
                    (eql 0 (logandc2 x x))
                    (eql x (logandc2 x (lognot x)))
                    (eql (lognot x) (logandc2 (lognot x) x))))
52 53 54 55
  t)

(deftest logandc2.5
  (loop for x = (random-fixnum)
sds's avatar
sds committed
56 57 58 59
        for xc = (lognot x)
        repeat 1000
        unless (eql x (logandc2 x xc))
        collect x)
60 61 62 63
  nil)

(deftest logandc2.6
  (loop for x = (random-from-interval (ash 1 (random 200)))
sds's avatar
sds committed
64 65 66 67 68 69 70 71 72 73 74 75
        for y = (random-from-interval (ash 1 (random 200)))
        for z = (logandc2 x y)
        repeat 1000
        unless (and (if (and (< x 0) (>= y 0))
                        (< z 0)
                      (>= z 0))
                    (loop for i from 1 to 210
                          always (if (and (not (logbitp i y))
                                          (logbitp i x))
                                     (logbitp i z)
                                   (not (logbitp i z)))))
        collect (list x y z))
76 77 78 79 80 81
  nil)

(deftest logandc2.order.1
  (let ((i 0) a b)
    (values
     (logandc2 (progn (setf a (incf i)) -1)
sds's avatar
sds committed
82
               (progn (setf b (incf i)) 0))
83 84
     i a b))
  -1 2 1 2)