Trust non-returning functions during sb-xc.
[sbcl.git] / tests / symbol.pure.lisp
blob8ae9b9499e696752d55e15b2f17544f1e37674ea
1 ;;;; miscellaneous tests of SYMBOL-related stuff
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 ;;;; absolutely no warranty. See the COPYING and CREDITS files for
12 ;;;; more information.
14 ;;; Reported by Paul F. Dietz
15 (with-test (:name (:symbol :non-simple-string-name))
16 (let ((sym (make-symbol (make-array '(1) :element-type 'character
17 :adjustable t :initial-contents "X"))))
18 (assert (simple-string-p (symbol-name sym)))
19 (print sym (make-broadcast-stream))))
21 (with-test (:name (gentemp :pprinter))
22 (let* ((*print-pprint-dispatch* (copy-pprint-dispatch)))
23 (set-pprint-dispatch 'string
24 (lambda (stream obj)
25 (declare (ignore obj))
26 (write-string "BAR-" stream)))
27 (assert (string= "FOO-" (gentemp "FOO-") :end2 4))))
29 (with-test (:name (gensym :fixnum-restriction))
30 (gensym (1+ most-positive-fixnum)))
32 ;; lp#1439921
33 ;; CLHS states that SYMBOL-FUNCTION of a symbol naming a special operator
34 ;; or macro must return something implementation-defined that might not
35 ;; be a function. In this implementation it is a function, but it is illegal
36 ;; to assign that function into another symbol via (SETF FDEFINITION).
37 (with-test (:name :setf-fdefinition-no-magic-functions)
38 (assert-error (setf (fdefinition 'mysym) (fdefinition 'and)))
39 (assert-error (setf (fdefinition 'mysym) (fdefinition 'if)))
40 (assert-error (setf (symbol-function 'mysym) (symbol-function 'and)))
41 (assert-error (setf (symbol-function 'mysym) (symbol-function 'if))))
43 (with-test (:name :macro-guard-function-name)
44 (do-all-symbols (s)
45 (when (macro-function s)
46 (let* ((f (symbol-function s))
47 (name (sb-kernel:%fun-name f)))
48 (if (special-operator-p s)
49 (assert (typep name '(cons (eql :special)
50 (cons symbol null))))
51 (assert (typep name '(cons (eql :macro)
52 (cons symbol null)))))))))
54 (with-test (:name :fdefinition-no-consing
55 :skipped-on :interpreter)
56 (ctu:assert-no-consing (fdefinition 'list)))
58 (with-test (:name :tree-shaker :skipped-on :sb-devel)
59 ;; Assert that even without the "!" prefix convention
60 ;; these used-only-at-cross-compile-time macros disappear.
61 (dolist (s '("DEFINE-FOP"
62 "DEFINE-TYPE-CLASS"
63 "DEFINE-TYPE-METHOD"
64 "DEF-TYPE-TRANSLATOR"
65 "DEFINE-TYPE-VOP"
66 "DEFINE-PRIMITIVE-OBJECT"))
67 (assert (not (apropos-list s)))))
69 (with-test (:name :progv-no-body)
70 (checked-compile-and-assert
72 '(lambda (vars vals)
73 (progv vars vals))
74 ((nil nil) nil)))
76 (defun summarize-colliding-hashes (print)
77 ;; Collisions on SYMBOL-HASH aren't errors, merely unfortunate.
78 ;; The printed output is nifty because it shows e.g.
79 ;; - of the 25 distinct symbols named "ARGS", only 2 collide on hash
80 ;; - 20 symbols named "RESULT", 2 collide, etc.
81 (let ((ht (make-hash-table :test 'equal))
82 (result)
83 (n-homograph-sets 0)
84 (n-well-hashed-sets 0))
85 ;; map string -> set of symbols whose print name is that string
86 (do-all-symbols (s)
87 (pushnew s (gethash (string s) ht)))
88 (sb-int:dohash ((string symbols) ht)
89 (when (cdr symbols)
90 (incf n-homograph-sets)
91 (let* ((hashes (mapcar #'sb-kernel:symbol-hash symbols))
92 (dedup (remove-duplicates hashes))
93 (alist))
94 (when (< (length dedup) (length symbols))
95 (push symbols result))
96 (cond ((= (length dedup) (length symbols))
97 (incf n-well-hashed-sets))
98 (print
99 ;; Start by binning symbols by their hash.
100 (dolist (s symbols)
101 (let* ((h (sb-kernel:symbol-hash s))
102 (cell (assoc h alist)))
103 (if cell
104 (push s (cdr cell))
105 (push (list h s) alist))))
106 (format t "Collisions on ~S (~D same-named symbols):~%"
107 string (length symbols))
108 ;; Only print bins having > 1 symbol
109 (dolist (cell alist)
110 (when (cddr cell)
111 (format t " ~x ~s~%" (car cell) (cdr cell)))))))))
112 (when print
113 (format t "~D sets of symbols spelled the same, ~D well-hashed~%"
114 n-homograph-sets n-well-hashed-sets))
115 ;; The primary result is a "score", the nearer to 1 the better.
116 ;; The secondary result is a list of all sets with collisions
117 (values (/ n-well-hashed-sets n-homograph-sets)
118 result)))
120 (with-test (:name :hashing-improvements :skipped-on (not :salted-symbol-hash))
121 ;; Roughly: For each set of symbols colliding on SXHASH at all, what fraction
122 ;; of those sets do NOT have any collisions on SYMBOL-HASH.
123 (let ((expectation
124 #+64-bit .95
125 #-64-bit .70))
126 (assert (> (summarize-colliding-hashes nil) expectation))))
128 (with-test (:name :fast-slot-name-mapper-small)
129 ;; The XSET type has only2 slots, does not get a compiled function
130 ;; as its slot mapper. Test that STRUCTURE-SLOT-VALUE is ok with that.
131 (assert (vectorp (sb-kernel::layout-slot-mapper (sb-kernel:find-layout 'sb-int:xset))))
132 (let ((x (sb-int:alloc-xset)))
133 (sb-int:add-to-xset #\A x)
134 (assert (equal (sb-pcl::structure-slot-value x 'sb-kernel::data) '(#\A)))))
136 (with-test (:name :fast-slot-name-mapper-big)
137 (let ((collision-sets
138 #+salted-symbol-hash
139 (nth-value 1 (summarize-colliding-hashes nil))
140 ;; Require at least 4 different sets of colliding symbols,
141 ;; Needless to say, this hashes horribly, with some sets
142 ;; having 20 element per bucket.
143 #-salted-symbol-hash (list (find-all-symbols "X")
144 (find-all-symbols "Y")
145 (find-all-symbols "RESULT") ; lots of these
146 (find-all-symbols "ARGS")))
147 (alist)
148 (arb-value 0))
149 (dolist (set collision-sets)
150 (dolist (symbol set)
151 (push (cons symbol (incf arb-value)) alist)))
152 ;; the mapper shouldn't be a simple-vector
153 (let ((function
154 (the function (sb-kernel::make-hash-based-slot-mapper
155 alist "foo"))))
156 ;; now try it
157 (dolist (pair alist)
158 (let* ((key (car pair))
159 (computed (funcall function key)))
160 (assert (eql computed (cdr pair))))))))