symbols.test 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. ;;;; symbols.test --- test suite for Guile's symbols -*- scheme -*-
  2. ;;;;
  3. ;;;; Copyright (C) 2001, 2006, 2008, 2009, 2011 Free Software Foundation, Inc.
  4. ;;;;
  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.
  9. ;;;;
  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.
  14. ;;;;
  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
  18. (define-module (test-suite test-symbols)
  19. #:use-module (test-suite lib)
  20. #:use-module (ice-9 documentation))
  21. ;;;
  22. ;;; miscellaneous
  23. ;;;
  24. (define exception:immutable-string
  25. (cons 'misc-error "^string is read-only"))
  26. (define (documented? object)
  27. (not (not (object-documentation object))))
  28. (define (symbol-length s)
  29. (string-length (symbol->string s)))
  30. ;;
  31. ;; symbol internals
  32. ;;
  33. (with-test-prefix "symbol internals"
  34. (pass-if "length of new symbol same as stringbuf"
  35. (let ((s 'def))
  36. (= (symbol-length s) (assq-ref (%symbol-dump s) 'stringbuf-length))))
  37. (pass-if "contents of new symbol same as stringbuf"
  38. (let ((s 'ghi))
  39. (string=? (symbol->string s)
  40. (assq-ref (%symbol-dump s) 'stringbuf-chars))))
  41. (with-test-prefix "hashes"
  42. (pass-if "equal symbols have equal hashes"
  43. (let ((s1 'mux)
  44. (s2 'mux))
  45. (= (assq-ref (%symbol-dump s1) 'hash)
  46. (assq-ref (%symbol-dump s2) 'hash))))
  47. (pass-if "different symbols have different hashes"
  48. (let ((s1 'mux)
  49. (s2 'muy))
  50. (not (= (assq-ref (%symbol-dump s1) 'hash)
  51. (assq-ref (%symbol-dump s2) 'hash))))))
  52. (with-test-prefix "encodings"
  53. (pass-if "the null symbol is Latin-1 encoded"
  54. (let ((s '#{}#))
  55. (not (assq-ref (%symbol-dump s) 'stringbuf-wide))))
  56. (pass-if "ASCII symbols are Latin-1 encoded"
  57. (let ((s 'jkl))
  58. (not (assq-ref (%symbol-dump s) 'stringbuf-wide))))
  59. (pass-if "Latin-1 symbols are Latin-1 encoded"
  60. (let ((s (string->symbol "\xC0\xC1\xC2")))
  61. (not (assq-ref (%symbol-dump s) 'stringbuf-wide))))
  62. (pass-if "BMP symbols are UCS-4 encoded"
  63. (let ((s (string->symbol "\u0100\u0101\x0102")))
  64. (assq-ref (%symbol-dump s) 'stringbuf-wide)))
  65. (pass-if "SMP symbols are UCS-4 encoded"
  66. (let ((s (string->symbol "\U010300\u010301\x010302")))
  67. (assq-ref (%symbol-dump s) 'stringbuf-wide)))))
  68. ;;;
  69. ;;; symbol?
  70. ;;;
  71. (with-test-prefix "symbol?"
  72. (pass-if "documented?"
  73. (documented? symbol?))
  74. (pass-if "string"
  75. (not (symbol? "foo")))
  76. (pass-if "symbol"
  77. (symbol? 'foo)))
  78. ;;;
  79. ;;; wide symbols
  80. ;;;
  81. (with-test-prefix "BMP symbols"
  82. (pass-if "BMP symbol's string"
  83. (and (= 4 (string-length "abc\u0100"))
  84. (string=? "abc\u0100"
  85. (symbol->string (string->symbol "abc\u0100"))))))
  86. ;;;
  87. ;;; symbol->string
  88. ;;;
  89. (with-test-prefix "symbol->string"
  90. (pass-if-exception "result is an immutable string"
  91. exception:immutable-string
  92. (string-set! (symbol->string 'abc) 1 #\space)))
  93. ;;;
  94. ;;; gensym
  95. ;;;
  96. (with-test-prefix "gensym"
  97. (pass-if "documented?"
  98. (documented? gensym))
  99. (pass-if "produces a symbol"
  100. (symbol? (gensym)))
  101. (pass-if "produces a fresh symbol"
  102. (not (eq? (gensym) (gensym))))
  103. (pass-if "accepts a string prefix"
  104. (symbol? (gensym "foo")))
  105. (pass-if-exception "does not accept a symbol prefix"
  106. exception:wrong-type-arg
  107. (gensym 'foo))
  108. (pass-if "accepts long prefices"
  109. (symbol? (gensym (make-string 4000 #\!))))
  110. (pass-if "accepts embedded NULs"
  111. (> (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)))
  112. (with-test-prefix "extended read syntax"
  113. (pass-if (equal? "#{}#" (object->string (string->symbol ""))))
  114. (pass-if (equal? "a" (object->string (string->symbol "a"))))
  115. (pass-if (equal? "#{a b}#" (object->string (string->symbol "a b"))))
  116. (pass-if (equal? "#{\\x7d;}#" (object->string (string->symbol "}")))))