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 ;; symbol->string isn't ready for UCS-4 yet
66 ;;(pass-if "short UCS-4-encoded symbols are not inlined"
67 ;; (let ((s (string->symbol "\u0100")))
68 ;; (not (assq-ref (%symbol-dump s) 'stringbuf-inline))))
70 ;;(pass-if "long UCS-4-encoded symbols are not inlined"
71 ;; (let ((s (string->symbol "\u010012345678901234567890123456789")))
72 ;; (not (assq-ref (%symbol-dump s) 'stringbuf-inline))))
74 (with-test-prefix "hashes"
76 (pass-if "equal symbols have equal hashes"
79 (= (assq-ref (%symbol-dump s1) 'hash)
80 (assq-ref (%symbol-dump s2) 'hash))))
82 (pass-if "different symbols have different hashes"
85 (not (= (assq-ref (%symbol-dump s1) 'hash)
86 (assq-ref (%symbol-dump s2) 'hash))))))
88 (with-test-prefix "encodings"
90 (pass-if "the null symbol is Latin-1 encoded"
92 (not (assq-ref (%symbol-dump s) 'stringbuf-wide))))
94 (pass-if "ASCII symbols are Latin-1 encoded"
96 (not (assq-ref (%symbol-dump s) 'stringbuf-wide))))
98 (pass-if "Latin-1 symbols are Latin-1 encoded"
99 (let ((s (string->symbol "\xC0\xC1\xC2")))
100 (not (assq-ref (%symbol-dump s) 'stringbuf-wide))))
102 ;; symbol->string isn't ready for UCS-4 yet
104 ;;(pass-if "BMP symbols are UCS-4 encoded"
105 ;; (let ((s (string->symbol "\u0100\u0101\x0102")))
106 ;; (assq-ref (%symbol-dump s) 'stringbuf-wide)))
108 ;;(pass-if "SMP symbols are UCS-4 encoded"
109 ;; (let ((s (string->symbol "\U010300\u010301\x010302")))
110 ;; (assq-ref (%symbol-dump s) 'stringbuf-wide)))
117 (with-test-prefix "symbol?"
119 (pass-if "documented?"
120 (documented? symbol?))
123 (not (symbol? "foo")))
133 (with-test-prefix "symbol->string"
135 (pass-if-exception "result is an immutable string"
136 exception:immutable-string
137 (string-set! (symbol->string 'abc) 1 #\space)))
144 (with-test-prefix "gensym"
146 (pass-if "documented?"
147 (documented? gensym))
149 (pass-if "produces a symbol"
152 (pass-if "produces a fresh symbol"
153 (not (eq? (gensym) (gensym))))
155 (pass-if "accepts a string prefix"
156 (symbol? (gensym "foo")))
158 (pass-if-exception "does not accept a symbol prefix"
159 exception:wrong-type-arg
162 (pass-if "accepts long prefices"
163 (symbol? (gensym (make-string 4000 #\!))))
165 (pass-if "accepts embedded NULs"
166 (> (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)))