Debian Codegraph @

test-suite/tests/structs.test

raw
1 ;;;; structs.test --- Structures. -*- mode: scheme; coding: utf-8; -*-
2 ;;;; Ludovic Courtès <[email protected]>, 2006-06-12.
3 ;;;;
4 ;;;; Copyright (C) 2006, 2007, 2009, 2010, 2012, 2017 Free Software Foundation, Inc.
5 ;;;;
6 ;;;; This library is free software; you can redistribute it and/or
7 ;;;; modify it under the terms of the GNU Lesser General Public
8 ;;;; License as published by the Free Software Foundation; either
9 ;;;; version 3 of the License, or (at your option) any later version.
10 ;;;;
11 ;;;; This library is distributed in the hope that it will be useful,
12 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ;;;; Lesser General Public License for more details.
15 ;;;;
16 ;;;; You should have received a copy of the GNU Lesser General Public
17 ;;;; License along with this library; if not, write to the Free Software
18 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
20 (define-module (test-suite test-structs)
21 :use-module (test-suite lib))
22
23
24
25 ;;;
26 ;;; Struct example taken from the reference manual (by Tom Lord).
27 ;;;
28
29 (define ball-root
30 (make-vtable (string-append standard-vtable-fields "pw") 0))
31
32 (define (make-ball-type ball-color)
33 (make-struct/no-tail ball-root
34 (make-struct-layout "pw")
35 (lambda (ball port)
36 (format port "#<a ~A ball owned by ~A>"
37 (color ball)
38 (owner ball)))
39 ball-color))
40
41 (define (color ball) (struct-ref (struct-vtable ball) vtable-offset-user))
42 (define (owner ball) (struct-ref ball 0))
43 (define (set-owner! ball owner) (struct-set! ball 0 owner))
44
45 (define red (make-ball-type 'red))
46 (define green (make-ball-type 'green))
47
48 (define (make-ball type owner) (make-struct/no-tail type owner))
49
50
51
52 ;;;
53 ;;; Test suite.
54 ;;;
55
56 (with-test-prefix "low-level struct procedures"
57
58 (pass-if "constructors"
59 (and (struct-vtable? ball-root)
60 (struct-vtable? red)
61 (struct-vtable? green)))
62
63 (pass-if "vtables"
64 (and (eq? (struct-vtable red) ball-root)
65 (eq? (struct-vtable green) ball-root)
66 (eq? (struct-vtable (make-ball red "Bob")) red)
67 (eq? (struct-vtable ball-root) <standard-vtable>)
68
69 ;; end of the vtable tower
70 (eq? (struct-vtable <standard-vtable>) <standard-vtable>)))
71
72 (pass-if "write"
73 (set-owner! (make-ball red "Bob") "Fred")
74 #t)
75
76 (pass-if "struct-set!"
77 (let ((ball (make-ball green "Bob")))
78 (set-owner! ball "Bill")
79 (string=? (owner ball) "Bill")))
80
81 (pass-if "struct-ref"
82 (let ((ball (make-ball red "Alice")))
83 (equal? (struct-ref ball 0) "Alice")))
84
85 (pass-if "struct-set!"
86 (let* ((v (make-vtable "pw"))
87 (s (make-struct/no-tail v))
88 (r (struct-set! s 0 'a)))
89 (eq? r
90 (struct-ref s 0)
91 'a)))
92
93 (pass-if-exception "struct-ref out-of-range"
94 exception:out-of-range
95 (let* ((v (make-vtable "pwpw"))
96 (s (make-struct/no-tail v 'a 'b)))
97 (struct-ref s 2)))
98
99 (pass-if-exception "struct-set! out-of-range"
100 exception:out-of-range
101 (let* ((v (make-vtable "pwpw"))
102 (s (make-struct/no-tail v 'a 'b)))
103 (struct-set! s 2 'c))))
104
105
106 (with-test-prefix "equal?"
107
108 (pass-if "simple structs"
109 (let* ((vtable (make-vtable "pw"))
110 (s1 (make-struct/no-tail vtable "hello"))
111 (s2 (make-struct/no-tail vtable "hello")))
112 (equal? s1 s2)))
113
114 (pass-if "more complex structs"
115 (let ((first (make-ball red (string-copy "Bob")))
116 (second (make-ball red (string-copy "Bob"))))
117 (equal? first second)))
118
119 (pass-if "not-equal?"
120 (not (or (equal? (make-ball red "Bob") (make-ball green "Bob"))
121 (equal? (make-ball red "Bob") (make-ball red "Bill"))))))
122
123
124 (with-test-prefix "hash"
125
126 (pass-if "simple structs"
127 (let* ((v (make-vtable "pw"))
128 (s1 (make-struct/no-tail v "hello"))
129 (s2 (make-struct/no-tail v "hello")))
130 (= (hash s1 7777) (hash s2 7777))))
131
132 (pass-if "different structs"
133 (let* ((v (make-vtable "pw"))
134 (s1 (make-struct/no-tail v "hello"))
135 (s2 (make-struct/no-tail v "world")))
136 (or (not (= (hash s1 7777) (hash s2 7777)))
137 (throw 'unresolved))))
138
139 (pass-if "different struct types"
140 (let* ((v1 (make-vtable "pw"))
141 (v2 (make-vtable "pw"))
142 (s1 (make-struct/no-tail v1 "hello"))
143 (s2 (make-struct/no-tail v2 "hello")))
144 (or (not (= (hash s1 7777) (hash s2 7777)))
145 (throw 'unresolved))))
146
147 (pass-if "more complex structs"
148 (let ((s1 (make-ball red (string-copy "Bob")))
149 (s2 (make-ball red (string-copy "Bob"))))
150 (= (hash s1 7777) (hash s2 7777))))
151
152 (pass-if "struct with weird fields"
153 (let* ((v (make-vtable "pwuwph"))
154 (s1 (make-struct/no-tail v "hello" 123 "invisible-secret1"))
155 (s2 (make-struct/no-tail v "hello" 123 "invisible-secret2")))
156 (= (hash s1 7777) (hash s2 7777))))
157
158 (pass-if "cyclic structs"
159 (let* ((v (make-vtable "pw"))
160 (a (make-struct/no-tail v #f))
161 (b (make-struct/no-tail v a)))
162 (struct-set! a 0 b)
163 (and (hash a 7777) (hash b 7777) #t))))
164
165
166 ;;
167 ;; make-struct
168 ;;
169
170 (with-test-prefix "make-struct"
171
172 ;; in guile 1.8.1 and earlier, this caused an error throw out of an
173 ;; SCM_CRITICAL_SECTION_START / SCM_CRITICAL_SECTION_END, which abort()ed
174 ;; the program
175 ;;
176 (pass-if-exception "wrong type for `u' field" exception:wrong-type-arg
177 (let* ((vv (make-vtable standard-vtable-fields))
178 (v (make-struct/no-tail vv (make-struct-layout "uw"))))
179 (make-struct/no-tail v 'x))))
180
181 ;;
182 ;; make-vtable
183 ;;
184
185 (with-test-prefix "make-vtable"
186
187 (pass-if "without printer"
188 (let* ((vtable (make-vtable "pwpw"))
189 (struct (make-struct/no-tail vtable 'x 'y)))
190 (and (eq? 'x (struct-ref struct 0))
191 (eq? 'y (struct-ref struct 1)))))
192
193 (pass-if "with printer"
194 (let ()
195 (define (print struct port)
196 (display "hello" port))
197
198 (let* ((vtable (make-vtable "pwpw" print))
199 (struct (make-struct/no-tail vtable 'x 'y))
200 (str (call-with-output-string
201 (lambda (port)
202 (display struct port)))))
203 (equal? str "hello")))))