~ chicken-core (chicken-5) /tests/srfi-45-tests.scm
Trap1;;; Tests adapted from SRFI 45 (for "lazy" -> "delay-force").2;;; That SRFI Copyright (C) André van Tonder (2003).34(import (only chicken.format printf)5 (only chicken.port with-output-to-string))67(define *errors* 0)89(define-syntax test10 (syntax-rules ()11 ((_ name expect form)12 (let ((ok (equal? expect form)))13 (printf "(~a) ~a~n" (if ok "PASS" "FAIL") name)14 (when (not ok) (set! *errors* (add1 *errors*)))))))1516(define-syntax output17 (syntax-rules ()18 ((_ . body) (with-output-to-string (lambda () . body)))))1920(test "Memoization test 1"21 "hello"22 (output (define s (delay (begin (display 'hello) 1)))23 (force s)24 (force s)))2526(test "Memoization test 2"27 "bonjour"28 (output (let ((s (delay (begin (display 'bonjour) 2))))29 (+ (force s) (force s)))))3031(test "Memoization test 3"32 "hi"33 (output (define r (delay (begin (display 'hi) 1)))34 (define s (delay-force r))35 (define t (delay-force s))36 (force t)37 (force r)))3839(test "Memoization test 4"40 "hohohohoho"41 (output (define (stream-drop s index)42 (delay-force43 (if (zero? index)44 s45 (stream-drop (cdr (force s)) (- index 1)))))46 (define (ones)47 (delay (begin48 (display 'ho)49 (cons 1 (ones)))))50 (define s (ones))51 (car (force (stream-drop s 4)))52 (car (force (stream-drop s 4)))))5354(let ()55 (define count 0)56 (define p57 (delay (begin (set! count (+ count 1))58 (if (> count x)59 count60 (force p)))))61 (define x 5)62 (test "Reentrancy test 1 (1)" 6 (force p))63 (set! x 10)64 (test "Reentrancy test 1 (2)" 6 (force p)))6566(let ()67 (define f68 (let ((first? #t))69 (delay70 (if first?71 (begin72 (set! first? #f)73 (force f))74 'second))))75 (test "Reentrancy test 2" 'second (force f)))7677(let ()78 (define q79 (let ((count 5))80 (define (get-count) count)81 (define p (delay (if (<= count 0)82 count83 (begin (set! count (- count 1))84 (force p)85 (set! count (+ count 2))86 count))))87 (list get-count p)))88 (define get-count (car q))89 (define p (cadr q))90 (test "Reentrancy test 3 (1)" 5 (get-count))91 (test "Reentrancy test 3 (2)" 0 (force p))92 (test "Reentrancy test 3 (3)" 10 (get-count)))9394(exit *errors*)