1 ;; Copyright (C) 1999 Free Software Foundation, Inc.
3 ;; Author: Hrvoje Niksic <hniksic@xemacs.org>
4 ;; Maintainer: Hrvoje Niksic <hniksic@xemacs.org>
8 ;; This file is part of SXEmacs.
10 ;; SXEmacs is free software: you can redistribute it and/or modify it
11 ;; under the terms of the GNU General Public License as published by the
12 ;; Free Software Foundation, either version 3 of the License, or (at your
13 ;; option) any later version.
15 ;; SXEmacs is distributed in the hope that it will be
16 ;; useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ;; General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Synched up with: Not in FSF.
27 ;; Test symbols operations.
28 ;; See test-harness.el for instructions on how to run these tests.
32 (require 'test-harness)
35 (when (and (boundp 'load-file-name) (stringp load-file-name))
36 (push (file-name-directory load-file-name) load-path))
37 (require 'test-harness))))
40 (defun ts-fresh-symbol-name (name)
41 "Return a variant of NAME (a string) that is not interned."
42 (when (intern-soft name)
46 (setq name (format "%s-%d" orig count))
51 ;;-----------------------------------------------------
52 ;; Creating, reading, and printing symbols
53 ;;-----------------------------------------------------
55 (dolist (name '("foo" "bar" ""
56 "something with space in it"
57 "a string with \0 in the middle."
58 "100" "10.0" "#<>[]]]];'\\';"
60 (let ((interned (intern name))
61 (uninterned (make-symbol name)))
62 (Assert (symbolp interned))
63 (Assert (symbolp uninterned))
64 (Assert-Equal (symbol-name interned) name)
65 (Assert-Equal (symbol-name uninterned) name)
66 (Assert (not (eq interned uninterned)))
67 (Assert-Not-Equal interned uninterned)))
69 (flet ((check-weak-list-unique (weak-list &optional reversep)
70 "Check that elements of WEAK-LIST are referenced only there."
71 (let ((len (length (weak-list-list weak-list))))
72 (Assert (not (zerop len)))
74 (unless (featurep 'bdwgc)
75 (Assert (eq (length (weak-list-list weak-list))
76 (if (not reversep) 0 len)))))))
77 (let ((weak-list (make-weak-list))
78 (gc-cons-threshold most-positive-fixnum))
79 ;; Symbols created with `make-symbol' and `gensym' should be fresh
80 ;; and not referenced anywhere else. We check that no other
81 ;; references are available using a weak list.
83 ;; This statement must not be run byte-compiled, or the values
84 ;; remain referenced on the bytecode interpreter stack.
85 '(set-weak-list-list weak-list (list (make-symbol "foo") (gensym "foo"))))
86 (check-weak-list-unique weak-list)
88 ;; Equivalent test for `intern' and `gentemp'.
90 '(set-weak-list-list weak-list
91 (list (intern (ts-fresh-symbol-name "foo"))
92 (gentemp (ts-fresh-symbol-name "bar")))))
93 (check-weak-list-unique weak-list 'not)))
95 (Assert (not (intern-soft (make-symbol "foo"))))
96 (Assert (not (intern-soft (gensym "foo"))))
97 (Assert (intern-soft (intern (ts-fresh-symbol-name "foo"))))
98 (Assert (intern-soft (gentemp (ts-fresh-symbol-name "bar"))))
100 ;; Reading a symbol should intern it automatically, unless the symbol
101 ;; is marked specially.
102 (dolist (string (mapcar #'ts-fresh-symbol-name '("foo" "bar" "\\\0\\\1")))
103 (setq symbol (read string)
104 string (read (concat "\"" string "\"")))
105 (Assert (intern-soft string))
106 (Assert (intern-soft symbol))
107 (Assert (eq (intern-soft string) (intern-soft symbol))))
109 (let ((fresh (read (concat "#:" (ts-fresh-symbol-name "foo")))))
110 (Assert (not (intern-soft fresh))))
112 ;; Check #N=OBJECT and #N# read syntax.
113 (let* ((list (read "(#1=#:foo #1# #2=#:bar #2# #1# #2#)"))
120 (Assert (symbolp foo))
121 (Assert (not (intern-soft foo)))
122 (Assert-Equal (symbol-name foo) "foo")
123 (Assert (symbolp bar))
124 (Assert (not (intern-soft bar)))
125 (Assert-Equal (symbol-name bar) "bar")
127 (Assert (eq foo foo2))
128 (Assert (eq foo2 foo3))
129 (Assert (eq bar bar2))
130 (Assert (eq bar2 bar3)))
132 ;; Check #N=OBJECT and #N# print syntax.
133 (let* ((foo (make-symbol "foo"))
134 (bar (make-symbol "bar"))
135 (list (list foo foo bar bar foo bar)))
136 (let* ((print-gensym nil)
137 (printed-list (prin1-to-string list)))
138 (Assert-Equal printed-list "(foo foo bar bar foo bar)"))
139 (let* ((print-gensym t)
140 (printed-list (prin1-to-string list)))
141 (Assert-Equal printed-list "(#1=#:foo #1# #2=#:bar #2# #1# #2#)")))
143 ;;-----------------------------------------------------
145 ;;-----------------------------------------------------
147 (Check-Error setting-constant
149 (Check-Error setting-constant
152 ;;-----------------------------------------------------
153 ;; Variable indirections
154 ;;-----------------------------------------------------
158 (defvaralias 'foo 'bar)
159 (Assert (eq foo bar))
161 (Assert (eq (variable-alias 'foo) 'bar))
162 (defvaralias 'bar 'foo)
163 (Check-Error cyclic-variable-indirection
165 (Check-Error cyclic-variable-indirection
167 (defvaralias 'foo nil)
169 (defvaralias 'bar nil)
172 ;;-----------------------------------------------------
174 ;;-----------------------------------------------------
178 ;; In Elisp, a keyword is by definition a symbol beginning with `:'
179 ;; that is interned in the global obarray.
181 ;; In Elisp, a keyword is interned as any other symbol.
182 (Assert (eq (read ":foo") (intern ":foo")))
184 ;; A keyword is self-quoting and evaluates to itself.
185 (Assert (eq (eval (intern ":foo")) :foo))
187 ;; Keywords are recognized as such only if interned in the global
188 ;; obarray, and `keywordp' is aware of that.
189 (Assert (keywordp :foo))
190 (Assert (not (keywordp (intern ":foo" [0]))))
192 ;; Keywords used to be initialized at read-time, which resulted in
193 ;; (symbol-value (intern ":some-new-keyword")) signaling an error.
194 ;; Now we handle keywords at the time when the symbol is interned, so
195 ;; that (intern ":something) and (read ":something) will be
196 ;; equivalent. These tests check various operations on symbols that
197 ;; are guaranteed to be freshly interned.
199 ;; Interning a fresh keyword string should produce a regular
201 (let* ((fresh-keyword-name (ts-fresh-symbol-name ":foo"))
202 (fresh-keyword (intern fresh-keyword-name)))
203 (Assert (eq (symbol-value fresh-keyword) fresh-keyword))
204 (Assert (keywordp fresh-keyword)))
206 ;; Likewise, reading a fresh keyword string should produce a regular
208 (let* ((fresh-keyword-name (ts-fresh-symbol-name ":foo"))
209 (fresh-keyword (read fresh-keyword-name)))
210 (Assert (eq (symbol-value fresh-keyword) fresh-keyword))
211 (Assert (keywordp fresh-keyword)))
213 ;;; Assigning to keywords
215 ;; You shouldn't be able to set its value to something bogus.
216 (Check-Error setting-constant
219 ;; But, for backward compatibility, setting to the same value is OK.
221 (eq (set :foo :foo) :foo))
223 ;; Playing games with `intern' shouldn't fool us.
224 (Check-Error setting-constant
225 (set (intern ":foo" obarray) 5))
227 (eq (set (intern ":foo" obarray) :foo) :foo))
229 ;; But symbols not interned in the global obarray are not real
230 ;; keywords (in elisp):
231 (Assert (eq (set (intern ":foo" [0]) 5) 5))
233 ;;; Printing keywords
235 (let ((print-gensym t))
236 (Assert-Equal (prin1-to-string :foo) ":foo")
237 (Assert-Equal (prin1-to-string (intern ":foo")) ":foo")
238 (Assert-Equal (prin1-to-string (intern ":foo" [0])) "#::foo"))
240 (let ((print-gensym nil))
241 (Assert-Equal (prin1-to-string :foo) ":foo")
242 (Assert-Equal (prin1-to-string (intern ":foo")) ":foo")
243 (Assert-Equal (prin1-to-string (intern ":foo" [0])) ":foo"))
245 ;; #### Add many more tests for printing and reading symbols, as well
246 ;; as print-gensym and print-gensym-alist!
248 ;;-----------------------------------------------------
250 ;;-----------------------------------------------------
252 ;; Magic symbols are only half implemented. However, a subset of the
253 ;; functionality is being used to implement backward compatibility or
254 ;; clearer error messages for new features such as specifiers and
255 ;; glyphs. These tests try to test that working subset.
257 (let ((mysym (make-symbol "test-symbol"))
259 (dontusethis-set-symbol-value-handler
263 (throw 'test-tag args)))
264 (Assert (not (boundp mysym)))
265 (Assert-Equal (catch 'test-tag
267 `(,mysym (foo) set nil nil))
268 (Assert (not (boundp mysym)))
269 (dontusethis-set-symbol-value-handler
272 (lambda (&rest args) (setq save (nth 1 args))))
274 (Assert-Equal save '(foo))
275 (Assert (eq (symbol-value mysym) 'foo))
278 (let ((mysym (make-symbol "test-symbol"))
280 (dontusethis-set-symbol-value-handler
284 (throw 'test-tag args)))
285 (Assert-Equal (catch 'test-tag
287 `(,mysym nil makunbound nil nil))
288 (dontusethis-set-symbol-value-handler
291 (lambda (&rest args) (setq save (nth 2 args))))
292 (Assert (not (boundp mysym)))
295 (Assert (eq (symbol-value mysym) 'bar))
297 (Assert (not (boundp mysym)))
298 (Assert (eq save 'makunbound))
301 (when (featurep 'file-coding)
302 (Assert (eq pathname-coding-system file-name-coding-system))
303 (let ((val1 file-name-coding-system)
304 (val2 pathname-coding-system))
305 (Assert (eq val1 val2))
306 (let ((file-name-coding-system 'no-conversion-dos))
307 (Assert (eq file-name-coding-system 'no-conversion-dos))
308 (Assert (eq pathname-coding-system file-name-coding-system)))
309 (let ((pathname-coding-system 'no-conversion-mac))
310 (Assert (eq file-name-coding-system 'no-conversion-mac))
311 (Assert (eq pathname-coding-system file-name-coding-system)))
312 (Assert (eq file-name-coding-system pathname-coding-system))
313 (Assert (eq val1 file-name-coding-system)))
314 (Assert (eq pathname-coding-system file-name-coding-system)))
317 ;(let ((mysym (make-symbol "test-symbol")))
318 ; (dontusethis-set-symbol-value-handler
321 ; (lambda (&rest args)
322 ; (throw 'test-tag args)))
323 ; (Assert-Equal (catch 'test-tag
325 ; `(,mysym (foo) make-local nil nil)))