123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- ;;;; types.test --- Type tag decoding. -*- mode: scheme; coding: utf-8; -*-
- ;;;;
- ;;;; Copyright (C) 2014, 2015, 2018 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 'λ
- #:keyword #:λ
- '(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?))
- ((%make-void-port "w") port (? inferior-object?))
- ((open-input-string "hello") port (? inferior-object?))
- ((lambda () #t) program _)
- ((make-variable 'foo) variable _)
- ((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 _)
- ((make-fluid) fluid _)))
- (define-syntax test-inferior-ports
- (syntax-rules ()
- "Test whether each OBJECT is a port with the given TYPE-NAME."
- ((_ (object type-name) rest ...)
- (begin
- (pass-if-equal (object->string object)
- type-name
- (let ((result (scm->object (object-address object))))
- (and (eq? 'port (inferior-object-kind result))
- (let ((type (inferior-object-sub-kind result)))
- (and (eq? 'port-type (inferior-object-kind type))
- (inferior-object-sub-kind type))))))
- (test-inferior-ports rest ...)))
- ((_)
- *unspecified*)))
- (with-test-prefix "ports"
- (test-inferior-ports
- ((open-input-file "/dev/null") "file")
- ((open-output-file "/dev/null") "file")
- ((open-input-string "the string") "string")
- ((open-output-string) "string")
- ((open-bytevector-input-port #vu8(1 2 3 4 5)) "r6rs-bytevector-input-port")
- ((open-bytevector-output-port) "r6rs-bytevector-output-port")))
- (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))))))))
|