1 ;;;; symbols.test --- test suite for Guile's symbols -*- scheme -*-
3 ;;;; Copyright (C) 2001, 2006, 2008, 2009, 2011 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))))
53 (with-test-prefix "hashes"
55 (pass-if "equal symbols have equal hashes"
58 (= (assq-ref (%symbol-dump s1) 'hash)
59 (assq-ref (%symbol-dump s2) 'hash))))
61 (pass-if "different symbols have different hashes"
64 (not (= (assq-ref (%symbol-dump s1) 'hash)
65 (assq-ref (%symbol-dump s2) 'hash))))))
67 (with-test-prefix "encodings"
69 (pass-if "the null symbol is Latin-1 encoded"
71 (not (assq-ref (%symbol-dump s) 'stringbuf-wide))))
73 (pass-if "ASCII symbols are Latin-1 encoded"
75 (not (assq-ref (%symbol-dump s) 'stringbuf-wide))))
77 (pass-if "Latin-1 symbols are Latin-1 encoded"
78 (let ((s (string->symbol "\xC0\xC1\xC2")))
79 (not (assq-ref (%symbol-dump s) 'stringbuf-wide))))
81 (pass-if "BMP symbols are UCS-4 encoded"
82 (let ((s (string->symbol "\u0100\u0101\x0102")))
83 (assq-ref (%symbol-dump s) 'stringbuf-wide)))
85 (pass-if "SMP symbols are UCS-4 encoded"
86 (let ((s (string->symbol "\U010300\u010301\x010302")))
87 (assq-ref (%symbol-dump s) 'stringbuf-wide)))))
93 (with-test-prefix "symbol?"
95 (pass-if "documented?"
96 (documented? symbol?))
99 (not (symbol? "foo")))
108 (with-test-prefix "BMP symbols"
110 (pass-if "BMP symbol's string"
111 (and (= 4 (string-length "abc\u0100"))
112 (string=? "abc\u0100"
113 (symbol->string (string->symbol "abc\u0100"))))))
119 (with-test-prefix "symbol->string"
121 (pass-if-exception "result is an immutable string"
122 exception:immutable-string
123 (string-set! (symbol->string 'abc) 1 #\space)))
130 (with-test-prefix "gensym"
132 (pass-if "documented?"
133 (documented? gensym))
135 (pass-if "produces a symbol"
138 (pass-if "produces a fresh symbol"
139 (not (eq? (gensym) (gensym))))
141 (pass-if "accepts a string prefix"
142 (symbol? (gensym "foo")))
144 (pass-if-exception "does not accept a symbol prefix"
145 exception:wrong-type-arg
148 (pass-if "accepts long prefices"
149 (symbol? (gensym (make-string 4000 #\!))))
151 (pass-if "accepts embedded NULs"
152 (> (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)))
154 (with-test-prefix "extended read syntax"
155 (pass-if (equal? "#{}#" (object->string (string->symbol ""))))
156 (pass-if (equal? "a" (object->string (string->symbol "a"))))
157 (pass-if (equal? "#{a b}#" (object->string (string->symbol "a b"))))
158 (pass-if (equal? "#{\\x7d;}#" (object->string (string->symbol "}")))))