types.test 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. ;;;; types.test --- Type tag decoding. -*- mode: scheme; coding: utf-8; -*-
  2. ;;;;
  3. ;;;; Copyright (C) 2014, 2015, 2018 Free Software Foundation, Inc.
  4. ;;;;
  5. ;;;; This file is part of GNU Guile.
  6. ;;;;
  7. ;;;; GNU Guile is free software; you can redistribute it and/or modify it
  8. ;;;; under the terms of the GNU Lesser General Public License as published by
  9. ;;;; the Free Software Foundation; either version 3 of the License, or (at
  10. ;;;; your option) any later version.
  11. ;;;;
  12. ;;;; GNU Guile is distributed in the hope that it will be useful, but
  13. ;;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
  15. ;;;; General Public License for more details.
  16. ;;;;
  17. ;;;; You should have received a copy of the GNU Lesser General Public License
  18. ;;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. (define-module (test-types)
  20. #:use-module (test-suite lib)
  21. #:use-module (rnrs io ports)
  22. #:use-module (ice-9 match)
  23. #:use-module (ice-9 regex)
  24. #:use-module (ice-9 weak-vector)
  25. #:use-module (srfi srfi-1)
  26. #:use-module (srfi srfi-9)
  27. #:use-module (system foreign)
  28. #:use-module (system vm vm)
  29. #:use-module (system base types))
  30. (define-syntax test-cloneable
  31. (syntax-rules ()
  32. "Test whether each simple OBJECT is properly decoded."
  33. ((_ object rest ...)
  34. (begin
  35. (let ((obj object))
  36. (pass-if-equal (object->string obj) obj
  37. (scm->object (object-address obj))))
  38. (test-cloneable rest ...)))
  39. ((_)
  40. *unspecified*)))
  41. ;; Test objects that can be directly cloned.
  42. (with-test-prefix "clonable objects"
  43. (test-cloneable
  44. #t #f #nil (if #f #f) (eof-object)
  45. 42 (expt 2 28) 3.14
  46. "narrow string" "wide στρινγ"
  47. 'symbol 'λ
  48. #:keyword #:λ
  49. '(2 . 3) (iota 123) '(1 (two ("three")))
  50. #(1 2 3) #(foo bar baz)
  51. #vu8(255 254 253)
  52. (make-pointer 123) (make-pointer #xdeadbeef)))
  53. ;; Circular objects cannot be compared with 'equal?', so here's their
  54. ;; home.
  55. (with-test-prefix "clonable circular objects"
  56. (pass-if "list"
  57. (let* ((lst (circular-list 0 1))
  58. (result (scm->object (object-address lst))))
  59. (match result
  60. ((0 1 . self)
  61. (eq? self result)))))
  62. (pass-if "vector"
  63. (define (circular-vector)
  64. (let ((v (make-vector 3 'hey)))
  65. (vector-set! v 2 v)
  66. v))
  67. (let* ((vec (circular-vector))
  68. (result (scm->object (object-address vec))))
  69. (match result
  70. (#('hey 'hey self)
  71. (eq? self result))))))
  72. (define-syntax test-inferior-objects
  73. (syntax-rules ()
  74. "Test whether each OBJECT is recognized and wrapped as an
  75. 'inferior-object'."
  76. ((_ (object kind sub-kind-pattern) rest ...)
  77. (begin
  78. (let ((obj object))
  79. (pass-if (object->string obj)
  80. (let ((result (scm->object (object-address obj))))
  81. (and (inferior-object? result)
  82. (eq? 'kind (inferior-object-kind result))
  83. (match (inferior-object-sub-kind result)
  84. (sub-kind-pattern #t)
  85. (_ #f))))))
  86. (test-inferior-objects rest ...)))
  87. ((_)
  88. *unspecified*)))
  89. (with-test-prefix "opaque objects"
  90. (test-inferior-objects
  91. ((make-guardian) smob (? integer?))
  92. ((%make-void-port "w") port (? inferior-object?))
  93. ((open-input-string "hello") port (? inferior-object?))
  94. ((lambda () #t) program _)
  95. ((make-variable 'foo) variable _)
  96. ((make-weak-vector 3 #t) weak-vector _)
  97. ((make-weak-key-hash-table) weak-table _)
  98. ((make-weak-value-hash-table) weak-table _)
  99. ((make-doubly-weak-hash-table) weak-table _)
  100. (#2((1 2 3) (4 5 6)) array _)
  101. (#*00000110 bitvector _)
  102. ((expt 2 70) bignum _)
  103. ((make-fluid) fluid _)))
  104. (define-syntax test-inferior-ports
  105. (syntax-rules ()
  106. "Test whether each OBJECT is a port with the given TYPE-NAME."
  107. ((_ (object type-name) rest ...)
  108. (begin
  109. (pass-if-equal (object->string object)
  110. type-name
  111. (let ((result (scm->object (object-address object))))
  112. (and (eq? 'port (inferior-object-kind result))
  113. (let ((type (inferior-object-sub-kind result)))
  114. (and (eq? 'port-type (inferior-object-kind type))
  115. (inferior-object-sub-kind type))))))
  116. (test-inferior-ports rest ...)))
  117. ((_)
  118. *unspecified*)))
  119. (with-test-prefix "ports"
  120. (test-inferior-ports
  121. ((open-input-file "/dev/null") "file")
  122. ((open-output-file "/dev/null") "file")
  123. ((open-input-string "the string") "string")
  124. ((open-output-string) "string")
  125. ((open-bytevector-input-port #vu8(1 2 3 4 5)) "r6rs-bytevector-input-port")
  126. ((open-bytevector-output-port) "r6rs-bytevector-output-port")))
  127. (define-record-type <some-struct>
  128. (some-struct x y z)
  129. some-struct?
  130. (x struct-x set-struct-x!)
  131. (y struct-y)
  132. (z struct-z))
  133. (with-test-prefix "structs"
  134. (pass-if-equal "simple struct"
  135. '(<some-struct> a b c)
  136. (let* ((struct (some-struct 'a 'b 'c))
  137. (result (scm->object (object-address struct))))
  138. (and (inferior-struct? result)
  139. (cons (inferior-struct-name result)
  140. (inferior-struct-fields result)))))
  141. (pass-if "circular struct"
  142. (let ((struct (some-struct #f 'b 'c)))
  143. (set-struct-x! struct struct)
  144. (let ((result (scm->object (object-address struct))))
  145. (and (inferior-struct? result)
  146. (eq? (inferior-struct-name result) '<some-struct>)
  147. (match (inferior-struct-fields result)
  148. ((self 'b 'c)
  149. (eq? self result)))))))
  150. (pass-if "printed circular struct"
  151. (->bool
  152. (string-match "#<struct <some-struct> #0# b c [[:xdigit:]]+>"
  153. (let ((struct (some-struct #f 'b 'c)))
  154. (set-struct-x! struct struct)
  155. (object->string (scm->object (object-address struct)))))))
  156. (pass-if "printed deep circular struct"
  157. (->bool
  158. (string-match
  159. "#<struct <some-struct> \
  160. #<struct <some-struct> #-1# 3 4 [[:xdigit:]]+> \
  161. 1 2 [[:xdigit:]]+>"
  162. (let* ((a (some-struct #f 1 2))
  163. (b (some-struct a 3 4)))
  164. (set-struct-x! a b)
  165. (object->string (scm->object (object-address a))))))))