Use recently-added utility function.
[sbcl.git] / tests / raw-slots-interleaved.impure.lisp
blob53c3732806f5a9663bc3fdacc3a5bb6c28f20b8f
1 ;;;; gc tests
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; While most of SBCL is derived from the CMU CL system, the test
7 ;;;; files (like this one) were written from scratch after the fork
8 ;;;; from CMU CL.
9 ;;;
10 ;;;; This software is in the public domain and is provided with
11 ;;;; absoluely no warranty. See the COPYING and CREDITS files for
12 ;;;; more information.
14 (in-package :cl-user)
16 #-interleaved-raw-slots (invoke-restart 'run-tests::skip-file)
18 ;;; More tests of raw slots can be found in 'defstruct.impure.lisp'
19 ;;; Since those are all passing, it's fair to say that interleaving works.
20 ;;; But we want also to test what happens in a very specific case that
21 ;;; is difficult to provoke, when a structure contains enough slots that
22 ;;; its raw bitmap is a bignum and the bignum is moved during GC.
24 (macrolet ((defbiggy ()
25 `(defstruct biggy
26 ,@(loop for i from 1 to 62
27 collect `(,(sb-int:symbolicate "SLOT" (write-to-string i))
28 0 :type ,(if (> i 60) 'sb-ext:word t))))))
29 (defbiggy))
31 (assert (typep (sb-kernel:layout-raw-slot-metadata
32 (sb-kernel::find-layout 'biggy)) 'bignum))
34 (defvar *x* nil)
35 (defvar *y* nil)
37 ;; This test offers "anecdotal evidence" that it works to have
38 ;; a bignum for raw slot metadata, *and* that the bignum could be
39 ;; transported by GC, leaving a forwarding pointer,
40 ;; before transporting an instance of an object whose layout
41 ;; sees the bignum.
43 ;; Without extra augmentation of the GC code [such as printf("got here!")]
44 ;; there is no visible means of determining that this works,
45 ;; aside from GC not crashing.
46 ;; Additionally, the test does not work - which is to say, the GC behavior
47 ;; is different and the desired effect can't be observed - when placed in
48 ;; a WITH-TEST or any other toplevel "noise"; but even without that,
49 ;; the test is brittle.
50 ;; With some extra annotation (printf of otherwise), the line
51 ;; of code in positive_bignum_logbitp() is seen to be reached 63 times
52 ;; in each test run, corresponding to the 63 slots (counting the layout)
53 ;; in each structure instance, times two structure instances.
55 ;; Run it twice to make sure things really worked.
57 (let ((*y* (make-biggy))
58 (*x* (sb-kernel:layout-raw-slot-metadata
59 (sb-kernel::find-layout 'biggy))))
60 (sb-ext:gc :gen 1))
61 (princ 'did-pass-1) (terpri)
62 (force-output)
64 (let ((*y* (make-biggy))
65 (*x* (sb-kernel:layout-raw-slot-metadata
66 (sb-kernel::find-layout 'biggy))))
67 (sb-ext:gc :gen 1))
68 (princ 'did-pass-2) (terpri)
69 (force-output)
71 ;; Test the C bignum bit extractor.
72 ;; Surprisingly, there was a bug in it, unrelated to forwarding
73 ;; pointers that remained dormant until the randomized
74 ;; HUGE-MANYRAW test in 'defstruct.impure.lisp' found it.
75 (defun c-bignum-logbitp (index bignum)
76 (assert (typep bignum 'bignum))
77 (sb-sys:with-pinned-objects (bignum)
78 (alien-funcall (extern-alien "positive_bignum_logbitp"
79 (function boolean int system-area-pointer))
80 index
81 (sb-sys:int-sap
82 (- (sb-kernel:get-lisp-obj-address bignum)
83 sb-vm:other-pointer-lowtag)))))
85 (with-test (:name :c-bignum-logbitp)
86 ;; walking 1 bit
87 (dotimes (i 256)
88 (let ((num (ash 1 i)))
89 (when (typep num 'bignum)
90 (dotimes (j 257)
91 (assert (eq (c-bignum-logbitp j num)
92 (logbitp j num)))))))
93 ;; random bits
94 (let ((max (ash 1 768)))
95 (dotimes (i 100)
96 (let ((num (random max)))
97 (when (typep num 'bignum)
98 (dotimes (j (* (sb-bignum:%bignum-length num)
99 sb-vm:n-word-bits))
100 (assert (eq (c-bignum-logbitp j num)
101 (logbitp j num)))))))))
103 ;; for testing the comparator
104 (defstruct foo1
105 (df 1d0 :type double-float) ; index 1
106 (a 'aaay) ; index 2
107 (sf 1f0 :type single-float) ; index 3
108 (cdf #c(1d0 1d0) :type (complex double-float)) ; indices 4 and 5
109 (b 'bee) ; index 6
110 (csf #c(2f0 2f0) :type (complex single-float)) ; index 7
111 (w 0 :type sb-ext:word) ; index 8
112 (c 'cee)) ; index 9
114 (defvar *afoo* (make-foo1))
115 (with-test (:name :tagged-slot-iterator-macro)
116 (setf (sb-kernel:%instance-ref *afoo* 10) 'magic)
117 (let (l)
118 (push `(0 ,(sb-kernel:%instance-layout *afoo*)) l)
119 (sb-kernel:do-instance-tagged-slot (i *afoo*)
120 (push `(,i ,(sb-kernel:%instance-ref *afoo* i)) l))
121 (assert (oddp (sb-kernel:%instance-length *afoo*)))
122 (assert (= (sb-kernel:layout-length (sb-kernel:layout-of *afoo*))
123 (1- (sb-kernel:%instance-length *afoo*))))
124 (assert (equalp (nreverse l)
125 `((0 ,(sb-kernel:find-layout 'foo1))
126 (2 aaay)
127 (6 bee)
128 (9 cee)
129 ;; slots 1 through 10 exist, to keep total
130 ;; object length EVEN.
131 (10 magic))))))
133 (defvar *anotherfoo* (make-foo1))
135 (with-test (:name :structure-obj-equalp-raw-slots)
136 ;; these structures are EQUALP even though one of them
137 ;; has a word of junk in its padding slot, as could happen
138 ;; if the structure was stack-allocated (I think)
139 (assert (equalp *anotherfoo* *afoo*)))
141 (defstruct foo
143 (w 0 :type sb-ext:word)
145 (cdf #c(0d0 0d0) :type (complex double-float))
147 (sb-kernel:define-structure-slot-addressor
148 foo-w-ptr :structure foo :slot w)
149 (sb-kernel:define-structure-slot-addressor
150 foo-cdf-ptr :structure foo :slot cdf)
152 (with-test (:name :define-structure-slot-addressor)
153 (let* ((word (logand sb-ext:most-positive-word #xfeedbad))
154 (re 4.2d58)
155 (im 8.93d-10)
156 (thing (make-foo :cdf (complex re im) :w word)))
157 (sb-sys:with-pinned-objects (thing)
158 (assert (= word (sb-sys:sap-ref-word
159 (sb-sys:int-sap (foo-w-ptr thing)) 0)))
160 (assert (= re (sb-sys:sap-ref-double
161 (sb-sys:int-sap (foo-cdf-ptr thing)) 0)))
162 (assert (= im (sb-sys:sap-ref-double
163 (sb-sys:int-sap (foo-cdf-ptr thing)) 8))))))
165 (load "compiler-test-util.lisp")
166 (with-test (:name :copy-structure-efficient-case)
167 (assert (not (ctu:find-named-callees #'copy-structure :name 'ash))))