bit.lsp 3.04 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Sun Jan 26 13:22:59 2003
;;;; Contains: Tests for accessor BIT

(in-package :cl-test)

(deftest bit.1
  (bit #*0010 2)
  1)

(deftest bit.2
  (let ((a #*00000000))
    (loop for i from 0 below (length a)
sds's avatar
sds committed
15 16 17
          collect (let ((b (copy-seq a)))
                    (setf (bit b i) 1)
                    b)))
18 19 20 21 22 23 24 25 26 27 28 29
  (#*10000000
   #*01000000
   #*00100000
   #*00010000
   #*00001000
   #*00000100
   #*00000010
   #*00000001))

(deftest bit.3
  (let ((a #*11111111))
    (loop for i from 0 below (length a)
sds's avatar
sds committed
30 31 32
          collect (let ((b (copy-seq a)))
                    (setf (bit b i) 0)
                    b)))
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
  (#*01111111
   #*10111111
   #*11011111
   #*11101111
   #*11110111
   #*11111011
   #*11111101
   #*11111110))

(deftest bit.4
  (let ((a (make-array nil :element-type 'bit :initial-element 0)))
    (values
     (aref a)
     (bit a)
     (setf (bit a) 1)
     (aref a)
     (bit a)))
  0 0 1 1 1)

(deftest bit.5
  (let ((a (make-array '(1 1) :element-type 'bit :initial-element 0)))
    (values
     (aref a 0 0)
     (bit a 0 0)
     (setf (bit a 0 0) 1)
     (aref a 0 0)
     (bit a 0 0)))
  0 0 1 1 1)

(deftest bit.6
  (let ((a (make-array '(10 10) :element-type 'bit :initial-element 0)))
    (values
     (aref a 5 5)
     (bit a 5 5)
     (setf (bit a 5 5) 1)
     (aref a 5 5)
     (bit a 5 5)))
  0 0 1 1 1)

;;; Check that the fill pointer is ignored

(deftest bit.7
  (let ((a (make-array '(10) :initial-contents '(0 1 1 0 0 1 1 1 0 0)
sds's avatar
sds committed
76 77
                       :element-type 'bit
                       :fill-pointer 5)))
78 79 80 81
    (values
     (coerce a 'list)
     (loop for i from 0 below 10 collect (bit a i))
     (loop for i from 0 below 10
sds's avatar
sds committed
82
           collect (setf (bit a i) (- 1 (bit a i))))
83 84 85 86 87 88 89 90 91 92 93 94 95 96
     (coerce a 'list)
     (loop for i from 0 below 10 collect (bit a i))
     (fill-pointer a)))
  (0 1 1 0 0)
  (0 1 1 0 0 1 1 1 0 0)
  (1 0 0 1 1 0 0 0 1 1)
  (1 0 0 1 1)
  (1 0 0 1 1 0 0 0 1 1)
  5)

;;; Check that adjustability is not relevant

(deftest bit.8
  (let ((a (make-array '(10) :initial-contents '(0 1 1 0 0 1 1 1 0 0)
sds's avatar
sds committed
97 98
                       :element-type 'bit
                       :adjustable t)))
99 100 101 102
    (values
     (coerce a 'list)
     (loop for i from 0 below 10 collect (bit a i))
     (loop for i from 0 below 10
sds's avatar
sds committed
103
           collect (setf (bit a i) (- 1 (bit a i))))
104 105 106 107 108 109 110
     (coerce a 'list)
     (loop for i from 0 below 10 collect (bit a i))))
  (0 1 1 0 0 1 1 1 0 0)
  (0 1 1 0 0 1 1 1 0 0)
  (1 0 0 1 1 0 0 0 1 1)
  (1 0 0 1 1 0 0 0 1 1)
  (1 0 0 1 1 0 0 0 1 1))
111 112 113

;;; Order of evaluation tests

114
(deftest bit.order.1
115
  (let ((x 0) y z
sds's avatar
sds committed
116
        (b (copy-seq #*01010)))
117 118
    (values
     (bit (progn (setf y (incf x)) b)
sds's avatar
sds committed
119
          (progn (setf z (incf x)) 1))
120 121 122
     x y z))
  1 2 1 2)

123
(deftest bit.order.2
124
  (let ((x 0) y z w
sds's avatar
sds committed
125
        (b (copy-seq #*01010)))
126 127
    (values
     (setf (bit (progn (setf y (incf x)) b)
sds's avatar
sds committed
128 129
                (progn (setf z (incf x)) 1))
           (progn (setf w (incf x)) 0))
130 131 132 133
     b
     x y z w))
  0 #*00010 3 1 2 3)

134
(deftest bit.error.1
135 136
  (signals-error (bit) program-error)
  t)