logand.lsp 2.38 KB
Newer Older
1 2 3 4 5 6 7 8 9
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Mon Sep  8 21:23:22 2003
;;;; Contains: Tests of LOGAND

(in-package :cl-test)

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

10 11
;;; Error tests

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

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

(deftest logand.error.3
  (check-type-error #'(lambda (x) (logand x 1)) #'integerp)
22 23
  nil)

24
;;; Non-error tests
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

(deftest logand.1
  (logand)
  -1)

(deftest logand.2
  (logand 1231)
  1231)

(deftest logand.3
  (logand -198)
  -198)

(deftest logand.4
  (loop for x in *integers*
sds's avatar
sds committed
40
        always (eql x (logand x)))
41 42 43 44
  t)

(deftest logand.5
  (loop for x in *integers*
sds's avatar
sds committed
45
        always (eql 0 (logand x (lognot x))))
46 47 48 49
  t)

(deftest logand.6
  (loop for x = (random-fixnum)
sds's avatar
sds committed
50 51 52 53
        for xc = (lognot x)
        repeat 1000
        unless (eql 0 (logand x xc))
        collect x)
54 55 56 57
  nil)

(deftest logand.7
  (loop for x = (random-from-interval (ash 1 (random 200)))
sds's avatar
sds committed
58 59 60 61 62 63 64 65 66 67 68 69
        for y = (random-from-interval (ash 1 (random 200)))
        for z = (logand 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 (logbitp i x)
                                          (logbitp i y))
                                     (logbitp i z)
                                   (not (logbitp i z)))))
        collect (list x y z))
70 71 72 73
  nil)

(deftest logand.8
  (loop for i from 1 to (min 256 (1- call-arguments-limit))
sds's avatar
sds committed
74 75 76
        for args = (nconc (make-list (1- i) :initial-element -1)
                          (list 183))
        always (eql (apply #'logand args) 183))
77 78 79 80
  t)

(deftest logand.9
  (loop for i from -1 to 0 always
sds's avatar
sds committed
81 82 83
        (loop for j from -1 to 0 always
              (locally (declare (type (integer -1 0) i j))
                       (eql (logand i j) (if (or (zerop i) (zerop j)) 0 -1)))))
84
  t)
85 86 87 88 89

(deftest logand.order.1
  (let ((i 0) a b)
    (values
     (logand (progn (setf a (incf i)) #b11011)
sds's avatar
sds committed
90
             (progn (setf b (incf i)) #b10110))
91 92 93 94 95 96 97
     i a b))
  #b10010 2 1 2)

(deftest logand.order.2
  (let ((i 0) a b c)
    (values
     (logand (progn (setf a (incf i))  #b11011)
sds's avatar
sds committed
98 99
             (progn (setf b (incf i))  #b10110)
             (progn (setf c (incf i)) #b110101))
100 101
     i a b c))
  #b10000 3 1 2 3)