1 ;;; -*- mode: scheme; coding: utf-8; -*-
3 ;; Copyright André van Tonder. All Rights Reserved.
5 ;; Permission is hereby granted, free of charge, to any person
6 ;; obtaining a copy of this software and associated documentation
7 ;; files (the "Software"), to deal in the Software without
8 ;; restriction, including without limitation the rights to use, copy,
9 ;; modify, merge, publish, distribute, sublicense, and/or sell copies
10 ;; of the Software, and to permit persons to whom the Software is
11 ;; furnished to do so, subject to the following conditions:
13 ;; The above copyright notice and this permission notice shall be
14 ;; included in all copies or substantial portions of the Software.
16 ;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 ;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 ;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 ;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 ;; BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 ;; ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 ;; CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 ;; Modified by Andreas Rottmann for Guile.
27 (define-module (test-srfi-45)
28 #:use-module (test-suite lib)
29 #:use-module (srfi srfi-45))
31 (define-syntax test-output
34 (let ((output (call-with-output-string proc)))
35 (pass-if (equal? expected output))))))
37 (define-syntax test-equal
40 (pass-if (equal? expected expr)))))
42 (define test-leaks? #f)
44 (define-syntax test-leak
48 (display "Leak test, please watch memory consumption;")
49 (display " press C-c when satisfied.\n")
52 (sigaction SIGINT (lambda (signal) (k #t)))
55 ;=========================================================================
56 ; TESTS AND BENCHMARKS:
57 ;=========================================================================
59 ;=========================================================================
64 (define s (delay (begin (display 'hello port) 1)))
65 (test-equal 1 (force s))
66 (test-equal 1 (force s))))
68 ;=========================================================================
71 (test-output "bonjour"
73 (let ((s (delay (begin (display 'bonjour port) 2))))
74 (test-equal 4 (+ (force s) (force s))))))
76 ;=========================================================================
77 ; Memoization test 3: (pointed out by Alejandro Forero Cuervo)
81 (define r (delay (begin (display 'hi port) 1)))
84 (test-equal 1 (force t))
85 (test-equal 1 (force r))))
87 ;=========================================================================
88 ; Memoization test 4: Stream memoization
90 (define (stream-drop s index)
94 (stream-drop (cdr (force s)) (- index 1)))))
99 (cons 1 (ones port)))))
101 (test-output "hohohohoho"
103 (define s (ones port))
105 (car (force (stream-drop s 4))))
107 (car (force (stream-drop s 4))))))
109 ;=========================================================================
110 ; Reentrancy test 1: from R5RS
113 (p (delay (begin (set! count (+ count 1))
118 (test-equal 6 (force p))
120 (test-equal 6 (force p)))
122 ;=========================================================================
123 ; Reentrancy test 2: from SRFI 40
125 (letrec ((f (let ((first? #t))
132 (test-equal 'second (force f)))
134 ;=========================================================================
135 ; Reentrancy test 3: due to John Shutt
137 (let* ((q (let ((count 5))
138 (define (get-count) count)
139 (define p (delay (if (<= count 0)
141 (begin (set! count (- count 1))
143 (set! count (+ count 2))
149 (test-equal 5 (get-count))
150 (test-equal 0 (force p))
151 (test-equal 10 (get-count)))
153 ;=========================================================================
154 ; Test leaks: All the leak tests should run in bounded space.
156 ;=========================================================================
157 ; Leak test 1: Infinite loop in bounded space.
159 (define (loop) (lazy (loop)))
160 (test-leak (force (loop))) ;==> bounded space
162 ;=========================================================================
163 ; Leak test 2: Pending memos should not accumulate
164 ; in shared structures.
168 (test-leak (force s))) ;==> bounded space
170 ;=========================================================================
171 ; Leak test 3: Safely traversing infinite stream.
174 (delay (cons n (from (+ n 1)))))
177 (lazy (traverse (cdr (force s)))))
179 (test-leak (force (traverse (from 0)))) ;==> bounded space
181 ;=========================================================================
182 ; Leak test 4: Safely traversing infinite stream
183 ; while pointer to head of result exists.
186 (define s (traverse (from 0)))
187 (test-leak (force s))) ;==> bounded space
189 ;=========================================================================
190 ; Convenient list deconstructor used below.
198 (cond ((null? lst) exp1)
199 ((pair? lst) (let ((h (car lst))
202 (else 'match-error))))))
204 ;========================================================================
205 ; Leak test 5: Naive stream-filter should run in bounded space.
208 (define (stream-filter p? s)
209 (lazy (match (force s)
212 (delay (cons h (stream-filter p? t)))
213 (stream-filter p? t))))))
216 (force (stream-filter (lambda (n) (= n 10000000000))
217 (from 0)))) ;==> bounded space
219 ;========================================================================
220 ; Leak test 6: Another long traversal should run in bounded space.
222 ; The stream-ref procedure below does not strictly need to be lazy.
223 ; It is defined lazy for the purpose of testing safe compostion of
224 ; lazy procedures in the times3 benchmark below (previous
225 ; candidate solutions had failed this).
227 (define (stream-ref s index)
231 ((h . t) (if (zero? index)
233 (stream-ref t (- index 1)))))))
235 ; Check that evenness is correctly implemented - should terminate:
238 (force (stream-ref (stream-filter zero? (from 0))
241 ;; Commented out since it takes too long
244 (define s (stream-ref (from 0) 100000000))
245 (test-equal 100000000 (force s))) ;==> bounded space
247 ;======================================================================
248 ; Leak test 7: Infamous example from SRFI 40.
251 (stream-ref (stream-filter
252 (lambda (x) (zero? (modulo x n)))
256 (test-equal 21 (force (times3 7)))
258 ;; Commented out since it takes too long
260 (test-equal 300000000 (force (times3 100000000))) ;==> bounded space