summaryrefslogtreecommitdiff
path: root/test-suite/tests/types.test
blob: ea71d3ceb320775af815d70d90a3026d656e750a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
;;;; types.test --- Type tag decoding.      -*- mode: scheme; coding: utf-8; -*-
;;;;
;;;; 	Copyright (C) 2014 Free Software Foundation, Inc.
;;;;
;;;; This file is part of GNU Guile.
;;;;
;;;; GNU Guile is free software; you can redistribute it and/or modify it
;;;; under the terms of the GNU Lesser General Public License as published by
;;;; the Free Software Foundation; either version 3 of the License, or (at
;;;; your option) any later version.
;;;;
;;;; GNU Guile is distributed in the hope that it will be useful, but
;;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
;;;; General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU Lesser General Public License
;;;; along with this program.  If not, see <http://www.gnu.org/licenses/>.

(define-module (test-types)
  #:use-module (test-suite lib)
  #:use-module (rnrs io ports)
  #:use-module (ice-9 match)
  #:use-module (ice-9 regex)
  #:use-module (ice-9 weak-vector)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-9)
  #:use-module (system foreign)
  #:use-module (system vm vm)
  #:use-module (system base types))

(define-syntax test-cloneable
  (syntax-rules ()
    "Test whether each simple OBJECT is properly decoded."
    ((_ object rest ...)
     (begin
       (let ((obj object))
         (pass-if-equal (object->string obj) obj
           (scm->object (object-address obj))))
       (test-cloneable rest ...)))
    ((_)
     *unspecified*)))

;; Test objects that can be directly cloned.
(with-test-prefix "clonable objects"
  (test-cloneable
   #t #f #nil (if #f #f) (eof-object)
   42 (expt 2 28) 3.14
   "narrow string" "wide στρινγ"
   'symbol 'λ
   ;; NB: keywords are SMOBs.
   '(2 . 3) (iota 123) '(1 (two ("three")))
   #(1 2 3) #(foo bar baz)
   #vu8(255 254 253)
   (make-pointer 123) (make-pointer #xdeadbeef)))

;; Circular objects cannot be compared with 'equal?', so here's their
;; home.
(with-test-prefix "clonable circular objects"

  (pass-if "list"
    (let* ((lst    (circular-list 0 1))
           (result (scm->object (object-address lst))))
      (match result
        ((0 1 . self)
         (eq? self result)))))

  (pass-if "vector"
    (define (circular-vector)
      (let ((v (make-vector 3 'hey)))
        (vector-set! v 2 v)
        v))

    (let* ((vec    (circular-vector))
           (result (scm->object (object-address vec))))
      (match result
        (#('hey 'hey self)
         (eq? self result))))))

(define-syntax test-inferior-objects
  (syntax-rules ()
    "Test whether each OBJECT is recognized and wrapped as an
'inferior-object'."
    ((_ (object kind sub-kind-pattern) rest ...)
     (begin
       (let ((obj object))
         (pass-if (object->string obj)
           (let ((result (scm->object (object-address obj))))
             (and (inferior-object? result)
                  (eq? 'kind (inferior-object-kind result))
                  (match (inferior-object-sub-kind result)
                    (sub-kind-pattern #t)
                    (_ #f))))))
       (test-inferior-objects rest ...)))
    ((_)
     *unspecified*)))

(with-test-prefix "opaque objects"
  (test-inferior-objects
   ((make-guardian) smob (? integer?))
   (#:keyword smob (? integer?))
   ((%make-void-port "w") port (? integer?))
   ((open-input-string "hello") port (? integer?))
   ((lambda () #t) program _)
   ((make-weak-vector 3 #t) weak-vector _)
   ((make-weak-key-hash-table) weak-table _)
   ((make-weak-value-hash-table) weak-table _)
   ((make-doubly-weak-hash-table) weak-table _)
   (#2((1 2 3) (4 5 6)) array _)
   (#*00000110 bitvector _)
   ((expt 2 70) bignum _))

  (pass-if "fluid"
    (let ((fluid (make-fluid)))
      (inferior-fluid? (scm->object (object-address fluid))))))

(define-record-type <some-struct>
  (some-struct x y z)
  some-struct?
  (x struct-x set-struct-x!)
  (y struct-y)
  (z struct-z))

(with-test-prefix "structs"

  (pass-if-equal "simple struct"
      '(<some-struct> a b c)
    (let* ((struct (some-struct 'a 'b 'c))
           (result (scm->object (object-address struct))))
      (and (inferior-struct? result)
           (cons (inferior-struct-name result)
                 (inferior-struct-fields result)))))

  (pass-if "circular struct"
    (let ((struct (some-struct #f 'b 'c)))
      (set-struct-x! struct struct)
      (let ((result (scm->object (object-address struct))))
        (and (inferior-struct? result)
             (eq? (inferior-struct-name result) '<some-struct>)
             (match (inferior-struct-fields result)
               ((self 'b 'c)
                (eq? self result)))))))

  (pass-if "printed circular struct"
    (->bool
     (string-match "#<struct <some-struct> #0# b c [[:xdigit:]]+>"
                   (let ((struct (some-struct #f 'b 'c)))
                     (set-struct-x! struct struct)
                     (object->string (scm->object (object-address struct)))))))

  (pass-if "printed deep circular struct"
    (->bool
     (string-match
      "#<struct <some-struct> \
#<struct <some-struct> #-1# 3 4 [[:xdigit:]]+> \
1 2 [[:xdigit:]]+>"
      (let* ((a (some-struct #f 1 2))
             (b (some-struct a 3 4)))
        (set-struct-x! a b)
        (object->string (scm->object (object-address a))))))))