Fix make-array transforms.
[sbcl.git] / tests / bit-vector.impure-cload.lisp
1 ;;;; This software is part of the SBCL system. See the README file for
2 ;;;; more information.
3 ;;;;
4 ;;;; While most of SBCL is derived from the CMU CL system, the test
5 ;;;; files (like this one) were written from scratch after the fork
6 ;;;; from CMU CL.
7 ;;;;
8 ;;;; This software is in the public domain and is provided with
9 ;;;; absolutely no warranty. See the COPYING and CREDITS files for
10 ;;;; more information.
11
12 ;;; the bitvector transforms were buggy prior to sbcl-0.7.3.4 under
13 ;;; speed-optimizing regimes; in particular, they would fail if the
14 ;;; vector length were near ARRAY-DIMENSION-LIMIT. Testing this takes
15 ;;; up a certain amount of time...
16
17 (declaim (optimize (speed 3) (safety 1) (space 0) (compilation-speed 0)))
18
19 (defun test-small-bit-vectors ()
20   ;; deal with the potential length 0 special case
21   (let ((a (make-array 0 :element-type 'bit))
22         (b (make-array 0 :element-type 'bit)))
23     (assert (equal (bit-not a) #*))
24     (assert (equal (bit-xor a b a) #*))
25     (assert (equal (bit-and a a b) #*)))
26   ;; also test some return values for sanity
27   (let ((a (make-array 33 :element-type 'bit :initial-element 0))
28         (b (make-array 33 :element-type 'bit :initial-element 0)))
29     (assert (equal (bit-not a a) #*111111111111111111111111111111111))
30     (setf (aref a 0) 0) ; a = #*011..1
31     (setf (aref b 1) 1) ; b = #*010..0
32     (assert (equal (bit-xor a b) #*001111111111111111111111111111111))
33     (assert (equal (bit-and a b) #*010000000000000000000000000000000)))
34   ;; a special COUNT transform on bitvectors; triggers on (>= SPEED SPACE)
35   (locally
36       (declare (optimize (speed 3) (space 1)))
37     (let ((bv1 (make-array 5 :element-type 'bit))
38           (bv2 (make-array 0 :element-type 'bit))
39           (bv3 (make-array 68 :element-type 'bit)))
40       (declare (type simple-bit-vector bv1 bv2 bv3))
41       (setf (sbit bv3 42) 1)
42       ;; bitvector smaller than the word size
43       (assert (= 0 (count 1 bv1)))
44       (assert (= 5 (count 0 bv1)))
45       ;; special case of 0-length bitvectors
46       (assert (= 0 (count 1 bv2)))
47       (assert (= 0 (count 0 bv2)))
48       ;; bitvector larger than the word size
49       (assert (= 1 (count 1 bv3)))
50       (assert (= 67 (count 0 bv3))))))
51
52 (defun inform (msg)
53   (print msg)
54   (force-output))
55
56 (defun test-big-bit-vectors ()
57   ;; now test the biggy, mostly that it works...
58   (let ((a (progn
59              (inform :make-array-1)
60              (make-array (1- array-dimension-limit)
61                          :element-type 'bit :initial-element 0)))
62         (b (progn
63              (inform :make-array-2)
64              (make-array (1- array-dimension-limit)
65                          :element-type 'bit :initial-element 0))))
66     (inform :bit-not)
67     (bit-not a a)
68     (inform :aref-1)
69     (assert (= (aref a 0) 1))
70     (inform :aref-2)
71     (assert (= (aref a (- array-dimension-limit 2)) 1))
72     (inform :bit-and)
73     (bit-and a b a)
74     (inform :aref-3)
75     (assert (= (aref a 0) 0))
76     (inform :aref-4)
77     (assert (= (aref a (- array-dimension-limit 2)) 0))))
78
79 (test-small-bit-vectors)
80
81 ;; except on machines where the arrays won't fit into the dynamic space.
82 #+#.(cl:if (cl:> (sb-ext:dynamic-space-size)
83                  (cl:truncate (cl:1- cl:array-dimension-limit)
84                               sb-vm:n-word-bits))
85            '(and)
86            '(or))
87 (test-big-bit-vectors)
88
89 (with-test (:name :find-non-bit-from-bit-vector)
90   (assert (not (find #\a #*0101)))
91   (assert (not (position #\a #*0101)))
92   (let ((f1 (compile nil
93                      `(lambda (b)
94                         (find b #*0101))))
95         (f2 (compile nil
96                      `(lambda (b)
97                         (position b #*0101)))))
98     (assert (not (funcall f1 t)))
99     (assert (not (funcall f2 t))))
100   (let ((f1 (compile nil
101                      `(lambda (b)
102                         (declare (bit-vector b))
103                         (find t b))))
104         (f2 (compile nil
105                      `(lambda (b)
106                         (declare (bit-vector b))
107                         (position t b)))))
108     (assert (not (funcall f1 #*010101)))
109     (assert (not (funcall f2 #*101010)))))