1 ;;;; symbols.test --- test suite for Guile's symbols -*- scheme -*-
3 ;;;; Copyright (C) 2001, 2006, 2008, 2009 Free Software Foundation, Inc.
5 ;;;; This library is free software; you can redistribute it and/or
6 ;;;; modify it under the terms of the GNU Lesser General Public
7 ;;;; License as published by the Free Software Foundation; either
8 ;;;; version 3 of the License, or (at your option) any later version.
10 ;;;; This library is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;;;; Lesser General Public License for more details.
15 ;;;; You should have received a copy of the GNU Lesser General Public
16 ;;;; License along with this library; if not, write to the Free Software
17 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 (define-module (test-suite test-symbols)
20 #:use-module (test-suite lib)
21 #:use-module (ice-9 documentation))
28 (define exception:immutable-string
29 (cons 'misc-error "^string is read-only"))
31 (define (documented? object)
32 (not (not (object-documentation object))))
34 (define (symbol-length s)
35 (string-length (symbol->string s)))
41 (with-test-prefix "symbol internals"
43 (pass-if "length of new symbol same as stringbuf"
45 (= (symbol-length s) (assq-ref (%symbol-dump s) 'stringbuf-length))))
47 (pass-if "contents of new symbol same as stringbuf"
49 (string=? (symbol->string s)
50 (assq-ref (%symbol-dump s) 'stringbuf-chars))))
52 (pass-if "the null symbol is inlined"
54 (assq-ref (%symbol-dump s) 'stringbuf-inline)))
56 (pass-if "short Latin-1-encoded symbols are inlined"
58 (assq-ref (%symbol-dump s) 'stringbuf-inline)))
60 (pass-if "long Latin-1-encoded symbols are not inlined"
61 (let ((s 'x0123456789012345678901234567890123456789))
62 (not (assq-ref (%symbol-dump s) 'stringbuf-inline))))
64 (pass-if "short UCS-4-encoded symbols are not inlined"
65 (let ((s (string->symbol "\u0100")))
66 (not (assq-ref (%symbol-dump s) 'stringbuf-inline))))
68 (pass-if "long UCS-4-encoded symbols are not inlined"
69 (let ((s (string->symbol "\u010012345678901234567890123456789")))
70 (not (assq-ref (%symbol-dump s) 'stringbuf-inline))))
72 (with-test-prefix "hashes"
74 (pass-if "equal symbols have equal hashes"
77 (= (assq-ref (%symbol-dump s1) 'hash)
78 (assq-ref (%symbol-dump s2) 'hash))))
80 (pass-if "different symbols have different hashes"
83 (not (= (assq-ref (%symbol-dump s1) 'hash)
84 (assq-ref (%symbol-dump s2) 'hash))))))
86 (with-test-prefix "encodings"
88 (pass-if "the null symbol is Latin-1 encoded"
90 (not (assq-ref (%symbol-dump s) 'stringbuf-wide))))
92 (pass-if "ASCII symbols are Latin-1 encoded"
94 (not (assq-ref (%symbol-dump s) 'stringbuf-wide))))
96 (pass-if "Latin-1 symbols are Latin-1 encoded"
97 (let ((s (string->symbol "\xC0\xC1\xC2")))
98 (not (assq-ref (%symbol-dump s) 'stringbuf-wide))))
100 (pass-if "BMP symbols are UCS-4 encoded"
101 (let ((s (string->symbol "\u0100\u0101\x0102")))
102 (assq-ref (%symbol-dump s) 'stringbuf-wide)))
104 (pass-if "SMP symbols are UCS-4 encoded"
105 (let ((s (string->symbol "\U010300\u010301\x010302")))
106 (assq-ref (%symbol-dump s) 'stringbuf-wide)))))
112 (with-test-prefix "symbol?"
114 (pass-if "documented?"
115 (documented? symbol?))
118 (not (symbol? "foo")))
127 (with-test-prefix "BMP symbols"
129 (pass-if "BMP symbol's string"
130 (and (= 4 (string-length "abc\u0100"))
131 (string=? "abc\u0100"
132 (symbol->string (string->symbol "abc\u0100"))))))
138 (with-test-prefix "symbol->string"
140 (pass-if-exception "result is an immutable string"
141 exception:immutable-string
142 (string-set! (symbol->string 'abc) 1 #\space)))
149 (with-test-prefix "gensym"
151 (pass-if "documented?"
152 (documented? gensym))
154 (pass-if "produces a symbol"
157 (pass-if "produces a fresh symbol"
158 (not (eq? (gensym) (gensym))))
160 (pass-if "accepts a string prefix"
161 (symbol? (gensym "foo")))
163 (pass-if-exception "does not accept a symbol prefix"
164 exception:wrong-type-arg
167 (pass-if "accepts long prefices"
168 (symbol? (gensym (make-string 4000 #\!))))
170 (pass-if "accepts embedded NULs"
171 (> (string-length (symbol->string (gensym "foo\0bar\0braz\0foo\0bar\0braz\0foo\0bar\0braz\0foo\0bar\0braz\0foo\0bar\0braz\0foo\0bar\0braz\0"))) 6)))